Skip to content

Upgrade hmac, sha2 and generic_array #313

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

Merged
merged 3 commits into from
Jan 15, 2018

Conversation

FauxFaux
Copy link
Contributor

@FauxFaux FauxFaux commented Jan 14, 2018

hmac has had a couple of changes:

  • around errors during ::new, which I've handled by propagating the only case as an io::Error(InvalidInput, ...)
  • .code() has become a move, so lift that out of some loops, etc.

Tests are green. Note to self: To run the tests, first you must run docker-compose up.

The only slightly scary change is the changes around referencing in *hi ^= prev;, which is something that's easy to get wrong, at least in my experience as a zero-star programmer.

fn hi(str: &[u8], salt: &[u8], i: u32) -> GenericArray<u8, U32> {
let mut hmac = Hmac::<Sha256>::new(str);
fn hi(str: &[u8], salt: &[u8], i: u32) -> io::Result<GenericArray<u8, U32>> {
let mut hmac = Hmac::<Sha256>::new(str)
Copy link
Owner

Choose a reason for hiding this comment

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

Under what conditions will Hmac fail to be constructed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The documentation implies it fails if it is unhappy with the key length, and, looking at the rust-postgres code, I decided that the user had sufficient control over the key to trigger this. As there's already a Result being returned from the public API, propagating the error would be the right thing to do.
https://docs.rs/hmac/0.5.0/hmac/trait.Mac.html#tymethod.new

However, the current hmac crate implementation does not actually validate the key length. It's unclear to me from the code why they've made the change to return a Result; perhaps to make the interface consistent with other crypto apis (e.g. cmac), which do validate the key length?

While (as far as I'm aware) it's not technically necessary to have "long" (>= digest size) keys in theory, it's always what you want to do in practice. As such, it's not inconceivable that the hmac crate would be updated to validate the key length is not 1 byte? Hence unnecessarily unwrap()ing seemed risky.

Copy link
Owner

Choose a reason for hiding this comment

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

Rejecting 1 byte keys wouldn't be a valid HMAC implementation IMO.

@newpavlov is Hmac::new actually fallible?

Choose a reason for hiding this comment

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

No, Hmac::new is infallible and will accept even zero sized keys.

Copy link
Owner

Choose a reason for hiding this comment

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

Thanks! Let's remove the error propagation here then, and just .expect("BUG") or whatever.

Choose a reason for hiding this comment

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

I think something like .expect("HMAC is able to accept all key sizes") will be better. :)

About why Result is used: Hmac is just one of implementations of Mac trait and other algorithms can have restrictions on key sizes (e.g. Cmac works only with fixed size keys). I hope one day with stabilized never_type and associated type defaults we will be able to express infallibility better.

@sfackler
Copy link
Owner

Thanks!

@sfackler sfackler merged commit 691eec2 into sfackler:master Jan 15, 2018
@FauxFaux FauxFaux deleted the update-sha2-0.7 branch January 15, 2018 20:16
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