Commit 90e2f782 authored by Ard Biesheuvel's avatar Ard Biesheuvel Committed by Herbert Xu

crypto: qce - permit asynchronous skcipher as fallback

Even though the qce driver implements asynchronous versions of ecb(aes),
cbc(aes)and xts(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.

While at it, remove the pointless memset() from qce_skcipher_init(), and
remove the call to it qce_skcipher_init_fallback().
Signed-off-by: default avatarArd Biesheuvel <ardb@kernel.org>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent dc6e71c9
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
struct qce_cipher_ctx { struct qce_cipher_ctx {
u8 enc_key[QCE_MAX_KEY_SIZE]; u8 enc_key[QCE_MAX_KEY_SIZE];
unsigned int enc_keylen; unsigned int enc_keylen;
struct crypto_sync_skcipher *fallback; struct crypto_skcipher *fallback;
}; };
/** /**
...@@ -43,6 +43,7 @@ struct qce_cipher_reqctx { ...@@ -43,6 +43,7 @@ struct qce_cipher_reqctx {
struct sg_table src_tbl; struct sg_table src_tbl;
struct scatterlist *src_sg; struct scatterlist *src_sg;
unsigned int cryptlen; unsigned int cryptlen;
struct skcipher_request fallback_req; // keep at the end
}; };
static inline struct qce_alg_template *to_cipher_tmpl(struct crypto_skcipher *tfm) static inline struct qce_alg_template *to_cipher_tmpl(struct crypto_skcipher *tfm)
......
...@@ -178,7 +178,7 @@ static int qce_skcipher_setkey(struct crypto_skcipher *ablk, const u8 *key, ...@@ -178,7 +178,7 @@ static int qce_skcipher_setkey(struct crypto_skcipher *ablk, const u8 *key,
break; break;
} }
ret = crypto_sync_skcipher_setkey(ctx->fallback, key, keylen); ret = crypto_skcipher_setkey(ctx->fallback, key, keylen);
if (!ret) if (!ret)
ctx->enc_keylen = keylen; ctx->enc_keylen = keylen;
return ret; return ret;
...@@ -235,16 +235,15 @@ static int qce_skcipher_crypt(struct skcipher_request *req, int encrypt) ...@@ -235,16 +235,15 @@ static int qce_skcipher_crypt(struct skcipher_request *req, int encrypt)
req->cryptlen <= aes_sw_max_len) || req->cryptlen <= aes_sw_max_len) ||
(IS_XTS(rctx->flags) && req->cryptlen > QCE_SECTOR_SIZE && (IS_XTS(rctx->flags) && req->cryptlen > QCE_SECTOR_SIZE &&
req->cryptlen % QCE_SECTOR_SIZE))) { req->cryptlen % QCE_SECTOR_SIZE))) {
SYNC_SKCIPHER_REQUEST_ON_STACK(subreq, ctx->fallback); skcipher_request_set_tfm(&rctx->fallback_req, ctx->fallback);
skcipher_request_set_callback(&rctx->fallback_req,
skcipher_request_set_sync_tfm(subreq, ctx->fallback); req->base.flags,
skcipher_request_set_callback(subreq, req->base.flags, req->base.complete,
NULL, NULL); req->base.data);
skcipher_request_set_crypt(subreq, req->src, req->dst, skcipher_request_set_crypt(&rctx->fallback_req, req->src,
req->cryptlen, req->iv); req->dst, req->cryptlen, req->iv);
ret = encrypt ? crypto_skcipher_encrypt(subreq) : ret = encrypt ? crypto_skcipher_encrypt(&rctx->fallback_req) :
crypto_skcipher_decrypt(subreq); crypto_skcipher_decrypt(&rctx->fallback_req);
skcipher_request_zero(subreq);
return ret; return ret;
} }
...@@ -263,10 +262,9 @@ static int qce_skcipher_decrypt(struct skcipher_request *req) ...@@ -263,10 +262,9 @@ static int qce_skcipher_decrypt(struct skcipher_request *req)
static int qce_skcipher_init(struct crypto_skcipher *tfm) static int qce_skcipher_init(struct crypto_skcipher *tfm)
{ {
struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(tfm); /* take the size without the fallback skcipher_request at the end */
crypto_skcipher_set_reqsize(tfm, offsetof(struct qce_cipher_reqctx,
memset(ctx, 0, sizeof(*ctx)); fallback_req));
crypto_skcipher_set_reqsize(tfm, sizeof(struct qce_cipher_reqctx));
return 0; return 0;
} }
...@@ -274,17 +272,21 @@ static int qce_skcipher_init_fallback(struct crypto_skcipher *tfm) ...@@ -274,17 +272,21 @@ static int qce_skcipher_init_fallback(struct crypto_skcipher *tfm)
{ {
struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(tfm); struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
qce_skcipher_init(tfm); ctx->fallback = crypto_alloc_skcipher(crypto_tfm_alg_name(&tfm->base),
ctx->fallback = crypto_alloc_sync_skcipher(crypto_tfm_alg_name(&tfm->base),
0, CRYPTO_ALG_NEED_FALLBACK); 0, CRYPTO_ALG_NEED_FALLBACK);
return PTR_ERR_OR_ZERO(ctx->fallback); if (IS_ERR(ctx->fallback))
return PTR_ERR(ctx->fallback);
crypto_skcipher_set_reqsize(tfm, sizeof(struct qce_cipher_reqctx) +
crypto_skcipher_reqsize(ctx->fallback));
return 0;
} }
static void qce_skcipher_exit(struct crypto_skcipher *tfm) static void qce_skcipher_exit(struct crypto_skcipher *tfm)
{ {
struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(tfm); struct qce_cipher_ctx *ctx = crypto_skcipher_ctx(tfm);
crypto_free_sync_skcipher(ctx->fallback); crypto_free_skcipher(ctx->fallback);
} }
struct qce_skcipher_def { struct qce_skcipher_def {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment