diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
commit | 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c (patch) | |
tree | 46dcd36c86e7fbc6e5df36deb463b33e9967a6f7 /Source/WTF/wtf/OSRandomSource.cpp | |
parent | 32761a6cee1d0dee366b885b7b9c777e67885688 (diff) | |
download | WebKitGtk-tarball-master.tar.gz |
webkitgtk-2.16.5HEADwebkitgtk-2.16.5master
Diffstat (limited to 'Source/WTF/wtf/OSRandomSource.cpp')
-rw-r--r-- | Source/WTF/wtf/OSRandomSource.cpp | 43 |
1 files changed, 36 insertions, 7 deletions
diff --git a/Source/WTF/wtf/OSRandomSource.cpp b/Source/WTF/wtf/OSRandomSource.cpp index 2495abf71..378795dc7 100644 --- a/Source/WTF/wtf/OSRandomSource.cpp +++ b/Source/WTF/wtf/OSRandomSource.cpp @@ -13,7 +13,7 @@ * THIS SOFTWARE IS PROVIDED BY GOOGLE, INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR @@ -29,7 +29,8 @@ #include <stdint.h> #include <stdlib.h> -#if OS(UNIX) +#if !OS(DARWIN) && OS(UNIX) +#include <errno.h> #include <fcntl.h> #include <unistd.h> #endif @@ -39,19 +40,47 @@ #include <wincrypt.h> // windows.h must be included before wincrypt.h. #endif +#if OS(DARWIN) +#include "CommonCryptoSPI.h" +#endif + namespace WTF { +#if !OS(DARWIN) && OS(UNIX) +NEVER_INLINE NO_RETURN_DUE_TO_CRASH static void crashUnableToOpenURandom() +{ + CRASH(); +} + +NEVER_INLINE NO_RETURN_DUE_TO_CRASH static void crashUnableToReadFromURandom() +{ + CRASH(); +} +#endif + void cryptographicallyRandomValuesFromOS(unsigned char* buffer, size_t length) { -#if OS(UNIX) +#if OS(DARWIN) + RELEASE_ASSERT(!CCRandomCopyBytes(kCCRandomDefault, buffer, length)); +#elif OS(UNIX) int fd = open("/dev/urandom", O_RDONLY, 0); if (fd < 0) - CRASH(); // We need /dev/urandom for this API to work... - - if (read(fd, buffer, length) != static_cast<ssize_t>(length)) - CRASH(); + crashUnableToOpenURandom(); // We need /dev/urandom for this API to work... + ssize_t amountRead = 0; + while (static_cast<size_t>(amountRead) < length) { + ssize_t currentRead = read(fd, buffer + amountRead, length - amountRead); + // We need to check for both EAGAIN and EINTR since on some systems /dev/urandom + // is blocking and on others it is non-blocking. + if (currentRead == -1) { + if (!(errno == EAGAIN || errno == EINTR)) + crashUnableToReadFromURandom(); + } else + amountRead += currentRead; + } + close(fd); + #elif OS(WINDOWS) HCRYPTPROV hCryptProv = 0; if (!CryptAcquireContext(&hCryptProv, 0, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |