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 34 static int getentropy_urandom(void* buffer, size_t buffer_size, int saved_errno) { 35 int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_NOFOLLOW | O_CLOEXEC, 0)); 36 if (fd == -1) return -1; 37 38 size_t collected = 0; 39 while (collected < buffer_size) { 40 ssize_t count = TEMP_FAILURE_RETRY(read(fd, static_cast<char*>(buffer) + collected, 41 buffer_size - collected)); 42 if (count == -1) { 43 close(fd); 44 return -1; 45 } 46 collected += count; 47 } 48 49 close(fd); 50 errno = saved_errno; 51 return 0; 52 } 53 54 int getentropy(void* buffer, size_t buffer_size) { 55 if (buffer_size > 256) { 56 errno = EIO; 57 return -1; 58 } 59 60 int saved_errno = errno; 61 62 size_t collected = 0; 63 while (collected < buffer_size) { 64 long count = TEMP_FAILURE_RETRY(getrandom(static_cast<char*>(buffer) + collected, 65 buffer_size - collected, GRND_NONBLOCK)); 66 if (count == -1) { 67 // EAGAIN: there isn't enough entropy right now. 68 // ENOSYS/EINVAL: getrandom(2) or GRND_NONBLOCK isn't supported. 69 // EFAULT: `buffer` is invalid. 70 // Try /dev/urandom regardless because it can't hurt, 71 // and we don't need to optimize the EFAULT case. 72 // See http://b/33059407 and http://b/67015565. 73 return getentropy_urandom(buffer, buffer_size, saved_errno); 74 } 75 collected += count; 76 } 77 78 errno = saved_errno; 79 return 0; 80 } 81