1
1
use alloc:: vec;
2
2
use alloc:: vec:: Vec ;
3
3
use core:: fmt:: { Debug , Display , Formatter , LowerHex , UpperHex } ;
4
+ use core:: marker:: PhantomData ;
5
+ use digest:: Digest ;
4
6
use rand_core:: { CryptoRng , RngCore } ;
5
7
use signature:: { RandomizedSigner , Signature as SignSignature , Signer , Verifier } ;
6
8
use subtle:: { Choice , ConditionallySelectable , ConstantTimeEq } ;
@@ -285,12 +287,19 @@ fn non_zero_random_bytes<R: RngCore + CryptoRng>(rng: &mut R, data: &mut [u8]) {
285
287
}
286
288
}
287
289
288
- pub struct SigningKey {
290
+ pub struct SigningKey < D >
291
+ where
292
+ D : Digest ,
293
+ {
289
294
inner : RsaPrivateKey ,
290
295
hash : Option < Hash > ,
296
+ phantom : PhantomData < D > ,
291
297
}
292
298
293
- impl SigningKey {
299
+ impl < D > SigningKey < D >
300
+ where
301
+ D : Digest ,
302
+ {
294
303
pub ( crate ) fn key ( & self ) -> & RsaPrivateKey {
295
304
& self . inner
296
305
}
@@ -303,79 +312,118 @@ impl SigningKey {
303
312
Self {
304
313
inner : key,
305
314
hash : None ,
315
+ phantom : Default :: default ( ) ,
306
316
}
307
317
}
308
318
309
319
pub fn new_with_hash ( key : RsaPrivateKey , hash : Hash ) -> Self {
310
320
Self {
311
321
inner : key,
312
322
hash : Some ( hash) ,
323
+ phantom : Default :: default ( ) ,
313
324
}
314
325
}
315
326
}
316
327
317
- impl Signer < Signature > for SigningKey {
318
- fn try_sign ( & self , digest : & [ u8 ] ) -> signature:: Result < Signature > {
319
- sign :: < DummyRng , _ > ( None , & self . inner , self . hash . as_ref ( ) , digest)
328
+ impl < D > Signer < Signature > for SigningKey < D >
329
+ where
330
+ D : Digest ,
331
+ {
332
+ fn try_sign ( & self , msg : & [ u8 ] ) -> signature:: Result < Signature > {
333
+ sign :: < DummyRng , _ > ( None , & self . inner , self . hash . as_ref ( ) , & D :: digest ( msg) )
320
334
. map ( |v| v. into ( ) )
321
335
. map_err ( |e| e. into ( ) )
322
336
}
323
337
}
324
338
325
- impl RandomizedSigner < Signature > for SigningKey {
339
+ impl < D > RandomizedSigner < Signature > for SigningKey < D >
340
+ where
341
+ D : Digest ,
342
+ {
326
343
fn try_sign_with_rng (
327
344
& self ,
328
345
mut rng : impl CryptoRng + RngCore ,
329
- digest : & [ u8 ] ,
346
+ msg : & [ u8 ] ,
330
347
) -> signature:: Result < Signature > {
331
- sign ( Some ( & mut rng) , & self . inner , self . hash . as_ref ( ) , digest)
332
- . map ( |v| v. into ( ) )
333
- . map_err ( |e| e. into ( ) )
348
+ sign (
349
+ Some ( & mut rng) ,
350
+ & self . inner ,
351
+ self . hash . as_ref ( ) ,
352
+ & D :: digest ( msg) ,
353
+ )
354
+ . map ( |v| v. into ( ) )
355
+ . map_err ( |e| e. into ( ) )
334
356
}
335
357
}
336
358
337
- pub struct VerifyingKey {
359
+ pub struct VerifyingKey < D >
360
+ where
361
+ D : Digest ,
362
+ {
338
363
inner : RsaPublicKey ,
339
364
hash : Option < Hash > ,
365
+ phantom : PhantomData < D > ,
340
366
}
341
367
342
- impl VerifyingKey {
368
+ impl < D > VerifyingKey < D >
369
+ where
370
+ D : Digest ,
371
+ {
343
372
pub fn new ( key : RsaPublicKey ) -> Self {
344
373
Self {
345
374
inner : key,
346
375
hash : None ,
376
+ phantom : Default :: default ( ) ,
347
377
}
348
378
}
349
379
350
380
pub fn new_with_hash ( key : RsaPublicKey , hash : Hash ) -> Self {
351
381
Self {
352
382
inner : key,
353
383
hash : Some ( hash) ,
384
+ phantom : Default :: default ( ) ,
354
385
}
355
386
}
356
387
}
357
388
358
- impl From < SigningKey > for VerifyingKey {
359
- fn from ( key : SigningKey ) -> Self {
389
+ impl < D > From < SigningKey < D > > for VerifyingKey < D >
390
+ where
391
+ D : Digest ,
392
+ {
393
+ fn from ( key : SigningKey < D > ) -> Self {
360
394
Self {
361
395
inner : key. key ( ) . into ( ) ,
362
396
hash : key. hash ( ) ,
397
+ phantom : Default :: default ( ) ,
363
398
}
364
399
}
365
400
}
366
401
367
- impl From < & SigningKey > for VerifyingKey {
368
- fn from ( key : & SigningKey ) -> Self {
402
+ impl < D > From < & SigningKey < D > > for VerifyingKey < D >
403
+ where
404
+ D : Digest ,
405
+ {
406
+ fn from ( key : & SigningKey < D > ) -> Self {
369
407
Self {
370
408
inner : key. key ( ) . into ( ) ,
371
409
hash : key. hash ( ) ,
410
+ phantom : Default :: default ( ) ,
372
411
}
373
412
}
374
413
}
375
414
376
- impl Verifier < Signature > for VerifyingKey {
415
+ impl < D > Verifier < Signature > for VerifyingKey < D >
416
+ where
417
+ D : Digest ,
418
+ {
377
419
fn verify ( & self , msg : & [ u8 ] , signature : & Signature ) -> signature:: Result < ( ) > {
378
- verify ( & self . inner , self . hash . as_ref ( ) , msg, signature. as_ref ( ) ) . map_err ( |e| e. into ( ) )
420
+ verify (
421
+ & self . inner ,
422
+ self . hash . as_ref ( ) ,
423
+ & D :: digest ( msg) ,
424
+ signature. as_ref ( ) ,
425
+ )
426
+ . map_err ( |e| e. into ( ) )
379
427
}
380
428
}
381
429
@@ -526,17 +574,16 @@ mod tests {
526
574
) ,
527
575
) ] ;
528
576
529
- let signing_key = SigningKey :: new_with_hash ( priv_key, Hash :: SHA1 ) ;
577
+ let signing_key = SigningKey :: < Sha1 > :: new_with_hash ( priv_key, Hash :: SHA1 ) ;
530
578
531
579
for ( text, expected) in & tests {
532
- let digest = Sha1 :: digest ( text. as_bytes ( ) ) . to_vec ( ) ;
533
-
534
- let out = signing_key. sign ( & digest) ;
535
- assert_ne ! ( out. as_ref( ) , digest) ;
580
+ let out = signing_key. sign ( text. as_bytes ( ) ) ;
581
+ assert_ne ! ( out. as_ref( ) , text. as_bytes( ) ) ;
582
+ assert_ne ! ( out. as_ref( ) , & Sha1 :: digest( text. as_bytes( ) ) . to_vec( ) ) ;
536
583
assert_eq ! ( out. as_ref( ) , expected) ;
537
584
538
585
let mut rng = ChaCha8Rng :: from_seed ( [ 42 ; 32 ] ) ;
539
- let out2 = signing_key. sign_with_rng ( & mut rng, & digest ) ;
586
+ let out2 = signing_key. sign_with_rng ( & mut rng, text . as_bytes ( ) ) ;
540
587
assert_eq ! ( out2. as_ref( ) , expected) ;
541
588
}
542
589
}
@@ -606,12 +653,11 @@ mod tests {
606
653
) ,
607
654
] ;
608
655
let pub_key: RsaPublicKey = priv_key. into ( ) ;
609
- let verifying_key = VerifyingKey :: new_with_hash ( pub_key, Hash :: SHA1 ) ;
656
+ let verifying_key = VerifyingKey :: < Sha1 > :: new_with_hash ( pub_key, Hash :: SHA1 ) ;
610
657
611
658
for ( text, sig, expected) in & tests {
612
- let digest = Sha1 :: digest ( text. as_bytes ( ) ) . to_vec ( ) ;
613
-
614
- let result = verifying_key. verify ( & digest, & Signature :: from_bytes ( sig) . unwrap ( ) ) ;
659
+ let result =
660
+ verifying_key. verify ( text. as_bytes ( ) , & Signature :: from_bytes ( sig) . unwrap ( ) ) ;
615
661
match expected {
616
662
true => result. expect ( "failed to verify" ) ,
617
663
false => {
@@ -642,14 +688,14 @@ mod tests {
642
688
#[ test]
643
689
fn test_unpadded_signature_signer ( ) {
644
690
let msg = b"Thu Dec 19 18:06:16 EST 2013\n " ;
645
- let expected_sig = Base64 :: decode_vec ( "pX4DR8azytjdQ1rtUiC040FjkepuQut5q2ZFX1pTjBrOVKNjgsCDyiJDGZTCNoh9qpXYbhl7iEym30BWWwuiZg ==" ) . unwrap ( ) ;
691
+ let expected_sig = Base64 :: decode_vec ( "F8rxGUnrRLYr9nTWrYMZYk3Y0msVzfl9daWt32AZHJNCVENOWUS17OwcFawgmYhyJZDG3leTT6S5QZLaozun/A ==" ) . unwrap ( ) ;
646
692
let priv_key = get_private_key ( ) ;
647
693
648
- let signing_key = SigningKey :: new ( priv_key) ;
694
+ let signing_key = SigningKey :: < Sha1 > :: new ( priv_key) ;
649
695
let sig = signing_key. sign ( msg) ;
650
696
assert_eq ! ( sig. as_ref( ) , expected_sig) ;
651
697
652
- let verifying_key: VerifyingKey = ( & signing_key) . into ( ) ;
698
+ let verifying_key: VerifyingKey < _ > = ( & signing_key) . into ( ) ;
653
699
verifying_key
654
700
. verify ( msg, & Signature :: from_bytes ( & expected_sig) . unwrap ( ) )
655
701
. expect ( "failed to verify" ) ;
0 commit comments