]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/random-util.c
Merge pull request #2054 from keszybz/nss-link-less-2
[thirdparty/systemd.git] / src / basic / random-util.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <elf.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <stdbool.h>
24 #include <stdlib.h>
25 #include <sys/time.h>
26 #include <linux/random.h>
27 #include <stdint.h>
28
29 #ifdef HAVE_SYS_AUXV_H
30 #include <sys/auxv.h>
31 #endif
32
33 #include "fd-util.h"
34 #include "io-util.h"
35 #include "missing.h"
36 #include "random-util.h"
37 #include "time-util.h"
38
39 int dev_urandom(void *p, size_t n) {
40 static int have_syscall = -1;
41
42 _cleanup_close_ int fd = -1;
43 int r;
44
45 /* Gathers some randomness from the kernel. This call will
46 * never block, and will always return some data from the
47 * kernel, regardless if the random pool is fully initialized
48 * or not. It thus makes no guarantee for the quality of the
49 * returned entropy, but is good enough for or usual usecases
50 * of seeding the hash functions for hashtable */
51
52 /* Use the getrandom() syscall unless we know we don't have
53 * it, or when the requested size is too large for it. */
54 if (have_syscall != 0 || (size_t) (int) n != n) {
55 r = getrandom(p, n, GRND_NONBLOCK);
56 if (r == (int) n) {
57 have_syscall = true;
58 return 0;
59 }
60
61 if (r < 0) {
62 if (errno == ENOSYS)
63 /* we lack the syscall, continue with
64 * reading from /dev/urandom */
65 have_syscall = false;
66 else if (errno == EAGAIN)
67 /* not enough entropy for now. Let's
68 * remember to use the syscall the
69 * next time, again, but also read
70 * from /dev/urandom for now, which
71 * doesn't care about the current
72 * amount of entropy. */
73 have_syscall = true;
74 else
75 return -errno;
76 } else
77 /* too short read? */
78 return -ENODATA;
79 }
80
81 fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
82 if (fd < 0)
83 return errno == ENOENT ? -ENOSYS : -errno;
84
85 return loop_read_exact(fd, p, n, true);
86 }
87
88 void initialize_srand(void) {
89 static bool srand_called = false;
90 unsigned x;
91 #ifdef HAVE_SYS_AUXV_H
92 void *auxv;
93 #endif
94
95 if (srand_called)
96 return;
97
98 x = 0;
99
100 #ifdef HAVE_SYS_AUXV_H
101 /* The kernel provides us with a bit of entropy in auxv, so
102 * let's try to make use of that to seed the pseudo-random
103 * generator. It's better than nothing... */
104
105 auxv = (void*) getauxval(AT_RANDOM);
106 if (auxv)
107 x ^= *(unsigned*) auxv;
108 #endif
109
110 x ^= (unsigned) now(CLOCK_REALTIME);
111 x ^= (unsigned) gettid();
112
113 srand(x);
114 srand_called = true;
115 }
116
117 void random_bytes(void *p, size_t n) {
118 uint8_t *q;
119 int r;
120
121 r = dev_urandom(p, n);
122 if (r >= 0)
123 return;
124
125 /* If some idiot made /dev/urandom unavailable to us, he'll
126 * get a PRNG instead. */
127
128 initialize_srand();
129
130 for (q = p; q < (uint8_t*) p + n; q ++)
131 *q = rand();
132 }