Skip to content

Commit c3d2194

Browse files
committed
crypto: nx - Use new IV convention
This patch converts rfc4106 to the new calling convention where the IV is now part of the AD and needs to be skipped. This patch also makes use of type-safe AEAD functions where possible. Signed-off-by: Herbert Xu <[email protected]>
1 parent 7b05a37 commit c3d2194

File tree

1 file changed

+40
-26
lines changed

1 file changed

+40
-26
lines changed

drivers/crypto/nx/nx-aes-gcm.c

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@
2121

2222
#include <crypto/internal/aead.h>
2323
#include <crypto/aes.h>
24-
#include <crypto/algapi.h>
2524
#include <crypto/scatterwalk.h>
2625
#include <linux/module.h>
2726
#include <linux/types.h>
28-
#include <linux/crypto.h>
2927
#include <asm/vio.h>
3028

3129
#include "nx_csbcpb.h"
@@ -36,7 +34,7 @@ static int gcm_aes_nx_set_key(struct crypto_aead *tfm,
3634
const u8 *in_key,
3735
unsigned int key_len)
3836
{
39-
struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&tfm->base);
37+
struct nx_crypto_ctx *nx_ctx = crypto_aead_ctx(tfm);
4038
struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
4139
struct nx_csbcpb *csbcpb_aead = nx_ctx->csbcpb_aead;
4240

