Skip to content

Commit f0956d4

Browse files
ardbiesheuvelherbertx
authored andcommitted
crypto: omap-aes-gcm - use the AES library to encrypt the tag
The OMAP AES-GCM implementation uses a fallback ecb(aes) skcipher to produce the keystream to encrypt the output tag. Let's use the new AES library instead - this is much simpler, and shouldn't affect performance given that it only involves a single block. Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Reviewed-by: Tero Kristo <t-kristo@ti.com> Tested-by: Tero Kristo <t-kristo@ti.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent b877ad1 commit f0956d4

File tree

3 files changed

+33
-98
lines changed

3 files changed

+33
-98
lines changed

drivers/crypto/omap-aes-gcm.c

Lines changed: 25 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -167,62 +167,12 @@ static int omap_aes_gcm_copy_buffers(struct omap_aes_dev *dd,
167167
return 0;
168168
}
169169

170-
static void omap_aes_gcm_complete(struct crypto_async_request *req, int err)
171-
{
172-
struct omap_aes_gcm_result *res = req->data;
173-
174-
if (err == -EINPROGRESS)
175-
return;
176-
177-
res->err = err;
178-
complete(&res->completion);
179-
}
180-
181170
static int do_encrypt_iv(struct aead_request *req, u32 *tag, u32 *iv)
182171
{
183-
struct scatterlist iv_sg, tag_sg;
184-
struct skcipher_request *sk_req;
185-
struct omap_aes_gcm_result result;
186-
struct omap_aes_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
187-
int ret = 0;
188-
189-
sk_req = skcipher_request_alloc(ctx->ctr, GFP_KERNEL);
190-
if (!sk_req) {
191-
pr_err("skcipher: Failed to allocate request\n");
192-
return -ENOMEM;
193-
}
194-
195-
init_completion(&result.completion);
196-
197-
sg_init_one(&iv_sg, iv, AES_BLOCK_SIZE);
198-
sg_init_one(&tag_sg, tag, AES_BLOCK_SIZE);
199-
skcipher_request_set_callback(sk_req, CRYPTO_TFM_REQ_MAY_BACKLOG,
200-
omap_aes_gcm_complete, &result);
201-
ret = crypto_skcipher_setkey(ctx->ctr, (u8 *)ctx->key, ctx->keylen);
202-
skcipher_request_set_crypt(sk_req, &iv_sg, &tag_sg, AES_BLOCK_SIZE,
203-
NULL);
204-
ret = crypto_skcipher_encrypt(sk_req);
205-
switch (ret) {
206-
case 0:
207-
break;
208-
case -EINPROGRESS:
209-
case -EBUSY:
210-
ret = wait_for_completion_interruptible(&result.completion);
211-
if (!ret) {
212-
ret = result.err;
213-
if (!ret) {
214-
reinit_completion(&result.completion);
215-
break;
216-
}
217-
}
218-
/* fall through */
219-
default:
220-
pr_err("Encryption of IV failed for GCM mode\n");
221-
break;
222-
}
172+
struct omap_aes_gcm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
223173

224-
skcipher_request_free(sk_req);
225-
return ret;
174+
aes_encrypt(&ctx->actx, (u8 *)tag, (u8 *)iv);
175+
return 0;
226176
}
227177

