Skip to content

Commit 5ec97e3

Browse files
committed
interface changes
1 parent 5362726 commit 5ec97e3

File tree

2 files changed

+28
-25
lines changed

2 files changed

+28
-25
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ native = []
99

1010
[dependencies]
1111
arrayvec = "0.4.5"
12-
byteorder = "1.1.0"
1312
constant_time_eq = "0.1.3"
1413

1514
[build-dependencies]

src/lib.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
extern crate arrayvec;
2-
extern crate byteorder;
32
extern crate constant_time_eq;
43

54
use std::mem;
5+
use std::os::raw::c_void;
66
use arrayvec::{ArrayVec, ArrayString};
77
use constant_time_eq::constant_time_eq;
88

@@ -27,11 +27,11 @@ pub fn blake2s_256(input: &[u8]) -> blake2s::Digest {
2727
pub mod blake2b {
2828
use super::*;
2929

30-
pub const BLOCKBYTES: usize = sys::BLAKE2B_BLOCKBYTES as usize;
31-
pub const OUTBYTES: usize = sys::BLAKE2B_OUTBYTES as usize;
32-
pub const KEYBYTES: usize = sys::BLAKE2B_KEYBYTES as usize;
33-
pub const SALTBYTES: usize = sys::BLAKE2B_SALTBYTES as usize;
34-
pub const PERSONALBYTES: usize = sys::BLAKE2B_PERSONALBYTES as usize;
30+
pub const BLOCKBYTES: usize = sys::blake2b_constant_BLAKE2B_BLOCKBYTES as usize;
31+
pub const OUTBYTES: usize = sys::blake2b_constant_BLAKE2B_OUTBYTES as usize;
32+
pub const KEYBYTES: usize = sys::blake2b_constant_BLAKE2B_KEYBYTES as usize;
33+
pub const SALTBYTES: usize = sys::blake2b_constant_BLAKE2B_SALTBYTES as usize;
34+
pub const PERSONALBYTES: usize = sys::blake2b_constant_BLAKE2B_PERSONALBYTES as usize;
3535

3636
// TODO: Clone, Debug
3737
pub struct Builder {
@@ -50,6 +50,7 @@ pub mod blake2b {
5050
depth: 1,
5151
leaf_length: 0,
5252
node_offset: 0,
53+
xof_length: 0,
5354
node_depth: 0,
5455
inner_length: 0,
5556
reserved: [0; 14],
@@ -122,8 +123,10 @@ pub mod blake2b {
122123
}
123124

124125
pub fn node_offset(&mut self, offset: u64) -> &mut Self {
125-
// NOTE: Tricky endianness issues, https://github.com/BLAKE2/libb2/issues/12.
126-
self.params.node_offset = offset.to_le();
126+
// The version of "blake2.h" we're using includes the xof_length
127+
// param from BLAKE2X, which occupies the high bits of node_offset.
128+
self.params.node_offset = offset as u32;
129+
self.params.xof_length = (offset >> 32) as u32;
127130
self
128131
}
129132

@@ -187,7 +190,7 @@ pub mod blake2b {
187190

188191
pub fn update(&mut self, input: &[u8]) -> &mut Self {
189192
unsafe {
190-
sys::blake2b_update(&mut self.0, input.as_ptr(), input.len());
193+
sys::blake2b_update(&mut self.0, input.as_ptr() as *const c_void, input.len());
191194
}
192195
self
193196
}
@@ -198,8 +201,8 @@ pub mod blake2b {
198201
pub fn finalize(&mut self) -> Digest {
199202
let mut bytes = ArrayVec::new();
200203
unsafe {
201-
bytes.set_len(self.0.outlen as usize);
202-
sys::blake2b_final(&mut self.0, bytes.as_mut_ptr(), bytes.len());
204+
bytes.set_len(self.0.outlen);
205+
sys::blake2b_final(&mut self.0, bytes.as_mut_ptr() as *mut c_void, bytes.len());
203206
}
204207
Digest { bytes }
205208
}
@@ -248,11 +251,11 @@ pub mod blake2b {
248251
pub mod blake2s {
249252
use super::*;
250253

251-
pub const BLOCKBYTES: usize = sys::BLAKE2S_BLOCKBYTES as usize;
252-
pub const OUTBYTES: usize = sys::BLAKE2S_OUTBYTES as usize;
253-
pub const KEYBYTES: usize = sys::BLAKE2S_KEYBYTES as usize;
254-
pub const SALTBYTES: usize = sys::BLAKE2S_SALTBYTES as usize;
255-
pub const PERSONALBYTES: usize = sys::BLAKE2S_PERSONALBYTES as usize;
254+
pub const BLOCKBYTES: usize = sys::blake2s_constant_BLAKE2S_BLOCKBYTES as usize;
255+
pub const OUTBYTES: usize = sys::blake2s_constant_BLAKE2S_OUTBYTES as usize;
256+
pub const KEYBYTES: usize = sys::blake2s_constant_BLAKE2S_KEYBYTES as usize;
257+
pub const SALTBYTES: usize = sys::blake2s_constant_BLAKE2S_SALTBYTES as usize;
258+
pub const PERSONALBYTES: usize = sys::blake2s_constant_BLAKE2S_PERSONALBYTES as usize;
256259

257260
// TODO: Clone, Debug
258261
pub struct Builder {
@@ -270,7 +273,8 @@ pub mod blake2s {
270273
fanout: 1,
271274
depth: 1,
272275
leaf_length: 0,
273-
node_offset: [0; 6],
276+
node_offset: 0,
277+
xof_length: 0,
274278
node_depth: 0,
275279
inner_length: 0,
276280
salt: [0; SALTBYTES],
@@ -342,13 +346,13 @@ pub mod blake2s {
342346
}
343347

344348
pub fn node_offset(&mut self, offset: u64) -> &mut Self {
345-
use byteorder::{ByteOrder, LittleEndian};
349+
// The version of "blake2.h" we're using includes the xof_length
350+
// param from BLAKE2X, which occupies the high bits of node_offset.
346351
if offset > ((1 << 48) - 1) {
347352
panic!("Bad node offset: {}", offset);
348353
}
349-
let mut buf = [0; 8];
350-
LittleEndian::write_u64(&mut buf, offset);
351-
self.params.node_offset[..].copy_from_slice(&buf[..6]);
354+
self.params.node_offset = offset as u32;
355+
self.params.xof_length = (offset >> 32) as u16;
352356
self
353357
}
354358

@@ -412,7 +416,7 @@ pub mod blake2s {
412416

413417
pub fn update(&mut self, input: &[u8]) -> &mut Self {
414418
unsafe {
415-
sys::blake2s_update(&mut self.0, input.as_ptr(), input.len());
419+
sys::blake2s_update(&mut self.0, input.as_ptr() as *const c_void, input.len());
416420
}
417421
self
418422
}
@@ -423,8 +427,8 @@ pub mod blake2s {
423427
pub fn finalize(&mut self) -> Digest {
424428
let mut bytes = ArrayVec::new();
425429
unsafe {
426-
bytes.set_len(self.0.outlen as usize);
427-
sys::blake2s_final(&mut self.0, bytes.as_mut_ptr(), bytes.len());
430+
bytes.set_len(self.0.outlen);
431+
sys::blake2s_final(&mut self.0, bytes.as_mut_ptr() as *mut c_void, bytes.len());
428432
}
429433
Digest { bytes }
430434
}

0 commit comments

Comments
 (0)