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 | Authors: Arnaud Le Blanc <arnaud.lb@gmail.com> |
14 | Tim Düsterhus <timwolla@php.net> |
15 +----------------------------------------------------------------------+
16 */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #include "php_random_zend_utils.h"
23
php_random_bytes_insecure_for_zend(zend_random_bytes_insecure_state * opaque_state,void * bytes,size_t size)24 ZEND_ATTRIBUTE_NONNULL PHPAPI void php_random_bytes_insecure_for_zend(
25 zend_random_bytes_insecure_state *opaque_state, void *bytes, size_t size)
26 {
27 php_random_bytes_insecure_state_for_zend *state = (php_random_bytes_insecure_state_for_zend*) opaque_state;
28
29 if (UNEXPECTED(!state->initialized)) {
30 uint64_t t[4];
31 php_random_fallback_seed_state fallback_state;
32 fallback_state.initialized = false;
33
34 do {
35 /* Skip the CSPRNG if it has already failed */
36 if (!fallback_state.initialized) {
37 char errstr[128];
38 if (php_random_bytes_ex(&t, sizeof(t), errstr, sizeof(errstr)) == FAILURE) {
39 #if ZEND_DEBUG
40 fprintf(stderr, "php_random_bytes_ex: Failed to generate a random seed: %s\n", errstr);
41 #endif
42 goto fallback;
43 }
44 } else {
45 fallback:
46 t[0] = php_random_generate_fallback_seed_ex(&fallback_state);
47 t[1] = php_random_generate_fallback_seed_ex(&fallback_state);
48 t[2] = php_random_generate_fallback_seed_ex(&fallback_state);
49 t[3] = php_random_generate_fallback_seed_ex(&fallback_state);
50 }
51 } while (UNEXPECTED(t[0] == 0 && t[1] == 0 && t[2] == 0 && t[3] == 0));
52
53 php_random_xoshiro256starstar_seed256(&state->xoshiro256starstar_state, t[0], t[1], t[2], t[3]);
54 state->initialized = true;
55 }
56
57 while (size > 0) {
58 php_random_result result = php_random_algo_xoshiro256starstar.generate(&state->xoshiro256starstar_state);
59 ZEND_ASSERT(result.size == 8 && sizeof(result.result) == 8);
60 size_t chunk_size = MIN(size, 8);
61 bytes = zend_mempcpy(bytes, &result.result, chunk_size);
62 size -= chunk_size;
63 }
64 }
65