Coverage Report

Created: 2022-10-14 11:21

/src/php-src/ext/standard/md5.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
   +----------------------------------------------------------------------+
3
   | Copyright (c) The PHP Group                                          |
4
   +----------------------------------------------------------------------+
5
   | This source file is subject to version 3.01 of the PHP license,      |
6
   | that is bundled with this package in the file LICENSE, and is        |
7
   | available through the world-wide-web at the following url:           |
8
   | http://www.php.net/license/3_01.txt                                  |
9
   | If you did not receive a copy of the PHP license and are unable to   |
10
   | obtain it through the world-wide-web, please send a note to          |
11
   | license@php.net so we can mail you a copy immediately.               |
12
   +----------------------------------------------------------------------+
13
   | Author: Alexander Peslyak (Solar Designer) <solar at openwall.com>   |
14
   |         Lachlan Roche                                                |
15
   |         Alessandro Astarita <aleast@capri.it>                        |
16
   +----------------------------------------------------------------------+
17
*/
18
19
#include "php.h"
20
#include "md5.h"
21
22
PHPAPI void make_digest(char *md5str, const unsigned char *digest) /* {{{ */
23
0
{
24
0
  make_digest_ex(md5str, digest, 16);
25
0
}
26
/* }}} */
27
28
PHPAPI void make_digest_ex(char *md5str, const unsigned char *digest, int len) /* {{{ */
29
0
{
30
0
  static const char hexits[17] = "0123456789abcdef";
31
0
  int i;
32
33
0
  for (i = 0; i < len; i++) {
34
0
    md5str[i * 2]       = hexits[digest[i] >> 4];
35
0
    md5str[(i * 2) + 1] = hexits[digest[i] &  0x0F];
36
0
  }
37
0
  md5str[len * 2] = '\0';
38
0
}
39
/* }}} */
40
41
/* {{{ proto string md5(string str, [ bool raw_output])
42
   Calculate the md5 hash of a string */
43
PHP_FUNCTION(md5)
44
0
{
45
0
  zend_string *arg;
46
0
  zend_bool raw_output = 0;
47
0
  PHP_MD5_CTX context;
48
0
  unsigned char digest[16];
49
50
0
  ZEND_PARSE_PARAMETERS_START(1, 2)
51
0
    Z_PARAM_STR(arg)
52
0
    Z_PARAM_OPTIONAL
53
0
    Z_PARAM_BOOL(raw_output)
54
0
  ZEND_PARSE_PARAMETERS_END();
55
56
0
  PHP_MD5Init(&context);
57
0
  PHP_MD5Update(&context, ZSTR_VAL(arg), ZSTR_LEN(arg));
58
0
  PHP_MD5Final(digest, &context);
59
0
  if (raw_output) {
60
0
    RETURN_STRINGL((char *) digest, 16);
61
0
  } else {
62
0
    RETVAL_NEW_STR(zend_string_alloc(32, 0));
63
0
    make_digest_ex(Z_STRVAL_P(return_value), digest, 16);
64
0
  }
65
66
0
}
67
/* }}} */
68
69
/* {{{ proto string md5_file(string filename [, bool raw_output])
70
   Calculate the md5 hash of given filename */
71
PHP_FUNCTION(md5_file)
72
0
{
73
0
  char          *arg;
74
0
  size_t           arg_len;
75
0
  zend_bool raw_output = 0;
76
0
  unsigned char buf[1024];
77
0
  unsigned char digest[16];
78
0
  PHP_MD5_CTX   context;
79
0
  ssize_t       n;
80
0
  php_stream    *stream;
81
82
0
  ZEND_PARSE_PARAMETERS_START(1, 2)
83
0
    Z_PARAM_PATH(arg, arg_len)
84
0
    Z_PARAM_OPTIONAL
85
0
    Z_PARAM_BOOL(raw_output)
86
0
  ZEND_PARSE_PARAMETERS_END();
87
88
0
  stream = php_stream_open_wrapper(arg, "rb", REPORT_ERRORS, NULL);
89
0
  if (!stream) {
90
0
    RETURN_FALSE;
91
0
  }
92
93
0
  PHP_MD5Init(&context);
94
95
0
  while ((n = php_stream_read(stream, (char*)buf, sizeof(buf))) > 0) {
96
0
    PHP_MD5Update(&context, buf, n);
97
0
  }
98
99
  /* XXX this probably can be improved with some number of retries */
100
0
  if (!php_stream_eof(stream)) {
101
0
    php_stream_close(stream);
102
0
    PHP_MD5Final(digest, &context);
103
104
0
    RETURN_FALSE;
105
0
  }
106
107
0
  php_stream_close(stream);
108
109
0
  PHP_MD5Final(digest, &context);
110
111
0
  if (raw_output) {
112
0
    RETURN_STRINGL((char *) digest, 16);
113
0
  } else {
114
0
    RETVAL_NEW_STR(zend_string_alloc(32, 0));
115
0
    make_digest_ex(Z_STRVAL_P(return_value), digest, 16);
116
0
  }
117
0
}
118
/* }}} */
119
120
/*
121
 * This is an OpenSSL-compatible implementation of the RSA Data Security,
122
 * Inc. MD5 Message-Digest Algorithm (RFC 1321).
123
 *
124
 * Written by Solar Designer <solar at openwall.com> in 2001, and placed
125
 * in the public domain.  There's absolutely no warranty.
126
 *
127
 * This differs from Colin Plumb's older public domain implementation in
128
 * that no 32-bit integer data type is required, there's no compile-time
129
 * endianness configuration, and the function prototypes match OpenSSL's.
130
 * The primary goals are portability and ease of use.
131
 *
132
 * This implementation is meant to be fast, but not as fast as possible.
133
 * Some known optimizations are not included to reduce source code size
134
 * and avoid compile-time configuration.
135
 */
