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