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