136
137
#include <string.h>
138
139
/*
140
 * The basic MD5 functions.
141
 *
142
 * F and G are optimized compared to their RFC 1321 definitions for
143
 * architectures that lack an AND-NOT instruction, just like in Colin Plumb's
144
 * implementation.
145
 */
146
0
#define F(x, y, z)      ((z) ^ ((x) & ((y) ^ (z))))
147
0
#define G(x, y, z)      ((y) ^ ((z) & ((x) ^ (y))))
148
0
#define H(x, y, z)      ((x) ^ (y) ^ (z))
149
0
#define I(x, y, z)      ((y) ^ ((x) | ~(z)))
150
151
/*
152
 * The MD5 transformation for all four rounds.
153
 */
154
#define STEP(f, a, b, c, d, x, t, s) \
155
0
  (a) += f((b), (c), (d)) + (x) + (t); \
156
0
  (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \
157
0
  (a) += (b);
158
159
/*
160
 * SET reads 4 input bytes in little-endian byte order and stores them
161
 * in a properly aligned word in host byte order.
162
 *
163
 * The check for little-endian architectures that tolerate unaligned
164
 * memory accesses is just an optimization.  Nothing will break if it
165
 * doesn't work.
166
 */
167
#if defined(__i386__) || defined(__x86_64__) || defined(__vax__)
168
typedef ZEND_SET_ALIGNED(1, uint32_t unaligned_uint32_t);
169
# define SET(n) \
170
  (*(unaligned_uint32_t *)&ptr[(n) * 4])
171
# define GET(n) \
172
  SET(n)
173
#else
174
# define SET(n) \
175
  (ctx->block[(n)] = \
176
  (uint32_t)ptr[(n) * 4] | \
177
  ((uint32_t)ptr[(n) * 4 + 1] << 8) | \
178
  ((uint32_t)ptr[(n) * 4 + 2] << 16) | \
179
  ((uint32_t)ptr[(n) * 4 + 3] << 24))
180
# define GET(n) \
181
  (ctx->block[(n)])
182
#endif
183
184
/*
185
 * This processes one or more 64-byte data blocks, but does NOT update
186
 * the bit counters.  There are no alignment requirements.
187
 */
188
static const void *body(PHP_MD5_CTX *ctx, const void *data, size_t size)
189
0
{
190
0
  const unsigned char *ptr;
191
0
  uint32_t a, b, c, d;
192
0
  uint32_t saved_a, saved_b, saved_c, saved_d;
193
194
0
  ptr = data;
195
196
0
  a = ctx->a;
197
0
  b = ctx->b;
198
0
  c = ctx->c;
199
0
  d = ctx->d;
200
201
0
  do {
202
0
    saved_a = a;
203
0
    saved_b = b;
204
0
    saved_c = c;
205
0
    saved_d = d;
206
207
/* Round 1 */
208
0
    STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7)
209
0
    STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12)
210
0
    STEP(F, c, d, a, b, SET(2), 0x242070db, 17)
211
0
    STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22)
212
0
    STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7)
213
0
    STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12)
214
0
    STEP(F, c, d, a, b, SET(6), 0xa8304613, 17)
215
0
    STEP(F, b, c, d, a, SET(7), 0xfd469501, 22)
216
0
    STEP(F, a, b, c, d, SET(8), 0x698098d8, 7)
217
0
    STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12)
218
0
    STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17)
219
0
    STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
220
0
    STEP(F, a, b, c, d, SET(12), 0x6b901122, 7)
221
0
    STEP(F, d, a, b, c, SET(13), 0xfd987193, 12)
222
0
    STEP(F, c, d, a, b, SET(14), 0xa679438e, 17)
223
0
    STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
224
225
/* Round 2 */
226
0
    STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5)
227
0
    STEP(G, d, a, b, c, GET(6), 0xc040b340, 9)
228
0
    STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14)
229
0
    STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20)
230
0
    STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5)
231
0
    STEP(G, d, a, b, c, GET(10), 0x02441453, 9)
232
0
    STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14)
233
0
    STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20)
234
0
    STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5)
235
0
    STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9)
236
0
    STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14)
237
0
    STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20)
238
0
    STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5)
239
0
    STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9)
240
0
    STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14)
241
0
    STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
242
243
/* Round 3 */
244
0
    STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4)
245
0
    STEP(H, d, a, b, c, GET(8), 0x8771f681, 11)
246
0
    STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16)
247
0
    STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23)
248
0
    STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4)
249
0
    STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11)
250
0
    STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16)
251
0
    STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23)
252
0
    STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4)
253
0
    STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11)
254
0
    STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16)
255
0
    STEP(H, b, c, d, a, GET(6), 0x04881d05, 23)
256
0
    STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4)
257
0
    STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11)
258
0
    STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16)
259
0
    STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23)
260
261
/* Round 4 */
262
0
    STEP(I, a, b, c, d, GET(0), 0xf4292244, 6)
263
0
    STEP(I, d, a, b, c, GET(7), 0x432aff97, 10)
264
0
    STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15)
265
0
    STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21)
266
0
    STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6)
267
0
    STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10)
268
0
    STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15)
269
0
    STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21)
270
0
    STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6)
271
0
    STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10)
272
0
    STEP(I, c, d, a, b, GET(6), 0xa3014314, 15)
273
0
    STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
274
0
    STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6)
275
0
    STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10)
276
0
    STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15)
277
0
    STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21)
278
279
0
    a += saved_a;
280
0
    b += saved_b;
281
0
    c += saved_c;
282
0
    d += saved_d;
283
284
0
    ptr += 64;
285
0
  } while (size -= 64);
286
287
0
  ctx->a = a;
288
0
  ctx->b = b;
289
0
  ctx->c = c;
290
0
  ctx->d = d;
291
292
0
  return ptr;
293
0
}
294
295
PHPAPI void PHP_MD5Init(PHP_MD5_CTX *ctx)
296
0
{
297
0
  ctx->a = 0x67452301;
298
0
  ctx->b = 0xefcdab89;
299
0
  ctx->c = 0x98badcfe;
300
0
  ctx->d = 0x10325476;
301
302
0
  ctx->lo = 0;
303
0
  ctx->hi = 0;
304
0
}
305
306
PHPAPI void PHP_MD5Update(PHP_MD5_CTX *ctx, const void *data, size_t size)
307
0
{
308
0
  uint32_t saved_lo;
309
0
  uint32_t used, free;
310
311
0
  saved_lo = ctx->lo;
312
0
  if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo) {
313
0
    ctx->hi++;
314
0
  }
315
0
  ctx->hi += size >> 29;
316
317
0
  used = saved_lo & 0x3f;
318
319
0
  if (used) {
320
0
    free = 64 - used;
321
322
0
    if (size < free) {
323
0
      memcpy(&ctx->buffer[used], data, size);
324
0
      return;
325
0
    }
326
327
0
    memcpy(&ctx->buffer[used], data, free);
328
0
    data = (unsigned char *)data + free;
329
0
    size -= free;
330
0
    body(ctx, ctx->buffer, 64);
331
0
  }
332
333
0
  if (size >= 64) {
334
0
    data = body(ctx, data, size & ~(size_t)0x3f);
335
0
    size &= 0x3f;
336
0
  }
337
338
0
  memcpy(ctx->buffer, data, size);
339
0
}
340
341
PHPAPI void PHP_MD5Final(unsigned char *result, PHP_MD5_CTX *ctx)
342
0
{
343
0
  uint32_t used, free;
344
345
0
  used = ctx->lo & 0x3f;
346
347
0
  ctx->buffer[used++] = 0x80;
348
349
0
  free = 64 - used;
350
351
0
  if (free < 8) {
352
0
    memset(&ctx->buffer[used], 0, free);
353
0
    body(ctx, ctx->buffer, 64);
354
0
    used = 0;
355
0
    free = 64;
356
0
  }
357
358
0
  memset(&ctx->buffer[used], 0, free - 8);
359
360
0
  ctx->lo <<= 3;
361
0
  ctx->buffer[56] = ctx->lo;
362
0
  ctx->buffer[57] = ctx->lo >> 8;
363
0
  ctx->buffer[58] = ctx->lo >> 16;
364
0
  ctx->buffer[59] = ctx->lo >> 24;
365
0
  ctx->buffer[60] = ctx->hi;
366
0
  ctx->buffer[61] = ctx->hi >> 8;
367
0
  ctx->buffer[62] = ctx->hi >> 16;
368
0
  ctx->buffer[63] = ctx->hi >> 24;
369
370
0
  body(ctx, ctx->buffer, 64);
371
372
0
  result[0] = ctx->a;
373
0
  result[1] = ctx->a >> 8;
374
0
  result[2] = ctx->a >> 16;
375
0
  result[3] = ctx->a >> 24;
376
0
  result[4] = ctx->b;
377
0
  result[5] = ctx->b >> 8;
378
0
  result[6] = ctx->b >> 16;
379
0
  result[7] = ctx->b >> 24;
380
0
  result[8] = ctx->c;
381
0
  result[9] = ctx->c >> 8;
382
0
  result[10] = ctx->c >> 16;
383
0
  result[11] = ctx->c >> 24;
384
0
  result[12] = ctx->d;
385
0
  result[13] = ctx->d >> 8;
386
0
  result[14] = ctx->d >> 16;
387
0
  result[15] = ctx->d >> 24;
388
389
0
  ZEND_SECURE_ZERO(ctx, sizeof(*ctx));
390
0
}