Skip to content

Commit eb28464

Browse files
Change SHA256 byte counter from size_t to uint64_t
This avoids that the SHA256 implementation would produce wrong paddings and thus wrong digests for messages of length >= 2^32 bytes on 32-bit platforms. This is not exploitable in any way since the SHA256 API is an internal API and we never call it with that long messages.
1 parent ac83be3 commit eb28464

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

src/hash.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
typedef struct {
1414
uint32_t s[8];
1515
uint32_t buf[16]; /* In big endian */
16-
size_t bytes;
16+
uint64_t bytes;
1717
} secp256k1_sha256;
1818

1919
static void secp256k1_sha256_initialize(secp256k1_sha256 *hash);

src/hash_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ static void secp256k1_sha256_finalize(secp256k1_sha256 *hash, unsigned char *out
153153
uint32_t sizedesc[2];
154154
uint32_t out[8];
155155
int i = 0;
156+
/* The maximum message size of SHA256 is 2^64-1 bits. */
157+
VERIFY_CHECK(hash->bytes < ((uint64_t)1 << 61));
156158
sizedesc[0] = BE32(hash->bytes >> 29);
157159
sizedesc[1] = BE32(hash->bytes << 3);
158160
secp256k1_sha256_write(hash, pad, 1 + ((119 - (hash->bytes % 64)) % 64));

0 commit comments

Comments
 (0)