Coverage Report

Created: 2026-04-01 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/ext/hash/hash.c
Line
Count
Source
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
  | https://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: Sara Golemon <pollita@php.net>                               |
14
  |         Scott MacVicar <scottmac@php.net>                            |
15
  +----------------------------------------------------------------------+
16
*/
17
18
#ifdef HAVE_CONFIG_H
19
#include <config.h>
20
#endif
21
22
#include <math.h>
23
#include "php_hash.h"
24
#include "ext/standard/info.h"
25
#include "ext/standard/file.h"
26
#include "ext/standard/php_var.h"
27
28
#include "zend_attributes.h"
29
#include "zend_exceptions.h"
30
#include "zend_interfaces.h"
31
#include "zend_smart_str.h"
32
33
#include "hash_arginfo.h"
34
35
#ifdef PHP_WIN32
36
# define __alignof__ __alignof
37
#else
38
# ifndef HAVE_ALIGNOF
39
#  include <stddef.h>
40
#  define __alignof__(type) offsetof (struct { char c; type member;}, member)
41
# endif
42
#endif
43
44
static HashTable php_hash_hashtable;
45
zend_class_entry *php_hashcontext_ce;
46
static zend_object_handlers php_hashcontext_handlers;
47
48
#ifdef PHP_MHASH_BC
49
struct mhash_bc_entry {
50
  char *mhash_name;
51
  char *hash_name;
52
  int value;
53
};
54
55
#define MHASH_NUM_ALGOS 42
56
57
static struct mhash_bc_entry mhash_to_hash[MHASH_NUM_ALGOS] = {
58
  {"CRC32", "crc32", 0}, /* used by bzip */
59
  {"MD5", "md5", 1},
60
  {"SHA1", "sha1", 2},
61
  {"HAVAL256", "haval256,3", 3},
62
  {NULL, NULL, 4},
63
  {"RIPEMD160", "ripemd160", 5},
64
  {NULL, NULL, 6},
65
  {"TIGER", "tiger192,3", 7},
66
  {"GOST", "gost", 8},
67
  {"CRC32B", "crc32b", 9}, /* used by ethernet (IEEE 802.3), gzip, zip, png, etc */
68
  {"HAVAL224", "haval224,3", 10},
69
  {"HAVAL192", "haval192,3", 11},
70
  {"HAVAL160", "haval160,3", 12},
71
  {"HAVAL128", "haval128,3", 13},
72
  {"TIGER128", "tiger128,3", 14},
73
  {"TIGER160", "tiger160,3", 15},
74
  {"MD4", "md4", 16},
75
  {"SHA256", "sha256", 17},
76
  {"ADLER32", "adler32", 18},
77
  {"SHA224", "sha224", 19},
78
  {"SHA512", "sha512", 20},
79
  {"SHA384", "sha384", 21},
80
  {"WHIRLPOOL", "whirlpool", 22},
81
  {"RIPEMD128", "ripemd128", 23},
82
  {"RIPEMD256", "ripemd256", 24},
83
  {"RIPEMD320", "ripemd320", 25},
84
  {NULL, NULL, 26}, /* support needs to be added for snefru 128 */
85
  {"SNEFRU256", "snefru256", 27},
86
  {"MD2", "md2", 28},
87
  {"FNV132", "fnv132", 29},
88
  {"FNV1A32", "fnv1a32", 30},
89
  {"FNV164", "fnv164", 31},
90
  {"FNV1A64", "fnv1a64", 32},
91
  {"JOAAT", "joaat", 33},
92
  {"CRC32C", "crc32c", 34}, /* Castagnoli's CRC, used by iSCSI, SCTP, Btrfs, ext4, etc */
93
  {"MURMUR3A", "murmur3a", 35},
94
  {"MURMUR3C", "murmur3c", 36},
95
  {"MURMUR3F", "murmur3f", 37},
96
  {"XXH32", "xxh32", 38},
97
  {"XXH64", "xxh64", 39},
98
  {"XXH3", "xxh3", 40},
99
  {"XXH128", "xxh128", 41},
100
};
101
#endif
102
103
/* Hash Registry Access */
104
105
PHP_HASH_API const php_hash_ops *php_hash_fetch_ops(zend_string *algo) /* {{{ */
106
3.18k
{
107
3.18k
  zend_string *lower = zend_string_tolower(algo);
108
3.18k
  const php_hash_ops *ops = zend_hash_find_ptr(&php_hash_hashtable, lower);
109
3.18k
  zend_string_release(lower);
110
111
3.18k
  return ops;
112
3.18k
}
113
/* }}} */
114
115
PHP_HASH_API void php_hash_register_algo(const char *algo, const php_hash_ops *ops) /* {{{ */
116
960
{
117
960
  size_t algo_len = strlen(algo);
118
960
  char *lower = zend_str_tolower_dup(algo, algo_len);
119
960
  zend_hash_add_ptr(&php_hash_hashtable, zend_string_init_interned(lower, algo_len, 1), (void *) ops);
120
960
  efree(lower);
121
960
}
122
/* }}} */
123
124
PHP_HASH_API zend_result php_hash_copy(const void *ops, const void *orig_context, void *dest_context) /* {{{ */
125
0
{
126
0
  php_hash_ops *hash_ops = (php_hash_ops *)ops;
127
128
0
  memcpy(dest_context, orig_context, hash_ops->context_size);
129
0
  return SUCCESS;
130
0
}
131
/* }}} */
132
133
134
10.9k
static inline size_t align_to(size_t pos, size_t alignment) {
135
10.9k
  size_t offset = pos & (alignment - 1);
136
10.9k
  return pos + (offset ? alignment - offset : 0);
137
10.9k
}
138
139
static size_t parse_serialize_spec(
140
8.53k
    const char **specp, size_t *pos, size_t *sz, size_t *max_alignment) {
141
8.53k
  size_t count, alignment;
142
8.53k
  const char *spec = *specp;
143
  /* parse size */
144
8.53k
  if (*spec == 's' || *spec == 'S') {
145
0
    *sz = 2;
146
0
    alignment = __alignof__(uint16_t); /* usually 2 */
147
8.53k
  } else if (*spec == 'l' || *spec == 'L') {
148
4.76k
    *sz = 4;
149
4.76k
    alignment = __alignof__(uint32_t); /* usually 4 */
150
4.76k
  } else if (*spec == 'q' || *spec == 'Q') {
151
1.42k
    *sz = 8;
152
1.42k
    alignment = __alignof__(uint64_t); /* usually 8 */
153
2.35k
  } else if (*spec == 'i' || *spec == 'I') {
154
448
    *sz = sizeof(int);
155
448
    alignment = __alignof__(int);      /* usually 4 */
156
1.90k
  } else {
157
1.90k
    ZEND_ASSERT(*spec == 'b' || *spec == 'B');
158
1.90k
    *sz = 1;
159
1.90k
    alignment = 1;
160
1.90k
  }
161
  /* process alignment */
162
8.53k
  *pos = align_to(*pos, alignment);
163
8.53k
  *max_alignment = *max_alignment < alignment ? alignment : *max_alignment;
164
  /* parse count */
165
8.53k
  ++spec;
166
8.53k
  if (isdigit((unsigned char) *spec)) {
167
4.30k
    count = 0;
168
6.82k
    while (isdigit((unsigned char) *spec)) {
169
6.82k
      count = 10 * count + *spec - '0';
170
6.82k
      ++spec;
171
6.82k
    }
172
4.30k
  } else {
173
4.23k
    count = 1;
174
4.23k
  }
175
8.53k
  *specp = spec;
176
8.53k
  return count;
177
8.53k
}
178
179
0
static uint64_t one_from_buffer(size_t sz, const unsigned char *buf) {
180
0
  if (sz == 2) {
181
0
    const uint16_t *x = (const uint16_t *) buf;
182
0
    return *x;
183
0
  } else if (sz == 4) {
184
0
    const uint32_t *x = (const uint32_t *) buf;
185
0
    return *x;
186
0
  } else if (sz == 8) {
187
0
    const uint64_t *x = (const uint64_t *) buf;
188
0
    return *x;
189
0
  } else {
190
0
    ZEND_ASSERT(sz == 1);
191
0
    return *buf;
192
0
  }
193
0
}
194
195
18.3k
static void one_to_buffer(size_t sz, unsigned char *buf, uint64_t val) {
196
18.3k
  if (sz == 2) {
197
0
    uint16_t *x = (uint16_t *) buf;
198
0
    *x = val;
199
18.3k
  } else if (sz == 4) {
200
13.9k
    uint32_t *x = (uint32_t *) buf;
201
13.9k
    *x = val;
202
13.9k
  } else if (sz == 8) {
203
4.13k
    uint64_t *x = (uint64_t *) buf;
204
4.13k
    *x = val;
205
4.13k
  } else {
206
190
    ZEND_ASSERT(sz == 1);
207
190
    *buf = val;
208
190
  }
209
18.3k
}
210
211
/* Serialize a hash context according to a `spec` string.
212
   Spec contents:
213
   b[COUNT] -- serialize COUNT bytes
214
   s[COUNT] -- serialize COUNT 16-bit integers
215
   l[COUNT] -- serialize COUNT 32-bit integers
216
   q[COUNT] -- serialize COUNT 64-bit integers
217
   i[COUNT] -- serialize COUNT `int`s
218
   B[COUNT] -- skip COUNT bytes
219
   S[COUNT], L[COUNT], etc. -- uppercase versions skip instead of read
220
   . (must be last character) -- assert that the hash context has exactly
221
       this size
222
   Example: "llllllb64l16." is the spec for an MD5 context: 6 32-bit
223
   integers, followed by 64 bytes, then 16 32-bit integers, and that's
224
   exactly the size of the context.
225
226
   The serialization result is an array. Each integer is serialized as a
227
   32-bit integer, except that a run of 2 or more bytes is encoded as a
228
   string, and each 64-bit integer is serialized as two 32-bit integers, least
229
   significant bits first. This allows 32-bit and 64-bit architectures to
230
   interchange serialized HashContexts. */
