Coverage Report

Created: 2026-06-02 06:39

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