| 1 | /* |
| 2 | * Copyright (C) 2011 Google Inc. |
| 3 | * Copyright (C) 2017 Yusuke Suzuki <utatane.tea@gmail.com> |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY GOOGLE, INC. ``AS IS'' AND ANY |
| 15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | #include "config.h" |
| 28 | #include <wtf/RandomDevice.h> |
| 29 | |
| 30 | #include <stdint.h> |
| 31 | #include <stdlib.h> |
| 32 | |
| 33 | #if !OS(DARWIN) && !OS(FUCHSIA) && OS(UNIX) |
| 34 | #include <errno.h> |
| 35 | #include <fcntl.h> |
| 36 | #include <unistd.h> |
| 37 | #endif |
| 38 | |
| 39 | #if OS(WINDOWS) |
| 40 | #include <windows.h> |
| 41 | #include <wincrypt.h> // windows.h must be included before wincrypt.h. |
| 42 | #endif |
| 43 | |
| 44 | #if OS(DARWIN) |
| 45 | #include <CommonCrypto/CommonCryptoError.h> |
| 46 | #include <CommonCrypto/CommonRandom.h> |
| 47 | #endif |
| 48 | |
| 49 | #if OS(FUCHSIA) |
| 50 | #include <zircon/syscalls.h> |
| 51 | #endif |
| 52 | |
| 53 | namespace WTF { |
| 54 | |
| 55 | #if !OS(DARWIN) && !OS(FUCHSIA) && OS(UNIX) |
| 56 | NEVER_INLINE NO_RETURN_DUE_TO_CRASH static void crashUnableToOpenURandom() |
| 57 | { |
| 58 | CRASH(); |
| 59 | } |
| 60 | |
| 61 | NEVER_INLINE NO_RETURN_DUE_TO_CRASH static void crashUnableToReadFromURandom() |
| 62 | { |
| 63 | CRASH(); |
| 64 | } |
| 65 | #endif |
| 66 | |
| 67 | #if !OS(DARWIN) && !OS(FUCHSIA) && !OS(WINDOWS) |
| 68 | RandomDevice::RandomDevice() |
| 69 | { |
| 70 | int ret = 0; |
| 71 | do { |
| 72 | ret = open("/dev/urandom" , O_RDONLY, 0); |
| 73 | } while (ret == -1 && errno == EINTR); |
| 74 | m_fd = ret; |
| 75 | if (m_fd < 0) |
| 76 | crashUnableToOpenURandom(); // We need /dev/urandom for this API to work... |
| 77 | } |
| 78 | #endif |
| 79 | |
| 80 | #if !OS(DARWIN) && !OS(FUCHSIA) && !OS(WINDOWS) |
| 81 | RandomDevice::~RandomDevice() |
| 82 | { |
| 83 | close(m_fd); |
| 84 | } |
| 85 | #endif |
| 86 | |
| 87 | // FIXME: Make this call fast by creating the pool in RandomDevice. |
| 88 | // https://bugs.webkit.org/show_bug.cgi?id=170190 |
| 89 | void RandomDevice::cryptographicallyRandomValues(unsigned char* buffer, size_t length) |
| 90 | { |
| 91 | #if OS(DARWIN) |
| 92 | RELEASE_ASSERT(!CCRandomGenerateBytes(buffer, length)); |
| 93 | #elif OS(FUCHSIA) |
| 94 | zx_cprng_draw(buffer, length); |
| 95 | #elif OS(UNIX) |
| 96 | ssize_t amountRead = 0; |
| 97 | while (static_cast<size_t>(amountRead) < length) { |
| 98 | ssize_t currentRead = read(m_fd, buffer + amountRead, length - amountRead); |
| 99 | // We need to check for both EAGAIN and EINTR since on some systems /dev/urandom |
| 100 | // is blocking and on others it is non-blocking. |
| 101 | if (currentRead == -1) { |
| 102 | if (!(errno == EAGAIN || errno == EINTR)) |
| 103 | crashUnableToReadFromURandom(); |
| 104 | } else |
| 105 | amountRead += currentRead; |
| 106 | } |
| 107 | #elif OS(WINDOWS) |
| 108 | // FIXME: We cannot ensure that Cryptographic Service Provider context and CryptGenRandom are safe across threads. |
| 109 | // If it is safe, we can acquire context per RandomDevice. |
| 110 | HCRYPTPROV hCryptProv = 0; |
| 111 | if (!CryptAcquireContext(&hCryptProv, 0, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) |
| 112 | CRASH(); |
| 113 | if (!CryptGenRandom(hCryptProv, length, buffer)) |
| 114 | CRASH(); |
| 115 | CryptReleaseContext(hCryptProv, 0); |
| 116 | #else |
| 117 | #error "This configuration doesn't have a strong source of randomness." |
| 118 | // WARNING: When adding new sources of OS randomness, the randomness must |
| 119 | // be of cryptographic quality! |
| 120 | #endif |
| 121 | } |
| 122 | |
| 123 | } |
| 124 | |