Store the partial block as uint8_t, not uint32_t.

The uint32_t likely dates to them using HASH_LONG everywhere. Nothing ever
touches c->data as a uint32_t, only bytes. (Which makes sense seeing as it
stores the partial block.)

Change-Id: I634cb7f2b6306523aa663f8697b7dc92aa491320
Reviewed-on: https://boringssl-review.googlesource.com/6651
Reviewed-by: Adam Langley <agl@google.com>
This commit is contained in:
David Benjamin 2015-12-07 13:17:03 -05:00 committed by Adam Langley
parent 5a19d7dfa8
commit 0aff3ffb88
4 changed files with 21 additions and 24 deletions

View File

@ -77,7 +77,7 @@ extern "C" {
* typedef struct <name>_state_st {
* uint32_t h[<chaining length> / sizeof(uint32_t)];
* uint32_t Nl, Nh;
* uint32_t data[HASH_CBLOCK / sizeof(uint32_t)];
* uint8_t data[HASH_CBLOCK];
* unsigned num;
* ...
* } <NAME>_CTX;
@ -245,19 +245,17 @@ int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) {
size_t n = c->num;
if (n != 0) {
uint8_t *p = (uint8_t *)c->data;
if (len >= HASH_CBLOCK || len + n >= HASH_CBLOCK) {
memcpy(p + n, data, HASH_CBLOCK - n);
HASH_BLOCK_DATA_ORDER(c->h, p, 1);
memcpy(c->data + n, data, HASH_CBLOCK - n);
HASH_BLOCK_DATA_ORDER(c->h, c->data, 1);
n = HASH_CBLOCK - n;
data += n;
len -= n;
c->num = 0;
/* Keep |c->data| zeroed when unused. */
memset(p, 0, HASH_CBLOCK);
memset(c->data, 0, HASH_CBLOCK);
} else {
memcpy(p + n, data, len);
memcpy(c->data + n, data, len);
c->num += (unsigned)len;
return 1;
}
@ -272,9 +270,8 @@ int HASH_UPDATE(HASH_CTX *c, const void *data_, size_t len) {
}
if (len != 0) {
uint8_t *p = (uint8_t *)c->data;
c->num = (unsigned)len;
memcpy(p, data, len);
memcpy(c->data, data, len);
}
return 1;
}
@ -286,23 +283,23 @@ void HASH_TRANSFORM(HASH_CTX *c, const uint8_t *data) {
int HASH_FINAL(uint8_t *md, HASH_CTX *c) {
uint8_t *p = (uint8_t *)c->data;
size_t n = c->num;
/* |c->data| always has room for at least one byte. A full block would have
* been consumed. */
size_t n = c->num;
assert(n < HASH_CBLOCK);
p[n] = 0x80;
c->data[n] = 0x80;
n++;
/* Fill the block with zeros if there isn't room for a 64-bit length. */
if (n > (HASH_CBLOCK - 8)) {
memset(p + n, 0, HASH_CBLOCK - n);
memset(c->data + n, 0, HASH_CBLOCK - n);
n = 0;
HASH_BLOCK_DATA_ORDER(c->h, p, 1);
HASH_BLOCK_DATA_ORDER(c->h, c->data, 1);
}
memset(p + n, 0, HASH_CBLOCK - 8 - n);
memset(c->data + n, 0, HASH_CBLOCK - 8 - n);
p += HASH_CBLOCK - 8;
/* Append a 64-bit length to the block and process it. */
uint8_t *p = c->data + HASH_CBLOCK - 8;
#if defined(DATA_ORDER_IS_BIG_ENDIAN)
(void)HOST_l2c(c->Nh, p);
(void)HOST_l2c(c->Nl, p);
@ -310,10 +307,10 @@ int HASH_FINAL(uint8_t *md, HASH_CTX *c) {
(void)HOST_l2c(c->Nl, p);
(void)HOST_l2c(c->Nh, p);
#endif
p -= HASH_CBLOCK;
HASH_BLOCK_DATA_ORDER(c->h, p, 1);
assert(p == c->data + HASH_CBLOCK);
HASH_BLOCK_DATA_ORDER(c->h, c->data, 1);
c->num = 0;
memset(p, 0, HASH_CBLOCK);
memset(c->data, 0, HASH_CBLOCK);
HASH_MAKE_STRING(c, md);
return 1;

View File

@ -90,7 +90,7 @@ OPENSSL_EXPORT void MD4_Transform(MD4_CTX *md4, const uint8_t *block);
struct md4_state_st {
uint32_t h[4];
uint32_t Nl, Nh;
uint32_t data[16];
uint8_t data[MD4_CBLOCK];
unsigned num;
};

View File

@ -95,7 +95,7 @@ OPENSSL_EXPORT void MD5_Transform(MD5_CTX *md5, const uint8_t *block);
struct md5_state_st {
uint32_t h[4];
uint32_t Nl, Nh;
uint32_t data[16];
uint8_t data[MD5_CBLOCK];
unsigned num;
};

View File

@ -115,7 +115,7 @@ struct sha_state_st {
};
#endif
uint32_t Nl, Nh;
uint32_t data[16];
uint8_t data[SHA_CBLOCK];
unsigned num;
};
@ -176,7 +176,7 @@ OPENSSL_EXPORT void SHA256_Transform(SHA256_CTX *sha, const uint8_t *data);
struct sha256_state_st {
uint32_t h[8];
uint32_t Nl, Nh;
uint32_t data[16];
uint8_t data[SHA256_CBLOCK];
unsigned num, md_len;
};