xref: /php-src/ext/random/engine_secure.c (revision 99e7cf07)
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: Sammy Kaye Powers <me@sammyk.me>                            |
14    |          Go Kudo <zeriyoshi@php.net>                                 |
15    +----------------------------------------------------------------------+
16 */
17 
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21 
22 #include "php.h"
23 #include "php_random.h"
24 #include "php_random_csprng.h"
25 
26 #include "Zend/zend_exceptions.h"
27 
generate(void * state)28 static php_random_result generate(void *state)
29 {
30 	zend_ulong r = 0;
31 
32 	php_random_bytes_throw(&r, sizeof(r));
33 
34 	return (php_random_result){
35 		.size = sizeof(zend_ulong),
36 		.result = r,
37 	};
38 }
39 
range(void * state,zend_long min,zend_long max)40 static zend_long range(void *state, zend_long min, zend_long max)
41 {
42 	zend_long result = 0;
43 
44 	php_random_int_throw(min, max, &result);
45 
46 	return result;
47 }
48 
49 const php_random_algo php_random_algo_secure = {
50 	0,
51 	generate,
52 	range,
53 	NULL,
54 	NULL
55 };
56