fix(H6): authenticated-encrypted backups (AES-GCM + PBKDF2) - #18
Closed
d33mobile wants to merge 2 commits into
Closed
fix(H6): authenticated-encrypted backups (AES-GCM + PBKDF2)#18d33mobile wants to merge 2 commits into
d33mobile wants to merge 2 commits into
Conversation
…uth failure Backups exist to live off-device, so their raw HMAC seeds must be useless to anyone who obtains a blob, and a tampered blob must never be trusted. The v1 format was plaintext backup_key_t records guarded only by a CRC-32 (not a MAC): a leaked blob exposed every seed forever, and an attacker could set is_admin=1 on a chosen-secret record, fix the CRC, and import to gain permanent admin. New wire format v2: cleartext header (magic, version=2, key_count, 16B salt, 12B IV, 16B GCM tag, CRC-32 hint) followed by AES-256-GCM ciphertext of the serialised records. The AEAD key is PBKDF2-HMAC-SHA256(passphrase, salt) - a backup is portable so the key must come from an operator secret, not a device-bound key. The header prefix (through the IV) is fed to GCM as AAD, so magic/version/key_count/salt/IV are authenticated. With GCM the tag IS the MAC of ciphertext+AAD (encrypt-then-MAC by construction). backup_import derives the key from the blob's salt, GCM-verifies the tag, and REJECTS before parsing any record on failure - wrong passphrase and any tamper (header/ciphertext/tag) all fail the tag check. The CRC is kept only as a paste-corruption hint, not a trust boundary. Defense-in-depth: even an authenticated is_admin record is gated through a per-key operator confirmation callback before any destructive write; a denied record aborts the whole import. commands_backup.c prompts for the passphrase over the console (export + import) and drives the admin-confirm prompt. mbedtls_config.h enables AES/CIPHER/GCM/ PKCS5 (verify firmware image size on device). Host-verified: harness_storage.c exercises export->import roundtrip (incl. an admin key), wrong-passphrase reject, tag/ciphertext/AAD tamper reject, the is_admin backstop (deny + NULL-callback deny + confirm), a ciphertext no-cleartext-seed check, and the non-bool-flag UBSan path via a forged valid blob. make -C test asan/valgrind/coverage and ./ci --action=check all pass. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The CI host's mbedTLS predates 3.6 and lacks mbedtls_pkcs5_pbkdf2_hmac_ext, which broke the host link. Select the API by MBEDTLS_VERSION_NUMBER: the _ext form on 3.6+ (firmware / pico-sdk mbedtls), the context-based mbedtls_pkcs5_pbkdf2_hmac on older builds. Warning-clean on both (the context form is deprecated on 3.6+, the _ext form is absent pre-3.6). Applied in both backup.c and the forge helper in harness_storage.c. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Finding (ISSUES.md H6 — Backups are neither encrypted nor authenticated; CRC-32 is not a MAC)
Fix
A backup's whole purpose is to live off-device, so its raw seeds must be useless to anyone who obtains the blob, and a tampered blob must never be trusted. The payload is now encrypted-then-MAC'd under an operator passphrase (a backup is portable, so the key cannot be device-bound — device-bound at-rest secrecy is H7's concern).
New wire format v2
Cleartext header followed by AES-256-GCM ciphertext of the serialised records:
magicHSLLversion2key_countsaltivtagchecksumkey_count * sizeof(backup_key_t)Crypto
mbedtls_pkcs5_pbkdf2_hmac_ext(MBEDTLS_MD_SHA256, passphrase, salt, 100000 iters)→ 32-byte key.mbedtls_gcm_crypt_and_tag/mbedtls_gcm_auth_decrypt). The header prefix through the IV is passed as associated data, somagic/version/key_count/salt/ivare authenticated. With GCM the tag is the MAC of ciphertext + AAD — encrypt-then-MAC by construction.backup_exporttakes the passphrase, derives the key, encrypts, emits the v2 blob (salt/IV from the platform CSPRNG,get_rand_64).backup_importparses the cleartext header, derives the key from the blob's salt, GCM-verifies the tag, and rejects before parsing any record on failure. Wrong passphrase and any tamper (header / ciphertext / tag) all fail the tag check.is_admin backstop (defense-in-depth)
Even with a valid MAC, each
is_adminrecord is gated through a per-key operator confirmation callback (backup_admin_confirm_fn) before any destructive write; a denied record aborts the whole import with existing keys untouched. ANULLcallback denies all admin records.commands_backup.cprompts for the passphrase over the console (export + import) and drives the admin-confirm prompt.mbedtls config
mbedtls_config.henablesMBEDTLS_AES_C,MBEDTLS_CIPHER_C,MBEDTLS_GCM_C,MBEDTLS_PKCS5_C(previously only MD/SHA-1/SHA-256).mbedtls_pkcs5_pbkdf2_hmac_extis a mbedTLS 3.x API — matches pico-sdk master (docs/BUILD.md). verify-on-hw: firmware image-size impact of the added mbedtls modules, on-device PBKDF2 timing (100k iters, HW-accelerated SHA-256 whenLIB_PICO_SHA256), and the console passphrase-entry UX.Host-side verification
test/harness_storage.clinks the real first-partybackup.c+storage.c+ littlefs against system mbedTLS and tests the round-trip end to end:key_countaltered within range) → MAC failure → rejected;key_count, truncated, too-small) → rejected pre-decrypt;NULL, and imported only when confirmed — directly exercising the H6 escalation path;The test forges validly-authenticated blobs with the same PBKDF2+GCM construction (the "passphrase holder crafts a malicious payload" model), so the is_admin / non-bool paths are exercised through genuine ciphertext, not plaintext pokes.
CI host gates all green locally:
make -C test asan= 0,valgrind= 0,coverage= 0,./ci --action=check= 0. The libFuzzerfuzz_*.charnesses are out of scope (clang-only, not in CI) and untouched.Scoped to follow-up
backup_importstill deletes-then-writes, but the admin backstop runs before any delete.base64_decodebounds hardening is M5; buffer sizes here are derived fromsizeof(backup_header_t)/record counts and track the larger v2 header correctly.