blob: 04bf81af43367dc6515179363906ab2861d19417 [file] [log] [blame]
xf.lif1aed282024-02-06 00:31:51 -08001/*
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 <linux/random.h>
32#include <unistd.h>
33
34#ifndef O_NOFOLLOW
35#define O_NOFOLLOW 00400000
36#endif
37
38#ifndef O_CLOEXEC
39#define O_CLOEXEC 02000000
40#endif
41
42/* Used to retry syscalls that can return EINTR. */
43#define TEMP_FAILURE_RETRY(exp) ({ \
44 typeof(exp) _rc; \
45 do { \
46 _rc = (exp); \
47 } while (_rc == -1 && errno == EINTR); \
48 _rc; })
49
50static int getentropy_urandom(void* buffer, size_t buffer_size, int saved_errno) {
51 int fd = TEMP_FAILURE_RETRY(open("/dev/urandom", O_RDONLY | O_NOFOLLOW | O_CLOEXEC, 0));
52 if (fd == -1) return -1;
53
54 size_t collected = 0;
55 while (collected < buffer_size) {
56 ssize_t count = TEMP_FAILURE_RETRY(read(fd, (char*)buffer + collected, buffer_size - collected));
57 if (count == -1) {
58 close(fd);
59 return -1;
60 }
61 collected += count;
62 }
63
64 close(fd);
65 errno = saved_errno;
66 return 0;
67}
68
69int getentropy(void* buffer, size_t buffer_size) {
70 if (buffer_size > 256) {
71 errno = EIO;
72 return -1;
73 }
74
75 int saved_errno = errno;
76
77 size_t collected = 0;
78 while (collected < buffer_size) {
79#if 0 //No ""getrandom on Linux-3.4.5
80 long count = TEMP_FAILURE_RETRY(getrandom((char*)buffer + collected,
81 buffer_size - collected, GRND_NONBLOCK));
82#else
83 long count = -1;
84#endif
85 if (count == -1) {
86 // EAGAIN: there isn't enough entropy right now.
87 // ENOSYS/EINVAL: getrandom(2) or GRND_NONBLOCK isn't supported.
88 // EFAULT: `buffer` is invalid.
89 // Try /dev/urandom regardless because it can't hurt,
90 // and we don't need to optimize the EFAULT case.
91 // See http://b/33059407 and http://b/67015565.
92 return getentropy_urandom(buffer, buffer_size, saved_errno);
93 }
94 collected += count;
95 }
96
97 errno = saved_errno;
98 return 0;
99}