231
232
PHP_HASH_API hash_spec_result php_hash_serialize_spec(const php_hashcontext_object *hash, zval *zv, const char *spec) /* {{{ */
233
0
{
234
0
  size_t pos = 0, max_alignment = 1;
235
0
  unsigned char *buf = (unsigned char *) hash->context;
236
0
  zval tmp;
237
0
  if (buf == NULL) {
238
0
    return HASH_SPEC_FAILURE;
239
0
  }
240
0
  array_init(zv);
241
0
  while (*spec != '\0' && *spec != '.') {
242
0
    char spec_ch = *spec;
243
0
    size_t sz, count = parse_serialize_spec(&spec, &pos, &sz, &max_alignment);
244
0
    if (pos + count * sz > hash->ops->context_size) {
245
0
      return HASH_SPEC_FAILURE;
246
0
    }
247
0
    if (isupper((unsigned char) spec_ch)) {
248
0
      pos += count * sz;
249
0
    } else if (sz == 1 && count > 1) {
250
0
      ZVAL_STRINGL(&tmp, (char *) buf + pos, count);
251
0
      zend_hash_next_index_insert(Z_ARRVAL_P(zv), &tmp);
252
0
      pos += count;
253
0
    } else {
254
0
      while (count > 0) {
255
0
        uint64_t val = one_from_buffer(sz, buf + pos);
256
0
        pos += sz;
257
0
        ZVAL_LONG(&tmp, (int32_t) val);
258
0
        zend_hash_next_index_insert(Z_ARRVAL_P(zv), &tmp);
259
0
        if (sz == 8) {
260
0
          ZVAL_LONG(&tmp, (int32_t) (val >> 32));
261
0
          zend_hash_next_index_insert(Z_ARRVAL_P(zv), &tmp);
262
0
        }
263
0
        --count;
264
0
      }
265
0
    }
266
0
  }
267
0
  if (*spec == '.' && align_to(pos, max_alignment) != hash->ops->context_size) {
268
0
    return HASH_SPEC_FAILURE;
269
0
  }
270
0
  return HASH_SPEC_SUCCESS;
271
0
}
272
/* }}} */
273
274
/* Unserialize a hash context serialized by `php_hash_serialize_spec` with `spec`. */
275
PHP_HASH_API hash_spec_result php_hash_unserialize_spec(php_hashcontext_object *hash, const zval *zv, const char *spec) /* {{{ */
276
3.12k
{
277
3.12k
  size_t pos = 0, max_alignment = 1, j = 0;
278
3.12k
  unsigned char *buf = (unsigned char *) hash->context;
279
3.12k
  zval *elt;
280
3.12k
  if (Z_TYPE_P(zv) != IS_ARRAY) {
281
4
    return HASH_SPEC_FAILURE;
282
4
  }
283
11.5k
  while (*spec != '\0' && *spec != '.') {
284
8.53k
    char spec_ch = *spec;
285
8.53k
    size_t sz, count = parse_serialize_spec(&spec, &pos, &sz, &max_alignment);
286
8.53k
    if (pos + count * sz > hash->ops->context_size) {
287
0
      return WRONG_CONTEXT_SIZE;
288
0
    }
289
8.53k
    if (isupper((unsigned char) spec_ch)) {
290
305
      pos += count * sz;
291
8.22k
    } else if (sz == 1 && count > 1) {
292
1.63k
      elt = zend_hash_index_find(Z_ARRVAL_P(zv), j);
293
1.63k
      if (!elt || Z_TYPE_P(elt) != IS_STRING || Z_STRLEN_P(elt) != count) {
294
9
        return BYTE_OFFSET_POS_ERROR - pos;
295
9
      }
296
1.62k
      ++j;
297
1.62k
      memcpy(buf + pos, Z_STRVAL_P(elt), count);
298
1.62k
      pos += count;
299
6.59k
    } else {
300
24.9k
      while (count > 0) {
301
18.3k
        uint64_t val;
302
18.3k
        elt = zend_hash_index_find(Z_ARRVAL_P(zv), j);
303
18.3k
        if (!elt || Z_TYPE_P(elt) != IS_LONG) {
304
39
          return BYTE_OFFSET_POS_ERROR - pos;
305
39
        }
306
18.3k
        ++j;
307
18.3k
        val = (uint32_t) Z_LVAL_P(elt);
308
18.3k
        if (sz == 8) {
309
4.14k
          elt = zend_hash_index_find(Z_ARRVAL_P(zv), j);
310
4.14k
          if (!elt || Z_TYPE_P(elt) != IS_LONG) {
311
10
            return BYTE_OFFSET_POS_ERROR - pos;
312
10
          }
313
4.13k
          ++j;
314
4.13k
          val += ((uint64_t) Z_LVAL_P(elt)) << 32;
315
4.13k
        }
316
18.3k
        one_to_buffer(sz, buf + pos, val);
317
18.3k
        pos += sz;
318
18.3k
        --count;
319
18.3k
      }
320
6.59k
    }
321
8.53k
  }
322
3.06k
  if (*spec == '.' && align_to(pos, max_alignment) != hash->ops->context_size) {
323
0
    return WRONG_CONTEXT_SIZE;
324
0
  }
325
326
3.06k
  return HASH_SPEC_SUCCESS;
327
3.06k
}
328
/* }}} */
329
330
PHP_HASH_API hash_spec_result php_hash_serialize(const php_hashcontext_object *hash, zend_long *magic, zval *zv) /* {{{ */
331
0
{
332
0
  if (!hash->ops->serialize_spec) {
333
0
        return HASH_SPEC_FAILURE;
334
0
    }
335
336
0
    *magic = PHP_HASH_SERIALIZE_MAGIC_SPEC;
337
0
    return php_hash_serialize_spec(hash, zv, hash->ops->serialize_spec);
338
0
}
339
/* }}} */
340
341
PHP_HASH_API hash_spec_result php_hash_unserialize(php_hashcontext_object *hash, zend_long magic, const zval *zv) /* {{{ */
342
2.63k
{
343
2.63k
  if (hash->ops->serialize_spec
344
2.63k
    && magic == PHP_HASH_SERIALIZE_MAGIC_SPEC) {
345
2.61k
    return php_hash_unserialize_spec(hash, zv, hash->ops->serialize_spec);
346
2.61k
  }
347
348
18
    return HASH_SPEC_FAILURE;
349
2.63k
}
350
/* }}} */
351
352
/* Userspace */
353
354
static void php_hash_do_hash(
355
  zval *return_value, zend_string *algo, char *data, size_t data_len, bool raw_output, bool isfilename, HashTable *args
356
0
) /* {{{ */ {
357
0
  zend_string *digest;
358
0
  const php_hash_ops *ops;
359
0
  void *context;
360
0
  php_stream *stream = NULL;
361
362
0
  ops = php_hash_fetch_ops(algo);
363
0
  if (!ops) {
364
0
    zend_argument_value_error(1, "must be a valid hashing algorithm");
365
0
    RETURN_THROWS();
366
0
  }
367
0
  if (isfilename) {
368
0
    if (zend_char_has_nul_byte(data, data_len)) {
369
0
      zend_argument_value_error(1, "must not contain any null bytes");
370
0
      RETURN_THROWS();
371
0
    }
372
0
    stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
373
0
    if (!stream) {
374
      /* Stream will report errors opening file */
375
0
      RETURN_FALSE;
376
0
    }
377
0
  }
378
379
0
  context = php_hash_alloc_context(ops);
380
0
  ops->hash_init(context, args);
381
382
0
  if (isfilename) {
383
0
    char buf[1024];
384
0
    ssize_t n;
385
386
0
    while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
387
0
      ops->hash_update(context, (unsigned char *) buf, n);
388
0
    }
389
0
    php_stream_close(stream);
390
0
    if (n < 0) {
391
0
      efree(context);
392
0
      RETURN_FALSE;
393
0
    }
394
0
  } else {
395
0
    ops->hash_update(context, (unsigned char *) data, data_len);
396
0
  }
397
398
0
  digest = zend_string_alloc(ops->digest_size, 0);
399
0
  ops->hash_final((unsigned char *) ZSTR_VAL(digest), context);
400
0
  efree(context);
401
402
0
  if (raw_output) {
403
0
    ZSTR_VAL(digest)[ops->digest_size] = 0;
404
0
    RETURN_NEW_STR(digest);
405
0
  } else {
406
0
    zend_string *hex_digest = zend_string_safe_alloc(ops->digest_size, 2, 0, 0);
407
408
0
    php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
409
0
    ZSTR_VAL(hex_digest)[2 * ops->digest_size] = 0;
410
0
    zend_string_efree(digest);
411
0
    RETURN_NEW_STR(hex_digest);
412
0
  }
