Skip to content

Rework KDF interface#1551

Merged
ctz merged 9 commits into
mainfrom
jbp-kdf-interface
Oct 26, 2023
Merged

Rework KDF interface#1551
ctz merged 9 commits into
mainfrom
jbp-kdf-interface

Conversation

@ctz
Copy link
Copy Markdown
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
Copy Markdown

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

@ctz ctz force-pushed the jbp-kdf-interface branch from 27667ef to a5b4b8b Compare October 24, 2023 16:25
Comment thread rustls/src/crypto/tls12.rs Outdated
Comment thread rustls/src/crypto/tls12.rs Outdated
Comment thread rustls/src/crypto/tls12.rs Outdated
Comment thread rustls/src/crypto/tls13.rs Outdated
Comment thread rustls/src/crypto/tls12.rs Outdated
Comment thread rustls/src/crypto/tls13.rs Outdated
Comment thread rustls/src/crypto/tls13.rs Outdated
Comment thread rustls/src/crypto/tls13.rs Outdated
Comment thread rustls/src/crypto/tls13.rs Outdated
@ctz ctz force-pushed the jbp-kdf-interface branch from a5b4b8b to 0e5a64f Compare October 25, 2023 10:53
Comment thread rustls/tests/api.rs
}

#[test]
fn test_tls13_exporter_maximum_output_length() {
Copy link
Copy Markdown
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
Copy Markdown
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!

Comment thread rustls/src/crypto/tls12.rs Outdated
Comment thread rustls/src/crypto/tls12.rs Outdated
Comment thread rustls/src/crypto/tls12.rs Outdated
Comment thread rustls/src/crypto/tls12.rs Outdated
Comment thread rustls/src/crypto/tls12.rs Outdated
Comment thread provider-example/src/lib.rs
Comment thread rustls/src/quic.rs
Comment thread rustls/src/crypto/ring/quic.rs Outdated
Comment thread rustls/src/tls13/key_schedule.rs Outdated
Comment thread rustls/src/tls13/mod.rs
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 🤷‍♂️

@ctz ctz force-pushed the jbp-kdf-interface branch from 5ec056f to ed5b577 Compare October 26, 2023 09:07
ctz added 8 commits October 26, 2023 10:08
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.
@ctz ctz force-pushed the jbp-kdf-interface branch from ed5b577 to 82f1ccc Compare October 26, 2023 09:08
@djc
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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!

Comment thread rustls/src/crypto/ring/tls13.rs Outdated
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 force-pushed the jbp-kdf-interface branch from 57ade5b to 1fd7697 Compare October 26, 2023 11:03
@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
@ctz ctz deleted the jbp-kdf-interface branch October 26, 2023 11:19
@aochagavia
Copy link
Copy Markdown
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.

4 participants