228178
void omap_aes_gcm_dma_out_callback(void *data)
@@ -252,7 +202,7 @@ void omap_aes_gcm_dma_out_callback(void *data)
252202
static int omap_aes_gcm_handle_queue(struct omap_aes_dev *dd,
253203
struct aead_request *req)
254204
{
255-
struct omap_aes_ctx *ctx;
205+
struct omap_aes_gcm_ctx *ctx;
256206
struct aead_request *backlog;
257207
struct omap_aes_reqctx *rctx;
258208
unsigned long flags;
@@ -281,7 +231,7 @@ static int omap_aes_gcm_handle_queue(struct omap_aes_dev *dd,
281231
ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
282232
rctx = aead_request_ctx(req);
283233

284-
dd->ctx = ctx;
234+
dd->ctx = &ctx->octx;
285235
rctx->dd = dd;
286236
dd->aead_req = req;
287237

@@ -360,10 +310,10 @@ int omap_aes_gcm_decrypt(struct aead_request *req)
360310

361311
int omap_aes_4106gcm_encrypt(struct aead_request *req)
362312
{
363-
struct omap_aes_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
313+
struct omap_aes_gcm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
364314
struct omap_aes_reqctx *rctx = aead_request_ctx(req);
365315

366-
memcpy(rctx->iv, ctx->nonce, 4);
316+
memcpy(rctx->iv, ctx->octx.nonce, 4);
367317
memcpy(rctx->iv + 4, req->iv, 8);
368318
return crypto_ipsec_check_assoclen(req->assoclen) ?:
369319
omap_aes_gcm_crypt(req, FLAGS_ENCRYPT | FLAGS_GCM |
@@ -372,10 +322,10 @@ int omap_aes_4106gcm_encrypt(struct aead_request *req)
372322

373323
int omap_aes_4106gcm_decrypt(struct aead_request *req)
374324
{
375-
struct omap_aes_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
325+
struct omap_aes_gcm_ctx *ctx = crypto_aead_ctx(crypto_aead_reqtfm(req));
376326
struct omap_aes_reqctx *rctx = aead_request_ctx(req);
377327

378-
memcpy(rctx->iv, ctx->nonce, 4);
328+
memcpy(rctx->iv, ctx->octx.nonce, 4);
379329
memcpy(rctx->iv + 4, req->iv, 8);
380330
return crypto_ipsec_check_assoclen(req->assoclen) ?:
381331
omap_aes_gcm_crypt(req, FLAGS_GCM | FLAGS_RFC4106_GCM);
@@ -384,34 +334,36 @@ int omap_aes_4106gcm_decrypt(struct aead_request *req)
384334
int omap_aes_gcm_setkey(struct crypto_aead *tfm, const u8 *key,
385335
unsigned int keylen)
386336
{
387-
struct omap_aes_ctx *ctx = crypto_aead_ctx(tfm);
337+
struct omap_aes_gcm_ctx *ctx = crypto_aead_ctx(tfm);
338+
int ret;
388339

389-
if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
390-
keylen != AES_KEYSIZE_256)
391-
return -EINVAL;
340+
ret = aes_expandkey(&ctx->actx, key, keylen);
341+
if (ret)
342+
return ret;
392343

393-
memcpy(ctx->key, key, keylen);
394-
ctx->keylen = keylen;
344+
memcpy(ctx->octx.key, key, keylen);
345+
ctx->octx.keylen = keylen;
395346

396347
return 0;
397348
}
398349

399350
int omap_aes_4106gcm_setkey(struct crypto_aead *tfm, const u8 *key,
400351
unsigned int keylen)
401352
{
402-
struct omap_aes_ctx *ctx = crypto_aead_ctx(tfm);
353+
struct omap_aes_gcm_ctx *ctx = crypto_aead_ctx(tfm);
354+
int ret;
403355

404356
if (keylen < 4)
405357
return -EINVAL;
406-
407358
keylen -= 4;
408-
if (keylen != AES_KEYSIZE_128 && keylen != AES_KEYSIZE_192 &&
409-
keylen != AES_KEYSIZE_256)
410-
return -EINVAL;
411359

412-
memcpy(ctx->key, key, keylen);
413-
memcpy(ctx->nonce, key + keylen, 4);
414-
ctx->keylen = keylen;
360+
ret = aes_expandkey(&ctx->actx, key, keylen);
361+
if (ret)
362+
return ret;
363+
364+
memcpy(ctx->octx.key, key, keylen);
365+
memcpy(ctx->octx.nonce, key + keylen, 4);
366+
ctx->octx.keylen = keylen;
415367

416368
return 0;
417369
}

drivers/crypto/omap-aes.c

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,6 @@ static int omap_aes_init_tfm(struct crypto_skcipher *tfm)
645645
static int omap_aes_gcm_cra_init(struct crypto_aead *tfm)
646646
{
647647
struct omap_aes_dev *dd = NULL;
648-
struct omap_aes_ctx *ctx = crypto_aead_ctx(tfm);
649648
int err;
650649

651650
/* Find AES device, currently picks the first device */
@@ -663,12 +662,6 @@ static int omap_aes_gcm_cra_init(struct crypto_aead *tfm)
663662
}
664663

665664
tfm->reqsize = sizeof(struct omap_aes_reqctx);
666-
ctx->ctr = crypto_alloc_skcipher("ecb(aes)", 0, 0);
667-
if (IS_ERR(ctx->ctr)) {
668-
pr_warn("could not load aes driver for encrypting IV\n");
669-
return PTR_ERR(ctx->ctr);
670-
}
671-
672665
return 0;
673666
}
674667

@@ -682,19 +675,6 @@ static void omap_aes_exit_tfm(struct crypto_skcipher *tfm)
682675
ctx->fallback = NULL;
683676
}
684677

685-
static void omap_aes_gcm_cra_exit(struct crypto_aead *tfm)
686-
{
687-
struct omap_aes_ctx *ctx = crypto_aead_ctx(tfm);
688-
689-
if (ctx->fallback)
690-
crypto_free_sync_skcipher(ctx->fallback);
691-
692-
ctx->fallback = NULL;
693-
694-
if (ctx->ctr)
695-
crypto_free_skcipher(ctx->ctr);
696-
}
697-
698678
/* ********************** ALGS ************************************ */
699679

700680
static struct skcipher_alg algs_ecb_cbc[] = {
@@ -778,12 +758,11 @@ static struct aead_alg algs_aead_gcm[] = {
778758
.cra_flags = CRYPTO_ALG_ASYNC |
779759
CRYPTO_ALG_KERN_DRIVER_ONLY,
780760
.cra_blocksize = 1,
781-
.cra_ctxsize = sizeof(struct omap_aes_ctx),
761+
.cra_ctxsize = sizeof(struct omap_aes_gcm_ctx),
782762
.cra_alignmask = 0xf,
783763
.cra_module = THIS_MODULE,
784764
},
785765
.init = omap_aes_gcm_cra_init,
786-
.exit = omap_aes_gcm_cra_exit,
787766
.ivsize = GCM_AES_IV_SIZE,
788767
.maxauthsize = AES_BLOCK_SIZE,
789768
.setkey = omap_aes_gcm_setkey,
@@ -799,12 +778,11 @@ static struct aead_alg algs_aead_gcm[] = {
799778
.cra_flags = CRYPTO_ALG_ASYNC |
800779
CRYPTO_ALG_KERN_DRIVER_ONLY,
801780
.cra_blocksize = 1,
802-
.cra_ctxsize = sizeof(struct omap_aes_ctx),
781+
.cra_ctxsize = sizeof(struct omap_aes_gcm_ctx),
803782
.cra_alignmask = 0xf,
804783
.cra_module = THIS_MODULE,
805784
},
806785
.init = omap_aes_gcm_cra_init,
807-
.exit = omap_aes_gcm_cra_exit,
808786
.maxauthsize = AES_BLOCK_SIZE,
809787
.ivsize = GCM_RFC4106_IV_SIZE,
810788
.setkey = omap_aes_4106gcm_setkey,

drivers/crypto/omap-aes.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef __OMAP_AES_H__
1010
#define __OMAP_AES_H__
1111

12+
#include <crypto/aes.h>
1213
#include <crypto/engine.h>
1314

1415
#define DST_MAXBURST 4
@@ -98,7 +99,11 @@ struct omap_aes_ctx {
9899
u32 key[AES_KEYSIZE_256 / sizeof(u32)];
99100
u8 nonce[4];
100101
struct crypto_sync_skcipher *fallback;
101-
struct crypto_skcipher *ctr;
102+
};
103+
104+
struct omap_aes_gcm_ctx {
105+
struct omap_aes_ctx octx;
106+
struct crypto_aes_ctx actx;
102107
};
103108

104109
struct omap_aes_reqctx {

0 commit comments

Comments
 (0)