Skip to content

feat(hmac): Add generate_with_length method to hmac::Key #1218

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

littledivy
Copy link

@codecov
Copy link

codecov bot commented Mar 10, 2021

Codecov Report

Merging #1218 (02a452c) into main (9385c6d) will decrease coverage by 0.07%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1218      +/-   ##
==========================================
- Coverage   93.65%   93.57%   -0.08%     
==========================================
  Files         115      116       +1     
  Lines       17468    17459       -9     
==========================================
- Hits        16359    16338      -21     
- Misses       1109     1121      +12     
Impacted Files Coverage Δ
src/polyfill.rs 100.00% <ø> (ø)
src/aead/chacha.rs 99.42% <100.00%> (-0.03%) ⬇️
src/digest.rs 98.35% <100.00%> (-0.15%) ⬇️
src/digest/sha1.rs 100.00% <100.00%> (ø)
src/digest/sha2.rs 100.00% <100.00%> (ø)
src/hmac.rs 93.33% <100.00%> (+2.85%) ⬆️
src/polyfill/array_map.rs 100.00% <100.00%> (ø)
src/hkdf.rs 83.69% <0.00%> (-9.08%) ⬇️
src/error.rs 86.00% <0.00%> (-5.49%) ⬇️
src/test.rs 89.02% <0.00%> (-0.39%) ⬇️
... and 10 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 521081f...02a452c. Read the comment docs.

src/hmac.rs Outdated
where
F: FnOnce(&mut [u8]) -> Result<(), error::Unspecified>,
{
let mut key_bytes = [0; digest::MAX_OUTPUT_LEN];
let key_bytes = &mut key_bytes[..algorithm.0.output_len];
Copy link
Owner

Choose a reason for hiding this comment

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

This doesn't work when the requested length is larger than digest::MAX_OUTPUT_LEN.

Please write a parameterized test that accepts an hmac::Algorithm and a length, and then tests your new function with that algorithm and length, for the following lengths:
1, 2, output_len - 1, output_len, output_len + 1, block_len - 1, block_len, block_len + 1, (block_len * 2) - 1, block_len * 2, (block_len * 2) + 1.

Copy link
Author

Choose a reason for hiding this comment

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

Updated logic and added tests.

Copy link
Owner

Choose a reason for hiding this comment

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

You can't add a call to to_vec to construct because construct needs to be able to work even in environments that doesn't have a memory allocator. Instead you need to move the allocation to your new constructor and mark that constructor #[cfg(feature = "alloc")].

Copy link
Author

Choose a reason for hiding this comment

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

Ah nice catch! Moved new construct logic to generate_with_length behind the alloc feature 👍

Comment on lines +426 to +431
let _key = hmac::Key::generate_with_length(
*algorithm,
NonZeroUsize::new(*length).unwrap(),
&rng,
)
.unwrap();
Copy link
Author

Choose a reason for hiding this comment

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

Not sure how to assert the key length with the expected one. 🤔

@@ -364,4 +396,40 @@ mod tests {
assert!(hmac::verify(&key, HELLO_WORLD_BAD, tag.as_ref()).is_err())
}
}

#[test]
pub fn hmac_generate_with_length() {
Copy link
Owner

Choose a reason for hiding this comment

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

Tests of the public HMAC API should go in hmac_tests.rs.

I will write a test for the original generate() that you can emulate.

Copy link
Owner

@briansmith briansmith left a comment

Choose a reason for hiding this comment

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

Are you still interested in this? Sorry for the long delay.

let mut key_bytes = [0; digest::MAX_OUTPUT_LEN].to_vec();
if key_size > digest::MAX_OUTPUT_LEN {
key_bytes.resize(key_size, 0);
}
Copy link
Owner

Choose a reason for hiding this comment

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

I don't understand why we would truncate the length of the key to MAX_OUTPUT_LEN. The user asked for a key of length length and so I guess they should get a key of that length; i.e. without this truncation. Or if there's no need for such large keys then we'd return an error. But I don't think we'd ever silently truncate the key length.

rng: &dyn rand::SecureRandom,
) -> Result<Self, error::Unspecified> {
let key_size = length.get();
let mut key_bytes = [0; digest::MAX_OUTPUT_LEN].to_vec();
Copy link
Owner

Choose a reason for hiding this comment

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

In the long run, we really should avoid heap allocation here, especially just to support huge key sizes. This makes the function much harder to implement. OTOH, if the caller is willing to do a heap allocation then you can just construct a random array using ring::rand and then call ::new(algorithm, ...) instead.

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.

2 participants