NeoMutt  2024-11-14-34-g5aaf0d
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
md5.c File Reference

Calculate the MD5 checksum of a buffer. More...

#include "config.h"
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "md5.h"
+ Include dependency graph for md5.c:

Go to the source code of this file.

Macros

#define SWAP(n)   (n)
 
#define FF(b, c, d)   (d ^ (b & (c ^ d)))
 
#define FG(b, c, d)   FF(d, b, c)
 
#define FH(b, c, d)   (b ^ c ^ d)
 
#define FI(b, c, d)   (c ^ (b | ~d))
 
#define OP(a, b, c, d, s, T)
 
#define CYCLIC(w, s)   (w = (w << s) | (w >> (32 - s)))
 
#define OP(f, a, b, c, d, k, s, T)
 
#define alignof(type)
 
#define UNALIGNED_P(p)   (((size_t) p) % alignof(md5_uint32) != 0)
 

Functions

static void mutt_md5_process_block (const void *buffer, size_t len, struct Md5Ctx *md5ctx)
 Process a block with MD5.
 
static void set_uint32 (char *cp, md5_uint32 v)
 Write a 32 bit number.
 
static void * mutt_md5_read_ctx (const struct Md5Ctx *md5ctx, void *resbuf)
 Read from the context into a buffer.
 
void mutt_md5_init_ctx (struct Md5Ctx *md5ctx)
 Initialise the MD5 computation.
 
void * mutt_md5_finish_ctx (struct Md5Ctx *md5ctx, void *resbuf)
 Process the remaining bytes in the buffer.
 
void * mutt_md5 (const char *str, void *buf)
 Calculate the MD5 hash of a NULL-terminated string.
 
void * mutt_md5_bytes (const void *buffer, size_t len, void *resbuf)
 Calculate the MD5 hash of a buffer.
 
void mutt_md5_process (const char *str, struct Md5Ctx *md5ctx)
 Process a NULL-terminated string.
 
void mutt_md5_process_bytes (const void *buf, size_t buflen, struct Md5Ctx *md5ctx)
 Process a block of data.
 
void mutt_md5_toascii (const void *digest, char *resbuf)
 Convert a binary MD5 digest into ASCII Hexadecimal.
 

Variables

static const unsigned char fillbuf [64] = { 0x80, 0 }
 This array contains the bytes used to pad the buffer to the next 64-byte boundary.
 

Detailed Description

Calculate the MD5 checksum of a buffer.

Authors
  • Ulrich Drepper
  • Free Software Foundation, Inc.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file md5.c.

Macro Definition Documentation

◆ SWAP

#define SWAP (   n)    (n)

Definition at line 41 of file md5.c.

◆ FF

#define FF (   b,
  c,
 
)    (d ^ (b & (c ^ d)))

Definition at line 51 of file md5.c.

◆ FG

#define FG (   b,
  c,
 
)    FF(d, b, c)

Definition at line 52 of file md5.c.

◆ FH

#define FH (   b,
  c,
 
)    (b ^ c ^ d)

Definition at line 53 of file md5.c.

◆ FI

#define FI (   b,
  c,
 
)    (c ^ (b | ~d))

Definition at line 54 of file md5.c.

◆ OP [1/2]

#define OP (   a,
  b,
  c,
  d,
  s,
 
)
Value:
do \
{ \
a += FF(b, c, d) + (*cwp++ = SWAP(*words)) + T; \
words++; \
CYCLIC(a, s); \
a += b; \
} while (false)
#define FF(b, c, d)
Definition: md5.c:51
#define SWAP(n)
Definition: md5.c:41

◆ CYCLIC

#define CYCLIC (   w,
 
)    (w = (w << s) | (w >> (32 - s)))

◆ OP [2/2]

#define OP (   f,
  a,
  b,
  c,
  d,
  k,
  s,
 
)
Value:
do \
{ \
a += f(b, c, d) + correct_words[k] + T; \
CYCLIC(a, s); \
a += b; \
} while (false)

◆ alignof

#define alignof (   type)
Value:
offsetof( \
struct { \
char c; \
type x; \
}, \
x)

◆ UNALIGNED_P

#define UNALIGNED_P (   p)    (((size_t) p) % alignof(md5_uint32) != 0)

Function Documentation

◆ mutt_md5_process_block()

static void mutt_md5_process_block ( const void *  buffer,
size_t  len,
struct Md5Ctx md5ctx 
)
static

Process a block with MD5.

Parameters
bufferBuffer to hash
lenLength of buffer
md5ctxMD5 context

Process LEN bytes of Buffer, accumulating context into MD5CTX. LEN must be a multiple of 64.

Definition at line 65 of file md5.c.

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}
#define FH(b, c, d)
Definition: md5.c:53
#define FG(b, c, d)
Definition: md5.c:52
#define OP(a, b, c, d, s, T)
#define FI(b, c, d)
Definition: md5.c:54
uint32_t md5_uint32
Definition: md5.h:29
#define C
md5_uint32 total[2]
Definition: md5.h:43
md5_uint32 D
Definition: md5.h:41
md5_uint32 C
Definition: md5.h:40
md5_uint32 A
Definition: md5.h:38
md5_uint32 B
Definition: md5.h:39
+ Here is the caller graph for this function:

