Commit cf862cbc authored by Stephan Mueller's avatar Stephan Mueller Committed by Herbert Xu

crypto: drbg - eliminate constant reinitialization of SGL

The CTR DRBG requires two SGLs pointing to input/output buffers for the
CTR AES operation. The used SGLs always have only one entry. Thus, the
SGL can be initialized during allocation time, preventing a
re-initialization of the SGLs during each call.

The performance is increased by about 1 to 3 percent depending on the
size of the requested buffer size.
Signed-off-by: default avatarStephan Mueller <smueller@chronox.de>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent 3fd8093b
...@@ -1715,6 +1715,9 @@ static int drbg_init_sym_kernel(struct drbg_state *drbg) ...@@ -1715,6 +1715,9 @@ static int drbg_init_sym_kernel(struct drbg_state *drbg)
drbg->outscratchpad = (u8 *)PTR_ALIGN(drbg->outscratchpadbuf, drbg->outscratchpad = (u8 *)PTR_ALIGN(drbg->outscratchpadbuf,
alignmask + 1); alignmask + 1);
sg_init_table(&drbg->sg_in, 1);
sg_init_table(&drbg->sg_out, 1);
return alignmask; return alignmask;
} }
...@@ -1743,17 +1746,17 @@ static int drbg_kcapi_sym_ctr(struct drbg_state *drbg, ...@@ -1743,17 +1746,17 @@ static int drbg_kcapi_sym_ctr(struct drbg_state *drbg,
u8 *inbuf, u32 inlen, u8 *inbuf, u32 inlen,
u8 *outbuf, u32 outlen) u8 *outbuf, u32 outlen)
{ {
struct scatterlist sg_in, sg_out; struct scatterlist *sg_in = &drbg->sg_in, *sg_out = &drbg->sg_out;
int ret; int ret;
sg_init_one(&sg_in, inbuf, inlen); sg_set_buf(sg_in, inbuf, inlen);
sg_init_one(&sg_out, drbg->outscratchpad, DRBG_OUTSCRATCHLEN); sg_set_buf(sg_out, drbg->outscratchpad, DRBG_OUTSCRATCHLEN);
while (outlen) { while (outlen) {
u32 cryptlen = min3(inlen, outlen, (u32)DRBG_OUTSCRATCHLEN); u32 cryptlen = min3(inlen, outlen, (u32)DRBG_OUTSCRATCHLEN);
/* Output buffer may not be valid for SGL, use scratchpad */ /* Output buffer may not be valid for SGL, use scratchpad */
skcipher_request_set_crypt(drbg->ctr_req, &sg_in, &sg_out, skcipher_request_set_crypt(drbg->ctr_req, sg_in, sg_out,
cryptlen, drbg->V); cryptlen, drbg->V);
ret = crypto_wait_req(crypto_skcipher_encrypt(drbg->ctr_req), ret = crypto_wait_req(crypto_skcipher_encrypt(drbg->ctr_req),
&drbg->ctr_wait); &drbg->ctr_wait);
......
...@@ -127,6 +127,7 @@ struct drbg_state { ...@@ -127,6 +127,7 @@ struct drbg_state {
__u8 *outscratchpadbuf; /* CTR mode output scratchpad */ __u8 *outscratchpadbuf; /* CTR mode output scratchpad */
__u8 *outscratchpad; /* CTR mode aligned outbuf */ __u8 *outscratchpad; /* CTR mode aligned outbuf */
struct crypto_wait ctr_wait; /* CTR mode async wait obj */ struct crypto_wait ctr_wait; /* CTR mode async wait obj */
struct scatterlist sg_in, sg_out; /* CTR mode SGLs */
bool seeded; /* DRBG fully seeded? */ bool seeded; /* DRBG fully seeded? */
bool pr; /* Prediction resistance enabled? */ bool pr; /* Prediction resistance enabled? */
......
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