Coverage Report

Created: 2026-01-09 06:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
0
#define F(x, y, z)  ((z) ^ ((x) & ((y) ^ (z))))
26
0
#define G(x, y, z)  (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
27
0
#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
0
  (a) += f((b), (c), (d)) + (x);   \
34
0
  (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
0
{
55
0
  const unsigned char *ptr;
56
0
  uint32_t a, b, c, d;
57
0
  uint32_t saved_a, saved_b, saved_c, saved_d;
58
59
0
  ptr = data;
60
61
0
  a = ctx->a;
62
0
  b = ctx->b;
63
0
  c = ctx->c;
64
0
  d = ctx->d;
65
66
0
  do {
67
0
    saved_a = a;
68
0
    saved_b = b;
69
0
    saved_c = c;
70
0
    saved_d = d;
71
72
/* Round 1 */
73
0
    STEP(F, a, b, c, d, SET( 0),  3);
74
0
    STEP(F, d, a, b, c, SET( 1),  7);
75
0
    STEP(F, c, d, a, b, SET( 2), 11);
76
0
    STEP(F, b, c, d, a, SET( 3), 19);
77
78
0
    STEP(F, a, b, c, d, SET( 4),  3);
79
0
    STEP(F, d, a, b, c, SET( 5),  7);
80
0
    STEP(F, c, d, a, b, SET( 6), 11);
81
0
    STEP(F, b, c, d, a, SET( 7), 19);
82
83
0
    STEP(F, a, b, c, d, SET( 8),  3);
84
0
    STEP(F, d, a, b, c, SET( 9),  7);
85
0
    STEP(F, c, d, a, b, SET(10), 11);
86
0
    STEP(F, b, c, d, a, SET(11), 19);
87
88
0
    STEP(F, a, b, c, d, SET(12),  3);
89
0
    STEP(F, d, a, b, c, SET(13),  7);
90
0
    STEP(F, c, d, a, b, SET(14), 11);
91
0
    STEP(F, b, c, d, a, SET(15), 19);
92
/* Round 2 */
93
0
    STEP(G, a, b, c, d, GET( 0) + 0x5A827999,  3);
94
0
    STEP(G, d, a, b, c, GET( 4) + 0x5A827999,  5);
95
0
    STEP(G, c, d, a, b, GET( 8) + 0x5A827999,  9);
96
0
    STEP(G, b, c, d, a, GET(12) + 0x5A827999, 13);
97
98
0
    STEP(G, a, b, c, d, GET( 1) + 0x5A827999,  3);
99
0
    STEP(G, d, a, b, c, GET( 5) + 0x5A827999,  5);
100
0
    STEP(G, c, d, a, b, GET( 9) + 0x5A827999,  9);
101
0
    STEP(G, b, c, d, a, GET(13) + 0x5A827999, 13);
102
103
0
    STEP(G, a, b, c, d, GET( 2) + 0x5A827999,  3);
104
0
    STEP(G, d, a, b, c, GET( 6) + 0x5A827999,  5);
105
0
    STEP(G, c, d, a, b, GET(10) + 0x5A827999,  9);
106
0
    STEP(G, b, c, d, a, GET(14) + 0x5A827999, 13);
107
108
0
    STEP(G, a, b, c, d, GET( 3) + 0x5A827999,  3);
109
0
    STEP(G, d, a, b, c, GET( 7) + 0x5A827999,  5);
110
0
    STEP(G, c, d, a, b, GET(11) + 0x5A827999,  9);
111
0
    STEP(G, b, c, d, a, GET(15) + 0x5A827999, 13);
112
/* Round 3 */
113
0
    STEP(H, a, b, c, d, GET( 0) + 0x6ED9EBA1,  3);
114
0
    STEP(H, d, a, b, c, GET( 8) + 0x6ED9EBA1,  9);
115
0
    STEP(H, c, d, a, b, GET( 4) + 0x6ED9EBA1, 11);
116
0
    STEP(H, b, c, d, a, GET(12) + 0x6ED9EBA1, 15);
117
118
0
    STEP(H, a, b, c, d, GET( 2) + 0x6ED9EBA1,  3);
119
0
    STEP(H, d, a, b, c, GET(10) + 0x6ED9EBA1,  9);
120
0
    STEP(H, c, d, a, b, GET( 6) + 0x6ED9EBA1, 11);
121
0
    STEP(H, b, c, d, a, GET(14) + 0x6ED9EBA1, 15);
122
123
0
    STEP(H, a, b, c, d, GET( 1) + 0x6ED9EBA1,  3);
124
0
    STEP(H, d, a, b, c, GET( 9) + 0x6ED9EBA1,  9);
125
0
    STEP(H, c, d, a, b, GET( 5) + 0x6ED9EBA1, 11);
126
0
    STEP(H, b, c, d, a, GET(13) + 0x6ED9EBA1, 15);
127
128
0
    STEP(H, a, b, c, d, GET( 3) + 0x6ED9EBA1,  3);
129
0
    STEP(H, d, a, b, c, GET(11) + 0x6ED9EBA1,  9);
130
0
    STEP(H, c, d, a, b, GET( 7) + 0x6ED9EBA1, 11);
131
0
    STEP(H, b, c, d, a, GET(15) + 0x6ED9EBA1, 15);
132
133
0
    a += saved_a;
134
0
    b += saved_b;
135
0
    c += saved_c;
136
0
    d += saved_d;
137
138
0
    ptr += 64;
139
0
  } while ((size -= 64) != 0);
140
141
0
  ctx->a = a;
142
0
  ctx->b = b;
143
0
  ctx->c = c;
144
0
  ctx->d = d;
145
146
0
  return ptr;
147
0
}
148
149
void md4_init(struct md4_context *ctx)
150
0
{
151
0
  ctx->a = 0x67452301;
152
0
  ctx->b = 0xefcdab89;
153
0
  ctx->c = 0x98badcfe;
154
0
  ctx->d = 0x10325476;
155
156
0
  ctx->lo = 0;
157
0
  ctx->hi = 0;
158
0
}
159
160
void md4_update(struct md4_context *ctx, const void *data, size_t size)
161
0
{
162
  /* @UNSAFE */
163
0
  uint_fast32_t saved_lo;
164
0
  unsigned long used, free;
165
166
0
  if (size == 0)
167
0
    return;
168
169
0
  saved_lo = ctx->lo;
170
0
  if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo)
171
0
    ctx->hi++;
172
0
  ctx->hi += size >> 29;
173
174
0
  used = saved_lo & 0x3f;
175
176
0
  if (used != 0) {
177
0
    free = 64 - used;
178
179
0
    if (size < free) {
180
0
      memcpy(&ctx->buffer[used], data, size);
181
0
      return;
182
0
    }
183
184
0
    memcpy(&ctx->buffer[used], data, free);
185
0
    data = (const unsigned char *) data + free;
186
0
    size -= free;
187
0
    body(ctx, ctx->buffer, 64);
188
0
  }
189
190
0
  if (size >= 64) {
191
0
    data = body(ctx, data, size & ~0x3fUL);
192
0
    size &= 0x3f;
193
0
  }
194
195
0
  memcpy(ctx->buffer, data, size);
196
0
}
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
0
{
202
  /* @UNSAFE */
203
0
  unsigned long used, free;
204
205
0
  used = ctx->lo & 0x3f;
206
207
0
  ctx->buffer[used++] = 0x80;
208
209
0
  free = 64 - used;
210
211
0
  if (free < 8) {
212
0
    memset(&ctx->buffer[used], 0, free);
213
0
    body(ctx, ctx->buffer, 64);
214
0
    used = 0;
215
0
    free = 64;
216
0
  }
217
218
0
  memset(&ctx->buffer[used], 0, free - 8);
219
220
0
  ctx->lo <<= 3;
221
0
  ctx->buffer[56] = ctx->lo;
222
0
  ctx->buffer[57] = ctx->lo >> 8;
223
0
  ctx->buffer[58] = ctx->lo >> 16;
224
0
  ctx->buffer[59] = ctx->lo >> 24;
225
0
  ctx->buffer[60] = ctx->hi;
226
0
  ctx->buffer[61] = ctx->hi >> 8;
227
0
  ctx->buffer[62] = ctx->hi >> 16;
228
0
  ctx->buffer[63] = ctx->hi >> 24;
229
230
0
  body(ctx, ctx->buffer, 64);
231
232
0
  result[0] = ctx->a;
233
0
  result[1] = ctx->a >> 8;
234
0
  result[2] = ctx->a >> 16;
235
0
  result[3] = ctx->a >> 24;
236
0
  result[4] = ctx->b;
237
0
  result[5] = ctx->b >> 8;
238
0
  result[6] = ctx->b >> 16;
239
0
  result[7] = ctx->b >> 24;
240
0
  result[8] = ctx->c;
241
0
  result[9] = ctx->c >> 8;
242
0
  result[10] = ctx->c >> 16;
243
0
  result[11] = ctx->c >> 24;
244
0
  result[12] = ctx->d;
245
0
  result[13] = ctx->d >> 8;
246
0
  result[14] = ctx->d >> 16;
247
0
  result[15] = ctx->d >> 24;
248
249
0
  i_zero_safe(ctx);
250
0
}
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
};