1818#include <linux/module.h>
1919#include <linux/random.h>
2020
21+ /*
22+ * Hash algorithm OIDs plus ASN.1 DER wrappings [RFC4880 sec 5.2.2].
23+ */
24+ static const u8 rsa_digest_info_md5 [] = {
25+ 0x30 , 0x20 , 0x30 , 0x0c , 0x06 , 0x08 ,
26+ 0x2a , 0x86 , 0x48 , 0x86 , 0xf7 , 0x0d , 0x02 , 0x05 , /* OID */
27+ 0x05 , 0x00 , 0x04 , 0x10
28+ };
29+
30+ static const u8 rsa_digest_info_sha1 [] = {
31+ 0x30 , 0x21 , 0x30 , 0x09 , 0x06 , 0x05 ,
32+ 0x2b , 0x0e , 0x03 , 0x02 , 0x1a ,
33+ 0x05 , 0x00 , 0x04 , 0x14
34+ };
35+
36+ static const u8 rsa_digest_info_rmd160 [] = {
37+ 0x30 , 0x21 , 0x30 , 0x09 , 0x06 , 0x05 ,
38+ 0x2b , 0x24 , 0x03 , 0x02 , 0x01 ,
39+ 0x05 , 0x00 , 0x04 , 0x14
40+ };
41+
42+ static const u8 rsa_digest_info_sha224 [] = {
43+ 0x30 , 0x2d , 0x30 , 0x0d , 0x06 , 0x09 ,
44+ 0x60 , 0x86 , 0x48 , 0x01 , 0x65 , 0x03 , 0x04 , 0x02 , 0x04 ,
45+ 0x05 , 0x00 , 0x04 , 0x1c
46+ };
47+
48+ static const u8 rsa_digest_info_sha256 [] = {
49+ 0x30 , 0x31 , 0x30 , 0x0d , 0x06 , 0x09 ,
50+ 0x60 , 0x86 , 0x48 , 0x01 , 0x65 , 0x03 , 0x04 , 0x02 , 0x01 ,
51+ 0x05 , 0x00 , 0x04 , 0x20
52+ };
53+
54+ static const u8 rsa_digest_info_sha384 [] = {
55+ 0x30 , 0x41 , 0x30 , 0x0d , 0x06 , 0x09 ,
56+ 0x60 , 0x86 , 0x48 , 0x01 , 0x65 , 0x03 , 0x04 , 0x02 , 0x02 ,
57+ 0x05 , 0x00 , 0x04 , 0x30
58+ };
59+
60+ static const u8 rsa_digest_info_sha512 [] = {
61+ 0x30 , 0x51 , 0x30 , 0x0d , 0x06 , 0x09 ,
62+ 0x60 , 0x86 , 0x48 , 0x01 , 0x65 , 0x03 , 0x04 , 0x02 , 0x03 ,
63+ 0x05 , 0x00 , 0x04 , 0x40
64+ };
65+
66+ static const struct rsa_asn1_template {
67+ const char * name ;
68+ const u8 * data ;
69+ size_t size ;
70+ } rsa_asn1_templates [] = {
71+ #define _ (X ) { #X , rsa_digest_info_##X, sizeof(rsa_digest_info_##X) }
72+ _ (md5 ),
73+ _ (sha1 ),
74+ _ (rmd160 ),
75+ _ (sha256 ),
76+ _ (sha384 ),
77+ _ (sha512 ),
78+ _ (sha224 ),
79+ { NULL }
80+ #undef _
81+ };
82+
83+ static const struct rsa_asn1_template * rsa_lookup_asn1 (const char * name )
84+ {
85+ const struct rsa_asn1_template * p ;
86+
87+ for (p = rsa_asn1_templates ; p -> name ; p ++ )
88+ if (strcmp (name , p -> name ) == 0 )
89+ return p ;
90+ return NULL ;
91+ }
92+
2193struct pkcs1pad_ctx {
2294 struct crypto_akcipher * child ;
23-
95+ const char * hash_name ;
2496 unsigned int key_size ;
2597};
2698
99+ struct pkcs1pad_inst_ctx {
100+ struct crypto_akcipher_spawn spawn ;
101+ const char * hash_name ;
102+ };
103+
27104struct pkcs1pad_request {
28105 struct akcipher_request child_req ;
29106
@@ -339,13 +416,22 @@ static int pkcs1pad_sign(struct akcipher_request *req)
339416 struct crypto_akcipher * tfm = crypto_akcipher_reqtfm (req );
340417 struct pkcs1pad_ctx * ctx = akcipher_tfm_ctx (tfm );
341418 struct pkcs1pad_request * req_ctx = akcipher_request_ctx (req );
419+ const struct rsa_asn1_template * digest_info = NULL ;
342420 int err ;
343- unsigned int ps_end ;
421+ unsigned int ps_end , digest_size = 0 ;
344422
345423 if (!ctx -> key_size )
346424 return - EINVAL ;
347425
348- if (req -> src_len > ctx -> key_size - 11 )
426+ if (ctx -> hash_name ) {
427+ digest_info = rsa_lookup_asn1 (ctx -> hash_name );
428+ if (!digest_info )
429+ return - EINVAL ;
430+
431+ digest_size = digest_info -> size ;
432+ }
433+
434+ if (req -> src_len + digest_size > ctx -> key_size - 11 )
349435 return - EOVERFLOW ;
350436
351437 if (req -> dst_len < ctx -> key_size ) {
@@ -371,11 +457,16 @@ static int pkcs1pad_sign(struct akcipher_request *req)
371457 if (!req_ctx -> in_buf )
372458 return - ENOMEM ;
373459
374- ps_end = ctx -> key_size - req -> src_len - 2 ;
460+ ps_end = ctx -> key_size - digest_size - req -> src_len - 2 ;
375461 req_ctx -> in_buf [0 ] = 0x01 ;
376462 memset (req_ctx -> in_buf + 1 , 0xff , ps_end - 1 );
377463 req_ctx -> in_buf [ps_end ] = 0x00 ;
378464
465+ if (digest_info ) {
466+ memcpy (req_ctx -> in_buf + ps_end + 1 , digest_info -> data ,
467+ digest_info -> size );
468+ }
469+
379470 pkcs1pad_sg_set_buf (req_ctx -> in_sg , req_ctx -> in_buf ,
380471 ctx -> key_size - 1 - req -> src_len , req -> src );
381472
@@ -408,6 +499,7 @@ static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)
408499 struct crypto_akcipher * tfm = crypto_akcipher_reqtfm (req );
409500 struct pkcs1pad_ctx * ctx = akcipher_tfm_ctx (tfm );
410501 struct pkcs1pad_request * req_ctx = akcipher_request_ctx (req );
502+ const struct rsa_asn1_template * digest_info ;
411503 unsigned int pos ;
412504
413505 if (err == - EOVERFLOW )
@@ -422,20 +514,33 @@ static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)
422514 goto done ;
423515 }
424516
425- if ( req_ctx -> out_buf [ 0 ] != 0x01 ) {
426- err = - EINVAL ;
517+ err = - EBADMSG ;
518+ if ( req_ctx -> out_buf [ 0 ] != 0x01 )
427519 goto done ;
428- }
520+
429521 for (pos = 1 ; pos < req_ctx -> child_req .dst_len ; pos ++ )
430522 if (req_ctx -> out_buf [pos ] != 0xff )
431523 break ;
524+
432525 if (pos < 9 || pos == req_ctx -> child_req .dst_len ||
433- req_ctx -> out_buf [pos ] != 0x00 ) {
434- err = - EINVAL ;
526+ req_ctx -> out_buf [pos ] != 0x00 )
435527 goto done ;
436- }
437528 pos ++ ;
438529
530+ if (ctx -> hash_name ) {
531+ digest_info = rsa_lookup_asn1 (ctx -> hash_name );
532+ if (!digest_info )
533+ goto done ;
534+
535+ if (memcmp (req_ctx -> out_buf + pos , digest_info -> data ,
536+ digest_info -> size ))
537+ goto done ;
538+
539+ pos += digest_info -> size ;
540+ }
541+
542+ err = 0 ;
543+
439544 if (req -> dst_len < req_ctx -> child_req .dst_len - pos )
440545 err = - EOVERFLOW ;
441546 req -> dst_len = req_ctx -> child_req .dst_len - pos ;
@@ -444,7 +549,6 @@ static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)
444549 sg_copy_from_buffer (req -> dst ,
445550 sg_nents_for_len (req -> dst , req -> dst_len ),
446551 req_ctx -> out_buf + pos , req -> dst_len );
447-
448552done :
449553 kzfree (req_ctx -> out_buf );
450554
@@ -481,7 +585,7 @@ static int pkcs1pad_verify(struct akcipher_request *req)
481585 struct pkcs1pad_request * req_ctx = akcipher_request_ctx (req );
482586 int err ;
483587
484- if (!ctx -> key_size || req -> src_len != ctx -> key_size )
588+ if (!ctx -> key_size || req -> src_len < ctx -> key_size )
485589 return - EINVAL ;
486590
487591 if (ctx -> key_size > PAGE_SIZE )
@@ -518,6 +622,7 @@ static int pkcs1pad_verify(struct akcipher_request *req)
518622static int pkcs1pad_init_tfm (struct crypto_akcipher * tfm )
519623{
520624 struct akcipher_instance * inst = akcipher_alg_instance (tfm );
625+ struct pkcs1pad_inst_ctx * ictx = akcipher_instance_ctx (inst );
521626 struct pkcs1pad_ctx * ctx = akcipher_tfm_ctx (tfm );
522627 struct crypto_akcipher * child_tfm ;
523628
@@ -526,7 +631,7 @@ static int pkcs1pad_init_tfm(struct crypto_akcipher *tfm)
526631 return PTR_ERR (child_tfm );
527632
528633 ctx -> child = child_tfm ;
529-
634+ ctx -> hash_name = ictx -> hash_name ;
530635 return 0 ;
531636}
532637
@@ -539,20 +644,23 @@ static void pkcs1pad_exit_tfm(struct crypto_akcipher *tfm)
539644
540645static void pkcs1pad_free (struct akcipher_instance * inst )
541646{
542- struct crypto_akcipher_spawn * spawn = akcipher_instance_ctx (inst );
647+ struct pkcs1pad_inst_ctx * ctx = akcipher_instance_ctx (inst );
648+ struct crypto_akcipher_spawn * spawn = & ctx -> spawn ;
543649
544650 crypto_drop_akcipher (spawn );
545-
651+ kfree ( ctx -> hash_name );
546652 kfree (inst );
547653}
548654
549655static int pkcs1pad_create (struct crypto_template * tmpl , struct rtattr * * tb )
550656{
551657 struct crypto_attr_type * algt ;
552658 struct akcipher_instance * inst ;
659+ struct pkcs1pad_inst_ctx * ctx ;
553660 struct crypto_akcipher_spawn * spawn ;
554661 struct akcipher_alg * rsa_alg ;
555662 const char * rsa_alg_name ;
663+ const char * hash_name ;
556664 int err ;
557665
558666 algt = crypto_get_attr_type (tb );
@@ -566,11 +674,18 @@ static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)
566674 if (IS_ERR (rsa_alg_name ))
567675 return PTR_ERR (rsa_alg_name );
568676
569- inst = kzalloc (sizeof (* inst ) + sizeof (* spawn ), GFP_KERNEL );
677+ hash_name = crypto_attr_alg_name (tb [2 ]);
678+ if (IS_ERR (hash_name ))
679+ hash_name = NULL ;
680+
681+ inst = kzalloc (sizeof (* inst ) + sizeof (* ctx ), GFP_KERNEL );
570682 if (!inst )
571683 return - ENOMEM ;
572684
573- spawn = akcipher_instance_ctx (inst );
685+ ctx = akcipher_instance_ctx (inst );
686+ spawn = & ctx -> spawn ;
687+ ctx -> hash_name = hash_name ? kstrdup (hash_name , GFP_KERNEL ) : NULL ;
688+
574689 crypto_set_spawn (& spawn -> base , akcipher_crypto_instance (inst ));
575690 err = crypto_grab_akcipher (spawn , rsa_alg_name , 0 ,
576691 crypto_requires_sync (algt -> type , algt -> mask ));
@@ -580,15 +695,28 @@ static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)
580695 rsa_alg = crypto_spawn_akcipher_alg (spawn );
581696
582697 err = - ENAMETOOLONG ;
583- if (snprintf (inst -> alg .base .cra_name ,
584- CRYPTO_MAX_ALG_NAME , "pkcs1pad(%s)" ,
585- rsa_alg -> base .cra_name ) >=
586- CRYPTO_MAX_ALG_NAME ||
587- snprintf (inst -> alg .base .cra_driver_name ,
588- CRYPTO_MAX_ALG_NAME , "pkcs1pad(%s)" ,
589- rsa_alg -> base .cra_driver_name ) >=
590- CRYPTO_MAX_ALG_NAME )
698+
699+ if (!hash_name ) {
700+ if (snprintf (inst -> alg .base .cra_name ,
701+ CRYPTO_MAX_ALG_NAME , "pkcs1pad(%s)" ,
702+ rsa_alg -> base .cra_name ) >=
703+ CRYPTO_MAX_ALG_NAME ||
704+ snprintf (inst -> alg .base .cra_driver_name ,
705+ CRYPTO_MAX_ALG_NAME , "pkcs1pad(%s)" ,
706+ rsa_alg -> base .cra_driver_name ) >=
707+ CRYPTO_MAX_ALG_NAME )
591708 goto out_drop_alg ;
709+ } else {
710+ if (snprintf (inst -> alg .base .cra_name ,
711+ CRYPTO_MAX_ALG_NAME , "pkcs1pad(%s,%s)" ,
712+ rsa_alg -> base .cra_name , hash_name ) >=
713+ CRYPTO_MAX_ALG_NAME ||
714+ snprintf (inst -> alg .base .cra_driver_name ,
715+ CRYPTO_MAX_ALG_NAME , "pkcs1pad(%s,%s)" ,
716+ rsa_alg -> base .cra_driver_name , hash_name ) >=
717+ CRYPTO_MAX_ALG_NAME )
718+ goto out_free_hash ;
719+ }
592720
593721 inst -> alg .base .cra_flags = rsa_alg -> base .cra_flags & CRYPTO_ALG_ASYNC ;
594722 inst -> alg .base .cra_priority = rsa_alg -> base .cra_priority ;
@@ -610,10 +738,12 @@ static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)
610738
611739 err = akcipher_register_instance (tmpl , inst );
612740 if (err )
613- goto out_drop_alg ;
741+ goto out_free_hash ;
614742
615743 return 0 ;
616744
745+ out_free_hash :
746+ kfree (ctx -> hash_name );
617747out_drop_alg :
618748 crypto_drop_akcipher (spawn );
619749out_free_inst :
0 commit comments