Skip to content
Snippets Groups Projects
Commit f773cbd7 authored by Niels Möller's avatar Niels Möller
Browse files

(md5_init): Use memcpy for initializing the state vector.

(COMPRESS): New macro, wrapping _nettle_md5_compress.
(md5_update): Use MD_UPDATE.
(md5_digest): Inline md5_final processing. Use MD_PAD and
_nettle_write_le32.
(md5_final): Deleted.

Rev: nettle/md5.c:1.3
parent 563a82e7
No related branches found
No related tags found
No related merge requests found
...@@ -38,110 +38,50 @@ ...@@ -38,110 +38,50 @@
#include "macros.h" #include "macros.h"
#include "nettle-write.h" #include "nettle-write.h"
static void
md5_final(struct md5_ctx *ctx);
void void
md5_init(struct md5_ctx *ctx) md5_init(struct md5_ctx *ctx)
{ {
ctx->digest[0] = 0x67452301; const uint32_t iv[_MD5_DIGEST_LENGTH] =
ctx->digest[1] = 0xefcdab89; {
ctx->digest[2] = 0x98badcfe; 0x67452301,
ctx->digest[3] = 0x10325476; 0xefcdab89,
0x98badcfe,
ctx->count_l = ctx->count_h = 0; 0x10325476,
};
memcpy(ctx->state, iv, sizeof(ctx->state));
ctx->count_low = ctx->count_high = 0;
ctx->index = 0; ctx->index = 0;
} }
#define MD5_INCR(ctx) ((ctx)->count_h += !++(ctx)->count_l) #define COMPRESS(ctx, data) (_nettle_md5_compress((ctx)->state, (data)))
void void
md5_update(struct md5_ctx *ctx, md5_update(struct md5_ctx *ctx,
unsigned length, unsigned length,
const uint8_t *data) const uint8_t *data)
{ {
if (ctx->index) MD_UPDATE(ctx, length, data, COMPRESS, MD_INCR(ctx));
{
/* Try to fill partial block */
unsigned left = MD5_DATA_SIZE - ctx->index;
if (length < left)
{
memcpy(ctx->block + ctx->index, data, length);
ctx->index += length;
return; /* Finished */
}
else
{
memcpy(ctx->block + ctx->index, data, left);
_nettle_md5_compress(ctx->digest, ctx->block);
MD5_INCR(ctx);
data += left;
length -= left;
}
}
while (length >= MD5_DATA_SIZE)
{
_nettle_md5_compress(ctx->digest, data);
MD5_INCR(ctx);
data += MD5_DATA_SIZE;
length -= MD5_DATA_SIZE;
}
if ((ctx->index = length)) /* This assignment is intended */
/* Buffer leftovers */
memcpy(ctx->block, data, length);
} }
void void
md5_digest(struct md5_ctx *ctx, md5_digest(struct md5_ctx *ctx,
unsigned length, unsigned length,
uint8_t *digest) uint8_t *digest)
{
assert(length <= MD5_DIGEST_SIZE);
md5_final(ctx);
_nettle_write_le32(length, digest, ctx->digest);
md5_init(ctx);
}
/* Final wrapup - pad to MD5_DATA_SIZE-byte boundary with the bit
* pattern 1 0* (64-bit count of bits processed, LSB-first) */
static void
md5_final(struct md5_ctx *ctx)
{ {
uint32_t bitcount_high; uint32_t high, low;
uint32_t bitcount_low;
unsigned i;
i = ctx->index; assert(length <= MD5_DIGEST_SIZE);
/* Set the first char of padding to 0x80. This is safe since there MD_PAD(ctx, 8, COMPRESS);
* is always at least one byte free */
assert(i < MD5_DATA_SIZE);
ctx->block[i++] = 0x80;
if (i > (MD5_DATA_SIZE - 8)) /* There are 512 = 2^9 bits in one block */
{ high = (ctx->count_high << 9) | (ctx->count_low >> 23);
/* No room for length in this block. Process it and low = (ctx->count_low << 9) | (ctx->index << 3);
pad with another one */
memset(ctx->block + i, 0, MD5_DATA_SIZE - i);
_nettle_md5_compress(ctx->digest, ctx->block);
i = 0;
}
if (i < (MD5_DATA_SIZE - 8))
memset(ctx->block + i, 0, (MD5_DATA_SIZE - 8) - i);
/* There are 512 = 2^9 bits in one block
* Little-endian order => Least significant word first */
bitcount_low = (ctx->count_l << 9) | (ctx->index << 3); LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 8), low);
bitcount_high = (ctx->count_h << 9) | (ctx->count_l >> 23); LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 4), high);
LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 8), bitcount_low); _nettle_md5_compress(ctx->state, ctx->block);
LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 4), bitcount_high);
_nettle_write_le32(length, digest, ctx->state);
_nettle_md5_compress(ctx->digest, ctx->block); md5_init(ctx);
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment