1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 7 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Author: Sara Golemon <pollita@php.net> |
16 | Scott MacVicar <scottmac@php.net> |
17 +----------------------------------------------------------------------+
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <math.h>
25 #include "php_hash.h"
26 #include "ext/standard/info.h"
27 #include "ext/standard/file.h"
28
29 #include "zend_interfaces.h"
30 #include "zend_exceptions.h"
31
32 HashTable php_hash_hashtable;
33 zend_class_entry *php_hashcontext_ce;
34 static zend_object_handlers php_hashcontext_handlers;
35
36 #ifdef PHP_MHASH_BC
37 struct mhash_bc_entry {
38 char *mhash_name;
39 char *hash_name;
40 int value;
41 };
42
43 #define MHASH_NUM_ALGOS 35
44
45 static struct mhash_bc_entry mhash_to_hash[MHASH_NUM_ALGOS] = {
46 {"CRC32", "crc32", 0}, /* used by bzip */
47 {"MD5", "md5", 1},
48 {"SHA1", "sha1", 2},
49 {"HAVAL256", "haval256,3", 3},
50 {NULL, NULL, 4},
51 {"RIPEMD160", "ripemd160", 5},
52 {NULL, NULL, 6},
53 {"TIGER", "tiger192,3", 7},
54 {"GOST", "gost", 8},
55 {"CRC32B", "crc32b", 9}, /* used by ethernet (IEEE 802.3), gzip, zip, png, etc */
56 {"HAVAL224", "haval224,3", 10},
57 {"HAVAL192", "haval192,3", 11},
58 {"HAVAL160", "haval160,3", 12},
59 {"HAVAL128", "haval128,3", 13},
60 {"TIGER128", "tiger128,3", 14},
61 {"TIGER160", "tiger160,3", 15},
62 {"MD4", "md4", 16},
63 {"SHA256", "sha256", 17},
64 {"ADLER32", "adler32", 18},
65 {"SHA224", "sha224", 19},
66 {"SHA512", "sha512", 20},
67 {"SHA384", "sha384", 21},
68 {"WHIRLPOOL", "whirlpool", 22},
69 {"RIPEMD128", "ripemd128", 23},
70 {"RIPEMD256", "ripemd256", 24},
71 {"RIPEMD320", "ripemd320", 25},
72 {NULL, NULL, 26}, /* support needs to be added for snefru 128 */
73 {"SNEFRU256", "snefru256", 27},
74 {"MD2", "md2", 28},
75 {"FNV132", "fnv132", 29},
76 {"FNV1A32", "fnv1a32", 30},
77 {"FNV164", "fnv164", 31},
78 {"FNV1A64", "fnv1a64", 32},
79 {"JOAAT", "joaat", 33},
80 {"CRC32C", "crc32c", 34}, /* Castagnoli's CRC, used by iSCSI, SCTP, Btrfs, ext4, etc */
81 };
82 #endif
83
84 /* Hash Registry Access */
85
php_hash_fetch_ops(const char * algo,size_t algo_len)86 PHP_HASH_API const php_hash_ops *php_hash_fetch_ops(const char *algo, size_t algo_len) /* {{{ */
87 {
88 char *lower = zend_str_tolower_dup(algo, algo_len);
89 php_hash_ops *ops = zend_hash_str_find_ptr(&php_hash_hashtable, lower, algo_len);
90 efree(lower);
91
92 return ops;
93 }
94 /* }}} */
95
php_hash_register_algo(const char * algo,const php_hash_ops * ops)96 PHP_HASH_API void php_hash_register_algo(const char *algo, const php_hash_ops *ops) /* {{{ */
97 {
98 size_t algo_len = strlen(algo);
99 char *lower = zend_str_tolower_dup(algo, algo_len);
100 zend_hash_add_ptr(&php_hash_hashtable, zend_string_init_interned(lower, algo_len, 1), (void *) ops);
101 efree(lower);
102 }
103 /* }}} */
104
php_hash_copy(const void * ops,void * orig_context,void * dest_context)105 PHP_HASH_API int php_hash_copy(const void *ops, void *orig_context, void *dest_context) /* {{{ */
106 {
107 php_hash_ops *hash_ops = (php_hash_ops *)ops;
108
109 memcpy(dest_context, orig_context, hash_ops->context_size);
110 return SUCCESS;
111 }
112 /* }}} */
113
114 /* Userspace */
115
php_hash_do_hash(INTERNAL_FUNCTION_PARAMETERS,int isfilename,zend_bool raw_output_default)116 static void php_hash_do_hash(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_bool raw_output_default) /* {{{ */
117 {
118 zend_string *digest;
119 char *algo, *data;
120 size_t algo_len, data_len;
121 zend_bool raw_output = raw_output_default;
122 const php_hash_ops *ops;
123 void *context;
124 php_stream *stream = NULL;
125
126 if (zend_parse_parameters(ZEND_NUM_ARGS(), "ss|b", &algo, &algo_len, &data, &data_len, &raw_output) == FAILURE) {
127 return;
128 }
129
130 ops = php_hash_fetch_ops(algo, algo_len);
131 if (!ops) {
132 php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo);
133 RETURN_FALSE;
134 }
135 if (isfilename) {
136 if (CHECK_NULL_PATH(data, data_len)) {
137 php_error_docref(NULL, E_WARNING, "Invalid path");
138 RETURN_FALSE;
139 }
140 stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
141 if (!stream) {
142 /* Stream will report errors opening file */
143 RETURN_FALSE;
144 }
145 }
146
147 context = emalloc(ops->context_size);
148 ops->hash_init(context);
149
150 if (isfilename) {
151 char buf[1024];
152 ssize_t n;
153
154 while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
155 ops->hash_update(context, (unsigned char *) buf, n);
156 }
157 php_stream_close(stream);
158 if (n < 0) {
159 efree(context);
160 RETURN_FALSE;
161 }
162 } else {
163 ops->hash_update(context, (unsigned char *) data, data_len);
164 }
165
166 digest = zend_string_alloc(ops->digest_size, 0);
167 ops->hash_final((unsigned char *) ZSTR_VAL(digest), context);
168 efree(context);
169
170 if (raw_output) {
171 ZSTR_VAL(digest)[ops->digest_size] = 0;
172 RETURN_NEW_STR(digest);
173 } else {
174 zend_string *hex_digest = zend_string_safe_alloc(ops->digest_size, 2, 0, 0);
175
176 php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
177 ZSTR_VAL(hex_digest)[2 * ops->digest_size] = 0;
178 zend_string_release_ex(digest, 0);
179 RETURN_NEW_STR(hex_digest);
180 }
181 }
182 /* }}} */
183
184 /* {{{ proto string hash(string algo, string data[, bool raw_output = false])
185 Generate a hash of a given input string
186 Returns lowercase hexits by default */
PHP_FUNCTION(hash)187 PHP_FUNCTION(hash)
188 {
189 php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
190 }
191 /* }}} */
192
193 /* {{{ proto string hash_file(string algo, string filename[, bool raw_output = false])
194 Generate a hash of a given file
195 Returns lowercase hexits by default */
PHP_FUNCTION(hash_file)196 PHP_FUNCTION(hash_file)
197 {
198 php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 0);
199 }
200 /* }}} */
201
php_hash_string_xor_char(unsigned char * out,const unsigned char * in,const unsigned char xor_with,const size_t length)202 static inline void php_hash_string_xor_char(unsigned char *out, const unsigned char *in, const unsigned char xor_with, const size_t length) {
203 size_t i;
204 for (i=0; i < length; i++) {
205 out[i] = in[i] ^ xor_with;
206 }
207 }
208
php_hash_string_xor(unsigned char * out,const unsigned char * in,const unsigned char * xor_with,const size_t length)209 static inline void php_hash_string_xor(unsigned char *out, const unsigned char *in, const unsigned char *xor_with, const size_t length) {
210 size_t i;
211 for (i=0; i < length; i++) {
212 out[i] = in[i] ^ xor_with[i];
213 }
214 }
215
php_hash_hmac_prep_key(unsigned char * K,const php_hash_ops * ops,void * context,const unsigned char * key,const size_t key_len)216 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) {
217 memset(K, 0, ops->block_size);
218 if (key_len > ops->block_size) {
219 /* Reduce the key first */
220 ops->hash_init(context);
221 ops->hash_update(context, key, key_len);
222 ops->hash_final(K, context);
223 } else {
224 memcpy(K, key, key_len);
225 }
226 /* XOR the key with 0x36 to get the ipad) */
227 php_hash_string_xor_char(K, K, 0x36, ops->block_size);
228 }
229
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)230 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) {
231 ops->hash_init(context);
232 ops->hash_update(context, key, ops->block_size);
233 ops->hash_update(context, data, data_size);
234 ops->hash_final(final, context);
235 }
236
php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS,int isfilename,zend_bool raw_output_default)237 static void php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAMETERS, int isfilename, zend_bool raw_output_default) /* {{{ */
238 {
239 zend_string *digest;
240 char *algo, *data, *key;
241 unsigned char *K;
242 size_t algo_len, data_len, key_len;
243 zend_bool raw_output = raw_output_default;
244 const php_hash_ops *ops;
245 void *context;
246 php_stream *stream = NULL;
247
248 if (zend_parse_parameters(ZEND_NUM_ARGS(), "sss|b", &algo, &algo_len, &data, &data_len,
249 &key, &key_len, &raw_output) == FAILURE) {
250 return;
251 }
252
253 ops = php_hash_fetch_ops(algo, algo_len);
254 if (!ops) {
255 php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo);
256 RETURN_FALSE;
257 }
258 else if (!ops->is_crypto) {
259 php_error_docref(NULL, E_WARNING, "Non-cryptographic hashing algorithm: %s", algo);
260 RETURN_FALSE;
261 }
262
263 if (isfilename) {
264 if (CHECK_NULL_PATH(data, data_len)) {
265 php_error_docref(NULL, E_WARNING, "Invalid path");
266 RETURN_FALSE;
267 }
268 stream = php_stream_open_wrapper_ex(data, "rb", REPORT_ERRORS, NULL, FG(default_context));
269 if (!stream) {
270 /* Stream will report errors opening file */
271 RETURN_FALSE;
272 }
273 }
274
275 context = emalloc(ops->context_size);
276
277 K = emalloc(ops->block_size);
278 digest = zend_string_alloc(ops->digest_size, 0);
279
280 php_hash_hmac_prep_key(K, ops, context, (unsigned char *) key, key_len);
281
282 if (isfilename) {
283 char buf[1024];
284 ssize_t n;
285 ops->hash_init(context);
286 ops->hash_update(context, K, ops->block_size);
287 while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
288 ops->hash_update(context, (unsigned char *) buf, n);
289 }
290 php_stream_close(stream);
291 if (n < 0) {
292 efree(context);
293 efree(K);
294 zend_string_release(digest);
295 RETURN_FALSE;
296 }
297
298 ops->hash_final((unsigned char *) ZSTR_VAL(digest), context);
299 } else {
300 php_hash_hmac_round((unsigned char *) ZSTR_VAL(digest), ops, context, K, (unsigned char *) data, data_len);
301 }
302
303 php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
304
305 php_hash_hmac_round((unsigned char *) ZSTR_VAL(digest), ops, context, K, (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
306
307 /* Zero the key */
308 ZEND_SECURE_ZERO(K, ops->block_size);
309 efree(K);
310 efree(context);
311
312 if (raw_output) {
313 ZSTR_VAL(digest)[ops->digest_size] = 0;
314 RETURN_NEW_STR(digest);
315 } else {
316 zend_string *hex_digest = zend_string_safe_alloc(ops->digest_size, 2, 0, 0);
317
318 php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), ops->digest_size);
319 ZSTR_VAL(hex_digest)[2 * ops->digest_size] = 0;
320 zend_string_release_ex(digest, 0);
321 RETURN_NEW_STR(hex_digest);
322 }
323 }
324 /* }}} */
325
326 /* {{{ proto string hash_hmac(string algo, string data, string key[, bool raw_output = false])
327 Generate a hash of a given input string with a key using HMAC
328 Returns lowercase hexits by default */
PHP_FUNCTION(hash_hmac)329 PHP_FUNCTION(hash_hmac)
330 {
331 php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
332 }
333 /* }}} */
334
335 /* {{{ proto string hash_hmac_file(string algo, string filename, string key[, bool raw_output = false])
336 Generate a hash of a given file with a key using HMAC
337 Returns lowercase hexits by default */
PHP_FUNCTION(hash_hmac_file)338 PHP_FUNCTION(hash_hmac_file)
339 {
340 php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1, 0);
341 }
342 /* }}} */
343
344 /* {{{ proto HashContext hash_init(string algo[, int options, string key])
345 Initialize a hashing context */
PHP_FUNCTION(hash_init)346 PHP_FUNCTION(hash_init)
347 {
348 zend_string *algo, *key = NULL;
349 zend_long options = 0;
350 int argc = ZEND_NUM_ARGS();
351 void *context;
352 const php_hash_ops *ops;
353 php_hashcontext_object *hash;
354
355 if (zend_parse_parameters(argc, "S|lS", &algo, &options, &key) == FAILURE) {
356 RETURN_NULL();
357 }
358
359 ops = php_hash_fetch_ops(ZSTR_VAL(algo), ZSTR_LEN(algo));
360 if (!ops) {
361 php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
362 RETURN_FALSE;
363 }
364
365 if (options & PHP_HASH_HMAC) {
366 if (!ops->is_crypto) {
367 php_error_docref(NULL, E_WARNING, "HMAC requested with a non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
368 RETURN_FALSE;
369 }
370 if (!key || (ZSTR_LEN(key) == 0)) {
371 /* Note: a zero length key is no key at all */
372 php_error_docref(NULL, E_WARNING, "HMAC requested without a key");
373 RETURN_FALSE;
374 }
375 }
376
377 object_init_ex(return_value, php_hashcontext_ce);
378 hash = php_hashcontext_from_object(Z_OBJ_P(return_value));
379
380 context = emalloc(ops->context_size);
381 ops->hash_init(context);
382
383 hash->ops = ops;
384 hash->context = context;
385 hash->options = options;
386 hash->key = NULL;
387
388 if (options & PHP_HASH_HMAC) {
389 char *K = emalloc(ops->block_size);
390 size_t i, block_size;
391
392 memset(K, 0, ops->block_size);
393
394 if (ZSTR_LEN(key) > ops->block_size) {
395 /* Reduce the key first */
396 ops->hash_update(context, (unsigned char *) ZSTR_VAL(key), ZSTR_LEN(key));
397 ops->hash_final((unsigned char *) K, context);
398 /* Make the context ready to start over */
399 ops->hash_init(context);
400 } else {
401 memcpy(K, ZSTR_VAL(key), ZSTR_LEN(key));
402 }
403
404 /* XOR ipad */
405 block_size = ops->block_size;
406 for(i = 0; i < block_size; i++) {
407 K[i] ^= 0x36;
408 }
409 ops->hash_update(context, (unsigned char *) K, ops->block_size);
410 hash->key = (unsigned char *) K;
411 }
412 }
413 /* }}} */
414
415 #define PHP_HASHCONTEXT_VERIFY(func, hash) { \
416 if (!hash->context) { \
417 php_error(E_WARNING, "%s(): supplied resource is not a valid Hash Context resource", func); \
418 RETURN_NULL(); \
419 } \
420 }
421
422 /* {{{ proto bool hash_update(HashContext context, string data)
423 Pump data into the hashing algorithm */
PHP_FUNCTION(hash_update)424 PHP_FUNCTION(hash_update)
425 {
426 zval *zhash;
427 php_hashcontext_object *hash;
428 zend_string *data;
429
430 if (zend_parse_parameters(ZEND_NUM_ARGS(), "OS", &zhash, php_hashcontext_ce, &data) == FAILURE) {
431 return;
432 }
433
434 hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
435 PHP_HASHCONTEXT_VERIFY("hash_update", hash);
436 hash->ops->hash_update(hash->context, (unsigned char *) ZSTR_VAL(data), ZSTR_LEN(data));
437
438 RETURN_TRUE;
439 }
440 /* }}} */
441
442 /* {{{ proto int hash_update_stream(HashContext context, resource handle[, int length])
443 Pump data into the hashing algorithm from an open stream */
PHP_FUNCTION(hash_update_stream)444 PHP_FUNCTION(hash_update_stream)
445 {
446 zval *zhash, *zstream;
447 php_hashcontext_object *hash;
448 php_stream *stream = NULL;
449 zend_long length = -1, didread = 0;
450
451 if (zend_parse_parameters(ZEND_NUM_ARGS(), "Or|l", &zhash, php_hashcontext_ce, &zstream, &length) == FAILURE) {
452 return;
453 }
454
455 hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
456 PHP_HASHCONTEXT_VERIFY("hash_update_stream", hash);
457 php_stream_from_zval(stream, zstream);
458
459 while (length) {
460 char buf[1024];
461 zend_long toread = 1024;
462 ssize_t n;
463
464 if (length > 0 && toread > length) {
465 toread = length;
466 }
467
468 if ((n = php_stream_read(stream, buf, toread)) <= 0) {
469 RETURN_LONG(didread);
470 }
471 hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
472 length -= n;
473 didread += n;
474 }
475
476 RETURN_LONG(didread);
477 }
478 /* }}} */
479
480 /* {{{ proto bool hash_update_file(HashContext context, string filename[, resource context])
481 Pump data into the hashing algorithm from a file */
PHP_FUNCTION(hash_update_file)482 PHP_FUNCTION(hash_update_file)
483 {
484 zval *zhash, *zcontext = NULL;
485 php_hashcontext_object *hash;
486 php_stream_context *context;
487 php_stream *stream;
488 zend_string *filename;
489 char buf[1024];
490 ssize_t n;
491
492 if (zend_parse_parameters(ZEND_NUM_ARGS(), "OP|r", &zhash, php_hashcontext_ce, &filename, &zcontext) == FAILURE) {
493 return;
494 }
495
496 hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
497 PHP_HASHCONTEXT_VERIFY("hash_update_file", hash);
498 context = php_stream_context_from_zval(zcontext, 0);
499
500 stream = php_stream_open_wrapper_ex(ZSTR_VAL(filename), "rb", REPORT_ERRORS, NULL, context);
501 if (!stream) {
502 /* Stream will report errors opening file */
503 RETURN_FALSE;
504 }
505
506 while ((n = php_stream_read(stream, buf, sizeof(buf))) > 0) {
507 hash->ops->hash_update(hash->context, (unsigned char *) buf, n);
508 }
509 php_stream_close(stream);
510
511 RETURN_BOOL(n >= 0);
512 }
513 /* }}} */
514
515 /* {{{ proto string hash_final(HashContext context[, bool raw_output=false])
516 Output resulting digest */
PHP_FUNCTION(hash_final)517 PHP_FUNCTION(hash_final)
518 {
519 zval *zhash;
520 php_hashcontext_object *hash;
521 zend_bool raw_output = 0;
522 zend_string *digest;
523 size_t digest_len;
524
525 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &zhash, php_hashcontext_ce, &raw_output) == FAILURE) {
526 return;
527 }
528
529 hash = php_hashcontext_from_object(Z_OBJ_P(zhash));
530 PHP_HASHCONTEXT_VERIFY("hash_final", hash);
531
532 digest_len = hash->ops->digest_size;
533 digest = zend_string_alloc(digest_len, 0);
534 hash->ops->hash_final((unsigned char *) ZSTR_VAL(digest), hash->context);
535 if (hash->options & PHP_HASH_HMAC) {
536 size_t i, block_size;
537
538 /* Convert K to opad -- 0x6A = 0x36 ^ 0x5C */
539 block_size = hash->ops->block_size;
540 for(i = 0; i < block_size; i++) {
541 hash->key[i] ^= 0x6A;
542 }
543
544 /* Feed this result into the outter hash */
545 hash->ops->hash_init(hash->context);
546 hash->ops->hash_update(hash->context, hash->key, hash->ops->block_size);
547 hash->ops->hash_update(hash->context, (unsigned char *) ZSTR_VAL(digest), hash->ops->digest_size);
548 hash->ops->hash_final((unsigned char *) ZSTR_VAL(digest), hash->context);
549
550 /* Zero the key */
551 ZEND_SECURE_ZERO(hash->key, hash->ops->block_size);
552 efree(hash->key);
553 hash->key = NULL;
554 }
555 ZSTR_VAL(digest)[digest_len] = 0;
556
557 /* Invalidate the object from further use */
558 efree(hash->context);
559 hash->context = NULL;
560
561 if (raw_output) {
562 RETURN_NEW_STR(digest);
563 } else {
564 zend_string *hex_digest = zend_string_safe_alloc(digest_len, 2, 0, 0);
565
566 php_hash_bin2hex(ZSTR_VAL(hex_digest), (unsigned char *) ZSTR_VAL(digest), digest_len);
567 ZSTR_VAL(hex_digest)[2 * digest_len] = 0;
568 zend_string_release_ex(digest, 0);
569 RETURN_NEW_STR(hex_digest);
570 }
571 }
572 /* }}} */
573
574 /* {{{ proto HashContext hash_copy(HashContext context)
575 Copy hash object */
PHP_FUNCTION(hash_copy)576 PHP_FUNCTION(hash_copy)
577 {
578 zval *zhash;
579
580 if (zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zhash, php_hashcontext_ce) == FAILURE) {
581 return;
582 }
583
584 RETVAL_OBJ(Z_OBJ_HANDLER_P(zhash, clone_obj)(zhash));
585
586 if (php_hashcontext_from_object(Z_OBJ_P(return_value))->context == NULL) {
587 zval_ptr_dtor(return_value);
588 RETURN_FALSE;
589 }
590 }
591 /* }}} */
592
593 /* {{{ proto array hash_algos(void)
594 Return a list of registered hashing algorithms */
PHP_FUNCTION(hash_algos)595 PHP_FUNCTION(hash_algos)
596 {
597 zend_string *str;
598
599 array_init(return_value);
600 ZEND_HASH_FOREACH_STR_KEY(&php_hash_hashtable, str) {
601 add_next_index_str(return_value, zend_string_copy(str));
602 } ZEND_HASH_FOREACH_END();
603 }
604 /* }}} */
605
606 /* {{{ proto array hash_hmac_algos(void)
607 Return a list of registered hashing algorithms suitable for hash_hmac() */
PHP_FUNCTION(hash_hmac_algos)608 PHP_FUNCTION(hash_hmac_algos)
609 {
610 zend_string *str;
611 const php_hash_ops *ops;
612
613 array_init(return_value);
614 ZEND_HASH_FOREACH_STR_KEY_PTR(&php_hash_hashtable, str, ops) {
615 if (ops->is_crypto) {
616 add_next_index_str(return_value, zend_string_copy(str));
617 }
618 } ZEND_HASH_FOREACH_END();
619 }
620 /* }}} */
621
622 /* {{{ proto string hash_hkdf(string algo, string ikm [, int length = 0, string info = '', string salt = ''])
623 RFC5869 HMAC-based key derivation function */
PHP_FUNCTION(hash_hkdf)624 PHP_FUNCTION(hash_hkdf)
625 {
626 zend_string *returnval, *ikm, *algo, *info = NULL, *salt = NULL;
627 zend_long length = 0;
628 unsigned char *prk, *digest, *K;
629 size_t i;
630 size_t rounds;
631 const php_hash_ops *ops;
632 void *context;
633
634 if (zend_parse_parameters(ZEND_NUM_ARGS(), "SS|lSS", &algo, &ikm, &length, &info, &salt) == FAILURE) {
635 return;
636 }
637
638 ops = php_hash_fetch_ops(ZSTR_VAL(algo), ZSTR_LEN(algo));
639 if (!ops) {
640 php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", ZSTR_VAL(algo));
641 RETURN_FALSE;
642 }
643
644 if (!ops->is_crypto) {
645 php_error_docref(NULL, E_WARNING, "Non-cryptographic hashing algorithm: %s", ZSTR_VAL(algo));
646 RETURN_FALSE;
647 }
648
649 if (ZSTR_LEN(ikm) == 0) {
650 php_error_docref(NULL, E_WARNING, "Input keying material cannot be empty");
651 RETURN_FALSE;
652 }
653
654 if (length < 0) {
655 php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
656 RETURN_FALSE;
657 } else if (length == 0) {
658 length = ops->digest_size;
659 } else if (length > (zend_long) (ops->digest_size * 255)) {
660 php_error_docref(NULL, E_WARNING, "Length must be less than or equal to %zd: " ZEND_LONG_FMT, ops->digest_size * 255, length);
661 RETURN_FALSE;
662 }
663
664 context = emalloc(ops->context_size);
665
666 // Extract
667 ops->hash_init(context);
668 K = emalloc(ops->block_size);
669 php_hash_hmac_prep_key(K, ops, context,
670 (unsigned char *) (salt ? ZSTR_VAL(salt) : ""), salt ? ZSTR_LEN(salt) : 0);
671
672 prk = emalloc(ops->digest_size);
673 php_hash_hmac_round(prk, ops, context, K, (unsigned char *) ZSTR_VAL(ikm), ZSTR_LEN(ikm));
674 php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
675 php_hash_hmac_round(prk, ops, context, K, prk, ops->digest_size);
676 ZEND_SECURE_ZERO(K, ops->block_size);
677
678 // Expand
679 returnval = zend_string_alloc(length, 0);
680 digest = emalloc(ops->digest_size);
681 for (i = 1, rounds = (length - 1) / ops->digest_size + 1; i <= rounds; i++) {
682 // chr(i)
683 unsigned char c[1];
684 c[0] = (i & 0xFF);
685
686 php_hash_hmac_prep_key(K, ops, context, prk, ops->digest_size);
687 ops->hash_init(context);
688 ops->hash_update(context, K, ops->block_size);
689
690 if (i > 1) {
691 ops->hash_update(context, digest, ops->digest_size);
692 }
693
694 if (info != NULL && ZSTR_LEN(info) > 0) {
695 ops->hash_update(context, (unsigned char *) ZSTR_VAL(info), ZSTR_LEN(info));
696 }
697
698 ops->hash_update(context, c, 1);
699 ops->hash_final(digest, context);
700 php_hash_string_xor_char(K, K, 0x6A, ops->block_size);
701 php_hash_hmac_round(digest, ops, context, K, digest, ops->digest_size);
702 memcpy(
703 ZSTR_VAL(returnval) + ((i - 1) * ops->digest_size),
704 digest,
705 (i == rounds ? length - ((i - 1) * ops->digest_size) : ops->digest_size)
706 );
707 }
708
709 ZEND_SECURE_ZERO(K, ops->block_size);
710 ZEND_SECURE_ZERO(digest, ops->digest_size);
711 ZEND_SECURE_ZERO(prk, ops->digest_size);
712 efree(K);
713 efree(context);
714 efree(prk);
715 efree(digest);
716 ZSTR_VAL(returnval)[length] = 0;
717 RETURN_STR(returnval);
718 }
719
720 /* {{{ proto string hash_pbkdf2(string algo, string password, string salt, int iterations [, int length = 0, bool raw_output = false])
721 Generate a PBKDF2 hash of the given password and salt
722 Returns lowercase hexits by default */
PHP_FUNCTION(hash_pbkdf2)723 PHP_FUNCTION(hash_pbkdf2)
724 {
725 zend_string *returnval;
726 char *algo, *salt, *pass = NULL;
727 unsigned char *computed_salt, *digest, *temp, *result, *K1, *K2 = NULL;
728 zend_long loops, i, j, iterations, digest_length = 0, length = 0;
729 size_t algo_len, pass_len, salt_len = 0;
730 zend_bool raw_output = 0;
731 const php_hash_ops *ops;
732 void *context;
733
734 if (zend_parse_parameters(ZEND_NUM_ARGS(), "sssl|lb", &algo, &algo_len, &pass, &pass_len, &salt, &salt_len, &iterations, &length, &raw_output) == FAILURE) {
735 return;
736 }
737
738 ops = php_hash_fetch_ops(algo, algo_len);
739 if (!ops) {
740 php_error_docref(NULL, E_WARNING, "Unknown hashing algorithm: %s", algo);
741 RETURN_FALSE;
742 }
743 else if (!ops->is_crypto) {
744 php_error_docref(NULL, E_WARNING, "Non-cryptographic hashing algorithm: %s", algo);
745 RETURN_FALSE;
746 }
747
748 if (iterations <= 0) {
749 php_error_docref(NULL, E_WARNING, "Iterations must be a positive integer: " ZEND_LONG_FMT, iterations);
750 RETURN_FALSE;
751 }
752
753 if (length < 0) {
754 php_error_docref(NULL, E_WARNING, "Length must be greater than or equal to 0: " ZEND_LONG_FMT, length);
755 RETURN_FALSE;
756 }
757
758 if (salt_len > INT_MAX - 4) {
759 php_error_docref(NULL, E_WARNING, "Supplied salt is too long, max of INT_MAX - 4 bytes: %zd supplied", salt_len);
760 RETURN_FALSE;
761 }
762
763 context = emalloc(ops->context_size);
764 ops->hash_init(context);
765
766 K1 = emalloc(ops->block_size);
767 K2 = emalloc(ops->block_size);
768 digest = emalloc(ops->digest_size);
769 temp = emalloc(ops->digest_size);
770
771 /* Setup Keys that will be used for all hmac rounds */
772 php_hash_hmac_prep_key(K1, ops, context, (unsigned char *) pass, pass_len);
773 /* Convert K1 to opad -- 0x6A = 0x36 ^ 0x5C */
774 php_hash_string_xor_char(K2, K1, 0x6A, ops->block_size);
775
776 /* Setup Main Loop to build a long enough result */
777 if (length == 0) {
778 length = ops->digest_size;
779 if (!raw_output) {
780 length = length * 2;
781 }
782 }
783 digest_length = length;
784 if (!raw_output) {
785 digest_length = (zend_long) ceil((float) length / 2.0);
786 }
787
788 loops = (zend_long) ceil((float) digest_length / (float) ops->digest_size);
789
790 result = safe_emalloc(loops, ops->digest_size, 0);
791
792 computed_salt = safe_emalloc(salt_len, 1, 4);
793 memcpy(computed_salt, (unsigned char *) salt, salt_len);
794
795 for (i = 1; i <= loops; i++) {
796 /* digest = hash_hmac(salt + pack('N', i), password) { */
797
798 /* pack("N", i) */
799 computed_salt[salt_len] = (unsigned char) (i >> 24);
800 computed_salt[salt_len + 1] = (unsigned char) ((i & 0xFF0000) >> 16);
801 computed_salt[salt_len + 2] = (unsigned char) ((i & 0xFF00) >> 8);
802 computed_salt[salt_len + 3] = (unsigned char) (i & 0xFF);
803
804 php_hash_hmac_round(digest, ops, context, K1, computed_salt, (zend_long) salt_len + 4);
805 php_hash_hmac_round(digest, ops, context, K2, digest, ops->digest_size);
806 /* } */
807
808 /* temp = digest */
809 memcpy(temp, digest, ops->digest_size);
810
811 /*
812 * Note that the loop starting at 1 is intentional, since we've already done
813 * the first round of the algorithm.
814 */
815 for (j = 1; j < iterations; j++) {
816 /* digest = hash_hmac(digest, password) { */
817 php_hash_hmac_round(digest, ops, context, K1, digest, ops->digest_size);
818 php_hash_hmac_round(digest, ops, context, K2, digest, ops->digest_size);
819 /* } */
820 /* temp ^= digest */
821 php_hash_string_xor(temp, temp, digest, ops->digest_size);
822 }
823 /* result += temp */
824 memcpy(result + ((i - 1) * ops->digest_size), temp, ops->digest_size);
825 }
826 /* Zero potentially sensitive variables */
827 ZEND_SECURE_ZERO(K1, ops->block_size);
828 ZEND_SECURE_ZERO(K2, ops->block_size);
829 ZEND_SECURE_ZERO(computed_salt, salt_len + 4);
830 efree(K1);
831 efree(K2);
832 efree(computed_salt);
833 efree(context);
834 efree(digest);
835 efree(temp);
836
837 returnval = zend_string_alloc(length, 0);
838 if (raw_output) {
839 memcpy(ZSTR_VAL(returnval), result, length);
840 } else {
841 php_hash_bin2hex(ZSTR_VAL(returnval), result, digest_length);
842 }
843 ZSTR_VAL(returnval)[length] = 0;
844 efree(result);
845 RETURN_NEW_STR(returnval);
846 }
847 /* }}} */
848
849 /* {{{ proto bool hash_equals(string known_string, string user_string)
850 Compares two strings using the same time whether they're equal or not.
851 A difference in length will leak */
PHP_FUNCTION(hash_equals)852 PHP_FUNCTION(hash_equals)
853 {
854 zval *known_zval, *user_zval;
855 char *known_str, *user_str;
856 int result = 0;
857 size_t j;
858
859 if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &known_zval, &user_zval) == FAILURE) {
860 return;
861 }
862
863 /* We only allow comparing string to prevent unexpected results. */
864 if (Z_TYPE_P(known_zval) != IS_STRING) {
865 php_error_docref(NULL, E_WARNING, "Expected known_string to be a string, %s given", zend_zval_type_name(known_zval));
866 RETURN_FALSE;
867 }
868
869 if (Z_TYPE_P(user_zval) != IS_STRING) {
870 php_error_docref(NULL, E_WARNING, "Expected user_string to be a string, %s given", zend_zval_type_name(user_zval));
871 RETURN_FALSE;
872 }
873
874 if (Z_STRLEN_P(known_zval) != Z_STRLEN_P(user_zval)) {
875 RETURN_FALSE;
876 }
877
878 known_str = Z_STRVAL_P(known_zval);
879 user_str = Z_STRVAL_P(user_zval);
880
881 /* This is security sensitive code. Do not optimize this for speed. */
882 for (j = 0; j < Z_STRLEN_P(known_zval); j++) {
883 result |= known_str[j] ^ user_str[j];
884 }
885
886 RETURN_BOOL(0 == result);
887 }
888 /* }}} */
889
890 /* {{{ proto HashContext::__construct() */
PHP_METHOD(HashContext,__construct)891 static PHP_METHOD(HashContext, __construct) {
892 /* Normally unreachable as private/final */
893 zend_throw_exception(zend_ce_error, "Illegal call to private/final constructor", 0);
894 }
895 /* }}} */
896
897 static const zend_function_entry php_hashcontext_methods[] = {
898 PHP_ME(HashContext, __construct, NULL, ZEND_ACC_PRIVATE)
899 PHP_FE_END
900 };
901
902 /* Module Housekeeping */
903
904 #define PHP_HASH_HAVAL_REGISTER(p,b) php_hash_register_algo("haval" #b "," #p , &php_hash_##p##haval##b##_ops);
905
906 #ifdef PHP_MHASH_BC
907
908 #if 0
909 /* See #69823, we should not insert module into module_registry while doing startup */
910
911 PHP_MINFO_FUNCTION(mhash)
912 {
913 php_info_print_table_start();
914 php_info_print_table_row(2, "MHASH support", "Enabled");
915 php_info_print_table_row(2, "MHASH API Version", "Emulated Support");
916 php_info_print_table_end();
917 }
918
919 zend_module_entry mhash_module_entry = {
920 STANDARD_MODULE_HEADER,
921 "mhash",
922 NULL,
923 NULL,
924 NULL,
925 NULL,
926 NULL,
927 PHP_MINFO(mhash),
928 PHP_MHASH_VERSION,
929 STANDARD_MODULE_PROPERTIES,
930 };
931 #endif
932
mhash_init(INIT_FUNC_ARGS)933 static void mhash_init(INIT_FUNC_ARGS)
934 {
935 char buf[128];
936 int len;
937 int algo_number = 0;
938
939 for (algo_number = 0; algo_number < MHASH_NUM_ALGOS; algo_number++) {
940 struct mhash_bc_entry algorithm = mhash_to_hash[algo_number];
941 if (algorithm.mhash_name == NULL) {
942 continue;
943 }
944
945 len = slprintf(buf, 127, "MHASH_%s", algorithm.mhash_name);
946 zend_register_long_constant(buf, len, algorithm.value, CONST_CS | CONST_PERSISTENT, module_number);
947 }
948
949 /* TODO: this cause #69823 zend_register_internal_module(&mhash_module_entry); */
950 }
951
952 /* {{{ proto string mhash(int hash, string data [, string key])
953 Hash data with hash */
PHP_FUNCTION(mhash)954 PHP_FUNCTION(mhash)
955 {
956 zval *z_algorithm;
957 zend_long algorithm;
958
959 if (zend_parse_parameters(1, "z", &z_algorithm) == FAILURE) {
960 return;
961 }
962
963 algorithm = zval_get_long(z_algorithm);
964
965 /* need to convert the first parameter from int constant to string algorithm name */
966 if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
967 struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
968 if (algorithm_lookup.hash_name) {
969 ZVAL_STRING(z_algorithm, algorithm_lookup.hash_name);
970 }
971 }
972
973 if (ZEND_NUM_ARGS() == 3) {
974 php_hash_do_hash_hmac(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
975 } else if (ZEND_NUM_ARGS() == 2) {
976 php_hash_do_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 1);
977 } else {
978 WRONG_PARAM_COUNT;
979 }
980 }
981 /* }}} */
982
983 /* {{{ proto string mhash_get_hash_name(int hash)
984 Gets the name of hash */
PHP_FUNCTION(mhash_get_hash_name)985 PHP_FUNCTION(mhash_get_hash_name)
986 {
987 zend_long algorithm;
988
989 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &algorithm) == FAILURE) {
990 return;
991 }
992
993 if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
994 struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
995 if (algorithm_lookup.mhash_name) {
996 RETURN_STRING(algorithm_lookup.mhash_name);
997 }
998 }
999 RETURN_FALSE;
1000 }
1001 /* }}} */
1002
1003 /* {{{ proto int mhash_count(void)
1004 Gets the number of available hashes */
PHP_FUNCTION(mhash_count)1005 PHP_FUNCTION(mhash_count)
1006 {
1007 if (zend_parse_parameters_none() == FAILURE) {
1008 return;
1009 }
1010 RETURN_LONG(MHASH_NUM_ALGOS - 1);
1011 }
1012 /* }}} */
1013
1014 /* {{{ proto int mhash_get_block_size(int hash)
1015 Gets the block size of hash */
PHP_FUNCTION(mhash_get_block_size)1016 PHP_FUNCTION(mhash_get_block_size)
1017 {
1018 zend_long algorithm;
1019
1020 if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &algorithm) == FAILURE) {
1021 return;
1022 }
1023 RETVAL_FALSE;
1024
1025 if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
1026 struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1027 if (algorithm_lookup.mhash_name) {
1028 const php_hash_ops *ops = php_hash_fetch_ops(algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
1029 if (ops) {
1030 RETVAL_LONG(ops->digest_size);
1031 }
1032 }
1033 }
1034 }
1035 /* }}} */
1036
1037 #define SALT_SIZE 8
1038
1039 /* {{{ proto string mhash_keygen_s2k(int hash, string input_password, string salt, int bytes)
1040 Generates a key using hash functions */
PHP_FUNCTION(mhash_keygen_s2k)1041 PHP_FUNCTION(mhash_keygen_s2k)
1042 {
1043 zend_long algorithm, l_bytes;
1044 int bytes;
1045 char *password, *salt;
1046 size_t password_len, salt_len;
1047 char padded_salt[SALT_SIZE];
1048
1049 if (zend_parse_parameters(ZEND_NUM_ARGS(), "lssl", &algorithm, &password, &password_len, &salt, &salt_len, &l_bytes) == FAILURE) {
1050 return;
1051 }
1052
1053 bytes = (int)l_bytes;
1054 if (bytes <= 0){
1055 php_error_docref(NULL, E_WARNING, "the byte parameter must be greater than 0");
1056 RETURN_FALSE;
1057 }
1058
1059 salt_len = MIN(salt_len, SALT_SIZE);
1060
1061 memcpy(padded_salt, salt, salt_len);
1062 if (salt_len < SALT_SIZE) {
1063 memset(padded_salt + salt_len, 0, SALT_SIZE - salt_len);
1064 }
1065 salt_len = SALT_SIZE;
1066
1067 RETVAL_FALSE;
1068 if (algorithm >= 0 && algorithm < MHASH_NUM_ALGOS) {
1069 struct mhash_bc_entry algorithm_lookup = mhash_to_hash[algorithm];
1070 if (algorithm_lookup.mhash_name) {
1071 const php_hash_ops *ops = php_hash_fetch_ops(algorithm_lookup.hash_name, strlen(algorithm_lookup.hash_name));
1072 if (ops) {
1073 unsigned char null = '\0';
1074 void *context;
1075 char *key, *digest;
1076 int i = 0, j = 0;
1077 size_t block_size = ops->digest_size;
1078 size_t times = bytes / block_size;
1079
1080 if ((bytes % block_size) != 0) {
1081 times++;
1082 }
1083
1084 context = emalloc(ops->context_size);
1085 ops->hash_init(context);
1086
1087 key = ecalloc(1, times * block_size);
1088 digest = emalloc(ops->digest_size + 1);
1089
1090 for (i = 0; i < times; i++) {
1091 ops->hash_init(context);
1092
1093 for (j=0;j<i;j++) {
1094 ops->hash_update(context, &null, 1);
1095 }
1096 ops->hash_update(context, (unsigned char *)padded_salt, salt_len);
1097 ops->hash_update(context, (unsigned char *)password, password_len);
1098 ops->hash_final((unsigned char *)digest, context);
1099 memcpy( &key[i*block_size], digest, block_size);
1100 }
1101
1102 RETVAL_STRINGL(key, bytes);
1103 ZEND_SECURE_ZERO(key, bytes);
1104 efree(digest);
1105 efree(context);
1106 efree(key);
1107 }
1108 }
1109 }
1110 }
1111 /* }}} */
1112
1113 #endif
1114
1115 /* ----------------------------------------------------------------------- */
1116
1117 /* {{{ php_hashcontext_create */
php_hashcontext_create(zend_class_entry * ce)1118 static zend_object* php_hashcontext_create(zend_class_entry *ce) {
1119 php_hashcontext_object *objval = zend_object_alloc(sizeof(php_hashcontext_object), ce);
1120 zend_object *zobj = &objval->std;
1121
1122 zend_object_std_init(zobj, ce);
1123 object_properties_init(zobj, ce);
1124 zobj->handlers = &php_hashcontext_handlers;
1125
1126 return zobj;
1127 }
1128 /* }}} */
1129
1130 /* {{{ php_hashcontext_dtor */
php_hashcontext_dtor(zend_object * obj)1131 static void php_hashcontext_dtor(zend_object *obj) {
1132 php_hashcontext_object *hash = php_hashcontext_from_object(obj);
1133
1134 /* Just in case the algo has internally allocated resources */
1135 if (hash->context) {
1136 unsigned char *dummy = emalloc(hash->ops->digest_size);
1137 hash->ops->hash_final(dummy, hash->context);
1138 efree(dummy);
1139 efree(hash->context);
1140 hash->context = NULL;
1141 }
1142
1143 if (hash->key) {
1144 ZEND_SECURE_ZERO(hash->key, hash->ops->block_size);
1145 efree(hash->key);
1146 hash->key = NULL;
1147 }
1148 }
1149 /* }}} */
1150
1151 /* {{{ php_hashcontext_clone */
php_hashcontext_clone(zval * pzv)1152 static zend_object *php_hashcontext_clone(zval *pzv) {
1153 php_hashcontext_object *oldobj = php_hashcontext_from_object(Z_OBJ_P(pzv));
1154 zend_object *znew = php_hashcontext_create(Z_OBJCE_P(pzv));
1155 php_hashcontext_object *newobj = php_hashcontext_from_object(znew);
1156
1157 zend_objects_clone_members(znew, Z_OBJ_P(pzv));
1158
1159 newobj->ops = oldobj->ops;
1160 newobj->options = oldobj->options;
1161 newobj->context = emalloc(newobj->ops->context_size);
1162 newobj->ops->hash_init(newobj->context);
1163
1164 if (SUCCESS != newobj->ops->hash_copy(newobj->ops, oldobj->context, newobj->context)) {
1165 efree(newobj->context);
1166 newobj->context = NULL;
1167 return znew;
1168 }
1169
1170 newobj->key = ecalloc(1, newobj->ops->block_size);
1171 if (oldobj->key) {
1172 memcpy(newobj->key, oldobj->key, newobj->ops->block_size);
1173 }
1174
1175 return znew;
1176 }
1177 /* }}} */
1178
1179 /* {{{ PHP_MINIT_FUNCTION
1180 */
PHP_MINIT_FUNCTION(hash)1181 PHP_MINIT_FUNCTION(hash)
1182 {
1183 zend_class_entry ce;
1184
1185 zend_hash_init(&php_hash_hashtable, 35, NULL, NULL, 1);
1186
1187 php_hash_register_algo("md2", &php_hash_md2_ops);
1188 php_hash_register_algo("md4", &php_hash_md4_ops);
1189 php_hash_register_algo("md5", &php_hash_md5_ops);
1190 php_hash_register_algo("sha1", &php_hash_sha1_ops);
1191 php_hash_register_algo("sha224", &php_hash_sha224_ops);
1192 php_hash_register_algo("sha256", &php_hash_sha256_ops);
1193 php_hash_register_algo("sha384", &php_hash_sha384_ops);
1194 php_hash_register_algo("sha512/224", &php_hash_sha512_224_ops);
1195 php_hash_register_algo("sha512/256", &php_hash_sha512_256_ops);
1196 php_hash_register_algo("sha512", &php_hash_sha512_ops);
1197 php_hash_register_algo("sha3-224", &php_hash_sha3_224_ops);
1198 php_hash_register_algo("sha3-256", &php_hash_sha3_256_ops);
1199 php_hash_register_algo("sha3-384", &php_hash_sha3_384_ops);
1200 php_hash_register_algo("sha3-512", &php_hash_sha3_512_ops);
1201 php_hash_register_algo("ripemd128", &php_hash_ripemd128_ops);
1202 php_hash_register_algo("ripemd160", &php_hash_ripemd160_ops);
1203 php_hash_register_algo("ripemd256", &php_hash_ripemd256_ops);
1204 php_hash_register_algo("ripemd320", &php_hash_ripemd320_ops);
1205 php_hash_register_algo("whirlpool", &php_hash_whirlpool_ops);
1206 php_hash_register_algo("tiger128,3", &php_hash_3tiger128_ops);
1207 php_hash_register_algo("tiger160,3", &php_hash_3tiger160_ops);
1208 php_hash_register_algo("tiger192,3", &php_hash_3tiger192_ops);
1209 php_hash_register_algo("tiger128,4", &php_hash_4tiger128_ops);
1210 php_hash_register_algo("tiger160,4", &php_hash_4tiger160_ops);
1211 php_hash_register_algo("tiger192,4", &php_hash_4tiger192_ops);
1212 php_hash_register_algo("snefru", &php_hash_snefru_ops);
1213 php_hash_register_algo("snefru256", &php_hash_snefru_ops);
1214 php_hash_register_algo("gost", &php_hash_gost_ops);
1215 php_hash_register_algo("gost-crypto", &php_hash_gost_crypto_ops);
1216 php_hash_register_algo("adler32", &php_hash_adler32_ops);
1217 php_hash_register_algo("crc32", &php_hash_crc32_ops);
1218 php_hash_register_algo("crc32b", &php_hash_crc32b_ops);
1219 php_hash_register_algo("crc32c", &php_hash_crc32c_ops);
1220 php_hash_register_algo("fnv132", &php_hash_fnv132_ops);
1221 php_hash_register_algo("fnv1a32", &php_hash_fnv1a32_ops);
1222 php_hash_register_algo("fnv164", &php_hash_fnv164_ops);
1223 php_hash_register_algo("fnv1a64", &php_hash_fnv1a64_ops);
1224 php_hash_register_algo("joaat", &php_hash_joaat_ops);
1225
1226 PHP_HASH_HAVAL_REGISTER(3,128);
1227 PHP_HASH_HAVAL_REGISTER(3,160);
1228 PHP_HASH_HAVAL_REGISTER(3,192);
1229 PHP_HASH_HAVAL_REGISTER(3,224);
1230 PHP_HASH_HAVAL_REGISTER(3,256);
1231
1232 PHP_HASH_HAVAL_REGISTER(4,128);
1233 PHP_HASH_HAVAL_REGISTER(4,160);
1234 PHP_HASH_HAVAL_REGISTER(4,192);
1235 PHP_HASH_HAVAL_REGISTER(4,224);
1236 PHP_HASH_HAVAL_REGISTER(4,256);
1237
1238 PHP_HASH_HAVAL_REGISTER(5,128);
1239 PHP_HASH_HAVAL_REGISTER(5,160);
1240 PHP_HASH_HAVAL_REGISTER(5,192);
1241 PHP_HASH_HAVAL_REGISTER(5,224);
1242 PHP_HASH_HAVAL_REGISTER(5,256);
1243
1244 REGISTER_LONG_CONSTANT("HASH_HMAC", PHP_HASH_HMAC, CONST_CS | CONST_PERSISTENT);
1245
1246 INIT_CLASS_ENTRY(ce, "HashContext", php_hashcontext_methods);
1247 php_hashcontext_ce = zend_register_internal_class(&ce);
1248 php_hashcontext_ce->ce_flags |= ZEND_ACC_FINAL;
1249 php_hashcontext_ce->create_object = php_hashcontext_create;
1250 php_hashcontext_ce->serialize = zend_class_serialize_deny;
1251 php_hashcontext_ce->unserialize = zend_class_unserialize_deny;
1252
1253 memcpy(&php_hashcontext_handlers, &std_object_handlers,
1254 sizeof(zend_object_handlers));
1255 php_hashcontext_handlers.offset = XtOffsetOf(php_hashcontext_object, std);
1256 php_hashcontext_handlers.dtor_obj = php_hashcontext_dtor;
1257 php_hashcontext_handlers.clone_obj = php_hashcontext_clone;
1258
1259 #ifdef PHP_MHASH_BC
1260 mhash_init(INIT_FUNC_ARGS_PASSTHRU);
1261 #endif
1262
1263 return SUCCESS;
1264 }
1265 /* }}} */
1266
1267 /* {{{ PHP_MSHUTDOWN_FUNCTION
1268 */
PHP_MSHUTDOWN_FUNCTION(hash)1269 PHP_MSHUTDOWN_FUNCTION(hash)
1270 {
1271 zend_hash_destroy(&php_hash_hashtable);
1272
1273 return SUCCESS;
1274 }
1275 /* }}} */
1276
1277 /* {{{ PHP_MINFO_FUNCTION
1278 */
PHP_MINFO_FUNCTION(hash)1279 PHP_MINFO_FUNCTION(hash)
1280 {
1281 char buffer[2048];
1282 zend_string *str;
1283 char *s = buffer, *e = s + sizeof(buffer);
1284
1285 ZEND_HASH_FOREACH_STR_KEY(&php_hash_hashtable, str) {
1286 s += slprintf(s, e - s, "%s ", ZSTR_VAL(str));
1287 } ZEND_HASH_FOREACH_END();
1288 *s = 0;
1289
1290 php_info_print_table_start();
1291 php_info_print_table_row(2, "hash support", "enabled");
1292 php_info_print_table_row(2, "Hashing Engines", buffer);
1293 php_info_print_table_end();
1294
1295 #ifdef PHP_MHASH_BC
1296 php_info_print_table_start();
1297 php_info_print_table_row(2, "MHASH support", "Enabled");
1298 php_info_print_table_row(2, "MHASH API Version", "Emulated Support");
1299 php_info_print_table_end();
1300 #endif
1301
1302 }
1303 /* }}} */
1304
1305 /* {{{ arginfo */
1306 #ifdef PHP_HASH_MD5_NOT_IN_CORE
1307 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_md5, 0, 0, 1)
1308 ZEND_ARG_INFO(0, str)
1309 ZEND_ARG_INFO(0, raw_output)
1310 ZEND_END_ARG_INFO()
1311
1312 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_md5_file, 0, 0, 1)
1313 ZEND_ARG_INFO(0, filename)
1314 ZEND_ARG_INFO(0, raw_output)
1315 ZEND_END_ARG_INFO()
1316 #endif
1317
1318 #ifdef PHP_HASH_SHA1_NOT_IN_CORE
1319 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_sha1, 0, 0, 1)
1320 ZEND_ARG_INFO(0, str)
1321 ZEND_ARG_INFO(0, raw_output)
1322 ZEND_END_ARG_INFO()
1323
1324 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_sha1_file, 0, 0, 1)
1325 ZEND_ARG_INFO(0, filename)
1326 ZEND_ARG_INFO(0, raw_output)
1327 ZEND_END_ARG_INFO()
1328 #endif
1329
1330 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash, 0, 0, 2)
1331 ZEND_ARG_INFO(0, algo)
1332 ZEND_ARG_INFO(0, data)
1333 ZEND_ARG_INFO(0, raw_output)
1334 ZEND_END_ARG_INFO()
1335
1336 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_file, 0, 0, 2)
1337 ZEND_ARG_INFO(0, algo)
1338 ZEND_ARG_INFO(0, filename)
1339 ZEND_ARG_INFO(0, raw_output)
1340 ZEND_END_ARG_INFO()
1341
1342 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_hmac, 0, 0, 3)
1343 ZEND_ARG_INFO(0, algo)
1344 ZEND_ARG_INFO(0, data)
1345 ZEND_ARG_INFO(0, key)
1346 ZEND_ARG_INFO(0, raw_output)
1347 ZEND_END_ARG_INFO()
1348
1349 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_hmac_file, 0, 0, 3)
1350 ZEND_ARG_INFO(0, algo)
1351 ZEND_ARG_INFO(0, filename)
1352 ZEND_ARG_INFO(0, key)
1353 ZEND_ARG_INFO(0, raw_output)
1354 ZEND_END_ARG_INFO()
1355
1356 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_init, 0, 0, 1)
1357 ZEND_ARG_INFO(0, algo)
1358 ZEND_ARG_INFO(0, options)
1359 ZEND_ARG_INFO(0, key)
1360 ZEND_END_ARG_INFO()
1361
1362 ZEND_BEGIN_ARG_INFO(arginfo_hash_update, 0)
1363 ZEND_ARG_INFO(0, context)
1364 ZEND_ARG_INFO(0, data)
1365 ZEND_END_ARG_INFO()
1366
1367 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_update_stream, 0, 0, 2)
1368 ZEND_ARG_INFO(0, context)
1369 ZEND_ARG_INFO(0, handle)
1370 ZEND_ARG_INFO(0, length)
1371 ZEND_END_ARG_INFO()
1372
1373 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_update_file, 0, 0, 2)
1374 ZEND_ARG_INFO(0, context)
1375 ZEND_ARG_INFO(0, filename)
1376 ZEND_ARG_INFO(0, stream_context)
1377 ZEND_END_ARG_INFO()
1378
1379 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_final, 0, 0, 1)
1380 ZEND_ARG_INFO(0, context)
1381 ZEND_ARG_INFO(0, raw_output)
1382 ZEND_END_ARG_INFO()
1383
1384 ZEND_BEGIN_ARG_INFO(arginfo_hash_copy, 0)
1385 ZEND_ARG_INFO(0, context)
1386 ZEND_END_ARG_INFO()
1387
1388 ZEND_BEGIN_ARG_INFO(arginfo_hash_algos, 0)
1389 ZEND_END_ARG_INFO()
1390
1391 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_pbkdf2, 0, 0, 4)
1392 ZEND_ARG_INFO(0, algo)
1393 ZEND_ARG_INFO(0, password)
1394 ZEND_ARG_INFO(0, salt)
1395 ZEND_ARG_INFO(0, iterations)
1396 ZEND_ARG_INFO(0, length)
1397 ZEND_ARG_INFO(0, raw_output)
1398 ZEND_END_ARG_INFO()
1399
1400 ZEND_BEGIN_ARG_INFO(arginfo_hash_equals, 0)
1401 ZEND_ARG_INFO(0, known_string)
1402 ZEND_ARG_INFO(0, user_string)
1403 ZEND_END_ARG_INFO()
1404
1405 ZEND_BEGIN_ARG_INFO_EX(arginfo_hash_hkdf, 0, 0, 2)
1406 ZEND_ARG_INFO(0, ikm)
1407 ZEND_ARG_INFO(0, algo)
1408 ZEND_ARG_INFO(0, length)
1409 ZEND_ARG_INFO(0, string)
1410 ZEND_ARG_INFO(0, salt)
1411 ZEND_END_ARG_INFO()
1412
1413 /* BC Land */
1414 #ifdef PHP_MHASH_BC
1415 ZEND_BEGIN_ARG_INFO(arginfo_mhash_get_block_size, 0)
1416 ZEND_ARG_INFO(0, hash)
1417 ZEND_END_ARG_INFO()
1418
1419 ZEND_BEGIN_ARG_INFO(arginfo_mhash_get_hash_name, 0)
1420 ZEND_ARG_INFO(0, hash)
1421 ZEND_END_ARG_INFO()
1422
1423 ZEND_BEGIN_ARG_INFO(arginfo_mhash_keygen_s2k, 0)
1424 ZEND_ARG_INFO(0, hash)
1425 ZEND_ARG_INFO(0, input_password)
1426 ZEND_ARG_INFO(0, salt)
1427 ZEND_ARG_INFO(0, bytes)
1428 ZEND_END_ARG_INFO()
1429
1430 ZEND_BEGIN_ARG_INFO(arginfo_mhash_count, 0)
1431 ZEND_END_ARG_INFO()
1432
1433 ZEND_BEGIN_ARG_INFO_EX(arginfo_mhash, 0, 0, 2)
1434 ZEND_ARG_INFO(0, hash)
1435 ZEND_ARG_INFO(0, data)
1436 ZEND_ARG_INFO(0, key)
1437 ZEND_END_ARG_INFO()
1438 #endif
1439
1440 /* }}} */
1441
1442 /* {{{ hash_functions[]
1443 */
1444 static const zend_function_entry hash_functions[] = {
1445 PHP_FE(hash, arginfo_hash)
1446 PHP_FE(hash_file, arginfo_hash_file)
1447
1448 PHP_FE(hash_hmac, arginfo_hash_hmac)
1449 PHP_FE(hash_hmac_file, arginfo_hash_hmac_file)
1450
1451 PHP_FE(hash_init, arginfo_hash_init)
1452 PHP_FE(hash_update, arginfo_hash_update)
1453 PHP_FE(hash_update_stream, arginfo_hash_update_stream)
1454 PHP_FE(hash_update_file, arginfo_hash_update_file)
1455 PHP_FE(hash_final, arginfo_hash_final)
1456 PHP_FE(hash_copy, arginfo_hash_copy)
1457
1458 PHP_FE(hash_algos, arginfo_hash_algos)
1459 PHP_FE(hash_hmac_algos, arginfo_hash_algos)
1460 PHP_FE(hash_pbkdf2, arginfo_hash_pbkdf2)
1461 PHP_FE(hash_equals, arginfo_hash_equals)
1462 PHP_FE(hash_hkdf, arginfo_hash_hkdf)
1463
1464 /* BC Land */
1465 #ifdef PHP_HASH_MD5_NOT_IN_CORE
1466 PHP_NAMED_FE(md5, php_if_md5, arginfo_hash_md5)
1467 PHP_NAMED_FE(md5_file, php_if_md5_file, arginfo_hash_md5_file)
1468 #endif /* PHP_HASH_MD5_NOT_IN_CORE */
1469
1470 #ifdef PHP_HASH_SHA1_NOT_IN_CORE
1471 PHP_NAMED_FE(sha1, php_if_sha1, arginfo_hash_sha1)
1472 PHP_NAMED_FE(sha1_file, php_if_sha1_file, arginfo_hash_sha1_file)
1473 #endif /* PHP_HASH_SHA1_NOT_IN_CORE */
1474
1475 #ifdef PHP_MHASH_BC
1476 PHP_FE(mhash_keygen_s2k, arginfo_mhash_keygen_s2k)
1477 PHP_FE(mhash_get_block_size, arginfo_mhash_get_block_size)
1478 PHP_FE(mhash_get_hash_name, arginfo_mhash_get_hash_name)
1479 PHP_FE(mhash_count, arginfo_mhash_count)
1480 PHP_FE(mhash, arginfo_mhash)
1481 #endif
1482
1483 PHP_FE_END
1484 };
1485 /* }}} */
1486
1487 /* {{{ hash_module_entry
1488 */
1489 zend_module_entry hash_module_entry = {
1490 STANDARD_MODULE_HEADER,
1491 PHP_HASH_EXTNAME,
1492 hash_functions,
1493 PHP_MINIT(hash),
1494 PHP_MSHUTDOWN(hash),
1495 NULL, /* RINIT */
1496 NULL, /* RSHUTDOWN */
1497 PHP_MINFO(hash),
1498 PHP_HASH_VERSION,
1499 STANDARD_MODULE_PROPERTIES
1500 };
1501 /* }}} */
1502