Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework KDF interface #1551

Merged
merged 9 commits into from Oct 26, 2023
Merged

Rework KDF interface #1551

merged 9 commits into from Oct 26, 2023

Conversation

ctz
Copy link
Member

@ctz ctz commented Oct 24, 2023

Previously we implemented the TLS1.2 PRF and HKDF ourselves given something that impl rustls::crypto::hmac::Hmac. As outlined in #1540 (comment) this isn't the right approach for FIPS.

This puts both those dependencies behind new traits, referenced from the cipher suite types. The existing implementations we have are recast to be in terms of these traits, and continue to take an dyn hmac::Hmac. That means writing a crypto provider continues to be as easy as it was previously.

@codecov
Copy link

codecov bot commented Oct 24, 2023

Codecov Report

Merging #1551 (1fd7697) into main (60420c5) will decrease coverage by 0.18%.
The diff coverage is 94.05%.

@@            Coverage Diff             @@
##             main    #1551      +/-   ##
==========================================
- Coverage   96.51%   96.34%   -0.18%     
==========================================
  Files          75       75              
  Lines       15355    15521     +166     
==========================================
+ Hits        14820    14953     +133     
- Misses        535      568      +33     
Files Coverage Δ
rustls/src/client/tls13.rs 97.05% <100.00%> (-0.01%) ⬇️
rustls/src/crypto/mod.rs 100.00% <ø> (ø)
rustls/src/crypto/ring/quic.rs 99.68% <100.00%> (+<0.01%) ⬆️
rustls/src/crypto/ring/tls12.rs 97.75% <ø> (ø)
rustls/src/crypto/ring/tls13.rs 100.00% <100.00%> (ø)
rustls/src/crypto/tls12.rs 100.00% <100.00%> (ø)
rustls/src/quic.rs 77.74% <100.00%> (+1.09%) ⬆️
rustls/src/server/tls13.rs 96.97% <100.00%> (-0.01%) ⬇️
rustls/src/tls12/mod.rs 97.73% <100.00%> (-0.13%) ⬇️
rustls/src/tls13/key_schedule.rs 99.74% <100.00%> (+0.01%) ⬆️
... and 2 more

... and 1 file with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

rustls/src/crypto/tls12.rs Outdated Show resolved Hide resolved
rustls/src/crypto/tls12.rs Outdated Show resolved Hide resolved
rustls/src/crypto/tls12.rs Outdated Show resolved Hide resolved
rustls/src/crypto/tls13.rs Outdated Show resolved Hide resolved
rustls/src/crypto/tls12.rs Outdated Show resolved Hide resolved
rustls/src/crypto/tls13.rs Outdated Show resolved Hide resolved
rustls/src/crypto/tls13.rs Outdated Show resolved Hide resolved
rustls/src/crypto/tls13.rs Outdated Show resolved Hide resolved
rustls/src/crypto/tls13.rs Outdated Show resolved Hide resolved
@@ -2422,6 +2422,54 @@ fn test_tls13_exporter() {
}
}

#[test]
fn test_tls13_exporter_maximum_output_length() {
Copy link
Member Author

Choose a reason for hiding this comment

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

nb. this test uncovers a panic against rel-0.21. Since nobody has reported it, and I think it's highly unlikely to be reachable except deliberately writing this code (in which case, why not write panic!() and save some effort) I don't think it's a pressing concern. However this test could be a backport candidate if we choose to fix that.

Copy link
Member

@djc djc left a comment

Choose a reason for hiding this comment

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

Good stuff!

rustls/src/crypto/tls12.rs Outdated Show resolved Hide resolved
rustls/src/crypto/tls12.rs Outdated Show resolved Hide resolved
rustls/src/crypto/tls12.rs Outdated Show resolved Hide resolved
rustls/src/crypto/tls12.rs Outdated Show resolved Hide resolved
rustls/src/crypto/tls12.rs Outdated Show resolved Hide resolved
provider-example/src/lib.rs Show resolved Hide resolved
rustls/src/quic.rs Show resolved Hide resolved
rustls/src/crypto/ring/quic.rs Outdated Show resolved Hide resolved
rustls/src/tls13/key_schedule.rs Outdated Show resolved Hide resolved
@@ -15,6 +15,9 @@ pub struct Tls13CipherSuite {
/// How to compute HMAC with the suite's hash function.
pub hmac_provider: &'static dyn crypto::hmac::Hmac,

/// How to complete HKDF with the suite's hash function.
pub hkdf_provider: &'static dyn crypto::tls13::Hkdf,
Copy link
Member

Choose a reason for hiding this comment

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

It looks like hmac_provider has only one user left after this. Is there a way to get rid of it here?

Copy link
Member Author

Choose a reason for hiding this comment

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

It would be a slight confusion of the tls13::Hkdf interface to add it there, but on the other hand it would be a much simpler result than requiring the whole of hmac::Hmac when only a tiny proportion of it is actually used. Hmm.

Copy link
Member Author

Choose a reason for hiding this comment

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

Have added a final commit which tries this. Seems a bit simpler, but I think it might need a bit of renaming of trait Hkdf to look sensible?

Copy link
Member

@djc djc Oct 26, 2023

Choose a reason for hiding this comment

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

Yeah, I like the changes but can't think of a better name either -- I guess maybe something like HmacOperations or HashOps? Just leave it for now?

@ctz ctz force-pushed the jbp-kdf-interface branch 2 times, most recently from 7d4a0d8 to e8997f9 Compare October 25, 2023 16:37
Copy link
Member

@cpu cpu left a comment

Choose a reason for hiding this comment

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

Nice work!

It comes at the expense of trait Hkdf having a weird bit bolted on. Maybe we rename it?

It doesn't feel too out of place to me, and I'm not sure I can think of a better name 🤷‍♂️

This replaces the HMAC trait in Tls12CipherSuite
(there were no other uses of HMAC).

Provide an implementation of the new PRF trait in terms of
HMAC, for convenience of providers that have a HMAC (common)
but not a separate TLS1.2 PRF (relatively uncommon).  The
*ring* and `provider-example/` providers use this.
Have an impl of this for hmac::Hmac
This means `Hkdf` covers the entire use of TLS1.3
for HMAC/HKDF, and that avoids having to implement
the HMAC traits just for this.
@djc
Copy link
Member

djc commented Oct 26, 2023

Consistent but relatively small perf regression for handshake latency. @aochagavia should maybe get a run for wall clock benchmarks?

@ctz
Copy link
Member Author

ctz commented Oct 26, 2023

Consistent but relatively small perf regression for handshake latency

I think this was due to the extra Box allocations introduced in the additional indirection. I've pushed an additional commit (sorry!) that goes back to using the ring hkdf API; this turns the ~0.3% regression into a ~0.6% improvement compared to main.

Copy link
Member

@djc djc left a comment

Choose a reason for hiding this comment

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

Awesome!

rustls/src/crypto/ring/tls13.rs Outdated Show resolved Hide resolved
Now we no longer have a depedency on hmac, we can avoid that
and save some heap allocations.

This marginally improves TLS1.3 handshake performance.
@ctz ctz enabled auto-merge October 26, 2023 11:03
@ctz ctz added this pull request to the merge queue Oct 26, 2023
Merged via the queue into main with commit 0013a08 Oct 26, 2023
39 of 42 checks passed
@ctz ctz deleted the jbp-kdf-interface branch October 26, 2023 11:19
@aochagavia
Copy link
Contributor

Great to see the icount stuff is paying off :)

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.

None yet

4 participants