413
0
}
414
/* }}} */
415
416
/* {{{ Generate a hash of a given input string
417
Returns lowercase hexits by default */
418
PHP_FUNCTION(hash)
419
0
{
420
0
  zend_string *algo;
421
0
  char *data;
422
0
  size_t data_len;
423
0
  bool raw_output = 0;
424
0
  HashTable *args = NULL;
425
426
0
  ZEND_PARSE_PARAMETERS_START(2, 4)
427
0
    Z_PARAM_STR(algo)
428
0
    Z_PARAM_STRING(data, data_len)
429
0
    Z_PARAM_OPTIONAL
430
0
    Z_PARAM_BOOL(raw_output)
431
0
    Z_PARAM_ARRAY_HT(args)
432
0
  ZEND_PARSE_PARAMETERS_END();
433
434
0
  php_hash_do_hash(return_value, algo, data, data_len, raw_output, 0, args);
435
0
}
436
/* }}} */
437
438
/* {{{ Generate a hash of a given file
439
Returns lowercase hexits by default */
440
PHP_FUNCTION(hash_file)
441
0
{
442
0
  zend_string *algo;
443
0
  char *data;
444
0
  size_t data_len;
445
0
  bool raw_output = 0;
446
0
  HashTable *args = NULL;
447
448
0
  ZEND_PARSE_PARAMETERS_START(2, 4)
449
0
    Z_PARAM_STR(algo)
450
0
    Z_PARAM_STRING(data, data_len)
451
0
    Z_PARAM_OPTIONAL
452
0
    Z_PARAM_BOOL(raw_output)
453
0
    Z_PARAM_ARRAY_HT(args)
454
0
  ZEND_PARSE_PARAMETERS_END();
455
456
0
  php_hash_do_hash(return_value, algo, data, data_len, raw_output, 1, args);
457
0
}
458
/* }}} */
459
460
0
static inline void php_hash_string_xor_char(unsigned char *out, const unsigned char *in, const unsigned char xor_with, const size_t length) {
461
0
  size_t i;
462
0
  for (i=0; i < length; i++) {
463
0
    out[i] = in[i] ^ xor_with;
464
0
  }
465
0
}
466
467
0
static inline void php_hash_string_xor(unsigned char *out, const unsigned char *in, const unsigned char *xor_with, const size_t length) {
468
0
  size_t i;
469
0
  for (i=0; i < length; i++) {
470
0
    out[i] = in[i] ^ xor_with[i];
471
0
  }
472
0
}
473
474
0
static inline void php_hash_hmac_prep_key(unsigned char *K, const php_hash_ops *ops, void *context, const unsigned char *key, const size_t key_len) {
475
0
  memset(K, 0, ops->block_size);
476
0
  if (key_len > ops->block_size) {
477
    /* Reduce the key first */
478
0
    ops->hash_init(context, NULL);
479
0
    ops->hash_update(context, key, key_len);
480
0
    ops->hash_final(K, context);
481
0
  } else {
482
0
    memcpy(K, key, key_len);
483
0
  }
484
  /* XOR the key with 0x36 to get the ipad) */
485
0
  php_hash_string_xor_char(K, K, 0x36, ops->block_size);
486
0
}
487
488
0
static inline void php_hash_hmac_round(unsigned char *final, const php_hash_ops *ops, void *context, const unsigned char *key, const unsigned char *data, const zend_long data_size) {
489
0
  ops->hash_init(context, NULL);
490
0
  ops->hash_update(context, key, ops->block_size);
491
0
  ops->hash_update(context, data, data_size);
492
0
  ops->hash_final(final, context);
493
0
}
494
495
static void php_hash_do_hash_hmac(
496
  zval *return_value, zend_string *algo, char *data, size_t data_len, char *key, size_t key_len, bool raw_output, bool isfilename
497
0
) /* {{{ */ {
498
0
  zend_string *digest;
499
0
  unsigned char *K;
500
0
  const php_hash_ops *ops;
501
0
  void *context;
502
0
  php_stream *stream = NULL;
503
504
0
  ops = php_hash_fetch_ops(algo);
505
0
  if (!ops || !ops->is_crypto) {
506
0
    zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
507
0
    RETURN_THROWS();
508
0
  }
509
510
0
  if (isfilename) {
511
0
    if (zend_char_has_nul_byte(data, data_len)) {
512
0
      zend_argument_value_error(2, "must not contain any null bytes");
513
0
      RETURN_THROWS();
514
0
    }
515
0
    stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
516
0
    if (!stream) {
517
      /* Stream will report errors opening file */
518
0
      RETURN_FALSE;
519
0
    }
520
0
  }
521
522
0
  context = php_hash_alloc_context(ops);
523
524
0
  K = emalloc(ops->block_size);
525
0
  digest = zend_string_alloc(ops->digest_size, 0);
526
527
0
  php_hash_hmac_prep_key(K, ops, context, (unsigned char *) key, key_len);
528
529
0
  if (isfilename) {
530
0
    char buf[1024];
531
0
    ssize_t n;
532
0
    ops->hash_init(context, NULL);
533
0
    ops->hash_update(context, K, ops->block_size);
534
0
    while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
535
0
      ops->hash_update(context, (unsigned char *) buf, n);
536
0
    }
537
0
    php_stream_close(stream);
538
0
    if (n < 0) {
539
0
      efree(context);
540
0
      efree(K);
541
0
      zend_string_efree(digest);
542
0
      RETURN_FALSE;
543
0
    }
544
545
0
    ops->hash_final((unsigned char *) ZSTR_VAL(digest), context);
546
0
  } else {
547
0
    php_hash_hmac_round((unsigned char *) ZSTR_VAL(digest), ops, context, K, (unsigned char *) data, data_len);
548
0
  }
549
550
0
  php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
551
552
0
  php_hash_hmac_round((unsigned char *) ZSTR_VAL(digest), ops, context, K, (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
553
554
  /* Zero the key */
555
0
  ZEND_SECURE_ZERO(K, ops->block_size);
556
0
  efree(K);
557
0
  efree(context);
558
559
0
  if (raw_output) {
560
0
    ZSTR_VAL(digest)[ops->digest_size] = 0;
561
0
    RETURN_NEW_STR(digest);
562
0
  } else {
563
0
    zend_string *hex_digest = zend_string_safe_alloc(ops->digest_size, 2, 0, 0);
564
565
0
    php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
566
0
    ZSTR_VAL(hex_digest)[2 * ops->digest_size] = 0;
567
0
    zend_string_efree(digest);
568
0
    RETURN_NEW_STR(hex_digest);
569
0
  }
570
0
}
571
/* }}} */
572
573
/* {{{ Generate a hash of a given input string with a key using HMAC
574
Returns lowercase hexits by default */
575
PHP_FUNCTION(hash_hmac)
576
0
{
577
0
  zend_string *algo;
578
0
  char *data, *key;
579
0
  size_t data_len, key_len;
580
0
  bool raw_output = false;
581
582
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sss|b", &algo, &data, &data_len, &key, &key_len, &raw_output) == FAILURE) {
583
0
    RETURN_THROWS();
584
0
  }
585
586
0
  php_hash_do_hash_hmac(return_value, algo, data, data_len, key, key_len, raw_output, false);
587
0
}
588
/* }}} */
589
590
/* {{{ Generate a hash of a given file with a key using HMAC
591
Returns lowercase hexits by default */
592
PHP_FUNCTION(hash_hmac_file)
593
0
{
594
0
  zend_string *algo;
595
0
  char *data, *key;
596
0
  size_t data_len, key_len;
597
0
  bool raw_output = false;
598
599
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sss|b", &algo, &data, &data_len, &key, &key_len, &raw_output) == FAILURE) {
600
0
    RETURN_THROWS();
601
0
  }
602
603
0
  php_hash_do_hash_hmac(return_value, algo, data, data_len, key, key_len, raw_output, true);
604
0
}
605
/* }}} */
606
607
/* {{{ Initialize a hashing context */
608
PHP_FUNCTION(hash_init)
609
0
{
610
0
  zend_string *algo, *key = NULL;
611
0
  zend_long options = 0;
612
0
  void *context;
613
0
  const php_hash_ops *ops;
614
0
  php_hashcontext_object *hash;
615
0
  HashTable *args = NULL;
616
617
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|lSh", &algo, &options, &key, &args) == FAILURE) {
618
0
    RETURN_THROWS();
619
0
  }
620
621
0
  ops = php_hash_fetch_ops(algo);
622
0
  if (!ops) {
623
0
    zend_argument_value_error(1, "must be a valid hashing algorithm");
624
0
    RETURN_THROWS();
625
0
  }
626
627
0
  if (options & PHP_HASH_HMAC) {
628
0
    if (!ops->is_crypto) {
629
0
      zend_argument_value_error(1, "must be a cryptographic hashing algorithm if HMAC is requested");
630
0
      RETURN_THROWS();
631
0
    }
632
0
    if (!key || (ZSTR_LEN(key) == 0)) {
633
      /* Note: a zero length key is no key at all */
634
0
      zend_argument_value_error(3, "must not be empty when HMAC is requested");
635
0
      RETURN_THROWS();
636
0
    }
637
0
  }
638
639
0
  object_init_ex(return_value, php_hashcontext_ce);
640
0
  hash = php_hashcontext_from_object(Z_OBJ_P(return_value));
641
642
0
  context = php_hash_alloc_context(ops);
643
0
  ops->hash_init(context, args);
644
645
0
  hash->ops = ops;
646
0
  hash->context = context;
647
0
  hash->options = options;
648
0
  hash->key = NULL;
649
650
0
  if (options & PHP_HASH_HMAC) {
651
0
    char *K = emalloc(ops->block_size);
652
0
    size_t i, block_size;
653
654
0
    memset(K, 0, ops->block_size);
655
656
0
    if (ZSTR_LEN(key) > ops->block_size) {
657
      /* Reduce the key first */
658
0
      ops->hash_update(context, (unsigned char *) ZSTR_VAL(key), ZSTR_LEN(key));
659
0
      ops->hash_final((unsigned char *) K, context);
660
      /* Make the context ready to start over */
661
0
      ops->hash_init(context, args);
662
0
    } else {
663
0
      memcpy(K, ZSTR_VAL(key), ZSTR_LEN(key));
664
0
    }
665
666
    /* XOR ipad */
667
0
    block_size = ops->block_size;
668
0
    for(i = 0; i < block_size; i++) {
669
0
      K[i] ^= 0x36;
670
0
    }
671
0
    ops->hash_update(context, (unsigned char *) K, ops->block_size);
672
0
    hash->key = (unsigned char *) K;
673
0
  }
