On Mon, Oct 24, 2005 at 05:56:06PM +0000, Nicolas Pitre wrote:
> 
> 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 course0.

I added a couple of other optimisations.  How does this look to you?
I'll switch to your variable names in a diffferent patch so that they
don't obscure the actual changes.

[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.

Thanks,
-- 
Visit Openswan at http://www.openswan.org/
Email: Herbert Xu ~{PmV>HI~} <[EMAIL PROTECTED]>
Home Page: http://gondor.apana.org.au/~herbert/
PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
diff --git a/crypto/sha1.c b/crypto/sha1.c
--- a/crypto/sha1.c
+++ b/crypto/sha1.c
@@ -50,21 +50,29 @@ static void sha1_update(void *ctx, const
 {
        struct sha1_ctx *sctx = ctx;
        unsigned int i, j;
-       u32 temp[SHA_WORKSPACE_WORDS];
 
        j = (sctx->count >> 3) & 0x3f;
        sctx->count += len << 3;
+       i = 0;
 
        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];
+               const u8 *src = data;
+
+               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);
 }
 

Reply via email to