summaryrefslogtreecommitdiff
path: root/cipher/sha256.c
diff options
context:
space:
mode:
authorJussi Kivilinna <jussi.kivilinna@iki.fi>2019-04-05 18:52:47 +0300
committerJussi Kivilinna <jussi.kivilinna@iki.fi>2019-04-05 18:52:47 +0300
commite76cd0e2b1f6025c1319576a5848815d1d231aeb (patch)
tree9615b401df9891583e0d95b1f7e6de370472a972 /cipher/sha256.c
parentc54b1c96c644c941f3eb3d2a09432b82f25b6ff1 (diff)
downloadlibgcrypt-e76cd0e2b1f6025c1319576a5848815d1d231aeb.tar.gz
Optimizations for digest final functions
* cipher/md4.c (md4_final): Avoid byte-by-byte buffer setting when padding; Merge extra and last block processing. * cipher/md5.c (md5_final): Ditto. * cipher/rmd160.c (rmd160_final): Ditto. * cipher/sha1.c (sha1_final): Ditto. * cipher/sha256.c (sha256_final): Ditto. * cipher/sm3.c (sm3_final): Ditto. * cipher/tiger.c (tiger_final): Ditto. * cipher/sha512.c (sha512_final): Avoid byte-by-byte buffer setting when padding. * cipher/stribog.c (stribog_final): Ditto. * cipher/whirlpool.c (whirlpool_final): Ditto. -- Signed-off-by: Jussi Kivilinna <jussi.kivilinna@iki.fi>
Diffstat (limited to 'cipher/sha256.c')
-rw-r--r--cipher/sha256.c37
1 files changed, 22 insertions, 15 deletions
diff --git a/cipher/sha256.c b/cipher/sha256.c
index e82a9d90..327e1029 100644
--- a/cipher/sha256.c
+++ b/cipher/sha256.c
@@ -498,25 +498,30 @@ sha256_final(void *context)
msb <<= 3;
msb |= t >> 29;
- if (hd->bctx.count < 56)
- { /* enough room */
+ if (hd->bctx.count < 56) /* enough room */
+ {
hd->bctx.buf[hd->bctx.count++] = 0x80; /* pad */
- while (hd->bctx.count < 56)
- hd->bctx.buf[hd->bctx.count++] = 0; /* pad */
+ if (hd->bctx.count < 56)
+ memset (&hd->bctx.buf[hd->bctx.count], 0, 56 - hd->bctx.count);
+ hd->bctx.count = 56;
+
+ /* append the 64 bit count */
+ buf_put_be32(hd->bctx.buf + 56, msb);
+ buf_put_be32(hd->bctx.buf + 60, lsb);
+ burn = (*hd->bctx.bwrite) (hd, hd->bctx.buf, 1);
}
- else
- { /* need one extra block */
+ else /* need one extra block */
+ {
hd->bctx.buf[hd->bctx.count++] = 0x80; /* pad character */
- while (hd->bctx.count < 64)
- hd->bctx.buf[hd->bctx.count++] = 0;
- _gcry_md_block_write (hd, NULL, 0); /* flush */;
- memset (hd->bctx.buf, 0, 56 ); /* fill next block with zeroes */
+ /* fill pad and next block with zeroes */
+ memset (&hd->bctx.buf[hd->bctx.count], 0, 64 - hd->bctx.count + 56);
+ hd->bctx.count = 64 + 56;
+
+ /* append the 64 bit count */
+ buf_put_be32(hd->bctx.buf + 64 + 56, msb);
+ buf_put_be32(hd->bctx.buf + 64 + 60, lsb);
+ burn = (*hd->bctx.bwrite) (hd, hd->bctx.buf, 2);
}
- /* append the 64 bit count */
- buf_put_be32(hd->bctx.buf + 56, msb);
- buf_put_be32(hd->bctx.buf + 60, lsb);
- burn = (*hd->bctx.bwrite) (hd, hd->bctx.buf, 1);
- _gcry_burn_stack (burn);
p = hd->bctx.buf;
#define X(a) do { buf_put_be32(p, hd->h##a); p += 4; } while(0)
@@ -529,6 +534,8 @@ sha256_final(void *context)
X(6);
X(7);
#undef X
+
+ _gcry_burn_stack (burn);
}
static byte *