NeoMutt  2024-10-02-37-gfa9146
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
md5.c
Go to the documentation of this file.
1
30#include "config.h"
31#include <stdbool.h>
32#include <stddef.h> // IWYU pragma: keep
33#include <stdio.h>
34#include <string.h>
35#include "md5.h"
36
37#ifdef WORDS_BIGENDIAN
38#define SWAP(n) \
39 (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
40#else
41#define SWAP(n) (n)
42#endif
43
46static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
47
48/* These are the four functions used in the four steps of the MD5 algorithm
49 * and defined in the RFC1321. The first function is a little bit optimized
50 * (as found in Colin Plumbs public domain implementation). */
51#define FF(b, c, d) (d ^ (b & (c ^ d)))
52#define FG(b, c, d) FF(d, b, c)
53#define FH(b, c, d) (b ^ c ^ d)
54#define FI(b, c, d) (c ^ (b | ~d))
55
65static void mutt_md5_process_block(const void *buffer, size_t len, struct Md5Ctx *md5ctx)
66{
67 md5_uint32 correct_words[16];
68 const md5_uint32 *words = buffer;
69 size_t nwords = len / sizeof(md5_uint32);
70 const md5_uint32 *endp = words + nwords;
71 md5_uint32 A = md5ctx->A;
72 md5_uint32 B = md5ctx->B;
73 md5_uint32 C = md5ctx->C;
74 md5_uint32 D = md5ctx->D;
75
76 /* First increment the byte count. RFC1321 specifies the possible length of
77 * the file up to 2^64 bits. Here we only compute the number of bytes. Do a
78 * double word increment. */
79 md5ctx->total[0] += len;
80 if (md5ctx->total[0] < len)
81 md5ctx->total[1]++; // LCOV_EXCL_LINE
82
83 /* Process all bytes in the buffer with 64 bytes in each round of the loop. */
84 while (words < endp)
85 {
86 md5_uint32 *cwp = correct_words;
87 md5_uint32 save_A = A;
88 md5_uint32 save_B = B;
89 md5_uint32 save_C = C;
90 md5_uint32 save_D = D;
91
92 /* First round: using the given function, the context and a constant the
93 * next context is computed. Because the algorithms processing unit is a
94 * 32-bit word and it is determined to work on words in little endian byte
95 * order we perhaps have to change the byte order before the computation.
96 * To reduce the work for the next steps we store the swapped words in the
97 * array CORRECT_WORDS. */
98
99#define OP(a, b, c, d, s, T) \
100 do \
101 { \
102 a += FF(b, c, d) + (*cwp++ = SWAP(*words)) + T; \
103 words++; \
104 CYCLIC(a, s); \
105 a += b; \
106 } while (false)
107
108/* It is unfortunate that C does not provide an operator for
109 * cyclic rotation. Hope the C compiler is smart enough. */
110#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
111
112 /* Before we start, one word to the strange constants.
113 * They are defined in RFC1321 as
114 * T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64
115 * Here is an equivalent invocation using Perl:
116 * perl -e 'foreach(1..64){printf "0x%08x\n", int (4294967296 * abs (sin $_))}'
117 */
118
119 /* Round 1. */
120 OP(A, B, C, D, 7, 0xd76aa478);
121 OP(D, A, B, C, 12, 0xe8c7b756);
122 OP(C, D, A, B, 17, 0x242070db);
123 OP(B, C, D, A, 22, 0xc1bdceee);
124 OP(A, B, C, D, 7, 0xf57c0faf);
125 OP(D, A, B, C, 12, 0x4787c62a);
126 OP(C, D, A, B, 17, 0xa8304613);
127 OP(B, C, D, A, 22, 0xfd469501);
128 OP(A, B, C, D, 7, 0x698098d8);
129 OP(D, A, B, C, 12, 0x8b44f7af);
130 OP(C, D, A, B, 17, 0xffff5bb1);
131 OP(B, C, D, A, 22, 0x895cd7be);
132 OP(A, B, C, D, 7, 0x6b901122);
133 OP(D, A, B, C, 12, 0xfd987193);
134 OP(C, D, A, B, 17, 0xa679438e);
135 OP(B, C, D, A, 22, 0x49b40821);
136
137/* For the second to fourth round we have the possibly swapped words
138 * in CORRECT_WORDS. Redefine the macro to take an additional first
139 * argument specifying the function to use. */
140#undef OP
141#define OP(f, a, b, c, d, k, s, T) \
142 do \
143 { \
144 a += f(b, c, d) + correct_words[k] + T; \
145 CYCLIC(a, s); \
146 a += b; \
147 } while (false)
148
149 /* Round 2. */
150 OP(FG, A, B, C, D, 1, 5, 0xf61e2562);
151 OP(FG, D, A, B, C, 6, 9, 0xc040b340);
152 OP(FG, C, D, A, B, 11, 14, 0x265e5a51);
153 OP(FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
154 OP(FG, A, B, C, D, 5, 5, 0xd62f105d);
155 OP(FG, D, A, B, C, 10, 9, 0x02441453);
156 OP(FG, C, D, A, B, 15, 14, 0xd8a1e681);
157 OP(FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
158 OP(FG, A, B, C, D, 9, 5, 0x21e1cde6);
159 OP(FG, D, A, B, C, 14, 9, 0xc33707d6);
160 OP(FG, C, D, A, B, 3, 14, 0xf4d50d87);
161 OP(FG, B, C, D, A, 8, 20, 0x455a14ed);
162 OP(FG, A, B, C, D, 13, 5, 0xa9e3e905);
163 OP(FG, D, A, B, C, 2, 9, 0xfcefa3f8);
164 OP(FG, C, D, A, B, 7, 14, 0x676f02d9);
165 OP(FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
166
167 /* Round 3. */
168 OP(FH, A, B, C, D, 5, 4, 0xfffa3942);
169 OP(FH, D, A, B, C, 8, 11, 0x8771f681);
170 OP(FH, C, D, A, B, 11, 16, 0x6d9d6122);
171 OP(FH, B, C, D, A, 14, 23, 0xfde5380c);
172 OP(FH, A, B, C, D, 1, 4, 0xa4beea44);
173 OP(FH, D, A, B, C, 4, 11, 0x4bdecfa9);
174 OP(FH, C, D, A, B, 7, 16, 0xf6bb4b60);
175 OP(FH, B, C, D, A, 10, 23, 0xbebfbc70);
176 OP(FH, A, B, C, D, 13, 4, 0x289b7ec6);
177 OP(FH, D, A, B, C, 0, 11, 0xeaa127fa);
178 OP(FH, C, D, A, B, 3, 16, 0xd4ef3085);
179 OP(FH, B, C, D, A, 6, 23, 0x04881d05);
180 OP(FH, A, B, C, D, 9, 4, 0xd9d4d039);
181 OP(FH, D, A, B, C, 12, 11, 0xe6db99e5);
182 OP(FH, C, D, A, B, 15, 16, 0x1fa27cf8);
183 OP(FH, B, C, D, A, 2, 23, 0xc4ac5665);
184
185 /* Round 4. */
186 OP(FI, A, B, C, D, 0, 6, 0xf4292244);
187 OP(FI, D, A, B, C, 7, 10, 0x432aff97);
188 OP(FI, C, D, A, B, 14, 15, 0xab9423a7);
189 OP(FI, B, C, D, A, 5, 21, 0xfc93a039);
190 OP(FI, A, B, C, D, 12, 6, 0x655b59c3);
191 OP(FI, D, A, B, C, 3, 10, 0x8f0ccc92);
192 OP(FI, C, D, A, B, 10, 15, 0xffeff47d);
193 OP(FI, B, C, D, A, 1, 21, 0x85845dd1);
194 OP(FI, A, B, C, D, 8, 6, 0x6fa87e4f);
195 OP(FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
196 OP(FI, C, D, A, B, 6, 15, 0xa3014314);
197 OP(FI, B, C, D, A, 13, 21, 0x4e0811a1);
198 OP(FI, A, B, C, D, 4, 6, 0xf7537e82);
199 OP(FI, D, A, B, C, 11, 10, 0xbd3af235);
200 OP(FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
201 OP(FI, B, C, D, A, 9, 21, 0xeb86d391);
202
203 /* Add the starting values of the context. */
204 A += save_A;
205 B += save_B;
206 C += save_C;
207 D += save_D;
208 }
209
210 /* Put checksum in context given as argument. */
211 md5ctx->A = A;
212 md5ctx->B = B;
213 md5ctx->C = C;
214 md5ctx->D = D;
215}
216
226static inline void set_uint32(char *cp, md5_uint32 v)
227{
228 memcpy(cp, &v, sizeof(v));
229}
230
240static void *mutt_md5_read_ctx(const struct Md5Ctx *md5ctx, void *resbuf)
241{
242 if (!md5ctx || !resbuf)
243 return NULL;
244
245 char *r = resbuf;
246
247 set_uint32(r + 0 * sizeof(md5ctx->A), SWAP(md5ctx->A));
248 set_uint32(r + 1 * sizeof(md5ctx->B), SWAP(md5ctx->B));
249 set_uint32(r + 2 * sizeof(md5ctx->C), SWAP(md5ctx->C));
250 set_uint32(r + 3 * sizeof(md5ctx->D), SWAP(md5ctx->D));
251
252 return resbuf;
253}
254
261void mutt_md5_init_ctx(struct Md5Ctx *md5ctx)
262{
263 if (!md5ctx)
264 return;
265
266 md5ctx->A = 0x67452301;
267 md5ctx->B = 0xefcdab89;
268 md5ctx->C = 0x98badcfe;
269 md5ctx->D = 0x10325476;
270
271 md5ctx->total[0] = 0;
272 md5ctx->total[1] = 0;
273 md5ctx->buflen = 0;
274}
275
285void *mutt_md5_finish_ctx(struct Md5Ctx *md5ctx, void *resbuf)
286{
287 if (!md5ctx)
288 return NULL;
289
290 /* Take yet unprocessed bytes into account. */
291 md5_uint32 bytes = md5ctx->buflen;
292 size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4;
293
294 /* Now count remaining bytes. */
295 md5ctx->total[0] += bytes;
296 if (md5ctx->total[0] < bytes)
297 md5ctx->total[1]++; // LCOV_EXCL_LINE
298
299 /* Put the 64-bit file length in *bits* at the end of the buffer. */
300 md5ctx->buffer[size - 2] = SWAP(md5ctx->total[0] << 3);
301 md5ctx->buffer[size - 1] = SWAP((md5ctx->total[1] << 3) | (md5ctx->total[0] >> 29));
302
303 memcpy(&((char *) md5ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes);
304
305 /* Process last bytes. */
306 mutt_md5_process_block(md5ctx->buffer, size * 4, md5ctx);
307
308 return mutt_md5_read_ctx(md5ctx, resbuf);
309}
310
317void *mutt_md5(const char *str, void *buf)
318{
319 if (!str)
320 return NULL;
321
322 return mutt_md5_bytes(str, strlen(str), buf);
323}
324
336void *mutt_md5_bytes(const void *buffer, size_t len, void *resbuf)
337{
338 struct Md5Ctx md5ctx = { 0 };
339
340 /* Initialize the computation context. */
341 mutt_md5_init_ctx(&md5ctx);
342
343 /* Process whole buffer but last len % 64 bytes. */
344 mutt_md5_process_bytes(buffer, len, &md5ctx);
345
346 /* Put result in desired memory area. */
347 return mutt_md5_finish_ctx(&md5ctx, resbuf);
348}
349
355void mutt_md5_process(const char *str, struct Md5Ctx *md5ctx)
356{
357 if (!str)
358 return;
359
360 mutt_md5_process_bytes(str, strlen(str), md5ctx);
361}
362
373void mutt_md5_process_bytes(const void *buf, size_t buflen, struct Md5Ctx *md5ctx)
374{
375 if (!buf || !md5ctx)
376 return;
377
378 /* When we already have some bits in our internal buffer concatenate both
379 * inputs first. */
380 if (md5ctx->buflen != 0)
381 {
382 size_t left_over = md5ctx->buflen;
383 size_t add = ((128 - left_over) > buflen) ? buflen : (128 - left_over);
384
385 memcpy(&((char *) md5ctx->buffer)[left_over], buf, add);
386 md5ctx->buflen += add;
387
388 if (md5ctx->buflen > 64)
389 {
390 mutt_md5_process_block(md5ctx->buffer, md5ctx->buflen & ~63, md5ctx);
391
392 md5ctx->buflen &= 63;
393 /* The regions in the following copy operation can't overlap. */
394 memcpy(md5ctx->buffer, &((char *) md5ctx->buffer)[(left_over + add) & ~63],
395 md5ctx->buflen);
396 }
397
398 buf = (const char *) buf + add;
399 buflen -= add;
400 }
401
402 /* Process available complete blocks. */
403 if (buflen >= 64)
404 {
405#if !defined(_STRING_ARCH_unaligned)
406#define alignof(type) \
407 offsetof( \
408 struct { \
409 char c; \
410 type x; \
411 }, \
412 x)
413#define UNALIGNED_P(p) (((size_t) p) % alignof(md5_uint32) != 0)
414 if (UNALIGNED_P(buf))
415 {
416 while (buflen > 64)
417 {
418 mutt_md5_process_block(memcpy(md5ctx->buffer, buf, 64), 64, md5ctx);
419 buf = (const char *) buf + 64;
420 buflen -= 64;
421 }
422 }
423 else
424#endif
425 {
426 mutt_md5_process_block(buf, buflen & ~63, md5ctx);
427 buf = (const char *) buf + (buflen & ~63);
428 buflen &= 63;
429 }
430 }
431
432 /* Move remaining bytes in internal buffer. */
433 if (buflen > 0)
434 {
435 size_t left_over = md5ctx->buflen;
436
437 memcpy(&((char *) md5ctx->buffer)[left_over], buf, buflen);
438 left_over += buflen;
439 if (left_over >= 64)
440 { // LCOV_EXCL_START
441 mutt_md5_process_block(md5ctx->buffer, 64, md5ctx);
442 left_over -= 64;
443 memmove(md5ctx->buffer, &md5ctx->buffer[16], left_over);
444 } // LCOV_EXCL_STOP
445 md5ctx->buflen = left_over;
446 }
447}
448
456void mutt_md5_toascii(const void *digest, char *resbuf)
457{
458 if (!digest || !resbuf)
459 return;
460
461 const unsigned char *c = digest;
462 sprintf(resbuf, "%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
463 c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], c[10],
464 c[11], c[12], c[13], c[14], c[15]);
465}
#define UNALIGNED_P(p)
void * mutt_md5(const char *str, void *buf)
Calculate the MD5 hash of a NULL-terminated string.
Definition: md5.c:317
#define FH(b, c, d)
Definition: md5.c:53
void mutt_md5_process_bytes(const void *buf, size_t buflen, struct Md5Ctx *md5ctx)
Process a block of data.
Definition: md5.c:373
#define FG(b, c, d)
Definition: md5.c:52
static void * mutt_md5_read_ctx(const struct Md5Ctx *md5ctx, void *resbuf)
Read from the context into a buffer.
Definition: md5.c:240
#define OP(a, b, c, d, s, T)
void * mutt_md5_bytes(const void *buffer, size_t len, void *resbuf)
Calculate the MD5 hash of a buffer.
Definition: md5.c:336
void mutt_md5_process(const char *str, struct Md5Ctx *md5ctx)
Process a NULL-terminated string.
Definition: md5.c:355
static void mutt_md5_process_block(const void *buffer, size_t len, struct Md5Ctx *md5ctx)
Process a block with MD5.
Definition: md5.c:65
static void set_uint32(char *cp, md5_uint32 v)
Write a 32 bit number.
Definition: md5.c:226
void mutt_md5_init_ctx(struct Md5Ctx *md5ctx)
Initialise the MD5 computation.
Definition: md5.c:261
#define FI(b, c, d)
Definition: md5.c:54
void * mutt_md5_finish_ctx(struct Md5Ctx *md5ctx, void *resbuf)
Process the remaining bytes in the buffer.
Definition: md5.c:285
#define SWAP(n)
Definition: md5.c:41
void mutt_md5_toascii(const void *digest, char *resbuf)
Convert a binary MD5 digest into ASCII Hexadecimal.
Definition: md5.c:456
static const unsigned char fillbuf[64]
This array contains the bytes used to pad the buffer to the next 64-byte boundary.
Definition: md5.c:46
Calculate the MD5 checksum of a buffer.
uint32_t md5_uint32
Definition: md5.h:29
#define C
Cursor for the MD5 hashing.
Definition: md5.h:37
md5_uint32 total[2]
Definition: md5.h:43
md5_uint32 D
Definition: md5.h:41
md5_uint32 C
Definition: md5.h:40
md5_uint32 buflen
Definition: md5.h:44
md5_uint32 A
Definition: md5.h:38
md5_uint32 B
Definition: md5.h:39
md5_uint32 buffer[32]
Definition: md5.h:45