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 | |
Jens Axboe | fd00954 | 2007-07-29 20:59:01 +0200 | [diff] [blame] | 12 | a = hash[0]; |
| 13 | b = hash[1]; |
| 14 | c = hash[2]; |
| 15 | d = hash[3]; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 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 | |
Jens Axboe | fd00954 | 2007-07-29 20:59:01 +0200 | [diff] [blame] | 85 | hash[0] += a; |
| 86 | hash[1] += b; |
| 87 | hash[2] += c; |
| 88 | hash[3] += d; |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Jens Axboe | 61f821f | 2007-07-30 10:18:06 +0200 | [diff] [blame] | 91 | void md5_init(struct md5_ctx *mctx) |
| 92 | { |
| 93 | mctx->hash[0] = 0x67452301; |
| 94 | mctx->hash[1] = 0xefcdab89; |
| 95 | mctx->hash[2] = 0x98badcfe; |
| 96 | mctx->hash[3] = 0x10325476; |
| 97 | } |
| 98 | |
Jens Axboe | ebac465 | 2005-12-08 15:25:21 +0100 | [diff] [blame] | 99 | void md5_update(struct md5_ctx *mctx, const uint8_t *data, unsigned int len) |
| 100 | { |
| 101 | const uint32_t avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f); |
| 102 | |
| 103 | mctx->byte_count += len; |
| 104 | |
| 105 | if (avail > len) { |
| 106 | memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), |
| 107 | data, len); |
| 108 | return; |
| 109 | } |
| 110 | |
| 111 | memcpy((char *)mctx->block + (sizeof(mctx->block) - avail), |
| 112 | data, avail); |
| 113 | |
| 114 | md5_transform(mctx->hash, mctx->block); |
| 115 | data += avail; |
| 116 | len -= avail; |
| 117 | |
| 118 | while (len >= sizeof(mctx->block)) { |
| 119 | memcpy(mctx->block, data, sizeof(mctx->block)); |
| 120 | md5_transform(mctx->hash, mctx->block); |
| 121 | data += sizeof(mctx->block); |
| 122 | len -= sizeof(mctx->block); |
| 123 | } |
| 124 | |
| 125 | memcpy(mctx->block, data, len); |
| 126 | } |