hash: clear HMAC states after use #1967

pull fametrano wants to merge 1 commits into bitcoin-core:master from fametrano:rfc6979-clear-hmac changing 1 files +4 −0
  1. fametrano commented at 4:48 AM on October 15, 2026: contributor

    The RFC6979 initialize and generate functions in hash_impl.h never clear their local HMAC states, so the last one stays on the stack after a nonce is derived. It holds an intermediate V and an inner digest, not the secret key or the message, and the nonce cannot be recomputed from them without K, which the callers clear. secp256k1_hmac_sha256_clear exists since #1579 but has no caller.

    This PR clears each state after its last use, and also the SHA256 state that hashes an HMAC key longer than 64 bytes. I added no test. A throwaway program that scans the stack after secp256k1_nonce_function_rfc6979 finds one copy of V before this change and none after, at -O0, -O1 and -O2.

  2. hash: clear HMAC states after use
    The RFC6979 initialize and generate functions keep HMAC states in
    local variables and never clear them, so the last state stays on the
    stack after a nonce is derived. secp256k1_hmac_sha256_clear exists for
    this but has no caller.
    
    Clear each of these states after its last use. Also clear the SHA256
    state that hashes an HMAC key longer than 64 bytes.
    13726d4dfa
  3. theStack added the label side-channel on Oct 18, 2026
  4. Yudis-bit commented at 8:39 AM on October 20, 2026: contributor

    Tested ACK 13726d4 on Windows.

  5. real-or-random commented at 2:05 PM on October 20, 2026: contributor

    Concept ACK

    Clearing is in principle the right thing to do even though it's overkill here under the assumption that the attacker can't invert SHA256. But this is just because HMAC itself is overkill, and this is an entirely different discussion

  6. theStack commented at 4:14 AM on October 22, 2026: contributor

    Concept ACK

  7. theStack approved
  8. theStack commented at 3:20 AM on October 23, 2026: contributor

    ACK 13726d4dfaf3f54ed4f4c8ce66110700df10e648


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/secp256k1. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-10-23 07:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me