Skip to content

Commit

Permalink
sha1/256: Switch to 32bit byte counts
Browse files Browse the repository at this point in the history
This is the maximum length passed in to begin with, and comes with a stack and
code size reduction.  The only complexity is to explicitly widen before the
bytes=>bits conversion in sha*_final().

Signed-off-by: Andrew Cooper <[email protected]>
  • Loading branch information
andyhhp authored and dpsmith committed Jul 28, 2021
1 parent 23c1fbe commit bd4f9f0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
4 changes: 2 additions & 2 deletions sha1sum.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ rol( u32 x, int n)
}

typedef struct {
u64 count;
u32 count;
union {
struct {
u32 h0, h1, h2, h3, h4;
Expand Down Expand Up @@ -188,7 +188,7 @@ sha1_final(SHA1_CONTEXT *hd, u8 hash[SHA1_DIGEST_SIZE])

/* append the 64 bit count */
u64 *count = (void *)&hd->buf[56];
*count = cpu_to_be64(hd->count << 3);
*count = cpu_to_be64((u64)hd->count << 3);
sha1_transform(hd, hd->buf);

u32 *p = (void *)hash;
Expand Down
4 changes: 2 additions & 2 deletions sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

struct sha256_state {
u32 state[SHA256_DIGEST_SIZE / 4];
u64 count;
u32 count;
u8 buf[SHA256_BLOCK_SIZE];
};

Expand Down Expand Up @@ -178,7 +178,7 @@ static void sha256_final(struct sha256_state *sctx, void *_dst)

/* Append the 64 bit count */
count = (void *)&sctx->buf[56];
*count = cpu_to_be64(sctx->count << 3);
*count = cpu_to_be64((u64)sctx->count << 3);
sha256_transform(sctx->state, sctx->buf);

/* Store state in digest */
Expand Down

0 comments on commit bd4f9f0

Please sign in to comment.