Skip to content

Commit 1d63e45

Browse files
ardbiesheuvelherbertx
authored andcommitted
crypto: amlogic-gxl - permit async skcipher as fallback
Even though the amlogic-gxl driver implements asynchronous versions of ecb(aes) and cbc(aes), the fallbacks it allocates are required to be synchronous. Given that SIMD based software implementations are usually asynchronous as well, even though they rarely complete asynchronously (this typically only happens in cases where the request was made from softirq context, while SIMD was already in use in the task context that it interrupted), these implementations are disregarded, and either the generic C version or another table based version implemented in assembler is selected instead. Since falling back to synchronous AES is not only a performance issue, but potentially a security issue as well (due to the fact that table based AES is not time invariant), let's fix this, by allocating an ordinary skcipher as the fallback, and invoke it with the completion routine that was given to the outer request. Signed-off-by: Ard Biesheuvel <ardb@kernel.org> Tested-by: Corentin Labbe <clabbe@baylibre.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 3f368b8 commit 1d63e45

2 files changed

Lines changed: 15 additions & 15 deletions

File tree

‎drivers/crypto/amlogic/amlogic-gxl-cipher.c‎

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,20 @@ static int meson_cipher_do_fallback(struct skcipher_request *areq)
6464
#ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
6565
struct skcipher_alg *alg = crypto_skcipher_alg(tfm);
6666
struct meson_alg_template *algt;
67-
#endif
68-
SYNC_SKCIPHER_REQUEST_ON_STACK(req, op->fallback_tfm);
6967

70-
#ifdef CONFIG_CRYPTO_DEV_AMLOGIC_GXL_DEBUG
7168
algt = container_of(alg, struct meson_alg_template, alg.skcipher);
7269
algt->stat_fb++;
7370
#endif
74-
skcipher_request_set_sync_tfm(req, op->fallback_tfm);
75-
skcipher_request_set_callback(req, areq->base.flags, NULL, NULL);
76-
skcipher_request_set_crypt(req, areq->src, areq->dst,
71+
skcipher_request_set_tfm(&rctx->fallback_req, op->fallback_tfm);
72+
skcipher_request_set_callback(&rctx->fallback_req, areq->base.flags,
73+
areq->base.complete, areq->base.data);
74+
skcipher_request_set_crypt(&rctx->fallback_req, areq->src, areq->dst,
7775
areq->cryptlen, areq->iv);
76+
7877
if (rctx->op_dir == MESON_DECRYPT)
79-
err = crypto_skcipher_decrypt(req);
78+
err = crypto_skcipher_decrypt(&rctx->fallback_req);
8079
else
81-
err = crypto_skcipher_encrypt(req);
82-
skcipher_request_zero(req);
80+
err = crypto_skcipher_encrypt(&rctx->fallback_req);
8381
return err;
8482
}
8583

@@ -321,15 +319,16 @@ int meson_cipher_init(struct crypto_tfm *tfm)
321319
algt = container_of(alg, struct meson_alg_template, alg.skcipher);
322320
op->mc = algt->mc;
323321

324-
sktfm->reqsize = sizeof(struct meson_cipher_req_ctx);
325-
326-
op->fallback_tfm = crypto_alloc_sync_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
322+
op->fallback_tfm = crypto_alloc_skcipher(name, 0, CRYPTO_ALG_NEED_FALLBACK);
327323
if (IS_ERR(op->fallback_tfm)) {
328324
dev_err(op->mc->dev, "ERROR: Cannot allocate fallback for %s %ld\n",
329325
name, PTR_ERR(op->fallback_tfm));
330326
return PTR_ERR(op->fallback_tfm);
331327
}
332328

329+
sktfm->reqsize = sizeof(struct meson_cipher_req_ctx) +
330+
crypto_skcipher_reqsize(op->fallback_tfm);
331+
333332
op->enginectx.op.do_one_request = meson_handle_cipher_request;
334333
op->enginectx.op.prepare_request = NULL;
335334
op->enginectx.op.unprepare_request = NULL;
@@ -345,7 +344,7 @@ void meson_cipher_exit(struct crypto_tfm *tfm)
345344
memzero_explicit(op->key, op->keylen);
346345
kfree(op->key);
347346
}
348-
crypto_free_sync_skcipher(op->fallback_tfm);
347+
crypto_free_skcipher(op->fallback_tfm);
349348
}
350349

351350
int meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
@@ -377,5 +376,5 @@ int meson_aes_setkey(struct crypto_skcipher *tfm, const u8 *key,
377376
if (!op->key)
378377
return -ENOMEM;
379378

380-
return crypto_sync_skcipher_setkey(op->fallback_tfm, key, keylen);
379+
return crypto_skcipher_setkey(op->fallback_tfm, key, keylen);
381380
}

‎drivers/crypto/amlogic/amlogic-gxl.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ struct meson_dev {
109109
struct meson_cipher_req_ctx {
110110
u32 op_dir;
111111
int flow;
112+
struct skcipher_request fallback_req; // keep at the end
112113
};
113114

114115
/*
@@ -126,7 +127,7 @@ struct meson_cipher_tfm_ctx {
126127
u32 keylen;
127128
u32 keymode;
128129
struct meson_dev *mc;
129-
struct crypto_sync_skcipher *fallback_tfm;
130+
struct crypto_skcipher *fallback_tfm;
130131
};
131132

132133
/*

0 commit comments

Comments
 (0)