@@ -50,10 +50,12 @@ pub struct Blake2b {
50
50
last_node : u8 ,
51
51
digest_length : u8 ,
52
52
computed : bool , // whether the final digest has been computed
53
+ param : Blake2bParam
53
54
}
54
55
55
56
impl Clone for Blake2b { fn clone ( & self ) -> Blake2b { * self } }
56
57
58
+ #[ derive( Copy , Clone ) ]
57
59
struct Blake2bParam {
58
60
digest_length : u8 ,
59
61
key_length : u8 ,
@@ -108,7 +110,7 @@ impl Blake2b {
108
110
self . t [ 1 ] += if self . t [ 0 ] < inc { 1 } else { 0 } ;
109
111
}
110
112
111
- fn init0 ( digest_length : u8 , key : & [ u8 ] ) -> Blake2b {
113
+ fn init0 ( param : Blake2bParam , digest_length : u8 , key : & [ u8 ] ) -> Blake2b {
112
114
assert ! ( key. len( ) <= BLAKE2B_KEYBYTES ) ;
113
115
let mut b = Blake2b {
114
116
h : IV ,
@@ -120,30 +122,31 @@ impl Blake2b {
120
122
digest_length : digest_length,
121
123
computed : false ,
122
124
key : [ 0 ; BLAKE2B_KEYBYTES ] ,
123
- key_length : key. len ( ) as u8
125
+ key_length : key. len ( ) as u8 ,
126
+ param : param
124
127
} ;
125
128
copy_memory ( key, & mut b. key ) ;
126
129
b
127
130
}
128
131
129
- fn apply_param ( & mut self , p : & Blake2bParam ) {
132
+ fn apply_param ( & mut self ) {
130
133
use std:: io:: Write ;
131
134
use cryptoutil:: WriteExt ;
132
135
133
136
let mut param_bytes : [ u8 ; 64 ] = [ 0 ; 64 ] ;
134
137
{
135
138
let mut writer: & mut [ u8 ] = & mut param_bytes;
136
- writer. write_u8 ( p . digest_length ) . unwrap ( ) ;
137
- writer. write_u8 ( p . key_length ) . unwrap ( ) ;
138
- writer. write_u8 ( p . fanout ) . unwrap ( ) ;
139
- writer. write_u8 ( p . depth ) . unwrap ( ) ;
140
- writer. write_u32_le ( p . leaf_length ) . unwrap ( ) ;
141
- writer. write_u64_le ( p . node_offset ) . unwrap ( ) ;
142
- writer. write_u8 ( p . node_depth ) . unwrap ( ) ;
143
- writer. write_u8 ( p . inner_length ) . unwrap ( ) ;
144
- writer. write_all ( & p . reserved ) . unwrap ( ) ;
145
- writer. write_all ( & p . salt ) . unwrap ( ) ;
146
- writer. write_all ( & p . personal ) . unwrap ( ) ;
139
+ writer. write_u8 ( self . param . digest_length ) . unwrap ( ) ;
140
+ writer. write_u8 ( self . param . key_length ) . unwrap ( ) ;
141
+ writer. write_u8 ( self . param . fanout ) . unwrap ( ) ;
142
+ writer. write_u8 ( self . param . depth ) . unwrap ( ) ;
143
+ writer. write_u32_le ( self . param . leaf_length ) . unwrap ( ) ;
144
+ writer. write_u64_le ( self . param . node_offset ) . unwrap ( ) ;
145
+ writer. write_u8 ( self . param . node_depth ) . unwrap ( ) ;
146
+ writer. write_u8 ( self . param . inner_length ) . unwrap ( ) ;
147
+ writer. write_all ( & self . param . reserved ) . unwrap ( ) ;
148
+ writer. write_all ( & self . param . salt ) . unwrap ( ) ;
149
+ writer. write_all ( & self . param . personal ) . unwrap ( ) ;
147
150
}
148
151
149
152
let mut param_words : [ u64 ; 8 ] = [ 0 ; 8 ] ;
@@ -155,9 +158,9 @@ impl Blake2b {
155
158
156
159
157
160
// init xors IV with input parameter block
158
- fn init_param ( p : & Blake2bParam , key : & [ u8 ] ) -> Blake2b {
159
- let mut b = Blake2b :: init0 ( p. digest_length , key) ;
160
- b. apply_param ( p ) ;
161
+ fn init_param ( p : Blake2bParam , key : & [ u8 ] ) -> Blake2b {
162
+ let mut b = Blake2b :: init0 ( p, p . digest_length , key) ;
163
+ b. apply_param ( ) ;
161
164
b
162
165
}
163
166
@@ -179,7 +182,7 @@ impl Blake2b {
179
182
180
183
pub fn new ( outlen : usize ) -> Blake2b {
181
184
assert ! ( outlen > 0 && outlen <= BLAKE2B_OUTBYTES ) ;
182
- Blake2b :: init_param ( & Blake2b :: default_param ( outlen as u8 ) , & [ ] )
185
+ Blake2b :: init_param ( Blake2b :: default_param ( outlen as u8 ) , & [ ] )
183
186
}
184
187
185
188
fn apply_key ( & mut self ) {
@@ -207,7 +210,7 @@ impl Blake2b {
207
210
personal : [ 0 ; BLAKE2B_PERSONALBYTES ] ,
208
211
} ;
209
212
210
- let mut b = Blake2b :: init_param ( & param, key) ;
213
+ let mut b = Blake2b :: init_param ( param, key) ;
211
214
b. apply_key ( ) ;
212
215
b
213
216
}
@@ -291,9 +294,7 @@ impl Blake2b {
291
294
let incby = self . buflen as u64 ;
292
295
self . increment_counter ( incby) ;
293
296
self . set_lastblock ( ) ;
294
- let mut temp_buf = self . buf ;
295
- let buf_slice = & mut temp_buf[ self . buflen ..] ;
296
- for b in buf_slice. iter_mut ( ) {
297
+ for b in self . buf [ self . buflen ..] . iter_mut ( ) {
297
298
* b = 0 ;
298
299
}
299
300
self . compress ( ) ;
@@ -305,17 +306,7 @@ impl Blake2b {
305
306
copy_memory ( & self . buf [ 0 ..outlen] , out) ;
306
307
}
307
308
308
- pub fn blake2b ( out : & mut [ u8 ] , input : & [ u8 ] , key : & [ u8 ] ) {
309
- let mut hasher : Blake2b = if key. len ( ) > 0 { Blake2b :: new_keyed ( out. len ( ) , key) } else { Blake2b :: new ( out. len ( ) ) } ;
310
-
311
- hasher. update ( input) ;
312
- hasher. finalize ( out) ;
313
- }
314
-
315
- }
316
-
317
- impl Digest for Blake2b {
318
- fn reset ( & mut self ) {
309
+ pub fn reset ( & mut self ) {
319
310
for ( h_elem, iv_elem) in self . h . iter_mut ( ) . zip ( IV . iter ( ) ) {
320
311
* h_elem = * iv_elem;
321
312
}
@@ -331,9 +322,22 @@ impl Digest for Blake2b {
331
322
self . buflen = 0 ;
332
323
self . last_node = 0 ;
333
324
self . computed = false ;
334
- let len = self . digest_length ;
335
- self . apply_param ( & Blake2b :: default_param ( len) ) ;
325
+ self . apply_param ( ) ;
326
+ if self . key_length > 0 {
327
+ self . apply_key ( ) ;
328
+ }
336
329
}
330
+
331
+ pub fn blake2b ( out : & mut [ u8 ] , input : & [ u8 ] , key : & [ u8 ] ) {
332
+ let mut hasher : Blake2b = if key. len ( ) > 0 { Blake2b :: new_keyed ( out. len ( ) , key) } else { Blake2b :: new ( out. len ( ) ) } ;
333
+
334
+ hasher. update ( input) ;
335
+ hasher. finalize ( out) ;
336
+ }
337
+ }
338
+
339
+ impl Digest for Blake2b {
340
+ fn reset ( & mut self ) { Blake2b :: reset ( self ) ; }
337
341
fn input ( & mut self , msg : & [ u8 ] ) { self . update ( msg) ; }
338
342
fn result ( & mut self , out : & mut [ u8 ] ) { self . finalize ( out) ; }
339
343
fn output_bits ( & self ) -> usize { 8 * ( self . digest_length as usize ) }
@@ -356,24 +360,7 @@ impl Mac for Blake2b {
356
360
* Reset the Mac state to begin processing another input stream.
357
361
*/
358
362
fn reset ( & mut self ) {
359
- for ( h_elem, iv_elem) in self . h . iter_mut ( ) . zip ( IV . iter ( ) ) {
360
- * h_elem = * iv_elem;
361
- }
362
- for t_elem in self . t . iter_mut ( ) {
363
- * t_elem = 0 ;
364
- }
365
- for f_elem in self . f . iter_mut ( ) {
366
- * f_elem = 0 ;
367
- }
368
- for b in self . buf . iter_mut ( ) {
369
- * b = 0 ;
370
- }
371
- self . buflen = 0 ;
372
- self . last_node = 0 ;
373
- self . computed = false ;
374
- let len = self . digest_length ;
375
- self . apply_param ( & Blake2b :: default_param ( len) ) ;
376
- self . apply_key ( ) ;
363
+ Blake2b :: reset ( self ) ;
377
364
}
378
365
379
366
/**
@@ -405,62 +392,108 @@ mod digest_tests {
405
392
//use cryptoutil::test::test_digest_1million_random;
406
393
use blake2b:: Blake2b ;
407
394
use digest:: Digest ;
395
+ use serialize:: hex:: FromHex ;
408
396
409
397
410
398
struct Test {
411
- input : & ' static str ,
412
- output_str : & ' static str ,
399
+ input : Vec < u8 > ,
400
+ output : Vec < u8 > ,
401
+ key : Option < Vec < u8 > > ,
413
402
}
414
403
415
- fn test_hash < D : Digest > ( sh : & mut D , tests : & [ Test ] ) {
416
- // Test that it works when accepting the message all at once
417
- for t in tests. iter ( ) {
418
- sh. input_str ( t. input ) ;
404
+ fn test_hash ( tests : & [ Test ] ) {
405
+ for t in tests {
406
+ let mut sh = match t. key {
407
+ Some ( ref key) => Blake2b :: new_keyed ( 64 , & key) ,
408
+ None => Blake2b :: new ( 64 )
409
+ } ;
410
+
411
+ // Test that it works when accepting the message all at once
412
+ sh. input ( & t. input [ ..] ) ;
419
413
420
- let out_str = sh. result_str ( ) ;
421
- assert ! ( & out_str[ ..] == t. output_str) ;
414
+ let mut out = [ 0u8 ; 64 ] ;
415
+ sh. result ( & mut out) ;
416
+ assert ! ( & out[ ..] == & t. output[ ..] ) ;
422
417
423
418
sh. reset ( ) ;
424
- }
425
419
426
- // Test that it works when accepting the message in pieces
427
- for t in tests. iter ( ) {
420
+ // Test that it works when accepting the message in pieces
428
421
let len = t. input . len ( ) ;
429
422
let mut left = len;
430
423
while left > 0 {
431
424
let take = ( left + 1 ) / 2 ;
432
- sh. input_str ( & t. input [ len - left..take + len - left] ) ;
433
- left = left - take;
425
+ sh. input ( & t. input [ len - left..take + len - left] ) ;
426
+ left -= take;
434
427
}
435
428
436
- let out_str = sh. result_str ( ) ;
437
- assert ! ( & out_str[ ..] == t. output_str) ;
429
+ let mut out = [ 0u8 ; 64 ] ;
430
+ sh. result ( & mut out) ;
431
+ assert ! ( & out[ ..] == & t. output[ ..] ) ;
438
432
439
433
sh. reset ( ) ;
440
434
}
441
435
}
442
436
443
437
#[ test]
444
438
fn test_blake2b_digest ( ) {
445
- // Examples from wikipedia
446
- let wikipedia_tests = vec ! [
439
+ let tests = vec ! [
440
+ // Examples from wikipedia
447
441
Test {
448
- input: "" ,
449
- output_str: "786a02f742015903c6c6fd852552d272912f4740e15847618a86e217f71f5419\
450
- d25e1031afee585313896444934eb04b903a685b1448b755d56f701afe9be2ce"
442
+ input: vec![ ] ,
443
+ output: "786a02f742015903c6c6fd852552d272\
444
+ 912f4740e15847618a86e217f71f5419\
445
+ d25e1031afee585313896444934eb04b\
446
+ 903a685b1448b755d56f701afe9be2ce". from_hex( ) . unwrap( ) ,
447
+ key: None
451
448
} ,
452
449
Test {
453
- input: "The quick brown fox jumps over the lazy dog" ,
454
- output_str: "a8add4bdddfd93e4877d2746e62817b116364a1fa7bc148d95090bc7333b3673\
455
- f82401cf7aa2e4cb1ecd90296e3f14cb5413f8ed77be73045b13914cdcd6a918"
450
+ input: "The quick brown fox jumps over the lazy dog" . as_bytes( ) . to_vec( ) ,
451
+ output: "a8add4bdddfd93e4877d2746e62817b1\
452
+ 16364a1fa7bc148d95090bc7333b3673\
453
+ f82401cf7aa2e4cb1ecd90296e3f14cb\
454
+ 5413f8ed77be73045b13914cdcd6a918". from_hex( ) . unwrap( ) ,
455
+ key: None
456
+ } ,
457
+ // from: https://github.com/BLAKE2/BLAKE2/blob/master/testvectors/blake2b-test.txt
458
+ Test {
459
+ input: vec![ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0a , 0x0b ,
460
+ 0x0c , 0x0d , 0x0e , 0x0f , 0x10 , 0x11 , 0x12 , 0x13 , 0x14 , 0x15 , 0x16 , 0x17 ,
461
+ 0x18 , 0x19 , 0x1a , 0x1b , 0x1c , 0x1d , 0x1e , 0x1f , 0x20 , 0x21 , 0x22 , 0x23 ,
462
+ 0x24 , 0x25 , 0x26 , 0x27 , 0x28 , 0x29 , 0x2a , 0x2b , 0x2c , 0x2d , 0x2e , 0x2f ,
463
+ 0x30 , 0x31 , 0x32 , 0x33 , 0x34 , 0x35 , 0x36 , 0x37 , 0x38 , 0x39 , 0x3a , 0x3b ,
464
+ 0x3c , 0x3d , 0x3e , 0x3f , 0x40 , 0x41 , 0x42 , 0x43 , 0x44 , 0x45 , 0x46 , 0x47 ,
465
+ 0x48 , 0x49 , 0x4a , 0x4b , 0x4c , 0x4d , 0x4e , 0x4f , 0x50 , 0x51 , 0x52 , 0x53 ,
466
+ 0x54 , 0x55 , 0x56 , 0x57 , 0x58 , 0x59 , 0x5a , 0x5b , 0x5c , 0x5d , 0x5e , 0x5f ,
467
+ 0x60 , 0x61 , 0x62 , 0x63 , 0x64 , 0x65 , 0x66 , 0x67 , 0x68 , 0x69 , 0x6a , 0x6b ,
468
+ 0x6c , 0x6d , 0x6e , 0x6f , 0x70 , 0x71 , 0x72 , 0x73 , 0x74 , 0x75 , 0x76 , 0x77 ,
469
+ 0x78 , 0x79 , 0x7a , 0x7b , 0x7c , 0x7d , 0x7e , 0x7f , 0x80 , 0x81 , 0x82 , 0x83 ,
470
+ 0x84 , 0x85 , 0x86 , 0x87 , 0x88 , 0x89 , 0x8a , 0x8b , 0x8c , 0x8d , 0x8e , 0x8f ,
471
+ 0x90 , 0x91 , 0x92 , 0x93 , 0x94 , 0x95 , 0x96 , 0x97 , 0x98 , 0x99 , 0x9a , 0x9b ,
472
+ 0x9c , 0x9d , 0x9e , 0x9f , 0xa0 , 0xa1 , 0xa2 , 0xa3 , 0xa4 , 0xa5 , 0xa6 , 0xa7 ,
473
+ 0xa8 , 0xa9 , 0xaa , 0xab , 0xac , 0xad , 0xae , 0xaf , 0xb0 , 0xb1 , 0xb2 , 0xb3 ,
474
+ 0xb4 , 0xb5 , 0xb6 , 0xb7 , 0xb8 , 0xb9 , 0xba , 0xbb , 0xbc , 0xbd , 0xbe , 0xbf ,
475
+ 0xc0 , 0xc1 , 0xc2 , 0xc3 , 0xc4 , 0xc5 , 0xc6 , 0xc7 , 0xc8 , 0xc9 , 0xca , 0xcb ,
476
+ 0xcc , 0xcd , 0xce , 0xcf , 0xd0 , 0xd1 , 0xd2 , 0xd3 , 0xd4 , 0xd5 , 0xd6 , 0xd7 ,
477
+ 0xd8 , 0xd9 , 0xda , 0xdb , 0xdc , 0xdd , 0xde , 0xdf , 0xe0 , 0xe1 , 0xe2 , 0xe3 ,
478
+ 0xe4 , 0xe5 , 0xe6 , 0xe7 , 0xe8 , 0xe9 , 0xea , 0xeb , 0xec , 0xed , 0xee , 0xef ,
479
+ 0xf0 , 0xf1 , 0xf2 , 0xf3 , 0xf4 , 0xf5 , 0xf6 , 0xf7 , 0xf8 , 0xf9 , 0xfa , 0xfb ,
480
+ 0xfc , 0xfd , 0xfe ] ,
481
+ output: vec![ 0x14 , 0x27 , 0x09 , 0xd6 , 0x2e , 0x28 , 0xfc , 0xcc , 0xd0 , 0xaf , 0x97 ,
482
+ 0xfa , 0xd0 , 0xf8 , 0x46 , 0x5b , 0x97 , 0x1e , 0x82 , 0x20 , 0x1d , 0xc5 ,
483
+ 0x10 , 0x70 , 0xfa , 0xa0 , 0x37 , 0x2a , 0xa4 , 0x3e , 0x92 , 0x48 , 0x4b ,
484
+ 0xe1 , 0xc1 , 0xe7 , 0x3b , 0xa1 , 0x09 , 0x06 , 0xd5 , 0xd1 , 0x85 , 0x3d ,
485
+ 0xb6 , 0xa4 , 0x10 , 0x6e , 0x0a , 0x7b , 0xf9 , 0x80 , 0x0d , 0x37 , 0x3d ,
486
+ 0x6d , 0xee , 0x2d , 0x46 , 0xd6 , 0x2e , 0xf2 , 0xa4 , 0x61 ] ,
487
+ key: Some ( vec![ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0a ,
488
+ 0x0b , 0x0c , 0x0d , 0x0e , 0x0f , 0x10 , 0x11 , 0x12 , 0x13 , 0x14 , 0x15 ,
489
+ 0x16 , 0x17 , 0x18 , 0x19 , 0x1a , 0x1b , 0x1c , 0x1d , 0x1e , 0x1f , 0x20 ,
490
+ 0x21 , 0x22 , 0x23 , 0x24 , 0x25 , 0x26 , 0x27 , 0x28 , 0x29 , 0x2a , 0x2b ,
491
+ 0x2c , 0x2d , 0x2e , 0x2f , 0x30 , 0x31 , 0x32 , 0x33 , 0x34 , 0x35 , 0x36 ,
492
+ 0x37 , 0x38 , 0x39 , 0x3a , 0x3b , 0x3c , 0x3d , 0x3e , 0x3f ] )
456
493
} ,
457
494
] ;
458
495
459
- let tests = wikipedia_tests;
460
-
461
- let mut sh = Blake2b :: new ( 64 ) ;
462
-
463
- test_hash ( & mut sh, & tests[ ..] ) ;
496
+ test_hash ( & tests[ ..] ) ;
464
497
}
465
498
}
466
499
0 commit comments