Skip to content

Commit 5d5210c

Browse files
torvaldsgitster
authored andcommitted
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 [email protected]. Signed-off-by: Linus Torvalds <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e869e11 commit 5d5210c

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

block-sha1/sha1.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ static void blk_SHA1Block(blk_SHA_CTX *ctx, const unsigned int *data);
1414

1515
void blk_SHA1_Init(blk_SHA_CTX *ctx)
1616
{
17-
ctx->lenW = 0;
1817
ctx->size = 0;
1918

2019
/* Initialize H with the magic constants (see FIPS180 for constants)
@@ -29,9 +28,9 @@ void blk_SHA1_Init(blk_SHA_CTX *ctx)
2928

3029
void blk_SHA1_Update(blk_SHA_CTX *ctx, const void *data, unsigned long len)
3130
{
32-
int lenW = ctx->lenW;
31+
int lenW = ctx->size & 63;
3332

34-
ctx->size += (unsigned long long) len << 3;
33+
ctx->size += len;
3534

3635
/* Read the data into W and process blocks as they get full
3736
*/
@@ -43,7 +42,6 @@ void blk_SHA1_Update(blk_SHA_CTX *ctx, const void *data, unsigned long len)
4342
lenW = (lenW + left) & 63;
4443
len -= left;
4544
data += left;
46-
ctx->lenW = lenW;
4745
if (lenW)
4846
return;
4947
blk_SHA1Block(ctx, ctx->W);
@@ -53,10 +51,8 @@ void blk_SHA1_Update(blk_SHA_CTX *ctx, const void *data, unsigned long len)
5351
data += 64;
5452
len -= 64;
5553
}
56-
if (len) {
54+
if (len)
5755
memcpy(ctx->W, data, len);
58-
ctx->lenW = len;
59-
}
6056
}
6157

6258

@@ -68,10 +64,11 @@ void blk_SHA1_Final(unsigned char hashout[20], blk_SHA_CTX *ctx)
6864

6965
/* Pad with a binary 1 (ie 0x80), then zeroes, then length
7066
*/
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);
7369

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)));
7572
blk_SHA1_Update(ctx, padlen, 8);
7673

7774
/* Output hash

block-sha1/sha1.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
typedef struct {
88
unsigned int H[5];
99
unsigned int W[16];
10-
int lenW;
1110
unsigned long long size;
1211
} blk_SHA_CTX;
1312

0 commit comments

Comments
 (0)