Skip to content

feat(core): add a boring feature for SHA and HMAC - #923

Draft
vovacf201 wants to merge 2 commits into
apache:mainfrom
vovacf201:feat/core-boring-backend
Draft

vovacf201 wants to merge 2 commits into
apache:mainfrom
vovacf201:feat/core-boring-backend

Conversation

@vovacf201

Copy link
Copy Markdown

What

Adds an opt-in boring cargo feature to reqsign-core that computes the SHA and HMAC in reqsign_core::hash with BoringSSL (through the boring crate) instead of RustCrypto.

  • reqsign_core::hash keeps its public API. The functions now call a private backend module, which is either RustCrypto (default, unchanged) or BoringSSL (boring feature). Covered: hex_sha1, hex_sha256, hmac_sha256, base64_hmac_sha256, hex_hmac_sha1, hex_hmac_sha256, base64_hmac_sha1.
  • The facade reqsign crate gets a matching boring feature that forwards to reqsign-core/boring.
  • Known-answer tests run on both backends: SHA-256/SHA-1 digests of "" and "abc", RFC 4231 test cases 1 and 6 (including a key longer than the block size), RFC 2202 test case 1, and the AWS SigV4 signing-key example from the AWS documentation.
  • CI: one extra step runs the reqsign-core and reqsign-aws-v4 tests with --features reqsign-core/boring. Clippy already builds the feature through --all-features.
  • README: one line in Features.

Why

Some applications standardize all their cryptography on BoringSSL and don't want a second implementation of SHA/HMAC on the request-signing path. Today they have to fork reqsign-core to change hash.rs. Higher-level users such as OpenDAL build the signing Context internally, so a runtime hook wouldn't reach them without API changes in those crates as well.

A cargo feature works through feature unification. An application can add reqsign-core = { version = "3", features = ["boring"] } as a direct dependency, and the copy used by OpenDAL (or any other dependent) switches backends with no changes in the intermediate crates.

Scope and compatibility

  • Off by default, so nothing changes unless the feature is enabled. The RustCrypto dependencies stay non-optional, so turning on boring can't break a dependent that disables default features. When both are compiled, boring takes precedence.
  • Covers only reqsign_core::hash. That is all of the hashing and HMAC behind AWS SigV4 (reqsign-aws-v4) and the other services that call these helpers. Asymmetric signing (SigV4a ECDSA, Google/Oracle RSA, the jwt feature, Azure client certificates) is unchanged.
  • boring builds BoringSSL from source (CMake and a C/C++ toolchain) and isn't available on wasm32-unknown-unknown. The wasm-supported crates are unaffected because the feature is off by default. The CI wasm builds still pass.

Testing

  • cargo fmt --all -- --check
  • cargo clippy --workspace --all-targets --all-features -- -D warnings
  • cargo test --no-fail-fast -p reqsign-core -p reqsign-aws-v4 (RustCrypto): reqsign-core 45, reqsign-aws-v4 69 (+ integration test binaries), all pass.
  • cargo test --no-fail-fast -p reqsign-core -p reqsign-aws-v4 --features reqsign-core/boring (BoringSSL): the same results.
  • cargo build --target wasm32-unknown-unknown --no-default-features --features=aws,aws-v4a,azure,aliyun,tencent (facade): passes.

🤖 Generated with Claude Code

@erickguan erickguan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this feature.

Some requests:

  1. Reuse hasher. boring will allocate much less when signing again.
  2. Feel free to change data structure and function signatures. It can be lazy init hasher. It can be a struct.
  3. We validate at least reqsign compiles with at least one feature.

Comment thread core/src/hash.rs Resolved
@vovacf201
vovacf201 force-pushed the feat/core-boring-backend branch from 845cc4a to c62ef85 Compare November 18, 2026 14:02
@vovacf201

vovacf201 commented Nov 18, 2026 •

Copy link
Copy Markdown
Author

Thanks for the review! Pushed eb4db64 (and rebased on main):

  1. Reuse hasher. SHA-1 and SHA-256 now reuse a thread-local Hasher (see the inline reply). HMAC still goes through boring::hash::hmac_sha1/hmac_sha256: boring's safe API can't reset or re-key an Hmac (finalize consumes it, and HmacCtx has no safe constructor), so reusing an HMAC context would need unsafe calls into boring-sys. I'd rather not add the first unsafe to reqsign for that, but I'm happy to if you prefer it.
  2. Data structures. I kept the public reqsign_core::hash functions unchanged; the reuse lives in the private backend module, so callers such as reqsign-aws-v4 get it without changes.
  3. Build with features. CI now builds the reqsign facade with --features boring and with --no-default-features --features boring,aws (Linux and macOS). The commit also fixes the test step, which my first version had left without its RUST_LOG/RUST_BACKTRACE env.