674
0
}
675
/* }}} */
676
677
3.60k
#define PHP_HASHCONTEXT_VERIFY(hash) { \
678
3.60k
  if (!hash->context) { \
679
130
    zend_argument_type_error(1, "must be a valid, non-finalized HashContext"); \
680
130
    RETURN_THROWS(); \
681
130
  } \
682
3.60k
}
683
684
/* {{{ Pump data into the hashing algorithm */
685
PHP_FUNCTION(hash_update)
686
1.86k
{
687
1.86k
  zval *zhash;
688
1.86k
  php_hashcontext_object *hash;
689
1.86k
  zend_string *data;
690
691
1.86k
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "OS", &zhash, php_hashcontext_ce, &data) == FAILURE) {
692
0
    RETURN_THROWS();
693
0
  }
694
695
1.86k
  hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
696
1.86k
  PHP_HASHCONTEXT_VERIFY(hash);
697
1.73k
  hash->ops->hash_update(hash->context, (unsigned char *) ZSTR_VAL(data), ZSTR_LEN(data));
698
699
1.73k
  RETURN_TRUE;
700
1.73k
}
701
/* }}} */
702
703
/* {{{ Pump data into the hashing algorithm from an open stream */
704
PHP_FUNCTION(hash_update_stream)
705
0
{
706
0
  zend_object *hash_obj;
707
0
  php_hashcontext_object *hash;
708
0
  php_stream *stream = NULL;
709
0
  zend_long length = -1, didread = 0;
710
711
0
  ZEND_PARSE_PARAMETERS_START(2, 3)
712
0
    Z_PARAM_OBJ_OF_CLASS(hash_obj, php_hashcontext_ce)
713
0
    PHP_Z_PARAM_STREAM(stream)
714
0
    Z_PARAM_OPTIONAL
715
0
    Z_PARAM_LONG(length)
716
0
  ZEND_PARSE_PARAMETERS_END();
717
718
0
  hash = php_hashcontext_from_object(hash_obj);
719
0
  PHP_HASHCONTEXT_VERIFY(hash);
720
721
0
  while (length) {
722
0
    char buf[1024];
723
0
    zend_long toread = 1024;
724
0
    ssize_t n;
725
726
0
    if (length > 0 && toread > length) {
727
0
      toread = length;
728
0
    }
729
730
0
    if ((n = php_stream_read(stream, buf, toread)) <= 0) {
731
0
      RETURN_LONG(didread);
732
0
    }
733
0
    hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
734
0
    length -= n;
735
0
    didread += n;
736
0
  }
737
738
0
  RETURN_LONG(didread);
739
0
}
740
/* }}} */
741
742
/* {{{ Pump data into the hashing algorithm from a file */
743
PHP_FUNCTION(hash_update_file)
744
0
{
745
0
  zval *zhash, *zcontext = NULL;
746
0
  php_hashcontext_object *hash;
747
0
  php_stream_context *context = NULL;
748
0
  php_stream *stream;
749
0
  zend_string *filename;
750
0
  char buf[1024];
751
0
  ssize_t n;
752
753
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "OP|r!", &zhash, php_hashcontext_ce, &filename, &zcontext) == FAILURE) {
754
0
    RETURN_THROWS();
755
0
  }
756
757
0
  hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
758
0
  PHP_HASHCONTEXT_VERIFY(hash);
759
0
  context = php_stream_context_from_zval(zcontext, 0);
760
761
0
  stream = php_stream_open_wrapper_ex(ZSTR_VAL(filename), "rb", REPORT_ERRORS, NULL, context);
762
0
  if (!stream) {
763
    /* Stream will report errors opening file */
764
0
    RETURN_FALSE;
765
0
  }
766
767
0
  while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
768
0
    hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
769
0
  }
770
0
  php_stream_close(stream);
771
772
0
  RETURN_BOOL(n >= 0);
773
0
}
774
/* }}} */
775
776
/* {{{ Output resulting digest */
777
PHP_FUNCTION(hash_final)
778
1.73k
{
779
1.73k
  zval *zhash;
780
1.73k
  php_hashcontext_object *hash;
781
1.73k
  bool raw_output = false;
782
1.73k
  zend_string *digest;
783
1.73k
  size_t digest_len;
784
785
1.73k
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &zhash, php_hashcontext_ce, &raw_output) == FAILURE) {
786
0
    RETURN_THROWS();
787
0
  }
788
789
1.73k
  hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
790
1.73k
  PHP_HASHCONTEXT_VERIFY(hash);
791
792
1.73k
  digest_len = hash->ops->digest_size;
793
1.73k
  digest = zend_string_alloc(digest_len, 0);
794
1.73k
  hash->ops->hash_final((unsigned char *) ZSTR_VAL(digest), hash->context);
795
1.73k
  if (hash->options & PHP_HASH_HMAC) {
796
0
    size_t i, block_size;
797
798
    /* Convert K to opad -- 0x6A = 0x36 ^ 0x5C */
799
0
    block_size = hash->ops->block_size;
800
0
    for(i = 0; i < block_size; i++) {
801
0
      hash->key[i] ^= 0x6A;
802
0
    }
803
804
    /* Feed this result into the outer hash */
805
0
    hash->ops->hash_init(hash->context, NULL);
806
0
    hash->ops->hash_update(hash->context, hash->key, hash->ops->block_size);
807
0
    hash->ops->hash_update(hash->context, (unsigned char *) ZSTR_VAL(digest), hash->ops->digest_size);
808
0
    hash->ops->hash_final((unsigned char *) ZSTR_VAL(digest), hash->context);
809
810
    /* Zero the key */
811
0
    ZEND_SECURE_ZERO(hash->key, hash->ops->block_size);
812
0
    efree(hash->key);
813
0
    hash->key = NULL;
814
0
  }
815
1.73k
  ZSTR_VAL(digest)[digest_len] = 0;
816
817
  /* Invalidate the object from further use */
818
1.73k
  efree(hash->context);
819
1.73k
  hash->context = NULL;
820
821
1.73k
  if (raw_output) {
822
0
    RETURN_NEW_STR(digest);
823
1.73k
  } else {
824
1.73k
    zend_string *hex_digest = zend_string_safe_alloc(digest_len, 2, 0, 0);
825
826
1.73k
    php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), digest_len);
827
1.73k
    ZSTR_VAL(hex_digest)[2 * digest_len] = 0;
828
1.73k
    zend_string_efree(digest);
829
1.73k
    RETURN_NEW_STR(hex_digest);
830
1.73k
  }
831
1.73k
}
832
/* }}} */
833
834
/* {{{ Copy hash object */
835
PHP_FUNCTION(hash_copy)
836
0
{
837
0
  zval *zhash;
838
0
  php_hashcontext_object *context;
839
840
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zhash, php_hashcontext_ce) == FAILURE) {
841
0
    RETURN_THROWS();
842
0
  }
843
844
0
  context = php_hashcontext_from_object(Z_OBJ_P(zhash));
845
0
  PHP_HASHCONTEXT_VERIFY(context);
846
847
0
  RETVAL_OBJ(Z_OBJ_HANDLER_P(zhash, clone_obj)(Z_OBJ_P(zhash)));
848
849
0
  if (php_hashcontext_from_object(Z_OBJ_P(return_value))->context == NULL) {
850
0
    zend_throw_error(NULL, "Cannot copy hash");
851
0
    RETURN_THROWS();
852
0
  }
853
0
}
854
/* }}} */
855
856
/* {{{ Return a list of registered hashing algorithms */
857
PHP_FUNCTION(hash_algos)
858
0
{
859
0
  zend_string *str;
860
861
0
  ZEND_PARSE_PARAMETERS_NONE();
862
863
0
  array_init(return_value);
864
0
  ZEND_HASH_MAP_FOREACH_STR_KEY(&php_hash_hashtable, str) {
865
0
    add_next_index_str(return_value, zend_string_copy(str));
866
0
  } ZEND_HASH_FOREACH_END();
867
0
}
868
/* }}} */
869
870
/* {{{ Return a list of registered hashing algorithms suitable for hash_hmac() */
871
PHP_FUNCTION(hash_hmac_algos)
872
0
{
873
0
  zend_string *str;
874
0
  const php_hash_ops *ops;
875
876
0
  ZEND_PARSE_PARAMETERS_NONE();
877
878
0
  array_init(return_value);
879
0
  ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&php_hash_hashtable, str, ops) {
880
0
    if (ops->is_crypto) {
881
0
      add_next_index_str(return_value, zend_string_copy(str));
882
0
    }
883
0
  } ZEND_HASH_FOREACH_END();
884
0
}
885
/* }}} */
886
887
/* {{{ RFC5869 HMAC-based key derivation function */
888
PHP_FUNCTION(hash_hkdf)
889
0
{
890
0
  zend_string *returnval, *ikm, *algo, *info = NULL, *salt = NULL;
891
0
  zend_long length = 0;
892
0
  unsigned char *prk, *digest, *K;
893
0
  size_t i;
894
0
  size_t rounds;
895
0
  const php_hash_ops *ops;
896
0
  void *context;
897
898
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|lSS", &algo, &ikm, &length, &info, &salt) == FAILURE) {
899
0
    RETURN_THROWS();
900
0
  }
901
902
0
  ops = php_hash_fetch_ops(algo);
903
0
  if (!ops || !ops->is_crypto) {
904
0
    zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
905
0
    RETURN_THROWS();
906
0
  }
