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
indicator.
/* Helper macro for Merkle-Damgård hash functions. Assumes the context
structs includes the following fields:
- xxx count_low, count_high; // Two word block count
uint8_t block[...]; // Buffer holding one block
unsigned int index; // Index into block
*/
-/* FIXME: Should probably switch to using uint64_t for the count, but
- due to alignment and byte order that may be an ABI change. */
-
+/* Currently used by sha512 (and sha384) only. */
#define MD_INCR(ctx) ((ctx)->count_high += !++(ctx)->count_low)
/* Takes the compression function f as argument. NOTE: also clobbers
};
memcpy(ctx->state, iv, sizeof(ctx->state));
- ctx->count_low = ctx->count_high = 0;
+ ctx->count = 0;
ctx->index = 0;
}
size_t length,
const uint8_t *data)
{
- MD_UPDATE(ctx, length, data, md4_compress, MD_INCR(ctx));
+ MD_UPDATE(ctx, length, data, md4_compress, ctx->count++);
}
void
size_t length,
uint8_t *digest)
{
+ uint64_t bit_count;
uint32_t data[MD4_DATA_LENGTH];
unsigned i;
/* There are 512 = 2^9 bits in one block
* Little-endian order => Least significant word first */
-
- data[MD4_DATA_LENGTH-1] = (ctx->count_high << 9) | (ctx->count_low >> 23);
- data[MD4_DATA_LENGTH-2] = (ctx->count_low << 9) | (ctx->index << 3);
+ bit_count = (ctx->count << 9) | (ctx->index << 3);
+ data[MD4_DATA_LENGTH-2] = bit_count;
+ data[MD4_DATA_LENGTH-1] = bit_count >> 32;
md4_transform(ctx->state, data);
_nettle_write_le32(length, digest, ctx->state);
struct md4_ctx
{
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 */
unsigned index; /* Into buffer */
};
0x10325476,
};
memcpy(ctx->state, iv, sizeof(ctx->state));
- ctx->count_low = ctx->count_high = 0;
+ ctx->count = 0;
ctx->index = 0;
}
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
size_t length,
uint8_t *digest)
{
- uint32_t high, low;
+ uint64_t bit_count;
assert(length <= MD5_DIGEST_SIZE);
MD_PAD(ctx, 8, COMPRESS);
- /* There are 512 = 2^9 bits in one block */
- high = (ctx->count_high << 9) | (ctx->count_low >> 23);
- low = (ctx->count_low << 9) | (ctx->index << 3);
+ /* There are 512 = 2^9 bits in one block */
+ bit_count = (ctx->count << 9) | (ctx->index << 3);
- LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 8), low);
- LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 4), high);
+ LE_WRITE_UINT64(ctx->block + (MD5_DATA_SIZE - 8), bit_count);
_nettle_md5_compress(ctx->state, ctx->block);
_nettle_write_le32(length, digest, ctx->state);
struct md5_ctx
{
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 */
unsigned index; /* Into buffer */
};
0xC3D2E1F0,
};
memcpy(ctx->state, iv, sizeof(ctx->state));
- ctx->count_low = ctx->count_high = 0;
+ ctx->count = 0;
ctx->index = 0;
}
void
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
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);
MD_PAD(ctx, 8, COMPRESS);
/* There are 2^9 bits in one block */
- high = (ctx->count_high << 9) | (ctx->count_low >> 23);
- low = (ctx->count_low << 9) | (ctx->index << 3);
+ bit_count = (ctx->count << 9) | (ctx->index << 3);
\
/* append the 64 bit count */
- LE_WRITE_UINT32(ctx->block + 56, low);
- LE_WRITE_UINT32(ctx->block + 60, high);
+ LE_WRITE_UINT64(ctx->block + 56, bit_count);
_nettle_ripemd160_compress(ctx->state, ctx->block);
_nettle_write_le32(length, digest, ctx->state);
struct ripemd160_ctx
{
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];
unsigned int index;
};
};
memcpy(ctx->state, iv, sizeof(ctx->state));
- ctx->count_low = ctx->count_high = 0;
+ ctx->count = 0;
/* Initialize buffer */
ctx->index = 0;
sha1_update(struct sha1_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
size_t length,
uint8_t *digest)
{
- uint32_t high, low;
+ uint64_t bit_count;
assert(length <= SHA1_DIGEST_SIZE);
MD_PAD(ctx, 8, COMPRESS);
- /* There are 512 = 2^9 bits in one block */
- high = (ctx->count_high << 9) | (ctx->count_low >> 23);
- low = (ctx->count_low << 9) | (ctx->index << 3);
+ /* There are 512 = 2^9 bits in one block */
+ bit_count = (ctx->count << 9) | (ctx->index << 3);
/* append the 64 bit count */
- WRITE_UINT32(ctx->block + (SHA1_DATA_SIZE - 8), high);
- WRITE_UINT32(ctx->block + (SHA1_DATA_SIZE - 4), low);
+ WRITE_UINT64(ctx->block + (SHA1_DATA_SIZE - 8), bit_count);
_nettle_sha1_compress(ctx->state, ctx->block);
_nettle_write_be32(length, digest, ctx->state);
struct sha1_ctx
{
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 */
unsigned int index; /* index into buffer */
};
struct sha256_ctx
{
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 */
unsigned int index; /* index into buffer */
};
memcpy(ctx->state, H0, sizeof(H0));
/* Initialize bit count */
- ctx->count_low = ctx->count_high = 0;
+ ctx->count = 0;
/* Initialize buffer */
ctx->index = 0;
sha256_update(struct sha256_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++);
}
static void
size_t length,
uint8_t *digest)
{
- uint32_t high, low;
+ uint64_t bit_count;
assert(length <= SHA256_DIGEST_SIZE);
MD_PAD(ctx, 8, COMPRESS);
/* There are 512 = 2^9 bits in one block */
- high = (ctx->count_high << 9) | (ctx->count_low >> 23);
- low = (ctx->count_low << 9) | (ctx->index << 3);
+ bit_count = (ctx->count << 9) | (ctx->index << 3);
/* This is slightly inefficient, as the numbers are converted to
big-endian format, and will be converted back by the compression
function. It's probably not worth the effort to fix this. */
- WRITE_UINT32(ctx->block + (SHA256_DATA_SIZE - 8), high);
- WRITE_UINT32(ctx->block + (SHA256_DATA_SIZE - 4), low);
+ WRITE_UINT64(ctx->block + (SHA256_DATA_SIZE - 8), bit_count);
COMPRESS(ctx, ctx->block);
_nettle_write_be32(length, digest, ctx->state);
memcpy(ctx->state, H0, sizeof(H0));
/* Initialize bit count */
- ctx->count_low = ctx->count_high = 0;
+ ctx->count = 0;
/* Initialize buffer */
ctx->index = 0;