/src/dovecot/src/lib/md4.c
Line | Count | Source |
1 | | /* |
2 | | * MD4 (RFC-1320) message digest. |
3 | | * Modified from MD5 code by Andrey Panin <pazke@donpac.ru> |
4 | | * |
5 | | * Written by Solar Designer <solar@openwall.com> in 2001, and placed in |
6 | | * the public domain. There's absolutely no warranty. |
7 | | * |
8 | | * This differs from Colin Plumb's older public domain implementation in |
9 | | * that no 32-bit integer data type is required, there's no compile-time |
10 | | * endianness configuration, and the function prototypes match OpenSSL's. |
11 | | * The primary goals are portability and ease of use. |
12 | | * |
13 | | * This implementation is meant to be fast, but not as fast as possible. |
14 | | * Some known optimizations are not included to reduce source code size |
15 | | * and avoid compile-time configuration. |
16 | | */ |
17 | | |
18 | | #include "lib.h" |
19 | | #include "safe-memset.h" |
20 | | #include "md4.h" |
21 | | |
22 | | /* |
23 | | * The basic MD4 functions. |
24 | | */ |
25 | 4.41M | #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) |
26 | 4.41M | #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) |
27 | 4.41M | #define H(x, y, z) ((x) ^ (y) ^ (z)) |
28 | | |
29 | | /* |
30 | | * The MD4 transformation for all four rounds. |
31 | | */ |
32 | | #define STEP(f, a, b, c, d, x, s) \ |
33 | 13.2M | (a) += f((b), (c), (d)) + (x); \ |
34 | 13.2M | (a) = ((a) << (s)) | ((a) >> (32 - (s))) |
35 | | |
36 | | |
37 | | #define SET(n) \ |
38 | | (ctx->block[(n)] = \ |
39 | | (uint_fast32_t)ptr[(n) * 4] | \ |
40 | | ((uint_fast32_t)ptr[(n) * 4 + 1] << 8) | \ |
41 | | ((uint_fast32_t)ptr[(n) * 4 + 2] << 16) | \ |
42 | | ((uint_fast32_t)ptr[(n) * 4 + 3] << 24)) |
43 | | #define GET(n) \ |
44 | | (ctx->block[(n)]) |
45 | | |
46 | | /* |
47 | | * This processes one or more 64-byte data blocks, but does NOT update |
48 | | * the bit counters. There're no alignment requirements. |
49 | | */ |
50 | | static const void * ATTR_NOWARN_UNUSED_RESULT ATTR_UNSIGNED_WRAPS |
51 | | ATTR_NO_SANITIZE_UNDEFINED ATTR_NO_SANITIZE_INTEGER |
52 | | ATTR_NO_SANITIZE_IMPLICIT_CONVERSION |
53 | | body(struct md4_context *ctx, const void *data, size_t size) |
54 | 246k | { |
55 | 246k | const unsigned char *ptr; |
56 | 246k | uint32_t a, b, c, d; |
57 | 246k | uint32_t saved_a, saved_b, saved_c, saved_d; |
58 | | |
59 | 246k | ptr = data; |
60 | | |
61 | 246k | a = ctx->a; |
62 | 246k | b = ctx->b; |
63 | 246k | c = ctx->c; |
64 | 246k | d = ctx->d; |
65 | | |
66 | 276k | do { |
67 | 276k | saved_a = a; |
68 | 276k | saved_b = b; |
69 | 276k | saved_c = c; |
70 | 276k | saved_d = d; |
71 | | |
72 | | /* Round 1 */ |
73 | 276k | STEP(F, a, b, c, d, SET( 0), 3); |
74 | 276k | STEP(F, d, a, b, c, SET( 1), 7); |
75 | 276k | STEP(F, c, d, a, b, SET( 2), 11); |
76 | 276k | STEP(F, b, c, d, a, SET( 3), 19); |
77 | | |
78 | 276k | STEP(F, a, b, c, d, SET( 4), 3); |
79 | 276k | STEP(F, d, a, b, c, SET( 5), 7); |
80 | 276k | STEP(F, c, d, a, b, SET( 6), 11); |
81 | 276k | STEP(F, b, c, d, a, SET( 7), 19); |
82 | | |
83 | 276k | STEP(F, a, b, c, d, SET( 8), 3); |
84 | 276k | STEP(F, d, a, b, c, SET( 9), 7); |
85 | 276k | STEP(F, c, d, a, b, SET(10), 11); |
86 | 276k | STEP(F, b, c, d, a, SET(11), 19); |
87 | | |
88 | 276k | STEP(F, a, b, c, d, SET(12), 3); |
89 | 276k | STEP(F, d, a, b, c, SET(13), 7); |
90 | 276k | STEP(F, c, d, a, b, SET(14), 11); |
91 | 276k | STEP(F, b, c, d, a, SET(15), 19); |
92 | | /* Round 2 */ |
93 | 276k | STEP(G, a, b, c, d, GET( 0) + 0x5A827999, 3); |
94 | 276k | STEP(G, d, a, b, c, GET( 4) + 0x5A827999, 5); |
95 | 276k | STEP(G, c, d, a, b, GET( 8) + 0x5A827999, 9); |
96 | 276k | STEP(G, b, c, d, a, GET(12) + 0x5A827999, 13); |
97 | | |
98 | 276k | STEP(G, a, b, c, d, GET( 1) + 0x5A827999, 3); |
99 | 276k | STEP(G, d, a, b, c, GET( 5) + 0x5A827999, 5); |
100 | 276k | STEP(G, c, d, a, b, GET( 9) + 0x5A827999, 9); |
101 | 276k | STEP(G, b, c, d, a, GET(13) + 0x5A827999, 13); |
102 | | |
103 | 276k | STEP(G, a, b, c, d, GET( 2) + 0x5A827999, 3); |
104 | 276k | STEP(G, d, a, b, c, GET( 6) + 0x5A827999, 5); |
105 | 276k | STEP(G, c, d, a, b, GET(10) + 0x5A827999, 9); |
106 | 276k | STEP(G, b, c, d, a, GET(14) + 0x5A827999, 13); |
107 | | |
108 | 276k | STEP(G, a, b, c, d, GET( 3) + 0x5A827999, 3); |
109 | 276k | STEP(G, d, a, b, c, GET( 7) + 0x5A827999, 5); |
110 | 276k | STEP(G, c, d, a, b, GET(11) + 0x5A827999, 9); |
111 | 276k | STEP(G, b, c, d, a, GET(15) + 0x5A827999, 13); |
112 | | /* Round 3 */ |
113 | 276k | STEP(H, a, b, c, d, GET( 0) + 0x6ED9EBA1, 3); |
114 | 276k | STEP(H, d, a, b, c, GET( 8) + 0x6ED9EBA1, 9); |
115 | 276k | STEP(H, c, d, a, b, GET( 4) + 0x6ED9EBA1, 11); |
116 | 276k | STEP(H, b, c, d, a, GET(12) + 0x6ED9EBA1, 15); |
117 | | |
118 | 276k | STEP(H, a, b, c, d, GET( 2) + 0x6ED9EBA1, 3); |
119 | 276k | STEP(H, d, a, b, c, GET(10) + 0x6ED9EBA1, 9); |
120 | 276k | STEP(H, c, d, a, b, GET( 6) + 0x6ED9EBA1, 11); |
121 | 276k | STEP(H, b, c, d, a, GET(14) + 0x6ED9EBA1, 15); |
122 | | |
123 | 276k | STEP(H, a, b, c, d, GET( 1) + 0x6ED9EBA1, 3); |
124 | 276k | STEP(H, d, a, b, c, GET( 9) + 0x6ED9EBA1, 9); |
125 | 276k | STEP(H, c, d, a, b, GET( 5) + 0x6ED9EBA1, 11); |
126 | 276k | STEP(H, b, c, d, a, GET(13) + 0x6ED9EBA1, 15); |
127 | | |
128 | 276k | STEP(H, a, b, c, d, GET( 3) + 0x6ED9EBA1, 3); |
129 | 276k | STEP(H, d, a, b, c, GET(11) + 0x6ED9EBA1, 9); |
130 | 276k | STEP(H, c, d, a, b, GET( 7) + 0x6ED9EBA1, 11); |
131 | 276k | STEP(H, b, c, d, a, GET(15) + 0x6ED9EBA1, 15); |
132 | | |
133 | 276k | a += saved_a; |
134 | 276k | b += saved_b; |
135 | 276k | c += saved_c; |
136 | 276k | d += saved_d; |
137 | | |
138 | 276k | ptr += 64; |
139 | 276k | } while ((size -= 64) != 0); |
140 | | |
141 | 246k | ctx->a = a; |
142 | 246k | ctx->b = b; |
143 | 246k | ctx->c = c; |
144 | 246k | ctx->d = d; |
145 | | |
146 | 246k | return ptr; |
147 | 246k | } |
148 | | |
149 | | void md4_init(struct md4_context *ctx) |
150 | 245k | { |
151 | 245k | ctx->a = 0x67452301; |
152 | 245k | ctx->b = 0xefcdab89; |
153 | 245k | ctx->c = 0x98badcfe; |
154 | 245k | ctx->d = 0x10325476; |
155 | | |
156 | 245k | ctx->lo = 0; |
157 | 245k | ctx->hi = 0; |
158 | 245k | } |
159 | | |
160 | | void md4_update(struct md4_context *ctx, const void *data, size_t size) |
161 | 245k | { |
162 | | /* @UNSAFE */ |
163 | 245k | uint_fast32_t saved_lo; |
164 | 245k | unsigned long used, free; |
165 | | |
166 | 245k | if (size == 0) |
167 | 11 | return; |
168 | | |
169 | 245k | saved_lo = ctx->lo; |
170 | 245k | if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo) |
171 | 0 | ctx->hi++; |
172 | 245k | ctx->hi += size >> 29; |
173 | | |
174 | 245k | used = saved_lo & 0x3f; |
175 | | |
176 | 245k | if (used != 0) { |
177 | 129 | free = 64 - used; |
178 | | |
179 | 129 | if (size < free) { |
180 | 21 | memcpy(&ctx->buffer[used], data, size); |
181 | 21 | return; |
182 | 21 | } |
183 | | |
184 | 108 | memcpy(&ctx->buffer[used], data, free); |
185 | 108 | data = (const unsigned char *) data + free; |
186 | 108 | size -= free; |
187 | 108 | body(ctx, ctx->buffer, 64); |
188 | 108 | } |
189 | | |
190 | 245k | if (size >= 64) { |
191 | 98 | data = body(ctx, data, size & ~0x3fUL); |
192 | 98 | size &= 0x3f; |
193 | 98 | } |
194 | | |
195 | 245k | memcpy(ctx->buffer, data, size); |
196 | 245k | } |
197 | | |
198 | | void ATTR_NO_SANITIZE_UNDEFINED ATTR_NO_SANITIZE_INTEGER |
199 | | ATTR_NO_SANITIZE_IMPLICIT_CONVERSION |
200 | | md4_final(struct md4_context *ctx, unsigned char result[STATIC_ARRAY MD4_RESULTLEN]) |
201 | 245k | { |
202 | | /* @UNSAFE */ |
203 | 245k | unsigned long used, free; |
204 | | |
205 | 245k | used = ctx->lo & 0x3f; |
206 | | |
207 | 245k | ctx->buffer[used++] = 0x80; |
208 | | |
209 | 245k | free = 64 - used; |
210 | | |
211 | 245k | if (free < 8) { |
212 | 6 | memset(&ctx->buffer[used], 0, free); |
213 | 6 | body(ctx, ctx->buffer, 64); |
214 | 6 | used = 0; |
215 | 6 | free = 64; |
216 | 6 | } |
217 | | |
218 | 245k | memset(&ctx->buffer[used], 0, free - 8); |
219 | | |
220 | 245k | ctx->lo <<= 3; |
221 | 245k | ctx->buffer[56] = ctx->lo; |
222 | 245k | ctx->buffer[57] = ctx->lo >> 8; |
223 | 245k | ctx->buffer[58] = ctx->lo >> 16; |
224 | 245k | ctx->buffer[59] = ctx->lo >> 24; |
225 | 245k | ctx->buffer[60] = ctx->hi; |
226 | 245k | ctx->buffer[61] = ctx->hi >> 8; |
227 | 245k | ctx->buffer[62] = ctx->hi >> 16; |
228 | 245k | ctx->buffer[63] = ctx->hi >> 24; |
229 | | |
230 | 245k | body(ctx, ctx->buffer, 64); |
231 | | |
232 | 245k | result[0] = ctx->a; |
233 | 245k | result[1] = ctx->a >> 8; |
234 | 245k | result[2] = ctx->a >> 16; |
235 | 245k | result[3] = ctx->a >> 24; |
236 | 245k | result[4] = ctx->b; |
237 | 245k | result[5] = ctx->b >> 8; |
238 | 245k | result[6] = ctx->b >> 16; |
239 | 245k | result[7] = ctx->b >> 24; |
240 | 245k | result[8] = ctx->c; |
241 | 245k | result[9] = ctx->c >> 8; |
242 | 245k | result[10] = ctx->c >> 16; |
243 | 245k | result[11] = ctx->c >> 24; |
244 | 245k | result[12] = ctx->d; |
245 | 245k | result[13] = ctx->d >> 8; |
246 | 245k | result[14] = ctx->d >> 16; |
247 | 245k | result[15] = ctx->d >> 24; |
248 | | |
249 | 245k | i_zero_safe(ctx); |
250 | 245k | } |
251 | | |
252 | | void md4_get_digest(const void *data, size_t size, |
253 | | unsigned char result[STATIC_ARRAY MD4_RESULTLEN]) |
254 | 0 | { |
255 | 0 | struct md4_context ctx; |
256 | |
|
257 | 0 | md4_init(&ctx); |
258 | 0 | md4_update(&ctx, data, size); |
259 | 0 | md4_final(&ctx, result); |
260 | 0 | } |
261 | | |
262 | | static void hash_method_init_md4(void *context) |
263 | 0 | { |
264 | 0 | md4_init(context); |
265 | 0 | } |
266 | | static void hash_method_loop_md4(void *context, const void *data, size_t size) |
267 | 0 | { |
268 | 0 | md4_update(context, data, size); |
269 | 0 | } |
270 | | |
271 | | static void hash_method_result_md4(void *context, unsigned char *result_r) |
272 | 0 | { |
273 | 0 | md4_final(context, result_r); |
274 | 0 | } |
275 | | |
276 | | const struct hash_method hash_method_md4 = { |
277 | | .name = "md4", |
278 | | .block_size = 64, /* block size is 512 bits */ |
279 | | .context_size = sizeof(struct md4_context), |
280 | | .digest_size = MD4_RESULTLEN, |
281 | | |
282 | | .init = hash_method_init_md4, |
283 | | .loop = hash_method_loop_md4, |
284 | | .result = hash_method_result_md4, |
285 | | }; |