907
908
0
  if (ZSTR_LEN(ikm) == 0) {
909
0
    zend_argument_must_not_be_empty_error(2);
910
0
    RETURN_THROWS();
911
0
  }
912
913
0
  if (length < 0) {
914
0
    zend_argument_value_error(3, "must be greater than or equal to 0");
915
0
    RETURN_THROWS();
916
0
  } else if (length == 0) {
917
0
    length = ops->digest_size;
918
0
  } else if (length > (zend_long) (ops->digest_size * 255)) {
919
0
    zend_argument_value_error(3, "must be less than or equal to %zd", ops->digest_size * 255);
920
0
    RETURN_THROWS();
921
0
  }
922
923
0
  context = php_hash_alloc_context(ops);
924
925
  // Extract
926
0
  ops->hash_init(context, NULL);
927
0
  K = emalloc(ops->block_size);
928
0
  php_hash_hmac_prep_key(K, ops, context,
929
0
    (unsigned char *) (salt ? ZSTR_VAL(salt) : ""), salt ? ZSTR_LEN(salt) : 0);
930
931
0
  prk = emalloc(ops->digest_size);
932
0
  php_hash_hmac_round(prk, ops, context, K, (unsigned char *) ZSTR_VAL(ikm), ZSTR_LEN(ikm));
933
0
  php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
934
0
  php_hash_hmac_round(prk, ops, context, K, prk, ops->digest_size);
935
0
  ZEND_SECURE_ZERO(K, ops->block_size);
936
937
  // Expand
938
0
  returnval = zend_string_alloc(length, 0);
939
0
  digest = emalloc(ops->digest_size);
940
0
  for (i = 1, rounds = (length - 1) / ops->digest_size + 1; i <= rounds; i++) {
941
    // chr(i)
942
0
    unsigned char c[1];
943
0
    c[0] = (i & 0xFF);
944
945
0
    php_hash_hmac_prep_key(K, ops, context, prk, ops->digest_size);
946
0
    ops->hash_init(context, NULL);
947
0
    ops->hash_update(context, K, ops->block_size);
948
949
0
    if (i > 1) {
950
0
      ops->hash_update(context, digest, ops->digest_size);
951
0
    }
952
953
0
    if (info != NULL && ZSTR_LEN(info) > 0) {
954
0
      ops->hash_update(context, (unsigned char *) ZSTR_VAL(info), ZSTR_LEN(info));
955
0
    }
956
957
0
    ops->hash_update(context, c, 1);
958
0
    ops->hash_final(digest, context);
959
0
    php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
960
0
    php_hash_hmac_round(digest, ops, context, K, digest, ops->digest_size);
961
0
    memcpy(
962
0
      ZSTR_VAL(returnval) + ((i - 1) * ops->digest_size),
963
0
      digest,
964
0
      (i == rounds ? length - ((i - 1) * ops->digest_size) : ops->digest_size)
965
0
    );
966
0
  }
967
968
0
  ZEND_SECURE_ZERO(K, ops->block_size);
969
0
  ZEND_SECURE_ZERO(digest, ops->digest_size);
970
0
  ZEND_SECURE_ZERO(prk, ops->digest_size);
971
0
  efree(K);
972
0
  efree(context);
973
0
  efree(prk);
974
0
  efree(digest);
975
0
  ZSTR_VAL(returnval)[length] = 0;
976
0
  RETURN_STR(returnval);
977
0
}
978
979
/* {{{ Generate a PBKDF2 hash of the given password and salt
980
Returns lowercase hexits by default */
981
PHP_FUNCTION(hash_pbkdf2)
982
0
{
983
0
  zend_string *returnval, *algo;
984
0
  char *salt, *pass = NULL;
985
0
  unsigned char *computed_salt, *digest, *temp, *result, *K1, *K2 = NULL;
986
0
  zend_long loops, i, j, iterations, digest_length = 0, length = 0;
987
0
  size_t pass_len, salt_len = 0;
988
0
  bool raw_output = false;
989
0
  const php_hash_ops *ops;
990
0
  void *context;
991
0
  HashTable *args = NULL;
992
993
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sssl|lbh", &algo, &pass, &pass_len, &salt, &salt_len, &iterations, &length, &raw_output, &args) == FAILURE) {
994
0
    RETURN_THROWS();
995
0
  }
996
997
0
  ops = php_hash_fetch_ops(algo);
998
0
  if (!ops || !ops->is_crypto) {
999
0
    zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
1000
0
    RETURN_THROWS();
1001
0
  }
1002
1003
0
  if (salt_len > INT_MAX - 4) {
1004
0
    zend_argument_value_error(3, "must be less than or equal to INT_MAX - 4 bytes");
1005
0
    RETURN_THROWS();
1006
0
  }
1007
1008
0
  if (iterations <= 0) {
1009
0
    zend_argument_value_error(4, "must be greater than 0");
1010
0
    RETURN_THROWS();
1011
0
  }
1012
1013
0
  if (length < 0) {
1014
0
    zend_argument_value_error(5, "must be greater than or equal to 0");
1015
0
    RETURN_THROWS();
1016
0
  }
1017
1018
0
  context = php_hash_alloc_context(ops);
1019
0
  ops->hash_init(context, args);
1020
1021
0
  K1 = emalloc(ops->block_size);
1022
0
  K2 = emalloc(ops->block_size);
1023
0
  digest = emalloc(ops->digest_size);
1024
0
  temp = emalloc(ops->digest_size);
1025
1026
  /* Setup Keys that will be used for all hmac rounds */
1027
0
  php_hash_hmac_prep_key(K1, ops, context, (unsigned char *) pass, pass_len);
1028
  /* Convert K1 to opad -- 0x6A = 0x36 ^ 0x5C */
1029
0
  php_hash_string_xor_char(K2, K1, 0x6A, ops->block_size);
1030
1031
  /* Setup Main Loop to build a long enough result */
1032
0
  if (length == 0) {
1033
0
    length = ops->digest_size;
1034
0
    if (!raw_output) {
1035
0
      length = length * 2;
1036
0
    }
1037
0
  }
1038
0
  digest_length = length;
1039
0
  if (!raw_output) {
1040
0
    digest_length = (zend_long) ceil((float) length / 2.0);
1041
0
  }
1042
1043
0
  loops = (zend_long) ceil((float) digest_length / (float) ops->digest_size);
1044
1045
0
  result = safe_emalloc(loops, ops->digest_size, 0);
1046
1047
0
  computed_salt = safe_emalloc(salt_len, 1, 4);
1048
0
  memcpy(computed_salt, (unsigned char *) salt, salt_len);
1049
1050
0
  for (i = 1; i <= loops; i++) {
1051
    /* digest = hash_hmac(salt + pack('N', i), password) { */
1052
1053
    /* pack("N", i) */
1054
0
    computed_salt[salt_len] = (unsigned char) (i >> 24);
1055
0
    computed_salt[salt_len + 1] = (unsigned char) ((i & 0xFF0000) >> 16);
1056
0
    computed_salt[salt_len + 2] = (unsigned char) ((i & 0xFF00) >> 8);
1057
0
    computed_salt[salt_len + 3] = (unsigned char) (i & 0xFF);
1058
1059
0
    php_hash_hmac_round(digest, ops, context, K1, computed_salt, (zend_long) salt_len + 4);
1060
0
    php_hash_hmac_round(digest, ops, context, K2, digest, ops->digest_size);
1061
    /* } */
1062
1063
    /* temp = digest */
1064
0
    memcpy(temp, digest, ops->digest_size);
1065
1066
    /*
1067
     * Note that the loop starting at 1 is intentional, since we've already done
1068
     * the first round of the algorithm.
1069
     */
1070
0
    for (j = 1; j < iterations; j++) {
1071
      /* digest = hash_hmac(digest, password) { */
1072
0
      php_hash_hmac_round(digest, ops, context, K1, digest, ops->digest_size);
1073
0
      php_hash_hmac_round(digest, ops, context, K2, digest, ops->digest_size);
1074
      /* } */
1075
      /* temp ^= digest */
1076
0
      php_hash_string_xor(temp, temp, digest, ops->digest_size);
1077
0
    }
1078
    /* result += temp */
1079
0
    memcpy(result + ((i - 1) * ops->digest_size), temp, ops->digest_size);
1080
0
  }
1081
  /* Zero potentially sensitive variables */
1082
0
  ZEND_SECURE_ZERO(K1, ops->block_size);
1083
0
  ZEND_SECURE_ZERO(K2, ops->block_size);
1084
0
  ZEND_SECURE_ZERO(computed_salt, salt_len + 4);
1085
0
  efree(K1);
1086
0
  efree(K2);
1087
0
  efree(computed_salt);
1088
0
  efree(context);
1089
0
  efree(digest);
1090
0
  efree(temp);
1091
1092
0
  returnval = zend_string_alloc(length, 0);
1093
0
  if (raw_output) {
1094
0
    memcpy(ZSTR_VAL(returnval), result, length);
1095
0
  } else {
1096
0
    php_hash_bin2hex(ZSTR_VAL(returnval), result, digest_length);
1097
0
  }
1098
0
  ZSTR_VAL(returnval)[length] = 0;
1099
0
  efree(result);
1100
0
  RETURN_NEW_STR(returnval);
1101
0
}
1102
/* }}} */
1103
1104
/* {{{ Compares two strings using the same time whether they're equal or not.
1105
   A difference in length will leak */
1106
PHP_FUNCTION(hash_equals)
1107
0
{
1108
0
  zval *known_zval, *user_zval;
1109
0
  int result = 0;
1110
1111
0
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &known_zval, &user_zval) == FAILURE) {
1112
0
    RETURN_THROWS();
1113
0
  }
