aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTadeusz Struk <tadeusz.struk@intel.com>2016-03-03 16:49:26 -0500
committerDavid Howells <dhowells@redhat.com>2016-03-03 16:49:26 -0500
commita49de377e051eac5bc50a3b838614a05671da4e7 (patch)
tree0fa58c22cd828ee35e0387d88efd9dbce28e6216
parent41693d1c03212de3267bc77b1cb196294a438616 (diff)
crypto: Add hash param to pkcs1pad
This adds hash param to pkcs1pad. The pkcs1pad template can work with or without the hash. When hash param is provided then the verify operation will also verify the output against the known digest. Signed-off-by: Tadeusz Struk <tadeusz.struk@intel.com> Signed-off-by: David Howells <dhowells@redhat.com> Acked-by: Herbert Xu <herbert@gondor.apana.org.au>
-rw-r--r--crypto/rsa-pkcs1pad.c182
1 files changed, 156 insertions, 26 deletions
diff --git a/crypto/rsa-pkcs1pad.c b/crypto/rsa-pkcs1pad.c
index 50f5c97e1087..1cea67d43e1d 100644
--- a/crypto/rsa-pkcs1pad.c
+++ b/crypto/rsa-pkcs1pad.c
@@ -18,12 +18,89 @@
18#include <linux/module.h> 18#include <linux/module.h>
19#include <linux/random.h> 19#include <linux/random.h>
20 20
21/*
22 * Hash algorithm OIDs plus ASN.1 DER wrappings [RFC4880 sec 5.2.2].
23 */
24static 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
30static 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
36static 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
42static 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
48static 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
54static 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
60static 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
66static 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
83static 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
21struct pkcs1pad_ctx { 93struct pkcs1pad_ctx {
22 struct crypto_akcipher *child; 94 struct crypto_akcipher *child;
23 95 const char *hash_name;
24 unsigned int key_size; 96 unsigned int key_size;
25}; 97};
26 98
99struct pkcs1pad_inst_ctx {
100 struct crypto_akcipher_spawn spawn;
101 const char *hash_name;
102};
103
27struct pkcs1pad_request { 104struct pkcs1pad_request {
28 struct akcipher_request child_req; 105 struct akcipher_request child_req;
29 106
@@ -339,13 +416,22 @@ static int pkcs1pad_sign(struct akcipher_request *req)
339 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); 416 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
340 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); 417 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
341 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req); 418 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
419 const struct rsa_asn1_template *digest_info = NULL;
342 int err; 420 int err;
343 unsigned int ps_end; 421 unsigned int ps_end, digest_size = 0;
344 422
345 if (!ctx->key_size) 423 if (!ctx->key_size)
346 return -EINVAL; 424 return -EINVAL;
347 425
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)
349 return -EOVERFLOW; 435 return -EOVERFLOW;
350 436
351 if (req->dst_len < ctx->key_size) { 437 if (req->dst_len < ctx->key_size) {
@@ -371,11 +457,16 @@ static int pkcs1pad_sign(struct akcipher_request *req)
371 if (!req_ctx->in_buf) 457 if (!req_ctx->in_buf)
372 return -ENOMEM; 458 return -ENOMEM;
373 459
374 ps_end = ctx->key_size - req->src_len - 2; 460 ps_end = ctx->key_size - digest_size - req->src_len - 2;
375 req_ctx->in_buf[0] = 0x01; 461 req_ctx->in_buf[0] = 0x01;
376 memset(req_ctx->in_buf + 1, 0xff, ps_end - 1); 462 memset(req_ctx->in_buf + 1, 0xff, ps_end - 1);
377 req_ctx->in_buf[ps_end] = 0x00; 463 req_ctx->in_buf[ps_end] = 0x00;
378 464
465 if (digest_info) {
466 memcpy(req_ctx->in_buf + ps_end + 1, digest_info->data,
467 digest_info->size);
468 }
469
379 pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf, 470 pkcs1pad_sg_set_buf(req_ctx->in_sg, req_ctx->in_buf,
380 ctx->key_size - 1 - req->src_len, req->src); 471 ctx->key_size - 1 - req->src_len, req->src);
381 472
@@ -408,6 +499,7 @@ static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)
408 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req); 499 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
409 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); 500 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
410 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req); 501 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
502 const struct rsa_asn1_template *digest_info;
411 unsigned int pos; 503 unsigned int pos;
412 504
413 if (err == -EOVERFLOW) 505 if (err == -EOVERFLOW)
@@ -422,20 +514,33 @@ static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)
422 goto done; 514 goto done;
423 } 515 }
424 516
425 if (req_ctx->out_buf[0] != 0x01) { 517 err = -EBADMSG;
426 err = -EINVAL; 518 if (req_ctx->out_buf[0] != 0x01)
427 goto done; 519 goto done;
428 } 520
429 for (pos = 1; pos < req_ctx->child_req.dst_len; pos++) 521 for (pos = 1; pos < req_ctx->child_req.dst_len; pos++)
430 if (req_ctx->out_buf[pos] != 0xff) 522 if (req_ctx->out_buf[pos] != 0xff)
431 break; 523 break;
524
432 if (pos < 9 || pos == req_ctx->child_req.dst_len || 525 if (pos < 9 || pos == req_ctx->child_req.dst_len ||
433 req_ctx->out_buf[pos] != 0x00) { 526 req_ctx->out_buf[pos] != 0x00)
434 err = -EINVAL;
435 goto done; 527 goto done;
436 }
437 pos++; 528 pos++;
438 529
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
439 if (req->dst_len < req_ctx->child_req.dst_len - pos) 544 if (req->dst_len < req_ctx->child_req.dst_len - pos)
440 err = -EOVERFLOW; 545 err = -EOVERFLOW;
441 req->dst_len = req_ctx->child_req.dst_len - pos; 546 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)
444 sg_copy_from_buffer(req->dst, 549 sg_copy_from_buffer(req->dst,
445 sg_nents_for_len(req->dst, req->dst_len), 550 sg_nents_for_len(req->dst, req->dst_len),
446 req_ctx->out_buf + pos, req->dst_len); 551 req_ctx->out_buf + pos, req->dst_len);
447
448done: 552done:
449 kzfree(req_ctx->out_buf); 553 kzfree(req_ctx->out_buf);
450 554
@@ -481,7 +585,7 @@ static int pkcs1pad_verify(struct akcipher_request *req)
481 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req); 585 struct pkcs1pad_request *req_ctx = akcipher_request_ctx(req);
482 int err; 586 int err;
483 587
484 if (!ctx->key_size || req->src_len != ctx->key_size) 588 if (!ctx->key_size || req->src_len < ctx->key_size)
485 return -EINVAL; 589 return -EINVAL;
486 590
487 if (ctx->key_size > PAGE_SIZE) 591 if (ctx->key_size > PAGE_SIZE)
@@ -518,6 +622,7 @@ static int pkcs1pad_verify(struct akcipher_request *req)
518static int pkcs1pad_init_tfm(struct crypto_akcipher *tfm) 622static int pkcs1pad_init_tfm(struct crypto_akcipher *tfm)
519{ 623{
520 struct akcipher_instance *inst = akcipher_alg_instance(tfm); 624 struct akcipher_instance *inst = akcipher_alg_instance(tfm);
625 struct pkcs1pad_inst_ctx *ictx = akcipher_instance_ctx(inst);
521 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm); 626 struct pkcs1pad_ctx *ctx = akcipher_tfm_ctx(tfm);
522 struct crypto_akcipher *child_tfm; 627 struct crypto_akcipher *child_tfm;
523 628
@@ -526,7 +631,7 @@ static int pkcs1pad_init_tfm(struct crypto_akcipher *tfm)
526 return PTR_ERR(child_tfm); 631 return PTR_ERR(child_tfm);
527 632
528 ctx->child = child_tfm; 633 ctx->child = child_tfm;
529 634 ctx->hash_name = ictx->hash_name;
530 return 0; 635 return 0;
531} 636}
532 637
@@ -539,10 +644,11 @@ static void pkcs1pad_exit_tfm(struct crypto_akcipher *tfm)
539 644
540static void pkcs1pad_free(struct akcipher_instance *inst) 645static void pkcs1pad_free(struct akcipher_instance *inst)
541{ 646{
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;
543 649
544 crypto_drop_akcipher(spawn); 650 crypto_drop_akcipher(spawn);
545 651 kfree(ctx->hash_name);
546 kfree(inst); 652 kfree(inst);
547} 653}
548 654
@@ -550,9 +656,11 @@ static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)
550{ 656{
551 struct crypto_attr_type *algt; 657 struct crypto_attr_type *algt;
552 struct akcipher_instance *inst; 658 struct akcipher_instance *inst;
659 struct pkcs1pad_inst_ctx *ctx;
553 struct crypto_akcipher_spawn *spawn; 660 struct crypto_akcipher_spawn *spawn;
554 struct akcipher_alg *rsa_alg; 661 struct akcipher_alg *rsa_alg;
555 const char *rsa_alg_name; 662 const char *rsa_alg_name;
663 const char *hash_name;
556 int err; 664 int err;
557 665
558 algt = crypto_get_attr_type(tb); 666 algt = crypto_get_attr_type(tb);
@@ -566,11 +674,18 @@ static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)
566 if (IS_ERR(rsa_alg_name)) 674 if (IS_ERR(rsa_alg_name))
567 return PTR_ERR(rsa_alg_name); 675 return PTR_ERR(rsa_alg_name);
568 676
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);
570 if (!inst) 682 if (!inst)
571 return -ENOMEM; 683 return -ENOMEM;
572 684
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
574 crypto_set_spawn(&spawn->base, akcipher_crypto_instance(inst)); 689 crypto_set_spawn(&spawn->base, akcipher_crypto_instance(inst));
575 err = crypto_grab_akcipher(spawn, rsa_alg_name, 0, 690 err = crypto_grab_akcipher(spawn, rsa_alg_name, 0,
576 crypto_requires_sync(algt->type, algt->mask)); 691 crypto_requires_sync(algt->type, algt->mask));
@@ -580,15 +695,28 @@ static int pkcs1pad_create(struct crypto_template *tmpl, struct rtattr **tb)
580 rsa_alg = crypto_spawn_akcipher_alg(spawn); 695 rsa_alg = crypto_spawn_akcipher_alg(spawn);
581 696
582 err = -ENAMETOOLONG; 697 err = -ENAMETOOLONG;
583 if (snprintf(inst->alg.base.cra_name, 698
584 CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s)", 699 if (!hash_name) {
585 rsa_alg->base.cra_name) >= 700 if (snprintf(inst->alg.base.cra_name,
586 CRYPTO_MAX_ALG_NAME || 701 CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s)",
587 snprintf(inst->alg.base.cra_driver_name, 702 rsa_alg->base.cra_name) >=
588 CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s)", 703 CRYPTO_MAX_ALG_NAME ||
589 rsa_alg->base.cra_driver_name) >= 704 snprintf(inst->alg.base.cra_driver_name,
590 CRYPTO_MAX_ALG_NAME) 705 CRYPTO_MAX_ALG_NAME, "pkcs1pad(%s)",
706 rsa_alg->base.cra_driver_name) >=
707 CRYPTO_MAX_ALG_NAME)
591 goto out_drop_alg; 708 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 }
592 720
593 inst->alg.base.cra_flags = rsa_alg->base.cra_flags & CRYPTO_ALG_ASYNC; 721 inst->alg.base.cra_flags = rsa_alg->base.cra_flags & CRYPTO_ALG_ASYNC;
594 inst->alg.base.cra_priority = rsa_alg->base.cra_priority; 722 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)
610 738
611 err = akcipher_register_instance(tmpl, inst); 739 err = akcipher_register_instance(tmpl, inst);
612 if (err) 740 if (err)
613 goto out_drop_alg; 741 goto out_free_hash;
614 742
615 return 0; 743 return 0;
616 744
745out_free_hash:
746 kfree(ctx->hash_name);
617out_drop_alg: 747out_drop_alg:
618 crypto_drop_akcipher(spawn); 748 crypto_drop_akcipher(spawn);
619out_free_inst: 749out_free_inst: