Update formatting
[lhc/web/wiklou.git] / includes / utils / MWCryptRand.php
1 <?php
2 /**
3 * A cryptographic random generator class used for generating secret keys
4 *
5 * This is based in part on Drupal code as well as what we used in our own code
6 * prior to introduction of this class.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @author Daniel Friesen
24 * @file
25 */
26
27 class MWCryptRand {
28
29 /**
30 * Minimum number of iterations we want to make in our drift calculations.
31 */
32 const MIN_ITERATIONS = 1000;
33
34 /**
35 * Number of milliseconds we want to spend generating each separate byte
36 * of the final generated bytes.
37 * This is used in combination with the hash length to determine the duration
38 * we should spend doing drift calculations.
39 */
40 const MSEC_PER_BYTE = 0.5;
41
42 /**
43 * Singleton instance for public use
44 */
45 protected static $singleton = null;
46
47 /**
48 * The hash algorithm being used
49 */
50 protected $algo = null;
51
52 /**
53 * The number of bytes outputted by the hash algorithm
54 */
55 protected $hashLength = null;
56
57 /**
58 * A boolean indicating whether the previous random generation was done using
59 * cryptographically strong random number generator or not.
60 */
61 protected $strong = null;
62
63 /**
64 * Initialize an initial random state based off of whatever we can find
65 */
66 protected function initialRandomState() {
67 // $_SERVER contains a variety of unstable user and system specific information
68 // It'll vary a little with each page, and vary even more with separate users
69 // It'll also vary slightly across different machines
70 $state = serialize( $_SERVER );
71
72 // To try vary the system information of the state a bit more
73 // by including the system's hostname into the state
74 $state .= wfHostname();
75
76 // Try to gather a little entropy from the different php rand sources
77 $state .= rand() . uniqid( mt_rand(), true );
78
79 // Include some information about the filesystem's current state in the random state
80 $files = array();
81
82 // We know this file is here so grab some info about ourselves
83 $files[] = __FILE__;
84
85 // We must also have a parent folder, and with the usual file structure, a grandparent
86 $files[] = __DIR__;
87 $files[] = dirname( __DIR__ );
88
89 // The config file is likely the most often edited file we know should be around
90 // so include its stat info into the state.
91 // The constant with its location will almost always be defined, as WebStart.php defines
92 // MW_CONFIG_FILE to $IP/LocalSettings.php unless being configured with MW_CONFIG_CALLBACK (eg. the installer)
93 if ( defined( 'MW_CONFIG_FILE' ) ) {
94 $files[] = MW_CONFIG_FILE;
95 }
96
97 foreach ( $files as $file ) {
98 wfSuppressWarnings();
99 $stat = stat( $file );
100 wfRestoreWarnings();
101 if ( $stat ) {
102 // stat() duplicates data into numeric and string keys so kill off all the numeric ones
103 foreach ( $stat as $k => $v ) {
104 if ( is_numeric( $k ) ) {
105 unset( $k );
106 }
107 }
108 // The absolute filename itself will differ from install to install so don't leave it out
109 if ( ( $path = realpath( $file ) ) !== false ) {
110 $state .= $path;
111 } else {
112 $state .= $file;
113 }
114 $state .= implode( '', $stat );
115 } else {
116 // The fact that the file isn't there is worth at least a
117 // minuscule amount of entropy.
118 $state .= '0';
119 }
120 }
121
122 // Try and make this a little more unstable by including the varying process
123 // id of the php process we are running inside of if we are able to access it
124 if ( function_exists( 'getmypid' ) ) {
125 $state .= getmypid();
126 }
127
128 // If available try to increase the instability of the data by throwing in
129 // the precise amount of memory that we happen to be using at the moment.
130 if ( function_exists( 'memory_get_usage' ) ) {
131 $state .= memory_get_usage( true );
132 }
133
134 // It's mostly worthless but throw the wiki's id into the data for a little more variance
135 $state .= wfWikiID();
136
137 // If we have a secret key or proxy key set then throw it into the state as well
138 global $wgSecretKey, $wgProxyKey;
139 if ( $wgSecretKey ) {
140 $state .= $wgSecretKey;
141 } elseif ( $wgProxyKey ) {
142 $state .= $wgProxyKey;
143 }
144
145 return $state;
146 }
147
148 /**
149 * Randomly hash data while mixing in clock drift data for randomness
150 *
151 * @param string $data The data to randomly hash.
152 * @return String The hashed bytes
153 * @author Tim Starling
154 */
155 protected function driftHash( $data ) {
156 // Minimum number of iterations (to avoid slow operations causing the loop to gather little entropy)
157 $minIterations = self::MIN_ITERATIONS;
158 // Duration of time to spend doing calculations (in seconds)
159 $duration = ( self::MSEC_PER_BYTE / 1000 ) * $this->hashLength();
160 // Create a buffer to use to trigger memory operations
161 $bufLength = 10000000;
162 $buffer = str_repeat( ' ', $bufLength );
163 $bufPos = 0;
164
165 // Iterate for $duration seconds or at least $minIterations number of iterations
166 $iterations = 0;
167 $startTime = microtime( true );
168 $currentTime = $startTime;
169 while ( $iterations < $minIterations || $currentTime - $startTime < $duration ) {
170 // Trigger some memory writing to trigger some bus activity
171 // This may create variance in the time between iterations
172 $bufPos = ( $bufPos + 13 ) % $bufLength;
173 $buffer[$bufPos] = ' ';
174 // Add the drift between this iteration and the last in as entropy
175 $nextTime = microtime( true );
176 $delta = (int)( ( $nextTime - $currentTime ) * 1000000 );
177 $data .= $delta;
178 // Every 100 iterations hash the data and entropy
179 if ( $iterations % 100 === 0 ) {
180 $data = sha1( $data );
181 }
182 $currentTime = $nextTime;
183 $iterations++;
184 }
185 $timeTaken = $currentTime - $startTime;
186 $data = $this->hash( $data );
187
188 wfDebug( __METHOD__ . ": Clock drift calculation " .
189 "(time-taken=" . ( $timeTaken * 1000 ) . "ms, " .
190 "iterations=$iterations, " .
191 "time-per-iteration=" . ( $timeTaken / $iterations * 1e6 ) . "us)\n" );
192
193 return $data;
194 }
195
196 /**
197 * Return a rolling random state initially build using data from unstable sources
198 * @return string A new weak random state
199 */
200 protected function randomState() {
201 static $state = null;
202 if ( is_null( $state ) ) {
203 // Initialize the state with whatever unstable data we can find
204 // It's important that this data is hashed right afterwards to prevent
205 // it from being leaked into the output stream
206 $state = $this->hash( $this->initialRandomState() );
207 }
208 // Generate a new random state based on the initial random state or previous
209 // random state by combining it with clock drift
210 $state = $this->driftHash( $state );
211
212 return $state;
213 }
214
215 /**
216 * Decide on the best acceptable hash algorithm we have available for hash()
217 * @throws MWException
218 * @return String A hash algorithm
219 */
220 protected function hashAlgo() {
221 if ( !is_null( $this->algo ) ) {
222 return $this->algo;
223 }
224
225 $algos = hash_algos();
226 $preference = array( 'whirlpool', 'sha256', 'sha1', 'md5' );
227
228 foreach ( $preference as $algorithm ) {
229 if ( in_array( $algorithm, $algos ) ) {
230 $this->algo = $algorithm;
231 wfDebug( __METHOD__ . ": Using the {$this->algo} hash algorithm.\n" );
232
233 return $this->algo;
234 }
235 }
236
237 // We only reach here if no acceptable hash is found in the list, this should
238 // be a technical impossibility since most of php's hash list is fixed and
239 // some of the ones we list are available as their own native functions
240 // But since we already require at least 5.2 and hash() was default in
241 // 5.1.2 we don't bother falling back to methods like sha1 and md5.
242 throw new MWException( "Could not find an acceptable hashing function in hash_algos()" );
243 }
244
245 /**
246 * Return the byte-length output of the hash algorithm we are
247 * using in self::hash and self::hmac.
248 *
249 * @return int Number of bytes the hash outputs
250 */
251 protected function hashLength() {
252 if ( is_null( $this->hashLength ) ) {
253 $this->hashLength = strlen( $this->hash( '' ) );
254 }
255
256 return $this->hashLength;
257 }
258
259 /**
260 * Generate an acceptably unstable one-way-hash of some text
261 * making use of the best hash algorithm that we have available.
262 *
263 * @param $data string
264 * @return String A raw hash of the data
265 */
266 protected function hash( $data ) {
267 return hash( $this->hashAlgo(), $data, true );
268 }
269
270 /**
271 * Generate an acceptably unstable one-way-hmac of some text
272 * making use of the best hash algorithm that we have available.
273 *
274 * @param $data string
275 * @param $key string
276 * @return String A raw hash of the data
277 */
278 protected function hmac( $data, $key ) {
279 return hash_hmac( $this->hashAlgo(), $data, $key, true );
280 }
281
282 /**
283 * @see self::wasStrong()
284 */
285 public function realWasStrong() {
286 if ( is_null( $this->strong ) ) {
287 throw new MWException( __METHOD__ . ' called before generation of random data' );
288 }
289
290 return $this->strong;
291 }
292
293 /**
294 * @see self::generate()
295 */
296 public function realGenerate( $bytes, $forceStrong = false ) {
297 wfProfileIn( __METHOD__ );
298
299 wfDebug( __METHOD__ . ": Generating cryptographic random bytes for " . wfGetAllCallers( 5 ) . "\n" );
300
301 $bytes = floor( $bytes );
302 static $buffer = '';
303 if ( is_null( $this->strong ) ) {
304 // Set strength to false initially until we know what source data is coming from
305 $this->strong = true;
306 }
307
308 if ( strlen( $buffer ) < $bytes ) {
309 // If available make use of mcrypt_create_iv URANDOM source to generate randomness
310 // On unix-like systems this reads from /dev/urandom but does it without any buffering
311 // and bypasses openbasedir restrictions, so it's preferable to reading directly
312 // On Windows starting in PHP 5.3.0 Windows' native CryptGenRandom is used to generate
313 // entropy so this is also preferable to just trying to read urandom because it may work
314 // on Windows systems as well.
315 if ( function_exists( 'mcrypt_create_iv' ) ) {
316 wfProfileIn( __METHOD__ . '-mcrypt' );
317 $rem = $bytes - strlen( $buffer );
318 $iv = mcrypt_create_iv( $rem, MCRYPT_DEV_URANDOM );
319 if ( $iv === false ) {
320 wfDebug( __METHOD__ . ": mcrypt_create_iv returned false.\n" );
321 } else {
322 $buffer .= $iv;
323 wfDebug( __METHOD__ . ": mcrypt_create_iv generated " . strlen( $iv ) . " bytes of randomness.\n" );
324 }
325 wfProfileOut( __METHOD__ . '-mcrypt' );
326 }
327 }
328
329 if ( strlen( $buffer ) < $bytes ) {
330 // If available make use of openssl's random_pseudo_bytes method to attempt to generate randomness.
331 // However don't do this on Windows with PHP < 5.3.4 due to a bug:
332 // http://stackoverflow.com/questions/1940168/openssl-random-pseudo-bytes-is-slow-php
333 // http://git.php.net/?p=php-src.git;a=commitdiff;h=cd62a70863c261b07f6dadedad9464f7e213cad5
334 if ( function_exists( 'openssl_random_pseudo_bytes' )
335 && ( !wfIsWindows() || version_compare( PHP_VERSION, '5.3.4', '>=' ) )
336 ) {
337 wfProfileIn( __METHOD__ . '-openssl' );
338 $rem = $bytes - strlen( $buffer );
339 $openssl_bytes = openssl_random_pseudo_bytes( $rem, $openssl_strong );
340 if ( $openssl_bytes === false ) {
341 wfDebug( __METHOD__ . ": openssl_random_pseudo_bytes returned false.\n" );
342 } else {
343 $buffer .= $openssl_bytes;
344 wfDebug( __METHOD__ . ": openssl_random_pseudo_bytes generated " . strlen( $openssl_bytes ) . " bytes of " . ( $openssl_strong ? "strong" : "weak" ) . " randomness.\n" );
345 }
346 if ( strlen( $buffer ) >= $bytes ) {
347 // openssl tells us if the random source was strong, if some of our data was generated
348 // using it use it's say on whether the randomness is strong
349 $this->strong = !!$openssl_strong;
350 }
351 wfProfileOut( __METHOD__ . '-openssl' );
352 }
353 }
354
355 // Only read from urandom if we can control the buffer size or were passed forceStrong
356 if ( strlen( $buffer ) < $bytes && ( function_exists( 'stream_set_read_buffer' ) || $forceStrong ) ) {
357 wfProfileIn( __METHOD__ . '-fopen-urandom' );
358 $rem = $bytes - strlen( $buffer );
359 if ( !function_exists( 'stream_set_read_buffer' ) && $forceStrong ) {
360 wfDebug( __METHOD__ . ": Was forced to read from /dev/urandom without control over the buffer size.\n" );
361 }
362 // /dev/urandom is generally considered the best possible commonly
363 // available random source, and is available on most *nix systems.
364 wfSuppressWarnings();
365 $urandom = fopen( "/dev/urandom", "rb" );
366 wfRestoreWarnings();
367
368 // Attempt to read all our random data from urandom
369 // php's fread always does buffered reads based on the stream's chunk_size
370 // so in reality it will usually read more than the amount of data we're
371 // asked for and not storing that risks depleting the system's random pool.
372 // If stream_set_read_buffer is available set the chunk_size to the amount
373 // of data we need. Otherwise read 8k, php's default chunk_size.
374 if ( $urandom ) {
375 // php's default chunk_size is 8k
376 $chunk_size = 1024 * 8;
377 if ( function_exists( 'stream_set_read_buffer' ) ) {
378 // If possible set the chunk_size to the amount of data we need
379 stream_set_read_buffer( $urandom, $rem );
380 $chunk_size = $rem;
381 }
382 $random_bytes = fread( $urandom, max( $chunk_size, $rem ) );
383 $buffer .= $random_bytes;
384 fclose( $urandom );
385 wfDebug( __METHOD__ . ": /dev/urandom generated " . strlen( $random_bytes ) . " bytes of randomness.\n" );
386 if ( strlen( $buffer ) >= $bytes ) {
387 // urandom is always strong, set to true if all our data was generated using it
388 $this->strong = true;
389 }
390 } else {
391 wfDebug( __METHOD__ . ": /dev/urandom could not be opened.\n" );
392 }
393 wfProfileOut( __METHOD__ . '-fopen-urandom' );
394 }
395
396 // If we cannot use or generate enough data from a secure source
397 // use this loop to generate a good set of pseudo random data.
398 // This works by initializing a random state using a pile of unstable data
399 // and continually shoving it through a hash along with a variable salt.
400 // We hash the random state with more salt to avoid the state from leaking
401 // out and being used to predict the /randomness/ that follows.
402 if ( strlen( $buffer ) < $bytes ) {
403 wfDebug( __METHOD__ . ": Falling back to using a pseudo random state to generate randomness.\n" );
404 }
405 while ( strlen( $buffer ) < $bytes ) {
406 wfProfileIn( __METHOD__ . '-fallback' );
407 $buffer .= $this->hmac( $this->randomState(), mt_rand() );
408 // This code is never really cryptographically strong, if we use it
409 // at all, then set strong to false.
410 $this->strong = false;
411 wfProfileOut( __METHOD__ . '-fallback' );
412 }
413
414 // Once the buffer has been filled up with enough random data to fulfill
415 // the request shift off enough data to handle the request and leave the
416 // unused portion left inside the buffer for the next request for random data
417 $generated = substr( $buffer, 0, $bytes );
418 $buffer = substr( $buffer, $bytes );
419
420 wfDebug( __METHOD__ . ": " . strlen( $buffer ) . " bytes of randomness leftover in the buffer.\n" );
421
422 wfProfileOut( __METHOD__ );
423
424 return $generated;
425 }
426
427 /**
428 * @see self::generateHex()
429 */
430 public function realGenerateHex( $chars, $forceStrong = false ) {
431 // hex strings are 2x the length of raw binary so we divide the length in half
432 // odd numbers will result in a .5 that leads the generate() being 1 character
433 // short, so we use ceil() to ensure that we always have enough bytes
434 $bytes = ceil( $chars / 2 );
435 // Generate the data and then convert it to a hex string
436 $hex = bin2hex( $this->generate( $bytes, $forceStrong ) );
437
438 // A bit of paranoia here, the caller asked for a specific length of string
439 // here, and it's possible (eg when given an odd number) that we may actually
440 // have at least 1 char more than they asked for. Just in case they made this
441 // call intending to insert it into a database that does truncation we don't
442 // want to give them too much and end up with their database and their live
443 // code having two different values because part of what we gave them is truncated
444 // hence, we strip out any run of characters longer than what we were asked for.
445 return substr( $hex, 0, $chars );
446 }
447
448 /** Publicly exposed static methods **/
449
450 /**
451 * Return a singleton instance of MWCryptRand
452 * @return MWCryptRand
453 */
454 protected static function singleton() {
455 if ( is_null( self::$singleton ) ) {
456 self::$singleton = new self;
457 }
458
459 return self::$singleton;
460 }
461
462 /**
463 * Return a boolean indicating whether or not the source used for cryptographic
464 * random bytes generation in the previously run generate* call
465 * was cryptographically strong.
466 *
467 * @return bool Returns true if the source was strong, false if not.
468 */
469 public static function wasStrong() {
470 return self::singleton()->realWasStrong();
471 }
472
473 /**
474 * Generate a run of (ideally) cryptographically random data and return
475 * it in raw binary form.
476 * You can use MWCryptRand::wasStrong() if you wish to know if the source used
477 * was cryptographically strong.
478 *
479 * @param int $bytes the number of bytes of random data to generate
480 * @param bool $forceStrong Pass true if you want generate to prefer cryptographically
481 * strong sources of entropy even if reading from them may steal
482 * more entropy from the system than optimal.
483 * @return String Raw binary random data
484 */
485 public static function generate( $bytes, $forceStrong = false ) {
486 return self::singleton()->realGenerate( $bytes, $forceStrong );
487 }
488
489 /**
490 * Generate a run of (ideally) cryptographically random data and return
491 * it in hexadecimal string format.
492 * You can use MWCryptRand::wasStrong() if you wish to know if the source used
493 * was cryptographically strong.
494 *
495 * @param int $chars the number of hex chars of random data to generate
496 * @param bool $forceStrong Pass true if you want generate to prefer cryptographically
497 * strong sources of entropy even if reading from them may steal
498 * more entropy from the system than optimal.
499 * @return String Hexadecimal random data
500 */
501 public static function generateHex( $chars, $forceStrong = false ) {
502 return self::singleton()->realGenerateHex( $chars, $forceStrong );
503 }
504 }