| 1 |
|
| /* |
| 2 |
|
| * md5.c - MD5 implementation per RFC 1321. |
| 3 |
|
| * |
| 4 |
|
| * Plain C99, no external deps. Endian-agnostic (reads/writes go through |
| 5 |
|
| * byte-by-byte loaders/storers). Round constants and shift amounts come |
| 6 |
|
| * directly from the spec. |
| 7 |
|
| */ |
| 8 |
|
| |
| 9 |
|
| #include "md5.h" |
| 10 |
|
| #include <string.h> |
| 11 |
|
| |
| 12 |
|
| #define ROL32(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) |
| 13 |
|
| |
| 14 |
|
| static const uint32_t MD5_K[64] = { |
| 15 |
|
| 0xd76aa478u, 0xe8c7b756u, 0x242070dbu, 0xc1bdceeeu, |
| 16 |
|
| 0xf57c0fafu, 0x4787c62au, 0xa8304613u, 0xfd469501u, |
| 17 |
|
| 0x698098d8u, 0x8b44f7afu, 0xffff5bb1u, 0x895cd7beu, |
| 18 |
|
| 0x6b901122u, 0xfd987193u, 0xa679438eu, 0x49b40821u, |
| 19 |
|
| 0xf61e2562u, 0xc040b340u, 0x265e5a51u, 0xe9b6c7aau, |
| 20 |
|
| 0xd62f105du, 0x02441453u, 0xd8a1e681u, 0xe7d3fbc8u, |
| 21 |
|
| 0x21e1cde6u, 0xc33707d6u, 0xf4d50d87u, 0x455a14edu, |
| 22 |
|
| 0xa9e3e905u, 0xfcefa3f8u, 0x676f02d9u, 0x8d2a4c8au, |
| 23 |
|
| 0xfffa3942u, 0x8771f681u, 0x6d9d6122u, 0xfde5380cu, |
| 24 |
|
| 0xa4beea44u, 0x4bdecfa9u, 0xf6bb4b60u, 0xbebfbc70u, |
| 25 |
|
| 0x289b7ec6u, 0xeaa127fau, 0xd4ef3085u, 0x04881d05u, |
| 26 |
|
| 0xd9d4d039u, 0xe6db99e5u, 0x1fa27cf8u, 0xc4ac5665u, |
| 27 |
|
| 0xf4292244u, 0x432aff97u, 0xab9423a7u, 0xfc93a039u, |
| 28 |
|
| 0x655b59c3u, 0x8f0ccc92u, 0xffeff47du, 0x85845dd1u, |
| 29 |
|
| 0x6fa87e4fu, 0xfe2ce6e0u, 0xa3014314u, 0x4e0811a1u, |
| 30 |
|
| 0xf7537e82u, 0xbd3af235u, 0x2ad7d2bbu, 0xeb86d391u |
| 31 |
|
| }; |
| 32 |
|
| |
| 33 |
|
| static const unsigned MD5_S[64] = { |
| 34 |
|
| 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, |
| 35 |
|
| 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, |
| 36 |
|
| 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, |
| 37 |
|
| 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 |
| 38 |
|
| }; |
| 39 |
|
| |
| 40 |
|
| static uint32_t |
| 41 |
166208 |
| load32_le(const unsigned char *p) |
| 42 |
|
| { |
| 43 |
166208 |
| return (uint32_t)p[0] |
| 44 |
166208 |
| | ((uint32_t)p[1] << 8) |
| 45 |
166208 |
| | ((uint32_t)p[2] << 16) |
| 46 |
166208 |
| | ((uint32_t)p[3] << 24); |
| 47 |
|
| } |
| 48 |
|
| |
| 49 |
|
| static void |
| 50 |
104 |
| store32_le(unsigned char *p, uint32_t v) |
| 51 |
|
| { |
| 52 |
104 |
| p[0] = (unsigned char)( v & 0xFFu); |
| 53 |
104 |
| p[1] = (unsigned char)((v >> 8) & 0xFFu); |
| 54 |
104 |
| p[2] = (unsigned char)((v >> 16) & 0xFFu); |
| 55 |
104 |
| p[3] = (unsigned char)((v >> 24) & 0xFFu); |
| 56 |
104 |
| } |
| 57 |
|
| |
| 58 |
|
| static void |
| 59 |
10388 |
| md5_compress(md5_ctx_t *ctx, const unsigned char *block) |
| 60 |
|
| { |
| 61 |
|
| uint32_t M[16]; |
| 62 |
10388 |
| uint32_t a = ctx->state[0], b = ctx->state[1], |
| 63 |
10388 |
| c = ctx->state[2], d = ctx->state[3]; |
| 64 |
|
| uint32_t f, g, t; |
| 65 |
|
| int i; |
| 66 |
|
| |
| 67 |
176596 |
▶ | for (i = 0; i < 16; i++) M[i] = load32_le(block + i * 4); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 16; i++) M[i] = load32_le(block + i * 4); |
166208 |
10388 |
176596 |
|
| 68 |
|
| |
| 69 |
675220 |
▶ | for (i = 0; i < 64; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 64; i++) { |
664832 |
10388 |
675220 |
|
| 70 |
664832 |
▶ | if (i < 16) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i < 16) { |
166208 |
498624 |
664832 |
|
| 71 |
166208 |
| f = (b & c) | (~b & d); |
| 72 |
166208 |
| g = (uint32_t)i; |
| 73 |
498624 |
▶ | } else if (i < 32) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (i < 32) { |
166208 |
332416 |
498624 |
|
| 74 |
166208 |
| f = (d & b) | (~d & c); |
| 75 |
166208 |
| g = (uint32_t)(5 * i + 1) & 15u; |
| 76 |
332416 |
▶ | } else if (i < 48) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (i < 48) { |
166208 |
166208 |
332416 |
|
| 77 |
166208 |
| f = b ^ c ^ d; |
| 78 |
166208 |
| g = (uint32_t)(3 * i + 5) & 15u; |
| 79 |
|
| } else { |
| 80 |
166208 |
| f = c ^ (b | ~d); |
| 81 |
166208 |
| g = (uint32_t)(7 * i) & 15u; |
| 82 |
|
| } |
| 83 |
664832 |
| t = d; |
| 84 |
664832 |
| d = c; |
| 85 |
664832 |
| c = b; |
| 86 |
664832 |
| b = b + ROL32(a + f + MD5_K[i] + M[g], MD5_S[i]); |
| 87 |
664832 |
| a = t; |
| 88 |
|
| } |
| 89 |
|
| |
| 90 |
10388 |
| ctx->state[0] += a; |
| 91 |
10388 |
| ctx->state[1] += b; |
| 92 |
10388 |
| ctx->state[2] += c; |
| 93 |
10388 |
| ctx->state[3] += d; |
| 94 |
10388 |
| } |
| 95 |
|
| |
| 96 |
|
| void |
| 97 |
30 |
| md5_init(md5_ctx_t *ctx) |
| 98 |
|
| { |
| 99 |
30 |
| ctx->state[0] = 0x67452301u; |
| 100 |
30 |
| ctx->state[1] = 0xefcdab89u; |
| 101 |
30 |
| ctx->state[2] = 0x98badcfeu; |
| 102 |
30 |
| ctx->state[3] = 0x10325476u; |
| 103 |
30 |
| ctx->bit_count = 0; |
| 104 |
30 |
| ctx->buffered = 0; |
| 105 |
30 |
| } |
| 106 |
|
| |
| 107 |
|
| void |
| 108 |
85 |
| md5_update(md5_ctx_t *ctx, const void *data, size_t len) |
| 109 |
|
| { |
| 110 |
85 |
| const unsigned char *p = (const unsigned char *)data; |
| 111 |
|
| size_t take; |
| 112 |
|
| |
| 113 |
85 |
| ctx->bit_count += (uint64_t)len << 3; |
| 114 |
|
| |
| 115 |
85 |
▶ | if (ctx->buffered) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->buffered) { |
47 |
38 |
85 |
|
| 116 |
47 |
| take = MD5_BLOCK_SIZE - ctx->buffered; |
| 117 |
47 |
▶ | if (take > len) take = len; |
Branch: 1 branch
| Branch | True | False | Total |
| if (take > len) take = len; |
21 |
26 |
47 |
|
| 118 |
47 |
| memcpy(ctx->buffer + ctx->buffered, p, take); |
| 119 |
47 |
| ctx->buffered += take; |
| 120 |
47 |
| p += take; |
| 121 |
47 |
| len -= take; |
| 122 |
47 |
▶ | if (ctx->buffered == MD5_BLOCK_SIZE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->buffered == MD5_BLOCK_SIZE) { |
26 |
21 |
47 |
|
| 123 |
26 |
| md5_compress(ctx, ctx->buffer); |
| 124 |
26 |
| ctx->buffered = 0; |
| 125 |
|
| } |
| 126 |
|
| } |
| 127 |
10447 |
▶ | while (len >= MD5_BLOCK_SIZE) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (len >= MD5_BLOCK_SIZE) { |
10362 |
85 |
10447 |
|
| 128 |
10362 |
| md5_compress(ctx, p); |
| 129 |
10362 |
| p += MD5_BLOCK_SIZE; |
| 130 |
10362 |
| len -= MD5_BLOCK_SIZE; |
| 131 |
|
| } |
| 132 |
85 |
▶ | if (len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len) { |
26 |
59 |
85 |
|
| 133 |
26 |
| memcpy(ctx->buffer, p, len); |
| 134 |
26 |
| ctx->buffered = len; |
| 135 |
|
| } |
| 136 |
85 |
| } |
| 137 |
|
| |
| 138 |
|
| void |
| 139 |
26 |
| md5_final(md5_ctx_t *ctx, unsigned char out[MD5_DIGEST_SIZE]) |
| 140 |
|
| { |
| 141 |
|
| /* Pad: append 0x80, zeros, then 64-bit little-endian length. */ |
| 142 |
|
| unsigned char pad[MD5_BLOCK_SIZE]; |
| 143 |
|
| unsigned char len_le[8]; |
| 144 |
26 |
| uint64_t bits = ctx->bit_count; |
| 145 |
|
| size_t pad_len; |
| 146 |
|
| int i; |
| 147 |
|
| |
| 148 |
26 |
| pad[0] = 0x80; |
| 149 |
1664 |
▶ | 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; |
1638 |
26 |
1664 |
|
| 150 |
|
| |
| 151 |
|
| /* Need to land at offset 56 mod 64 after writing pad. */ |
| 152 |
26 |
▶ | if (ctx->buffered < 56) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ctx->buffered < 56) |
26 |
0 |
26 |
|
| 153 |
26 |
| pad_len = 56 - ctx->buffered; |
| 154 |
|
| else |
| 155 |
0 |
| pad_len = (MD5_BLOCK_SIZE + 56) - ctx->buffered; |
| 156 |
|
| |
| 157 |
26 |
| md5_update(ctx, pad, pad_len); |
| 158 |
|
| |
| 159 |
234 |
▶ | for (i = 0; i < 8; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 8; i++) { |
208 |
26 |
234 |
|
| 160 |
208 |
| len_le[i] = (unsigned char)((bits >> (i * 8)) & 0xFFu); |
| 161 |
|
| } |
| 162 |
26 |
| md5_update(ctx, len_le, 8); |
| 163 |
|
| |
| 164 |
|
| /* bit_count was bumped twice by the padding update — that's fine, |
| 165 |
|
| * it's only consumed once during the very pad we just wrote. */ |
| 166 |
130 |
▶ | 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]); |
104 |
26 |
130 |
|
| 167 |
26 |
| } |