]> git.neil.brown.name Git - edlib.git/blob - md5.h
TODO: clean out done items.
[edlib.git] / md5.h
1 /*
2  * This program is free software; you can redistribute it and/or modify it
3  * under the terms of the GNU General Public License as published by the Free
4  * Software Foundation; either version 2 of the License, or (at your option)
5  * any later version.
6  */
7
8 #include <stdint.h>
9 #include <memory.h>
10 #include <endian.h>
11
12 #include "safe.h"
13
14 #define MD5_DIGEST_SIZE         16
15 #define MD5_HMAC_BLOCK_SIZE     64
16 #define MD5_BLOCK_WORDS         16
17 #define MD5_HASH_WORDS          4
18
19 #define MD5_H0  0x67452301UL
20 #define MD5_H1  0xefcdab89UL
21 #define MD5_H2  0x98badcfeUL
22 #define MD5_H3  0x10325476UL
23
24 struct md5_state {
25         uint32_t hash[MD5_HASH_WORDS];
26         uint32_t block[MD5_BLOCK_WORDS];
27         uint64_t byte_count;
28 };
29
30 #define F1(x, y, z)     (z ^ (x & (y ^ z)))
31 #define F2(x, y, z)     F1(z, x, y)
32 #define F3(x, y, z)     (x ^ y ^ z)
33 #define F4(x, y, z)     (y ^ (x | ~z))
34
35 #define MD5STEP(f, w, x, y, z, in, s) \
36         (w += f(x, y, z) + in, w = (w<<s | w>>(32-s)) + x)
37
38 static void md5_transform(uint32_t hash[MD5_HASH_WORDS], uint32_t const in[MD5_BLOCK_WORDS])
39 {
40         uint32_t a, b, c, d;
41
42         a = hash[0];
43         b = hash[1];
44         c = hash[2];
45         d = hash[3];
46
47         MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
48         MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
49         MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
50         MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
51         MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
52         MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
53         MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
54         MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
55         MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
56         MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
57         MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
58         MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
59         MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
60         MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
61         MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
62         MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
63
64         MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
65         MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
66         MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
67         MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
68         MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
69         MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
70         MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
71         MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
72         MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
73         MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
74         MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
75         MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
76         MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
77         MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
78         MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
79         MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
80
81         MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
82         MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
83         MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
84         MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
85         MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
86         MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
87         MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
88         MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
89         MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
90         MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
91         MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
92         MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
93         MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
94         MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
95         MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
96         MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
97
98         MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
99         MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
100         MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
101         MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
102         MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
103         MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
104         MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
105         MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
106         MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
107         MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
108         MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
109         MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
110         MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
111         MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
112         MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
113         MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
114
115         hash[0] += a;
116         hash[1] += b;
117         hash[2] += c;
118         hash[3] += d;
119 }
120
121 static inline void le32_to_cpu_block(uint32_t buf[MD5_BLOCK_WORDS])
122 {
123         int i;
124
125         for (i = 0; i < MD5_BLOCK_WORDS; i++)
126                 buf[i] = le32toh(buf[i]);
127 }
128
129 static inline void cpu_to_le32_hash(uint32_t buf[MD5_HASH_WORDS])
130 {
131         int i;
132
133         for (i = 0; i < MD5_HASH_WORDS; i++)
134                 buf[i] = htole32(buf[i]);
135 }
136
137 static inline void md5_transform_helper(struct md5_state *ctx safe)
138 {
139         le32_to_cpu_block(ctx->block);
140         md5_transform(ctx->hash, ctx->block);
141 }
142
143 static int md5_init(struct md5_state *mctx safe)
144 {
145         mctx->hash[0] = MD5_H0;
146         mctx->hash[1] = MD5_H1;
147         mctx->hash[2] = MD5_H2;
148         mctx->hash[3] = MD5_H3;
149         mctx->byte_count = 0;
150
151         return 0;
152 }
153
154 static int md5_update(struct md5_state *mctx safe, const uint8_t *data safe, unsigned int len)
155 {
156         uint32_t avail = sizeof(mctx->block) - (mctx->byte_count & 0x3f);
157
158         mctx->byte_count += len;
159
160         while (len >= avail) {
161                 memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
162                        data, avail);
163                 data += avail;
164                 len -= avail;
165                 md5_transform_helper(mctx);
166                 avail = sizeof(mctx->block);
167         }
168
169         memcpy((char *)mctx->block + (sizeof(mctx->block) - avail),
170                data, len);
171         return 0;
172 }
173
174 static void md5_final(struct md5_state *mctx safe, uint8_t out[MD5_DIGEST_SIZE])
175 {
176         const unsigned int offset = mctx->byte_count & 0x3f;
177         char *p = safe_cast (char *)mctx->block + offset;
178         int padding = 56 - (offset + 1);
179
180         *p++ = 0x80;
181         if (padding < 0) {
182                 memset(p, 0x00, padding + sizeof (uint64_t));
183                 md5_transform_helper(mctx);
184                 p = (char *)mctx->block;
185                 padding = 56;
186         }
187
188         memset(p, 0, padding);
189         le32_to_cpu_block(mctx->block);
190         mctx->block[14] = mctx->byte_count << 3;
191         mctx->block[15] = mctx->byte_count >> 29;
192         md5_transform(mctx->hash, mctx->block);
193         cpu_to_le32_hash(mctx->hash);
194         memcpy(out, mctx->hash, sizeof(mctx->hash));
195         memset(mctx, 0, sizeof(*mctx));
196 }
197
198 static const char _md5_hex[] = "0123456789ABCDEF";
199 static void md5_final_txt(struct md5_state *mctx safe, char out[MD5_DIGEST_SIZE*2+1])
200 {
201         uint8_t outb[MD5_DIGEST_SIZE];
202         int i;
203
204         md5_final(mctx, outb);
205         for (i = 0; i < MD5_DIGEST_SIZE; i++) {
206                 out[i*2]   = _md5_hex[outb[i]>>4];
207                 out[i*2+1] = _md5_hex[outb[i]&0xf];
208         }
209         out[i*2] = '\0';
210 }