php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
hash.c
Go to the documentation of this file.
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"
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
44static HashTable php_hash_hashtable;
46static zend_object_handlers php_hashcontext_handlers;
47
48#ifdef PHP_MHASH_BC
49struct mhash_bc_entry {
50 char *mhash_name;
51 char *hash_name;
52 int value;
53};
54
55#define MHASH_NUM_ALGOS 42
56
57static 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
106{
107 zend_string *lower = zend_string_tolower(algo);
108 const php_hash_ops *ops = zend_hash_find_ptr(&php_hash_hashtable, lower);
109 zend_string_release(lower);
110
111 return ops;
112}
113/* }}} */
114
115PHP_HASH_API void php_hash_register_algo(const char *algo, const php_hash_ops *ops) /* {{{ */
116{
117 size_t algo_len = strlen(algo);
118 char *lower = zend_str_tolower_dup(algo, algo_len);
119 zend_hash_add_ptr(&php_hash_hashtable, zend_string_init_interned(lower, algo_len, 1), (void *) ops);
120 efree(lower);
121}
122/* }}} */
123
124PHP_HASH_API zend_result php_hash_copy(const void *ops, const void *orig_context, void *dest_context) /* {{{ */
125{
126 php_hash_ops *hash_ops = (php_hash_ops *)ops;
127
128 memcpy(dest_context, orig_context, hash_ops->context_size);
129 return SUCCESS;
130}
131/* }}} */
132
133
134static inline size_t align_to(size_t pos, size_t alignment) {
135 size_t offset = pos & (alignment - 1);
136 return pos + (offset ? alignment - offset : 0);
137}
138
139static size_t parse_serialize_spec(
140 const char **specp, size_t *pos, size_t *sz, size_t *max_alignment) {
141 size_t count, alignment;
142 const char *spec = *specp;
143 /* parse size */
144 if (*spec == 's' || *spec == 'S') {
145 *sz = 2;
146 alignment = __alignof__(uint16_t); /* usually 2 */
147 } else if (*spec == 'l' || *spec == 'L') {
148 *sz = 4;
149 alignment = __alignof__(uint32_t); /* usually 4 */
150 } else if (*spec == 'q' || *spec == 'Q') {
151 *sz = 8;
152 alignment = __alignof__(uint64_t); /* usually 8 */
153 } else if (*spec == 'i' || *spec == 'I') {
154 *sz = sizeof(int);
155 alignment = __alignof__(int); /* usually 4 */
156 } else {
157 ZEND_ASSERT(*spec == 'b' || *spec == 'B');
158 *sz = 1;
159 alignment = 1;
160 }
161 /* process alignment */
162 *pos = align_to(*pos, alignment);
163 *max_alignment = *max_alignment < alignment ? alignment : *max_alignment;
164 /* parse count */
165 ++spec;
166 if (isdigit((unsigned char) *spec)) {
167 count = 0;
168 while (isdigit((unsigned char) *spec)) {
169 count = 10 * count + *spec - '0';
170 ++spec;
171 }
172 } else {
173 count = 1;
174 }
175 *specp = spec;
176 return count;
177}
178
179static uint64_t one_from_buffer(size_t sz, const unsigned char *buf) {
180 if (sz == 2) {
181 const uint16_t *x = (const uint16_t *) buf;
182 return *x;
183 } else if (sz == 4) {
184 const uint32_t *x = (const uint32_t *) buf;
185 return *x;
186 } else if (sz == 8) {
187 const uint64_t *x = (const uint64_t *) buf;
188 return *x;
189 } else {
190 ZEND_ASSERT(sz == 1);
191 return *buf;
192 }
193}
194
195static void one_to_buffer(size_t sz, unsigned char *buf, uint64_t val) {
196 if (sz == 2) {
197 uint16_t *x = (uint16_t *) buf;
198 *x = val;
199 } else if (sz == 4) {
200 uint32_t *x = (uint32_t *) buf;
201 *x = val;
202 } else if (sz == 8) {
203 uint64_t *x = (uint64_t *) buf;
204 *x = val;
205 } else {
206 ZEND_ASSERT(sz == 1);
207 *buf = val;
208 }
209}
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
233{
234 size_t pos = 0, max_alignment = 1;
235 unsigned char *buf = (unsigned char *) hash->context;
236 zval tmp;
237 if (buf == NULL) {
238 return FAILURE;
239 }
240 array_init(zv);
241 while (*spec != '\0' && *spec != '.') {
242 char spec_ch = *spec;
243 size_t sz, count = parse_serialize_spec(&spec, &pos, &sz, &max_alignment);
244 if (pos + count * sz > hash->ops->context_size) {
245 return FAILURE;
246 }
247 if (isupper((unsigned char) spec_ch)) {
248 pos += count * sz;
249 } else if (sz == 1 && count > 1) {
250 ZVAL_STRINGL(&tmp, (char *) buf + pos, count);
252 pos += count;
253 } else {
254 while (count > 0) {
255 uint64_t val = one_from_buffer(sz, buf + pos);
256 pos += sz;
257 ZVAL_LONG(&tmp, (int32_t) val);
259 if (sz == 8) {
260 ZVAL_LONG(&tmp, (int32_t) (val >> 32));
262 }
263 --count;
264 }
265 }
266 }
267 if (*spec == '.' && align_to(pos, max_alignment) != hash->ops->context_size) {
268 return FAILURE;
269 }
270 return SUCCESS;
271}
272/* }}} */
273
274/* Unserialize a hash context serialized by `php_hash_serialize_spec` with `spec`.
275 Returns SUCCESS on success and a negative error code on failure.
276 Codes: FAILURE (-1) == generic failure
277 -999 == spec wrong size for context
278 -1000 - POS == problem at byte offset POS */
279
281{
282 size_t pos = 0, max_alignment = 1, j = 0;
283 unsigned char *buf = (unsigned char *) hash->context;
284 zval *elt;
285 if (Z_TYPE_P(zv) != IS_ARRAY) {
286 return FAILURE;
287 }
288 while (*spec != '\0' && *spec != '.') {
289 char spec_ch = *spec;
290 size_t sz, count = parse_serialize_spec(&spec, &pos, &sz, &max_alignment);
291 if (pos + count * sz > hash->ops->context_size) {
292 return -999;
293 }
294 if (isupper((unsigned char) spec_ch)) {
295 pos += count * sz;
296 } else if (sz == 1 && count > 1) {
298 if (!elt || Z_TYPE_P(elt) != IS_STRING || Z_STRLEN_P(elt) != count) {
299 return -1000 - pos;
300 }
301 ++j;
302 memcpy(buf + pos, Z_STRVAL_P(elt), count);
303 pos += count;
304 } else {
305 while (count > 0) {
306 uint64_t val;
308 if (!elt || Z_TYPE_P(elt) != IS_LONG) {
309 return -1000 - pos;
310 }
311 ++j;
312 val = (uint32_t) Z_LVAL_P(elt);
313 if (sz == 8) {
315 if (!elt || Z_TYPE_P(elt) != IS_LONG) {
316 return -1000 - pos;
317 }
318 ++j;
319 val += ((uint64_t) Z_LVAL_P(elt)) << 32;
320 }
321 one_to_buffer(sz, buf + pos, val);
322 pos += sz;
323 --count;
324 }
325 }
326 }
327 if (*spec == '.' && align_to(pos, max_alignment) != hash->ops->context_size) {
328 return -999;
329 }
330 return SUCCESS;
331}
332/* }}} */
333
335{
336 if (hash->ops->serialize_spec) {
338 return php_hash_serialize_spec(hash, zv, hash->ops->serialize_spec);
339 } else {
340 return FAILURE;
341 }
342}
343/* }}} */
344
346{
347 if (hash->ops->serialize_spec
349 return php_hash_unserialize_spec(hash, zv, hash->ops->serialize_spec);
350 } else {
351 return FAILURE;
352 }
353}
354/* }}} */
355
356/* Userspace */
357
358static void php_hash_do_hash(
359 zval *return_value, zend_string *algo, char *data, size_t data_len, bool raw_output, bool isfilename, HashTable *args
360) /* {{{ */ {
361 zend_string *digest;
362 const php_hash_ops *ops;
363 void *context;
364 php_stream *stream = NULL;
365
366 ops = php_hash_fetch_ops(algo);
367 if (!ops) {
368 zend_argument_value_error(1, "must be a valid hashing algorithm");
370 }
371 if (isfilename) {
372 if (CHECK_NULL_PATH(data, data_len)) {
373 zend_argument_value_error(1, "must not contain any null bytes");
375 }
376 stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
377 if (!stream) {
378 /* Stream will report errors opening file */
380 }
381 }
382
383 context = php_hash_alloc_context(ops);
384 ops->hash_init(context, args);
385
386 if (isfilename) {
387 char buf[1024];
388 ssize_t n;
389
390 while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
391 ops->hash_update(context, (unsigned char *) buf, n);
392 }
393 php_stream_close(stream);
394 if (n < 0) {
395 efree(context);
397 }
398 } else {
399 ops->hash_update(context, (unsigned char *) data, data_len);
400 }
401
402 digest = zend_string_alloc(ops->digest_size, 0);
403 ops->hash_final((unsigned char *) ZSTR_VAL(digest), context);
404 efree(context);
405
406 if (raw_output) {
407 ZSTR_VAL(digest)[ops->digest_size] = 0;
408 RETURN_NEW_STR(digest);
409 } else {
410 zend_string *hex_digest = zend_string_safe_alloc(ops->digest_size, 2, 0, 0);
411
412 php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
413 ZSTR_VAL(hex_digest)[2 * ops->digest_size] = 0;
414 zend_string_release_ex(digest, 0);
415 RETURN_NEW_STR(hex_digest);
416 }
417}
418/* }}} */
419
420/* {{{ Generate a hash of a given input string
421Returns lowercase hexits by default */
423{
424 zend_string *algo;
425 char *data;
426 size_t data_len;
427 bool raw_output = 0;
429
431 Z_PARAM_STR(algo)
432 Z_PARAM_STRING(data, data_len)
434 Z_PARAM_BOOL(raw_output)
437
438 php_hash_do_hash(return_value, algo, data, data_len, raw_output, 0, args);
439}
440/* }}} */
441
442/* {{{ Generate a hash of a given file
443Returns lowercase hexits by default */
445{
446 zend_string *algo;
447 char *data;
448 size_t data_len;
449 bool raw_output = 0;
451
453 Z_PARAM_STR(algo)
454 Z_PARAM_STRING(data, data_len)
456 Z_PARAM_BOOL(raw_output)
459
460 php_hash_do_hash(return_value, algo, data, data_len, raw_output, 1, args);
461}
462/* }}} */
463
464static inline void php_hash_string_xor_char(unsigned char *out, const unsigned char *in, const unsigned char xor_with, const size_t length) {
465 size_t i;
466 for (i=0; i < length; i++) {
467 out[i] = in[i] ^ xor_with;
468 }
469}
470
471static inline void php_hash_string_xor(unsigned char *out, const unsigned char *in, const unsigned char *xor_with, const size_t length) {
472 size_t i;
473 for (i=0; i < length; i++) {
474 out[i] = in[i] ^ xor_with[i];
475 }
476}
477
478static 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) {
479 memset(K, 0, ops->block_size);
480 if (key_len > ops->block_size) {
481 /* Reduce the key first */
482 ops->hash_init(context, NULL);
483 ops->hash_update(context, key, key_len);
484 ops->hash_final(K, context);
485 } else {
486 memcpy(K, key, key_len);
487 }
488 /* XOR the key with 0x36 to get the ipad) */
489 php_hash_string_xor_char(K, K, 0x36, ops->block_size);
490}
491
492static 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) {
493 ops->hash_init(context, NULL);
494 ops->hash_update(context, key, ops->block_size);
495 ops->hash_update(context, data, data_size);
496 ops->hash_final(final, context);
497}
498
499static void php_hash_do_hash_hmac(
500 zval *return_value, zend_string *algo, char *data, size_t data_len, char *key, size_t key_len, bool raw_output, bool isfilename
501) /* {{{ */ {
502 zend_string *digest;
503 unsigned char *K;
504 const php_hash_ops *ops;
505 void *context;
506 php_stream *stream = NULL;
507
508 ops = php_hash_fetch_ops(algo);
509 if (!ops || !ops->is_crypto) {
510 zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
512 }
513
514 if (isfilename) {
515 if (CHECK_NULL_PATH(data, data_len)) {
516 zend_argument_value_error(2, "must not contain any null bytes");
518 }
519 stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
520 if (!stream) {
521 /* Stream will report errors opening file */
523 }
524 }
525
526 context = php_hash_alloc_context(ops);
527
528 K = emalloc(ops->block_size);
529 digest = zend_string_alloc(ops->digest_size, 0);
530
531 php_hash_hmac_prep_key(K, ops, context, (unsigned char *) key, key_len);
532
533 if (isfilename) {
534 char buf[1024];
535 ssize_t n;
536 ops->hash_init(context, NULL);
537 ops->hash_update(context, K, ops->block_size);
538 while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
539 ops->hash_update(context, (unsigned char *) buf, n);
540 }
541 php_stream_close(stream);
542 if (n < 0) {
543 efree(context);
544 efree(K);
545 zend_string_release(digest);
547 }
548
549 ops->hash_final((unsigned char *) ZSTR_VAL(digest), context);
550 } else {
551 php_hash_hmac_round((unsigned char *) ZSTR_VAL(digest), ops, context, K, (unsigned char *) data, data_len);
552 }
553
554 php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
555
556 php_hash_hmac_round((unsigned char *) ZSTR_VAL(digest), ops, context, K, (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
557
558 /* Zero the key */
559 ZEND_SECURE_ZERO(K, ops->block_size);
560 efree(K);
561 efree(context);
562
563 if (raw_output) {
564 ZSTR_VAL(digest)[ops->digest_size] = 0;
565 RETURN_NEW_STR(digest);
566 } else {
567 zend_string *hex_digest = zend_string_safe_alloc(ops->digest_size, 2, 0, 0);
568
569 php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
570 ZSTR_VAL(hex_digest)[2 * ops->digest_size] = 0;
571 zend_string_release_ex(digest, 0);
572 RETURN_NEW_STR(hex_digest);
573 }
574}
575/* }}} */
576
577/* {{{ Generate a hash of a given input string with a key using HMAC
578Returns lowercase hexits by default */
580{
581 zend_string *algo;
582 char *data, *key;
583 size_t data_len, key_len;
584 bool raw_output = 0;
585
586 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sss|b", &algo, &data, &data_len, &key, &key_len, &raw_output) == FAILURE) {
588 }
589
590 php_hash_do_hash_hmac(return_value, algo, data, data_len, key, key_len, raw_output, 0);
591}
592/* }}} */
593
594/* {{{ Generate a hash of a given file with a key using HMAC
595Returns lowercase hexits by default */
597{
598 zend_string *algo;
599 char *data, *key;
600 size_t data_len, key_len;
601 bool raw_output = 0;
602
603 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sss|b", &algo, &data, &data_len, &key, &key_len, &raw_output) == FAILURE) {
605 }
606
607 php_hash_do_hash_hmac(return_value, algo, data, data_len, key, key_len, raw_output, 1);
608}
609/* }}} */
610
611/* {{{ Initialize a hashing context */
613{
614 zend_string *algo, *key = NULL;
615 zend_long options = 0;
616 void *context;
617 const php_hash_ops *ops;
620
621 if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|lSh", &algo, &options, &key, &args) == FAILURE) {
623 }
624
625 ops = php_hash_fetch_ops(algo);
626 if (!ops) {
627 zend_argument_value_error(1, "must be a valid hashing algorithm");
629 }
630
631 if (options & PHP_HASH_HMAC) {
632 if (!ops->is_crypto) {
633 zend_argument_value_error(1, "must be a cryptographic hashing algorithm if HMAC is requested");
635 }
636 if (!key || (ZSTR_LEN(key) == 0)) {
637 /* Note: a zero length key is no key at all */
638 zend_argument_value_error(3, "must not be empty when HMAC is requested");
640 }
641 }
642
644 hash = php_hashcontext_from_object(Z_OBJ_P(return_value));
645
646 context = php_hash_alloc_context(ops);
647 ops->hash_init(context, args);
648
649 hash->ops = ops;
650 hash->context = context;
651 hash->options = options;
652 hash->key = NULL;
653
654 if (options & PHP_HASH_HMAC) {
655 char *K = emalloc(ops->block_size);
656 size_t i, block_size;
657
658 memset(K, 0, ops->block_size);
659
660 if (ZSTR_LEN(key) > ops->block_size) {
661 /* Reduce the key first */
662 ops->hash_update(context, (unsigned char *) ZSTR_VAL(key), ZSTR_LEN(key));
663 ops->hash_final((unsigned char *) K, context);
664 /* Make the context ready to start over */
665 ops->hash_init(context, args);
666 } else {
668 }
669
670 /* XOR ipad */
671 block_size = ops->block_size;
672 for(i = 0; i < block_size; i++) {
673 K[i] ^= 0x36;
674 }
675 ops->hash_update(context, (unsigned char *) K, ops->block_size);
676 hash->key = (unsigned char *) K;
677 }
678}
679/* }}} */
680
681#define PHP_HASHCONTEXT_VERIFY(hash) { \
682 if (!hash->context) { \
683 zend_argument_type_error(1, "must be a valid, non-finalized HashContext"); \
684 RETURN_THROWS(); \
685 } \
686}
687
688/* {{{ Pump data into the hashing algorithm */
690{
691 zval *zhash;
694
697 }
698
699 hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
701 hash->ops->hash_update(hash->context, (unsigned char *) ZSTR_VAL(data), ZSTR_LEN(data));
702
704}
705/* }}} */
706
707/* {{{ Pump data into the hashing algorithm from an open stream */
709{
710 zval *zhash, *zstream;
712 php_stream *stream = NULL;
713 zend_long length = -1, didread = 0;
714
715 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Or|l", &zhash, php_hashcontext_ce, &zstream, &length) == FAILURE) {
717 }
718
719 hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
721 php_stream_from_zval(stream, zstream);
722
723 while (length) {
724 char buf[1024];
725 zend_long toread = 1024;
726 ssize_t n;
727
728 if (length > 0 && toread > length) {
729 toread = length;
730 }
731
732 if ((n = php_stream_read(stream, buf, toread)) <= 0) {
733 RETURN_LONG(didread);
734 }
735 hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
736 length -= n;
737 didread += n;
738 }
739
740 RETURN_LONG(didread);
741}
742/* }}} */
743
744/* {{{ Pump data into the hashing algorithm from a file */
746{
747 zval *zhash, *zcontext = NULL;
750 php_stream *stream;
751 zend_string *filename;
752 char buf[1024];
753 ssize_t n;
754
755 if (zend_parse_parameters(ZEND_NUM_ARGS(), "OP|r!", &zhash, php_hashcontext_ce, &filename, &zcontext) == FAILURE) {
757 }
758
759 hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
762
763 stream = php_stream_open_wrapper_ex(ZSTR_VAL(filename), "rb", REPORT_ERRORS, NULL, context);
764 if (!stream) {
765 /* Stream will report errors opening file */
767 }
768
769 while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
770 hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
771 }
772 php_stream_close(stream);
773
774 RETURN_BOOL(n >= 0);
775}
776/* }}} */
777
778/* {{{ Output resulting digest */
780{
781 zval *zhash;
783 bool raw_output = 0;
784 zend_string *digest;
785 size_t digest_len;
786
787 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &zhash, php_hashcontext_ce, &raw_output) == FAILURE) {
789 }
790
791 hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
793
794 digest_len = hash->ops->digest_size;
795 digest = zend_string_alloc(digest_len, 0);
796 hash->ops->hash_final((unsigned char *) ZSTR_VAL(digest), hash->context);
797 if (hash->options & PHP_HASH_HMAC) {
798 size_t i, block_size;
799
800 /* Convert K to opad -- 0x6A = 0x36 ^ 0x5C */
801 block_size = hash->ops->block_size;
802 for(i = 0; i < block_size; i++) {
803 hash->key[i] ^= 0x6A;
804 }
805
806 /* Feed this result into the outer hash */
807 hash->ops->hash_init(hash->context, NULL);
808 hash->ops->hash_update(hash->context, hash->key, hash->ops->block_size);
809 hash->ops->hash_update(hash->context, (unsigned char *) ZSTR_VAL(digest), hash->ops->digest_size);
810 hash->ops->hash_final((unsigned char *) ZSTR_VAL(digest), hash->context);
811
812 /* Zero the key */
813 ZEND_SECURE_ZERO(hash->key, hash->ops->block_size);
814 efree(hash->key);
815 hash->key = NULL;
816 }
817 ZSTR_VAL(digest)[digest_len] = 0;
818
819 /* Invalidate the object from further use */
820 efree(hash->context);
821 hash->context = NULL;
822
823 if (raw_output) {
824 RETURN_NEW_STR(digest);
825 } else {
826 zend_string *hex_digest = zend_string_safe_alloc(digest_len, 2, 0, 0);
827
828 php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), digest_len);
829 ZSTR_VAL(hex_digest)[2 * digest_len] = 0;
830 zend_string_release_ex(digest, 0);
831 RETURN_NEW_STR(hex_digest);
832 }
833}
834/* }}} */
835
836/* {{{ Copy hash object */
838{
839 zval *zhash;
841
844 }
845
846 context = php_hashcontext_from_object(Z_OBJ_P(zhash));
848
850
851 if (php_hashcontext_from_object(Z_OBJ_P(return_value))->context == NULL) {
853
854 zend_throw_error(NULL, "Cannot copy hash");
856 }
857}
858/* }}} */
859
860/* {{{ Return a list of registered hashing algorithms */
862{
863 zend_string *str;
864
867 }
868
870 ZEND_HASH_MAP_FOREACH_STR_KEY(&php_hash_hashtable, str) {
871 add_next_index_str(return_value, zend_string_copy(str));
873}
874/* }}} */
875
876/* {{{ Return a list of registered hashing algorithms suitable for hash_hmac() */
878{
879 zend_string *str;
880 const php_hash_ops *ops;
881
884 }
885
887 ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&php_hash_hashtable, str, ops) {
888 if (ops->is_crypto) {
889 add_next_index_str(return_value, zend_string_copy(str));
890 }
892}
893/* }}} */
894
895/* {{{ RFC5869 HMAC-based key derivation function */
897{
898 zend_string *returnval, *ikm, *algo, *info = NULL, *salt = NULL;
899 zend_long length = 0;
900 unsigned char *prk, *digest, *K;
901 size_t i;
902 size_t rounds;
903 const php_hash_ops *ops;
904 void *context;
905
906 if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|lSS", &algo, &ikm, &length, &info, &salt) == FAILURE) {
908 }
909
910 ops = php_hash_fetch_ops(algo);
911 if (!ops || !ops->is_crypto) {
912 zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
914 }
915
916 if (ZSTR_LEN(ikm) == 0) {
919 }
920
921 if (length < 0) {
922 zend_argument_value_error(3, "must be greater than or equal to 0");
924 } else if (length == 0) {
925 length = ops->digest_size;
926 } else if (length > (zend_long) (ops->digest_size * 255)) {
927 zend_argument_value_error(3, "must be less than or equal to %zd", ops->digest_size * 255);
929 }
930
931 context = php_hash_alloc_context(ops);
932
933 // Extract
934 ops->hash_init(context, NULL);
935 K = emalloc(ops->block_size);
936 php_hash_hmac_prep_key(K, ops, context,
937 (unsigned char *) (salt ? ZSTR_VAL(salt) : ""), salt ? ZSTR_LEN(salt) : 0);
938
939 prk = emalloc(ops->digest_size);
940 php_hash_hmac_round(prk, ops, context, K, (unsigned char *) ZSTR_VAL(ikm), ZSTR_LEN(ikm));
941 php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
942 php_hash_hmac_round(prk, ops, context, K, prk, ops->digest_size);
943 ZEND_SECURE_ZERO(K, ops->block_size);
944
945 // Expand
946 returnval = zend_string_alloc(length, 0);
947 digest = emalloc(ops->digest_size);
948 for (i = 1, rounds = (length - 1) / ops->digest_size + 1; i <= rounds; i++) {
949 // chr(i)
950 unsigned char c[1];
951 c[0] = (i & 0xFF);
952
953 php_hash_hmac_prep_key(K, ops, context, prk, ops->digest_size);
954 ops->hash_init(context, NULL);
955 ops->hash_update(context, K, ops->block_size);
956
957 if (i > 1) {
958 ops->hash_update(context, digest, ops->digest_size);
959 }
960
961 if (info != NULL && ZSTR_LEN(info) > 0) {
962 ops->hash_update(context, (unsigned char *) ZSTR_VAL(info), ZSTR_LEN(info));
963 }
964
965 ops->hash_update(context, c, 1);
966 ops->hash_final(digest, context);
967 php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
968 php_hash_hmac_round(digest, ops, context, K, digest, ops->digest_size);
969 memcpy(
970 ZSTR_VAL(returnval) + ((i - 1) * ops->digest_size),
971 digest,
972 (i == rounds ? length - ((i - 1) * ops->digest_size) : ops->digest_size)
973 );
974 }
975
976 ZEND_SECURE_ZERO(K, ops->block_size);
977 ZEND_SECURE_ZERO(digest, ops->digest_size);
978 ZEND_SECURE_ZERO(prk, ops->digest_size);
979 efree(K);
980 efree(context);
981 efree(prk);
982 efree(digest);
983 ZSTR_VAL(returnval)[length] = 0;
984 RETURN_STR(returnval);
985}
986
987/* {{{ Generate a PBKDF2 hash of the given password and salt
988Returns lowercase hexits by default */
990{
991 zend_string *returnval, *algo;
992 char *salt, *pass = NULL;
993 unsigned char *computed_salt, *digest, *temp, *result, *K1, *K2 = NULL;
994 zend_long loops, i, j, iterations, digest_length = 0, length = 0;
995 size_t pass_len, salt_len = 0;
996 bool raw_output = 0;
997 const php_hash_ops *ops;
998 void *context;
1000
1001 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Sssl|lbh", &algo, &pass, &pass_len, &salt, &salt_len, &iterations, &length, &raw_output, &args) == FAILURE) {
1002 RETURN_THROWS();
1003 }
1004
1005 ops = php_hash_fetch_ops(algo);
1006 if (!ops || !ops->is_crypto) {
1007 zend_argument_value_error(1, "must be a valid cryptographic hashing algorithm");
1008 RETURN_THROWS();
1009 }
1010
1011 if (salt_len > INT_MAX - 4) {
1012 zend_argument_value_error(3, "must be less than or equal to INT_MAX - 4 bytes");
1013 RETURN_THROWS();
1014 }
1015
1016 if (iterations <= 0) {
1017 zend_argument_value_error(4, "must be greater than 0");
1018 RETURN_THROWS();
1019 }
1020
1021 if (length < 0) {
1022 zend_argument_value_error(5, "must be greater than or equal to 0");
1023 RETURN_THROWS();
1024 }
1025
1026 context = php_hash_alloc_context(ops);
1027 ops->hash_init(context, args);
1028
1029 K1 = emalloc(ops->block_size);
1030 K2 = emalloc(ops->block_size);
1031 digest = emalloc(ops->digest_size);
1032 temp = emalloc(ops->digest_size);
1033
1034 /* Setup Keys that will be used for all hmac rounds */
1035 php_hash_hmac_prep_key(K1, ops, context, (unsigned char *) pass, pass_len);
1036 /* Convert K1 to opad -- 0x6A = 0x36 ^ 0x5C */
1037 php_hash_string_xor_char(K2, K1, 0x6A, ops->block_size);
1038
1039 /* Setup Main Loop to build a long enough result */
1040 if (length == 0) {
1041 length = ops->digest_size;
1042 if (!raw_output) {
1043 length = length * 2;
1044 }
1045 }
1046 digest_length = length;
1047 if (!raw_output) {
1048 digest_length = (zend_long) ceil((float) length / 2.0);
1049 }
1050
1051 loops = (zend_long) ceil((float) digest_length / (float) ops->digest_size);
1052
1053 result = safe_emalloc(loops, ops->digest_size, 0);
1054
1055 computed_salt = safe_emalloc(salt_len, 1, 4);
1056 memcpy(computed_salt, (unsigned char *) salt, salt_len);
1057
1058 for (i = 1; i <= loops; i++) {
1059 /* digest = hash_hmac(salt + pack('N', i), password) { */
1060
1061 /* pack("N", i) */
1062 computed_salt[salt_len] = (unsigned char) (i >> 24);
1063 computed_salt[salt_len + 1] = (unsigned char) ((i & 0xFF0000) >> 16);
1064 computed_salt[salt_len + 2] = (unsigned char) ((i & 0xFF00) >> 8);
1065 computed_salt[salt_len + 3] = (unsigned char) (i & 0xFF);
1066
1067 php_hash_hmac_round(digest, ops, context, K1, computed_salt, (zend_long) salt_len + 4);
1068 php_hash_hmac_round(digest, ops, context, K2, digest, ops->digest_size);
1069 /* } */
1070
1071 /* temp = digest */
1072 memcpy(temp, digest, ops->digest_size);
1073
1074 /*
1075 * Note that the loop starting at 1 is intentional, since we've already done
1076 * the first round of the algorithm.
1077 */
1078 for (j = 1; j < iterations; j++) {
1079 /* digest = hash_hmac(digest, password) { */
1080 php_hash_hmac_round(digest, ops, context, K1, digest, ops->digest_size);
1081 php_hash_hmac_round(digest, ops, context, K2, digest, ops->digest_size);
1082 /* } */
1083 /* temp ^= digest */
1084 php_hash_string_xor(temp, temp, digest, ops->digest_size);
1085 }
1086 /* result += temp */
1087 memcpy(result + ((i - 1) * ops->digest_size), temp, ops->digest_size);
1088 }
1089 /* Zero potentially sensitive variables */
1090 ZEND_SECURE_ZERO(K1, ops->block_size);
1091 ZEND_SECURE_ZERO(K2, ops->block_size);
1092 ZEND_SECURE_ZERO(computed_salt, salt_len + 4);
1093 efree(K1);
1094 efree(K2);
1095 efree(computed_salt);
1096 efree(context);
1097 efree(digest);
1098 efree(temp);
1099
1100 returnval = zend_string_alloc(length, 0);
1101 if (raw_output) {
1102 memcpy(ZSTR_VAL(returnval), result, length);
1103 } else {
1104 php_hash_bin2hex(ZSTR_VAL(returnval), result, digest_length);
1105 }
1106 ZSTR_VAL(returnval)[length] = 0;
1107 efree(result);
1108 RETURN_NEW_STR(returnval);
1109}
1110/* }}} */
1111
1112/* {{{ Compares two strings using the same time whether they're equal or not.
1113 A difference in length will leak */
1115{
1116 zval *known_zval, *user_zval;
1117 int result = 0;
1118
1119 if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &known_zval, &user_zval) == FAILURE) {
1120 RETURN_THROWS();
1121 }
1122
1123 /* We only allow comparing string to prevent unexpected results. */
1124 if (Z_TYPE_P(known_zval) != IS_STRING) {
1125 zend_argument_type_error(1, "must be of type string, %s given", zend_zval_value_name(known_zval));
1126 RETURN_THROWS();
1127 }
1128
1129 if (Z_TYPE_P(user_zval) != IS_STRING) {
1130 zend_argument_type_error(2, "must be of type string, %s given", zend_zval_value_name(user_zval));
1131 RETURN_THROWS();
1132 }
1133
1134 /* This is security sensitive code. Do not optimize this for speed. */
1135 result = php_safe_bcmp(Z_STR_P(known_zval), Z_STR_P(user_zval));
1136
1137 RETURN_BOOL(0 == result);
1138}
1139/* }}} */
1140
1141/* {{{ */
1143 /* Normally unreachable as private/final */
1144 zend_throw_exception(zend_ce_error, "Illegal call to private/final constructor", 0);
1145}
1146/* }}} */
1147
1148/* Module Housekeeping */
1149
1150#define PHP_HASH_HAVAL_REGISTER(p,b) php_hash_register_algo("haval" #b "," #p , &php_hash_##p##haval##b##_ops);
1151
1152#ifdef PHP_MHASH_BC
1153
1154#if 0
1155/* See #69823, we should not insert module into module_registry while doing startup */
1156
1158{
1160 php_info_print_table_row(2, "MHASH support", "Enabled");
1161 php_info_print_table_row(2, "MHASH API Version", "Emulated Support");
1163}
1164
1165zend_module_entry mhash_module_entry = {
1167 "mhash",
1168 NULL,
1169 NULL,
1170 NULL,
1171 NULL,
1172 NULL,
1176};
1177#endif
1178
1179static void mhash_init(INIT_FUNC_ARGS)
1180{
1181 char buf[128];
1182 int len;
1183 int algo_number = 0;
1184
1185 for (algo_number = 0; algo_number < MHASH_NUM_ALGOS; algo_number++) {
1186 struct mhash_bc_entry algorithm = mhash_to_hash[algo_number];
1187 if (algorithm.mhash_name == NULL) {
1188 continue;
1189 }
1190
1191 len = slprintf(buf, 127, "MHASH_%s", algorithm.mhash_name);
1192 zend_register_long_constant(buf, len, algorithm.value, CONST_PERSISTENT, module_number);
1193 }
1194
1195 /* TODO: this cause #69823 zend_register_internal_module(&mhash_module_entry); */
1196}
1197
1198/* {{{ Hash data with hash */
1200{
1201 zend_long algorithm;
1202 zend_string *algo = NULL;
1203 char *data, *key = NULL;
1204 size_t data_len, key_len = 0;
1205
1206 if (zend_parse_parameters(ZEND_NUM_ARGS(), "ls|s!", &algorithm, &data, &data_len, &key, &key_len) == FAILURE) {
1207 RETURN_THROWS();
1208 }
1209
1210 /* need to convert the first parameter from int constant to string algorithm name */
1211 if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
1212 struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1213 if (algorithm_lookup.hash_name) {
1214 algo = zend_string_init(algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name), 0);
1215 } else {
1217 }
1218 } else {
1220 }
1221
1222 if (key) {
1223 php_hash_do_hash_hmac(return_value, algo, data, data_len, key, key_len, 1, 0);
1224 } else {
1225 php_hash_do_hash(return_value, algo, data, data_len, 1, 0, NULL);
1226 }
1227
1228 if (algo) {
1229 zend_string_release(algo);
1230 }
1231}
1232/* }}} */
1233
1234/* {{{ Gets the name of hash */
1236{
1237 zend_long algorithm;
1238
1239 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &algorithm) == FAILURE) {
1240 RETURN_THROWS();
1241 }
1242
1243 if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
1244 struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1245 if (algorithm_lookup.mhash_name) {
1246 RETURN_STRING(algorithm_lookup.mhash_name);
1247 }
1248 }
1250}
1251/* }}} */
1252
1253/* {{{ Gets the number of available hashes */
1255{
1257 RETURN_THROWS();
1258 }
1259 RETURN_LONG(MHASH_NUM_ALGOS - 1);
1260}
1261/* }}} */
1262
1263/* {{{ Gets the block size of hash */
1265{
1266 zend_long algorithm;
1267
1268 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &algorithm) == FAILURE) {
1269 RETURN_THROWS();
1270 }
1272
1273 if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
1274 struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1275 if (algorithm_lookup.mhash_name) {
1276 const php_hash_ops *ops = zend_hash_str_find_ptr(&php_hash_hashtable, algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
1277 if (ops) {
1278 RETVAL_LONG(ops->digest_size);
1279 }
1280 }
1281 }
1282}
1283/* }}} */
1284
1285#define SALT_SIZE 8
1286
1287/* {{{ Generates a key using hash functions */
1289{
1290 zend_long algorithm, l_bytes;
1291 int bytes;
1292 char *password, *salt;
1293 size_t password_len, salt_len;
1294 char padded_salt[SALT_SIZE];
1295
1296 if (zend_parse_parameters(ZEND_NUM_ARGS(), "lssl", &algorithm, &password, &password_len, &salt, &salt_len, &l_bytes) == FAILURE) {
1297 RETURN_THROWS();
1298 }
1299
1300 bytes = (int)l_bytes;
1301 if (bytes <= 0){
1302 zend_argument_value_error(4, "must be a greater than 0");
1303 RETURN_THROWS();
1304 }
1305
1306 salt_len = MIN(salt_len, SALT_SIZE);
1307
1308 memcpy(padded_salt, salt, salt_len);
1309 if (salt_len < SALT_SIZE) {
1310 memset(padded_salt + salt_len, 0, SALT_SIZE - salt_len);
1311 }
1312 salt_len = SALT_SIZE;
1313
1315 if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
1316 struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1317 if (algorithm_lookup.mhash_name) {
1318 const php_hash_ops *ops = zend_hash_str_find_ptr(&php_hash_hashtable, algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
1319 if (ops) {
1320 unsigned char null = '\0';
1321 void *context;
1322 char *key, *digest;
1323 int i = 0, j = 0;
1324 size_t block_size = ops->digest_size;
1325 size_t times = bytes / block_size;
1326
1327 if ((bytes % block_size) != 0) {
1328 times++;
1329 }
1330
1331 context = php_hash_alloc_context(ops);
1332 ops->hash_init(context, NULL);
1333
1334 key = ecalloc(1, times * block_size);
1335 digest = emalloc(ops->digest_size + 1);
1336
1337 for (i = 0; i < times; i++) {
1338 ops->hash_init(context, NULL);
1339
1340 for (j=0;j<i;j++) {
1341 ops->hash_update(context, &null, 1);
1342 }
1343 ops->hash_update(context, (unsigned char *)padded_salt, salt_len);
1344 ops->hash_update(context, (unsigned char *)password, password_len);
1345 ops->hash_final((unsigned char *)digest, context);
1346 memcpy( &key[i*block_size], digest, block_size);
1347 }
1348
1349 RETVAL_STRINGL(key, bytes);
1350 ZEND_SECURE_ZERO(key, bytes);
1351 efree(digest);
1352 efree(context);
1353 efree(key);
1354 }
1355 }
1356 }
1357}
1358/* }}} */
1359
1360#endif
1361
1362/* ----------------------------------------------------------------------- */
1363
1364/* {{{ php_hashcontext_create */
1365static zend_object* php_hashcontext_create(zend_class_entry *ce) {
1366 php_hashcontext_object *objval = zend_object_alloc(sizeof(php_hashcontext_object), ce);
1367 zend_object *zobj = &objval->std;
1368
1371 zobj->handlers = &php_hashcontext_handlers;
1372
1373 return zobj;
1374}
1375/* }}} */
1376
1377/* {{{ php_hashcontext_dtor */
1378static void php_hashcontext_dtor(zend_object *obj) {
1379 php_hashcontext_object *hash = php_hashcontext_from_object(obj);
1380
1381 if (hash->context) {
1382 efree(hash->context);
1383 hash->context = NULL;
1384 }
1385
1386 if (hash->key) {
1387 ZEND_SECURE_ZERO(hash->key, hash->ops->block_size);
1388 efree(hash->key);
1389 hash->key = NULL;
1390 }
1391}
1392/* }}} */
1393
1394static void php_hashcontext_free(zend_object *obj) {
1395 php_hashcontext_dtor(obj);
1397}
1398
1399/* {{{ php_hashcontext_clone */
1400static zend_object *php_hashcontext_clone(zend_object *zobj) {
1401 php_hashcontext_object *oldobj = php_hashcontext_from_object(zobj);
1402 zend_object *znew = php_hashcontext_create(zobj->ce);
1403 php_hashcontext_object *newobj = php_hashcontext_from_object(znew);
1404
1405 if (!oldobj->context) {
1406 zend_throw_exception(zend_ce_value_error, "Cannot clone a finalized HashContext", 0);
1407 return znew;
1408 }
1409
1411
1412 newobj->ops = oldobj->ops;
1413 newobj->options = oldobj->options;
1414 newobj->context = php_hash_alloc_context(newobj->ops);
1415 newobj->ops->hash_init(newobj->context, NULL);
1416
1417 if (SUCCESS != newobj->ops->hash_copy(newobj->ops, oldobj->context, newobj->context)) {
1418 efree(newobj->context);
1419 newobj->context = NULL;
1420 return znew;
1421 }
1422
1423 newobj->key = ecalloc(1, newobj->ops->block_size);
1424 if (oldobj->key) {
1425 memcpy(newobj->key, oldobj->key, newobj->ops->block_size);
1426 }
1427
1428 return znew;
1429}
1430/* }}} */
1431
1432/* Serialization format: 5-element array
1433 Index 0: hash algorithm (string)
1434 Index 1: options (long, 0)
1435 Index 2: hash-determined serialization of context state (usually array)
1436 Index 3: magic number defining layout of context state (long, usually 2)
1437 Index 4: properties (array)
1438
1439 HashContext serializations are not necessarily portable between architectures or
1440 PHP versions. If the format of a serialized hash context changes, that should
1441 be reflected in either a different value of `magic` or a different format of
1442 the serialized context state. Most context states are unparsed and parsed using
1443 a spec string, such as "llb128.", using the format defined by
1444 `php_hash_serialize_spec`/`php_hash_unserialize_spec`. Some hash algorithms must
1445 also check the unserialized state for validity, to ensure that using an
1446 unserialized context is safe from memory errors.
1447
1448 Currently HASH_HMAC contexts cannot be serialized, because serializing them
1449 would require serializing the HMAC key in plaintext. */
1450
1451/* {{{ Serialize the object */
1453{
1454 zval *object = ZEND_THIS;
1455 php_hashcontext_object *hash = php_hashcontext_from_object(Z_OBJ_P(object));
1456 zend_long magic = 0;
1457 zval tmp;
1458
1460 RETURN_THROWS();
1461 }
1462
1464
1465 if (!hash->ops->hash_serialize) {
1466 goto serialize_failure;
1467 } else if (hash->options & PHP_HASH_HMAC) {
1468 zend_throw_exception(NULL, "HashContext with HASH_HMAC option cannot be serialized", 0);
1469 RETURN_THROWS();
1470 }
1471
1472 ZVAL_STRING(&tmp, hash->ops->algo);
1474
1475 ZVAL_LONG(&tmp, hash->options);
1477
1478 if (hash->ops->hash_serialize(hash, &magic, &tmp) != SUCCESS) {
1479 goto serialize_failure;
1480 }
1482
1483 ZVAL_LONG(&tmp, magic);
1485
1486 /* members */
1488 Z_TRY_ADDREF(tmp);
1490
1491 return;
1492
1493serialize_failure:
1494 zend_throw_exception_ex(NULL, 0, "HashContext for algorithm \"%s\" cannot be serialized", hash->ops->algo);
1495 RETURN_THROWS();
1496}
1497/* }}} */
1498
1499/* {{{ unserialize the object */
1501{
1502 zval *object = ZEND_THIS;
1503 php_hashcontext_object *hash = php_hashcontext_from_object(Z_OBJ_P(object));
1504 HashTable *data;
1505 zval *algo_zv, *magic_zv, *options_zv, *hash_zv, *members_zv;
1507 int unserialize_result;
1508 const php_hash_ops *ops;
1509
1511 RETURN_THROWS();
1512 }
1513
1514 if (hash->context) {
1515 zend_throw_exception(NULL, "HashContext::__unserialize called on initialized object", 0);
1516 RETURN_THROWS();
1517 }
1518
1519 algo_zv = zend_hash_index_find(data, 0);
1520 options_zv = zend_hash_index_find(data, 1);
1521 hash_zv = zend_hash_index_find(data, 2);
1522 magic_zv = zend_hash_index_find(data, 3);
1523 members_zv = zend_hash_index_find(data, 4);
1524
1525 if (!algo_zv || Z_TYPE_P(algo_zv) != IS_STRING
1526 || !magic_zv || Z_TYPE_P(magic_zv) != IS_LONG
1527 || !options_zv || Z_TYPE_P(options_zv) != IS_LONG
1528 || !hash_zv
1529 || !members_zv || Z_TYPE_P(members_zv) != IS_ARRAY) {
1530 zend_throw_exception(NULL, "Incomplete or ill-formed serialization data", 0);
1531 RETURN_THROWS();
1532 }
1533
1534 magic = Z_LVAL_P(magic_zv);
1535 options = Z_LVAL_P(options_zv);
1536 if (options & PHP_HASH_HMAC) {
1537 zend_throw_exception(NULL, "HashContext with HASH_HMAC option cannot be serialized", 0);
1538 RETURN_THROWS();
1539 }
1540
1541 ops = php_hash_fetch_ops(Z_STR_P(algo_zv));
1542 if (!ops) {
1543 zend_throw_exception(NULL, "Unknown hash algorithm", 0);
1544 RETURN_THROWS();
1545 } else if (!ops->hash_unserialize) {
1546 zend_throw_exception_ex(NULL, 0, "Hash algorithm \"%s\" cannot be unserialized", ops->algo);
1547 RETURN_THROWS();
1548 }
1549
1550 hash->ops = ops;
1551 hash->context = php_hash_alloc_context(ops);
1552 hash->options = options;
1553 ops->hash_init(hash->context, NULL);
1554
1555 unserialize_result = ops->hash_unserialize(hash, magic, hash_zv);
1556 if (unserialize_result != SUCCESS) {
1557 zend_throw_exception_ex(NULL, 0, "Incomplete or ill-formed serialization data (\"%s\" code %d)", ops->algo, unserialize_result);
1558 /* free context */
1559 php_hashcontext_dtor(Z_OBJ_P(object));
1560 RETURN_THROWS();
1561 }
1562
1563 object_properties_load(&hash->std, Z_ARRVAL_P(members_zv));
1564}
1565/* }}} */
1566
1568{
1569 zval *object = ZEND_THIS;
1570 php_hashcontext_object *hash = php_hashcontext_from_object(Z_OBJ_P(object));
1571
1573
1574 zval tmp;
1575
1577
1578 ZVAL_STRING(&tmp, hash->ops->algo);
1579 zend_hash_str_update(Z_ARR_P(return_value), "algo", strlen("algo"), &tmp);
1580}
1581
1582/* {{{ PHP_MINIT_FUNCTION */
1584{
1585 zend_hash_init(&php_hash_hashtable, 35, NULL, NULL, 1);
1586
1632
1638
1644
1650
1651 register_hash_symbols(module_number);
1652
1653 php_hashcontext_ce = register_class_HashContext();
1654 php_hashcontext_ce->create_object = php_hashcontext_create;
1655
1656 memcpy(&php_hashcontext_handlers, &std_object_handlers,
1657 sizeof(zend_object_handlers));
1658 php_hashcontext_handlers.offset = XtOffsetOf(php_hashcontext_object, std);
1659 php_hashcontext_handlers.free_obj = php_hashcontext_free;
1660 php_hashcontext_handlers.clone_obj = php_hashcontext_clone;
1661
1662#ifdef PHP_MHASH_BC
1663 mhash_init(INIT_FUNC_ARGS_PASSTHRU);
1664#endif
1665
1666 return SUCCESS;
1667}
1668/* }}} */
1669
1670/* {{{ PHP_MSHUTDOWN_FUNCTION */
1672{
1673 zend_hash_destroy(&php_hash_hashtable);
1674
1675 return SUCCESS;
1676}
1677/* }}} */
1678
1679/* {{{ PHP_MINFO_FUNCTION */
1681{
1682 char buffer[2048];
1683 zend_string *str;
1684 char *s = buffer, *e = s + sizeof(buffer);
1685
1686 ZEND_HASH_MAP_FOREACH_STR_KEY(&php_hash_hashtable, str) {
1687 s += slprintf(s, e - s, "%s ", ZSTR_VAL(str));
1689 *s = 0;
1690
1692 php_info_print_table_row(2, "hash support", "enabled");
1693 php_info_print_table_row(2, "Hashing Engines", buffer);
1695
1696#ifdef PHP_MHASH_BC
1698 php_info_print_table_row(2, "MHASH support", "Enabled");
1699 php_info_print_table_row(2, "MHASH API Version", "Emulated Support");
1701#endif
1702
1703}
1704/* }}} */
1705
1706/* {{{ hash_module_entry */
1710 ext_functions,
1711 PHP_MINIT(hash),
1713 NULL, /* RINIT */
1714 NULL, /* RSHUTDOWN */
1715 PHP_MINFO(hash),
1718};
1719/* }}} */
size_t len
Definition apprentice.c:174
count(Countable|array $value, int $mode=COUNT_NORMAL)
ceil(int|float $num)
char s[4]
Definition cdf.c:77
zval * zv
Definition ffi.c:3975
zend_long n
Definition ffi.c:4979
memcpy(ptr1, ptr2, size)
memset(ptr, 0, type->size)
zval * val
Definition ffi.c:4262
zend_ffi_ctype_name_buf buf
Definition ffi.c:4685
const php_stream_filter_ops * ops
Definition filters.c:1899
zval * zcontext
zend_long offset
#define NULL
Definition gdcache.h:45
PHP_HASH_API zend_result php_hash_serialize(const php_hashcontext_object *hash, zend_long *magic, zval *zv)
Definition hash.c:334
PHP_HASH_API zend_result php_hash_serialize_spec(const php_hashcontext_object *hash, zval *zv, const char *spec)
Definition hash.c:232
PHP_HASH_API zend_result php_hash_copy(const void *ops, const void *orig_context, void *dest_context)
Definition hash.c:124
#define PHP_HASH_HAVAL_REGISTER(p, b)
Definition hash.c:1150
#define __alignof__(type)
Definition hash.c:40
zend_module_entry hash_module_entry
Definition hash.c:1707
PHP_HASH_API void php_hash_register_algo(const char *algo, const php_hash_ops *ops)
Definition hash.c:115
zend_class_entry * php_hashcontext_ce
Definition hash.c:45
PHP_HASH_API int php_hash_unserialize_spec(php_hashcontext_object *hash, const zval *zv, const char *spec)
Definition hash.c:280
#define PHP_HASHCONTEXT_VERIFY(hash)
Definition hash.c:681
PHP_HASH_API int php_hash_unserialize(php_hashcontext_object *hash, zend_long magic, const zval *zv)
Definition hash.c:345
PHP_HASH_API const php_hash_ops * php_hash_fetch_ops(zend_string *algo)
Definition hash.c:105
mhash_count()
Definition hash.stub.php:89
mhash_get_hash_name(int $algo)
Definition hash.stub.php:80
hash_algos()
Definition hash.stub.php:51
mhash_get_block_size(int $algo)
Definition hash.stub.php:74
hash_update(HashContext $context, string $data)
Definition hash.stub.php:32
hash_update_stream(HashContext $context, $stream, int $length=-1)
Definition hash.stub.php:35
hash_file(string $algo, string $filename, bool $binary=false, array $options=[])
Definition hash.stub.php:15
hash_equals(#[\SensitiveParameter] string $known_string, #[\SensitiveParameter] string $user_string)
Definition hash.stub.php:65
hash_hmac_file(string $algo, string $filename, #[\SensitiveParameter] string $key, bool $binary=false)
Definition hash.stub.php:25
hash_init(string $algo, int $flags=0, #[\SensitiveParameter] string $key="", array $options=[])
Definition hash.stub.php:30
hash_final(HashContext $context, bool $binary=false)
Definition hash.stub.php:41
hash_hmac(string $algo, string $data, #[\SensitiveParameter] string $key, bool $binary=false)
Definition hash.stub.php:20
hash_hmac_algos()
Definition hash.stub.php:58
hash_hkdf(string $algo, #[\SensitiveParameter] string $key, int $length=0, string $info="", string $salt="")
Definition hash.stub.php:70
hash_pbkdf2(string $algo, #[\SensitiveParameter] string $password, string $salt, int $iterations, int $length=0, bool $binary=false, array $options=[])
Definition hash.stub.php:63
hash(string $algo, string $data, bool $binary=false, array $options=[])
Definition hash.stub.php:12
mhash(int $algo, string $data, ?string $key=null)
Definition hash.stub.php:95
hash_update_file(HashContext $context, string $filename, $stream_context=null)
Definition hash.stub.php:38
mhash_keygen_s2k(int $algo, string $password, string $salt, int $length)
Definition hash.stub.php:86
hash_copy(HashContext $context)
Definition hash.stub.php:44
const php_hash_ops php_hash_adler32_ops
const php_hash_ops php_hash_crc32c_ops
Definition hash_crc32.c:123
const php_hash_ops php_hash_crc32b_ops
Definition hash_crc32.c:108
const php_hash_ops php_hash_crc32_ops
Definition hash_crc32.c:93
const php_hash_ops php_hash_fnv1a32_ops
Definition hash_fnv.c:38
const php_hash_ops php_hash_fnv1a64_ops
Definition hash_fnv.c:68
const php_hash_ops php_hash_fnv132_ops
Definition hash_fnv.c:23
const php_hash_ops php_hash_fnv164_ops
Definition hash_fnv.c:53
const php_hash_ops php_hash_gost_crypto_ops
Definition hash_gost.c:335
const php_hash_ops php_hash_gost_ops
Definition hash_gost.c:320
const php_hash_ops php_hash_joaat_ops
Definition hash_joaat.c:24
const php_hash_ops php_hash_md2_ops
Definition hash_md.c:52
const php_hash_ops php_hash_md4_ops
Definition hash_md.c:35
const php_hash_ops php_hash_md5_ops
Definition hash_md.c:20
const php_hash_ops php_hash_murmur3c_ops
Definition hash_murmur.c:86
const php_hash_ops php_hash_murmur3f_ops
const php_hash_ops php_hash_murmur3a_ops
Definition hash_murmur.c:24
const php_hash_ops php_hash_ripemd128_ops
Definition hash_ripemd.c:24
const php_hash_ops php_hash_ripemd160_ops
Definition hash_ripemd.c:39
const php_hash_ops php_hash_ripemd320_ops
Definition hash_ripemd.c:69
const php_hash_ops php_hash_ripemd256_ops
Definition hash_ripemd.c:54
#define K(n)
#define SUCCESS
Definition hash_sha3.c:261
const php_hash_ops php_hash_sha256_ops
Definition hash_sha.c:83
const php_hash_ops php_hash_sha384_ops
Definition hash_sha.c:615
const php_hash_ops php_hash_sha1_ops
Definition hash_sha.c:66
const php_hash_ops php_hash_sha512_224_ops
Definition hash_sha.c:824
const php_hash_ops php_hash_sha224_ops
Definition hash_sha.c:98
const php_hash_ops php_hash_sha512_256_ops
Definition hash_sha.c:809
const php_hash_ops php_hash_sha512_ops
Definition hash_sha.c:794
const php_hash_ops php_hash_snefru_ops
#define pass(a, b, c, mul)
Definition hash_tiger.c:50
const php_hash_ops php_hash_whirlpool_ops
const php_hash_ops php_hash_xxh32_ops
Definition hash_xxhash.c:25
const php_hash_ops php_hash_xxh3_128_ops
const php_hash_ops php_hash_xxh3_64_ops
const php_hash_ops php_hash_xxh64_ops
Definition hash_xxhash.c:92
again j
php_info_print_table_start()
Definition info.c:1064
php_info_print_table_row(2, "PDO Driver for Firebird", "enabled")
php_info_print_table_end()
Definition info.c:1074
#define PHP_FUNCTION
Definition php.h:364
#define PHP_MSHUTDOWN_FUNCTION
Definition php.h:401
#define PHP_MINFO
Definition php.h:396
#define PHP_MINIT_FUNCTION
Definition php.h:400
#define PHP_MSHUTDOWN
Definition php.h:393
#define PHP_MINFO_FUNCTION
Definition php.h:404
#define INT_MAX
Definition php.h:237
PHPAPI int php_safe_bcmp(const zend_string *a, const zend_string *b)
Definition safe_bcmp.c:26
#define PHP_MINIT
Definition php.h:392
#define PHP_METHOD
Definition php.h:365
unsigned const char * pos
Definition php_ffi.h:52
#define PHP_HASH_EXTNAME
Definition php_hash.h:22
const php_hash_ops php_hash_sha3_256_ops
const php_hash_ops php_hash_sha3_224_ops
#define PHP_HASH_VERSION
Definition php_hash.h:23
struct _php_hashcontext_object php_hashcontext_object
Definition php_hash.h:32
const php_hash_ops php_hash_4tiger128_ops
const php_hash_ops php_hash_3tiger128_ops
const php_hash_ops php_hash_3tiger192_ops
#define PHP_HASH_API
Definition php_hash.h:144
const php_hash_ops php_hash_4tiger160_ops
const php_hash_ops php_hash_sha3_384_ops
#define PHP_HASH_HMAC
Definition php_hash.h:26
#define PHP_MHASH_VERSION
Definition php_hash.h:24
const php_hash_ops php_hash_4tiger192_ops
const php_hash_ops php_hash_3tiger160_ops
struct _php_hash_ops php_hash_ops
#define PHP_HASH_SERIALIZE_MAGIC_SPEC
Definition php_hash.h:28
const php_hash_ops php_hash_sha3_512_ops
PHP_JSON_API size_t int options
Definition php_json.h:102
reflection_object_handlers clone_obj
unsigned char key[REFLECTION_KEY_LEN]
#define php_stream_context_from_zval(zcontext, nocontext)
struct _php_stream php_stream
Definition php_streams.h:96
struct _php_stream_context php_stream_context
Definition php_streams.h:98
#define REPORT_ERRORS
#define php_stream_read(stream, buf, count)
#define php_stream_from_zval(xstr, pzval)
#define php_stream_open_wrapper_ex(path, mode, options, opened, context)
#define php_stream_close(stream)
zend_constant * data
#define slprintf
Definition snprintf.h:89
#define FG(v)
Definition file.h:117
size_t block_size
Definition php_hash.h:52
php_hash_copy_func_t hash_copy
Definition php_hash.h:46
size_t context_size
Definition php_hash.h:53
php_hash_init_func_t hash_init
Definition php_hash.h:43
unsigned char * key
Definition php_hash.h:62
const php_hash_ops * ops
Definition php_hash.h:58
Definition file.h:177
Definition dce.c:49
Definition file.h:202
ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const char *format,...)
Definition zend.c:1772
ZEND_API const char * zend_zval_value_name(const zval *arg)
Definition zend_API.c:148
ZEND_API zend_result object_init_ex(zval *arg, zend_class_entry *class_type)
Definition zend_API.c:1849
ZEND_API zend_result zend_parse_parameters(uint32_t num_args, const char *type_spec,...)
Definition zend_API.c:1300
ZEND_API void object_properties_init(zend_object *object, zend_class_entry *class_type)
Definition zend_API.c:1688
ZEND_API ZEND_COLD void zend_argument_must_not_be_empty_error(uint32_t arg_num)
Definition zend_API.c:443
ZEND_API ZEND_COLD void zend_argument_value_error(uint32_t arg_num, const char *format,...)
Definition zend_API.c:433
ZEND_API ZEND_COLD void zend_argument_type_error(uint32_t arg_num, const char *format,...)
Definition zend_API.c:423
ZEND_API void object_properties_load(zend_object *object, HashTable *properties)
Definition zend_API.c:1728
ZEND_API zend_result add_next_index_str(zval *arg, zend_string *str)
Definition zend_API.c:2177
#define CHECK_NULL_PATH(p, l)
Definition zend_API.h:950
#define ZEND_NUM_ARGS()
Definition zend_API.h:530
#define RETURN_STRING(s)
Definition zend_API.h:1043
#define ZEND_PARSE_PARAMETERS_END()
Definition zend_API.h:1641
#define RETURN_FALSE
Definition zend_API.h:1058
#define ZEND_PARSE_PARAMETERS_NONE()
Definition zend_API.h:1623
#define ZVAL_STRING(z, s)
Definition zend_API.h:956
#define Z_PARAM_OPTIONAL
Definition zend_API.h:1667
#define zend_parse_parameters_none()
Definition zend_API.h:353
#define Z_PARAM_STRING(dest, dest_len)
Definition zend_API.h:2071
#define Z_PARAM_STR(dest)
Definition zend_API.h:2086
#define ZEND_PARSE_PARAMETERS_START(min_num_args, max_num_args)
Definition zend_API.h:1620
#define RETURN_LONG(l)
Definition zend_API.h:1037
#define RETURN_BOOL(b)
Definition zend_API.h:1035
#define RETURN_NEW_STR(s)
Definition zend_API.h:1041
#define ZEND_METHOD(classname, name)
Definition zend_API.h:76
#define RETURN_THROWS()
Definition zend_API.h:1060
#define Z_PARAM_ARRAY_HT(dest)
Definition zend_API.h:1852
#define RETURN_STR(s)
Definition zend_API.h:1039
#define RETVAL_OBJ(r)
Definition zend_API.h:1026
#define ZEND_THIS
Definition zend_API.h:523
#define RETVAL_LONG(l)
Definition zend_API.h:1011
#define Z_PARAM_BOOL(dest)
Definition zend_API.h:1726
#define ZVAL_STRINGL(z, s, l)
Definition zend_API.h:952
#define RETVAL_FALSE
Definition zend_API.h:1032
#define RETURN_TRUE
Definition zend_API.h:1059
#define RETVAL_STRINGL(s, l)
Definition zend_API.h:1018
#define array_init(arg)
Definition zend_API.h:537
#define ecalloc(nmemb, size)
Definition zend_alloc.h:158
#define efree(ptr)
Definition zend_alloc.h:155
#define safe_emalloc(nmemb, size, offset)
Definition zend_alloc.h:154
#define emalloc(size)
Definition zend_alloc.h:151
struct _zval_struct zval
strlen(string $string)
zend_string_release_ex(func->internal_function.function_name, 0)
zval * args
ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number)
#define CONST_PERSISTENT
ZEND_API zend_class_entry * zend_ce_value_error
ZEND_API ZEND_COLD zend_object * zend_throw_exception(zend_class_entry *exception_ce, const char *message, zend_long code)
ZEND_API ZEND_COLD zend_object * zend_throw_exception_ex(zend_class_entry *exception_ce, zend_long code, const char *format,...)
ZEND_API zend_class_entry * zend_ce_error
ZEND_API void ZEND_FASTCALL zend_hash_destroy(HashTable *ht)
Definition zend_hash.c:1727
ZEND_API zval *ZEND_FASTCALL zend_hash_next_index_insert(HashTable *ht, zval *pData)
Definition zend_hash.c:1224
ZEND_API zval *ZEND_FASTCALL zend_hash_str_update(HashTable *ht, const char *str, size_t len, zval *pData)
Definition zend_hash.c:1031
ZEND_API zval *ZEND_FASTCALL zend_hash_index_find(const HashTable *ht, zend_ulong h)
Definition zend_hash.c:2701
#define zend_hash_init(ht, nSize, pHashFunction, pDestructor, persistent)
Definition zend_hash.h:108
#define ZEND_HASH_MAP_FOREACH_STR_KEY(ht, _key)
Definition zend_hash.h:1346
#define ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(ht, _key, _ptr)
Definition zend_hash.h:1433
#define ZEND_HASH_FOREACH_END()
Definition zend_hash.h:1086
int32_t zend_long
Definition zend_long.h:42
struct _zend_string zend_string
#define INIT_FUNC_ARGS
#define STANDARD_MODULE_HEADER
#define INIT_FUNC_ARGS_PASSTHRU
struct _zend_module_entry zend_module_entry
#define STANDARD_MODULE_PROPERTIES
ZEND_API HashTable * zend_std_get_properties(zend_object *zobj)
ZEND_API const zend_object_handlers std_object_handlers
ZEND_API void ZEND_FASTCALL zend_objects_clone_members(zend_object *new_object, zend_object *old_object)
ZEND_API void ZEND_FASTCALL zend_object_std_init(zend_object *object, zend_class_entry *ce)
ZEND_API void zend_object_std_dtor(zend_object *object)
ZEND_API char *ZEND_FASTCALL zend_str_tolower_dup(const char *source, size_t length)
#define MIN(a, b)
#define ZEND_SECURE_ZERO(var, size)
#define XtOffsetOf(s_type, field)
#define ZEND_ASSERT(c)
struct _zend_class_entry zend_class_entry
struct _zend_object zend_object
ZEND_API zend_string_init_interned_func_t zend_string_init_interned
Definition zend_string.c:31
#define ZSTR_VAL(zstr)
Definition zend_string.h:68
#define ZSTR_LEN(zstr)
Definition zend_string.h:69
#define Z_OBJ_HANDLER_P(zv_p, hf)
Definition zend_types.h:996
#define Z_TYPE_P(zval_p)
Definition zend_types.h:660
#define Z_STRVAL_P(zval_p)
Definition zend_types.h:975
#define Z_ARRVAL_P(zval_p)
Definition zend_types.h:987
#define ZVAL_LONG(z, l)
#define IS_STRING
Definition zend_types.h:606
struct _zend_array HashTable
Definition zend_types.h:386
#define Z_OBJ_P(zval_p)
Definition zend_types.h:990
#define IS_ARRAY
Definition zend_types.h:607
#define Z_STR_P(zval_p)
Definition zend_types.h:972
#define Z_STRLEN_P(zval_p)
Definition zend_types.h:978
@ FAILURE
Definition zend_types.h:61
#define Z_TRY_ADDREF(z)
#define IS_LONG
Definition zend_types.h:604
#define ZVAL_ARR(z, a)
ZEND_RESULT_CODE zend_result
Definition zend_types.h:64
struct _zend_object_handlers zend_object_handlers
Definition zend_types.h:88
#define Z_ARR_P(zval_p)
Definition zend_types.h:984
#define Z_LVAL_P(zval_p)
Definition zend_types.h:966
ZEND_API void zval_ptr_dtor(zval *zval_ptr)
zval * return_value
bool result
value
zend_object * zobj
out($f, $s)