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