On Fri, Jul 31, 2015 at 6:34 PM, Ferenc Kovacs <tyra3l@gmail.com> wrote: > > On Tue, Jul 14, 2015 at 11:04 PM, Sammy Kaye Powers <me@sammyk.me> wrote: > > > Hello lovely PHP nerds, > > > > There are two open PR's for PHP7 to modify the behavior of the CSPRNG's: > > > > https://github.com/php/php-src/pull/1397 (main discussion) > > https://github.com/php/php-src/pull/1398 > > > > Currently the random_*() functions will issue a warning and return false if > > a good source of random cannot be found. This is a potential security hole > > in the event the RNG fails and returns false which gets evaluated as 0 in a > > cryptographic context. > > > > To prevent this exploit the proposed behavior will throw an Exception when > > the RNG fails or certain argument validation fails. This also gives the > > developer a graceful way to fall back to an alternate CSPRNG. > > > > Since the core functions in PHP don't throw Exceptions, there is debate on > > whether or not this change should be implemented. Some say the CSPRNG's > > should get a special pass since they will be relied on for cryptography.. If > > we can't throw Exceptions, there were suggestions of raising a fatal error > > if the RNG fails. > > > > I think the argument can be boiled down to consistency vs security. We'd > > love to hear your feedback to decide what we should do in this context. :) > > > > Thanks, > > Sammy Kaye Powers > > sammyk.me > > > > Chicago, IL 60604 > > > > I would vote for E_WARNING and return false. > This can be wrapped in an oop wrapper in userland if somebody prefers and > exception but would still keep the procedural style as first class citizen. > Plus this would be consistent with other security/crypto related errors > like mcrypt_encrypt() getting an invalid key/iv > Nikita, Anthony what do you think? > > -- > Ferenc Kovács > @Tyr43l - http://tyrael.hu > This can be wrapped in an oop wrapper in userland if somebody prefers and > exception but would still keep the procedural style as first class citizen. Nobody's going to do that though. The end result is going to be less security because of a cargo cult devotion to consistency. This should be secure by default. The most secure way for an RNG to fail is to interrupt the application. This means: * E_ERROR * throw new Exception (or a subclass) * throw new Error (or a subclass) Exceptions and Errors have the advantage that a developer who wants to go out of their way to handle them can simply do this: function randomPassword($length, $alphabet = 'abcdefghijklmnopqrstuvwxyz') { $sizeOfAlphabetMinusOne = strlen($alphabet) - 1; try { for ($i = 0; $i < $length; ++$i) { $password .= $alphabet[random_int(0, $sizeOfAlphabetMinusOne)]; } } catch (Error $e) { return $this->framework->stylizedErrorMessage("RNG failure message here"); } return $password; } Care to guess what returning false will do for $password? Any cryptography-related implementation needs to fail closed, not fail open.. By raising E_WARNING and returning false, you are placing an extra responsibility on the developer. Or as Daniel J. Bernstein would put it, YOU ARE BLAMING THE IMPLEMENTOR. Ask any competent application security expert, they'll back me up. Don't enforce insecure defaults just because it's more "consistent". Consistency is important, sure, but security is MORE important. Also, death to libmcrypt: https://paragonie.com/blog/2015/05/if-you-re-typing-word-mcrypt-into-your-code-you-re-doing-it-wrong Scott Arciszewski Chief Development Officer Paragon Initiative Enterprises <https://paragonie.com>



