Coverage Report

Created: 2025-07-12 06:23

/src/njs/external/njs_md5.c
Line
Count
Source (jump to first uncovered line)
1
2
/*
3
 * An internal implementation, based on Alexander Peslyak's
4
 * public domain implementation:
5
 * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5
6
 */
7
8
9
#include <njs_unix.h>
10
#include <njs_types.h>
11
#include <njs_clang.h>
12
#include <njs_str.h>
13
#include "njs_hash.h"
14
15
16
static const u_char *njs_md5_body(njs_hash_t *ctx, const u_char *data,
17
    size_t size);
18
19
20
void
21
njs_md5_init(njs_hash_t *ctx)
22
0
{
23
0
    ctx->a = 0x67452301;
24
0
    ctx->b = 0xefcdab89;
25
0
    ctx->c = 0x98badcfe;
26
0
    ctx->d = 0x10325476;
27
28
0
    ctx->bytes = 0;
29
0
}
30
31
32
void
33
njs_md5_update(njs_hash_t *ctx, const void *data, size_t size)
34
0
{
35
0
    size_t  used, free;
36
37
0
    used = (size_t) (ctx->bytes & 0x3f);
38
0
    ctx->bytes += size;
39
40
0
    if (used) {
41
0
        free = 64 - used;
42
43
0
        if (size < free) {
44
0
            memcpy(&ctx->buffer[used], data, size);
45
0
            return;
46
0
        }
47
48
0
        memcpy(&ctx->buffer[used], data, free);
49
0
        data = (u_char *) data + free;
50
0
        size -= free;
51
0
        (void) njs_md5_body(ctx, ctx->buffer, 64);
52
0
    }
53
54
0
    if (size >= 64) {
55
0
        data = njs_md5_body(ctx, data, size & ~(size_t) 0x3f);
56
0
        size &= 0x3f;
57
0
    }
58
59
0
    memcpy(ctx->buffer, data, size);
60
0
}
61
62
63
void
64
njs_md5_final(u_char result[32], njs_hash_t *ctx)
65
0
{
66
0
    size_t  used, free;
67
68
0
    used = (size_t) (ctx->bytes & 0x3f);
69
70
0
    ctx->buffer[used++] = 0x80;
71
72
0
    free = 64 - used;
73
74
0
    if (free < 8) {
75
0
        njs_memzero(&ctx->buffer[used], free);
76
0
        (void) njs_md5_body(ctx, ctx->buffer, 64);
77
0
        used = 0;
78
0
        free = 64;
79
0
    }
80
81
0
    njs_memzero(&ctx->buffer[used], free - 8);
82
83
0
    ctx->bytes <<= 3;
84
0
    ctx->buffer[56] = (u_char)  ctx->bytes;
85
0
    ctx->buffer[57] = (u_char) (ctx->bytes >> 8);
86
0
    ctx->buffer[58] = (u_char) (ctx->bytes >> 16);
87
0
    ctx->buffer[59] = (u_char) (ctx->bytes >> 24);
88
0
    ctx->buffer[60] = (u_char) (ctx->bytes >> 32);
89
0
    ctx->buffer[61] = (u_char) (ctx->bytes >> 40);
90
0
    ctx->buffer[62] = (u_char) (ctx->bytes >> 48);
91
0
    ctx->buffer[63] = (u_char) (ctx->bytes >> 56);
92
93
0
    (void) njs_md5_body(ctx, ctx->buffer, 64);
94
95
0
    result[0]  = (u_char)  ctx->a;
96
0
    result[1]  = (u_char) (ctx->a >> 8);
97
0
    result[2]  = (u_char) (ctx->a >> 16);
98
0
    result[3]  = (u_char) (ctx->a >> 24);
99
0
    result[4]  = (u_char)  ctx->b;
100
0
    result[5]  = (u_char) (ctx->b >> 8);
101
0
    result[6]  = (u_char) (ctx->b >> 16);
102
0
    result[7]  = (u_char) (ctx->b >> 24);
103
0
    result[8]  = (u_char)  ctx->c;
104
0
    result[9]  = (u_char) (ctx->c >> 8);
105
0
    result[10] = (u_char) (ctx->c >> 16);
106
0
    result[11] = (u_char) (ctx->c >> 24);
107
0
    result[12] = (u_char)  ctx->d;
108
0
    result[13] = (u_char) (ctx->d >> 8);
109
0
    result[14] = (u_char) (ctx->d >> 16);
110
0
    result[15] = (u_char) (ctx->d >> 24);
111
112
0
    njs_explicit_memzero(ctx, sizeof(*ctx));
113
0
}
114
115
116
/*
117
 * The basic MD5 functions.
118
 *
119
 * F and G are optimized compared to their RFC 1321 definitions for
120
 * architectures that lack an AND-NOT instruction, just like in
121
 * Colin Plumb's implementation.
122
 */
123
124
0
#define F(x, y, z)  ((z) ^ ((x) & ((y) ^ (z))))
125
0
#define G(x, y, z)  ((y) ^ ((z) & ((x) ^ (y))))
126
0
#define H(x, y, z)  ((x) ^ (y) ^ (z))
127
0
#define I(x, y, z)  ((y) ^ ((x) | ~(z)))
128
129
/*
130
 * The MD5 transformation for all four rounds.
131
 */
132
133
#define STEP(f, a, b, c, d, x, t, s)                                          \
134
0
    (a) += f((b), (c), (d)) + (x) + (t);                                      \
135
0
    (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s))));                \
136
0
    (a) += (b)
137
138
/*
139
 * SET() reads 4 input bytes in little-endian byte order and stores them
140
 * in a properly aligned word in host byte order.
141
 */
142
143
#define SET(n)                                                                \
144
    (block[n] =                                                               \
145
     (   (uint32_t) p[n * 4]                                                  \
146
      | ((uint32_t) p[n * 4 + 1] << 8)                                        \
147
      | ((uint32_t) p[n * 4 + 2] << 16)                                       \
148
      | ((uint32_t) p[n * 4 + 3] << 24)))                                     \
149
150
#define GET(n)      block[n]
151
152
153
/*
154
 * This processes one or more 64-byte data blocks, but does not update
155
 * the bit counters.  There are no alignment requirements.
156
 */
157
158
static const u_char *
159
njs_md5_body(njs_hash_t *ctx, const u_char *data, size_t size)
160
0
{
161
0
    uint32_t       a, b, c, d;
162
0
    uint32_t       saved_a, saved_b, saved_c, saved_d;
163
0
    const u_char   *p;
164
0
    uint32_t       block[16];
165
166
0
    p = data;
167
168
0
    a = ctx->a;
169
0
    b = ctx->b;
170
0
    c = ctx->c;
171
0
    d = ctx->d;
172
173
0
    do {
174
0
        saved_a = a;
175
0
        saved_b = b;
176
0
        saved_c = c;
177
0
        saved_d = d;
178
179
        /* Round 1 */
180
181
0
        STEP(F, a, b, c, d, SET(0),  0xd76aa478, 7);
182
0
        STEP(F, d, a, b, c, SET(1),  0xe8c7b756, 12);
183
0
        STEP(F, c, d, a, b, SET(2),  0x242070db, 17);
184
0
        STEP(F, b, c, d, a, SET(3),  0xc1bdceee, 22);
185
0
        STEP(F, a, b, c, d, SET(4),  0xf57c0faf, 7);
186
0
        STEP(F, d, a, b, c, SET(5),  0x4787c62a, 12);
187
0
        STEP(F, c, d, a, b, SET(6),  0xa8304613, 17);
188
0
        STEP(F, b, c, d, a, SET(7),  0xfd469501, 22);
189
0
        STEP(F, a, b, c, d, SET(8),  0x698098d8, 7);
190
0
        STEP(F, d, a, b, c, SET(9),  0x8b44f7af, 12);
191
0
        STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17);
192
0
        STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22);
193
0
        STEP(F, a, b, c, d, SET(12), 0x6b901122, 7);
194
0
        STEP(F, d, a, b, c, SET(13), 0xfd987193, 12);
195
0
        STEP(F, c, d, a, b, SET(14), 0xa679438e, 17);
196
0
        STEP(F, b, c, d, a, SET(15), 0x49b40821, 22);
197
198
        /* Round 2 */
199
200
0
        STEP(G, a, b, c, d, GET(1),  0xf61e2562, 5);
201
0
        STEP(G, d, a, b, c, GET(6),  0xc040b340, 9);
202
0
        STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14);
203
0
        STEP(G, b, c, d, a, GET(0),  0xe9b6c7aa, 20);
204
0
        STEP(G, a, b, c, d, GET(5),  0xd62f105d, 5);
205
0
        STEP(G, d, a, b, c, GET(10), 0x02441453, 9);
206
0
        STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14);
207
0
        STEP(G, b, c, d, a, GET(4),  0xe7d3fbc8, 20);
208
0
        STEP(G, a, b, c, d, GET(9),  0x21e1cde6, 5);
209
0
        STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9);
210
0
        STEP(G, c, d, a, b, GET(3),  0xf4d50d87, 14);
211
0
        STEP(G, b, c, d, a, GET(8),  0x455a14ed, 20);
212
0
        STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5);
213
0
        STEP(G, d, a, b, c, GET(2),  0xfcefa3f8, 9);
214
0
        STEP(G, c, d, a, b, GET(7),  0x676f02d9, 14);
215
0
        STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20);
216
217
        /* Round 3 */
218
219
0
        STEP(H, a, b, c, d, GET(5),  0xfffa3942, 4);
220
0
        STEP(H, d, a, b, c, GET(8),  0x8771f681, 11);
221
0
        STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16);
222
0
        STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23);
223
0
        STEP(H, a, b, c, d, GET(1),  0xa4beea44, 4);
224
0
        STEP(H, d, a, b, c, GET(4),  0x4bdecfa9, 11);
225
0
        STEP(H, c, d, a, b, GET(7),  0xf6bb4b60, 16);
226
0
        STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23);
227
0
        STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4);
228
0
        STEP(H, d, a, b, c, GET(0),  0xeaa127fa, 11);
229
0
        STEP(H, c, d, a, b, GET(3),  0xd4ef3085, 16);
230
0
        STEP(H, b, c, d, a, GET(6),  0x04881d05, 23);
231
0
        STEP(H, a, b, c, d, GET(9),  0xd9d4d039, 4);
232
0
        STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11);
233
0
        STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16);
234
0
        STEP(H, b, c, d, a, GET(2),  0xc4ac5665, 23);
235
236
        /* Round 4 */
237
238
0
        STEP(I, a, b, c, d, GET(0),  0xf4292244, 6);
239
0
        STEP(I, d, a, b, c, GET(7),  0x432aff97, 10);
240
0
        STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15);
241
0
        STEP(I, b, c, d, a, GET(5),  0xfc93a039, 21);
242
0
        STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6);
243
0
        STEP(I, d, a, b, c, GET(3),  0x8f0ccc92, 10);
244
0
        STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15);
245
0
        STEP(I, b, c, d, a, GET(1),  0x85845dd1, 21);
246
0
        STEP(I, a, b, c, d, GET(8),  0x6fa87e4f, 6);
247
0
        STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10);
248
0
        STEP(I, c, d, a, b, GET(6),  0xa3014314, 15);
249
0
        STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21);
250
0
        STEP(I, a, b, c, d, GET(4),  0xf7537e82, 6);
251
0
        STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10);
252
0
        STEP(I, c, d, a, b, GET(2),  0x2ad7d2bb, 15);
253
0
        STEP(I, b, c, d, a, GET(9),  0xeb86d391, 21);
254
255
0
        a += saved_a;
256
0
        b += saved_b;
257
0
        c += saved_c;
258
0
        d += saved_d;
259
260
0
        p += 64;
261
262
0
    } while (size -= 64);
263
264
0
    ctx->a = a;
265
0
    ctx->b = b;
266
0
    ctx->c = c;
267
0
    ctx->d = d;
268
269
0
    return p;
270
0
}