1
1
use alloc:: vec;
2
2
use alloc:: vec:: Vec ;
3
3
use rand_core:: { CryptoRng , RngCore } ;
4
+ use signature:: { RandomizedSigner , Signer , Verifier } ;
4
5
use subtle:: { Choice , ConditionallySelectable , ConstantTimeEq } ;
5
6
use zeroize:: Zeroizing ;
6
7
8
+ use crate :: dummy_rng:: DummyRng ;
7
9
use crate :: errors:: { Error , Result } ;
8
10
use crate :: hash:: Hash ;
9
11
use crate :: key:: { self , PrivateKey , PublicKey } ;
12
+ use crate :: { PublicKeyParts , RsaPrivateKey , RsaPublicKey } ;
13
+
14
+ pub use crate :: Signature ;
10
15
11
16
// Encrypts the given message with RSA and the padding
12
17
// scheme from PKCS#1 v1.5. The message must be no longer than the
@@ -214,6 +219,162 @@ fn non_zero_random_bytes<R: RngCore + CryptoRng>(rng: &mut R, data: &mut [u8]) {
214
219
}
215
220
}
216
221
222
+ pub struct RsaPkcs1v15SigningKey {
223
+ inner : RsaPrivateKey ,
224
+ hash : Option < Hash > ,
225
+ }
226
+
227
+ impl < ' a > RsaPkcs1v15SigningKey {
228
+ pub ( crate ) fn key ( & ' a self ) -> & ' a RsaPrivateKey {
229
+ & self . inner
230
+ }
231
+
232
+ pub ( crate ) fn hash ( & self ) -> Option < Hash > {
233
+ self . hash
234
+ }
235
+ }
236
+
237
+ impl From < RsaPrivateKey > for RsaPkcs1v15SigningKey {
238
+ fn from ( key : RsaPrivateKey ) -> Self {
239
+ Self {
240
+ inner : key,
241
+ hash : None ,
242
+ }
243
+ }
244
+ }
245
+
246
+ impl From < & RsaPrivateKey > for RsaPkcs1v15SigningKey {
247
+ fn from ( key : & RsaPrivateKey ) -> Self {
248
+ Self {
249
+ inner : RsaPrivateKey :: from_components (
250
+ key. n ( ) . clone ( ) ,
251
+ key. e ( ) . clone ( ) ,
252
+ key. d ( ) . clone ( ) ,
253
+ key. primes ( ) . clone ( ) . to_vec ( ) ,
254
+ )
255
+ . unwrap ( ) ,
256
+ hash : None ,
257
+ }
258
+ }
259
+ }
260
+
261
+ impl From < ( RsaPrivateKey , Hash ) > for RsaPkcs1v15SigningKey {
262
+ fn from ( data : ( RsaPrivateKey , Hash ) ) -> Self {
263
+ let key = data. 0 ;
264
+ let hash = data. 1 ;
265
+ Self {
266
+ inner : key,
267
+ hash : Some ( hash) ,
268
+ }
269
+ }
270
+ }
271
+
272
+ impl From < ( & RsaPrivateKey , Hash ) > for RsaPkcs1v15SigningKey {
273
+ fn from ( data : ( & RsaPrivateKey , Hash ) ) -> Self {
274
+ let key = data. 0 ;
275
+ let hash = data. 1 ;
276
+ Self {
277
+ inner : RsaPrivateKey :: from_components (
278
+ key. n ( ) . clone ( ) ,
279
+ key. e ( ) . clone ( ) ,
280
+ key. d ( ) . clone ( ) ,
281
+ key. primes ( ) . clone ( ) . to_vec ( ) ,
282
+ )
283
+ . unwrap ( ) ,
284
+ hash : Some ( hash) ,
285
+ }
286
+ }
287
+ }
288
+
289
+ impl Signer < Signature > for RsaPkcs1v15SigningKey {
290
+ fn try_sign ( & self , digest : & [ u8 ] ) -> signature:: Result < Signature > {
291
+ sign :: < DummyRng , _ > ( None , & self . inner , self . hash . as_ref ( ) , digest)
292
+ . map ( |v| v. into ( ) )
293
+ . map_err ( |e| e. into ( ) )
294
+ }
295
+ }
296
+
297
+ impl RandomizedSigner < Signature > for RsaPkcs1v15SigningKey {
298
+ fn try_sign_with_rng (
299
+ & self ,
300
+ mut rng : impl CryptoRng + RngCore ,
301
+ digest : & [ u8 ] ,
302
+ ) -> signature:: Result < Signature > {
303
+ sign ( Some ( & mut rng) , & self . inner , self . hash . as_ref ( ) , digest)
304
+ . map ( |v| v. into ( ) )
305
+ . map_err ( |e| e. into ( ) )
306
+ }
307
+ }
308
+
309
+ pub struct RsaPkcs1v15VerifyingKey {
310
+ inner : RsaPublicKey ,
311
+ hash : Option < Hash > ,
312
+ }
313
+
314
+ impl From < RsaPublicKey > for RsaPkcs1v15VerifyingKey {
315
+ fn from ( key : RsaPublicKey ) -> Self {
316
+ Self {
317
+ inner : key,
318
+ hash : None ,
319
+ }
320
+ }
321
+ }
322
+
323
+ impl From < & RsaPublicKey > for RsaPkcs1v15VerifyingKey {
324
+ fn from ( key : & RsaPublicKey ) -> Self {
325
+ Self {
326
+ inner : RsaPublicKey :: new ( key. n ( ) . clone ( ) , key. e ( ) . clone ( ) ) . unwrap ( ) ,
327
+ hash : None ,
328
+ }
329
+ }
330
+ }
331
+
332
+ impl From < ( RsaPublicKey , Hash ) > for RsaPkcs1v15VerifyingKey {
333
+ fn from ( data : ( RsaPublicKey , Hash ) ) -> Self {
334
+ let key = data. 0 ;
335
+ let hash = data. 1 ;
336
+ Self {
337
+ inner : key,
338
+ hash : Some ( hash) ,
339
+ }
340
+ }
341
+ }
342
+
343
+ impl From < ( & RsaPublicKey , Hash ) > for RsaPkcs1v15VerifyingKey {
344
+ fn from ( data : ( & RsaPublicKey , Hash ) ) -> Self {
345
+ let key = data. 0 ;
346
+ let hash = data. 1 ;
347
+ Self {
348
+ inner : RsaPublicKey :: new ( key. n ( ) . clone ( ) , key. e ( ) . clone ( ) ) . unwrap ( ) ,
349
+ hash : Some ( hash) ,
350
+ }
351
+ }
352
+ }
353
+
354
+ impl From < RsaPkcs1v15SigningKey > for RsaPkcs1v15VerifyingKey {
355
+ fn from ( key : RsaPkcs1v15SigningKey ) -> Self {
356
+ Self {
357
+ inner : key. key ( ) . into ( ) ,
358
+ hash : key. hash ( ) ,
359
+ }
360
+ }
361
+ }
362
+
363
+ impl From < & RsaPkcs1v15SigningKey > for RsaPkcs1v15VerifyingKey {
364
+ fn from ( key : & RsaPkcs1v15SigningKey ) -> Self {
365
+ Self {
366
+ inner : key. key ( ) . into ( ) ,
367
+ hash : key. hash ( ) ,
368
+ }
369
+ }
370
+ }
371
+
372
+ impl Verifier < Signature > for RsaPkcs1v15VerifyingKey {
373
+ fn verify ( & self , msg : & [ u8 ] , signature : & Signature ) -> signature:: Result < ( ) > {
374
+ verify ( & self . inner , self . hash . as_ref ( ) , msg, signature. as_ref ( ) ) . map_err ( |e| e. into ( ) )
375
+ }
376
+ }
377
+
217
378
#[ cfg( test) ]
218
379
mod tests {
219
380
use super :: * ;
@@ -224,6 +385,7 @@ mod tests {
224
385
use num_traits:: Num ;
225
386
use rand_chacha:: { rand_core:: SeedableRng , ChaCha8Rng } ;
226
387
use sha1:: { Digest , Sha1 } ;
388
+ use signature:: { RandomizedSigner , Signature , Signer , Verifier } ;
227
389
228
390
use crate :: { Hash , PaddingScheme , PublicKey , PublicKeyParts , RsaPrivateKey , RsaPublicKey } ;
229
391
@@ -348,6 +510,32 @@ mod tests {
348
510
}
349
511
}
350
512
513
+ #[ test]
514
+ fn test_sign_pkcs1v15_signer ( ) {
515
+ let priv_key = get_private_key ( ) ;
516
+
517
+ let tests = [ (
518
+ "Test.\n " ,
519
+ hex ! (
520
+ "a4f3fa6ea93bcdd0c57be020c1193ecbfd6f200a3d95c409769b029578fa0e33"
521
+ "6ad9a347600e40d3ae823b8c7e6bad88cc07c1d54c3a1523cbbb6d58efc362ae"
522
+ ) ,
523
+ ) ] ;
524
+
525
+ for ( text, expected) in & tests {
526
+ let digest = Sha1 :: digest ( text. as_bytes ( ) ) . to_vec ( ) ;
527
+
528
+ let signing_key: RsaPkcs1v15SigningKey = ( & priv_key, Hash :: SHA1 ) . into ( ) ;
529
+ let out = signing_key. sign ( & digest) ;
530
+ assert_ne ! ( out. as_ref( ) , digest) ;
531
+ assert_eq ! ( out. as_ref( ) , expected) ;
532
+
533
+ let mut rng = ChaCha8Rng :: from_seed ( [ 42 ; 32 ] ) ;
534
+ let out2 = signing_key. sign_with_rng ( & mut rng, & digest) ;
535
+ assert_eq ! ( out2. as_ref( ) , expected) ;
536
+ }
537
+ }
538
+
351
539
#[ test]
352
540
fn test_verify_pkcs1v15 ( ) {
353
541
let priv_key = get_private_key ( ) ;
@@ -390,6 +578,45 @@ mod tests {
390
578
}
391
579
}
392
580
581
+ #[ test]
582
+ fn test_verify_pkcs1v15_signer ( ) {
583
+ let priv_key = get_private_key ( ) ;
584
+
585
+ let tests = [
586
+ (
587
+ "Test.\n " ,
588
+ hex ! (
589
+ "a4f3fa6ea93bcdd0c57be020c1193ecbfd6f200a3d95c409769b029578fa0e33"
590
+ "6ad9a347600e40d3ae823b8c7e6bad88cc07c1d54c3a1523cbbb6d58efc362ae"
591
+ ) ,
592
+ true ,
593
+ ) ,
594
+ (
595
+ "Test.\n " ,
596
+ hex ! (
597
+ "a4f3fa6ea93bcdd0c57be020c1193ecbfd6f200a3d95c409769b029578fa0e33"
598
+ "6ad9a347600e40d3ae823b8c7e6bad88cc07c1d54c3a1523cbbb6d58efc362af"
599
+ ) ,
600
+ false ,
601
+ ) ,
602
+ ] ;
603
+ let pub_key: RsaPublicKey = priv_key. into ( ) ;
604
+ let verifying_key: RsaPkcs1v15VerifyingKey = ( & pub_key, Hash :: SHA1 ) . into ( ) ;
605
+
606
+ for ( text, sig, expected) in & tests {
607
+ let digest = Sha1 :: digest ( text. as_bytes ( ) ) . to_vec ( ) ;
608
+
609
+ let result = verifying_key. verify ( & digest, & Signature :: from_bytes ( sig) . unwrap ( ) ) ;
610
+ match expected {
611
+ true => result. expect ( "failed to verify" ) ,
612
+ false => {
613
+ result. expect_err ( "expected verifying error" ) ;
614
+ ( )
615
+ }
616
+ }
617
+ }
618
+ }
619
+
393
620
#[ test]
394
621
fn test_unpadded_signature ( ) {
395
622
let msg = b"Thu Dec 19 18:06:16 EST 2013\n " ;
@@ -406,4 +633,26 @@ mod tests {
406
633
. verify ( PaddingScheme :: new_pkcs1v15_sign ( None ) , msg, & sig)
407
634
. expect ( "failed to verify" ) ;
408
635
}
636
+
637
+ #[ test]
638
+ fn test_unpadded_signature_signer ( ) {
639
+ let msg = b"Thu Dec 19 18:06:16 EST 2013\n " ;
640
+ let expected_sig = Base64 :: decode_vec ( "pX4DR8azytjdQ1rtUiC040FjkepuQut5q2ZFX1pTjBrOVKNjgsCDyiJDGZTCNoh9qpXYbhl7iEym30BWWwuiZg==" ) . unwrap ( ) ;
641
+ let priv_key = get_private_key ( ) ;
642
+
643
+ let signing_key: RsaPkcs1v15SigningKey = ( & priv_key) . into ( ) ;
644
+ let sig = signing_key. sign ( msg) ;
645
+ assert_eq ! ( sig. as_ref( ) , expected_sig) ;
646
+
647
+ let verifying_key: RsaPkcs1v15VerifyingKey = ( & signing_key) . into ( ) ;
648
+ verifying_key
649
+ . verify ( msg, & Signature :: from_bytes ( & expected_sig) . unwrap ( ) )
650
+ . expect ( "failed to verify" ) ;
651
+
652
+ let mut rng = ChaCha8Rng :: from_seed ( [ 42 ; 32 ] ) ;
653
+ let sig = signing_key. sign_with_rng ( & mut rng, msg) ;
654
+ assert_eq ! ( sig. as_ref( ) , expected_sig) ;
655
+
656
+ verifying_key. verify ( msg, & sig) . expect ( "failed to verify" ) ;
657
+ }
409
658
}
0 commit comments