1114
1115
  /* We only allow comparing string to prevent unexpected results. */
1116
0
  if (Z_TYPE_P(known_zval) != IS_STRING) {
1117
0
    zend_argument_type_error(1, "must be of type string, %s given", zend_zval_value_name(known_zval));
1118
0
    RETURN_THROWS();
1119
0
  }
1120
1121
0
  if (Z_TYPE_P(user_zval) != IS_STRING) {
1122
0
    zend_argument_type_error(2, "must be of type string, %s given", zend_zval_value_name(user_zval));
1123
0
    RETURN_THROWS();
1124
0
  }
1125
1126
  /* This is security sensitive code. Do not optimize this for speed. */
1127
0
  result = php_safe_bcmp(Z_STR_P(known_zval), Z_STR_P(user_zval));
1128
1129
0
  RETURN_BOOL(0 == result);
1130
0
}
1131
/* }}} */
1132
1133
/* {{{ */
1134
0
PHP_METHOD(HashContext, __construct) {
1135
  /* Normally unreachable as private/final */
1136
0
  zend_throw_exception(zend_ce_error, "Illegal call to private/final constructor", 0);
1137
0
}
1138
/* }}} */
1139
1140
/* Module Housekeeping */
1141
1142
240
#define PHP_HASH_HAVAL_REGISTER(p,b)  php_hash_register_algo("haval" #b "," #p , &php_hash_##p##haval##b##_ops);
1143
1144
#ifdef PHP_MHASH_BC
1145
1146
#if 0
1147
/* See #69823, we should not insert module into module_registry while doing startup */
1148
1149
PHP_MINFO_FUNCTION(mhash)
1150
{
1151
  php_info_print_table_start();
1152
  php_info_print_table_row(2, "MHASH support", "Enabled");
1153
  php_info_print_table_row(2, "MHASH API Version", "Emulated Support");
1154
  php_info_print_table_end();
1155
}
1156
1157
zend_module_entry mhash_module_entry = {
1158
  STANDARD_MODULE_HEADER,
1159
  "mhash",
1160
  NULL,
1161
  NULL,
1162
  NULL,
1163
  NULL,
1164
  NULL,
1165
  PHP_MINFO(mhash),
1166
  PHP_MHASH_VERSION,
1167
  STANDARD_MODULE_PROPERTIES,
1168
};
1169
#endif
1170
1171
static void mhash_init(INIT_FUNC_ARGS)
1172
{
1173
  char buf[128];
1174
  int len;
1175
  int algo_number = 0;
1176
1177
  zend_string *deprecation_reason = zend_string_init("as the mhash*() functions were deprecated", strlen("as the mhash*() functions were deprecated"), 1);
1178
  for (algo_number = 0; algo_number < MHASH_NUM_ALGOS; algo_number++) {
1179
    struct mhash_bc_entry algorithm = mhash_to_hash[algo_number];
1180
    if (algorithm.mhash_name == NULL) {
1181
      continue;
1182
    }
1183
1184
    len = slprintf(buf, 127, "MHASH_%s", algorithm.mhash_name);
1185
    zend_constant *mhash_const = zend_register_long_constant(buf, len, algorithm.value, CONST_PERSISTENT|CONST_DEPRECATED, module_number);
1186
1187
    zend_attribute *deprecation_attrib = zend_add_global_constant_attribute(mhash_const, ZSTR_KNOWN(ZEND_STR_DEPRECATED_CAPITALIZED), 2);
1188
    ZVAL_STR(&deprecation_attrib->args[0].value, ZSTR_KNOWN(ZEND_STR_8_DOT_5));
1189
    deprecation_attrib->args[0].name = ZSTR_KNOWN(ZEND_STR_SINCE);
1190
    ZVAL_STR_COPY(&deprecation_attrib->args[1].value, deprecation_reason);
1191
    deprecation_attrib->args[1].name = ZSTR_KNOWN(ZEND_STR_MESSAGE);
1192
  }
1193
  /* Each of the attributes uses ZVAL_STR_COPY instead of trying to special
1194
   * case one of them to use ZVAL_STR, but that means there is one more
1195
   * reference that needs to be decremented. */
1196
  zend_string_release(deprecation_reason);
1197
1198
  /* TODO: this cause #69823 zend_register_internal_module(&mhash_module_entry); */
1199
}
1200
1201
/* {{{ Hash data with hash */
1202
PHP_FUNCTION(mhash)
1203
{
1204
  zend_long algorithm;
1205
  zend_string *algo = NULL;
1206
  char *data, *key = NULL;
1207
  size_t data_len, key_len = 0;
1208
1209
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "ls|s!", &algorithm, &data, &data_len, &key, &key_len) == FAILURE) {
1210
    RETURN_THROWS();
1211
  }
1212
1213
  /* need to convert the first parameter from int constant to string algorithm name */
1214
  if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
1215
    struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1216
    if (algorithm_lookup.hash_name) {
1217
      algo = zend_string_init(algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name), 0);
1218
    } else {
1219
      RETURN_FALSE;
1220
    }
1221
  } else {
1222
    RETURN_FALSE;
1223
  }
1224
1225
  if (key) {
1226
    php_hash_do_hash_hmac(return_value, algo, data, data_len, key, key_len, true, false);
1227
  } else {
1228
    php_hash_do_hash(return_value, algo, data, data_len, true, false, NULL);
1229
  }
1230
1231
  if (algo) {
1232
    zend_string_release(algo);
1233
  }
1234
}
1235
/* }}} */
1236
1237
/* {{{ Gets the name of hash */
1238
PHP_FUNCTION(mhash_get_hash_name)
1239
{
1240
  zend_long algorithm;
1241
1242
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &algorithm) == FAILURE) {
1243
    RETURN_THROWS();
1244
  }
1245
1246
  if (algorithm >= 0 && algorithm  < MHASH_NUM_ALGOS) {
1247
    struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1248
    if (algorithm_lookup.mhash_name) {
1249
      RETURN_STRING(algorithm_lookup.mhash_name);
1250
    }
1251
  }
1252
  RETURN_FALSE;
1253
}
1254
/* }}} */
1255
1256
/* {{{ Gets the number of available hashes */
1257
PHP_FUNCTION(mhash_count)
1258
{
1259
  ZEND_PARSE_PARAMETERS_NONE();
1260
  RETURN_LONG(MHASH_NUM_ALGOS - 1);
1261
}
1262
/* }}} */
1263
1264
/* {{{ Gets the block size of hash */
1265
PHP_FUNCTION(mhash_get_block_size)
1266
{
1267
  zend_long algorithm;
1268
1269
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &algorithm) == FAILURE) {
1270
    RETURN_THROWS();
1271
  }
1272
  RETVAL_FALSE;
1273
1274
  if (algorithm >= 0 && algorithm  < MHASH_NUM_ALGOS) {
1275
    struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1276
    if (algorithm_lookup.mhash_name) {
1277
      const php_hash_ops *ops = zend_hash_str_find_ptr(&php_hash_hashtable, algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
1278
      if (ops) {
1279
        RETVAL_LONG(ops->digest_size);
1280
      }
1281
    }
1282
  }
1283
}
1284
/* }}} */
1285
1286
#define SALT_SIZE 8
1287
1288
/* {{{ Generates a key using hash functions */
1289
PHP_FUNCTION(mhash_keygen_s2k)
1290
{
1291
  zend_long algorithm, l_bytes;
1292
  int bytes;
1293
  char *password, *salt;
1294
  size_t password_len, salt_len;
1295
  char padded_salt[SALT_SIZE];
1296
1297
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "lssl", &algorithm, &password, &password_len, &salt, &salt_len, &l_bytes) == FAILURE) {
1298
    RETURN_THROWS();
1299
  }
1300
1301
  bytes = (int)l_bytes;
1302
  if (bytes <= 0){
1303
    zend_argument_value_error(4, "must be a greater than 0");
1304
    RETURN_THROWS();
1305
  }
1306
1307
  salt_len = MIN(salt_len, SALT_SIZE);
1308
1309
  memcpy(padded_salt, salt, salt_len);
1310
  if (salt_len < SALT_SIZE) {
1311
    memset(padded_salt + salt_len, 0, SALT_SIZE - salt_len);
1312
  }
1313
  salt_len = SALT_SIZE;
1314
1315
  RETVAL_FALSE;
1316
  if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
1317
    struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1318
    if (algorithm_lookup.mhash_name) {
1319
      const php_hash_ops *ops = zend_hash_str_find_ptr(&php_hash_hashtable, algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
1320
      if (ops) {
1321
        unsigned char null = '\0';
1322
        void *context;
1323
        char *key, *digest;
1324
        int i = 0, j = 0;
1325
        size_t block_size = ops->digest_size;
1326
        size_t times = bytes / block_size;
1327
1328
        if ((bytes % block_size) != 0) {
1329
          times++;
1330
        }
1331
1332
        context = php_hash_alloc_context(ops);
1333
        ops->hash_init(context, NULL);
1334
1335
        key = ecalloc(1, times * block_size);
1336
        digest = emalloc(ops->digest_size + 1);
1337
1338
        for (i = 0; i < times; i++) {
1339
          ops->hash_init(context, NULL);
1340
1341
          for (j=0;j<i;j++) {
1342
            ops->hash_update(context, &null, 1);
1343
          }
1344
          ops->hash_update(context, (unsigned char *)padded_salt, salt_len);
1345
          ops->hash_update(context, (unsigned char *)password, password_len);
1346
          ops->hash_final((unsigned char *)digest, context);
1347
          memcpy( &key[i*block_size], digest, block_size);
1348
        }
1349
1350
        RETVAL_STRINGL(key, bytes);
1351
        ZEND_SECURE_ZERO(key, bytes);
1352
        efree(digest);
1353
        efree(context);
1354
        efree(key);
1355
      }
1356
    }
1357
  }
