Embedded Security as a Future Topic (Secure Boot, TrustZone)
June 11, 2024Threat modeling, secure boot chains and lifecycle updates for device fleets. Monorepo vs. multirepo, vendor drops and tooling for release traces. Unit tests (host/target), mocks/stubs, HIL and measuring release quality. When to use which protocol? Edge-to-cloud paths with practical patterns and pitfalls.Embedded Security Meets Signal Chain: Why ADC/DAC Must Be Both Secure and Precise Today
1) A Brief Overview: ADC/DAC in Everyday Embedded Work
- Resolution (bits): determines the LSB size (Vref/2^N).
- Sampling rate fs: determines how fast you measure (Nyquist: >= 2*fmax of the signal).
- Quantization/ENOB: real effective bits < datasheet bits; can be improved via oversampling & averaging (e.g. +1 bit at 4x, +2 bits at 16x). (Silicon Labs, Texas Instruments)
2) Anti-Aliasing & Signal Conditioning: The Mandatory Program
- The input source must charge the ADC's sample-and-hold capacitor quickly -- high source impedance leads to erroneous samples. Good practical explanations are available on this topic. (Embedded Related)
- RC filter close to the pin, star grounding, short traces, dedicated VDDA/VSSA (decoupling!). (EDN)
3) Oversampling & Averaging: "Free" Bits
4) Practical Example 1: STM32 -- ADC with DMA (Timer-Triggered)
C
// Global buffers
#define ADC_BUF 1024
volatile uint16_t adc_buf[ADC_BUF];
// Init: ADC (Scan, Continuous off), external trigger e.g. TIM3_TRGO
// DMA: Peripheral->Memory, Half/Full Transfer IRQ, Circular
// Timer: Upcounter, Update Event as TRGO, Rate = desired fs
void start_adc_dma(void) {
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adc_buf, ADC_BUF);
HAL_TIM_Base_Start(&htim3); // provides TRGO to ADC
}
// Callbacks: deterministic processing in blocks
void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef *hadc) {
// process adc_buf[0 .. ADC_BUF/2-1]
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef *hadc) {
// process adc_buf[ADC_BUF/2 .. ADC_BUF-1]
}
5) Practical Example 2: Arduino -- ADC Basics & Voltage Calculation
Cpp
const int PIN = A0;
const float VREF = 5.0; // or 3.3V -- depends on board/analogReference()
void setup() {
Serial.begin(115200);
}
void loop() {
const int N = 32; // simple averaging
long acc = 0;
for (int i=0; i<N; ++i) acc += analogRead(PIN);
float avg = acc / float(N); // 10-bit -> 0..1023
float volt = avg * (VREF / 1023.0);
Serial.println(volt);
}
6) Practical Example 3: ESP32 -- DAC Output (For Tests/HIL)
C
#include "driver/dac.h"
void app_main(void) {
dac_output_enable(DAC_CHANNEL_1); // GPIO25
for (;;) {
for (int v=0; v<256; ++v) {
dac_output_voltage(DAC_CHANNEL_1, v); // 0..255 -> 0..Vref
}
}
}
7) Reproducible Measurements: Checklist
- Reference voltage (Vref): internal vs. external, temperature drift/noise; decouple separately, short return path to ADC ground. Poor Vref ruins any calibration; app notes on oversampling and practical articles show effects and countermeasures. (Silicon Labs, EDN)
- Layout: Separate AGND/DGND sensibly, star topology, no return currents crossing the measurement front; filter close to the pin. (EDN)
- Oversampling/Averaging: Sample N times, use averaged values; for high dynamics, use adaptive averaging. Formal bit gains as described above. (Silicon Labs)
- DMA double buffer: deterministic blocks; with caches: use non-cacheable regions. (STMicroelectronics)
- Calibration: 2-point (offset/gain), characterize across temperature.
- Anti-aliasing: Bessel for time-domain signals (clean phase), Butterworth for spectral separation; TI/ADI provide concrete dimensioning. (Texas Instruments, Analog Devices)
8) Security Side: Secure Boot, TrustZone-M & Lifecycle Updates
9) Edge-to-Cloud Paths: Choosing Protocols Pragmatically
- MQTT (TCP/TLS, Pub/Sub): excellent for telemetry, "last will", QoS 0/1/2; ideal for gateway/cloud integration, v5 brings better error reporting and features. (OASIS Open Docs, mqtt.org)
- CoAP (UDP/DTLS, REST-like): lightweight, Observe/Blockwise; well-suited for constrained networks, IPv6/6LoWPAN. (IETF Datatracker)
- HTTP/2/3: updates/downloads, broad infrastructure -- with TLS/mTLS and resume mechanisms.
10) Engineering Workflow: Monorepo? Vendor Drops? Release Traces!
- Monorepo vs. multirepo: Monorepo simplifies cross-cutting changes, multirepo isolates components/products. There is no dogma -- tooling and team size decide. Martin Fowler examines the trade-offs and branching patterns. (martinfowler.com)
- Vendor drops (HAL/SDK): pull in as submodules/mirrors with immutable tags; reference changelogs; no manual patch mixing in the project root.
- Release traces: build SBOMs (e.g. CycloneDX) and provenance (SLSA) directly into the pipeline -- artifacts can later be traced back to sources/builds unambiguously. Invaluable for IoT fleets (forensics, CVE matching). (CycloneDX, GitHub, SLSA)
11) Quality Assurance: Unit Tests, Mocks, HIL & Metrics
-
Unit tests (host): e.g. Unity/Ceedling -- a lean ANSI C framework, ideal for driver logic (with CMock for HAL stubs). (Throw The Switch, GitHub)
Mini example (scaling):
C
// scales raw ADC value (0..4095) to volts (Vref=3.3) float adc_to_volt(uint16_t raw) { return (3.3f * raw) / 4095.0f; } TEST(AdcScale, ConvertsCorrectly) { TEST_ASSERT_FLOAT_WITHIN(0.005f, 1.650f, adc_to_volt(2048)); } - Unit tests (target): small test suite on-target (e.g. via semihosting/serial) to catch compiler/ABI/linker effects.
- HIL: real controller hardware ↔ simulated environment (ESP32 DAC feeds STM32 ADC; digital I/Os drive simulated sensors). Critical for timing/I/O paths and safety functions (e.g. secure update recovery). (Ansys)
- Release metrics: code coverage (host), on-target smoke tests, latency/jitter measurements of the DMA chain, ENOB/noise floor, update MTTR (recovery time per SP 800-193). (NIST Publications)
12) Security Best Practices -- Short and Sweet
- Threat model: Which attackers? (Physical/remote). Which assets? (Keys, IP, cloud credentials, sensor integrity).
- Harden the boot chain: ROM root, mandatory signatures, rollback protection, secure storage. SBSFU/TF-M/MCUboot are battle-tested. (STMicroelectronics, mcuboot)
- Isolation: TrustZone-M for crypto/keys/update agent. (Arm Developer)
- Lifecycle updates: consider standardized PSA Firmware Update API concepts; clean, repeatable process -- before series production. (arm-software.github.io)
- Transports: MQTT/CoAP with (m)TLS/DTLS, device identity via mTLS, robust retry/backoff, clock sync. (OASIS Open Docs, IETF Datatracker)
- Supply chain: SBOM + SLSA provenance per release. (CycloneDX, SLSA)
13) Conclusion
References & Further Reading
- Anti-Aliasing & Oversampling TI: Antialiasing Filter Circuit Design...; Designing an anti-aliasing filter... (Texas Instruments) Analog Devices: Guide to Anti-Aliasing Filter Basics; Anti-aliasing filtering considerations... (Analog Devices) Silicon Labs: Improving ADC Resolution by Oversampling and Averaging (Silicon Labs)
- STM32 ADC/DMA ST Wiki: Getting started with ADC (DMA, TIM); ST App Notes: ADC modes; DMA introduction (STMicroelectronics, STMicroelectronics)
- Arduino/ESP32 Arduino Docs: analogRead() & examples (docs.arduino.cc, Arduino) Espressif Docs: ESP32 DAC (API & TRM) (Espressif Docs, Espressif)
- Secure Boot / TrustZone / Lifecycle ST SBSFU (product page, AN5056), TrustZone-based solution (AN5447) (STMicroelectronics) ARM/TF-M & TrustZone-M fundamentals (Arm Developer, trustedfirmware-m.readthedocs.io) MCUboot docs & signing tool (mcuboot) NIST SP 800-193: Platform Firmware Resiliency (NIST Publications) PSA Firmware Update API (design goals/model) (arm-software.github.io)
- Edge-to-Cloud Protocols MQTT (OASIS v3.1.1/v5) (OASIS Open Docs) CoAP (RFC 7252) & extensions (IETF Datatracker)
- Workflow & Traces CycloneDX SBOM (spec/guide) (CycloneDX) SLSA Provenance/Levels (SLSA) Monorepo trade-offs (Fowler) (martinfowler.com)
Bonus idea for the lab: Use the ESP32 DAC as a programmable stimulus, feed it into the STM32 ADC (DMA, timer-triggered) and verify filter/scaling in a HIL loop -- including automated Unity tests for signal processing. This way you can test security-relevant update recovery paths and measure real ENOB at the same time. (Espressif Docs, STMicroelectronics, Throw The Switch)