block-sha1: get rid of redundant 'lenW' context
.. and simplify the ctx->size logic. We now count the size in bytes, which means that 'lenW' was always just the low 6 bits of the total size, so we don't carry it around separately any more. And we do the 'size in bits' shift at the end. Suggested by Nicolas Pitre and linux@horizon.com. Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Linus Torvalds committed
Aug 6, 2009 at 07:45 UTC
5d5210c35aa83342163ab0ab80b8e6d6fa3ce931
2 files changed
+7
-11
block-sha1/sha1.c
+7
-10
@@ -14,7 +14,6 @@ static void blk_SHA1Block(blk_SHA_CTX *ctx, const unsigned int *data);
14
15
void blk_SHA1_Init(blk_SHA_CTX *ctx)
16
{
17
- ctx->lenW = 0;
17
ctx->size = 0;
18
19
/* Initialize H with the magic constants (see FIPS180 for constants)
@@ -29,9 +28,9 @@ void blk_SHA1_Init(blk_SHA_CTX *ctx)
28
29
void blk_SHA1_Update(blk_SHA_CTX *ctx, const void *data, unsigned long len)
30
{
32
- int lenW = ctx->lenW;
31
+ int lenW = ctx->size & 63;
32
34
- ctx->size += (unsigned long long) len << 3;
33
+ ctx->size += len;
34
35
/* Read the data into W and process blocks as they get full
36
*/
@@ -43,7 +42,6 @@ void blk_SHA1_Update(blk_SHA_CTX *ctx, const void *data, unsigned long len)
42
lenW = (lenW + left) & 63;
43
len -= left;
44
data += left;
46
- ctx->lenW = lenW;
45
if (lenW)
46
return;
47
blk_SHA1Block(ctx, ctx->W);
@@ -53,10 +51,8 @@ void blk_SHA1_Update(blk_SHA_CTX *ctx, const void *data, unsigned long len)
51
data += 64;
52
len -= 64;
53
}
56
- if (len) {
54
+ if (len)
55
memcpy(ctx->W, data, len);
58
- ctx->lenW = len;
59
- }
56
}
57
58
@@ -68,10 +64,11 @@ void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx)
64
65
/* Pad with a binary 1 (ie 0x80), then zeroes, then length
66
*/
71
- padlen[0] = htonl(ctx->size >> 32);
72
- padlen[1] = htonl(ctx->size);
67
+ padlen[0] = htonl(ctx->size >> 29);
68
+ padlen[1] = htonl(ctx->size << 3);
69
74
- blk_SHA1_Update(ctx, pad, 1+ (63 & (55 - ctx->lenW)));
70
+ i = ctx->size & 63;
71
+ blk_SHA1_Update(ctx, pad, 1+ (63 & (55 - i)));
72
blk_SHA1_Update(ctx, padlen, 8);
73
74
/* Output hash
block-sha1/sha1.h
-1
@@ -7,7 +7,6 @@
7
typedef struct {
8
unsigned int H[5];
9
unsigned int W[16];
10
- int lenW;
10
unsigned long long size;
11
} blk_SHA_CTX;
12