Coverage Report

Created: 2025-09-27 06:26

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