@@ -75,7 +73,7 @@ static int gcm4106_aes_nx_set_key(struct crypto_aead *tfm,
7573
const u8 *in_key,
7674
unsigned int key_len)
7775
{
78-
struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(&tfm->base);
76+
struct nx_crypto_ctx *nx_ctx = crypto_aead_ctx(tfm);
7977
char *nonce = nx_ctx->priv.gcm.nonce;
8078
int rc;
8179

@@ -110,13 +108,14 @@ static int gcm4106_aes_nx_setauthsize(struct crypto_aead *tfm,
110108

111109
static int nx_gca(struct nx_crypto_ctx *nx_ctx,
112110
struct aead_request *req,
113-
u8 *out)
111+
u8 *out,
112+
unsigned int assoclen)
114113
{
115114
int rc;
116115
struct nx_csbcpb *csbcpb_aead = nx_ctx->csbcpb_aead;
117116
struct scatter_walk walk;
118117
struct nx_sg *nx_sg = nx_ctx->in_sg;
119-
unsigned int nbytes = req->assoclen;
118+
unsigned int nbytes = assoclen;
120119
unsigned int processed = 0, to_process;
121120
unsigned int max_sg_len;
122121

@@ -167,7 +166,7 @@ static int nx_gca(struct nx_crypto_ctx *nx_ctx,
167166
NX_CPB_FDM(csbcpb_aead) |= NX_FDM_CONTINUATION;
168167

169168
atomic_inc(&(nx_ctx->stats->aes_ops));
170-
atomic64_add(req->assoclen, &(nx_ctx->stats->aes_bytes));
169+
atomic64_add(assoclen, &(nx_ctx->stats->aes_bytes));
171170

172171
processed += to_process;
173172
} while (processed < nbytes);
@@ -177,13 +176,15 @@ static int nx_gca(struct nx_crypto_ctx *nx_ctx,
177176
return rc;
178177
}
179178

180-
static int gmac(struct aead_request *req, struct blkcipher_desc *desc)
179+
static int gmac(struct aead_request *req, struct blkcipher_desc *desc,
180+
unsigned int assoclen)
181181
{
182182
int rc;
183-
struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
183+
struct nx_crypto_ctx *nx_ctx =
184+
crypto_aead_ctx(crypto_aead_reqtfm(req));
184185
struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
185186
struct nx_sg *nx_sg;
186-
unsigned int nbytes = req->assoclen;
187+
unsigned int nbytes = assoclen;
187188
unsigned int processed = 0, to_process;
188189
unsigned int max_sg_len;
189190

@@ -238,7 +239,7 @@ static int gmac(struct aead_request *req, struct blkcipher_desc *desc)
238239
NX_CPB_FDM(csbcpb) |= NX_FDM_CONTINUATION;
239240

240241
atomic_inc(&(nx_ctx->stats->aes_ops));
241-
atomic64_add(req->assoclen, &(nx_ctx->stats->aes_bytes));
242+
atomic64_add(assoclen, &(nx_ctx->stats->aes_bytes));
242243

243244
processed += to_process;
244245
} while (processed < nbytes);
@@ -253,7 +254,8 @@ static int gcm_empty(struct aead_request *req, struct blkcipher_desc *desc,
253254
int enc)
254255
{
255256
int rc;
256-
struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
257+
struct nx_crypto_ctx *nx_ctx =
258+
crypto_aead_ctx(crypto_aead_reqtfm(req));
257259
struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
258260
char out[AES_BLOCK_SIZE];
259261
struct nx_sg *in_sg, *out_sg;
@@ -314,9 +316,11 @@ static int gcm_empty(struct aead_request *req, struct blkcipher_desc *desc,
314316
return rc;
315317
}
316318

317-
static int gcm_aes_nx_crypt(struct aead_request *req, int enc)
319+
static int gcm_aes_nx_crypt(struct aead_request *req, int enc,
320+
unsigned int assoclen)
318321
{
319-
struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
322+
struct nx_crypto_ctx *nx_ctx =
323+
crypto_aead_ctx(crypto_aead_reqtfm(req));
320324
struct nx_gcm_rctx *rctx = aead_request_ctx(req);
321325
struct nx_csbcpb *csbcpb = nx_ctx->csbcpb;
322326
struct blkcipher_desc desc;
@@ -332,20 +336,21 @@ static int gcm_aes_nx_crypt(struct aead_request *req, int enc)
332336
*(u32 *)(desc.info + NX_GCM_CTR_OFFSET) = 1;
333337

334338
if (nbytes == 0) {
335-
if (req->assoclen == 0)
339+
if (assoclen == 0)
336340
rc = gcm_empty(req, &desc, enc);
337341
else
338-
rc = gmac(req, &desc);
342+
rc = gmac(req, &desc, assoclen);
339343
if (rc)
340344
goto out;
341345
else
342346
goto mac;
343347
}
344348

345349
/* Process associated data */
346-
csbcpb->cpb.aes_gcm.bit_length_aad = req->assoclen * 8;
347-
if (req->assoclen) {
348-
rc = nx_gca(nx_ctx, req, csbcpb->cpb.aes_gcm.in_pat_or_aad);
350+
csbcpb->cpb.aes_gcm.bit_length_aad = assoclen * 8;
351+
if (assoclen) {
352+
rc = nx_gca(nx_ctx, req, csbcpb->cpb.aes_gcm.in_pat_or_aad,
353+
assoclen);
349354
if (rc)
350355
goto out;
351356
}
@@ -363,7 +368,6 @@ static int gcm_aes_nx_crypt(struct aead_request *req, int enc)
363368
to_process = nbytes - processed;
364369

365370
csbcpb->cpb.aes_gcm.bit_length_data = nbytes * 8;
366-
desc.tfm = (struct crypto_blkcipher *) req->base.tfm;
367371
rc = nx_build_sg_lists(nx_ctx, &desc, req->dst,
368372
req->src, &to_process,
369373
processed + req->assoclen,
@@ -430,7 +434,7 @@ static int gcm_aes_nx_encrypt(struct aead_request *req)
430434

431435
memcpy(iv, req->iv, 12);
432436

433-
return gcm_aes_nx_crypt(req, 1);
437+
return gcm_aes_nx_crypt(req, 1, req->assoclen);
434438
}
435439

436440
static int gcm_aes_nx_decrypt(struct aead_request *req)
@@ -440,33 +444,41 @@ static int gcm_aes_nx_decrypt(struct aead_request *req)
440444

441445
memcpy(iv, req->iv, 12);
442446

443-
return gcm_aes_nx_crypt(req, 0);
447+
return gcm_aes_nx_crypt(req, 0, req->assoclen);
444448
}
445449

446450
static int gcm4106_aes_nx_encrypt(struct aead_request *req)
447451
{
448-
struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
452+
struct nx_crypto_ctx *nx_ctx =
453+
crypto_aead_ctx(crypto_aead_reqtfm(req));
449454
struct nx_gcm_rctx *rctx = aead_request_ctx(req);
450455
char *iv = rctx->iv;
451456
char *nonce = nx_ctx->priv.gcm.nonce;
452457

453458
memcpy(iv, nonce, NX_GCM4106_NONCE_LEN);
454459
memcpy(iv + NX_GCM4106_NONCE_LEN, req->iv, 8);
455460

456-
return gcm_aes_nx_crypt(req, 1);
461+
if (req->assoclen < 8)
462+
return -EINVAL;
463+
464+
return gcm_aes_nx_crypt(req, 1, req->assoclen - 8);
457465
}
458466

459467
static int gcm4106_aes_nx_decrypt(struct aead_request *req)
460468
{
461-
struct nx_crypto_ctx *nx_ctx = crypto_tfm_ctx(req->base.tfm);
469+
struct nx_crypto_ctx *nx_ctx =
470+
crypto_aead_ctx(crypto_aead_reqtfm(req));
462471
struct nx_gcm_rctx *rctx = aead_request_ctx(req);
463472
char *iv = rctx->iv;
464473
char *nonce = nx_ctx->priv.gcm.nonce;
465474

466475
memcpy(iv, nonce, NX_GCM4106_NONCE_LEN);
467476
memcpy(iv + NX_GCM4106_NONCE_LEN, req->iv, 8);
468477

469-
return gcm_aes_nx_crypt(req, 0);
478+
if (req->assoclen < 8)
479+
return -EINVAL;
480+
481+
return gcm_aes_nx_crypt(req, 0, req->assoclen - 8);
470482
}
471483

472484
/* tell the block cipher walk routines that this is a stream cipher by
@@ -478,6 +490,7 @@ struct aead_alg nx_gcm_aes_alg = {
478490
.base = {
479491
.cra_name = "gcm(aes)",
480492
.cra_driver_name = "gcm-aes-nx",
493+
.cra_flags = CRYPTO_ALG_AEAD_NEW,
481494
.cra_priority = 300,
482495
.cra_blocksize = 1,
483496
.cra_ctxsize = sizeof(struct nx_crypto_ctx),
@@ -496,6 +509,7 @@ struct aead_alg nx_gcm4106_aes_alg = {
496509
.base = {
497510
.cra_name = "rfc4106(gcm(aes))",
498511
.cra_driver_name = "rfc4106-gcm-aes-nx",
512+
.cra_flags = CRYPTO_ALG_AEAD_NEW,
499513
.cra_priority = 300,
500514
.cra_blocksize = 1,
501515
.cra_ctxsize = sizeof(struct nx_crypto_ctx),

0 commit comments

Comments
 (0)