| 1 |
|
| /* |
| 2 |
|
| * md5.c - MD5 implementation per RFC 1321. |
| 3 |
|
| * |
| 4 |
|
| * Plain C99, no external deps. The 64 steps are written out with their |
| 5 |
|
| * constants and shifts literal, as in the RFC's reference; blocks load |
| 6 |
|
| * with one copy on little-endian hosts and byte by byte elsewhere. |
| 7 |
|
| */ |
| 8 |
|
| |
| 9 |
|
| #include "md5.h" |
| 10 |
|
| #include <string.h> |
| 11 |
|
| |
| 12 |
|
| #define ROL32(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) |
| 13 |
|
| |
| 14 |
|
| static uint32_t |
| 15 |
0 |
| load32_le(const unsigned char *p) |
| 16 |
|
| { |
| 17 |
0 |
| return (uint32_t)p[0] |
| 18 |
0 |
| | ((uint32_t)p[1] << 8) |
| 19 |
0 |
| | ((uint32_t)p[2] << 16) |
| 20 |
0 |
| | ((uint32_t)p[3] << 24); |
| 21 |
|
| } |
| 22 |
|
| |
| 23 |
|
| static void |
| 24 |
224 |
| store32_le(unsigned char *p, uint32_t v) |
| 25 |
|
| { |
| 26 |
224 |
| p[0] = (unsigned char)( v & 0xFFu); |
| 27 |
224 |
| p[1] = (unsigned char)((v >> 8) & 0xFFu); |
| 28 |
224 |
| p[2] = (unsigned char)((v >> 16) & 0xFFu); |
| 29 |
224 |
| p[3] = (unsigned char)((v >> 24) & 0xFFu); |
| 30 |
224 |
| } |
| 31 |
|
| |
| 32 |
|
| /* The four round functions, in the forms that need fewest operations: F and |
| 33 |
|
| * G as selects, H and I as written in RFC 1321. */ |
| 34 |
|
| #define MD5_F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) |
| 35 |
|
| #define MD5_G(x, y, z) ((y) ^ ((z) & ((x) ^ (y)))) |
| 36 |
|
| #define MD5_H(x, y, z) ((x) ^ (y) ^ (z)) |
| 37 |
|
| #define MD5_I(x, y, z) ((y) ^ ((x) | ~(z))) |
| 38 |
|
| |
| 39 |
|
| /* One step: the 64 are written out, so every constant, shift and message |
| 40 |
|
| * index is literal and the four words rotate by name, not by copying. */ |
| 41 |
|
| #define MD5_STEP(f, a, b, c, d, x, k, s) do { \ |
| 42 |
|
| (a) += (x) + (uint32_t)(k); \ |
| 43 |
|
| (a) += f((b), (c), (d)); \ |
| 44 |
|
| (a) = ROL32((a), (s)) + (b); \ |
| 45 |
|
| } while (0) |
| 46 |
|
| |
| 47 |
|
| /* G's two halves share no bit, so they are added, not ORed: the CPU can |
| 48 |
|
| * start on (~d & c) before b is known. */ |
| 49 |
|
| #define MD5_STEP_G(a, b, c, d, x, k, s) do { \ |
| 50 |
|
| (a) += (x) + (uint32_t)(k) + (~(d) & (c)); \ |
| 51 |
|
| (a) += (d) & (b); \ |
| 52 |
|
| (a) = ROL32((a), (s)) + (b); \ |
| 53 |
|
| } while (0) |
| 54 |
|
| |
| 55 |
|
| static void |
| 56 |
88553 |
| md5_compress(md5_ctx_t *ctx, const unsigned char *block) |
| 57 |
|
| { |
| 58 |
|
| uint32_t M[16]; |
| 59 |
88553 |
| uint32_t a = ctx->state[0], b = ctx->state[1], |
| 60 |
88553 |
| c = ctx->state[2], d = ctx->state[3]; |
| 61 |
|
| |
| 62 |
|
| #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) \ |
| 63 |
|
| && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 64 |
88553 |
| memcpy(M, block, sizeof M); |
| 65 |
|
| #else |
| 66 |
|
| { |
| 67 |
|
| int i; |
| 68 |
|
| for (i = 0; i < 16; i++) M[i] = load32_le(block + i * 4); |
| 69 |
|
| } |
| 70 |
|
| #endif |
| 71 |
|
| |
| 72 |
88553 |
| MD5_STEP(MD5_F, a, b, c, d, M[ 0], 0xd76aa478, 7); |
| 73 |
88553 |
| MD5_STEP(MD5_F, d, a, b, c, M[ 1], 0xe8c7b756, 12); |
| 74 |
88553 |
| MD5_STEP(MD5_F, c, d, a, b, M[ 2], 0x242070db, 17); |
| 75 |
88553 |
| MD5_STEP(MD5_F, b, c, d, a, M[ 3], 0xc1bdceee, 22); |
| 76 |
88553 |
| MD5_STEP(MD5_F, a, b, c, d, M[ 4], 0xf57c0faf, 7); |
| 77 |
88553 |
| MD5_STEP(MD5_F, d, a, b, c, M[ 5], 0x4787c62a, 12); |
| 78 |
88553 |
| MD5_STEP(MD5_F, c, d, a, b, M[ 6], 0xa8304613, 17); |
| 79 |
88553 |
| MD5_STEP(MD5_F, b, c, d, a, M[ 7], 0xfd469501, 22); |
| 80 |
88553 |
| MD5_STEP(MD5_F, a, b, c, d, M[ 8], 0x698098d8, 7); |
| 81 |
88553 |
| MD5_STEP(MD5_F, d, a, b, c, M[ 9], 0x8b44f7af, 12); |
| 82 |
88553 |
| MD5_STEP(MD5_F, c, d, a, b, M[10], 0xffff5bb1, 17); |
| 83 |
88553 |
| MD5_STEP(MD5_F, b, c, d, a, M[11], 0x895cd7be, 22); |
| 84 |
88553 |
| MD5_STEP(MD5_F, a, b, c, d, M[12], 0x6b901122, 7); |
| 85 |
88553 |
| MD5_STEP(MD5_F, d, a, b, c, M[13], 0xfd987193, 12); |
| 86 |
88553 |
| MD5_STEP(MD5_F, c, d, a, b, M[14], 0xa679438e, 17); |
| 87 |
88553 |
| MD5_STEP(MD5_F, b, c, d, a, M[15], 0x49b40821, 22); |
| 88 |
|
| |
| 89 |
88553 |
| MD5_STEP_G(a, b, c, d, M[ 1], 0xf61e2562, 5); |
| 90 |
88553 |
| MD5_STEP_G(d, a, b, c, M[ 6], 0xc040b340, 9); |
| 91 |
88553 |
| MD5_STEP_G(c, d, a, b, M[11], 0x265e5a51, 14); |
| 92 |
88553 |
| MD5_STEP_G(b, c, d, a, M[ 0], 0xe9b6c7aa, 20); |
| 93 |
88553 |
| MD5_STEP_G(a, b, c, d, M[ 5], 0xd62f105d, 5); |
| 94 |
88553 |
| MD5_STEP_G(d, a, b, c, M[10], 0x02441453, 9); |
| 95 |
88553 |
| MD5_STEP_G(c, d, a, b, M[15], 0xd8a1e681, 14); |
| 96 |
88553 |
| MD5_STEP_G(b, c, d, a, M[ 4], 0xe7d3fbc8, 20); |
| 97 |
88553 |
| MD5_STEP_G(a, b, c, d, M[ 9], 0x21e1cde6, 5); |
| 98 |
88553 |
| MD5_STEP_G(d, a, b, c, M[14], 0xc33707d6, 9); |
| 99 |
88553 |
| MD5_STEP_G(c, d, a, b, M[ 3], 0xf4d50d87, 14); |
| 100 |
88553 |
| MD5_STEP_G(b, c, d, a, M[ 8], 0x455a14ed, 20); |
| 101 |
88553 |
| MD5_STEP_G(a, b, c, d, M[13], 0xa9e3e905, 5); |
| 102 |
88553 |
| MD5_STEP_G(d, a, b, c, M[ 2], 0xfcefa3f8, 9); |
| 103 |
88553 |
| MD5_STEP_G(c, d, a, b, M[ 7], 0x676f02d9, 14); |
| 104 |
88553 |
| MD5_STEP_G(b, c, d, a, M[12], 0x8d2a4c8a, 20); |
| 105 |
|
| |
| 106 |
88553 |
| MD5_STEP(MD5_H, a, b, c, d, M[ 5], 0xfffa3942, 4); |
| 107 |
88553 |
| MD5_STEP(MD5_H, d, a, b, c, M[ 8], 0x8771f681, 11); |
| 108 |
88553 |
| MD5_STEP(MD5_H, c, d, a, b, M[11], 0x6d9d6122, 16); |
| 109 |
88553 |
| MD5_STEP(MD5_H, b, c, d, a, M[14], 0xfde5380c, 23); |
| 110 |
88553 |
| MD5_STEP(MD5_H, a, b, c, d, M[ 1], 0xa4beea44, 4); |
| 111 |
88553 |
| MD5_STEP(MD5_H, d, a, b, c, M[ 4], 0x4bdecfa9, 11); |
| 112 |
88553 |
| MD5_STEP(MD5_H, c, d, a, b, M[ 7], 0xf6bb4b60, 16); |
| 113 |
88553 |
| MD5_STEP(MD5_H, b, c, d, a, M[10], 0xbebfbc70, 23); |
| 114 |
88553 |
| MD5_STEP(MD5_H, a, b, c, d, M[13], 0x289b7ec6, 4); |
| 115 |
88553 |
| MD5_STEP(MD5_H, d, a, b, c, M[ 0], 0xeaa127fa, 11); |
| 116 |
88553 |
| MD5_STEP(MD5_H, c, d, a, b, M[ 3], 0xd4ef3085, 16); |
| 117 |
88553 |
| MD5_STEP(MD5_H, b, c, d, a, M[ 6], 0x04881d05, 23); |
| 118 |
88553 |
| MD5_STEP(MD5_H, a, b, c, d, M[ 9], 0xd9d4d039, 4); |
| 119 |
88553 |
| MD5_STEP(MD5_H, d, a, b, c, M[12], 0xe6db99e5, 11); |
| 120 |
88553 |
| MD5_STEP(MD5_H, c, d, a, b, M[15], 0x1fa27cf8, 16); |
| 121 |
88553 |
| MD5_STEP(MD5_H, b, c, d, a, M[ 2], 0xc4ac5665, 23); |
| 122 |
|
| |
| 123 |
88553 |
| MD5_STEP(MD5_I, a, b, c, d, M[ 0], 0xf4292244, 6); |
| 124 |
88553 |
| MD5_STEP(MD5_I, d, a, b, c, M[ 7], 0x432aff97, 10); |
| 125 |
88553 |
| MD5_STEP(MD5_I, c, d, a, b, M[14], 0xab9423a7, 15); |
| 126 |
88553 |
| MD5_STEP(MD5_I, b, c, d, a, M[ 5], 0xfc93a039, 21); |
| 127 |
88553 |
| MD5_STEP(MD5_I, a, b, c, d, M[12], 0x655b59c3, 6); |
| 128 |
88553 |
| MD5_STEP(MD5_I, d, a, b, c, M[ 3], 0x8f0ccc92, 10); |
| 129 |
88553 |
| MD5_STEP(MD5_I, c, d, a, b, M[10], 0xffeff47d, 15); |
| 130 |
88553 |
| MD5_STEP(MD5_I, b, c, d, a, M[ 1], 0x85845dd1, 21); |
| 131 |
88553 |
| MD5_STEP(MD5_I, a, b, c, d, M[ 8], 0x6fa87e4f, 6); |
| 132 |
88553 |
| MD5_STEP(MD5_I, d, a, b, c, M[15], 0xfe2ce6e0, 10); |
| 133 |
88553 |
| MD5_STEP(MD5_I, c, d, a, b, M[ 6], 0xa3014314, 15); |
| 134 |
88553 |
| MD5_STEP(MD5_I, b, c, d, a, M[13], 0x4e0811a1, 21); |
| 135 |
88553 |
| MD5_STEP(MD5_I, a, b, c, d, M[ 4], 0xf7537e82, 6); |
| 136 |
88553 |
| MD5_STEP(MD5_I, d, a, b, c, M[11], 0xbd3af235, 10); |
| 137 |
88553 |
| MD5_STEP(MD5_I, c, d, a, b, M[ 2], 0x2ad7d2bb, 15); |
| 138 |
88553 |
| MD5_STEP(MD5_I, b, c, d, a, M[ 9], 0xeb86d391, 21); |
| 139 |
|
| |
| 140 |
88553 |
| ctx->state[0] += a; |
| 141 |
88553 |
| ctx->state[1] += b; |
| 142 |
88553 |
| ctx->state[2] += c; |
| 143 |
88553 |
| ctx->state[3] += d; |
| 144 |
88553 |
| } |
| 145 |
|
| |
| 146 |
|
| |
| 147 |
|
| void |
| 148 |
60 |
| md5_init(md5_ctx_t *ctx) |
| 149 |
|
| { |
| 150 |
60 |
| ctx->state[0] = 0x67452301u; |
| 151 |
60 |
| ctx->state[1] = 0xefcdab89u; |
| 152 |
60 |
| ctx->state[2] = 0x98badcfeu; |
| 153 |
60 |
| ctx->state[3] = 0x10325476u; |
| 154 |
60 |
| ctx->bit_count = 0; |
| 155 |
60 |
| ctx->buffered = 0; |
| 156 |
60 |
| } |
| 157 |
|
| |
| 158 |
|
| void |
| 159 |
18383 |
| md5_update(md5_ctx_t *ctx, const void *data, size_t len) |
| 160 |
|
| { |
| 161 |
18383 |
| const unsigned char *p = (const unsigned char *)data; |
| 162 |
|
| size_t take; |
| 163 |
|
| |
| 164 |
18383 |
| ctx->bit_count += (uint64_t)len << 3; |
| 165 |
|
| |
| 166 |
18383 |
▶ | if (ctx->buffered) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->buffered) { |
16790 |
1593 |
18383 |
|
| 167 |
16790 |
| take = MD5_BLOCK_SIZE - ctx->buffered; |
| 168 |
16790 |
▶ | if (take > len) take = len; |
Branch: 1 branch
| Branch | True | False | Total |
| if (take > len) take = len; |
2705 |
14085 |
16790 |
|
| 169 |
16790 |
| memcpy(ctx->buffer + ctx->buffered, p, take); |
| 170 |
16790 |
| ctx->buffered += take; |
| 171 |
16790 |
| p += take; |
| 172 |
16790 |
| len -= take; |
| 173 |
16790 |
▶ | if (ctx->buffered == MD5_BLOCK_SIZE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->buffered == MD5_BLOCK_SIZE) { |
14085 |
2705 |
16790 |
|
| 174 |
14085 |
| md5_compress(ctx, ctx->buffer); |
| 175 |
14085 |
| ctx->buffered = 0; |
| 176 |
|
| } |
| 177 |
|
| } |
| 178 |
92851 |
▶ | while (len >= MD5_BLOCK_SIZE) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (len >= MD5_BLOCK_SIZE) { |
74468 |
18383 |
92851 |
|
| 179 |
74468 |
| md5_compress(ctx, p); |
| 180 |
74468 |
| p += MD5_BLOCK_SIZE; |
| 181 |
74468 |
| len -= MD5_BLOCK_SIZE; |
| 182 |
|
| } |
| 183 |
18383 |
▶ | if (len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len) { |
14085 |
4298 |
18383 |
|
| 184 |
14085 |
| memcpy(ctx->buffer, p, len); |
| 185 |
14085 |
| ctx->buffered = len; |
| 186 |
|
| } |
| 187 |
18383 |
| } |
| 188 |
|
| |
| 189 |
|
| void |
| 190 |
56 |
| md5_final(md5_ctx_t *ctx, unsigned char out[MD5_DIGEST_SIZE]) |
| 191 |
|
| { |
| 192 |
|
| /* Pad: append 0x80, zeros, then 64-bit little-endian length. */ |
| 193 |
|
| unsigned char pad[MD5_BLOCK_SIZE]; |
| 194 |
|
| unsigned char len_le[8]; |
| 195 |
56 |
| uint64_t bits = ctx->bit_count; |
| 196 |
|
| size_t pad_len; |
| 197 |
|
| int i; |
| 198 |
|
| |
| 199 |
56 |
| pad[0] = 0x80; |
| 200 |
3584 |
▶ | for (i = 1; i < MD5_BLOCK_SIZE; i++) pad[i] = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < MD5_BLOCK_SIZE; i++) pad[i] = 0; |
3528 |
56 |
3584 |
|
| 201 |
|
| |
| 202 |
|
| /* Need to land at offset 56 mod 64 after writing pad. */ |
| 203 |
56 |
▶ | if (ctx->buffered < 56) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->buffered < 56) |
51 |
5 |
56 |
|
| 204 |
51 |
| pad_len = 56 - ctx->buffered; |
| 205 |
|
| else |
| 206 |
5 |
| pad_len = (MD5_BLOCK_SIZE + 56) - ctx->buffered; |
| 207 |
|
| |
| 208 |
56 |
| md5_update(ctx, pad, pad_len); |
| 209 |
|
| |
| 210 |
504 |
▶ | for (i = 0; i < 8; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 8; i++) { |
448 |
56 |
504 |
|
| 211 |
448 |
| len_le[i] = (unsigned char)((bits >> (i * 8)) & 0xFFu); |
| 212 |
|
| } |
| 213 |
56 |
| md5_update(ctx, len_le, 8); |
| 214 |
|
| |
| 215 |
|
| /* bit_count was bumped twice by the padding update — that's fine, |
| 216 |
|
| * it's only consumed once during the very pad we just wrote. */ |
| 217 |
280 |
▶ | for (i = 0; i < 4; i++) store32_le(out + i * 4, ctx->state[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 4; i++) store32_le(out + i * 4, ctx->state[i]); |
224 |
56 |
280 |
|
| 218 |
56 |
| } |