6
6
/* nettle, low-level cryptographics library
8
* Copyright (C) 2001 Niels M�ller
8
* Copyright (C) 2001 Niels Möller
10
10
* The nettle library is free software; you can redistribute it and/or modify
11
11
* it under the terms of the GNU Lesser General Public License as published by
38
38
#include "macros.h"
41
md5_final(struct md5_ctx *ctx);
39
#include "nettle-write.h"
44
42
md5_init(struct md5_ctx *ctx)
46
ctx->digest[0] = 0x67452301;
47
ctx->digest[1] = 0xefcdab89;
48
ctx->digest[2] = 0x98badcfe;
49
ctx->digest[3] = 0x10325476;
51
ctx->count_l = ctx->count_h = 0;
44
const uint32_t iv[_MD5_DIGEST_LENGTH] =
51
memcpy(ctx->state, iv, sizeof(ctx->state));
52
ctx->count_low = ctx->count_high = 0;
55
#define MD5_INCR(ctx) ((ctx)->count_h += !++(ctx)->count_l)
56
#define COMPRESS(ctx, data) (_nettle_md5_compress((ctx)->state, (data)))
58
59
md5_update(struct md5_ctx *ctx,
60
61
const uint8_t *data)
64
/* Try to fill partial block */
65
unsigned left = MD5_DATA_SIZE - ctx->index;
68
memcpy(ctx->block + ctx->index, data, length);
70
return; /* Finished */
74
memcpy(ctx->block + ctx->index, data, left);
76
_nettle_md5_compress(ctx->digest, ctx->block);
83
while (length >= MD5_DATA_SIZE)
85
_nettle_md5_compress(ctx->digest, data);
88
data += MD5_DATA_SIZE;
89
length -= MD5_DATA_SIZE;
91
if ((ctx->index = length)) /* This assignment is intended */
92
/* Buffer leftovers */
93
memcpy(ctx->block, data, length);
63
MD_UPDATE(ctx, length, data, COMPRESS, MD_INCR(ctx));
105
73
assert(length <= MD5_DIGEST_SIZE);
110
leftover = length % 4;
112
/* Little endian order */
113
for (i = 0; i < words; i++, digest += 4)
114
LE_WRITE_UINT32(digest, ctx->digest[i]);
121
assert(i < _MD5_DIGEST_LENGTH);
123
/* Still least significant byte first. */
124
for (word = ctx->digest[i], j = 0; j < leftover;
126
digest[j] = word & 0xff;
75
MD_PAD(ctx, 8, COMPRESS);
77
/* There are 512 = 2^9 bits in one block */
78
high = (ctx->count_high << 9) | (ctx->count_low >> 23);
79
low = (ctx->count_low << 9) | (ctx->index << 3);
81
LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 8), low);
82
LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 4), high);
83
_nettle_md5_compress(ctx->state, ctx->block);
85
_nettle_write_le32(length, digest, ctx->state);
131
/* Final wrapup - pad to MD5_DATA_SIZE-byte boundary with the bit
132
* pattern 1 0* (64-bit count of bits processed, LSB-first) */
135
md5_final(struct md5_ctx *ctx)
137
uint32_t bitcount_high;
138
uint32_t bitcount_low;
143
/* Set the first char of padding to 0x80. This is safe since there
144
* is always at least one byte free */
145
assert(i < MD5_DATA_SIZE);
146
ctx->block[i++] = 0x80;
148
if (i > (MD5_DATA_SIZE - 8))
150
/* No room for length in this block. Process it and
151
pad with another one */
152
memset(ctx->block + i, 0, MD5_DATA_SIZE - i);
154
_nettle_md5_compress(ctx->digest, ctx->block);
157
if (i < (MD5_DATA_SIZE - 8))
158
memset(ctx->block + i, 0, (MD5_DATA_SIZE - 8) - i);
160
/* There are 512 = 2^9 bits in one block
161
* Little-endian order => Least significant word first */
163
bitcount_low = (ctx->count_l << 9) | (ctx->index << 3);
164
bitcount_high = (ctx->count_h << 9) | (ctx->count_l >> 23);
165
LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 8), bitcount_low);
166
LE_WRITE_UINT32(ctx->block + (MD5_DATA_SIZE - 4), bitcount_high);
168
_nettle_md5_compress(ctx->digest, ctx->block);