diff options
author | Jussi Kivilinna <jussi.kivilinna@iki.fi> | 2022-10-23 17:05:31 +0300 |
---|---|---|
committer | Jussi Kivilinna <jussi.kivilinna@iki.fi> | 2022-10-26 21:43:04 +0300 |
commit | a43e03ef842b2bb93b10cd2b85230af1f0269ca0 (patch) | |
tree | edcdf935e635d1f9994eadf38369233a552e8cf4 /cipher | |
parent | 6475d0915ffecc54a8d1105262edb28ff4026803 (diff) | |
download | libgcrypt-a43e03ef842b2bb93b10cd2b85230af1f0269ca0.tar.gz |
sm4: fix lookup-table prefetching
* cipher/sm4.c (sm4_expand_key): Prefetch sbox table.
(sm4_get_crypt_blk1_16_fn): Do not prefetch sbox table.
(sm4_expand_key, _gcry_sm4_ctr_enc, _gcry_sm4_cbc_dec)
(_gcry_sm4_cfb_dec): Prefetch sbox table if table look-up
implementation is used.
--
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Diffstat (limited to 'cipher')
-rw-r--r-- | cipher/sm4.c | 18 |
1 files changed, 16 insertions, 2 deletions
diff --git a/cipher/sm4.c b/cipher/sm4.c index 99a1e840..32a21dd9 100644 --- a/cipher/sm4.c +++ b/cipher/sm4.c @@ -701,6 +701,8 @@ sm4_expand_key (SM4_context *ctx, const byte *key) } #endif + prefetch_sbox_table (); + rk[0] = buf_get_be32(key + 4 * 0) ^ fk[0]; rk[1] = buf_get_be32(key + 4 * 1) ^ fk[1]; rk[2] = buf_get_be32(key + 4 * 2) ^ fk[2]; @@ -1008,7 +1010,6 @@ sm4_get_crypt_blk1_16_fn(SM4_context *ctx) else { (void)ctx; - prefetch_sbox_table (); return &sm4_crypt_blocks; } } @@ -1149,6 +1150,9 @@ _gcry_sm4_ctr_enc(void *context, unsigned char *ctr, unsigned int tmp_used = 16; size_t nburn; + if (crypt_blk1_16 == &sm4_crypt_blocks) + prefetch_sbox_table (); + nburn = bulk_ctr_enc_128(ctx->rkey_enc, crypt_blk1_16, outbuf, inbuf, nblocks, ctr, tmpbuf, sizeof(tmpbuf) / 16, &tmp_used); @@ -1295,6 +1299,9 @@ _gcry_sm4_cbc_dec(void *context, unsigned char *iv, unsigned int tmp_used = 16; size_t nburn; + if (crypt_blk1_16 == &sm4_crypt_blocks) + prefetch_sbox_table (); + nburn = bulk_cbc_dec_128(ctx->rkey_dec, crypt_blk1_16, outbuf, inbuf, nblocks, iv, tmpbuf, sizeof(tmpbuf) / 16, &tmp_used); @@ -1441,6 +1448,9 @@ _gcry_sm4_cfb_dec(void *context, unsigned char *iv, unsigned int tmp_used = 16; size_t nburn; + if (crypt_blk1_16 == &sm4_crypt_blocks) + prefetch_sbox_table (); + nburn = bulk_cfb_dec_128(ctx->rkey_enc, crypt_blk1_16, outbuf, inbuf, nblocks, iv, tmpbuf, sizeof(tmpbuf) / 16, &tmp_used); @@ -1457,6 +1467,7 @@ static unsigned int sm4_crypt_blk1_32 (const SM4_context *ctx, byte *outbuf, const byte *inbuf, unsigned int num_blks, const u32 *rk) { + crypt_blk1_16_fn_t crypt_blk1_16 = ctx->crypt_blk1_16; unsigned int stack_burn_size = 0; unsigned int nburn; @@ -1479,7 +1490,7 @@ sm4_crypt_blk1_32 (const SM4_context *ctx, byte *outbuf, const byte *inbuf, do { unsigned int curr_blks = num_blks > 16 ? 16 : num_blks; - nburn = ctx->crypt_blk1_16 (rk, outbuf, inbuf, curr_blks); + nburn = crypt_blk1_16 (rk, outbuf, inbuf, curr_blks); stack_burn_size = nburn > stack_burn_size ? nburn : stack_burn_size; outbuf += curr_blks * 16; inbuf += curr_blks * 16; @@ -1534,6 +1545,9 @@ _gcry_sm4_xts_crypt (void *context, unsigned char *tweak, void *outbuf_arg, unsigned int tmp_used = 16; size_t nburn; + if (ctx->crypt_blk1_16 == &sm4_crypt_blocks) + prefetch_sbox_table (); + nburn = bulk_xts_crypt_128(ctx, encrypt ? sm4_encrypt_blk1_32 : sm4_decrypt_blk1_32, outbuf, inbuf, nblocks, |