1358
}
1359
/* }}} */
1360
1361
#endif
1362
1363
/* ----------------------------------------------------------------------- */
1364
1365
/* {{{ php_hashcontext_create */
1366
29.2k
static zend_object* php_hashcontext_create(zend_class_entry *ce) {
1367
29.2k
  php_hashcontext_object *objval = zend_object_alloc(sizeof(php_hashcontext_object), ce);
1368
29.2k
  zend_object *zobj = &objval->std;
1369
1370
29.2k
  zend_object_std_init(zobj, ce);
1371
29.2k
  object_properties_init(zobj, ce);
1372
29.2k
  zobj->handlers = &php_hashcontext_handlers;
1373
1374
29.2k
  return zobj;
1375
29.2k
}
1376
/* }}} */
1377
1378
/* {{{ php_hashcontext_dtor */
1379
29.3k
static void php_hashcontext_dtor(zend_object *obj) {
1380
29.3k
  php_hashcontext_object *hash = php_hashcontext_from_object(obj);
1381
1382
29.3k
  if (hash->context) {
1383
1.44k
    efree(hash->context);
1384
1.44k
    hash->context = NULL;
1385
1.44k
  }
1386
1387
29.3k
  if (hash->key) {
1388
0
    ZEND_SECURE_ZERO(hash->key, hash->ops->block_size);
1389
0
    efree(hash->key);
1390
0
    hash->key = NULL;
1391
0
  }
1392
29.3k
}
1393
/* }}} */
1394
1395
29.2k
static void php_hashcontext_free(zend_object *obj) {
1396
29.2k
  php_hashcontext_dtor(obj);
1397
29.2k
  zend_object_std_dtor(obj);
1398
29.2k
}
1399
1400
/* {{{ php_hashcontext_clone */
1401
0
static zend_object *php_hashcontext_clone(zend_object *zobj) {
1402
0
  php_hashcontext_object *oldobj = php_hashcontext_from_object(zobj);
1403
0
  zend_object *znew = php_hashcontext_create(zobj->ce);
1404
0
  php_hashcontext_object *newobj = php_hashcontext_from_object(znew);
1405
1406
0
  if (!oldobj->context) {
1407
0
    zend_throw_exception(zend_ce_value_error, "Cannot clone a finalized HashContext", 0);
1408
0
    return znew;
1409
0
  }
1410
1411
0
  zend_objects_clone_members(znew, zobj);
1412
1413
0
  newobj->ops = oldobj->ops;
1414
0
  newobj->options = oldobj->options;
1415
0
  newobj->context = php_hash_alloc_context(newobj->ops);
1416
0
  newobj->ops->hash_init(newobj->context, NULL);
1417
1418
0
  if (SUCCESS != newobj->ops->hash_copy(newobj->ops, oldobj->context, newobj->context)) {
1419
0
    efree(newobj->context);
1420
0
    newobj->context = NULL;
1421
0
    return znew;
1422
0
  }
1423
1424
0
  newobj->key = ecalloc(1, newobj->ops->block_size);
1425
0
  if (oldobj->key) {
1426
0
    memcpy(newobj->key, oldobj->key, newobj->ops->block_size);
1427
0
  }
1428
1429
0
  return znew;
1430
0
}
1431
/* }}} */
1432
1433
/* Serialization format: 5-element array
1434
   Index 0: hash algorithm (string)
1435
   Index 1: options (long, 0)
1436
   Index 2: hash-determined serialization of context state (usually array)
1437
   Index 3: magic number defining layout of context state (long, usually 2)
1438
   Index 4: properties (array)
1439
1440
   HashContext serializations are not necessarily portable between architectures or
1441
   PHP versions. If the format of a serialized hash context changes, that should
1442
   be reflected in either a different value of `magic` or a different format of
1443
   the serialized context state. Most context states are unparsed and parsed using
1444
   a spec string, such as "llb128.", using the format defined by
1445
   `php_hash_serialize_spec`/`php_hash_unserialize_spec`. Some hash algorithms must
1446
   also check the unserialized state for validity, to ensure that using an
1447
   unserialized context is safe from memory errors.
1448
1449
   Currently HASH_HMAC contexts cannot be serialized, because serializing them
1450
   would require serializing the HMAC key in plaintext. */
