php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
zend_utils.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | 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
23
25 zend_random_bytes_insecure_state *opaque_state, void *bytes, size_t size)
26{
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 {
45fallback:
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}
fprintf($stream, string $format, mixed ... $values)
ZEND_ATTRIBUTE_NONNULL PHPAPI zend_result php_random_bytes_ex(void *bytes, size_t size, char *errstr, size_t errstr_size)
Definition csprng.c:69
PHPAPI const php_random_algo php_random_algo_xoshiro256starstar
PHPAPI void php_random_xoshiro256starstar_seed256(php_random_status_state_xoshiro256starstar *state, uint64_t s0, uint64_t s1, uint64_t s2, uint64_t s3)
new_type size
Definition ffi.c:4365
#define PHPAPI
Definition php.h:71
PHPAPI uint64_t php_random_generate_fallback_seed_ex(php_random_fallback_seed_state *state)
Definition random.c:644
struct _php_random_fallback_seed_state php_random_fallback_seed_state
Definition php_random.h:40
struct _php_random_result php_random_result
struct _php_random_bytes_insecure_state_for_zend php_random_bytes_insecure_state_for_zend
#define MIN(a, b)
#define ZEND_ATTRIBUTE_NONNULL
#define ZEND_ASSERT(c)
#define UNEXPECTED(condition)
@ FAILURE
Definition zend_types.h:61
ZEND_ATTRIBUTE_NONNULL PHPAPI void php_random_bytes_insecure_for_zend(zend_random_bytes_insecure_state *opaque_state, void *bytes, size_t size)
Definition zend_utils.c:24
bool result