◆ set_uint32()

static void set_uint32 ( char *  cp,
md5_uint32  v 
)
inlinestatic

Write a 32 bit number.

Parameters
cpDestination for data
vValue to write

Copy the 4 byte value from v into the memory location pointed to by *cp, If your architecture allows unaligned access this is equivalent to *(md5_uint32*) cp = v

Definition at line 226 of file md5.c.

227{
228 memcpy(cp, &v, sizeof(v));
229}
+ Here is the caller graph for this function:

◆ mutt_md5_read_ctx()

static void * mutt_md5_read_ctx ( const struct Md5Ctx md5ctx,
void *  resbuf 
)
static

Read from the context into a buffer.

Parameters
md5ctxMD5 context
resbufBuffer for result
Return values
ptrResults buffer

Put result from MD5CTX in first 16 bytes following RESBUF. The result must be in little endian byte order.

Definition at line 240 of file md5.c.

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}
static void set_uint32(char *cp, md5_uint32 v)
Write a 32 bit number.
Definition: md5.c:226
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_md5_init_ctx()

void mutt_md5_init_ctx ( struct Md5Ctx md5ctx)

Initialise the MD5 computation.

Parameters
md5ctxMD5 context

RFC1321, 3.3: Step 3

Definition at line 261 of file md5.c.

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}
md5_uint32 buflen
Definition: md5.h:44
+ Here is the caller graph for this function:

◆ mutt_md5_finish_ctx()

void * mutt_md5_finish_ctx ( struct Md5Ctx md5ctx,
void *  resbuf 
)

Process the remaining bytes in the buffer.

Parameters
md5ctxMD5 context
resbufBuffer for result
Return values
ptrResults buffer

Process the remaining bytes in the internal buffer and the usual prologue according to the standard and write the result to RESBUF.

Definition at line 285 of file md5.c.

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}
static void * mutt_md5_read_ctx(const struct Md5Ctx *md5ctx, void *resbuf)
Read from the context into a buffer.
Definition: md5.c:240
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 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
md5_uint32 buffer[32]
Definition: md5.h:45
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_md5()

void * mutt_md5 ( const char *  str,
void *  buf 
)

Calculate the MD5 hash of a NULL-terminated string.

Parameters
strString to hash
bufBuffer for result
Return values
ptrResults buffer

Definition at line 317 of file md5.c.

318{
319 if (!str)
320 return NULL;
321
322 return mutt_md5_bytes(str, strlen(str), buf);
323}
void * mutt_md5_bytes(const void *buffer, size_t len, void *resbuf)
Calculate the MD5 hash of a buffer.
Definition: md5.c:336
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_md5_bytes()

void * mutt_md5_bytes ( const void *  buffer,
size_t  len,
void *  resbuf 
)

Calculate the MD5 hash of a buffer.

Parameters
bufferBuffer to hash
lenLength of buffer
resbufBuffer for result
Return values
ptrResults buffer

Compute MD5 message digest for LEN bytes beginning at Buffer. The result is always in little endian byte order, so that a byte-wise output yields to the wanted ASCII representation of the message digest.

Definition at line 336 of file md5.c.

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}
void mutt_md5_process_bytes(const void *buf, size_t buflen, struct Md5Ctx *md5ctx)
Process a block of data.
Definition: md5.c:373
void mutt_md5_init_ctx(struct Md5Ctx *md5ctx)
Initialise the MD5 computation.
Definition: md5.c:261
void * mutt_md5_finish_ctx(struct Md5Ctx *md5ctx, void *resbuf)
Process the remaining bytes in the buffer.
Definition: md5.c:285
Cursor for the MD5 hashing.
Definition: md5.h:37
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_md5_process()

void mutt_md5_process ( const char *  str,
struct Md5Ctx md5ctx 
)

Process a NULL-terminated string.

Parameters
strString to process
md5ctxMD5 context

Definition at line 355 of file md5.c.

356{
357 if (!str)
358 return;
359
360 mutt_md5_process_bytes(str, strlen(str), md5ctx);
361}
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_md5_process_bytes()

void mutt_md5_process_bytes ( const void *  buf,
size_t  buflen,
struct Md5Ctx md5ctx 
)

Process a block of data.

Parameters
bufBuffer to process
buflenLength of buffer
md5ctxMD5 context

Starting with the result of former calls of this function (or the initialization function update the context for the next BUFLEN bytes starting at Buffer. It is NOT required that BUFLEN is a multiple of 64.

Definition at line 373 of file md5.c.

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}
#define UNALIGNED_P(p)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_md5_toascii()

void mutt_md5_toascii ( const void *  digest,
char *  resbuf 
)

Convert a binary MD5 digest into ASCII Hexadecimal.

Parameters
digestBinary MD5 digest
resbufBuffer for the ASCII result
Note
refbuf must be at least 33 bytes long.

Definition at line 456 of file md5.c.

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}
+ Here is the caller graph for this function:

Variable Documentation

◆ fillbuf

const unsigned char fillbuf[64] = { 0x80, 0 }
static

This array contains the bytes used to pad the buffer to the next 64-byte boundary.

(RFC1321, 3.1: Step 1)

Definition at line 46 of file md5.c.