Commit cfa8d17cc836905ad174fd924701b352585d62f1

Authored by Nicolas Pitre
Committed by David S. Miller
1 parent c8a19c91b5

[CRYPTO] sha1: Avoid useless memcpy()

The current code unconditionally copy the first block for every call to
sha1_update().  This can be avoided if there is no pending partial block.
This is always the case on the first call to sha1_update() (if the length
is >= 64 of course.

Furthermore, temp does need to be called if sha_transform is never invoked.
Also consolidate the sha_transform calls into one to reduce code size.

Signed-off-by: Nicolas Pitre <nico@cam.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Showing 1 changed file with 17 additions and 8 deletions Side-by-side Diff

... ... @@ -50,22 +50,31 @@
50 50 {
51 51 struct sha1_ctx *sctx = ctx;
52 52 unsigned int i, j;
53   - u32 temp[SHA_WORKSPACE_WORDS];
  53 + const u8 *src;
54 54  
55 55 j = (sctx->count >> 3) & 0x3f;
56 56 sctx->count += len << 3;
  57 + i = 0;
  58 + src = data;
57 59  
58 60 if ((j + len) > 63) {
59   - memcpy(&sctx->buffer[j], data, (i = 64-j));
60   - sha_transform(sctx->state, sctx->buffer, temp);
61   - for ( ; i + 63 < len; i += 64) {
62   - sha_transform(sctx->state, &data[i], temp);
  61 + u32 temp[SHA_WORKSPACE_WORDS];
  62 +
  63 + if (j) {
  64 + memcpy(&sctx->buffer[j], data, (i = 64-j));
  65 + src = sctx->buffer;
63 66 }
  67 +
  68 + do {
  69 + sha_transform(sctx->state, src, temp);
  70 + i += 64;
  71 + src = &data[i];
  72 + } while (i + 63 < len);
  73 +
  74 + memset(temp, 0, sizeof(temp));
64 75 j = 0;
65 76 }
66   - else i = 0;
67   - memset(temp, 0, sizeof(temp));
68   - memcpy(&sctx->buffer[j], &data[i], len - i);
  77 + memcpy(&sctx->buffer[j], src, len - i);
69 78 }
70 79  
71 80