| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
#include <stdio.h> |
| 33 |
#include "common.h" |
| 34 |
#include "endianess.h" |
| 35 |
|
| 36 |
FAKE_INIT(MD5) |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#define F(x, y, z) (((x) & (y)) | ((~x) & (z))) |
| 43 |
#define G(x, y, z) (((x) & (z)) | ((y) & (~z))) |
| 44 |
#define H(x, y, z) ((x) ^ (y) ^ (z)) |
| 45 |
#define I(x, y, z) ((y) ^ ((x) | (~z))) |
| 46 |
|
| 47 |
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) |
| 48 |
|
| 49 |
#define FF(a, b, c, d, x, s, ac) { \ |
| 50 |
(a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \ |
| 51 |
(a) = ROTATE_LEFT ((a), (s)); \ |
| 52 |
(a) += (b); \ |
| 53 |
} |
| 54 |
|
| 55 |
#define GG(a, b, c, d, x, s, ac) { \ |
| 56 |
(a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \ |
| 57 |
(a) = ROTATE_LEFT ((a), (s)); \ |
| 58 |
(a) += (b); \ |
| 59 |
} |
| 60 |
|
| 61 |
#define HH(a, b, c, d, x, s, ac) { \ |
| 62 |
(a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \ |
| 63 |
(a) = ROTATE_LEFT ((a), (s)); \ |
| 64 |
(a) += (b); \ |
| 65 |
} |
| 66 |
|
| 67 |
#define II(a, b, c, d, x, s, ac) { \ |
| 68 |
(a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \ |
| 69 |
(a) = ROTATE_LEFT ((a), (s)); \ |
| 70 |
(a) += (b); \ |
| 71 |
} |
| 72 |
|
| 73 |
#define S11 7 |
| 74 |
#define S12 12 |
| 75 |
#define S13 17 |
| 76 |
#define S14 22 |
| 77 |
#define S21 5 |
| 78 |
#define S22 9 |
| 79 |
#define S23 14 |
| 80 |
#define S24 20 |
| 81 |
#define S31 4 |
| 82 |
#define S32 11 |
| 83 |
#define S33 16 |
| 84 |
#define S34 23 |
| 85 |
#define S41 6 |
| 86 |
#define S42 10 |
| 87 |
#define S43 15 |
| 88 |
#define S44 21 |
| 89 |
|
| 90 |
#define BLOCK_SIZE 64 |
| 91 |
|
| 92 |
#define DIGEST_SIZE (128/8) |
| 93 |
|
| 94 |
typedef struct t_hash_state { |
| 95 |
uint32_t h[4]; |
| 96 |
uint8_t buf[BLOCK_SIZE]; |
| 97 |
unsigned curlen; |
| 98 |
uint64_t totbits; |
| 99 |
} hash_state; |
| 100 |
|
| 101 |
static int add_bits(hash_state *hs, unsigned bits) |
| 102 |
{ |
| 103 |
|
| 104 |
hs->totbits += bits; |
| 105 |
return (hs->totbits < bits) ? ERR_MAX_DATA : 0; |
| 106 |
} |
| 107 |
|
| 108 |
static void md5_compress(hash_state * hs) |
| 109 |
{ |
| 110 |
uint32_t a, b, c, d; |
| 111 |
uint32_t x[16]; |
| 112 |
int i; |
| 113 |
|
| 114 |
|
| 115 |
for (i=0; i<16; i++) { |
| 116 |
x[i] = LOAD_U32_LITTLE(&hs->buf[i*4]); |
| 117 |
} |
| 118 |
|
| 119 |
a = hs->h[0]; |
| 120 |
b = hs->h[1]; |
| 121 |
c = hs->h[2]; |
| 122 |
d = hs->h[3]; |
| 123 |
|
| 124 |
|
| 125 |
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); |
| 126 |
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); |
| 127 |
FF (c, d, a, b, x[ 2], S13, 0x242070db); |
| 128 |
FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); |
| 129 |
FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); |
| 130 |
FF (d, a, b, c, x[ 5], S12, 0x4787c62a); |
| 131 |
FF (c, d, a, b, x[ 6], S13, 0xa8304613); |
| 132 |
FF (b, c, d, a, x[ 7], S14, 0xfd469501); |
| 133 |
FF (a, b, c, d, x[ 8], S11, 0x698098d8); |
| 134 |
FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); |
| 135 |
FF (c, d, a, b, x[10], S13, 0xffff5bb1); |
| 136 |
FF (b, c, d, a, x[11], S14, 0x895cd7be); |
| 137 |
FF (a, b, c, d, x[12], S11, 0x6b901122); |
| 138 |
FF (d, a, b, c, x[13], S12, 0xfd987193); |
| 139 |
FF (c, d, a, b, x[14], S13, 0xa679438e); |
| 140 |
FF (b, c, d, a, x[15], S14, 0x49b40821); |
| 141 |
|
| 142 |
|
| 143 |
GG (a, b, c, d, x[ 1], S21, 0xf61e2562); |
| 144 |
GG (d, a, b, c, x[ 6], S22, 0xc040b340); |
| 145 |
GG (c, d, a, b, x[11], S23, 0x265e5a51); |
| 146 |
GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); |
| 147 |
GG (a, b, c, d, x[ 5], S21, 0xd62f105d); |
| 148 |
GG (d, a, b, c, x[10], S22, 0x2441453); |
| 149 |
GG (c, d, a, b, x[15], S23, 0xd8a1e681); |
| 150 |
GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); |
| 151 |
GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); |
| 152 |
GG (d, a, b, c, x[14], S22, 0xc33707d6); |
| 153 |
GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); |
| 154 |
GG (b, c, d, a, x[ 8], S24, 0x455a14ed); |
| 155 |
GG (a, b, c, d, x[13], S21, 0xa9e3e905); |
| 156 |
GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); |
| 157 |
GG (c, d, a, b, x[ 7], S23, 0x676f02d9); |
| 158 |
GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); |
| 159 |
|
| 160 |
|
| 161 |
HH (a, b, c, d, x[ 5], S31, 0xfffa3942); |
| 162 |
HH (d, a, b, c, x[ 8], S32, 0x8771f681); |
| 163 |
HH (c, d, a, b, x[11], S33, 0x6d9d6122); |
| 164 |
HH (b, c, d, a, x[14], S34, 0xfde5380c); |
| 165 |
HH (a, b, c, d, x[ 1], S31, 0xa4beea44); |
| 166 |
HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); |
| 167 |
HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); |
| 168 |
HH (b, c, d, a, x[10], S34, 0xbebfbc70); |
| 169 |
HH (a, b, c, d, x[13], S31, 0x289b7ec6); |
| 170 |
HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); |
| 171 |
HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); |
| 172 |
HH (b, c, d, a, x[ 6], S34, 0x4881d05); |
| 173 |
HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); |
| 174 |
HH (d, a, b, c, x[12], S32, 0xe6db99e5); |
| 175 |
HH (c, d, a, b, x[15], S33, 0x1fa27cf8); |
| 176 |
HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); |
| 177 |
|
| 178 |
|
| 179 |
II (a, b, c, d, x[ 0], S41, 0xf4292244); |
| 180 |
II (d, a, b, c, x[ 7], S42, 0x432aff97); |
| 181 |
II (c, d, a, b, x[14], S43, 0xab9423a7); |
| 182 |
II (b, c, d, a, x[ 5], S44, 0xfc93a039); |
| 183 |
II (a, b, c, d, x[12], S41, 0x655b59c3); |
| 184 |
II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); |
| 185 |
II (c, d, a, b, x[10], S43, 0xffeff47d); |
| 186 |
II (b, c, d, a, x[ 1], S44, 0x85845dd1); |
| 187 |
II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); |
| 188 |
II (d, a, b, c, x[15], S42, 0xfe2ce6e0); |
| 189 |
II (c, d, a, b, x[ 6], S43, 0xa3014314); |
| 190 |
II (b, c, d, a, x[13], S44, 0x4e0811a1); |
| 191 |
II (a, b, c, d, x[ 4], S41, 0xf7537e82); |
| 192 |
II (d, a, b, c, x[11], S42, 0xbd3af235); |
| 193 |
II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); |
| 194 |
II (b, c, d, a, x[ 9], S44, 0xeb86d391); |
| 195 |
|
| 196 |
|
| 197 |
hs->h[0] += a; |
| 198 |
hs->h[1] += b; |
| 199 |
hs->h[2] += c; |
| 200 |
hs->h[3] += d; |
| 201 |
} |
| 202 |
|
| 203 |
EXPORT_SYM int MD5_init(hash_state **mdState) |
| 204 |
{ |
| 205 |
hash_state *hs; |
| 206 |
|
| 207 |
if (NULL == mdState) { |
| 208 |
return ERR_NULL; |
| 209 |
} |
| 210 |
|
| 211 |
*mdState = hs = (hash_state*) calloc(1, sizeof(hash_state)); |
| 212 |
if (NULL == hs) |
| 213 |
return ERR_MEMORY; |
| 214 |
|
| 215 |
hs->curlen = 0; |
| 216 |
hs->totbits = 0; |
| 217 |
|
| 218 |
|
| 219 |
hs->h[0] = 0x67452301; |
| 220 |
hs->h[1] = 0xefcdab89; |
| 221 |
hs->h[2] = 0x98badcfe; |
| 222 |
hs->h[3] = 0x10325476; |
| 223 |
|
| 224 |
return 0; |
| 225 |
} |
| 226 |
|
| 227 |
EXPORT_SYM int MD5_destroy (hash_state *mdState) |
| 228 |
{ |
| 229 |
free(mdState); |
| 230 |
return 0; |
| 231 |
} |
| 232 |
|
| 233 |
EXPORT_SYM int MD5_update(hash_state *hs, const uint8_t *buf, size_t len) |
| 234 |
{ |
| 235 |
if (NULL == hs || NULL == buf) { |
| 236 |
return ERR_NULL; |
| 237 |
} |
| 238 |
|
| 239 |
assert(hs->curlen < BLOCK_SIZE); |
| 240 |
|
| 241 |
while (len>0) { |
| 242 |
unsigned left, btc; |
| 243 |
|
| 244 |
left = BLOCK_SIZE - hs->curlen; |
| 245 |
btc = (unsigned)MIN(left, len); |
| 246 |
memcpy(&hs->buf[hs->curlen], buf, btc); |
| 247 |
buf += btc; |
| 248 |
hs->curlen += btc; |
| 249 |
len -= btc; |
| 250 |
|
| 251 |
if (hs->curlen == BLOCK_SIZE) { |
| 252 |
md5_compress(hs); |
| 253 |
hs->curlen = 0; |
| 254 |
if (add_bits(hs, BLOCK_SIZE*8)) { |
| 255 |
return ERR_MAX_DATA; |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
return 0; |
| 261 |
} |
| 262 |
|
| 263 |
static int md5_finalize(hash_state *hs, uint8_t *hash ) |
| 264 |
{ |
| 265 |
unsigned left, i; |
| 266 |
|
| 267 |
assert(hs->curlen < BLOCK_SIZE); |
| 268 |
|
| 269 |
|
| 270 |
if (add_bits(hs, hs->curlen*8)) { |
| 271 |
return ERR_MAX_DATA; |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
hs->buf[hs->curlen++] = 0x80; |
| 277 |
|
| 278 |
|
| 279 |
left = BLOCK_SIZE - hs->curlen; |
| 280 |
if (left < 8) { |
| 281 |
memset(&hs->buf[hs->curlen], 0, left); |
| 282 |
md5_compress(hs); |
| 283 |
hs->curlen = 0; |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
left = BLOCK_SIZE - hs->curlen; |
| 291 |
memset(&hs->buf[hs->curlen], 0, left); |
| 292 |
STORE_U64_LITTLE(&hs->buf[BLOCK_SIZE-8], hs->totbits); |
| 293 |
|
| 294 |
|
| 295 |
md5_compress(hs); |
| 296 |
|
| 297 |
|
| 298 |
for (i=0; i<4; i++) { |
| 299 |
STORE_U32_LITTLE(&hash[i*4], hs->h[i]); |
| 300 |
} |
| 301 |
|
| 302 |
return 0; |
| 303 |
} |
| 304 |
|
| 305 |
EXPORT_SYM int MD5_digest(const hash_state *mdState, uint8_t digest[DIGEST_SIZE]) |
| 306 |
{ |
| 307 |
hash_state temp; |
| 308 |
|
| 309 |
if (NULL == mdState) { |
| 310 |
return ERR_NULL; |
| 311 |
} |
| 312 |
|
| 313 |
temp = *mdState; |
| 314 |
md5_finalize(&temp, digest); |
| 315 |
return 0; |
| 316 |
} |
| 317 |
|
| 318 |
EXPORT_SYM int MD5_copy(const hash_state *src, hash_state *dst) |
| 319 |
{ |
| 320 |
if (NULL == src || NULL == dst) { |
| 321 |
return ERR_NULL; |
| 322 |
} |
| 323 |
|
| 324 |
*dst = *src; |
| 325 |
return 0; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
EXPORT_SYM int MD5_pbkdf2_hmac_assist(const hash_state *inner, const hash_state *outer, |
| 340 |
const uint8_t first_hmac[DIGEST_SIZE], |
| 341 |
uint8_t result[DIGEST_SIZE], |
| 342 |
size_t iterations) |
| 343 |
{ |
| 344 |
hash_state inner_temp, outer_temp; |
| 345 |
size_t i; |
| 346 |
uint8_t last_hmac[DIGEST_SIZE]; |
| 347 |
|
| 348 |
if (NULL == inner || NULL == outer || NULL == first_hmac || NULL == result) { |
| 349 |
return ERR_NULL; |
| 350 |
} |
| 351 |
|
| 352 |
if (iterations == 0) { |
| 353 |
return ERR_NR_ROUNDS; |
| 354 |
} |
| 355 |
|
| 356 |
memcpy(result, first_hmac, DIGEST_SIZE); |
| 357 |
memcpy(last_hmac, first_hmac, DIGEST_SIZE); |
| 358 |
|
| 359 |
for (i=1; i<iterations; i++) { |
| 360 |
int j; |
| 361 |
|
| 362 |
inner_temp = *inner; |
| 363 |
outer_temp = *outer; |
| 364 |
|
| 365 |
MD5_update(&inner_temp, last_hmac, DIGEST_SIZE); |
| 366 |
md5_finalize(&inner_temp, last_hmac); |
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
MD5_update(&outer_temp, last_hmac, DIGEST_SIZE); |
| 371 |
md5_finalize(&outer_temp, last_hmac); |
| 372 |
|
| 373 |
for (j=0; j<DIGEST_SIZE; j++) { |
| 374 |
result[j] ^= last_hmac[j]; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
return 0; |
| 379 |
} |
| 380 |
|
| 381 |
#ifdef MAIN |
| 382 |
int main(void) |
| 383 |
{ |
| 384 |
hash_state *hs; |
| 385 |
const uint8_t tv[] = "The quick brown fox jumps over the lazy dog"; |
| 386 |
uint8_t result[DIGEST_SIZE]; |
| 387 |
int i; |
| 388 |
|
| 389 |
MD5_init(&hs); |
| 390 |
MD5_update(hs, tv, sizeof tv - 1); |
| 391 |
MD5_digest(hs, result); |
| 392 |
MD5_destroy(hs); |
| 393 |
|
| 394 |
for (i=0; i<sizeof result; i++) { |
| 395 |
printf("%02X", result[i]); |
| 396 |
} |
| 397 |
printf("\n"); |
| 398 |
|
| 399 |
MD5_init(&hs); |
| 400 |
MD5_digest(hs, result); |
| 401 |
MD5_destroy(hs); |
| 402 |
|
| 403 |
for (i=0; i<sizeof result; i++) { |
| 404 |
printf("%02X", result[i]); |
| 405 |
} |
| 406 |
printf("\n"); |
| 407 |
|
| 408 |
MD5_init(&hs); |
| 409 |
for (i=0; i<10000000; i++) { |
| 410 |
MD5_update(hs, tv, sizeof tv - 1); |
| 411 |
} |
| 412 |
MD5_destroy(hs); |
| 413 |
|
| 414 |
printf("\n"); |
| 415 |
} |
| 416 |
#endif |