diff options
author | Jussi Kivilinna <jussi.kivilinna@iki.fi> | 2019-10-14 22:08:29 +0300 |
---|---|---|
committer | Jussi Kivilinna <jussi.kivilinna@iki.fi> | 2019-10-14 22:08:29 +0300 |
commit | f9d8b5a0369cc94e125d36d9c8864d5cd2eaa1d2 (patch) | |
tree | 67a58ad7a8d040946964ac1753c00144ba4f9066 /cipher | |
parent | ff0f1782560eb45458d9a8dd97088dabeddb34e7 (diff) | |
download | libgcrypt-f9d8b5a0369cc94e125d36d9c8864d5cd2eaa1d2.tar.gz |
hash-common: avoid integer division to reduce call overhead
* cipher/hash-common.h (gcry_md_block_ctx): Replace 'blocksize' with
'blocksize_shift'.
* cipher/hash-common.c (_gcry_md_block_write): Use bit-level operations
instead of division to get number of blocks.
* cipher/gostr2411-94.c (gost3411_init): Initialize 'blocksize_shift'
instead of 'blocksize'.
* cipher/md2.c (md2_init): Ditto.
* cipher/md4.c (md4_init): Ditto.
* cipher/md5.c (md5_init): Ditto.
* cipher/rmd160.c (rmd160_init): Ditto.
* cipher/sha1.c (sha1_init): Ditto.
* cipher/sha256.c (sha256_common_init): Ditto.
* cipher/sha512.c (sha512_init_common): Ditto.
* cipher/sm3.c (sm3_init): Ditto.
* cipher/stribog.c (stribog_init_512): Ditto.
* cipher/tiger.c (do_init): Ditto.
* cipher/whirlpool.c (whirlpool_init): Ditto.
--
Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Diffstat (limited to 'cipher')
-rw-r--r-- | cipher/gostr3411-94.c | 2 | ||||
-rw-r--r-- | cipher/hash-common.c | 9 | ||||
-rw-r--r-- | cipher/hash-common.h | 2 | ||||
-rw-r--r-- | cipher/md2.c | 2 | ||||
-rw-r--r-- | cipher/md4.c | 2 | ||||
-rw-r--r-- | cipher/md5.c | 2 | ||||
-rw-r--r-- | cipher/rmd160.c | 2 | ||||
-rw-r--r-- | cipher/sha1.c | 2 | ||||
-rw-r--r-- | cipher/sha256.c | 2 | ||||
-rw-r--r-- | cipher/sha512.c | 2 | ||||
-rw-r--r-- | cipher/sm3.c | 2 | ||||
-rw-r--r-- | cipher/stribog.c | 2 | ||||
-rw-r--r-- | cipher/tiger.c | 2 | ||||
-rw-r--r-- | cipher/whirlpool.c | 2 |
14 files changed, 18 insertions, 17 deletions
diff --git a/cipher/gostr3411-94.c b/cipher/gostr3411-94.c index d9746275..ebf9e0a2 100644 --- a/cipher/gostr3411-94.c +++ b/cipher/gostr3411-94.c @@ -61,7 +61,7 @@ gost3411_init (void *context, unsigned int flags) hd->bctx.nblocks = 0; hd->bctx.count = 0; - hd->bctx.blocksize = 32; + hd->bctx.blocksize_shift = _gcry_ctz(32); hd->bctx.bwrite = transform; hd->cryptopro = 0; } diff --git a/cipher/hash-common.c b/cipher/hash-common.c index 74675d49..ab486f06 100644 --- a/cipher/hash-common.c +++ b/cipher/hash-common.c @@ -123,7 +123,8 @@ _gcry_md_block_write (void *context, const void *inbuf_arg, size_t inlen) gcry_md_block_ctx_t *hd = context; unsigned int stack_burn = 0; unsigned int nburn; - const unsigned int blocksize = hd->blocksize; + const unsigned int blocksize_shift = hd->blocksize_shift; + const unsigned int blocksize = 1 << blocksize_shift; size_t inblocks; size_t copylen; @@ -164,14 +165,14 @@ _gcry_md_block_write (void *context, const void *inbuf_arg, size_t inlen) if (inlen >= blocksize) { - inblocks = inlen / blocksize; + inblocks = inlen >> blocksize_shift; nburn = hd->bwrite (hd, inbuf, inblocks); stack_burn = nburn > stack_burn ? nburn : stack_burn; hd->count = 0; hd->nblocks_high += (hd->nblocks + inblocks < inblocks); hd->nblocks += inblocks; - inlen -= inblocks * blocksize; - inbuf += inblocks * blocksize; + inlen -= inblocks << blocksize_shift; + inbuf += inblocks << blocksize_shift; } if (inlen) diff --git a/cipher/hash-common.h b/cipher/hash-common.h index 0b3ade11..561e77a7 100644 --- a/cipher/hash-common.h +++ b/cipher/hash-common.h @@ -51,7 +51,7 @@ typedef struct gcry_md_block_ctx MD_NBLOCKS_TYPE nblocks; MD_NBLOCKS_TYPE nblocks_high; int count; - size_t blocksize; + unsigned int blocksize_shift; _gcry_md_block_write_t bwrite; } gcry_md_block_ctx_t; diff --git a/cipher/md2.c b/cipher/md2.c index bf2fbee4..dc4deac3 100644 --- a/cipher/md2.c +++ b/cipher/md2.c @@ -135,7 +135,7 @@ md2_init (void *context, unsigned int flags) (void)flags; memset (ctx, 0, sizeof(*ctx)); - ctx->bctx.blocksize = 16; + ctx->bctx.blocksize_shift = _gcry_ctz(16); ctx->bctx.bwrite = transform; } diff --git a/cipher/md4.c b/cipher/md4.c index b75bc5e6..24986c27 100644 --- a/cipher/md4.c +++ b/cipher/md4.c @@ -83,7 +83,7 @@ md4_init (void *context, unsigned int flags) ctx->bctx.nblocks = 0; ctx->bctx.nblocks_high = 0; ctx->bctx.count = 0; - ctx->bctx.blocksize = 64; + ctx->bctx.blocksize_shift = _gcry_ctz(64); ctx->bctx.bwrite = transform; } diff --git a/cipher/md5.c b/cipher/md5.c index 94fcdf03..6859d566 100644 --- a/cipher/md5.c +++ b/cipher/md5.c @@ -67,7 +67,7 @@ md5_init( void *context, unsigned int flags) ctx->bctx.nblocks = 0; ctx->bctx.nblocks_high = 0; ctx->bctx.count = 0; - ctx->bctx.blocksize = 64; + ctx->bctx.blocksize_shift = _gcry_ctz(64); ctx->bctx.bwrite = transform; } diff --git a/cipher/rmd160.c b/cipher/rmd160.c index 24210a07..0608f74c 100644 --- a/cipher/rmd160.c +++ b/cipher/rmd160.c @@ -166,7 +166,7 @@ rmd160_init (void *context, unsigned int flags) hd->bctx.nblocks = 0; hd->bctx.nblocks_high = 0; hd->bctx.count = 0; - hd->bctx.blocksize = 64; + hd->bctx.blocksize_shift = _gcry_ctz(64); hd->bctx.bwrite = transform; } diff --git a/cipher/sha1.c b/cipher/sha1.c index 23aceef3..d3ee982b 100644 --- a/cipher/sha1.c +++ b/cipher/sha1.c @@ -274,7 +274,7 @@ sha1_init (void *context, unsigned int flags) hd->bctx.nblocks = 0; hd->bctx.nblocks_high = 0; hd->bctx.count = 0; - hd->bctx.blocksize = 64; + hd->bctx.blocksize_shift = _gcry_ctz(64); /* Order of feature checks is important here; last match will be * selected. Keep slower implementations at the top and faster at diff --git a/cipher/sha256.c b/cipher/sha256.c index 562dee9a..175da909 100644 --- a/cipher/sha256.c +++ b/cipher/sha256.c @@ -246,7 +246,7 @@ sha256_common_init (SHA256_CONTEXT *hd) hd->bctx.nblocks = 0; hd->bctx.nblocks_high = 0; hd->bctx.count = 0; - hd->bctx.blocksize = 64; + hd->bctx.blocksize_shift = _gcry_ctz(64); /* Order of feature checks is important here; last match will be * selected. Keep slower implementations at the top and faster at diff --git a/cipher/sha512.c b/cipher/sha512.c index b05157aa..df9a449c 100644 --- a/cipher/sha512.c +++ b/cipher/sha512.c @@ -302,7 +302,7 @@ sha512_init_common (SHA512_CONTEXT *ctx, unsigned int flags) ctx->bctx.nblocks = 0; ctx->bctx.nblocks_high = 0; ctx->bctx.count = 0; - ctx->bctx.blocksize = 128; + ctx->bctx.blocksize_shift = _gcry_ctz(128); /* Order of feature checks is important here; last match will be * selected. Keep slower implementations at the top and faster at diff --git a/cipher/sm3.c b/cipher/sm3.c index b6f0ab28..aee94987 100644 --- a/cipher/sm3.c +++ b/cipher/sm3.c @@ -77,7 +77,7 @@ sm3_init (void *context, unsigned int flags) hd->bctx.nblocks = 0; hd->bctx.nblocks_high = 0; hd->bctx.count = 0; - hd->bctx.blocksize = 64; + hd->bctx.blocksize_shift = _gcry_ctz(64); hd->bctx.bwrite = transform; (void)features; diff --git a/cipher/stribog.c b/cipher/stribog.c index 26787247..c919182a 100644 --- a/cipher/stribog.c +++ b/cipher/stribog.c @@ -1206,7 +1206,7 @@ stribog_init_512 (void *context, unsigned int flags) memset (hd, 0, sizeof (*hd)); - hd->bctx.blocksize = 64; + hd->bctx.blocksize_shift = _gcry_ctz(64); hd->bctx.bwrite = transform; } diff --git a/cipher/tiger.c b/cipher/tiger.c index c78e3ac3..b2f16677 100644 --- a/cipher/tiger.c +++ b/cipher/tiger.c @@ -601,7 +601,7 @@ do_init (void *context, int variant) hd->bctx.nblocks = 0; hd->bctx.nblocks_high = 0; hd->bctx.count = 0; - hd->bctx.blocksize = 64; + hd->bctx.blocksize_shift = _gcry_ctz(64); hd->bctx.bwrite = transform; hd->variant = variant; } diff --git a/cipher/whirlpool.c b/cipher/whirlpool.c index d9b79cf1..79b2026b 100644 --- a/cipher/whirlpool.c +++ b/cipher/whirlpool.c @@ -1179,7 +1179,7 @@ whirlpool_init (void *ctx, unsigned int flags) memset (context, 0, sizeof (*context)); - context->bctx.blocksize = BLOCK_SIZE; + context->bctx.blocksize_shift = _gcry_ctz(BLOCK_SIZE); context->bctx.bwrite = whirlpool_transform; if ((flags & GCRY_MD_FLAG_BUGEMU1)) { |