Coverage Report

Created: 2026-12-01 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dovecot/src/lib/md5.c
Line
Count
Source
1
/*
2
 * This is an OpenSSL-compatible implementation of the RSA Data Security,
3
 * Inc. MD5 Message-Digest Algorithm.
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 "md5.h"
21
22
/*
23
 * The basic MD5 functions.
24
 *
25
 * F is optimized compared to its RFC 1321 definition just like in Colin
26
 * Plumb's implementation.
27
 */
28
0
#define F(x, y, z)      ((z) ^ ((x) & ((y) ^ (z))))
29
0
#define G(x, y, z)      ((y) ^ ((z) & ((x) ^ (y))))
30
0
#define H(x, y, z)      ((x) ^ (y) ^ (z))
31
0
#define I(x, y, z)      ((y) ^ ((x) | ~(z)))
32
33
/*
34
 * The MD5 transformation for all four rounds.
35
 */
36
#define STEP(f, a, b, c, d, x, t, s) \
37
0
  (a) += f((b), (c), (d)) + (x) + (t); \
38
0
  (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
39
0
  (a) += (b);
40
41
#define SET(n) \
42
  (ctx->block[(n)] = \
43
  (uint_fast32_t)ptr[(n) * 4] | \
44
  ((uint_fast32_t)ptr[(n) * 4 + 1] << 8) | \
45
  ((uint_fast32_t)ptr[(n) * 4 + 2] << 16) | \
46
  ((uint_fast32_t)ptr[(n) * 4 + 3] << 24))
47
#define GET(n) \
48
  (ctx->block[(n)])
49
50
/*
51
 * This processes one or more 64-byte data blocks, but does NOT update
52
 * the bit counters.  There're no alignment requirements.
53
 */
54
static const void * ATTR_NOWARN_UNUSED_RESULT ATTR_UNSIGNED_WRAPS
55
        ATTR_NO_SANITIZE_UNDEFINED ATTR_NO_SANITIZE_INTEGER
56
        ATTR_NO_SANITIZE_IMPLICIT_CONVERSION
57
body(struct md5_context *ctx, const void *data, size_t size)
58
0
{
59
0
  const unsigned char *ptr;
60
0
  uint_fast32_t a, b, c, d;
61
0
  uint_fast32_t saved_a, saved_b, saved_c, saved_d;
62
63
0
  ptr = data;
64
65
0
  a = ctx->a;
66
0
  b = ctx->b;
67
0
  c = ctx->c;
68
0
  d = ctx->d;
69
70
0
  do {
71
0
    saved_a = a;
72
0
    saved_b = b;
73
0
    saved_c = c;
74
0
    saved_d = d;
75
76
/* Round 1 */
77
0
    STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
78
0
    STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
79
0
    STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
80
0
    STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
81
0
    STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
82
0
    STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
83
0
    STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
84
0
    STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
85
0
    STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
86
0
    STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
87
0
    STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
88
0
    STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
89
0
    STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
90
0
    STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
91
0
    STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
92
0
    STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
93
94
/* Round 2 */
95
0
    STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
96
0
    STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
97
0
    STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
98
0
    STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
99
0
    STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
100
0
    STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
101
0
    STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
102
0
    STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
103
0
    STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
104
0
    STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
105
0
    STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
106
0
    STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
107
0
    STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
108
0
    STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
109
0
    STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
110
0
    STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
111
112
/* Round 3 */
113
0
    STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
114
0
    STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
115
0
    STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
116
0
    STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
117
0
    STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
118
0
    STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
119
0
    STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
120
0
    STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
121
0
    STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
122
0
    STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
123
0
    STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
124
0
    STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
125
0
    STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
126
0
    STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
127
0
    STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
128
0
    STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
129
130
/* Round 4 */
131
0
    STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
132
0
    STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
133
0
    STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
134
0
    STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
135
0
    STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
136
0
    STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
137
0
    STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
138
0
    STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
139
0
    STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
140
0
    STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
141
0
    STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
142
0
    STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
143
0
    STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
144
0
    STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
145
0
    STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
146
0
    STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
147
148
0
    a += saved_a;
149
0
    b += saved_b;
150
0
    c += saved_c;
151
0
    d += saved_d;
152
153
0
    ptr += 64;
154
0
  } while ((size -= 64) != 0);
155
156
0
  ctx->a = a;
157
0
  ctx->b = b;
158
0
  ctx->c = c;
159
0
  ctx->d = d;
160
161
0
  return ptr;
162
0
}
163
164
void md5_init(struct md5_context *ctx)
165
0
{
166
0
  ctx->a = 0x67452301;
167
0
  ctx->b = 0xefcdab89;
168
0
  ctx->c = 0x98badcfe;
169
0
  ctx->d = 0x10325476;
170
171
0
  ctx->lo = 0;
172
0
  ctx->hi = 0;
173
0
  memset(ctx->block, 0, sizeof(ctx->block));
174
0
}
175
176
void ATTR_UNSIGNED_WRAPS
177
md5_update(struct md5_context *ctx, const void *data, size_t size)
178
0
{
179
  /* @UNSAFE */
180
0
  uint_fast32_t saved_lo;
181
0
  unsigned long used, free;
182
183
0
  if (size == 0)
184
0
    return;
185
186
0
  saved_lo = ctx->lo;
187
0
  if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
188
0
    ctx->hi++;
189
0
  ctx->hi += size >> 29;
190
191
0
  used = saved_lo & 0x3f;
192
193
0
  if (used != 0) {
194
0
    free = 64 - used;
195
196
0
    if (size < free) {
197
0
      memcpy(&ctx->buffer[used], data, size);
198
0
      return;
199
0
    }
200
201
0
    memcpy(&ctx->buffer[used], data, free);
202
0
    data = (const unsigned char *) data + free;
203
0
    size -= free;
204
0
    body(ctx, ctx->buffer, 64);
205
0
  }
206
207
0
  if (size >= 64) {
208
0
    data = body(ctx, data, size & ~0x3fUL);
209
0
    size &= 0x3f;
210
0
  }
211
212
0
  memcpy(ctx->buffer, data, size);
213
0
}
214
215
void ATTR_UNSIGNED_WRAPS ATTR_NO_SANITIZE_UNDEFINED
216
  ATTR_NO_SANITIZE_INTEGER ATTR_NO_SANITIZE_IMPLICIT_CONVERSION
217
md5_final(struct md5_context *ctx, unsigned char result[STATIC_ARRAY MD5_RESULTLEN])
218
0
{
219
  /* @UNSAFE */
220
0
  unsigned long used, free;
221
222
0
  used = ctx->lo & 0x3f;
223
224
0
  ctx->buffer[used++] = 0x80;
225
226
0
  free = 64 - used;
227
228
0
  if (free < 8) {
229
0
    memset(&ctx->buffer[used], 0, free);
230
0
    body(ctx, ctx->buffer, 64);
231
0
    used = 0;
232
0
    free = 64;
233
0
  }
234
235
0
  memset(&ctx->buffer[used], 0, free - 8);
236
237
0
  ctx->lo <<= 3;
238
0
  ctx->buffer[56] = ctx->lo;
239
0
  ctx->buffer[57] = ctx->lo >> 8;
240
0
  ctx->buffer[58] = ctx->lo >> 16;
241
0
  ctx->buffer[59] = ctx->lo >> 24;
242
0
  ctx->buffer[60] = ctx->hi;
243
0
  ctx->buffer[61] = ctx->hi >> 8;
244
0
  ctx->buffer[62] = ctx->hi >> 16;
245
0
  ctx->buffer[63] = ctx->hi >> 24;
246
247
0
  body(ctx, ctx->buffer, 64);
248
249
0
  result[0] = ctx->a;
250
0
  result[1] = ctx->a >> 8;
251
0
  result[2] = ctx->a >> 16;
252
0
  result[3] = ctx->a >> 24;
253
0
  result[4] = ctx->b;
254
0
  result[5] = ctx->b >> 8;
255
0
  result[6] = ctx->b >> 16;
256
0
  result[7] = ctx->b >> 24;
257
0
  result[8] = ctx->c;
258
0
  result[9] = ctx->c >> 8;
259
0
  result[10] = ctx->c >> 16;
260
0
  result[11] = ctx->c >> 24;
261
0
  result[12] = ctx->d;
262
0
  result[13] = ctx->d >> 8;
263
0
  result[14] = ctx->d >> 16;
264
0
  result[15] = ctx->d >> 24;
265
266
0
  i_zero_safe(ctx);
267
0
}
268
269
void md5_get_digest(const void *data, size_t size,
270
        unsigned char result[STATIC_ARRAY MD5_RESULTLEN])
271
0
{
272
0
  struct md5_context ctx;
273
274
0
  md5_init(&ctx);
275
0
  md5_update(&ctx, data, size);
276
0
  md5_final(&ctx, result);
277
0
}
278
279
static void hash_method_init_md5(void *context)
280
0
{
281
0
  md5_init(context);
282
0
}
283
static void hash_method_loop_md5(void *context, const void *data, size_t size)
284
0
{
285
0
  md5_update(context, data, size);
286
0
}
287
288
static void hash_method_result_md5(void *context, unsigned char *result_r)
289
0
{
290
0
  md5_final(context, result_r);
291
0
}
292
293
const struct hash_method hash_method_md5 = {
294
  .name = "md5",
295
  .block_size = 64, /* block size is 512 bits */
296
  .context_size = sizeof(struct md5_context),
297
  .digest_size = MD5_RESULTLEN,
298
299
  .init = hash_method_init_md5,
300
  .loop = hash_method_loop_md5,
301
  .result = hash_method_result_md5,
302
};