1451
1452
/* {{{ Serialize the object */
1453
PHP_METHOD(HashContext, __serialize)
1454
0
{
1455
0
  zval *object = ZEND_THIS;
1456
0
  php_hashcontext_object *hash = php_hashcontext_from_object(Z_OBJ_P(object));
1457
0
  zend_long magic = 0;
1458
0
  zval tmp;
1459
1460
0
  ZEND_PARSE_PARAMETERS_NONE();
1461
1462
0
  array_init(return_value);
1463
1464
0
  if (!hash->ops->hash_serialize) {
1465
0
    goto serialize_failure;
1466
0
  } else if (hash->options & PHP_HASH_HMAC) {
1467
0
    zend_throw_exception(NULL, "HashContext with HASH_HMAC option cannot be serialized", 0);
1468
0
    RETURN_THROWS();
1469
0
  }
1470
1471
0
  ZVAL_STRING(&tmp, hash->ops->algo);
1472
0
  zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1473
1474
0
  ZVAL_LONG(&tmp, hash->options);
1475
0
  zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1476
1477
0
  if (hash->ops->hash_serialize(hash, &magic, &tmp) != HASH_SPEC_SUCCESS) {
1478
0
    goto serialize_failure;
1479
0
  }
1480
0
  zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1481
1482
0
  ZVAL_LONG(&tmp, magic);
1483
0
  zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1484
1485
  /* members */
1486
0
  ZVAL_ARR(&tmp, zend_std_get_properties(&hash->std));
1487
0
  Z_TRY_ADDREF(tmp);
1488
0
  zend_hash_next_index_insert(Z_ARRVAL_P(return_value), &tmp);
1489
1490
0
  return;
1491
1492
0
serialize_failure:
1493
0
  zend_throw_exception_ex(NULL, 0, "HashContext for algorithm \"%s\" cannot be serialized", hash->ops->algo);
1494
0
  RETURN_THROWS();
1495
0
}
1496
/* }}} */
1497
1498
/* {{{ unserialize the object */
1499
PHP_METHOD(HashContext, __unserialize)
1500
3.24k
{
1501
3.24k
  zval *object = ZEND_THIS;
1502
3.24k
  php_hashcontext_object *hash = php_hashcontext_from_object(Z_OBJ_P(object));
1503
3.24k
  HashTable *data;
1504
3.24k
  zval *algo_zv, *magic_zv, *options_zv, *hash_zv, *members_zv;
1505
3.24k
  zend_long magic, options;
1506
3.24k
  hash_spec_result unserialize_result;
1507
3.24k
  const php_hash_ops *ops;
1508
1509
3.24k
  if (zend_parse_parameters(ZEND_NUM_ARGS(), "h", &data) == FAILURE) {
1510
0
    RETURN_THROWS();
1511
0
  }
1512
1513
3.24k
  if (hash->context) {
1514
0
    zend_throw_exception(NULL, "HashContext::__unserialize called on initialized object", 0);
1515
0
    RETURN_THROWS();
1516
0
  }
1517
1518
3.24k
  algo_zv = zend_hash_index_find(data, 0);
1519
3.24k
  options_zv = zend_hash_index_find(data, 1);
1520
3.24k
  hash_zv = zend_hash_index_find(data, 2);
1521
3.24k
  magic_zv = zend_hash_index_find(data, 3);
1522
3.24k
  members_zv = zend_hash_index_find(data, 4);
1523
1524
3.24k
  if (!algo_zv || Z_TYPE_P(algo_zv) != IS_STRING
1525
3.19k
    || !magic_zv || Z_TYPE_P(magic_zv) != IS_LONG
1526
3.18k
    || !options_zv || Z_TYPE_P(options_zv) != IS_LONG
1527
3.18k
    || !hash_zv
1528
3.18k
    || !members_zv || Z_TYPE_P(members_zv) != IS_ARRAY) {
1529
63
    zend_throw_exception(NULL, "Incomplete or ill-formed serialization data", 0);
1530
63
    RETURN_THROWS();
1531
63
  }
1532
1533
3.18k
  magic = Z_LVAL_P(magic_zv);
1534
3.18k
  options = Z_LVAL_P(options_zv);
1535
3.18k
  if (options & PHP_HASH_HMAC) {
1536
1
    zend_throw_exception(NULL, "HashContext with HASH_HMAC option cannot be serialized", 0);
1537
1
    RETURN_THROWS();
1538
1
  }
1539
1540
3.18k
  ops = php_hash_fetch_ops(Z_STR_P(algo_zv));
1541
3.18k
  if (!ops) {
1542
5
    zend_throw_exception(NULL, "Unknown hash algorithm", 0);
1543
5
    RETURN_THROWS();
1544
3.17k
  } else if (!ops->hash_unserialize) {
1545
0
    zend_throw_exception_ex(NULL, 0, "Hash algorithm \"%s\" cannot be unserialized", ops->algo);
1546
0
    RETURN_THROWS();
1547
0
  }
1548
1549
3.17k
  hash->ops = ops;
1550
3.17k
  hash->context = php_hash_alloc_context(ops);
1551
3.17k
  hash->options = options;
1552
3.17k
  ops->hash_init(hash->context, NULL);
1553
1554
3.17k
  unserialize_result = ops->hash_unserialize(hash, magic, hash_zv);
1555
3.17k
  if (unserialize_result != HASH_SPEC_SUCCESS) {
1556
151
    zend_throw_exception_ex(NULL, 0, "Incomplete or ill-formed serialization data (\"%s\" code %d)", ops->algo, unserialize_result);
1557
    /* free context */
1558
151
    php_hashcontext_dtor(Z_OBJ_P(object));
1559
151
    RETURN_THROWS();
1560
151
  }
1561
1562
3.02k
  object_properties_load(&hash->std, Z_ARRVAL_P(members_zv));
1563
3.02k
}
1564
/* }}} */
1565
1566
ZEND_METHOD(HashContext, __debugInfo)
1567
0
{
1568
0
  zval *object = ZEND_THIS;
1569
0
  php_hashcontext_object *hash = php_hashcontext_from_object(Z_OBJ_P(object));
1570
1571
0
  ZEND_PARSE_PARAMETERS_NONE();
1572
1573
0
  zval tmp;
1574
1575
0
  array_init(return_value);
1576
1577
0
  ZVAL_STRING(&tmp, hash->ops->algo);
1578
0
  zend_hash_str_update(Z_ARR_P(return_value), "algo", strlen("algo"), &tmp);
1579
0
}
1580
1581
/* {{{ PHP_MINIT_FUNCTION */
1582
PHP_MINIT_FUNCTION(hash)
1583
16
{
1584
16
  zend_hash_init(&php_hash_hashtable, 35, NULL, NULL, 1);
1585
1586
16
  php_hash_register_algo("md2",     &php_hash_md2_ops);
1587
16
  php_hash_register_algo("md4",     &php_hash_md4_ops);
1588
16
  php_hash_register_algo("md5",     &php_hash_md5_ops);
1589
16
  php_hash_register_algo("sha1",      &php_hash_sha1_ops);
1590
16
  php_hash_register_algo("sha224",    &php_hash_sha224_ops);
1591
16
  php_hash_register_algo("sha256",    &php_hash_sha256_ops);
1592
16
  php_hash_register_algo("sha384",    &php_hash_sha384_ops);
1593
16
  php_hash_register_algo("sha512/224",            &php_hash_sha512_224_ops);
1594
16
  php_hash_register_algo("sha512/256",            &php_hash_sha512_256_ops);
1595
16
  php_hash_register_algo("sha512",    &php_hash_sha512_ops);
1596
16
  php_hash_register_algo("sha3-224",    &php_hash_sha3_224_ops);
1597
16
  php_hash_register_algo("sha3-256",    &php_hash_sha3_256_ops);
1598
16
  php_hash_register_algo("sha3-384",    &php_hash_sha3_384_ops);
1599
16
  php_hash_register_algo("sha3-512",    &php_hash_sha3_512_ops);
1600
16
  php_hash_register_algo("ripemd128",   &php_hash_ripemd128_ops);
1601
16
  php_hash_register_algo("ripemd160",   &php_hash_ripemd160_ops);
1602
16
  php_hash_register_algo("ripemd256",   &php_hash_ripemd256_ops);
1603
16
  php_hash_register_algo("ripemd320",   &php_hash_ripemd320_ops);
1604
16
  php_hash_register_algo("whirlpool",   &php_hash_whirlpool_ops);
1605
16
  php_hash_register_algo("tiger128,3",  &php_hash_3tiger128_ops);
1606
16
  php_hash_register_algo("tiger160,3",  &php_hash_3tiger160_ops);
1607
16
  php_hash_register_algo("tiger192,3",  &php_hash_3tiger192_ops);
1608
16
  php_hash_register_algo("tiger128,4",  &php_hash_4tiger128_ops);
1609
16
  php_hash_register_algo("tiger160,4",  &php_hash_4tiger160_ops);
1610
16
  php_hash_register_algo("tiger192,4",  &php_hash_4tiger192_ops);
1611
16
  php_hash_register_algo("snefru",    &php_hash_snefru_ops);
1612
16
  php_hash_register_algo("snefru256",   &php_hash_snefru_ops);
1613
16
  php_hash_register_algo("gost",      &php_hash_gost_ops);
1614
16
  php_hash_register_algo("gost-crypto",   &php_hash_gost_crypto_ops);
1615
16
  php_hash_register_algo("adler32",   &php_hash_adler32_ops);
1616
16
  php_hash_register_algo("crc32",     &php_hash_crc32_ops);
1617
16
  php_hash_register_algo("crc32b",    &php_hash_crc32b_ops);
1618
16
  php_hash_register_algo("crc32c",    &php_hash_crc32c_ops);
1619
16
  php_hash_register_algo("fnv132",    &php_hash_fnv132_ops);
1620
16
  php_hash_register_algo("fnv1a32",   &php_hash_fnv1a32_ops);
1621
16
  php_hash_register_algo("fnv164",    &php_hash_fnv164_ops);
1622
16
  php_hash_register_algo("fnv1a64",   &php_hash_fnv1a64_ops);
1623
16
  php_hash_register_algo("joaat",     &php_hash_joaat_ops);
1624
16
  php_hash_register_algo("murmur3a",    &php_hash_murmur3a_ops);
1625
16
  php_hash_register_algo("murmur3c",    &php_hash_murmur3c_ops);
1626
16
  php_hash_register_algo("murmur3f",    &php_hash_murmur3f_ops);
1627
16
  php_hash_register_algo("xxh32",   &php_hash_xxh32_ops);
1628
16
  php_hash_register_algo("xxh64",   &php_hash_xxh64_ops);
1629
16
  php_hash_register_algo("xxh3",    &php_hash_xxh3_64_ops);
1630
16
  php_hash_register_algo("xxh128",    &php_hash_xxh3_128_ops);
1631
1632
16
  PHP_HASH_HAVAL_REGISTER(3,128);
1633
16
  PHP_HASH_HAVAL_REGISTER(3,160);
1634
16
  PHP_HASH_HAVAL_REGISTER(3,192);
1635
16
  PHP_HASH_HAVAL_REGISTER(3,224);
1636
16
  PHP_HASH_HAVAL_REGISTER(3,256);
1637
1638
16
  PHP_HASH_HAVAL_REGISTER(4,128);
1639
16
  PHP_HASH_HAVAL_REGISTER(4,160);
1640
16
  PHP_HASH_HAVAL_REGISTER(4,192);
1641
16
  PHP_HASH_HAVAL_REGISTER(4,224);
1642
16
  PHP_HASH_HAVAL_REGISTER(4,256);
1643
1644
16
  PHP_HASH_HAVAL_REGISTER(5,128);
1645
16
  PHP_HASH_HAVAL_REGISTER(5,160);
1646
16
  PHP_HASH_HAVAL_REGISTER(5,192);
1647
16
  PHP_HASH_HAVAL_REGISTER(5,224);
1648
16
  PHP_HASH_HAVAL_REGISTER(5,256);
1649
1650
16
  register_hash_symbols(module_number);
1651
1652
16
  php_hashcontext_ce = register_class_HashContext();
1653
16
  php_hashcontext_ce->create_object = php_hashcontext_create;
1654
1655
16
  memcpy(&php_hashcontext_handlers, &std_object_handlers,
1656
16
         sizeof(zend_object_handlers));
1657
16
  php_hashcontext_handlers.offset = XtOffsetOf(php_hashcontext_object, std);
1658
16
  php_hashcontext_handlers.free_obj = php_hashcontext_free;
1659
16
  php_hashcontext_handlers.clone_obj = php_hashcontext_clone;
1660
1661
#ifdef PHP_MHASH_BC
1662
  mhash_init(INIT_FUNC_ARGS_PASSTHRU);
1663
#endif
1664
1665
16
  return SUCCESS;
1666
16
}
1667
/* }}} */
1668
1669
/* {{{ PHP_MSHUTDOWN_FUNCTION */
1670
PHP_MSHUTDOWN_FUNCTION(hash)
1671
0
{
1672
0
  zend_hash_destroy(&php_hash_hashtable);
1673
1674
0
  return SUCCESS;
1675
0
}
1676
/* }}} */
1677
1678
/* {{{ PHP_MINFO_FUNCTION */
1679
PHP_MINFO_FUNCTION(hash)
1680
4
{
1681
4
  char buffer[2048];
1682
4
  zend_string *str;
1683
4
  char *s = buffer, *e = s + sizeof(buffer);
1684
1685
488
  ZEND_HASH_MAP_FOREACH_STR_KEY(&php_hash_hashtable, str) {
1686
488
    s += slprintf(s, e - s, "%s ", ZSTR_VAL(str));
1687
488
  } ZEND_HASH_FOREACH_END();
1688
4
  *s = 0;
1689
1690
4
  php_info_print_table_start();
1691
4
  php_info_print_table_row(2, "hash support", "enabled");
1692
4
  php_info_print_table_row(2, "Hashing Engines", buffer);
1693
4
  php_info_print_table_end();
1694
1695
#ifdef PHP_MHASH_BC
1696
  php_info_print_table_start();
1697
  php_info_print_table_row(2, "MHASH support", "Enabled");
1698
  php_info_print_table_row(2, "MHASH API Version", "Emulated Support");
1699
  php_info_print_table_end();
1700
#endif
1701
1702
4
}
1703
/* }}} */
1704
1705
/* {{{ hash_module_entry */
1706
zend_module_entry hash_module_entry = {
1707
  STANDARD_MODULE_HEADER,
1708
  PHP_HASH_EXTNAME,
1709
  ext_functions,
1710
  PHP_MINIT(hash),
1711
  PHP_MSHUTDOWN(hash),
1712
  NULL, /* RINIT */
1713
  NULL, /* RSHUTDOWN */
1714
  PHP_MINFO(hash),
1715
  PHP_HASH_VERSION,
1716
  STANDARD_MODULE_PROPERTIES
1717
};
1718
/* }}} */