|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace AdminNeo; |
| 4 | + |
| 5 | +use Exception; |
| 6 | + |
| 7 | +class Random |
| 8 | +{ |
| 9 | + /** |
| 10 | + * Returns requested amount of random bytes. |
| 11 | + * |
| 12 | + * @return string A binary string. |
| 13 | + * |
| 14 | + * @throws Exception |
| 15 | + */ |
| 16 | + public static function bytes(int $length): string |
| 17 | + { |
| 18 | + if (PHP_VERSION_ID >= 70000) { |
| 19 | + // There is an astronomically low chance of this throwing an exception. If it happens, the exception is |
| 20 | + // purposefully not caught, because it means that there is something very wrong in the system, and it cannot |
| 21 | + // be trusted to do TLS securely either. |
| 22 | + return random_bytes($length); |
| 23 | + } |
| 24 | + |
| 25 | + $result = self::tryAlternatives($length); |
| 26 | + if ($result !== false) { |
| 27 | + return $result; |
| 28 | + } |
| 29 | + |
| 30 | + $result = self::lastResortRandom($length); |
| 31 | + if ($result !== false) { |
| 32 | + return $result; |
| 33 | + } |
| 34 | + |
| 35 | + throw new Exception("Error generating random bytes"); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @return string|false |
| 40 | + */ |
| 41 | + private static function tryAlternatives(int $length) |
| 42 | + { |
| 43 | + if (extension_loaded("libsodium")) { |
| 44 | + return \Sodium\randombytes_buf($length); |
| 45 | + } |
| 46 | + |
| 47 | + $unix = DIRECTORY_SEPARATOR === "/"; |
| 48 | + |
| 49 | + if ($unix) { |
| 50 | + $result = self::readDevUrandom($length); |
| 51 | + if ($result !== false) { |
| 52 | + return $result; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + // https://bugs.php.net/bug.php?id=69833 |
| 57 | + $bug69833 = $unix && PHP_VERSION_ID > 50609 && PHP_VERSION_ID < 50613; |
| 58 | + |
| 59 | + if (extension_loaded("mcrypt") && !$bug69833) { |
| 60 | + // MCRYPT_DEV_URANDOM means "something secure" provided by the os. |
| 61 | + // It's something completely else on Windows. |
| 62 | + $result = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM); |
| 63 | + if ($result !== false) { |
| 64 | + return $result; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /* TODO Comment out after testing. |
| 69 | + if (!$unix && extension_loaded('com_dotnet') && class_exists('COM')) |
| 70 | + { |
| 71 | + return self::readCapicom($length); |
| 72 | + } |
| 73 | + */ |
| 74 | + |
| 75 | + // https://bugs.php.net/bug.php?id=70014 |
| 76 | + $bug70014 = PHP_VERSION_ID < 50444 || (PHP_VERSION_ID > 50500 && PHP_VERSION_ID < 50528) || (PHP_VERSION_ID > 50600 && PHP_VERSION_ID < 50612); |
| 77 | + |
| 78 | + if (extension_loaded("openssl") && !$bug70014) { |
| 79 | + $result = openssl_random_pseudo_bytes($length, $strong); |
| 80 | + if ($strong) { |
| 81 | + return $result; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + // No reliable source of randomness was found. |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @return string|false |
| 91 | + */ |
| 92 | + private static function readDevUrandom(int $length) |
| 93 | + { |
| 94 | + static $file = null; |
| 95 | + |
| 96 | + if ($file === null) { |
| 97 | + $file = @fopen("/dev/urandom", "rb"); |
| 98 | + } |
| 99 | + if (!$file) { |
| 100 | + return false; |
| 101 | + } |
| 102 | + |
| 103 | + $remaining = $length; |
| 104 | + $result = ""; |
| 105 | + |
| 106 | + do { |
| 107 | + $data = fread($file, $remaining); |
| 108 | + if ($data === false) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + $remaining -= strlen($data); |
| 113 | + $result .= $data; |
| 114 | + } while ($remaining > 0); |
| 115 | + |
| 116 | + return $result; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @return string|false |
| 121 | + */ |
| 122 | + private static function readCapicom(int $length) |
| 123 | + { |
| 124 | + $com = new \COM("CAPICOM.Utilities.1"); |
| 125 | + |
| 126 | + $remaining = $length; |
| 127 | + $result = ""; |
| 128 | + |
| 129 | + do { |
| 130 | + $data = base64_decode((string)$com->GetRandom($length, 0)); |
| 131 | + $remaining -= strlen($data); |
| 132 | + $result .= $data; |
| 133 | + } while ($remaining > 0); |
| 134 | + |
| 135 | + return $result; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * @return string|false |
| 140 | + */ |
| 141 | + private static function lastResortRandom(int $length) |
| 142 | + { |
| 143 | + static $key = null; |
| 144 | + static $salt = null; |
| 145 | + |
| 146 | + if ($key === null) { |
| 147 | + $data = $_SERVER; |
| 148 | + $data[] = uniqid("", true); |
| 149 | + shuffle($data); |
| 150 | + |
| 151 | + $key = sha1(serialize($data), true); |
| 152 | + |
| 153 | + // See referenced bug 70014 above. |
| 154 | + if (extension_loaded("openssl")) { |
| 155 | + $salt = openssl_random_pseudo_bytes(20); |
| 156 | + } else { |
| 157 | + $salt = ""; |
| 158 | + for ($i = 0; $i < 20; $i++) { |
| 159 | + $salt .= chr((mt_rand() ^ mt_rand()) % 256); |
| 160 | + } |
| 161 | + } |
| 162 | + } else { |
| 163 | + if ((ord($key) % 2 === 0) === (ord($salt) % 2 === 0)) { |
| 164 | + $key = Hash::hmacSha1($key, $salt); |
| 165 | + } else { |
| 166 | + $salt = Hash::hmacSha1($salt, $key); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + return Hash::hkdf($length, $key, "$length", $salt); |
| 171 | + } |
| 172 | +} |
0 commit comments