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

Use little-endian encoding for Blake2 hashing on all architectures #38960

Merged
merged 1 commit into from
Jan 11, 2017

Conversation

arielb1
Copy link
Contributor

@arielb1 arielb1 commented Jan 10, 2017

Like many hash functions, the blake2 hash is mathematically defined on
a sequence of 64-bit words. As Rust's hash interface operates on
sequences of octets, some encoding must be used to bridge that
difference.

The Blake2 RFC (RFC 7693) specifies that:

   Byte (octet) streams are interpreted as words in little-endian order,
   with the least-significant byte first.

So use that encoding consistently.

Fixes #38891.

Beta-nominating since this is a regression since 1.15.

r? @michaelwoerister

Copy link
Member

@michaelwoerister michaelwoerister left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!
I think that one comment is should say little-endian, instead of big endian. And we probably should a comment that all data going in must be converted to little-endian (which the hasher should already do).

if cfg!(target_endian = "big") {
for word in &mut m[..] {
*word = word.to_be();
*word = u64::from_le(*word);
Copy link
Member

Choose a reason for hiding this comment

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

Good catch!

let m: &mut [u64; 16] = unsafe {
let b: &mut [u8; 128] = &mut ctx.b;
::std::mem::transmute(b)
};

// It's OK to modify the buffer in place since this is the last time
// this data will be accessed before it's overwritten
Copy link
Member

Choose a reason for hiding this comment

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

Why remove this comment?

// Re-interpret the input buffer in the state as u64s
// Re-interpret the input buffer in the state as
// an array of big-endian u64s, converting them
// to machine endianness.
Copy link
Member

Choose a reason for hiding this comment

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

Isn't it the other way round: We always make sure that this buffer is little-endian and now we are making sure to convert it to machine endianess?

@arielb1
Copy link
Contributor Author

arielb1 commented Jan 10, 2017

Fixed nits.

And we probably should a comment that all data going in must be converted to little-endian (which the hasher should already do).

Blake2b is a standard bytestream hasher. It should not do anything weird with its Hasher impl.

@bors r=michaelwoerister

@bors
Copy link
Contributor

bors commented Jan 10, 2017

📌 Commit bccd756 has been approved by michaelwoerister

@arielb1 arielb1 added the beta-nominated Nominated for backporting to the compiler in the beta channel. label Jan 10, 2017
@arielb1 arielb1 changed the title Use big-endian encoding for Blake2 hashing on all architectures Use little-endian encoding for Blake2 hashing on all architectures Jan 10, 2017
Like many hash functions, the blake2 hash is mathematically defined on
a sequence of 64-bit words. As Rust's hash interface operates on
sequences of octets, some encoding must be used to bridge that
difference.

The Blake2 RFC (RFC 7693) specifies that:
   Byte (octet) streams are interpreted as words in little-endian order,
   with the least-significant byte first.

So use that encoding consistently.

Fixes rust-lang#38891.
@arielb1
Copy link
Contributor Author

arielb1 commented Jan 10, 2017

@bors r=michaelwoerister

@bors
Copy link
Contributor

bors commented Jan 10, 2017

📌 Commit a89475d has been approved by michaelwoerister

@arielb1
Copy link
Contributor Author

arielb1 commented Jan 10, 2017

@bors rollup

@nikomatsakis
Copy link
Contributor

@bors rollup-

@nikomatsakis
Copy link
Contributor

@bors p=1

  • Since this will likely go into beta, we need it to land ASAP

@nikomatsakis nikomatsakis added the beta-accepted Accepted for backporting to the compiler in the beta channel. label Jan 11, 2017
@nikomatsakis
Copy link
Contributor

Accepting for beta. Small patch, regression. cc @rust-lang/compiler

@bors
Copy link
Contributor

bors commented Jan 11, 2017

⌛ Testing commit a89475d with merge e4fee52...

bors added a commit that referenced this pull request Jan 11, 2017
Use little-endian encoding for Blake2 hashing on all architectures

Like many hash functions, the blake2 hash is mathematically defined on
a sequence of 64-bit words. As Rust's hash interface operates on
sequences of octets, some encoding must be used to bridge that
difference.

The Blake2 RFC (RFC 7693) specifies that:
```
   Byte (octet) streams are interpreted as words in little-endian order,
   with the least-significant byte first.
```

So use that encoding consistently.

Fixes #38891.

Beta-nominating since this is a regression since 1.15.

r? @michaelwoerister
@bors
Copy link
Contributor

bors commented Jan 11, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: michaelwoerister
Pushing e4fee52 to master...

@bors bors merged commit a89475d into rust-lang:master Jan 11, 2017
alexcrichton added a commit that referenced this pull request Jan 12, 2017
@alexcrichton alexcrichton removed the beta-nominated Nominated for backporting to the compiler in the beta channel. label Jan 12, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
beta-accepted Accepted for backporting to the compiler in the beta channel.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants