1
1
extern crate arrayvec;
2
- extern crate byteorder;
3
2
extern crate constant_time_eq;
4
3
5
4
use std:: mem;
5
+ use std:: os:: raw:: c_void;
6
6
use arrayvec:: { ArrayVec , ArrayString } ;
7
7
use constant_time_eq:: constant_time_eq;
8
8
@@ -27,11 +27,11 @@ pub fn blake2s_256(input: &[u8]) -> blake2s::Digest {
27
27
pub mod blake2b {
28
28
use super :: * ;
29
29
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 ;
35
35
36
36
// TODO: Clone, Debug
37
37
pub struct Builder {
@@ -50,6 +50,7 @@ pub mod blake2b {
50
50
depth : 1 ,
51
51
leaf_length : 0 ,
52
52
node_offset : 0 ,
53
+ xof_length : 0 ,
53
54
node_depth : 0 ,
54
55
inner_length : 0 ,
55
56
reserved : [ 0 ; 14 ] ,
@@ -122,8 +123,10 @@ pub mod blake2b {
122
123
}
123
124
124
125
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 ;
127
130
self
128
131
}
129
132
@@ -187,7 +190,7 @@ pub mod blake2b {
187
190
188
191
pub fn update ( & mut self , input : & [ u8 ] ) -> & mut Self {
189
192
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 ( ) ) ;
191
194
}
192
195
self
193
196
}
@@ -198,8 +201,8 @@ pub mod blake2b {
198
201
pub fn finalize ( & mut self ) -> Digest {
199
202
let mut bytes = ArrayVec :: new ( ) ;
200
203
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 ( ) ) ;
203
206
}
204
207
Digest { bytes }
205
208
}
@@ -248,11 +251,11 @@ pub mod blake2b {
248
251
pub mod blake2s {
249
252
use super :: * ;
250
253
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 ;
256
259
257
260
// TODO: Clone, Debug
258
261
pub struct Builder {
@@ -270,7 +273,8 @@ pub mod blake2s {
270
273
fanout : 1 ,
271
274
depth : 1 ,
272
275
leaf_length : 0 ,
273
- node_offset : [ 0 ; 6 ] ,
276
+ node_offset : 0 ,
277
+ xof_length : 0 ,
274
278
node_depth : 0 ,
275
279
inner_length : 0 ,
276
280
salt : [ 0 ; SALTBYTES ] ,
@@ -342,13 +346,13 @@ pub mod blake2s {
342
346
}
343
347
344
348
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.
346
351
if offset > ( ( 1 << 48 ) - 1 ) {
347
352
panic ! ( "Bad node offset: {}" , offset) ;
348
353
}
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 ;
352
356
self
353
357
}
354
358
@@ -412,7 +416,7 @@ pub mod blake2s {
412
416
413
417
pub fn update ( & mut self , input : & [ u8 ] ) -> & mut Self {
414
418
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 ( ) ) ;
416
420
}
417
421
self
418
422
}
@@ -423,8 +427,8 @@ pub mod blake2s {
423
427
pub fn finalize ( & mut self ) -> Digest {
424
428
let mut bytes = ArrayVec :: new ( ) ;
425
429
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 ( ) ) ;
428
432
}
429
433
Digest { bytes }
430
434
}
0 commit comments