Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Shamelessly lifted from the 2.6 kernel (crypto/md5.c) |
| 3 | */ |
| 4 | #include <string.h> |
| 5 | #include <stdint.h> |
| 6 | #include "md5.h" |
| 7 | |
| 8 | static void md5_transform(uint32_t *hash, uint32_t const *in) |
| 9 | { |
| 10 | uint32_t a, b, c, d; |
| 11 | |
| 12 | a = hash[0]; |
| 13 | b = hash[1]; |
| 14 | c = hash[2]; |
| 15 | d = hash[3]; |
| 16 | |
| 17 | MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7); |
| 18 | MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12); |
| 19 | MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17); |
| 20 | MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22); |
| 21 | MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7); |
| 22 | MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12); |
| 23 | MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17); |
| 24 | MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22); |
| 25 | MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7); |
| 26 | MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12); |
| 27 | MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17); |
| 28 | MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22); |
| 29 | MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7); |
| 30 | MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12); |
| 31 | MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17); |
| 32 | MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22); |
| 33 | |
| 34 | MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5); |
| 35 | MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9); |
| 36 | MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14); |
| 37 | MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20); |
| 38 | MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5); |
| 39 | MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9); |
| 40 | MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14); |
| 41 | MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20); |
| 42 | MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5); |
| 43 | MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9); |
| 44 | MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14); |
| 45 | MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20); |
| 46 | MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5); |
| 47 | MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9); |
| 48 | MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14); |
| 49 | MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20); |
| 50 | |
| 51 | MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4); |
| 52 | MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11); |
| 53 | MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16); |
| 54 | MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23); |
| 55 | MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4); |
| 56 | MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11); |
| 57 | MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16); |
| 58 | MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23); |
| 59 | MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4); |
| 60 | MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11); |
| 61 | MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16); |
| 62 | MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23); |
| 63 | MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4); |
| 64 | MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11); |
| 65 | MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16); |
| 66 | MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23); |
| 67 | |
| 68 | MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6); |
| 69 | MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10); |
| 70 | MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15); |
| 71 | MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21); |
| 72 | MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6); |
| 73 | MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10); |
| 74 | MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15); |
| 75 | MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21); |
| 76 | MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6); |
| 77 | MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10); |
| 78 | MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15); |
| 79 | MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21); |
| 80 | MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6); |
| 81 | MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10); |
| 82 | MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15); |
| 83 | MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21); |
| 84 | |
| 85 | hash[0] += a; |
| 86 | hash[1] += b; |
| 87 | hash[2] += c; |
| 88 | hash[3] += d; |
| 89 | } |
| 90 | |
| 91 | void md5_update(struct md5_ctx *mctx, const uint8_t *data, unsigned int len) |
| 92 | { |
| 93 | const uint32_t avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f); |
| 94 | |
| 95 | mctx->byte_count += len; |
| 96 | |
| 97 | if (avail > len) { |
| 98 | memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), |
| 99 | data, len); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), |
| 104 | data, avail); |
| 105 | |
| 106 | md5_transform(mctx->hash, mctx->block); |
| 107 | data += avail; |
| 108 | len -= avail; |
| 109 | |
| 110 | while (len >= sizeof(mctx->block)) { |
| 111 | memcpy(mctx->block, data, sizeof(mctx->block)); |
| 112 | md5_transform(mctx->hash, mctx->block); |
| 113 | data += sizeof(mctx->block); |
| 114 | len -= sizeof(mctx->block); |
| 115 | } |
| 116 | |
| 117 | memcpy(mctx->block, data, len); |
| 118 | } |