Speck
A production-grade runtime package manager for MMU-less microcontrollers. Enables secure dynamic loading of signed, position-independent code modules with flash-aware delta updates.
Features
- 🔐 Ed25519 Signatures: Compact 64-byte signatures with 32-byte public keys
- 📦 Optimized Delta Updates: Binary diff reduces OTA bandwidth by 50-95%
- ⚡ Flash-Aware Storage: Wear leveling, transactional updates, erase tracking
- 🛡️ Anti-Rollback Protection: Monotonic version counters prevent downgrade attacks
- 🔧 Position Independent: Supports runtime relocation for ARM Cortex-M, RISC-V
- 💾 Memory Efficient: Streaming parsers work in <1KB RAM
- 📊 Hardware Locked: Optional hardware revision binding
Quick Start
# Install CLI
# Generate signing key
# Sign firmware binary
# Verify signature
# Create delta update (v1 -> v2)
# Apply delta
Module Format
Offset Size Description
0 4 Magic "SPK\x02"
4 2 Format version
6 4 Total size
10 4 Code size
14 4 Entry offset
18 4 Flags (signed, compressed, etc)
22 8 Monotonic version (anti-rollback)
30 4 Hardware revision requirement
34 4 CRC32 of code
38 10 Reserved
48 32 Ed25519 public key
80 64 Ed25519 signature
116 var Code payload
Total header overhead: 116 bytes
Architecture
┌─────────────────────────────────────┐
│ Application │
├─────────────────────────────────────┤
│ Storage Manager │ Delta Applier │
├──────────┬──────────────────────────┤
│ Journal │ Wear Leveling │ Flash │
├──────────┴──────────────────────────┤
│ Module Parser/Loader │
├─────────────────────────────────────┤
│ Crypto (Ed25519, SHA256) │
└─────────────────────────────────────┘
Library Usage
use ;
// Generate keys
let keypair = generate;
// Create signed module
let module = builder
.code
.entry_offset
.version // Anti-rollback
.sign
.build?;
// Verify and load
module.verify?;
loader.load?;
Delta Compression
The delta algorithm uses a combination of COPY (from source) and INSERT (literal) operations:
Original: The quick brown fox jumps over the lazy dog
Modified: The very quick brown fox jumps over the lazy cat
Patch:
INSERT "very "
COPY 35 bytes from offset 0
INSERT "cat"
Typical compression ratios:
- Minor updates (bug fixes): 90-95% reduction
- Medium updates (features): 50-70% reduction
- Major updates: 20-40% reduction
Flash Simulation
The CLI includes a sophisticated NOR flash simulator:
# Install to offset with automatic page erase
# Check wear statistics
# Hex dump of flash contents
Features simulated:
- Page-level erase (4KB units)
- Write-before-erase protection
- Per-page erase cycle counting
- Wear leveling recommendations
Security Considerations
- Key Management: Store signing keys in HSM or secure enclave
- Rollback Protection: Always increment monotonic version
- Timing Attacks: Verification is constant-time via ed25519-dalek
- Replay Protection: Include nonce/timestamp in manifest for network updates
- Physical Attacks: Combine with secure boot and flash encryption
Performance
| Operation | Time (STM32F4@168MHz) |
|---|---|
| Sign 16KB | ~2ms |
| Verify 16KB | ~3ms |
| Delta 16KB | ~10ms |
| Apply delta | ~5ms |
| Flash erase (4KB) | ~20ms |
| Flash write (256B) | ~1ms |
Testing
# Unit tests
# Integration tests
# Benchmarks
# With logging
Real Device Integration
Hardware Requirements
Speck works with any MMU-less MCU with:
- Flash: 64KB+ (NOR flash preferred)
- RAM: 4KB+ for delta apply buffer
- Crypto: Software Ed25519 (no hardware accelerator required)
Tested platforms:
| Platform | Flash | RAM | Notes |
|---|---|---|---|
| STM32F103 | 64KB | 20KB | Blue Pill, Maple Mini |
| STM32F405 | 1MB | 192KB | High-performance |
| nRF52840 | 1MB | 256KB | BLE + Crypto hardware |
| RP2040 | External | 264KB | QSPI flash support |
| ESP32-C3 | 4MB | 400KB | WiFi OTA ready |
Step-by-Step Integration
1. Partition Your Flash
Reserve space for Speck storage:
0x0800_0000: Bootloader (8KB)
0x0800_2000: Main App (48KB)
0x0800_E000: Speck Slot 0 (4KB) - Download buffer
0x0800_F000: Speck Metadata (4KB) - Version, state
2. Implement Flash Trait
// src/hal/flash.rs
use ;
3. Initialize Speck in Main
// src/main.rs
use ;
!
4. Handle OTA Download
// Called when BLE/WiFi receives new firmware chunk
// Called when download complete
5. Build and Flash
# Build for STM32F103
# Convert to binary
# Sign the firmware
# Flash via ST-Link
Delta Update Workflow
// Delta updates reduce OTA bandwidth significantly
See examples/ for complete working implementations on various platforms.
Embedded Integration (no_std)
For no_std environments:
[]
= { = "0.2", = false, = ["no-alloc"] }
Implement the Flash trait for your hardware:
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE)
- MIT license (LICENSE-MIT)
at your option.
Contributing
See CONTRIBUTING.md for guidelines.
Acknowledgments
- Delta algorithm inspired by bsdiff
- Ed25519 implementation from dalek-cryptography
- Developed for embedded systems with 64KB flash constraints