aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--crypto/ablkcipher.c10
-rw-r--r--crypto/blkcipher.c185
-rw-r--r--include/crypto/internal/skcipher.h18
-rw-r--r--include/linux/crypto.h18
4 files changed, 225 insertions, 6 deletions
diff --git a/crypto/ablkcipher.c b/crypto/ablkcipher.c
index 8f48b4d58fca..092d9659b861 100644
--- a/crypto/ablkcipher.c
+++ b/crypto/ablkcipher.c
@@ -80,6 +80,7 @@ static int crypto_init_ablkcipher_ops(struct crypto_tfm *tfm, u32 type,
80 crt->setkey = setkey; 80 crt->setkey = setkey;
81 crt->encrypt = alg->encrypt; 81 crt->encrypt = alg->encrypt;
82 crt->decrypt = alg->decrypt; 82 crt->decrypt = alg->decrypt;
83 crt->base = __crypto_ablkcipher_cast(tfm);
83 crt->ivsize = alg->ivsize; 84 crt->ivsize = alg->ivsize;
84 85
85 return 0; 86 return 0;
@@ -122,11 +123,13 @@ static int crypto_init_givcipher_ops(struct crypto_tfm *tfm, u32 type,
122 if (alg->ivsize > PAGE_SIZE / 8) 123 if (alg->ivsize > PAGE_SIZE / 8)
123 return -EINVAL; 124 return -EINVAL;
124 125
125 crt->setkey = setkey; 126 crt->setkey = tfm->__crt_alg->cra_flags & CRYPTO_ALG_GENIV ?
127 alg->setkey : setkey;
126 crt->encrypt = alg->encrypt; 128 crt->encrypt = alg->encrypt;
127 crt->decrypt = alg->decrypt; 129 crt->decrypt = alg->decrypt;
128 crt->givencrypt = alg->givencrypt; 130 crt->givencrypt = alg->givencrypt;
129 crt->givdecrypt = alg->givdecrypt ?: no_givdecrypt; 131 crt->givdecrypt = alg->givdecrypt ?: no_givdecrypt;
132 crt->base = __crypto_ablkcipher_cast(tfm);
130 crt->ivsize = alg->ivsize; 133 crt->ivsize = alg->ivsize;
131 134
132 return 0; 135 return 0;
@@ -155,6 +158,11 @@ const struct crypto_type crypto_givcipher_type = {
155}; 158};
156EXPORT_SYMBOL_GPL(crypto_givcipher_type); 159EXPORT_SYMBOL_GPL(crypto_givcipher_type);
157 160
161const char *crypto_default_geniv(const struct crypto_alg *alg)
162{
163 return alg->cra_flags & CRYPTO_ALG_ASYNC ? "eseqiv" : "chainiv";
164}
165
158int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name, 166int crypto_grab_skcipher(struct crypto_skcipher_spawn *spawn, const char *name,
159 u32 type, u32 mask) 167 u32 type, u32 mask)
160{ 168{
diff --git a/crypto/blkcipher.c b/crypto/blkcipher.c
index 7a3e1a308074..ca6ef065cded 100644
--- a/crypto/blkcipher.c
+++ b/crypto/blkcipher.c
@@ -14,8 +14,8 @@
14 * 14 *
15 */ 15 */
16 16
17#include <crypto/internal/skcipher.h>
17#include <crypto/scatterwalk.h> 18#include <crypto/scatterwalk.h>
18#include <linux/crypto.h>
19#include <linux/errno.h> 19#include <linux/errno.h>
20#include <linux/hardirq.h> 20#include <linux/hardirq.h>
21#include <linux/kernel.h> 21#include <linux/kernel.h>
@@ -450,6 +450,7 @@ static int crypto_init_blkcipher_ops_async(struct crypto_tfm *tfm)
450 crt->setkey = async_setkey; 450 crt->setkey = async_setkey;
451 crt->encrypt = async_encrypt; 451 crt->encrypt = async_encrypt;
452 crt->decrypt = async_decrypt; 452 crt->decrypt = async_decrypt;
453 crt->base = __crypto_ablkcipher_cast(tfm);
453 crt->ivsize = alg->ivsize; 454 crt->ivsize = alg->ivsize;
454 455
455 return 0; 456 return 0;
@@ -509,5 +510,187 @@ const struct crypto_type crypto_blkcipher_type = {
509}; 510};
510EXPORT_SYMBOL_GPL(crypto_blkcipher_type); 511EXPORT_SYMBOL_GPL(crypto_blkcipher_type);
511 512
513static int crypto_grab_nivcipher(struct crypto_skcipher_spawn *spawn,
514 const char *name, u32 type, u32 mask)
515{
516 struct crypto_alg *alg;
517 int err;
518
519 type = crypto_skcipher_type(type);
520 mask = crypto_skcipher_mask(mask) | CRYPTO_ALG_GENIV;
521
522 alg = crypto_alg_mod_lookup(name, type, mask);
523 if (IS_ERR(alg))
524 return PTR_ERR(alg);
525
526 err = crypto_init_spawn(&spawn->base, alg, spawn->base.inst, mask);
527 crypto_mod_put(alg);
528 return err;
529}
530
531struct crypto_instance *skcipher_geniv_alloc(struct crypto_template *tmpl,
532 struct rtattr **tb, u32 type,
533 u32 mask)
534{
535 struct {
536 int (*setkey)(struct crypto_ablkcipher *tfm, const u8 *key,
537 unsigned int keylen);
538 int (*encrypt)(struct ablkcipher_request *req);
539 int (*decrypt)(struct ablkcipher_request *req);
540
541 unsigned int min_keysize;
542 unsigned int max_keysize;
543 unsigned int ivsize;
544
545 const char *geniv;
546 } balg;
547 const char *name;
548 struct crypto_skcipher_spawn *spawn;
549 struct crypto_attr_type *algt;
550 struct crypto_instance *inst;
551 struct crypto_alg *alg;
552 int err;
553
554 algt = crypto_get_attr_type(tb);
555 err = PTR_ERR(algt);
556 if (IS_ERR(algt))
557 return ERR_PTR(err);
558
559 if ((algt->type ^ (CRYPTO_ALG_TYPE_GIVCIPHER | CRYPTO_ALG_GENIV)) &
560 algt->mask)
561 return ERR_PTR(-EINVAL);
562
563 name = crypto_attr_alg_name(tb[1]);
564 err = PTR_ERR(name);
565 if (IS_ERR(name))
566 return ERR_PTR(err);
567
568 inst = kzalloc(sizeof(*inst) + sizeof(*spawn), GFP_KERNEL);
569 if (!inst)
570 return ERR_PTR(-ENOMEM);
571
572 spawn = crypto_instance_ctx(inst);
573
574 /* Ignore async algorithms if necessary. */
575 mask |= crypto_requires_sync(algt->type, algt->mask);
576
577 crypto_set_skcipher_spawn(spawn, inst);
578 err = crypto_grab_nivcipher(spawn, name, type, mask);
579 if (err)
580 goto err_free_inst;
581
582 alg = crypto_skcipher_spawn_alg(spawn);
583
584 if ((alg->cra_flags & CRYPTO_ALG_TYPE_MASK) ==
585 CRYPTO_ALG_TYPE_BLKCIPHER) {
586 balg.ivsize = alg->cra_blkcipher.ivsize;
587 balg.min_keysize = alg->cra_blkcipher.min_keysize;
588 balg.max_keysize = alg->cra_blkcipher.max_keysize;
589
590 balg.setkey = async_setkey;
591 balg.encrypt = async_encrypt;
592 balg.decrypt = async_decrypt;
593
594 balg.geniv = alg->cra_blkcipher.geniv;
595 } else {
596 balg.ivsize = alg->cra_ablkcipher.ivsize;
597 balg.min_keysize = alg->cra_ablkcipher.min_keysize;
598 balg.max_keysize = alg->cra_ablkcipher.max_keysize;
599
600 balg.setkey = alg->cra_ablkcipher.setkey;
601 balg.encrypt = alg->cra_ablkcipher.encrypt;
602 balg.decrypt = alg->cra_ablkcipher.decrypt;
603
604 balg.geniv = alg->cra_ablkcipher.geniv;
605 }
606
607 err = -EINVAL;
608 if (!balg.ivsize)
609 goto err_drop_alg;
610
611 /*
612 * This is only true if we're constructing an algorithm with its
613 * default IV generator. For the default generator we elide the
614 * template name and double-check the IV generator.
615 */
616 if (algt->mask & CRYPTO_ALG_GENIV) {
617 if (!balg.geniv)
618 balg.geniv = crypto_default_geniv(alg);
619 err = -EAGAIN;
620 if (strcmp(tmpl->name, balg.geniv))
621 goto err_drop_alg;
622
623 memcpy(inst->alg.cra_name, alg->cra_name, CRYPTO_MAX_ALG_NAME);
624 memcpy(inst->alg.cra_driver_name, alg->cra_driver_name,
625 CRYPTO_MAX_ALG_NAME);
626 } else {
627 err = -ENAMETOOLONG;
628 if (snprintf(inst->alg.cra_name, CRYPTO_MAX_ALG_NAME,
629 "%s(%s)", tmpl->name, alg->cra_name) >=
630 CRYPTO_MAX_ALG_NAME)
631 goto err_drop_alg;
632 if (snprintf(inst->alg.cra_driver_name, CRYPTO_MAX_ALG_NAME,
633 "%s(%s)", tmpl->name, alg->cra_driver_name) >=
634 CRYPTO_MAX_ALG_NAME)
635 goto err_drop_alg;
636 }
637
638 inst->alg.cra_flags = CRYPTO_ALG_TYPE_GIVCIPHER | CRYPTO_ALG_GENIV;
639 inst->alg.cra_flags |= alg->cra_flags & CRYPTO_ALG_ASYNC;
640 inst->alg.cra_priority = alg->cra_priority;
641 inst->alg.cra_blocksize = alg->cra_blocksize;
642 inst->alg.cra_alignmask = alg->cra_alignmask;
643 inst->alg.cra_type = &crypto_givcipher_type;
644
645 inst->alg.cra_ablkcipher.ivsize = balg.ivsize;
646 inst->alg.cra_ablkcipher.min_keysize = balg.min_keysize;
647 inst->alg.cra_ablkcipher.max_keysize = balg.max_keysize;
648 inst->alg.cra_ablkcipher.geniv = balg.geniv;
649
650 inst->alg.cra_ablkcipher.setkey = balg.setkey;
651 inst->alg.cra_ablkcipher.encrypt = balg.encrypt;
652 inst->alg.cra_ablkcipher.decrypt = balg.decrypt;
653
654out:
655 return inst;
656
657err_drop_alg:
658 crypto_drop_skcipher(spawn);
659err_free_inst:
660 kfree(inst);
661 inst = ERR_PTR(err);
662 goto out;
663}
664EXPORT_SYMBOL_GPL(skcipher_geniv_alloc);
665
666void skcipher_geniv_free(struct crypto_instance *inst)
667{
668 crypto_drop_skcipher(crypto_instance_ctx(inst));
669 kfree(inst);
670}
671EXPORT_SYMBOL_GPL(skcipher_geniv_free);
672
673int skcipher_geniv_init(struct crypto_tfm *tfm)
674{
675 struct crypto_instance *inst = (void *)tfm->__crt_alg;
676 struct crypto_ablkcipher *cipher;
677
678 cipher = crypto_spawn_skcipher(crypto_instance_ctx(inst));
679 if (IS_ERR(cipher))
680 return PTR_ERR(cipher);
681
682 tfm->crt_ablkcipher.base = cipher;
683 tfm->crt_ablkcipher.reqsize += crypto_ablkcipher_reqsize(cipher);
684
685 return 0;
686}
687EXPORT_SYMBOL_GPL(skcipher_geniv_init);
688
689void skcipher_geniv_exit(struct crypto_tfm *tfm)
690{
691 crypto_free_ablkcipher(tfm->crt_ablkcipher.base);
692}
693EXPORT_SYMBOL_GPL(skcipher_geniv_exit);
694
512MODULE_LICENSE("GPL"); 695MODULE_LICENSE("GPL");
513MODULE_DESCRIPTION("Generic block chaining cipher type"); 696MODULE_DESCRIPTION("Generic block chaining cipher type");
diff --git a/include/crypto/internal/skcipher.h b/include/crypto/internal/skcipher.h
index c9402dd12d03..07e7c82324ad 100644
--- a/include/crypto/internal/skcipher.h
+++ b/include/crypto/internal/skcipher.h
@@ -15,6 +15,9 @@
15 15
16#include <crypto/algapi.h> 16#include <crypto/algapi.h>
17#include <crypto/skcipher.h> 17#include <crypto/skcipher.h>
18#include <linux/types.h>
19
20struct rtattr;
18 21
19struct crypto_skcipher_spawn { 22struct crypto_skcipher_spawn {
20 struct crypto_spawn base; 23 struct crypto_spawn base;
@@ -50,6 +53,21 @@ static inline struct crypto_ablkcipher *crypto_spawn_skcipher(
50 crypto_skcipher_mask(0))); 53 crypto_skcipher_mask(0)));
51} 54}
52 55
56const char *crypto_default_geniv(const struct crypto_alg *alg);
57
58struct crypto_instance *skcipher_geniv_alloc(struct crypto_template *tmpl,
59 struct rtattr **tb, u32 type,
60 u32 mask);
61void skcipher_geniv_free(struct crypto_instance *inst);
62int skcipher_geniv_init(struct crypto_tfm *tfm);
63void skcipher_geniv_exit(struct crypto_tfm *tfm);
64
65static inline struct crypto_ablkcipher *skcipher_geniv_cipher(
66 struct crypto_ablkcipher *geniv)
67{
68 return crypto_ablkcipher_crt(geniv)->base;
69}
70
53static inline void *skcipher_givcrypt_reqctx( 71static inline void *skcipher_givcrypt_reqctx(
54 struct skcipher_givcrypt_request *req) 72 struct skcipher_givcrypt_request *req)
55{ 73{
diff --git a/include/linux/crypto.h b/include/linux/crypto.h
index facafa1bd8ca..fa7afa9b9f4f 100644
--- a/include/linux/crypto.h
+++ b/include/linux/crypto.h
@@ -53,6 +53,12 @@
53#define CRYPTO_ALG_NEED_FALLBACK 0x00000100 53#define CRYPTO_ALG_NEED_FALLBACK 0x00000100
54 54
55/* 55/*
56 * This bit is set for symmetric key ciphers that have already been wrapped
57 * with a generic IV generator to prevent them from being wrapped again.
58 */
59#define CRYPTO_ALG_GENIV 0x00000200
60
61/*
56 * Transform masks and values (for crt_flags). 62 * Transform masks and values (for crt_flags).
57 */ 63 */
58#define CRYPTO_TFM_REQ_MASK 0x000fff00 64#define CRYPTO_TFM_REQ_MASK 0x000fff00
@@ -331,6 +337,8 @@ struct ablkcipher_tfm {
331 int (*givencrypt)(struct skcipher_givcrypt_request *req); 337 int (*givencrypt)(struct skcipher_givcrypt_request *req);
332 int (*givdecrypt)(struct skcipher_givcrypt_request *req); 338 int (*givdecrypt)(struct skcipher_givcrypt_request *req);
333 339
340 struct crypto_ablkcipher *base;
341
334 unsigned int ivsize; 342 unsigned int ivsize;
335 unsigned int reqsize; 343 unsigned int reqsize;
336}; 344};
@@ -541,14 +549,14 @@ static inline struct crypto_ablkcipher *__crypto_ablkcipher_cast(
541 549
542static inline u32 crypto_skcipher_type(u32 type) 550static inline u32 crypto_skcipher_type(u32 type)
543{ 551{
544 type &= ~CRYPTO_ALG_TYPE_MASK; 552 type &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
545 type |= CRYPTO_ALG_TYPE_BLKCIPHER; 553 type |= CRYPTO_ALG_TYPE_BLKCIPHER;
546 return type; 554 return type;
547} 555}
548 556
549static inline u32 crypto_skcipher_mask(u32 mask) 557static inline u32 crypto_skcipher_mask(u32 mask)
550{ 558{
551 mask &= ~CRYPTO_ALG_TYPE_MASK; 559 mask &= ~(CRYPTO_ALG_TYPE_MASK | CRYPTO_ALG_GENIV);
552 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK; 560 mask |= CRYPTO_ALG_TYPE_BLKCIPHER_MASK;
553 return mask; 561 return mask;
554} 562}
@@ -623,7 +631,9 @@ static inline void crypto_ablkcipher_clear_flags(struct crypto_ablkcipher *tfm,
623static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm, 631static inline int crypto_ablkcipher_setkey(struct crypto_ablkcipher *tfm,
624 const u8 *key, unsigned int keylen) 632 const u8 *key, unsigned int keylen)
625{ 633{
626 return crypto_ablkcipher_crt(tfm)->setkey(tfm, key, keylen); 634 struct ablkcipher_tfm *crt = crypto_ablkcipher_crt(tfm);
635
636 return crt->setkey(crt->base, key, keylen);
627} 637}
628 638
629static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm( 639static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(
@@ -655,7 +665,7 @@ static inline unsigned int crypto_ablkcipher_reqsize(
655static inline void ablkcipher_request_set_tfm( 665static inline void ablkcipher_request_set_tfm(
656 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm) 666 struct ablkcipher_request *req, struct crypto_ablkcipher *tfm)
657{ 667{
658 req->base.tfm = crypto_ablkcipher_tfm(tfm); 668 req->base.tfm = crypto_ablkcipher_tfm(crypto_ablkcipher_crt(tfm)->base);
659} 669}
660 670
661static inline struct ablkcipher_request *ablkcipher_request_cast( 671static inline struct ablkcipher_request *ablkcipher_request_cast(