Skip to content

Commit

Permalink
[CRYPTO] sha1: Avoid useless memcpy()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Nicolas Pitre authored and David S. Miller committed Jan 9, 2006
1 parent c8a19c9 commit cfa8d17
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions crypto/sha1.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,31 @@ static void sha1_update(void *ctx, const u8 *data, unsigned int len)
{
struct sha1_ctx *sctx = ctx;
unsigned int i, j;
u32 temp[SHA_WORKSPACE_WORDS];
const u8 *src;

j = (sctx->count >> 3) & 0x3f;
sctx->count += len << 3;
i = 0;
src = data;

if ((j + len) > 63) {
memcpy(&sctx->buffer[j], data, (i = 64-j));
sha_transform(sctx->state, sctx->buffer, temp);
for ( ; i + 63 < len; i += 64) {
sha_transform(sctx->state, &data[i], temp);
u32 temp[SHA_WORKSPACE_WORDS];

if (j) {
memcpy(&sctx->buffer[j], data, (i = 64-j));
src = sctx->buffer;
}

do {
sha_transform(sctx->state, src, temp);
i += 64;
src = &data[i];
} while (i + 63 < len);

memset(temp, 0, sizeof(temp));
j = 0;
}
else i = 0;
memset(temp, 0, sizeof(temp));
memcpy(&sctx->buffer[j], &data[i], len - i);
memcpy(&sctx->buffer[j], src, len - i);
}


Expand Down

0 comments on commit cfa8d17

Please sign in to comment.