@vovacf201
vovacf201 force-pushed the feat/core-boring-backend branch from c62ef85 to eb4db64 Compare November 18, 2026 14:04
@erickguan

erickguan commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Is thread-local safe?

How do you think about APIs in user ends?

@vovacf201

Copy link
Copy Markdown
Author

Is thread-local safe?

How do you think about APIs in user ends?

yes it's safe

if the question about API means how API changes for user - nothing changes

@Xuanwo

Xuanwo commented Nov 19, 2026

Copy link
Copy Markdown
Member

Following up on your review (#923 (review)) against the current head 7d0f5903a6b2c039df208d00ae8be547340bf3f9:

  • SHA-1 and SHA-256 now reuse a per-thread boring::hash::Hasher, reset with finish(). The latest commit also falls back to a one-off hasher when the thread-local is unavailable, such as during thread-local destruction, and adds hashing_from_a_thread_local_destructor to cover it.
  • HMAC-SHA1 and HMAC-SHA256 remain one-shot through boring::hash::hmac_sha1 and boring::hash::hmac_sha256. Reusing an HMAC context would require unsafe calls into boring-sys (Hmac::finalize consumes the context and HmacCtx has no safe constructor); keeping this path safe was intentional.
  • CI builds the facade with --features boring and with --no-default-features --features boring,aws on Linux and macOS, and tests reqsign-core and reqsign-aws-v4 with the feature. The public reqsign_core::hash API is unchanged.

Could you let us know whether these changes address the concerns from your review, especially whether the one-shot HMAC choice is acceptable? CI is green on the current head.

@erickguan

erickguan commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

@Xuanwo We can merge the original commit fa98626. It's too complicated to use thread local for convenient functions. We can add a PR later for this. Linked issue.

@Xuanwo

Xuanwo commented Nov 19, 2026

Copy link
Copy Markdown
Member

Thanks for the work on #923. The contribution raised a concrete need to choose the hash backend for applications that use Reqsign through another library. We are discussing a process-wide provider that the application can install at startup, with RustCrypto built into reqsign-core. Please see the CryptoProvider discussion for the proposal and open API questions.

The functions in `reqsign_core::hash` keep their API and now go through a
private backend. RustCrypto stays the default. With the new `boring` feature,
SHA-1, SHA-256, HMAC-SHA1 and HMAC-SHA256 use BoringSSL through the `boring`
crate instead. The facade `reqsign` crate forwards the feature.

Known-answer tests (the standard SHA "abc" and empty-input digests, RFC 4231
and RFC 2202 HMAC, and the AWS SigV4 signing-key example) run on both
backends. CI runs the reqsign-core and reqsign-aws-v4 tests with the feature
enabled.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@erickguan
erickguan force-pushed the feat/core-boring-backend branch from e63b7ae to 562a4b2 Compare November 19, 2026 15:37
@erickguan
erickguan self-requested a review November 19, 2026 16:08
@Xuanwo
Xuanwo marked this pull request as draft November 19, 2026 16:09
@Xuanwo

Xuanwo commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Hi @erickguan, what do you think of #961? If it's possible, I don't want to add a boring feature.

I'm thinking of building reqsign-crypto-provider-boring

@erickguan

erickguan commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

@Xuanwo Of course. I thought you wanted to merge this one and work on #961. #961 can supersede this PR #923. And it will probably cover #957. I will reply in #961 too.

@Xuanwo

Xuanwo commented Nov 19, 2026

Copy link
Copy Markdown
Member

@Xuanwo Of course. I thought you wanted to merge this one and work on #961. #961 can supersede this PR #923. And it will probably cover #957. I will reply in #961 too.

Thank you @erickguan and @vovacf201! Sorry for not making this clear. I will start a PR soon and this should not block you for long.

@Xuanwo

Xuanwo commented Nov 20, 2026

Copy link
Copy Markdown
Member

Check #970 instead

This branch has not been deployed

No deployments
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.

3 participants