| Elliott Hughes | 211c4d3 | 2018-02-02 15:10:32 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2018 The Android Open Source Project | 
|  | 3 | * All rights reserved. | 
|  | 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 | *  * Redistributions of source code must retain the above copyright | 
|  | 9 | *    notice, this list of conditions and the following disclaimer. | 
|  | 10 | *  * Redistributions in binary form must reproduce the above copyright | 
|  | 11 | *    notice, this list of conditions and the following disclaimer in | 
|  | 12 | *    the documentation and/or other materials provided with the | 
|  | 13 | *    distribution. | 
|  | 14 | * | 
|  | 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
|  | 16 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
|  | 17 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | 
|  | 18 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | 
|  | 19 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | 
|  | 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | 
|  | 21 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS | 
|  | 22 | * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | 
|  | 23 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
|  | 24 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | 
|  | 25 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | 
|  | 26 | * SUCH DAMAGE. | 
|  | 27 | */ | 
|  | 28 |  | 
|  | 29 | #include <errno.h> | 
|  | 30 | #include <fcntl.h> | 
|  | 31 | #include <sys/random.h> | 
|  | 32 | #include <unistd.h> | 
|  | 33 |  | 
| Elliott Hughes | 6cb70ad | 2019-10-31 15:37:32 -0700 | [diff] [blame] | 34 | #include "private/ScopedFd.h" | 
|  | 35 |  | 
| Elliott Hughes | 211c4d3 | 2018-02-02 15:10:32 -0800 | [diff] [blame] | 36 | static int getentropy_urandom(void* buffer, size_t buffer_size, int saved_errno) { | 
| Elliott Hughes | 6cb70ad | 2019-10-31 15:37:32 -0700 | [diff] [blame] | 37 | ScopedFd fd(TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_NOFOLLOW | O_CLOEXEC, 0))); | 
|  | 38 | if (fd.get() == -1) return -1; | 
| Elliott Hughes | 211c4d3 | 2018-02-02 15:10:32 -0800 | [diff] [blame] | 39 |  | 
|  | 40 | size_t collected = 0; | 
|  | 41 | while (collected < buffer_size) { | 
| Elliott Hughes | 6cb70ad | 2019-10-31 15:37:32 -0700 | [diff] [blame] | 42 | ssize_t count = TEMP_FAILURE_RETRY(read(fd.get(), static_cast<char*>(buffer) + collected, | 
| Elliott Hughes | 211c4d3 | 2018-02-02 15:10:32 -0800 | [diff] [blame] | 43 | buffer_size - collected)); | 
| Elliott Hughes | 6cb70ad | 2019-10-31 15:37:32 -0700 | [diff] [blame] | 44 | if (count == -1) return -1; | 
| Elliott Hughes | 211c4d3 | 2018-02-02 15:10:32 -0800 | [diff] [blame] | 45 | collected += count; | 
|  | 46 | } | 
|  | 47 |  | 
| Elliott Hughes | 211c4d3 | 2018-02-02 15:10:32 -0800 | [diff] [blame] | 48 | errno = saved_errno; | 
|  | 49 | return 0; | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | int getentropy(void* buffer, size_t buffer_size) { | 
|  | 53 | if (buffer_size > 256) { | 
|  | 54 | errno = EIO; | 
|  | 55 | return -1; | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | int saved_errno = errno; | 
|  | 59 |  | 
|  | 60 | size_t collected = 0; | 
|  | 61 | while (collected < buffer_size) { | 
|  | 62 | long count = TEMP_FAILURE_RETRY(getrandom(static_cast<char*>(buffer) + collected, | 
|  | 63 | buffer_size - collected, GRND_NONBLOCK)); | 
|  | 64 | if (count == -1) { | 
|  | 65 | // EAGAIN: there isn't enough entropy right now. | 
|  | 66 | // ENOSYS/EINVAL: getrandom(2) or GRND_NONBLOCK isn't supported. | 
|  | 67 | // EFAULT: `buffer` is invalid. | 
|  | 68 | // Try /dev/urandom regardless because it can't hurt, | 
|  | 69 | // and we don't need to optimize the EFAULT case. | 
|  | 70 | // See http://b/33059407 and http://b/67015565. | 
|  | 71 | return getentropy_urandom(buffer, buffer_size, saved_errno); | 
|  | 72 | } | 
|  | 73 | collected += count; | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | errno = saved_errno; | 
|  | 77 | return 0; | 
|  | 78 | } |