REM 1.0

Secure OTA Encryption Method

AES-256-CFB + HMAC-SHA256 + HKDF

Abstract

REM (Secure OTA Encryption Method) is a custom encryption protocol designed for secure over-the-air (OTA) firmware updates on resource-constrained devices such as the ESP32. It provides both confidentiality and integrity by combining AES-256-CFB encryption with HMAC-SHA256 authentication and HKDF-SHA256 key derivation. Unlike AEAD schemes that require buffering the full ciphertext for tag verification, REM supports streaming decryption and tag verification in chunks, making it suitable for large patches where device RAM is limited. This document specifies the REM 1.0 wire format, algorithms, security properties, and comparison with related methods.

Introduction and problem statement

Firmware updates delivered over the air must be confidential (so only the target device can read them) and authentic (so tampering is detected). Standard solutions such as AES-GCM or ChaCha20-Poly1305 provide both, but verifying their authentication tag typically requires the full ciphertext to be available, which on memory-limited embedded devices (e.g. ESP32) can be impractical for large patches. The project therefore needs an encryption method that (1) provides confidentiality and integrity, (2) allows decryption and verification in a streaming fashion with bounded RAM, and (3) uses separate keys for encryption and authentication. REM 1.0 is a protocol that meets these requirements by using AES-256-CFB for streaming encryption, HMAC-SHA256 for an integrity tag over nonce and ciphertext, and HKDF-SHA256 to derive distinct AES and HMAC keys from a single device key.

What is REM?

REM (Secure OTA Encryption Method) is this project’s encryption protocol for over-the-air firmware patches. It provides confidentiality and integrity: data is encrypted so only the device can read it, and a tag ensures that any change to the ciphertext is detected before use.

Methods and algorithms used

REM is a protocol that uses these standard algorithms (no custom ciphers or hashes):

ComponentAlgorithmRole in REM
Symmetric encryptionAES-256-CFBEncrypts/decrypts patch; 256-bit key, 16-byte nonce, segment size 128. Allows streaming on the device.
Message authenticationHMAC-SHA25632-byte tag over (nonce ‖ ciphertext). Device verifies before using data.
Key derivationHKDF-SHA256Derives two 32-byte keys from device key: one for AES, one for HMAC. Salt: REM1-OTA-PATCH, context: REM1.
Hashing (inside HMAC)SHA-256Used inside HMAC for the integrity tag.
Signatures (with REM)ECDSA P-256Server signs plaintext patch; device verifies. SHA-256 hashes message before signing.
RandomnessCSPRNGServer generates a random 16-byte nonce per encryption.

How REM works (step-by-step)

Server (encryption):

Device (decryption):

Wire format

Every REM 1.0 payload has this layout:

FieldSizeDescription
Magic4 bytesREM1 (ASCII)
Nonce16 bytesRandom IV for AES-CFB
CiphertextN bytesAES-256-CFB(nonce, plaintext)
Tag32 bytesHMAC-SHA256(hmac_key, nonce ‖ ciphertext)

Total length = 4 + 16 + N + 32 = 52 + plaintext length.

Parameter summary

ParameterValue
Protocol versionREM 1.0 (magic REM1)
Device key size256 bits (32 bytes)
AES key size256 bits (32 bytes, derived)
HMAC key size256 bits (32 bytes, derived)
Nonce (IV) size128 bits (16 bytes)
AES block size128 bits
CFB segment size128 bits
Tag size256 bits (32 bytes)
HKDF saltREM1-OTA-PATCH (16 bytes, fixed)
HKDF contextREM1 (4 bytes)

Key derivation

From the 32-byte device key K:

AES key is used for AES-256-CFB; HMAC key is used for the tag. Signatures are unchanged: ECDSA P-256 over SHA-256 hash of the plaintext patch (server signs before encrypting).

Comparison with other methods

How REM 1.0 compares to other common encryption designs:

Method Confidentiality Integrity / authentication Streaming decryption Typical use
AES-256-CFB only Yes No Yes Legacy; no integrity check
REM 1.0 (this project) Yes Yes (HMAC) Yes OTA patches on ESP32
AES-256-GCM Yes Yes (AEAD) No* TLS, general AEAD
ChaCha20-Poly1305 Yes Yes (AEAD) No* TLS, mobile/embedded

* Full ciphertext typically needed for tag verification; streaming is possible with extra design.

How REM is different from others

Why REM for this project?

Security properties and threat model

What REM protects against:

Assumptions and scope: The device key is shared securely and kept secret; REM does not protect against its compromise. The nonce must be unique per encryption (server uses CSPRNG). REM does not provide non-repudiation by itself; in this project ECDSA signatures over the plaintext provide server authenticity.

Implementation context

Server: Python (FastAPI); crypto_utils.rem_encrypt_patch(); PyCryptodome for HKDF, AES, HMAC-SHA256.

Device: ESP32 firmware (C++, mbedTLS); decryption and HMAC verification in chunks (e.g. 1024 bytes) so large patches do not require full file in RAM.

Application: REM encrypts delta (bsdiff) patches; server signs plaintext with ECDSA then encrypts with REM; device verifies tag, stream-decrypts, then verifies ECDSA on plaintext before applying the patch.

Limitations

Conclusion

REM 1.0 is a custom encryption protocol that provides confidentiality and integrity for OTA firmware patches while supporting streaming decryption and tag verification on resource-constrained devices. It uses only standard algorithms (AES-256-CFB, HMAC-SHA256, HKDF-SHA256) and separates encryption and authentication keys via HKDF. The wire format is versioned with a magic header for future evolution. REM is implemented in this project for secure delta updates on the ESP32.

References

Full protocol specification: REM_SPEC.md.