]>
Commit | Line | Data |
---|---|---|
eaad4f9e | 1 | /* Pseudo Random Number Generator |
6f4e0fcf AZN |
2 | Copyright (C) 2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, see | |
17 | <https://www.gnu.org/licenses/>. */ | |
18 | ||
6f4e0fcf AZN |
19 | #include <errno.h> |
20 | #include <not-cancel.h> | |
21 | #include <stdio.h> | |
22 | #include <stdlib.h> | |
23 | #include <sys/mman.h> | |
24 | #include <sys/param.h> | |
25 | #include <sys/random.h> | |
6f4e0fcf AZN |
26 | |
27 | static void | |
28 | arc4random_getrandom_failure (void) | |
29 | { | |
30 | __libc_fatal ("Fatal glibc error: cannot get entropy for arc4random\n"); | |
31 | } | |
32 | ||
eaad4f9e JD |
33 | void |
34 | __arc4random_buf (void *p, size_t n) | |
6f4e0fcf | 35 | { |
eaad4f9e JD |
36 | static int seen_initialized; |
37 | size_t l; | |
38 | int fd; | |
6f4e0fcf | 39 | |
eaad4f9e | 40 | if (n == 0) |
6f4e0fcf AZN |
41 | return; |
42 | ||
eaad4f9e | 43 | for (;;) |
6f4e0fcf | 44 | { |
eaad4f9e JD |
45 | l = TEMP_FAILURE_RETRY (__getrandom_nocancel (p, n, 0)); |
46 | if (l > 0) | |
6f4e0fcf | 47 | { |
eaad4f9e JD |
48 | if ((size_t) l == n) |
49 | return; /* Done reading, success. */ | |
50 | p = (uint8_t *) p + l; | |
51 | n -= l; | |
52 | continue; /* Interrupted by a signal; keep going. */ | |
6f4e0fcf | 53 | } |
eaad4f9e JD |
54 | else if (l < 0 && errno == ENOSYS) |
55 | break; /* No syscall, so fallback to /dev/urandom. */ | |
56 | arc4random_getrandom_failure (); | |
6f4e0fcf | 57 | } |
6f4e0fcf | 58 | |
eaad4f9e | 59 | if (atomic_load_relaxed (&seen_initialized) == 0) |
6f4e0fcf | 60 | { |
eaad4f9e JD |
61 | /* Poll /dev/random as an approximation of RNG initialization. */ |
62 | struct pollfd pfd = { .events = POLLIN }; | |
63 | pfd.fd = TEMP_FAILURE_RETRY ( | |
64 | __open64_nocancel ("/dev/random", O_RDONLY | O_CLOEXEC | O_NOCTTY)); | |
65 | if (pfd.fd < 0) | |
66 | arc4random_getrandom_failure (); | |
67 | if (TEMP_FAILURE_RETRY (__poll_infinity_nocancel (&pfd, 1)) < 0) | |
68 | arc4random_getrandom_failure (); | |
69 | if (__close_nocancel (pfd.fd) < 0) | |
70 | arc4random_getrandom_failure (); | |
71 | atomic_store_relaxed (&seen_initialized, 1); | |
6f4e0fcf | 72 | } |
6f4e0fcf | 73 | |
eaad4f9e JD |
74 | fd = TEMP_FAILURE_RETRY ( |
75 | __open64_nocancel ("/dev/urandom", O_RDONLY | O_CLOEXEC | O_NOCTTY)); | |
76 | if (fd < 0) | |
77 | arc4random_getrandom_failure (); | |
78 | for (;;) | |
6f4e0fcf | 79 | { |
eaad4f9e JD |
80 | l = TEMP_FAILURE_RETRY (__read_nocancel (fd, p, n)); |
81 | if (l <= 0) | |
82 | arc4random_getrandom_failure (); | |
83 | if ((size_t) l == n) | |
84 | break; /* Done reading, success. */ | |
85 | p = (uint8_t *) p + l; | |
86 | n -= l; | |
6f4e0fcf | 87 | } |
eaad4f9e JD |
88 | if (__close_nocancel (fd) < 0) |
89 | arc4random_getrandom_failure (); | |
6f4e0fcf AZN |
90 | } |
91 | libc_hidden_def (__arc4random_buf) | |
92 | weak_alias (__arc4random_buf, arc4random_buf) | |
93 | ||
94 | uint32_t | |
95 | __arc4random (void) | |
96 | { | |
97 | uint32_t r; | |
eaad4f9e | 98 | __arc4random_buf (&r, sizeof (r)); |
6f4e0fcf AZN |
99 | return r; |
100 | } | |
101 | libc_hidden_def (__arc4random) | |
102 | weak_alias (__arc4random, arc4random) |