Commit cb83bd33 authored by Niels Möller's avatar Niels Möller

Made hash functions use an uint64_t for the block count.

parent 89755c91
2013-09-28 Niels Möller <nisse@lysator.liu.se> 2013-09-28 Niels Möller <nisse@lysator.liu.se>
* md4.h (struct md4_ctx): Use single uint64_t variable for block
count.
* md4.c: Use new block count variable.
* md5.c, md5.h (struct md5_ctx): Likewise.
* ripemd160.c, ripemd160.h (struct ripemd160_ctx): Likewise.
* sha1.c, sha1.h (struct sha1_ctx): Likewise.
* sha256.c, sha2.h (struct sha256_ctx): Likewise.
* testsuite/testutils.c (test_hash_large): Added simple progress * testsuite/testutils.c (test_hash_large): Added simple progress
indicator. indicator.
......
...@@ -162,14 +162,11 @@ do { \ ...@@ -162,14 +162,11 @@ do { \
/* Helper macro for Merkle-Damgård hash functions. Assumes the context /* Helper macro for Merkle-Damgård hash functions. Assumes the context
structs includes the following fields: structs includes the following fields:
xxx count_low, count_high; // Two word block count
uint8_t block[...]; // Buffer holding one block uint8_t block[...]; // Buffer holding one block
unsigned int index; // Index into block unsigned int index; // Index into block
*/ */
/* FIXME: Should probably switch to using uint64_t for the count, but /* Currently used by sha512 (and sha384) only. */
due to alignment and byte order that may be an ABI change. */
#define MD_INCR(ctx) ((ctx)->count_high += !++(ctx)->count_low) #define MD_INCR(ctx) ((ctx)->count_high += !++(ctx)->count_low)
/* Takes the compression function f as argument. NOTE: also clobbers /* Takes the compression function f as argument. NOTE: also clobbers
......
...@@ -61,7 +61,7 @@ md4_init(struct md4_ctx *ctx) ...@@ -61,7 +61,7 @@ md4_init(struct md4_ctx *ctx)
}; };
memcpy(ctx->state, iv, sizeof(ctx->state)); memcpy(ctx->state, iv, sizeof(ctx->state));
ctx->count_low = ctx->count_high = 0; ctx->count = 0;
ctx->index = 0; ctx->index = 0;
} }
...@@ -70,7 +70,7 @@ md4_update(struct md4_ctx *ctx, ...@@ -70,7 +70,7 @@ md4_update(struct md4_ctx *ctx,
size_t length, size_t length,
const uint8_t *data) const uint8_t *data)
{ {
MD_UPDATE(ctx, length, data, md4_compress, MD_INCR(ctx)); MD_UPDATE(ctx, length, data, md4_compress, ctx->count++);
} }
void void
...@@ -78,6 +78,7 @@ md4_digest(struct md4_ctx *ctx, ...@@ -78,6 +78,7 @@ md4_digest(struct md4_ctx *ctx,
size_t length, size_t length,
uint8_t *digest) uint8_t *digest)
{ {
uint64_t bit_count;
uint32_t data[MD4_DATA_LENGTH]; uint32_t data[MD4_DATA_LENGTH];
unsigned i; unsigned i;
...@@ -89,9 +90,9 @@ md4_digest(struct md4_ctx *ctx, ...@@ -89,9 +90,9 @@ md4_digest(struct md4_ctx *ctx,
/* There are 512 = 2^9 bits in one block /* There are 512 = 2^9 bits in one block
* Little-endian order => Least significant word first */ * Little-endian order => Least significant word first */
bit_count = (ctx->count << 9) | (ctx->index << 3);
data[MD4_DATA_LENGTH-1] = (ctx->count_high << 9) | (ctx->count_low >> 23); data[MD4_DATA_LENGTH-2] = bit_count;
data[MD4_DATA_LENGTH-2] = (ctx->count_low << 9) | (ctx->index << 3); data[MD4_DATA_LENGTH-1] = bit_count >> 32;
md4_transform(ctx->state, data); md4_transform(ctx->state, data);
_nettle_write_le32(length, digest, ctx->state); _nettle_write_le32(length, digest, ctx->state);
......
...@@ -47,7 +47,7 @@ extern "C" { ...@@ -47,7 +47,7 @@ extern "C" {
struct md4_ctx struct md4_ctx
{ {
uint32_t state[_MD4_DIGEST_LENGTH]; uint32_t state[_MD4_DIGEST_LENGTH];
uint32_t count_low, count_high; /* Block count */ uint64_t count; /* Block count */
uint8_t block[MD4_DATA_SIZE]; /* Block buffer */ uint8_t block[MD4_DATA_SIZE]; /* Block buffer */
unsigned index; /* Into buffer */ unsigned index; /* Into buffer */
}; };
......
...@@ -49,7 +49,7 @@ md5_init(struct md5_ctx *ctx) ...@@ -49,7 +49,7 @@ md5_init(struct md5_ctx *ctx)
0x10325476, 0x10325476,
}; };
memcpy(ctx->state, iv, sizeof(ctx->state)); memcpy(ctx->state, iv, sizeof(ctx->state));
ctx->count_low = ctx->count_high = 0; ctx->count = 0;
ctx->index = 0; ctx->index = 0;
} }
...@@ -60,7 +60,7 @@ md5_update(struct md5_ctx *ctx, ...@@ -60,7 +60,7 @@ md5_update(struct md5_ctx *ctx,
size_t length, size_t length,
const uint8_t *data) const uint8_t *data)
{ {
MD_UPDATE(ctx, length, data, COMPRESS, MD_INCR(ctx)); MD_UPDATE(ctx, length, data, COMPRESS, ctx->count++);
} }
void void
...@@ -68,18 +68,16 @@ md5_digest(struct md5_ctx *ctx, ...@@ -68,18 +68,16 @@ md5_digest(struct md5_ctx *ctx,
size_t length, size_t length,
uint8_t *digest) uint8_t *digest)
{ {
uint32_t high, low; uint64_t bit_count;
assert(length <= MD5_DIGEST_SIZE); assert(length <= MD5_DIGEST_SIZE);
MD_PAD(ctx, 8, COMPRESS); MD_PAD(ctx, 8, COMPRESS);
/* There are 512 = 2^9 bits in one block */ /* There are 512 = 2^9 bits in one block */
high = (ctx->count_high << 9) | (ctx->count_low >> 23); bit_count = (ctx->count << 9) | (ctx->index << 3);
low = (ctx->count_low << 9) | (ctx->index << 3);
LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 8), low); LE_WRITE_UINT64(ctx->block + (MD5_DATA_SIZE - 8), bit_count);
LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 4), high);
_nettle_md5_compress(ctx->state, ctx->block); _nettle_md5_compress(ctx->state, ctx->block);
_nettle_write_le32(length, digest, ctx->state); _nettle_write_le32(length, digest, ctx->state);
......
...@@ -46,7 +46,7 @@ extern "C" { ...@@ -46,7 +46,7 @@ extern "C" {
struct md5_ctx struct md5_ctx
{ {
uint32_t state[_MD5_DIGEST_LENGTH]; uint32_t state[_MD5_DIGEST_LENGTH];
uint32_t count_low, count_high; /* Block count */ uint64_t count; /* Block count */
uint8_t block[MD5_DATA_SIZE]; /* Block buffer */ uint8_t block[MD5_DATA_SIZE]; /* Block buffer */
unsigned index; /* Into buffer */ unsigned index; /* Into buffer */
}; };
......
...@@ -155,7 +155,7 @@ ripemd160_init(struct ripemd160_ctx *ctx) ...@@ -155,7 +155,7 @@ ripemd160_init(struct ripemd160_ctx *ctx)
0xC3D2E1F0, 0xC3D2E1F0,
}; };
memcpy(ctx->state, iv, sizeof(ctx->state)); memcpy(ctx->state, iv, sizeof(ctx->state));
ctx->count_low = ctx->count_high = 0; ctx->count = 0;
ctx->index = 0; ctx->index = 0;
} }
...@@ -167,25 +167,23 @@ ripemd160_init(struct ripemd160_ctx *ctx) ...@@ -167,25 +167,23 @@ ripemd160_init(struct ripemd160_ctx *ctx)
void void
ripemd160_update(struct ripemd160_ctx *ctx, size_t length, const uint8_t *data) ripemd160_update(struct ripemd160_ctx *ctx, size_t length, const uint8_t *data)
{ {
MD_UPDATE(ctx, length, data, COMPRESS, MD_INCR(ctx)); MD_UPDATE(ctx, length, data, COMPRESS, ctx->count++);
} }
void void
ripemd160_digest(struct ripemd160_ctx *ctx, size_t length, uint8_t *digest) ripemd160_digest(struct ripemd160_ctx *ctx, size_t length, uint8_t *digest)
{ {
uint32_t high, low; uint64_t bit_count;
assert(length <= RIPEMD160_DIGEST_SIZE); assert(length <= RIPEMD160_DIGEST_SIZE);
MD_PAD(ctx, 8, COMPRESS); MD_PAD(ctx, 8, COMPRESS);
/* There are 2^9 bits in one block */ /* There are 2^9 bits in one block */
high = (ctx->count_high << 9) | (ctx->count_low >> 23); bit_count = (ctx->count << 9) | (ctx->index << 3);
low = (ctx->count_low << 9) | (ctx->index << 3);
\ \
/* append the 64 bit count */ /* append the 64 bit count */
LE_WRITE_UINT32(ctx->block + 56, low); LE_WRITE_UINT64(ctx->block + 56, bit_count);
LE_WRITE_UINT32(ctx->block + 60, high);
_nettle_ripemd160_compress(ctx->state, ctx->block); _nettle_ripemd160_compress(ctx->state, ctx->block);
_nettle_write_le32(length, digest, ctx->state); _nettle_write_le32(length, digest, ctx->state);
......
...@@ -48,7 +48,7 @@ extern "C" { ...@@ -48,7 +48,7 @@ extern "C" {
struct ripemd160_ctx struct ripemd160_ctx
{ {
uint32_t state[_RIPEMD160_DIGEST_LENGTH]; uint32_t state[_RIPEMD160_DIGEST_LENGTH];
uint32_t count_low, count_high; /* 64-bit block count */ uint64_t count; /* 64-bit block count */
uint8_t block[RIPEMD160_DATA_SIZE]; uint8_t block[RIPEMD160_DATA_SIZE];
unsigned int index; unsigned int index;
}; };
......
...@@ -66,7 +66,7 @@ sha1_init(struct sha1_ctx *ctx) ...@@ -66,7 +66,7 @@ sha1_init(struct sha1_ctx *ctx)
}; };
memcpy(ctx->state, iv, sizeof(ctx->state)); memcpy(ctx->state, iv, sizeof(ctx->state));
ctx->count_low = ctx->count_high = 0; ctx->count = 0;
/* Initialize buffer */ /* Initialize buffer */
ctx->index = 0; ctx->index = 0;
...@@ -78,7 +78,7 @@ void ...@@ -78,7 +78,7 @@ void
sha1_update(struct sha1_ctx *ctx, sha1_update(struct sha1_ctx *ctx,
size_t length, const uint8_t *data) size_t length, const uint8_t *data)
{ {
MD_UPDATE (ctx, length, data, COMPRESS, MD_INCR(ctx)); MD_UPDATE (ctx, length, data, COMPRESS, ctx->count++);
} }
void void
...@@ -86,19 +86,17 @@ sha1_digest(struct sha1_ctx *ctx, ...@@ -86,19 +86,17 @@ sha1_digest(struct sha1_ctx *ctx,
size_t length, size_t length,
uint8_t *digest) uint8_t *digest)
{ {
uint32_t high, low; uint64_t bit_count;
assert(length <= SHA1_DIGEST_SIZE); assert(length <= SHA1_DIGEST_SIZE);
MD_PAD(ctx, 8, COMPRESS); MD_PAD(ctx, 8, COMPRESS);
/* There are 512 = 2^9 bits in one block */ /* There are 512 = 2^9 bits in one block */
high = (ctx->count_high << 9) | (ctx->count_low >> 23); bit_count = (ctx->count << 9) | (ctx->index << 3);
low = (ctx->count_low << 9) | (ctx->index << 3);
/* append the 64 bit count */ /* append the 64 bit count */
WRITE_UINT32(ctx->block + (SHA1_DATA_SIZE - 8), high); WRITE_UINT64(ctx->block + (SHA1_DATA_SIZE - 8), bit_count);
WRITE_UINT32(ctx->block + (SHA1_DATA_SIZE - 4), low);
_nettle_sha1_compress(ctx->state, ctx->block); _nettle_sha1_compress(ctx->state, ctx->block);
_nettle_write_be32(length, digest, ctx->state); _nettle_write_be32(length, digest, ctx->state);
......
...@@ -48,7 +48,7 @@ extern "C" { ...@@ -48,7 +48,7 @@ extern "C" {
struct sha1_ctx struct sha1_ctx
{ {
uint32_t state[_SHA1_DIGEST_LENGTH]; /* State variables */ uint32_t state[_SHA1_DIGEST_LENGTH]; /* State variables */
uint32_t count_low, count_high; /* 64-bit block count */ uint64_t count; /* 64-bit block count */
uint8_t block[SHA1_DATA_SIZE]; /* SHA1 data buffer */ uint8_t block[SHA1_DATA_SIZE]; /* SHA1 data buffer */
unsigned int index; /* index into buffer */ unsigned int index; /* index into buffer */
}; };
......
...@@ -55,7 +55,7 @@ extern "C" { ...@@ -55,7 +55,7 @@ extern "C" {
struct sha256_ctx struct sha256_ctx
{ {
uint32_t state[_SHA256_DIGEST_LENGTH]; /* State variables */ uint32_t state[_SHA256_DIGEST_LENGTH]; /* State variables */
uint32_t count_low, count_high; /* 64-bit block count */ uint64_t count; /* 64-bit block count */
uint8_t block[SHA256_DATA_SIZE]; /* SHA256 data buffer */ uint8_t block[SHA256_DATA_SIZE]; /* SHA256 data buffer */
unsigned int index; /* index into buffer */ unsigned int index; /* index into buffer */
}; };
......
...@@ -79,7 +79,7 @@ sha256_init(struct sha256_ctx *ctx) ...@@ -79,7 +79,7 @@ sha256_init(struct sha256_ctx *ctx)
memcpy(ctx->state, H0, sizeof(H0)); memcpy(ctx->state, H0, sizeof(H0));
/* Initialize bit count */ /* Initialize bit count */
ctx->count_low = ctx->count_high = 0; ctx->count = 0;
/* Initialize buffer */ /* Initialize buffer */
ctx->index = 0; ctx->index = 0;
...@@ -89,7 +89,7 @@ void ...@@ -89,7 +89,7 @@ void
sha256_update(struct sha256_ctx *ctx, sha256_update(struct sha256_ctx *ctx,
size_t length, const uint8_t *data) size_t length, const uint8_t *data)
{ {
MD_UPDATE (ctx, length, data, COMPRESS, MD_INCR(ctx)); MD_UPDATE (ctx, length, data, COMPRESS, ctx->count++);
} }
static void static void
...@@ -97,21 +97,19 @@ sha256_write_digest(struct sha256_ctx *ctx, ...@@ -97,21 +97,19 @@ sha256_write_digest(struct sha256_ctx *ctx,
size_t length, size_t length,
uint8_t *digest) uint8_t *digest)
{ {
uint32_t high, low; uint64_t bit_count;
assert(length <= SHA256_DIGEST_SIZE); assert(length <= SHA256_DIGEST_SIZE);
MD_PAD(ctx, 8, COMPRESS); MD_PAD(ctx, 8, COMPRESS);
/* There are 512 = 2^9 bits in one block */ /* There are 512 = 2^9 bits in one block */
high = (ctx->count_high << 9) | (ctx->count_low >> 23); bit_count = (ctx->count << 9) | (ctx->index << 3);
low = (ctx->count_low << 9) | (ctx->index << 3);
/* This is slightly inefficient, as the numbers are converted to /* This is slightly inefficient, as the numbers are converted to
big-endian format, and will be converted back by the compression big-endian format, and will be converted back by the compression
function. It's probably not worth the effort to fix this. */ function. It's probably not worth the effort to fix this. */
WRITE_UINT32(ctx->block + (SHA256_DATA_SIZE - 8), high); WRITE_UINT64(ctx->block + (SHA256_DATA_SIZE - 8), bit_count);
WRITE_UINT32(ctx->block + (SHA256_DATA_SIZE - 4), low);
COMPRESS(ctx, ctx->block); COMPRESS(ctx, ctx->block);
_nettle_write_be32(length, digest, ctx->state); _nettle_write_be32(length, digest, ctx->state);
...@@ -141,7 +139,7 @@ sha224_init(struct sha256_ctx *ctx) ...@@ -141,7 +139,7 @@ sha224_init(struct sha256_ctx *ctx)
memcpy(ctx->state, H0, sizeof(H0)); memcpy(ctx->state, H0, sizeof(H0));
/* Initialize bit count */ /* Initialize bit count */
ctx->count_low = ctx->count_high = 0; ctx->count = 0;
/* Initialize buffer */ /* Initialize buffer */
ctx->index = 0; ctx->index = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment