Chris@0
|
1 <?php
|
Chris@0
|
2 /**
|
Chris@0
|
3 * Random_* Compatibility Library
|
Chris@0
|
4 * for using the new PHP 7 random_* API in PHP 5 projects
|
Chris@0
|
5 *
|
Chris@0
|
6 * The MIT License (MIT)
|
Chris@0
|
7 *
|
Chris@2
|
8 * Copyright (c) 2015 - 2018 Paragon Initiative Enterprises
|
Chris@0
|
9 *
|
Chris@0
|
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
|
Chris@0
|
11 * of this software and associated documentation files (the "Software"), to deal
|
Chris@0
|
12 * in the Software without restriction, including without limitation the rights
|
Chris@0
|
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
Chris@0
|
14 * copies of the Software, and to permit persons to whom the Software is
|
Chris@0
|
15 * furnished to do so, subject to the following conditions:
|
Chris@0
|
16 *
|
Chris@0
|
17 * The above copyright notice and this permission notice shall be included in
|
Chris@0
|
18 * all copies or substantial portions of the Software.
|
Chris@0
|
19 *
|
Chris@0
|
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
Chris@0
|
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
Chris@0
|
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
Chris@0
|
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
Chris@0
|
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
Chris@0
|
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
Chris@0
|
26 * SOFTWARE.
|
Chris@0
|
27 */
|
Chris@0
|
28
|
Chris@0
|
29 if (!defined('RANDOM_COMPAT_READ_BUFFER')) {
|
Chris@0
|
30 define('RANDOM_COMPAT_READ_BUFFER', 8);
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 if (!is_callable('random_bytes')) {
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Unless open_basedir is enabled, use /dev/urandom for
|
Chris@0
|
36 * random numbers in accordance with best practices
|
Chris@0
|
37 *
|
Chris@0
|
38 * Why we use /dev/urandom and not /dev/random
|
Chris@0
|
39 * @ref http://sockpuppet.org/blog/2014/02/25/safely-generate-random-numbers
|
Chris@0
|
40 *
|
Chris@0
|
41 * @param int $bytes
|
Chris@0
|
42 *
|
Chris@0
|
43 * @throws Exception
|
Chris@0
|
44 *
|
Chris@0
|
45 * @return string
|
Chris@0
|
46 */
|
Chris@0
|
47 function random_bytes($bytes)
|
Chris@0
|
48 {
|
Chris@2
|
49 /** @var resource $fp */
|
Chris@0
|
50 static $fp = null;
|
Chris@2
|
51
|
Chris@0
|
52 /**
|
Chris@0
|
53 * This block should only be run once
|
Chris@0
|
54 */
|
Chris@0
|
55 if (empty($fp)) {
|
Chris@0
|
56 /**
|
Chris@0
|
57 * We use /dev/urandom if it is a char device.
|
Chris@0
|
58 * We never fall back to /dev/random
|
Chris@0
|
59 */
|
Chris@2
|
60 /** @var resource|bool $fp */
|
Chris@0
|
61 $fp = fopen('/dev/urandom', 'rb');
|
Chris@2
|
62 if (is_resource($fp)) {
|
Chris@2
|
63 /** @var array<string, int> $st */
|
Chris@0
|
64 $st = fstat($fp);
|
Chris@0
|
65 if (($st['mode'] & 0170000) !== 020000) {
|
Chris@0
|
66 fclose($fp);
|
Chris@0
|
67 $fp = false;
|
Chris@0
|
68 }
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@2
|
71 if (is_resource($fp)) {
|
Chris@0
|
72 /**
|
Chris@0
|
73 * stream_set_read_buffer() does not exist in HHVM
|
Chris@0
|
74 *
|
Chris@0
|
75 * If we don't set the stream's read buffer to 0, PHP will
|
Chris@0
|
76 * internally buffer 8192 bytes, which can waste entropy
|
Chris@0
|
77 *
|
Chris@0
|
78 * stream_set_read_buffer returns 0 on success
|
Chris@0
|
79 */
|
Chris@0
|
80 if (is_callable('stream_set_read_buffer')) {
|
Chris@0
|
81 stream_set_read_buffer($fp, RANDOM_COMPAT_READ_BUFFER);
|
Chris@0
|
82 }
|
Chris@0
|
83 if (is_callable('stream_set_chunk_size')) {
|
Chris@0
|
84 stream_set_chunk_size($fp, RANDOM_COMPAT_READ_BUFFER);
|
Chris@0
|
85 }
|
Chris@0
|
86 }
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 try {
|
Chris@2
|
90 /** @var int $bytes */
|
Chris@0
|
91 $bytes = RandomCompat_intval($bytes);
|
Chris@0
|
92 } catch (TypeError $ex) {
|
Chris@0
|
93 throw new TypeError(
|
Chris@0
|
94 'random_bytes(): $bytes must be an integer'
|
Chris@0
|
95 );
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 if ($bytes < 1) {
|
Chris@0
|
99 throw new Error(
|
Chris@0
|
100 'Length must be greater than 0'
|
Chris@0
|
101 );
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 /**
|
Chris@0
|
105 * This if() block only runs if we managed to open a file handle
|
Chris@0
|
106 *
|
Chris@0
|
107 * It does not belong in an else {} block, because the above
|
Chris@0
|
108 * if (empty($fp)) line is logic that should only be run once per
|
Chris@0
|
109 * page load.
|
Chris@0
|
110 */
|
Chris@2
|
111 if (is_resource($fp)) {
|
Chris@0
|
112 /**
|
Chris@0
|
113 * @var int
|
Chris@0
|
114 */
|
Chris@0
|
115 $remaining = $bytes;
|
Chris@0
|
116
|
Chris@0
|
117 /**
|
Chris@0
|
118 * @var string|bool
|
Chris@0
|
119 */
|
Chris@0
|
120 $buf = '';
|
Chris@0
|
121
|
Chris@0
|
122 /**
|
Chris@0
|
123 * We use fread() in a loop to protect against partial reads
|
Chris@0
|
124 */
|
Chris@0
|
125 do {
|
Chris@0
|
126 /**
|
Chris@0
|
127 * @var string|bool
|
Chris@0
|
128 */
|
Chris@0
|
129 $read = fread($fp, $remaining);
|
Chris@0
|
130 if (!is_string($read)) {
|
Chris@0
|
131 if ($read === false) {
|
Chris@0
|
132 /**
|
Chris@0
|
133 * We cannot safely read from the file. Exit the
|
Chris@0
|
134 * do-while loop and trigger the exception condition
|
Chris@0
|
135 *
|
Chris@0
|
136 * @var string|bool
|
Chris@0
|
137 */
|
Chris@0
|
138 $buf = false;
|
Chris@0
|
139 break;
|
Chris@0
|
140 }
|
Chris@0
|
141 }
|
Chris@0
|
142 /**
|
Chris@0
|
143 * Decrease the number of bytes returned from remaining
|
Chris@0
|
144 */
|
Chris@0
|
145 $remaining -= RandomCompat_strlen($read);
|
Chris@0
|
146 /**
|
Chris@0
|
147 * @var string|bool
|
Chris@0
|
148 */
|
Chris@0
|
149 $buf = $buf . $read;
|
Chris@0
|
150 } while ($remaining > 0);
|
Chris@0
|
151
|
Chris@0
|
152 /**
|
Chris@0
|
153 * Is our result valid?
|
Chris@0
|
154 */
|
Chris@0
|
155 if (is_string($buf)) {
|
Chris@0
|
156 if (RandomCompat_strlen($buf) === $bytes) {
|
Chris@0
|
157 /**
|
Chris@0
|
158 * Return our random entropy buffer here:
|
Chris@0
|
159 */
|
Chris@0
|
160 return $buf;
|
Chris@0
|
161 }
|
Chris@0
|
162 }
|
Chris@0
|
163 }
|
Chris@0
|
164
|
Chris@0
|
165 /**
|
Chris@0
|
166 * If we reach here, PHP has failed us.
|
Chris@0
|
167 */
|
Chris@0
|
168 throw new Exception(
|
Chris@0
|
169 'Error reading from source device'
|
Chris@0
|
170 );
|
Chris@0
|
171 }
|
Chris@0
|
172 }
|