Skip to content

Reuse HMAC hash state #957

Open
Open

Description

@erickguan

HMAC uses key to derive a state. We can keep hasher state to save redundant compute. The tradeoff is to keep calculated state. Because we always compute HMAC state by stored key, we are simply adding a parallel hasher along with credentials.

From #923 (comment)

The previous PR #923 didn't include a feature check to ensure at least one crypto lib is compiled.

Activity

  1. Xuanwo commented on Nov 19, 2026

    @Xuanwo
    Member

    HMAC key setup does produce reusable state, but on current main the helpers are one-shot: the AWS SigV4 path derives the signing key with four HMAC-SHA256 calls and then computes the signature with a fifth, and each call creates a fresh keyed HMAC state. Signer caches the credential itself, but sign_request receives it per call; there is no keyed HMAC state to reuse. A stateful path would need a keyed-state object (or an internal cache), an owner for it, and invalidation when the credential or signing scope changes. For SigV4, the scope includes the date, region, and service.

    For context, #766 reported about 20% faster sequential AWS SigV4 signing with one signer and credential by caching the derived signing key. That skips the four key-derivation HMACs on cache hits, but it does not isolate the benefit of reusing an initialized HMAC state; the signature HMAC still starts from a fresh state. The PR was closed unmerged, partly because the cache made RequestSigner stateful and coupled it to credentials.

    To judge whether the extra state is worthwhile, could you share:

    • which signing service or workload prompted this;
    • how often requests reuse the same credential, and whether the signing scope (date, region, service) stays constant;
    • any profile or end-to-end signing measurements showing HMAC key initialization is a material cost.

    On the feature check: on current main, reqsign-core has non-optional hmac, sha1, and sha2 dependencies. A build with default features disabled still includes RustCrypto, so at least one crypto backend is always compiled today. An "at least one backend" check would only be needed if those dependencies become optional.

  2. erickguan commented on Nov 19, 2026

    @erickguan
    MemberAuthor

    First of all, you are right this is a micro-optimization in performance. For how SHA works, no benefits. For HMAC, I am thinking to start with the root credentials.

    The other part is organization. Crypto providers provide implementation by an interface.

  3. Xuanwo commented on Nov 19, 2026

    @Xuanwo
    Member

    Agreed on the HMAC direction: the reusable part is the initialized keyed state, while a plain SHA state has no equivalent carry-over.

    One clarification: what matters for reuse is a long-lived signing secret with a stable signing scope, not whether the credential is a root-account key. If there is a workload to optimize, please name one specific signer (for example AWS SigV4) and share an end-to-end signing measurement under credential and scope reuse. That would show whether the additional state is worthwhile.

    For the backend and interface organization, the published CryptoProvider proposal in #961 is the right place; the provider shape and installation semantics remain open there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions