exp2l: Work around a NetBSD 10.0/i386 bug.
[gnulib.git] / lib / md4.c
blobec03ffe753a2aaa8c872561db68940bf03fe61c4
1 /* Functions to compute MD4 message digest of files or memory blocks.
2 according to the definition of MD4 in RFC 1320 from April 1992.
3 Copyright (C) 1995-1997, 1999-2003, 2005-2006, 2008-2024 Free Software
4 Foundation, Inc.
6 This file is free software: you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 This file is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>. */
19 /* Adapted by Simon Josefsson from gnulib md5.? and Libgcrypt
20 cipher/md4.c . */
22 #include <config.h>
24 /* Specification. */
25 #include "md4.h"
27 #include <stdint.h>
28 #include <string.h>
29 #include <sys/types.h>
31 #include <byteswap.h>
32 #ifdef WORDS_BIGENDIAN
33 # define SWAP(n) bswap_32 (n)
34 #else
35 # define SWAP(n) (n)
36 #endif
38 /* This array contains the bytes used to pad the buffer to the next
39 64-byte boundary. (RFC 1320, 3.1: Step 1) */
40 static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ };
43 /* Initialize structure containing state of computation.
44 (RFC 1320, 3.3: Step 3) */
45 void
46 md4_init_ctx (struct md4_ctx *ctx)
48 ctx->A = 0x67452301;
49 ctx->B = 0xefcdab89;
50 ctx->C = 0x98badcfe;
51 ctx->D = 0x10325476;
53 ctx->total[0] = ctx->total[1] = 0;
54 ctx->buflen = 0;
57 /* Copy the 4 byte value from v into the memory location pointed to by *cp,
58 If your architecture allows unaligned access this is equivalent to
59 * (uint32_t *) cp = v */
60 static void
61 set_uint32 (char *cp, uint32_t v)
63 memcpy (cp, &v, sizeof v);
66 /* Put result from CTX in first 16 bytes following RESBUF. The result
67 must be in little endian byte order. */
68 void *
69 md4_read_ctx (const struct md4_ctx *ctx, void *resbuf)
71 char *r = resbuf;
72 set_uint32 (r + 0 * sizeof ctx->A, SWAP (ctx->A));
73 set_uint32 (r + 1 * sizeof ctx->B, SWAP (ctx->B));
74 set_uint32 (r + 2 * sizeof ctx->C, SWAP (ctx->C));
75 set_uint32 (r + 3 * sizeof ctx->D, SWAP (ctx->D));
77 return resbuf;
80 /* Process the remaining bytes in the internal buffer and the usual
81 prolog according to the standard and write the result to RESBUF. */
82 void *
83 md4_finish_ctx (struct md4_ctx *ctx, void *resbuf)
85 /* Take yet unprocessed bytes into account. */
86 uint32_t bytes = ctx->buflen;
87 size_t pad;
89 /* Now count remaining bytes. */
90 ctx->total[0] += bytes;
91 if (ctx->total[0] < bytes)
92 ++ctx->total[1];
94 pad = bytes >= 56 ? 64 + 56 - bytes : 56 - bytes;
95 memcpy (&((char*)ctx->buffer)[bytes], fillbuf, pad);
97 /* Put the 64-bit file length in *bits* at the end of the buffer. */
98 ctx->buffer[(bytes + pad) / 4] = SWAP (ctx->total[0] << 3);
99 ctx->buffer[(bytes + pad) / 4 + 1] = SWAP ((ctx->total[1] << 3) |
100 (ctx->total[0] >> 29));
102 /* Process last bytes. */
103 md4_process_block (ctx->buffer, bytes + pad + 8, ctx);
105 return md4_read_ctx (ctx, resbuf);
108 /* Compute MD4 message digest for LEN bytes beginning at BUFFER. The
109 result is always in little endian byte order, so that a byte-wise
110 output yields to the wanted ASCII representation of the message
111 digest. */
112 void *
113 md4_buffer (const char *buffer, size_t len, void *resblock)
115 struct md4_ctx ctx;
117 /* Initialize the computation context. */
118 md4_init_ctx (&ctx);
120 /* Process whole buffer but last len % 64 bytes. */
121 md4_process_bytes (buffer, len, &ctx);
123 /* Put result in desired memory area. */
124 return md4_finish_ctx (&ctx, resblock);
127 void
128 md4_process_bytes (const void *buffer, size_t len, struct md4_ctx *ctx)
130 /* When we already have some bits in our internal buffer concatenate
131 both inputs first. */
132 if (ctx->buflen != 0)
134 size_t left_over = ctx->buflen;
135 size_t add = 128 - left_over > len ? len : 128 - left_over;
137 memcpy (&((char*)ctx->buffer)[left_over], buffer, add);
138 ctx->buflen += add;
140 if (ctx->buflen > 64)
142 md4_process_block (ctx->buffer, ctx->buflen & ~63, ctx);
144 ctx->buflen &= 63;
145 /* The regions in the following copy operation cannot overlap. */
146 memcpy (ctx->buffer, &((char*)ctx->buffer)[(left_over + add) & ~63],
147 ctx->buflen);
150 buffer = (const char *) buffer + add;
151 len -= add;
154 /* Process available complete blocks. */
155 if (len >= 64)
157 #if !(_STRING_ARCH_unaligned || _STRING_INLINE_unaligned)
158 # define UNALIGNED_P(p) ((uintptr_t) (p) % alignof (uint32_t) != 0)
159 if (UNALIGNED_P (buffer))
160 while (len > 64)
162 md4_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx);
163 buffer = (const char *) buffer + 64;
164 len -= 64;
166 else
167 #endif
169 md4_process_block (buffer, len & ~63, ctx);
170 buffer = (const char *) buffer + (len & ~63);
171 len &= 63;
175 /* Move remaining bytes in internal buffer. */
176 if (len > 0)
178 size_t left_over = ctx->buflen;
180 memcpy (&((char*)ctx->buffer)[left_over], buffer, len);
181 left_over += len;
182 if (left_over >= 64)
184 md4_process_block (ctx->buffer, 64, ctx);
185 left_over -= 64;
186 memcpy (ctx->buffer, &ctx->buffer[16], left_over);
188 ctx->buflen = left_over;
192 /* --- Code below is the primary difference between md5.c and md4.c --- */
194 /* MD4 round constants */
195 #define K1 0x5a827999
196 #define K2 0x6ed9eba1
198 /* Round functions. */
199 #define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
200 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
201 #define H(x, y, z) ((x) ^ (y) ^ (z))
202 #define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n))))
203 #define R1(a,b,c,d,k,s) a=rol(a+F(b,c,d)+x[k],s);
204 #define R2(a,b,c,d,k,s) a=rol(a+G(b,c,d)+x[k]+K1,s);
205 #define R3(a,b,c,d,k,s) a=rol(a+H(b,c,d)+x[k]+K2,s);
207 /* Process LEN bytes of BUFFER, accumulating context into CTX.
208 It is assumed that LEN % 64 == 0. */
210 void
211 md4_process_block (const void *buffer, size_t len, struct md4_ctx *ctx)
213 const uint32_t *words = buffer;
214 size_t nwords = len / sizeof (uint32_t);
215 const uint32_t *endp = words + nwords;
216 uint32_t x[16];
217 uint32_t A = ctx->A;
218 uint32_t B = ctx->B;
219 uint32_t C = ctx->C;
220 uint32_t D = ctx->D;
221 uint32_t lolen = len;
223 /* First increment the byte count. RFC 1320 specifies the possible
224 length of the file up to 2^64 bits. Here we only compute the
225 number of bytes. Do a double word increment. */
226 ctx->total[0] += lolen;
227 ctx->total[1] += (len >> 31 >> 1) + (ctx->total[0] < lolen);
229 /* Process all bytes in the buffer with 64 bytes in each round of
230 the loop. */
231 while (words < endp)
233 int t;
234 for (t = 0; t < 16; t++)
236 x[t] = SWAP (*words);
237 words++;
240 /* Round 1. */
241 R1 (A, B, C, D, 0, 3);
242 R1 (D, A, B, C, 1, 7);
243 R1 (C, D, A, B, 2, 11);
244 R1 (B, C, D, A, 3, 19);
245 R1 (A, B, C, D, 4, 3);
246 R1 (D, A, B, C, 5, 7);
247 R1 (C, D, A, B, 6, 11);
248 R1 (B, C, D, A, 7, 19);
249 R1 (A, B, C, D, 8, 3);
250 R1 (D, A, B, C, 9, 7);
251 R1 (C, D, A, B, 10, 11);
252 R1 (B, C, D, A, 11, 19);
253 R1 (A, B, C, D, 12, 3);
254 R1 (D, A, B, C, 13, 7);
255 R1 (C, D, A, B, 14, 11);
256 R1 (B, C, D, A, 15, 19);
258 /* Round 2. */
259 R2 (A, B, C, D, 0, 3);
260 R2 (D, A, B, C, 4, 5);
261 R2 (C, D, A, B, 8, 9);
262 R2 (B, C, D, A, 12, 13);
263 R2 (A, B, C, D, 1, 3);
264 R2 (D, A, B, C, 5, 5);
265 R2 (C, D, A, B, 9, 9);
266 R2 (B, C, D, A, 13, 13);
267 R2 (A, B, C, D, 2, 3);
268 R2 (D, A, B, C, 6, 5);
269 R2 (C, D, A, B, 10, 9);
270 R2 (B, C, D, A, 14, 13);
271 R2 (A, B, C, D, 3, 3);
272 R2 (D, A, B, C, 7, 5);
273 R2 (C, D, A, B, 11, 9);
274 R2 (B, C, D, A, 15, 13);
276 /* Round 3. */
277 R3 (A, B, C, D, 0, 3);
278 R3 (D, A, B, C, 8, 9);
279 R3 (C, D, A, B, 4, 11);
280 R3 (B, C, D, A, 12, 15);
281 R3 (A, B, C, D, 2, 3);
282 R3 (D, A, B, C, 10, 9);
283 R3 (C, D, A, B, 6, 11);
284 R3 (B, C, D, A, 14, 15);
285 R3 (A, B, C, D, 1, 3);
286 R3 (D, A, B, C, 9, 9);
287 R3 (C, D, A, B, 5, 11);
288 R3 (B, C, D, A, 13, 15);
289 R3 (A, B, C, D, 3, 3);
290 R3 (D, A, B, C, 11, 9);
291 R3 (C, D, A, B, 7, 11);
292 R3 (B, C, D, A, 15, 15);
294 A = ctx->A += A;
295 B = ctx->B += B;
296 C = ctx->C += C;
297 D = ctx->D += D;