Merge "Language: s/error_log/wfWarn/"
[lhc/web/wiklou.git] / includes / utils / MWCryptRand.php
index d71193f..b602f78 100644 (file)
@@ -25,7 +25,6 @@
  */
 
 class MWCryptRand {
-
        /**
         * Minimum number of iterations we want to make in our drift calculations.
         */
@@ -62,6 +61,7 @@ class MWCryptRand {
 
        /**
         * Initialize an initial random state based off of whatever we can find
+        * @return string
         */
        protected function initialRandomState() {
                // $_SERVER contains a variety of unstable user and system specific information
@@ -86,10 +86,11 @@ class MWCryptRand {
                $files[] = __DIR__;
                $files[] = dirname( __DIR__ );
 
-               // The config file is likely the most often edited file we know should be around
-               // so include its stat info into the state.
-               // The constant with its location will almost always be defined, as WebStart.php defines
-               // MW_CONFIG_FILE to $IP/LocalSettings.php unless being configured with MW_CONFIG_CALLBACK (eg. the installer)
+               // The config file is likely the most often edited file we know should
+               // be around so include its stat info into the state.
+               // The constant with its location will almost always be defined, as
+               // WebStart.php defines MW_CONFIG_FILE to $IP/LocalSettings.php unless
+               // being configured with MW_CONFIG_CALLBACK (e.g. the installer).
                if ( defined( 'MW_CONFIG_FILE' ) ) {
                        $files[] = MW_CONFIG_FILE;
                }
@@ -134,12 +135,10 @@ class MWCryptRand {
                // It's mostly worthless but throw the wiki's id into the data for a little more variance
                $state .= wfWikiID();
 
-               // If we have a secret key or proxy key set then throw it into the state as well
-               global $wgSecretKey, $wgProxyKey;
+               // If we have a secret key set then throw it into the state as well
+               global $wgSecretKey;
                if ( $wgSecretKey ) {
                        $state .= $wgSecretKey;
-               } elseif ( $wgProxyKey ) {
-                       $state .= $wgProxyKey;
                }
 
                return $state;
@@ -149,11 +148,12 @@ class MWCryptRand {
         * Randomly hash data while mixing in clock drift data for randomness
         *
         * @param string $data The data to randomly hash.
-        * @return String The hashed bytes
+        * @return string The hashed bytes
         * @author Tim Starling
         */
        protected function driftHash( $data ) {
-               // Minimum number of iterations (to avoid slow operations causing the loop to gather little entropy)
+               // Minimum number of iterations (to avoid slow operations causing the
+               // loop to gather little entropy)
                $minIterations = self::MIN_ITERATIONS;
                // Duration of time to spend doing calculations (in seconds)
                $duration = ( self::MSEC_PER_BYTE / 1000 ) * $this->hashLength();
@@ -215,7 +215,7 @@ class MWCryptRand {
        /**
         * Decide on the best acceptable hash algorithm we have available for hash()
         * @throws MWException
-        * @return String A hash algorithm
+        * @return string A hash algorithm
         */
        protected function hashAlgo() {
                if ( !is_null( $this->algo ) ) {
@@ -260,8 +260,8 @@ class MWCryptRand {
         * Generate an acceptably unstable one-way-hash of some text
         * making use of the best hash algorithm that we have available.
         *
-        * @param $data string
-        * @return String A raw hash of the data
+        * @param string $data
+        * @return string A raw hash of the data
         */
        protected function hash( $data ) {
                return hash( $this->hashAlgo(), $data, true );
@@ -271,9 +271,9 @@ class MWCryptRand {
         * Generate an acceptably unstable one-way-hmac of some text
         * making use of the best hash algorithm that we have available.
         *
-        * @param $data string
-        * @param $key string
-        * @return String A raw hash of the data
+        * @param string $data
+        * @param string $key
+        * @return string A raw hash of the data
         */
        protected function hmac( $data, $key ) {
                return hash_hmac( $this->hashAlgo(), $data, $key, true );
@@ -296,7 +296,8 @@ class MWCryptRand {
        public function realGenerate( $bytes, $forceStrong = false ) {
                wfProfileIn( __METHOD__ );
 
-               wfDebug( __METHOD__ . ": Generating cryptographic random bytes for " . wfGetAllCallers( 5 ) . "\n" );
+               wfDebug( __METHOD__ . ": Generating cryptographic random bytes for " .
+                       wfGetAllCallers( 5 ) . "\n" );
 
                $bytes = floor( $bytes );
                static $buffer = '';
@@ -320,15 +321,17 @@ class MWCryptRand {
                                        wfDebug( __METHOD__ . ": mcrypt_create_iv returned false.\n" );
                                } else {
                                        $buffer .= $iv;
-                                       wfDebug( __METHOD__ . ": mcrypt_create_iv generated " . strlen( $iv ) . " bytes of randomness.\n" );
+                                       wfDebug( __METHOD__ . ": mcrypt_create_iv generated " . strlen( $iv ) .
+                                               " bytes of randomness.\n" );
                                }
                                wfProfileOut( __METHOD__ . '-mcrypt' );
                        }
                }
 
                if ( strlen( $buffer ) < $bytes ) {
-                       // If available make use of openssl's random_pseudo_bytes method to attempt to generate randomness.
-                       // However don't do this on Windows with PHP < 5.3.4 due to a bug:
+                       // If available make use of openssl's random_pseudo_bytes method to
+                       // attempt to generate randomness. However don't do this on Windows
+                       // with PHP < 5.3.4 due to a bug:
                        // http://stackoverflow.com/questions/1940168/openssl-random-pseudo-bytes-is-slow-php
                        // http://git.php.net/?p=php-src.git;a=commitdiff;h=cd62a70863c261b07f6dadedad9464f7e213cad5
                        if ( function_exists( 'openssl_random_pseudo_bytes' )
@@ -341,7 +344,9 @@ class MWCryptRand {
                                        wfDebug( __METHOD__ . ": openssl_random_pseudo_bytes returned false.\n" );
                                } else {
                                        $buffer .= $openssl_bytes;
-                                       wfDebug( __METHOD__ . ": openssl_random_pseudo_bytes generated " . strlen( $openssl_bytes ) . " bytes of " . ( $openssl_strong ? "strong" : "weak" ) . " randomness.\n" );
+                                       wfDebug( __METHOD__ . ": openssl_random_pseudo_bytes generated " .
+                                               strlen( $openssl_bytes ) . " bytes of " .
+                                               ( $openssl_strong ? "strong" : "weak" ) . " randomness.\n" );
                                }
                                if ( strlen( $buffer ) >= $bytes ) {
                                        // openssl tells us if the random source was strong, if some of our data was generated
@@ -353,11 +358,14 @@ class MWCryptRand {
                }
 
                // Only read from urandom if we can control the buffer size or were passed forceStrong
-               if ( strlen( $buffer ) < $bytes && ( function_exists( 'stream_set_read_buffer' ) || $forceStrong ) ) {
+               if ( strlen( $buffer ) < $bytes &&
+                       ( function_exists( 'stream_set_read_buffer' ) || $forceStrong )
+               ) {
                        wfProfileIn( __METHOD__ . '-fopen-urandom' );
                        $rem = $bytes - strlen( $buffer );
                        if ( !function_exists( 'stream_set_read_buffer' ) && $forceStrong ) {
-                               wfDebug( __METHOD__ . ": Was forced to read from /dev/urandom without control over the buffer size.\n" );
+                               wfDebug( __METHOD__ . ": Was forced to read from /dev/urandom " .
+                                       "without control over the buffer size.\n" );
                        }
                        // /dev/urandom is generally considered the best possible commonly
                        // available random source, and is available on most *nix systems.
@@ -382,7 +390,9 @@ class MWCryptRand {
                                $random_bytes = fread( $urandom, max( $chunk_size, $rem ) );
                                $buffer .= $random_bytes;
                                fclose( $urandom );
-                               wfDebug( __METHOD__ . ": /dev/urandom generated " . strlen( $random_bytes ) . " bytes of randomness.\n" );
+                               wfDebug( __METHOD__ . ": /dev/urandom generated " . strlen( $random_bytes ) .
+                                       " bytes of randomness.\n" );
+
                                if ( strlen( $buffer ) >= $bytes ) {
                                        // urandom is always strong, set to true if all our data was generated using it
                                        $this->strong = true;
@@ -400,7 +410,8 @@ class MWCryptRand {
                // We hash the random state with more salt to avoid the state from leaking
                // out and being used to predict the /randomness/ that follows.
                if ( strlen( $buffer ) < $bytes ) {
-                       wfDebug( __METHOD__ . ": Falling back to using a pseudo random state to generate randomness.\n" );
+                       wfDebug( __METHOD__ .
+                               ": Falling back to using a pseudo random state to generate randomness.\n" );
                }
                while ( strlen( $buffer ) < $bytes ) {
                        wfProfileIn( __METHOD__ . '-fallback' );
@@ -417,7 +428,8 @@ class MWCryptRand {
                $generated = substr( $buffer, 0, $bytes );
                $buffer = substr( $buffer, $bytes );
 
-               wfDebug( __METHOD__ . ": " . strlen( $buffer ) . " bytes of randomness leftover in the buffer.\n" );
+               wfDebug( __METHOD__ . ": " . strlen( $buffer ) .
+                       " bytes of randomness leftover in the buffer.\n" );
 
                wfProfileOut( __METHOD__ );
 
@@ -476,11 +488,11 @@ class MWCryptRand {
         * You can use MWCryptRand::wasStrong() if you wish to know if the source used
         * was cryptographically strong.
         *
-        * @param int $bytes the number of bytes of random data to generate
+        * @param int $bytes The number of bytes of random data to generate
         * @param bool $forceStrong Pass true if you want generate to prefer cryptographically
         *                          strong sources of entropy even if reading from them may steal
         *                          more entropy from the system than optimal.
-        * @return String Raw binary random data
+        * @return string Raw binary random data
         */
        public static function generate( $bytes, $forceStrong = false ) {
                return self::singleton()->realGenerate( $bytes, $forceStrong );
@@ -492,11 +504,11 @@ class MWCryptRand {
         * You can use MWCryptRand::wasStrong() if you wish to know if the source used
         * was cryptographically strong.
         *
-        * @param int $chars the number of hex chars of random data to generate
+        * @param int $chars The number of hex chars of random data to generate
         * @param bool $forceStrong Pass true if you want generate to prefer cryptographically
         *                          strong sources of entropy even if reading from them may steal
         *                          more entropy from the system than optimal.
-        * @return String Hexadecimal random data
+        * @return string Hexadecimal random data
         */
        public static function generateHex( $chars, $forceStrong = false ) {
                return self::singleton()->realGenerateHex( $chars, $forceStrong );