Skip to content

Commit 464b93a

Browse files
committed
crypto: ccm - Use skcipher
This patch converts ccm to use the new skcipher interface as opposed to ablkcipher. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent b2b39c2 commit 464b93a

1 file changed

Lines changed: 37 additions & 35 deletions

File tree

‎crypto/ccm.c‎

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ struct ccm_instance_ctx {
2828

2929
struct crypto_ccm_ctx {
3030
struct crypto_cipher *cipher;
31-
struct crypto_ablkcipher *ctr;
31+
struct crypto_skcipher *ctr;
3232
};
3333

3434
struct crypto_rfc4309_ctx {
@@ -50,7 +50,7 @@ struct crypto_ccm_req_priv_ctx {
5050
u32 flags;
5151
struct scatterlist src[3];
5252
struct scatterlist dst[3];
53-
struct ablkcipher_request abreq;
53+
struct skcipher_request skreq;
5454
};
5555

5656
static inline struct crypto_ccm_req_priv_ctx *crypto_ccm_reqctx(
@@ -83,15 +83,15 @@ static int crypto_ccm_setkey(struct crypto_aead *aead, const u8 *key,
8383
unsigned int keylen)
8484
{
8585
struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
86-
struct crypto_ablkcipher *ctr = ctx->ctr;
86+
struct crypto_skcipher *ctr = ctx->ctr;
8787
struct crypto_cipher *tfm = ctx->cipher;
8888
int err = 0;
8989

90-
crypto_ablkcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
91-
crypto_ablkcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
92-
CRYPTO_TFM_REQ_MASK);
93-
err = crypto_ablkcipher_setkey(ctr, key, keylen);
94-
crypto_aead_set_flags(aead, crypto_ablkcipher_get_flags(ctr) &
90+
crypto_skcipher_clear_flags(ctr, CRYPTO_TFM_REQ_MASK);
91+
crypto_skcipher_set_flags(ctr, crypto_aead_get_flags(aead) &
92+
CRYPTO_TFM_REQ_MASK);
93+
err = crypto_skcipher_setkey(ctr, key, keylen);
94+
crypto_aead_set_flags(aead, crypto_skcipher_get_flags(ctr) &
9595
CRYPTO_TFM_RES_MASK);
9696
if (err)
9797
goto out;
@@ -347,7 +347,7 @@ static int crypto_ccm_encrypt(struct aead_request *req)
347347
struct crypto_aead *aead = crypto_aead_reqtfm(req);
348348
struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
349349
struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
350-
struct ablkcipher_request *abreq = &pctx->abreq;
350+
struct skcipher_request *skreq = &pctx->skreq;
351351
struct scatterlist *dst;
352352
unsigned int cryptlen = req->cryptlen;
353353
u8 *odata = pctx->odata;
@@ -366,11 +366,11 @@ static int crypto_ccm_encrypt(struct aead_request *req)
366366
if (req->src != req->dst)
367367
dst = pctx->dst;
368368

369-
ablkcipher_request_set_tfm(abreq, ctx->ctr);
370-
ablkcipher_request_set_callback(abreq, pctx->flags,
371-
crypto_ccm_encrypt_done, req);
372-
ablkcipher_request_set_crypt(abreq, pctx->src, dst, cryptlen + 16, iv);
373-
err = crypto_ablkcipher_encrypt(abreq);
369+
skcipher_request_set_tfm(skreq, ctx->ctr);
370+
skcipher_request_set_callback(skreq, pctx->flags,
371+
crypto_ccm_encrypt_done, req);
372+
skcipher_request_set_crypt(skreq, pctx->src, dst, cryptlen + 16, iv);
373+
err = crypto_skcipher_encrypt(skreq);
374374
if (err)
375375
return err;
376376

@@ -407,7 +407,7 @@ static int crypto_ccm_decrypt(struct aead_request *req)
407407
struct crypto_aead *aead = crypto_aead_reqtfm(req);
408408
struct crypto_ccm_ctx *ctx = crypto_aead_ctx(aead);
409409
struct crypto_ccm_req_priv_ctx *pctx = crypto_ccm_reqctx(req);
410-
struct ablkcipher_request *abreq = &pctx->abreq;
410+
struct skcipher_request *skreq = &pctx->skreq;
411411
struct scatterlist *dst;
412412
unsigned int authsize = crypto_aead_authsize(aead);
413413
unsigned int cryptlen = req->cryptlen;
@@ -429,11 +429,11 @@ static int crypto_ccm_decrypt(struct aead_request *req)
429429
if (req->src != req->dst)
430430
dst = pctx->dst;
431431

432-
ablkcipher_request_set_tfm(abreq, ctx->ctr);
433-
ablkcipher_request_set_callback(abreq, pctx->flags,
434-
crypto_ccm_decrypt_done, req);
435-
ablkcipher_request_set_crypt(abreq, pctx->src, dst, cryptlen + 16, iv);
436-
err = crypto_ablkcipher_decrypt(abreq);
432+
skcipher_request_set_tfm(skreq, ctx->ctr);
433+
skcipher_request_set_callback(skreq, pctx->flags,
434+
crypto_ccm_decrypt_done, req);
435+
skcipher_request_set_crypt(skreq, pctx->src, dst, cryptlen + 16, iv);
436+
err = crypto_skcipher_decrypt(skreq);
437437
if (err)
438438
return err;
439439

@@ -454,15 +454,15 @@ static int crypto_ccm_init_tfm(struct crypto_aead *tfm)
454454
struct ccm_instance_ctx *ictx = aead_instance_ctx(inst);
455455
struct crypto_ccm_ctx *ctx = crypto_aead_ctx(tfm);
456456
struct crypto_cipher *cipher;
457-
struct crypto_ablkcipher *ctr;
457+
struct crypto_skcipher *ctr;
458458
unsigned long align;
459459
int err;
460460

461461
cipher = crypto_spawn_cipher(&ictx->cipher);
462462
if (IS_ERR(cipher))
463463
return PTR_ERR(cipher);
464464

465-
ctr = crypto_spawn_skcipher(&ictx->ctr);
465+
ctr = crypto_spawn_skcipher2(&ictx->ctr);
466466
err = PTR_ERR(ctr);
467467
if (IS_ERR(ctr))
468468
goto err_free_cipher;
@@ -475,7 +475,7 @@ static int crypto_ccm_init_tfm(struct crypto_aead *tfm)
475475
crypto_aead_set_reqsize(
476476
tfm,
477477
align + sizeof(struct crypto_ccm_req_priv_ctx) +
478-
crypto_ablkcipher_reqsize(ctr));
478+
crypto_skcipher_reqsize(ctr));
479479

480480
return 0;
481481

@@ -489,7 +489,7 @@ static void crypto_ccm_exit_tfm(struct crypto_aead *tfm)
489489
struct crypto_ccm_ctx *ctx = crypto_aead_ctx(tfm);
490490

491491
crypto_free_cipher(ctx->cipher);
492-
crypto_free_ablkcipher(ctx->ctr);
492+
crypto_free_skcipher(ctx->ctr);
493493
}
494494

495495
static void crypto_ccm_free(struct aead_instance *inst)
@@ -509,7 +509,7 @@ static int crypto_ccm_create_common(struct crypto_template *tmpl,
509509
{
510510
struct crypto_attr_type *algt;
511511
struct aead_instance *inst;
512-
struct crypto_alg *ctr;
512+
struct skcipher_alg *ctr;
513513
struct crypto_alg *cipher;
514514
struct ccm_instance_ctx *ictx;
515515
int err;
@@ -544,39 +544,40 @@ static int crypto_ccm_create_common(struct crypto_template *tmpl,
544544
goto err_free_inst;
545545

546546
crypto_set_skcipher_spawn(&ictx->ctr, aead_crypto_instance(inst));
547-
err = crypto_grab_skcipher(&ictx->ctr, ctr_name, 0,
548-
crypto_requires_sync(algt->type,
549-
algt->mask));
547+
err = crypto_grab_skcipher2(&ictx->ctr, ctr_name, 0,
548+
crypto_requires_sync(algt->type,
549+
algt->mask));
550550
if (err)
551551
goto err_drop_cipher;
552552

553-
ctr = crypto_skcipher_spawn_alg(&ictx->ctr);
553+
ctr = crypto_spawn_skcipher_alg(&ictx->ctr);
554554

555555
/* Not a stream cipher? */
556556
err = -EINVAL;
557-
if (ctr->cra_blocksize != 1)
557+
if (ctr->base.cra_blocksize != 1)
558558
goto err_drop_ctr;
559559

560560
/* We want the real thing! */
561-
if (ctr->cra_ablkcipher.ivsize != 16)
561+
if (crypto_skcipher_alg_ivsize(ctr) != 16)
562562
goto err_drop_ctr;
563563

564564
err = -ENAMETOOLONG;
565565
if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
566-
"ccm_base(%s,%s)", ctr->cra_driver_name,
566+
"ccm_base(%s,%s)", ctr->base.cra_driver_name,
567567
cipher->cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
568568
goto err_drop_ctr;
569569

570570
memcpy(inst->alg.base.cra_name, full_name, CRYPTO_MAX_ALG_NAME);
571571

572-
inst->alg.base.cra_flags = ctr->cra_flags & CRYPTO_ALG_ASYNC;
572+
inst->alg.base.cra_flags = ctr->base.cra_flags & CRYPTO_ALG_ASYNC;
573573
inst->alg.base.cra_priority = (cipher->cra_priority +
574-
ctr->cra_priority) / 2;
574+
ctr->base.cra_priority) / 2;
575575
inst->alg.base.cra_blocksize = 1;
576576
inst->alg.base.cra_alignmask = cipher->cra_alignmask |
577-
ctr->cra_alignmask |
577+
ctr->base.cra_alignmask |
578578
(__alignof__(u32) - 1);
579579
inst->alg.ivsize = 16;
580+
inst->alg.chunksize = crypto_skcipher_alg_chunksize(ctr);
580581
inst->alg.maxauthsize = 16;
581582
inst->alg.base.cra_ctxsize = sizeof(struct crypto_ccm_ctx);
582583
inst->alg.init = crypto_ccm_init_tfm;
@@ -863,6 +864,7 @@ static int crypto_rfc4309_create(struct crypto_template *tmpl,
863864
inst->alg.base.cra_alignmask = alg->base.cra_alignmask;
864865

865866
inst->alg.ivsize = 8;
867+
inst->alg.chunksize = crypto_aead_alg_chunksize(alg);
866868
inst->alg.maxauthsize = 16;
867869

868870
inst->alg.base.cra_ctxsize = sizeof(struct crypto_rfc4309_ctx);

0 commit comments

Comments
 (0)