8 #include "heivs/hash_md5.h"
36 static void MD5Transform(uint32_t [4], uint8_t [64]);
37 static void Encode(uint8_t *, uint32_t *, uint32_t);
38 static void Decode(uint32_t *, uint8_t *, uint32_t);
40 static const uint8_t PADDING[64] = {
41 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
42 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
43 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
48 #define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
49 #define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
50 #define H(x, y, z) ((x) ^ (y) ^ (z))
51 #define I(x, y, z) ((y) ^ ((x) | (~z)))
55 #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
60 #define FF(a, b, c, d, x, s, ac) { \
61 (a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \
62 (a) = ROTATE_LEFT ((a), (s)); \
65 #define GG(a, b, c, d, x, s, ac) { \
66 (a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \
67 (a) = ROTATE_LEFT ((a), (s)); \
70 #define HH(a, b, c, d, x, s, ac) { \
71 (a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \
72 (a) = ROTATE_LEFT ((a), (s)); \
75 #define II(a, b, c, d, x, s, ac) { \
76 (a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \
77 (a) = ROTATE_LEFT ((a), (s)); \
81 static status_e init(
struct heivs_hash_t *hash,
void *_state,
size_t state_size)
83 struct MD5_CTX *context = (
struct MD5_CTX *)_state;
87 context->count[0] = context->count[1] = 0;
90 context->state[0] = 0x67452301;
91 context->state[1] = 0xefcdab89;
92 context->state[2] = 0x98badcfe;
93 context->state[3] = 0x10325476;
102 void MD5Update (context, input, inputLen)
103 struct MD5_CTX *context;
107 uint32_t i, index, partLen;
110 index = (uint32_t)((context->count[0] >> 3) & 0x3F);
113 if ((context->count[0] += ((uint32_t)inputLen << 3))
114 < ((uint32_t)inputLen << 3))
116 context->count[1] += ((uint32_t)inputLen >> 29);
118 partLen = 64 - index;
122 if (inputLen >= partLen) {
124 ((
void *)&context->buffer[index], (
void *)input, partLen);
125 MD5Transform (context->state, context->buffer);
127 for (i = partLen; i + 63 < inputLen; i += 64)
128 MD5Transform (context->state, &input[i]);
137 ((
void *)&context->buffer[index], (
void *)&input[i],
142 static status_e run(
struct heivs_hash_t *hash,
void *_state,
const void *_data,
size_t size)
144 struct MD5_CTX *context = (
struct MD5_CTX *)_state;
147 MD5Update(context, (uint8_t*)_data, size);
152 static status_e finish(
struct heivs_hash_t *hash,
void *_state)
154 struct MD5_CTX *context = (
struct MD5_CTX *)_state;
157 uint32_t index, padLen;
158 uint8_t digest[128/8];
162 Encode (bits, context->count, 8);
166 index = (uint32_t)((context->count[0] >> 3) & 0x3f);
167 padLen = (index < 56) ? (56 - index) : (120 - index);
168 MD5Update(context, PADDING, padLen);
171 MD5Update(context, bits, 8);
174 Encode(digest, context->state, 16);
178 memset((
void *)context, 0,
sizeof (*context));
180 memcpy(context, digest,
sizeof(digest));
185 static void MD5Transform (uint32_t state[4], uint8_t block[64])
187 uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
189 Decode (x, block, 64);
192 FF (a, b, c, d, x[ 0], S11, 0xd76aa478);
193 FF (d, a, b, c, x[ 1], S12, 0xe8c7b756);
194 FF (c, d, a, b, x[ 2], S13, 0x242070db);
195 FF (b, c, d, a, x[ 3], S14, 0xc1bdceee);
196 FF (a, b, c, d, x[ 4], S11, 0xf57c0faf);
197 FF (d, a, b, c, x[ 5], S12, 0x4787c62a);
198 FF (c, d, a, b, x[ 6], S13, 0xa8304613);
199 FF (b, c, d, a, x[ 7], S14, 0xfd469501);
200 FF (a, b, c, d, x[ 8], S11, 0x698098d8);
201 FF (d, a, b, c, x[ 9], S12, 0x8b44f7af);
202 FF (c, d, a, b, x[10], S13, 0xffff5bb1);
203 FF (b, c, d, a, x[11], S14, 0x895cd7be);
204 FF (a, b, c, d, x[12], S11, 0x6b901122);
205 FF (d, a, b, c, x[13], S12, 0xfd987193);
206 FF (c, d, a, b, x[14], S13, 0xa679438e);
207 FF (b, c, d, a, x[15], S14, 0x49b40821);
210 GG (a, b, c, d, x[ 1], S21, 0xf61e2562);
211 GG (d, a, b, c, x[ 6], S22, 0xc040b340);
212 GG (c, d, a, b, x[11], S23, 0x265e5a51);
213 GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa);
214 GG (a, b, c, d, x[ 5], S21, 0xd62f105d);
215 GG (d, a, b, c, x[10], S22, 0x2441453);
216 GG (c, d, a, b, x[15], S23, 0xd8a1e681);
217 GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8);
218 GG (a, b, c, d, x[ 9], S21, 0x21e1cde6);
219 GG (d, a, b, c, x[14], S22, 0xc33707d6);
220 GG (c, d, a, b, x[ 3], S23, 0xf4d50d87);
221 GG (b, c, d, a, x[ 8], S24, 0x455a14ed);
222 GG (a, b, c, d, x[13], S21, 0xa9e3e905);
223 GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8);
224 GG (c, d, a, b, x[ 7], S23, 0x676f02d9);
225 GG (b, c, d, a, x[12], S24, 0x8d2a4c8a);
228 HH (a, b, c, d, x[ 5], S31, 0xfffa3942);
229 HH (d, a, b, c, x[ 8], S32, 0x8771f681);
230 HH (c, d, a, b, x[11], S33, 0x6d9d6122);
231 HH (b, c, d, a, x[14], S34, 0xfde5380c);
232 HH (a, b, c, d, x[ 1], S31, 0xa4beea44);
233 HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9);
234 HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60);
235 HH (b, c, d, a, x[10], S34, 0xbebfbc70);
236 HH (a, b, c, d, x[13], S31, 0x289b7ec6);
237 HH (d, a, b, c, x[ 0], S32, 0xeaa127fa);
238 HH (c, d, a, b, x[ 3], S33, 0xd4ef3085);
239 HH (b, c, d, a, x[ 6], S34, 0x4881d05);
240 HH (a, b, c, d, x[ 9], S31, 0xd9d4d039);
241 HH (d, a, b, c, x[12], S32, 0xe6db99e5);
242 HH (c, d, a, b, x[15], S33, 0x1fa27cf8);
243 HH (b, c, d, a, x[ 2], S34, 0xc4ac5665);
246 II (a, b, c, d, x[ 0], S41, 0xf4292244);
247 II (d, a, b, c, x[ 7], S42, 0x432aff97);
248 II (c, d, a, b, x[14], S43, 0xab9423a7);
249 II (b, c, d, a, x[ 5], S44, 0xfc93a039);
250 II (a, b, c, d, x[12], S41, 0x655b59c3);
251 II (d, a, b, c, x[ 3], S42, 0x8f0ccc92);
252 II (c, d, a, b, x[10], S43, 0xffeff47d);
253 II (b, c, d, a, x[ 1], S44, 0x85845dd1);
254 II (a, b, c, d, x[ 8], S41, 0x6fa87e4f);
255 II (d, a, b, c, x[15], S42, 0xfe2ce6e0);
256 II (c, d, a, b, x[ 6], S43, 0xa3014314);
257 II (b, c, d, a, x[13], S44, 0x4e0811a1);
258 II (a, b, c, d, x[ 4], S41, 0xf7537e82);
259 II (d, a, b, c, x[11], S42, 0xbd3af235);
260 II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb);
261 II (b, c, d, a, x[ 9], S44, 0xeb86d391);
272 static void Encode (output, input, len)
279 for (i = 0, j = 0; j < len; i++, j += 4) {
280 output[j] = (uint8_t)(input[i] & 0xff);
281 output[j+1] = (uint8_t)((input[i] >> 8) & 0xff);
282 output[j+2] = (uint8_t)((input[i] >> 16) & 0xff);
283 output[j+3] = (uint8_t)((input[i] >> 24) & 0xff);
290 static void Decode (output, input, len)
297 for (i = 0, j = 0; j < len; i++, j += 4)
298 output[i] = ((uint32_t)input[j]) | (((uint32_t)input[j+1]) << 8) |
299 (((uint32_t)input[j+2]) << 16) | (((uint32_t)input[j+3]) << 24);
302 struct heivs_hash_t hash_md5 =
309 .state_size =
sizeof(
struct MD5_CTX),
void * memcpy(void *dest, const void *src, size_t n)
void * memset(void *dest, int n, size_t n)