SimpleOTA client main
Shared core of the SimpleOTA Arduino and ESP-IDF clients
Loading...
Searching...
No Matches
sota_signing.h File Reference

Ed25519 firmware-signature helpers for the SimpleOTA clients. More...

Go to the source code of this file.

Data Structures

struct  sota_sig_verifier_t
 A streaming signature check against each candidate key at once. More...
 

Macros

#define SOTA_SIGNED_UPDATES   1
 Signed updates can be left out of a build that will never take one, with -DSIMPLEOTA_DISABLE_SIGNED_UPDATES, which drops Ed25519 and SHA-512 from the image.
 
#define SOTA_VERIFIER_MAX_KEYS   2
 Streaming Ed25519 verifier over one firmware image, for up to two candidate public keys (key rotation windows).
 

Enumerations

enum  sota_signed_gate_t { SOTA_GATE_SKIP , SOTA_GATE_VERIFY , SOTA_GATE_FAIL_CLOSED , SOTA_GATE_WARN_UNVERIFIED }
 Outcome of the signed-firmware policy decision (see sota_signed_gate). More...
 

Functions

sota_signed_gate_t sota_signed_gate (bool device_reports_signed, uint8_t num_keys, bool offer_signed, bool offer_has_signature)
 Decide how to handle a (possibly signed) offer, from device policy and the offer's own claims.
 
bool sota_parse_ed25519_public_key_pem (const char *pem, uint8_t out[32])
 Parse a PEM "BEGIN PUBLIC KEY" (SubjectPublicKeyInfo) Ed25519 key, the exact format the SimpleOTA dashboard and API hand out.
 
bool sota_decode_signature_b64 (const char *b64, uint8_t out[64])
 Decode a base64 Ed25519 signature (as sent in the OTA check response) into its raw 64 bytes.
 
bool sota_verifier_begin (sota_sig_verifier_t *v, const uint8_t sig[64], const uint8_t keys[][32], uint8_t num_keys)
 Start checking sig against each of the candidate keys.
 
void sota_verifier_update (sota_sig_verifier_t *v, const uint8_t *data, size_t len)
 Feed the next chunk of the image, in download order.
 
bool sota_verifier_final (sota_sig_verifier_t *v)
 

Detailed Description

Ed25519 firmware-signature helpers for the SimpleOTA clients.

Portable (no platform dependencies) so it can be unit-tested on a host machine with plain cc/g++; see test/host/test_signing.c.

The SimpleOTA server delivers, for artifacts uploaded with security_mode "signed", a base64 Ed25519 signature computed over the raw firmware bytes exactly as downloaded. The device pins the project's PUBLIC key (it is not a secret) and must verify the signature over the downloaded bytes BEFORE the new image is marked bootable.

Verification is incremental (Monocypher's streaming Ed25519 check) so the image never has to fit in RAM: the apply path feeds the same chunks it writes to the OTA partition.

Macro Definition Documentation

◆ SOTA_SIGNED_UPDATES

#define SOTA_SIGNED_UPDATES   1

Signed updates can be left out of a build that will never take one, with -DSIMPLEOTA_DISABLE_SIGNED_UPDATES, which drops Ed25519 and SHA-512 from the image.

Such a build pins no key (sota_engine_add_key() fails) and treats a signed offer as a device without a key does. 1 when the build can verify signed updates.

◆ SOTA_VERIFIER_MAX_KEYS

#define SOTA_VERIFIER_MAX_KEYS   2

Streaming Ed25519 verifier over one firmware image, for up to two candidate public keys (key rotation windows).

Runs one Monocypher incremental check context per candidate key; the signature is accepted if it verifies under ANY candidate. Contexts are a few hundred bytes each (embedded SHA-512 state); the whole struct is about 700 bytes, so the apply path allocates it on the heap rather than on a task stack next to the download buffer.

Usage: sota_verifier_begin() once, sota_verifier_update() per downloaded chunk, sota_verifier_final() once.

Enumeration Type Documentation

◆ sota_signed_gate_t

Outcome of the signed-firmware policy decision (see sota_signed_gate).

Enumerator
SOTA_GATE_SKIP 

Not a signed update; apply normally.

SOTA_GATE_VERIFY 

Verify the signature during download.

SOTA_GATE_FAIL_CLOSED 

A signature is required but absent/unusable; reject now.

SOTA_GATE_WARN_UNVERIFIED 

Signed offer but no key to check it; apply + warn.

Function Documentation

◆ sota_signed_gate()

sota_signed_gate_t sota_signed_gate ( bool  device_reports_signed,
uint8_t  num_keys,
bool  offer_signed,
bool  offer_has_signature 
)

Decide how to handle a (possibly signed) offer, from device policy and the offer's own claims.

Pure and host-testable.

The guard against stripped signatures lives here: a device that declares signed mode AND has a key pinned MUST see a usable signature, no matter what the offer claims. A compromised server or MITM that strips the security_mode / signature fields from the response therefore cannot silently downgrade the device to checksum-only flashing; it gets SOTA_GATE_FAIL_CLOSED. When the device is not enforcing (not in signed mode, or no key pinned), the offer's own declaration is trusted, which keeps basic-to-signed fleet migration flowing.

Parameters
device_reports_signedDevice is configured with security_mode "signed".
num_keysCount of pinned public keys (0..2).
offer_signedThe offer's security_mode is "signed".
offer_has_signatureA usable (decoded, ed25519) signature is present.

◆ sota_parse_ed25519_public_key_pem()

bool sota_parse_ed25519_public_key_pem ( const char *  pem,
uint8_t  out[32] 
)

Parse a PEM "BEGIN PUBLIC KEY" (SubjectPublicKeyInfo) Ed25519 key, the exact format the SimpleOTA dashboard and API hand out.

The DER inside an Ed25519 SPKI is fixed-size (44 bytes: a constant 12-byte header followed by the 32 raw key bytes), so no ASN.1 parser is needed.

Parameters
pemNull-terminated PEM text (armor lines + base64 body).
outReceives the raw 32-byte public key on success.
Returns
true on success; false for malformed PEM, wrong key type, or wrong length. out is untouched on failure.

◆ sota_decode_signature_b64()

bool sota_decode_signature_b64 ( const char *  b64,
uint8_t  out[64] 
)

Decode a base64 Ed25519 signature (as sent in the OTA check response) into its raw 64 bytes.

Returns
true only if the input is valid base64 of exactly 64 bytes.

◆ sota_verifier_begin()

bool sota_verifier_begin ( sota_sig_verifier_t *  v,
const uint8_t  sig[64],
const uint8_t  keys[][32],
uint8_t  num_keys 
)

Start checking sig against each of the candidate keys.

Parameters
vVerifier state (zero-init not required; begin() resets it).
sigRaw 64-byte signature from the OTA offer.
keysArray of raw 32-byte public keys.
num_keys1..SOTA_VERIFIER_MAX_KEYS candidate keys (extra ignored).
Returns
false (verifier inert; final() will fail) when num_keys is 0.

◆ sota_verifier_final()

bool sota_verifier_final ( sota_sig_verifier_t *  v)
Returns
true if the signature verifies under any candidate key.