-
-
Notifications
You must be signed in to change notification settings - Fork 395
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
[#783] Encrypt retry token #833
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, thanks for taking a look at this! That's some pretty involved code for your first Rust project. 👍
Can you give more context on what the design is here? Which of the proposals as made by @Ralith in the original issue did you follow, and why?
Thanks 🙂 but I hope it is going in the right direction!
Sure, as I understand the issue #783 proposes to use AEAD instead of HMAC for token authentication (plus encryption), namely following proposal has been made,
A
The
I chose to place |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! This will be a nice additional layer of security and robustness. What you've got so far is definitely on the right track.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working on this! I tried to answer your question and wanted to point out some other things I noticed while trying to figure out your code. Hope that helps, let me know if you have further questions.
(Also, since the commits so far don't seem to be logically distinct, we'd prefer if you squash them into a single one.) |
Sounds good. Once this PR gets to a point where it can be merged I will squash the commits 👍 |
fe47254
to
fb41600
Compare
@djc @Ralith CI build fail with the following error
Seems like it cannot compile |
This is the issue tracked in #839. You should probably just increase the type length limit for the interop crate for now. |
A work-around for the build issue was merged in #844, so this should be unblocked with a rebase! |
fb41600
to
964c005
Compare
I suggest using something like XChaCha20-Poly1305 with a random nonce instead of the current ad-hoc construction. XChaCha20-Poly1305 has a 192-bit nonce, so the likelihood of reuse is negligible. The nonce should be randomly generated for the first use, and then incremented for all subsequent encryptions. |
If we could easily do that, the usual 96 bit nonce would be plenty. The problem is there's no way to recover the nonce used by such a scheme when validating a client-supplied retry token without defeating the goal of producing a retry token indistinguishable from random data. I also don't think the risk of a custom construction here is high. In the unlikely event that a third party manages to decrypt a retry token, they don't learn anything useful. The main goal here is just future-proofing: nobody can bake knowledge of the token's structure into a middlebox if we don't expose any structure. |
@Ralith a 96-bit nonce would NOT be plenty, as the birthday bound for it is too low. However, if you use a nonce that is chosen randomly for every encryption, then the nonce itself is indistinguishable from random bits and can be made part of the retry token itself. |
Randomly generating each use has different dynamics than incrementing each use as you suggested in your prior post, yes. I still don't see the need to rewrite this PR and use a cipher unsupported by ring when the current approach works fine and there's little at risk, in any case. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few final tweaks!
964c005
to
4022e8d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Two more cosmetic issues, then please squash.
cfc5948
to
01134c5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your work!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the work! One hopefully minor concern and, if you work on that, a tiny nit to go along with it.
@@ -43,46 +50,59 @@ impl RetryToken { | |||
.unwrap_or(0), | |||
); | |||
|
|||
let signature_pos = buf.len(); | |||
let mut additional_data = [0u8; Self::MAX_ADDITIONAL_DATA_SIZE]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be viable to factor out some of the duplication between encode()
and from_bytes()
in writing out the additional_data
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure 🙂
} | ||
.encode(&*server_config.token_key, &remote, &temp_loc_cid); | ||
let mut buf = Vec::new(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: moving the initialization of buf
around here seems orthogonal to the rest of this change. If it's not too much of hassle, maybe revert it?
Linting seems to fail, its mostly suggesting to use |
Feel free to ignore, I'll fix that up in a separate PR. |
okay, then I guess the PR can approved and merged. |
74a6296
to
d29bea3
Compare
d29bea3
to
6c5a219
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took the liberty of rebasing this on top of current main to deal with some new conflicts, and split out my formatting nit into a separate commit. Thanks for working on this!
Merged despite some new clippy problems which are addressed in #864. |
Thanks for your contribution! It's great to have this checked off, and it's super cool that you were willing to take it on as a learning project! |
DISCLAIMER: I am a first time contributor (Rust and QUIC).
I would like some feedback on the direction of this 🙂
Resolves #783 , following changes have been introduced,
master_key
has been added toServerConfig
for aiding AEAD key derivationAeadKey
has been introduced which provided following API,from_hkdf
: derive a AEAD key usingmaster_key
and somerandom_bytes
seal
: seal a message with the given AEAD keyopen
: open a message with a given AEAD keyTODO[test]
scenario when a garbage retry token is received and the opening operation fails