Skip to content

fix: clear sign_hmac key residue from the stack - #22

Merged
derdelean merged 1 commit into
mainfrom
fix/sign-hmac-residue
Nov 7, 2026
Merged

derdelean merged 1 commit into
mainfrom
fix/sign-hmac-residue

Conversation

@derdelean

Copy link
Copy Markdown
Owner

sign_hmac ran HMAC-SHA256 through the sha2 hasher, which does not zero its chaining state and is moved by value. A stack probe on aarch64 macOS found key-derived values on the stack after one call on main: at least half of the inner and of the outer chaining state in the shipped LTO build, three copies of each in a debug build, and a ChaCha20 state holding the vault master key in a release build without LTO.

  • hmac_sha256 drives sha2::block_api::compress256 directly. The padded key, the chaining state, the last padded block and the inner digest each live in their own Zeroizing buffer and are only passed by reference.
  • sign_hmac calls the key-touching part (hmac_with_secret, #[inline(never)]) and then scrub_stack, which overwrites 32 KiB of stack below the caller.
  • Tests: RFC 4231 cases 1 to 4, 6 and 7, the hmac crate (new dev-dependency) as an oracle for key and message lengths around the block boundaries, and the SHA-256 padding against Sha256::digest for every length from 0 to 193.

After the fix the probe found nothing in the shipped LTO build, a release build without LTO, a debug build, and the software SHA-256 backend. The other paths that decrypt under the master key (decrypt_secret behind reserve_invoke, retrieve_secret behind consume, encrypt_and_store) are not scrubbed yet. Registers are not cleared. Linux x86_64 is not measured.

Cost: sign_hmac with a 256-byte payload, median of 7 x 200,000 calls, shipped profile: 2.31 µs on main, 1.98 to 2.00 µs here. The direct compression calls and the single padded-key buffer save more than the scrub costs.

Local: fmt, clippy -D warnings, cargo 110 passed (macOS), pytest 131 passed and 1 skipped with the real broker.

@derdelean
derdelean merged commit afae583 into main Nov 7, 2026
10 checks passed
@derdelean
derdelean deleted the fix/sign-hmac-residue branch November 7, 2026 15:03
@derdelean derdelean mentioned this pull request Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant