Blender V4.3
hash_md5.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 1995 Free Software Foundation, Inc.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 * Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>. */
5
13#include <cstdio>
14#include <cstdlib>
15#include <cstring>
16#include <sys/types.h>
17
18#include "BLI_hash_md5.hh" /* own include */
19
20#if defined HAVE_LIMITS_H || defined _LIBC
21# include <limits.h>
22#endif
23
24/* The following contortions are an attempt to use the C preprocessor to determine an unsigned
25 * integral type that is 32 bits wide.
26 * An alternative approach is to use autoconf's AC_CHECK_SIZEOF macro, but doing that would require
27 * that the configure script compile and *run* the resulting executable.
28 * Locally running cross-compiled executables is usually not possible.
29 */
30
31#if defined __STDC__ && __STDC__
32# define UINT_MAX_32_BITS 4294967295U
33#else
34# define UINT_MAX_32_BITS 0xFFFFFFFF
35#endif
36
37/* If UINT_MAX isn't defined, assume it's a 32-bit type.
38 * This should be valid for all systems GNU cares about
39 * because that doesn't include 16-bit systems, and only modern systems
40 * (that certainly have <limits.h>) have 64+-bit integral types.
41 */
42
43#ifndef UINT_MAX
44# define UINT_MAX UINT_MAX_32_BITS
45#endif
46
47#if UINT_MAX == UINT_MAX_32_BITS
48typedef unsigned int md5_uint32;
49#else
50# if USHRT_MAX == UINT_MAX_32_BITS
51typedef unsigned short md5_uint32;
52# else
53# if ULONG_MAX == UINT_MAX_32_BITS
54typedef unsigned long md5_uint32;
55# else
56/* The following line is intended to evoke an error. Using #error is not portable enough. */
57"Cannot determine unsigned 32-bit data type."
58# endif
59# endif
60#endif
61
62/* Following code is low level, upon which are built up the functions
63 * 'BLI_hash_md5_stream' and 'BLI_hash_md5_buffer'. */
64
65/* Structure to save state of computation between the single steps. */
72
73#ifdef __BIG_ENDIAN__
74# define SWAP(n) (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24))
75#else
76# define SWAP(n) (n)
77#endif
78
79/* This array contains the bytes used to pad the buffer to the next 64-byte boundary.
80 * (RFC 1321, 3.1: Step 1) */
81static const uchar fillbuf[64] = {0x80, 0 /* , 0, 0, ... */};
82
87static void md5_init_ctx(md5_ctx *ctx)
88{
89 ctx->A = 0x67452301;
90 ctx->B = 0xefcdab89;
91 ctx->C = 0x98badcfe;
92 ctx->D = 0x10325476;
93}
94
100static void md5_process_block(const void *buffer, size_t len, md5_ctx *ctx)
101{
102/* These are the four functions used in the four steps of the MD5 algorithm and defined in the
103 * RFC 1321. The first function is a little bit optimized
104 * (as found in Colin Plumbs public domain implementation).
105 */
106// #define FF(b, c, d) ((b & c) | (~b & d))
107#define FF(b, c, d) (d ^ (b & (c ^ d)))
108#define FG(b, c, d) FF(d, b, c)
109#define FH(b, c, d) (b ^ c ^ d)
110#define FI(b, c, d) (c ^ (b | ~d))
111
112/* It is unfortunate that C does not provide an operator for cyclic rotation.
113 * Hope the C compiler is smart enough. */
114#define CYCLIC(w, s) (w = (w << s) | (w >> (32 - s)))
115
116 md5_uint32 correct_words[16];
117 const md5_uint32 *words = static_cast<const md5_uint32 *>(buffer);
118 size_t nwords = len / sizeof(md5_uint32);
119 const md5_uint32 *endp = words + nwords;
120 md5_uint32 A = ctx->A;
121 md5_uint32 B = ctx->B;
122 md5_uint32 C = ctx->C;
123 md5_uint32 D = ctx->D;
124
125 /* Process all bytes in the buffer with 64 bytes in each round of the loop. */
126 while (words < endp) {
127 md5_uint32 *cwp = correct_words;
128 md5_uint32 A_save = A;
129 md5_uint32 B_save = B;
130 md5_uint32 C_save = C;
131 md5_uint32 D_save = D;
132
133 /* First round: using the given function, the context and a constant the next context is
134 * computed. Because the algorithms processing unit is a 32-bit word and it is determined
135 * to work on words in little endian byte order we perhaps have to change the byte order
136 * before the computation. To reduce the work for the next steps we store the swapped words
137 * in the array CORRECT_WORDS.
138 */
139#define OP(a, b, c, d, s, T) \
140 a += FF(b, c, d) + (*cwp++ = SWAP(*words)) + T; \
141 words++; \
142 CYCLIC(a, s); \
143 a += b; \
144 (void)0
145
146 /* Before we start, one word to the strange constants. They are defined in RFC 1321 as:
147 * `T[i] = (int) (4294967296.0 * fabs (sin (i))), i=1..64`
148 */
149
150 /* Round 1. */
151 OP(A, B, C, D, 7, 0xd76aa478);
152 OP(D, A, B, C, 12, 0xe8c7b756);
153 OP(C, D, A, B, 17, 0x242070db);
154 OP(B, C, D, A, 22, 0xc1bdceee);
155 OP(A, B, C, D, 7, 0xf57c0faf);
156 OP(D, A, B, C, 12, 0x4787c62a);
157 OP(C, D, A, B, 17, 0xa8304613);
158 OP(B, C, D, A, 22, 0xfd469501);
159 OP(A, B, C, D, 7, 0x698098d8);
160 OP(D, A, B, C, 12, 0x8b44f7af);
161 OP(C, D, A, B, 17, 0xffff5bb1);
162 OP(B, C, D, A, 22, 0x895cd7be);
163 OP(A, B, C, D, 7, 0x6b901122);
164 OP(D, A, B, C, 12, 0xfd987193);
165 OP(C, D, A, B, 17, 0xa679438e);
166 OP(B, C, D, A, 22, 0x49b40821);
167
168#undef OP
169
170 /* For the second to fourth round we have the possibly swapped words in CORRECT_WORDS.
171 * Redefine the macro to take an additional first argument specifying the function to use.
172 */
173#define OP(f, a, b, c, d, k, s, T) \
174 a += f(b, c, d) + correct_words[k] + T; \
175 CYCLIC(a, s); \
176 a += b; \
177 (void)0
178
179 /* Round 2. */
180 OP(FG, A, B, C, D, 1, 5, 0xf61e2562);
181 OP(FG, D, A, B, C, 6, 9, 0xc040b340);
182 OP(FG, C, D, A, B, 11, 14, 0x265e5a51);
183 OP(FG, B, C, D, A, 0, 20, 0xe9b6c7aa);
184 OP(FG, A, B, C, D, 5, 5, 0xd62f105d);
185 OP(FG, D, A, B, C, 10, 9, 0x02441453);
186 OP(FG, C, D, A, B, 15, 14, 0xd8a1e681);
187 OP(FG, B, C, D, A, 4, 20, 0xe7d3fbc8);
188 OP(FG, A, B, C, D, 9, 5, 0x21e1cde6);
189 OP(FG, D, A, B, C, 14, 9, 0xc33707d6);
190 OP(FG, C, D, A, B, 3, 14, 0xf4d50d87);
191 OP(FG, B, C, D, A, 8, 20, 0x455a14ed);
192 OP(FG, A, B, C, D, 13, 5, 0xa9e3e905);
193 OP(FG, D, A, B, C, 2, 9, 0xfcefa3f8);
194 OP(FG, C, D, A, B, 7, 14, 0x676f02d9);
195 OP(FG, B, C, D, A, 12, 20, 0x8d2a4c8a);
196
197 /* Round 3. */
198 OP(FH, A, B, C, D, 5, 4, 0xfffa3942);
199 OP(FH, D, A, B, C, 8, 11, 0x8771f681);
200 OP(FH, C, D, A, B, 11, 16, 0x6d9d6122);
201 OP(FH, B, C, D, A, 14, 23, 0xfde5380c);
202 OP(FH, A, B, C, D, 1, 4, 0xa4beea44);
203 OP(FH, D, A, B, C, 4, 11, 0x4bdecfa9);
204 OP(FH, C, D, A, B, 7, 16, 0xf6bb4b60);
205 OP(FH, B, C, D, A, 10, 23, 0xbebfbc70);
206 OP(FH, A, B, C, D, 13, 4, 0x289b7ec6);
207 OP(FH, D, A, B, C, 0, 11, 0xeaa127fa);
208 OP(FH, C, D, A, B, 3, 16, 0xd4ef3085);
209 OP(FH, B, C, D, A, 6, 23, 0x04881d05);
210 OP(FH, A, B, C, D, 9, 4, 0xd9d4d039);
211 OP(FH, D, A, B, C, 12, 11, 0xe6db99e5);
212 OP(FH, C, D, A, B, 15, 16, 0x1fa27cf8);
213 OP(FH, B, C, D, A, 2, 23, 0xc4ac5665);
214
215 /* Round 4. */
216 OP(FI, A, B, C, D, 0, 6, 0xf4292244);
217 OP(FI, D, A, B, C, 7, 10, 0x432aff97);
218 OP(FI, C, D, A, B, 14, 15, 0xab9423a7);
219 OP(FI, B, C, D, A, 5, 21, 0xfc93a039);
220 OP(FI, A, B, C, D, 12, 6, 0x655b59c3);
221 OP(FI, D, A, B, C, 3, 10, 0x8f0ccc92);
222 OP(FI, C, D, A, B, 10, 15, 0xffeff47d);
223 OP(FI, B, C, D, A, 1, 21, 0x85845dd1);
224 OP(FI, A, B, C, D, 8, 6, 0x6fa87e4f);
225 OP(FI, D, A, B, C, 15, 10, 0xfe2ce6e0);
226 OP(FI, C, D, A, B, 6, 15, 0xa3014314);
227 OP(FI, B, C, D, A, 13, 21, 0x4e0811a1);
228 OP(FI, A, B, C, D, 4, 6, 0xf7537e82);
229 OP(FI, D, A, B, C, 11, 10, 0xbd3af235);
230 OP(FI, C, D, A, B, 2, 15, 0x2ad7d2bb);
231 OP(FI, B, C, D, A, 9, 21, 0xeb86d391);
232
233#undef OP
234
235 /* Add the starting values of the context. */
236 A += A_save;
237 B += B_save;
238 C += C_save;
239 D += D_save;
240 }
241
242 /* Put checksum in context given as argument. */
243 ctx->A = A;
244 ctx->B = B;
245 ctx->C = C;
246 ctx->D = D;
247
248#undef FF
249#undef FG
250#undef FH
251#undef FI
252#undef CYCLIC
253}
254
260static void *md5_read_ctx(const md5_ctx *ctx, void *resbuf)
261{
262 md5_uint32 *digest = static_cast<md5_uint32 *>(resbuf);
263 digest[0] = SWAP(ctx->A);
264 digest[1] = SWAP(ctx->B);
265 digest[2] = SWAP(ctx->C);
266 digest[3] = SWAP(ctx->D);
267
268 return resbuf;
269}
270
271/* Top level public functions. */
272
273int BLI_hash_md5_stream(FILE *stream, void *resblock)
274{
275#define BLOCKSIZE 4096 /* IMPORTANT: must be a multiple of 64. */
276 md5_ctx ctx;
277 md5_uint32 len[2];
278 char buffer[BLOCKSIZE + 72];
279 size_t pad, sum;
280
281 /* Initialize the computation context. */
282 md5_init_ctx(&ctx);
283
284 len[0] = 0;
285 len[1] = 0;
286
287 /* Iterate over full file contents. */
288 while (true) {
289 /* We read the file in blocks of BLOCKSIZE bytes.
290 * One call of the computation function processes the whole buffer
291 * so that with the next round of the loop another block can be read.
292 */
293 size_t n;
294 sum = 0;
295
296 /* Read block. Take care for partial reads. */
297 do {
298 n = fread(buffer, 1, BLOCKSIZE - sum, stream);
299 sum += n;
300 } while (sum < BLOCKSIZE && n != 0);
301
302 if (n == 0 && ferror(stream)) {
303 return 1;
304 }
305
306 /* RFC 1321 specifies the possible length of the file up to 2^64 bits.
307 * Here we only compute the number of bytes. Do a double word increment.
308 */
309 len[0] += sum;
310 if (len[0] < sum) {
311 ++len[1];
312 }
313
314 /* If end of file is reached, end the loop. */
315 if (n == 0) {
316 break;
317 }
318
319 /* Process buffer with BLOCKSIZE bytes. Note that `BLOCKSIZE % 64 == 0`. */
320 md5_process_block(buffer, BLOCKSIZE, &ctx);
321 }
322
323 /* We can copy 64 bytes because the buffer is always big enough.
324 * 'fillbuf' contains the needed bits. */
325 memcpy(&buffer[sum], fillbuf, 64);
326
327 /* Compute amount of padding bytes needed. Alignment is done to `(N + PAD) % 64 == 56`.
328 * There is always at least one byte padded, i.e. if the alignment is correctly aligned,
329 * 64 padding bytes are added.
330 */
331 pad = sum & 63;
332 pad = pad >= 56 ? 64 + 56 - pad : 56 - pad;
333
334 /* Put the 64-bit file length in *bits* at the end of the buffer. */
335 *(md5_uint32 *)&buffer[sum + pad] = SWAP(len[0] << 3);
336 *(md5_uint32 *)&buffer[sum + pad + 4] = SWAP((len[1] << 3) | (len[0] >> 29));
337
338 /* Process last bytes. */
339 md5_process_block(buffer, sum + pad + 8, &ctx);
340
341 /* Construct result in desired memory. */
342 md5_read_ctx(&ctx, resblock);
343 return 0;
344}
345
346void *BLI_hash_md5_buffer(const char *buffer, size_t len, void *resblock)
347{
348 md5_ctx ctx;
349 char restbuf[64 + 72];
350 size_t blocks = len & ~63;
351 size_t pad, rest;
352
353 /* Initialize the computation context. */
354 md5_init_ctx(&ctx);
355
356 /* Process whole buffer but last len % 64 bytes. */
357 md5_process_block(buffer, blocks, &ctx);
358
359 /* REST bytes are not processed yet. */
360 rest = len - blocks;
361 /* Copy to own buffer. */
362 memcpy(restbuf, &buffer[blocks], rest);
363 /* Append needed fill bytes at end of buffer.
364 * We can copy 64 bytes because the buffer is always big enough. */
365 memcpy(&restbuf[rest], fillbuf, 64);
366
367 /* PAD bytes are used for padding to correct alignment.
368 * Note that always at least one byte is padded. */
369 pad = rest >= 56 ? 64 + 56 - rest : 56 - rest;
370
371 /* Put length of buffer in *bits* in last eight bytes. */
372 *(md5_uint32 *)&restbuf[rest + pad] = (md5_uint32)SWAP(len << 3);
373 *(md5_uint32 *)&restbuf[rest + pad + 4] = (md5_uint32)SWAP(len >> 29);
374
375 /* Process last bytes. */
376 md5_process_block(restbuf, rest + pad + 8, &ctx);
377
378 /* Put result in desired memory area. */
379 return md5_read_ctx(&ctx, resblock);
380}
381
382char *BLI_hash_md5_to_hexdigest(const void *resblock, char r_hex_digest[33])
383{
384 static const char hex_map[17] = "0123456789abcdef";
385 const uchar *p;
386 char *q;
387 short len;
388
389 for (q = r_hex_digest, p = (const uchar *)resblock, len = 0; len < 16; p++, len++) {
390 const uchar c = *p;
391 *q++ = hex_map[c >> 4];
392 *q++ = hex_map[c & 15];
393 }
394 *q = '\0';
395
396 return r_hex_digest;
397}
#define D
unsigned char uchar
#define C
Definition RandGen.cpp:29
int pad[32 - sizeof(int)]
#define A
static T sum(const btAlignedObjectArray< T > &items)
int len
static const uchar fillbuf[64]
Definition hash_md5.cc:81
#define FH(b, c, d)
#define FG(b, c, d)
static void * md5_read_ctx(const md5_ctx *ctx, void *resbuf)
Definition hash_md5.cc:260
#define OP(a, b, c, d, s, T)
char * BLI_hash_md5_to_hexdigest(const void *resblock, char r_hex_digest[33])
Definition hash_md5.cc:382
static void md5_init_ctx(md5_ctx *ctx)
Definition hash_md5.cc:87
unsigned int md5_uint32
Definition hash_md5.cc:48
int BLI_hash_md5_stream(FILE *stream, void *resblock)
Definition hash_md5.cc:273
#define FI(b, c, d)
#define SWAP(n)
Definition hash_md5.cc:76
static void md5_process_block(const void *buffer, size_t len, md5_ctx *ctx)
Definition hash_md5.cc:100
void * BLI_hash_md5_buffer(const char *buffer, size_t len, void *resblock)
Definition hash_md5.cc:346
#define BLOCKSIZE
#define B
md5_uint32 B
Definition hash_md5.cc:68
md5_uint32 C
Definition hash_md5.cc:69
md5_uint32 A
Definition hash_md5.cc:67
md5_uint32 D
Definition hash_md5.cc:70