Coverage Report

Created: 2026-09-14 06:25

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