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