aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHerbert Xu <herbert@gondor.apana.org.au>2016-07-12 01:17:39 -0400
committerHerbert Xu <herbert@gondor.apana.org.au>2016-07-18 05:35:40 -0400
commit16f37ecdd068884fc4f4177f83ac77910f4fc113 (patch)
tree2830a78883efffb4f91bb63e6a40f511355d2d9a
parent464b93a3c7d16831bca9e02e6c9624e793d7e5cc (diff)
crypto: gcm - Use skcipher
This patch converts gcm to use the new skcipher interface as opposed to ablkcipher. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
-rw-r--r--crypto/gcm.c111
1 files changed, 58 insertions, 53 deletions
diff --git a/crypto/gcm.c b/crypto/gcm.c
index d9ea5f9c0574..70a892e87ccb 100644
--- a/crypto/gcm.c
+++ b/crypto/gcm.c
@@ -29,7 +29,7 @@ struct gcm_instance_ctx {
29}; 29};
30 30
31struct crypto_gcm_ctx { 31struct crypto_gcm_ctx {
32 struct crypto_ablkcipher *ctr; 32 struct crypto_skcipher *ctr;
33 struct crypto_ahash *ghash; 33 struct crypto_ahash *ghash;
34}; 34};
35 35
@@ -50,7 +50,7 @@ struct crypto_rfc4543_instance_ctx {
50 50
51struct crypto_rfc4543_ctx { 51struct crypto_rfc4543_ctx {
52 struct crypto_aead *child; 52 struct crypto_aead *child;
53 struct crypto_blkcipher *null; 53 struct crypto_skcipher *null;
54 u8 nonce[4]; 54 u8 nonce[4];
55}; 55};
56 56
@@ -74,7 +74,7 @@ struct crypto_gcm_req_priv_ctx {
74 struct crypto_gcm_ghash_ctx ghash_ctx; 74 struct crypto_gcm_ghash_ctx ghash_ctx;
75 union { 75 union {
76 struct ahash_request ahreq; 76 struct ahash_request ahreq;
77 struct ablkcipher_request abreq; 77 struct skcipher_request skreq;
78 } u; 78 } u;
79}; 79};
80 80
@@ -114,7 +114,7 @@ static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
114{ 114{
115 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead); 115 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
116 struct crypto_ahash *ghash = ctx->ghash; 116 struct crypto_ahash *ghash = ctx->ghash;
117 struct crypto_ablkcipher *ctr = ctx->ctr; 117 struct crypto_skcipher *ctr = ctx->ctr;
118 struct { 118 struct {
119 be128 hash; 119 be128 hash;
120 u8 iv[8]; 120 u8 iv[8];
@@ -122,35 +122,35 @@ static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
122 struct crypto_gcm_setkey_result result; 122 struct crypto_gcm_setkey_result result;
123 123
124 struct scatterlist sg[1]; 124 struct scatterlist sg[1];
125 struct ablkcipher_request req; 125 struct skcipher_request req;
126 } *data; 126 } *data;
127 int err; 127 int err;
128 128
129 crypto_ablkcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK); 129 crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
130 crypto_ablkcipher_set_flags(ctr, crypto_aead_get_flags(aead) & 130 crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
131 CRYPTO_TFM_REQ_MASK); 131 CRYPTO_TFM_REQ_MASK);
132 err = crypto_ablkcipher_setkey(ctr, key, keylen); 132 err = crypto_skcipher_setkey(ctr, key, keylen);
133 crypto_aead_set_flags(aead, crypto_ablkcipher_get_flags(ctr) & 133 crypto_aead_set_flags(aead, crypto_skcipher_get_flags(ctr) &
134 CRYPTO_TFM_RES_MASK); 134 CRYPTO_TFM_RES_MASK);
135 if (err) 135 if (err)
136 return err; 136 return err;
137 137
138 data = kzalloc(sizeof(*data) + crypto_ablkcipher_reqsize(ctr), 138 data = kzalloc(sizeof(*data) + crypto_skcipher_reqsize(ctr),
139 GFP_KERNEL); 139 GFP_KERNEL);
140 if (!data) 140 if (!data)
141 return -ENOMEM; 141 return -ENOMEM;
142 142
143 init_completion(&data->result.completion); 143 init_completion(&data->result.completion);
144 sg_init_one(data->sg, &data->hash, sizeof(data->hash)); 144 sg_init_one(data->sg, &data->hash, sizeof(data->hash));
145 ablkcipher_request_set_tfm(&data->req, ctr); 145 skcipher_request_set_tfm(&data->req, ctr);
146 ablkcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP | 146 skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
147 CRYPTO_TFM_REQ_MAY_BACKLOG, 147 CRYPTO_TFM_REQ_MAY_BACKLOG,
148 crypto_gcm_setkey_done, 148 crypto_gcm_setkey_done,
149 &data->result); 149 &data->result);
150 ablkcipher_request_set_crypt(&data->req, data->sg, data->sg, 150 skcipher_request_set_crypt(&data->req, data->sg, data->sg,
151 sizeof(data->hash), data->iv); 151 sizeof(data->hash), data->iv);
152 152
153 err = crypto_ablkcipher_encrypt(&data->req); 153 err = crypto_skcipher_encrypt(&data->req);
154 if (err == -EINPROGRESS || err == -EBUSY) { 154 if (err == -EINPROGRESS || err == -EBUSY) {
155 err = wait_for_completion_interruptible( 155 err = wait_for_completion_interruptible(
156 &data->result.completion); 156 &data->result.completion);
@@ -223,13 +223,13 @@ static void crypto_gcm_init_crypt(struct aead_request *req,
223 struct crypto_aead *aead = crypto_aead_reqtfm(req); 223 struct crypto_aead *aead = crypto_aead_reqtfm(req);
224 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead); 224 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(aead);
225 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); 225 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
226 struct ablkcipher_request *ablk_req = &pctx->u.abreq; 226 struct skcipher_request *skreq = &pctx->u.skreq;
227 struct scatterlist *dst; 227 struct scatterlist *dst;
228 228
229 dst = req->src == req->dst ? pctx->src : pctx->dst; 229 dst = req->src == req->dst ? pctx->src : pctx->dst;
230 230
231 ablkcipher_request_set_tfm(ablk_req, ctx->ctr); 231 skcipher_request_set_tfm(skreq, ctx->ctr);
232 ablkcipher_request_set_crypt(ablk_req, pctx->src, dst, 232 skcipher_request_set_crypt(skreq, pctx->src, dst,
233 cryptlen + sizeof(pctx->auth_tag), 233 cryptlen + sizeof(pctx->auth_tag),
234 pctx->iv); 234 pctx->iv);
235} 235}
@@ -494,14 +494,14 @@ out:
494static int crypto_gcm_encrypt(struct aead_request *req) 494static int crypto_gcm_encrypt(struct aead_request *req)
495{ 495{
496 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); 496 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
497 struct ablkcipher_request *abreq = &pctx->u.abreq; 497 struct skcipher_request *skreq = &pctx->u.skreq;
498 u32 flags = aead_request_flags(req); 498 u32 flags = aead_request_flags(req);
499 499
500 crypto_gcm_init_common(req); 500 crypto_gcm_init_common(req);
501 crypto_gcm_init_crypt(req, req->cryptlen); 501 crypto_gcm_init_crypt(req, req->cryptlen);
502 ablkcipher_request_set_callback(abreq, flags, gcm_encrypt_done, req); 502 skcipher_request_set_callback(skreq, flags, gcm_encrypt_done, req);
503 503
504 return crypto_ablkcipher_encrypt(abreq) ?: 504 return crypto_skcipher_encrypt(skreq) ?:
505 gcm_encrypt_continue(req, flags); 505 gcm_encrypt_continue(req, flags);
506} 506}
507 507
@@ -533,12 +533,12 @@ static void gcm_decrypt_done(struct crypto_async_request *areq, int err)
533static int gcm_dec_hash_continue(struct aead_request *req, u32 flags) 533static int gcm_dec_hash_continue(struct aead_request *req, u32 flags)
534{ 534{
535 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req); 535 struct crypto_gcm_req_priv_ctx *pctx = crypto_gcm_reqctx(req);
536 struct ablkcipher_request *abreq = &pctx->u.abreq; 536 struct skcipher_request *skreq = &pctx->u.skreq;
537 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx; 537 struct crypto_gcm_ghash_ctx *gctx = &pctx->ghash_ctx;
538 538
539 crypto_gcm_init_crypt(req, gctx->cryptlen); 539 crypto_gcm_init_crypt(req, gctx->cryptlen);
540 ablkcipher_request_set_callback(abreq, flags, gcm_decrypt_done, req); 540 skcipher_request_set_callback(skreq, flags, gcm_decrypt_done, req);
541 return crypto_ablkcipher_decrypt(abreq) ?: crypto_gcm_verify(req); 541 return crypto_skcipher_decrypt(skreq) ?: crypto_gcm_verify(req);
542} 542}
543 543
544static int crypto_gcm_decrypt(struct aead_request *req) 544static int crypto_gcm_decrypt(struct aead_request *req)
@@ -566,7 +566,7 @@ static int crypto_gcm_init_tfm(struct crypto_aead *tfm)
566 struct aead_instance *inst = aead_alg_instance(tfm); 566 struct aead_instance *inst = aead_alg_instance(tfm);
567 struct gcm_instance_ctx *ictx = aead_instance_ctx(inst); 567 struct gcm_instance_ctx *ictx = aead_instance_ctx(inst);
568 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm); 568 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
569 struct crypto_ablkcipher *ctr; 569 struct crypto_skcipher *ctr;
570 struct crypto_ahash *ghash; 570 struct crypto_ahash *ghash;
571 unsigned long align; 571 unsigned long align;
572 int err; 572 int err;
@@ -575,7 +575,7 @@ static int crypto_gcm_init_tfm(struct crypto_aead *tfm)
575 if (IS_ERR(ghash)) 575 if (IS_ERR(ghash))
576 return PTR_ERR(ghash); 576 return PTR_ERR(ghash);
577 577
578 ctr = crypto_spawn_skcipher(&ictx->ctr); 578 ctr = crypto_spawn_skcipher2(&ictx->ctr);
579 err = PTR_ERR(ctr); 579 err = PTR_ERR(ctr);
580 if (IS_ERR(ctr)) 580 if (IS_ERR(ctr))
581 goto err_free_hash; 581 goto err_free_hash;
@@ -587,8 +587,8 @@ static int crypto_gcm_init_tfm(struct crypto_aead *tfm)
587 align &= ~(crypto_tfm_ctx_alignment() - 1); 587 align &= ~(crypto_tfm_ctx_alignment() - 1);
588 crypto_aead_set_reqsize(tfm, 588 crypto_aead_set_reqsize(tfm,
589 align + offsetof(struct crypto_gcm_req_priv_ctx, u) + 589 align + offsetof(struct crypto_gcm_req_priv_ctx, u) +
590 max(sizeof(struct ablkcipher_request) + 590 max(sizeof(struct skcipher_request) +
591 crypto_ablkcipher_reqsize(ctr), 591 crypto_skcipher_reqsize(ctr),
592 sizeof(struct ahash_request) + 592 sizeof(struct ahash_request) +
593 crypto_ahash_reqsize(ghash))); 593 crypto_ahash_reqsize(ghash)));
594 594
@@ -604,7 +604,7 @@ static void crypto_gcm_exit_tfm(struct crypto_aead *tfm)
604 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm); 604 struct crypto_gcm_ctx *ctx = crypto_aead_ctx(tfm);
605 605
606 crypto_free_ahash(ctx->ghash); 606 crypto_free_ahash(ctx->ghash);
607 crypto_free_ablkcipher(ctx->ctr); 607 crypto_free_skcipher(ctx->ctr);
608} 608}
609 609
610static void crypto_gcm_free(struct aead_instance *inst) 610static void crypto_gcm_free(struct aead_instance *inst)
@@ -624,7 +624,7 @@ static int crypto_gcm_create_common(struct crypto_template *tmpl,
624{ 624{
625 struct crypto_attr_type *algt; 625 struct crypto_attr_type *algt;
626 struct aead_instance *inst; 626 struct aead_instance *inst;
627 struct crypto_alg *ctr; 627 struct skcipher_alg *ctr;
628 struct crypto_alg *ghash_alg; 628 struct crypto_alg *ghash_alg;
629 struct hash_alg_common *ghash; 629 struct hash_alg_common *ghash;
630 struct gcm_instance_ctx *ctx; 630 struct gcm_instance_ctx *ctx;
@@ -663,41 +663,42 @@ static int crypto_gcm_create_common(struct crypto_template *tmpl,
663 goto err_drop_ghash; 663 goto err_drop_ghash;
664 664
665 crypto_set_skcipher_spawn(&ctx->ctr, aead_crypto_instance(inst)); 665 crypto_set_skcipher_spawn(&ctx->ctr, aead_crypto_instance(inst));
666 err = crypto_grab_skcipher(&ctx->ctr, ctr_name, 0, 666 err = crypto_grab_skcipher2(&ctx->ctr, ctr_name, 0,
667 crypto_requires_sync(algt->type, 667 crypto_requires_sync(algt->type,
668 algt->mask)); 668 algt->mask));
669 if (err) 669 if (err)
670 goto err_drop_ghash; 670 goto err_drop_ghash;
671 671
672 ctr = crypto_skcipher_spawn_alg(&ctx->ctr); 672 ctr = crypto_spawn_skcipher_alg(&ctx->ctr);
673 673
674 /* We only support 16-byte blocks. */ 674 /* We only support 16-byte blocks. */
675 if (ctr->cra_ablkcipher.ivsize != 16) 675 if (crypto_skcipher_alg_ivsize(ctr) != 16)
676 goto out_put_ctr; 676 goto out_put_ctr;
677 677
678 /* Not a stream cipher? */ 678 /* Not a stream cipher? */
679 err = -EINVAL; 679 err = -EINVAL;
680 if (ctr->cra_blocksize != 1) 680 if (ctr->base.cra_blocksize != 1)
681 goto out_put_ctr; 681 goto out_put_ctr;
682 682
683 err = -ENAMETOOLONG; 683 err = -ENAMETOOLONG;
684 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME, 684 if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
685 "gcm_base(%s,%s)", ctr->cra_driver_name, 685 "gcm_base(%s,%s)", ctr->base.cra_driver_name,
686 ghash_alg->cra_driver_name) >= 686 ghash_alg->cra_driver_name) >=
687 CRYPTO_MAX_ALG_NAME) 687 CRYPTO_MAX_ALG_NAME)
688 goto out_put_ctr; 688 goto out_put_ctr;
689 689
690 memcpy(inst->alg.base.cra_name, full_name, CRYPTO_MAX_ALG_NAME); 690 memcpy(inst->alg.base.cra_name, full_name, CRYPTO_MAX_ALG_NAME);
691 691
692 inst->alg.base.cra_flags = (ghash->base.cra_flags | ctr->cra_flags) & 692 inst->alg.base.cra_flags = (ghash->base.cra_flags |
693 CRYPTO_ALG_ASYNC; 693 ctr->base.cra_flags) & CRYPTO_ALG_ASYNC;
694 inst->alg.base.cra_priority = (ghash->base.cra_priority + 694 inst->alg.base.cra_priority = (ghash->base.cra_priority +
695 ctr->cra_priority) / 2; 695 ctr->base.cra_priority) / 2;
696 inst->alg.base.cra_blocksize = 1; 696 inst->alg.base.cra_blocksize = 1;
697 inst->alg.base.cra_alignmask = ghash->base.cra_alignmask | 697 inst->alg.base.cra_alignmask = ghash->base.cra_alignmask |
698 ctr->cra_alignmask; 698 ctr->base.cra_alignmask;
699 inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx); 699 inst->alg.base.cra_ctxsize = sizeof(struct crypto_gcm_ctx);
700 inst->alg.ivsize = 12; 700 inst->alg.ivsize = 12;
701 inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
701 inst->alg.maxauthsize = 16; 702 inst->alg.maxauthsize = 16;
702 inst->alg.init = crypto_gcm_init_tfm; 703 inst->alg.init = crypto_gcm_init_tfm;
703 inst->alg.exit = crypto_gcm_exit_tfm; 704 inst->alg.exit = crypto_gcm_exit_tfm;
@@ -982,6 +983,7 @@ static int crypto_rfc4106_create(struct crypto_template *tmpl,
982 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx); 983 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4106_ctx);
983 984
984 inst->alg.ivsize = 8; 985 inst->alg.ivsize = 8;
986 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
985 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg); 987 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
986 988
987 inst->alg.init = crypto_rfc4106_init_tfm; 989 inst->alg.init = crypto_rfc4106_init_tfm;
@@ -1086,11 +1088,13 @@ static int crypto_rfc4543_copy_src_to_dst(struct aead_request *req, bool enc)
1086 unsigned int authsize = crypto_aead_authsize(aead); 1088 unsigned int authsize = crypto_aead_authsize(aead);
1087 unsigned int nbytes = req->assoclen + req->cryptlen - 1089 unsigned int nbytes = req->assoclen + req->cryptlen -
1088 (enc ? 0 : authsize); 1090 (enc ? 0 : authsize);
1089 struct blkcipher_desc desc = { 1091 SKCIPHER_REQUEST_ON_STACK(nreq, ctx->null);
1090 .tfm = ctx->null, 1092
1091 }; 1093 skcipher_request_set_tfm(nreq, ctx->null);
1094 skcipher_request_set_callback(nreq, req->base.flags, NULL, NULL);
1095 skcipher_request_set_crypt(nreq, req->src, req->dst, nbytes, NULL);
1092 1096
1093 return crypto_blkcipher_encrypt(&desc, req->dst, req->src, nbytes); 1097 return crypto_skcipher_encrypt(nreq);
1094} 1098}
1095 1099
1096static int crypto_rfc4543_encrypt(struct aead_request *req) 1100static int crypto_rfc4543_encrypt(struct aead_request *req)
@@ -1110,7 +1114,7 @@ static int crypto_rfc4543_init_tfm(struct crypto_aead *tfm)
1110 struct crypto_aead_spawn *spawn = &ictx->aead; 1114 struct crypto_aead_spawn *spawn = &ictx->aead;
1111 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm); 1115 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
1112 struct crypto_aead *aead; 1116 struct crypto_aead *aead;
1113 struct crypto_blkcipher *null; 1117 struct crypto_skcipher *null;
1114 unsigned long align; 1118 unsigned long align;
1115 int err = 0; 1119 int err = 0;
1116 1120
@@ -1118,7 +1122,7 @@ static int crypto_rfc4543_init_tfm(struct crypto_aead *tfm)
1118 if (IS_ERR(aead)) 1122 if (IS_ERR(aead))
1119 return PTR_ERR(aead); 1123 return PTR_ERR(aead);
1120 1124
1121 null = crypto_get_default_null_skcipher(); 1125 null = crypto_get_default_null_skcipher2();
1122 err = PTR_ERR(null); 1126 err = PTR_ERR(null);
1123 if (IS_ERR(null)) 1127 if (IS_ERR(null))
1124 goto err_free_aead; 1128 goto err_free_aead;
@@ -1146,7 +1150,7 @@ static void crypto_rfc4543_exit_tfm(struct crypto_aead *tfm)
1146 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm); 1150 struct crypto_rfc4543_ctx *ctx = crypto_aead_ctx(tfm);
1147 1151
1148 crypto_free_aead(ctx->child); 1152 crypto_free_aead(ctx->child);
1149 crypto_put_default_null_skcipher(); 1153 crypto_put_default_null_skcipher2();
1150} 1154}
1151 1155
1152static void crypto_rfc4543_free(struct aead_instance *inst) 1156static void crypto_rfc4543_free(struct aead_instance *inst)
@@ -1221,6 +1225,7 @@ static int crypto_rfc4543_create(struct crypto_template *tmpl,
1221 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx); 1225 inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4543_ctx);
1222 1226
1223 inst->alg.ivsize = 8; 1227 inst->alg.ivsize = 8;
1228 inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
1224 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg); 1229 inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(alg);
1225 1230
1226 inst->alg.init = crypto_rfc4543_init_tfm; 1231 inst->alg.init = crypto_rfc4543_init_tfm;