Debian Codegraph @

apps/kcapi-rng.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2017 - 2022, Stephan Mueller <[email protected]>
3 *
4 * License: see COPYING file in root directory
5 *
6 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
7 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
8 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
9 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
10 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
11 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
12 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
13 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
14 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
17 * DAMAGE.
18 */
19
20 #define _GNU_SOURCE
21 #include <unistd.h>
22 #include <sys/syscall.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <stdint.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <getopt.h>
33
34 #include <linux/random.h>
35 #ifdef HAVE_GETRANDOM
36 #include <sys/random.h>
37 #endif
38
39 #include <kcapi.h>
40
41 #include "app-internal.h"
42
43 /* For efficiency reasons, this should be identical to algif_rng.c:MAXSIZE. */
44 #define KCAPI_RNG_BUFSIZE 128
45 /* Minimum seed is 256 bits. */
46 #define KCAPI_RNG_MINSEEDSIZE 32
47
48 static struct kcapi_handle *rng = NULL;
49 static unsigned int Verbosity = KCAPI_LOG_WARN;
50 static char *rng_name = NULL;
51 static bool hexout = false;
52
53 #if !defined(HAVE_GETRANDOM) && !defined(__NR_getrandom)
54 static int random_fd = -1;
55 static int open_random(void)
56 {
57 random_fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC);
58 if (0 > random_fd)
59 return random_fd;
60
61 return 0;
62 }
63
64 static void close_random(void)
65 {
66 close(random_fd);
67 }
68 #endif
69
70 static ssize_t get_random(uint8_t *buf, size_t buflen)
71 {
72 ssize_t ret;
73
74 if (buflen > INT_MAX)
75 return 1;
76
77 #if (!defined(HAVE_GETRANDOM) && !defined(__NR_getrandom))
78 ret = open_random();
79 if (ret)
80 return ret;
81 #endif
82
83 do {
84 #ifdef HAVE_GETRANDOM
85 ret = getrandom(buf, buflen, 0);
86 dolog(KCAPI_LOG_DEBUG,
87 "Accessed getrandom system call for %lu bytes", buflen);
88 #elif defined __NR_getrandom
89 ret = syscall(__NR_getrandom, buf, buflen, 0);
90 dolog(KCAPI_LOG_DEBUG,
91 "Accessed getrandom system call for %lu bytes", buflen);
92 #else
93 ret = read(random_fd, buf, buflen);
94 dolog(KCAPI_LOG_DEBUG,
95 "Accessed /dev/urandom for %lu bytes", buflen);
96 #endif
97 if (0 < ret) {
98 buflen -= (size_t)ret;
99 buf += (size_t)ret;
100 }
101 } while ((0 < ret || EINTR == errno || ERESTART == errno)
102 && buflen > 0);
103
104 #if (!defined(HAVE_GETRANDOM) && !defined(__NR_getrandom))
105 close_random();
106 #endif
107
108 if (buflen == 0)
109 return 0;
110 return 1;
111 }
112
113 static void usage(void)
114 {
115 char version[30];
116 uint32_t ver = kcapi_version();
117
118 memset(version, 0, sizeof(version));
119 kcapi_versionstring(version, sizeof(version));
120
121 fprintf(stderr, "\nKernel Crypto API Random Number Gatherer\n");
122 fprintf(stderr, "\nKernel Crypto API interface library version: %s\n", version);
123 fprintf(stderr, "Reported numeric version number %u\n\n", ver);
124 fprintf(stderr, "Usage:\n");
125 fprintf(stderr, "\t-b --bytes <BYTES>\tNumber of bytes to generate (required option)\n");
126 fprintf(stderr, "\t-n --name <RNGNAME>\tDRNG name as advertised in /proc/crypto\n");
127 fprintf(stderr, "\t\t\t\t(stdrng is default)\n");
128 fprintf(stderr, "\t --hex\t\tThe random number is returned in hexadecimal\n");
129 fprintf(stderr, "\t\t\t\tnotation\n");
130 fprintf(stderr, "\t-h --help\t\tThis help information\n");
131 fprintf(stderr, "\t --version\t\tPrint version\n");
132 fprintf(stderr, "\t-v --verbose\t\tVerbose logging, multiple options increase\n");
133 fprintf(stderr, "\t\t\t\tverbosity\n");
134 fprintf(stderr, "\nData provided at stdin is used to seed the DRNG\n");
135
136 exit(1);
137 }
138
139 static int parse_opts(int argc, char *argv[], size_t *outlen)
140 {
141 int c = 0;
142 char version[30];
143 size_t bytes = 0;
144
145 while (1) {
146 int opt_index = 0;
147 static struct option opts[] = {
148 {"verbose", no_argument, 0, 'v'},
149 {"quiet", no_argument, 0, 'q'},
150 {"help", no_argument, 0, 'h'},
151 {"version", no_argument, 0, 0},
152 {"bytes", required_argument, 0, 'b'},
153 {"name", required_argument, 0, 'n'},
154 {"hex", no_argument, 0, 0},
155 {0, 0, 0, 0}
156 };
157 c = getopt_long(argc, argv, "vqhb:n:", opts, &opt_index);
158 if (-1 == c)
159 break;
160 switch (c) {
161 case 0:
162 switch (opt_index) {
163 case 0:
164 Verbosity++;
165 break;
166 case 1:
167 Verbosity = KCAPI_LOG_NONE;
168 break;
169 case 2:
170 usage();
171 break;
172 case 3:
173 memset(version, 0, sizeof(version));
174 kcapi_versionstring(version, sizeof(version));
175 fprintf(stderr, "Version %s\n", version);
176 exit(0);
177 break;
178 case 4:
179 bytes = strtoul(optarg, NULL, 10);
180 if (bytes == ULONG_MAX) {
181 usage();
182 return -EINVAL;
183 }
184 break;
185 case 5:
186 rng_name = optarg;
187 break;
188 case 6:
189 hexout = true;
190 break;
191 default:
192 usage();
193 }
194 break;
195 case 'v':
196 Verbosity++;
197 break;
198 case 'q':
199 Verbosity = KCAPI_LOG_NONE;
200 break;
201 case 'h':
202 usage();
203 break;
204 case 'b':
205 bytes = strtoul(optarg, NULL, 10);
206 if (bytes == ULONG_MAX) {
207 usage();
208 return -EINVAL;
209 }
210 break;
211 case 'n':
212 rng_name = optarg;
213 break;
214 default:
215 usage();
216 }
217 }
218
219 if (!bytes)
220 usage();
221
222 *outlen = (size_t)bytes;
223 return 0;
224 }
225
226 int main(int argc, char *argv[])
227 {
228 ssize_t ret;
229 uint8_t buf[KCAPI_RNG_BUFSIZE] __aligned(KCAPI_APP_ALIGN);
230 uint8_t *seedbuf = buf;
231 uint32_t seedsize = 0;
232 size_t outlen;
233
234 ret = parse_opts(argc, argv, &outlen);
235 if (ret)
236 return (int)ret;
237
238 set_verbosity("kcapi-rng", Verbosity);
239
240 if (rng_name)
241 ret = kcapi_rng_init(&rng, rng_name, 0);
242 else
243 ret = kcapi_rng_init(&rng, "stdrng", 0);
244 if (ret)
245 return (int)ret;
246
247 seedsize = kcapi_rng_seedsize(rng);
248 if (seedsize) {
249 /*
250 * Only reseed, if there is a seedsize defined. For example,
251 * the DRBG has a seedsize of 0 because it seeds itself from
252 * known good noise sources.
253 */
254 if (seedsize < KCAPI_RNG_MINSEEDSIZE)
255 seedsize = KCAPI_RNG_MINSEEDSIZE;
256
257 /*
258 * Only allocate a new buffer if our buffer is
259 * insufficiently large.
260 */
261 if (seedsize > KCAPI_RNG_BUFSIZE) {
262 seedbuf = calloc(1, seedsize);
263 if (!seedbuf) {
264 ret = -ENOMEM;
265 goto out;
266 }
267 }
268
269 ret = get_random(seedbuf, seedsize);
270 if (ret)
271 goto out;
272 }
273
274 /*
275 * Invoke seeding even if seedsize is 0 -- this also triggers any
276 * internal seeding operation like in the DRBG.
277 */
278 ret = kcapi_rng_seed(rng, seedbuf, seedsize);
279 if (ret)
280 goto out;
281 dolog(KCAPI_LOG_DEBUG, "Seeding the DRNG with %u bytes of data",
282 seedsize);
283
284 if (!isatty(0) && (errno == EINVAL || errno == ENOTTY)) {
285 while (fgets((char *)seedbuf, (int)seedsize, stdin)) {
286 ret = kcapi_rng_seed(rng, seedbuf, seedsize);
287 if (ret)
288 dolog(KCAPI_LOG_WARN,
289 "User-provided seed of %lu bytes not accepted by DRNG (error: %ld)",
290 (unsigned long)sizeof(buf), ret);
291 else
292 dolog(KCAPI_LOG_DEBUG,
293 "User-provided seed of %u bytes",
294 seedsize);
295 }
296 }
297
298 while (outlen) {
299 size_t todo = (outlen < KCAPI_RNG_BUFSIZE) ?
300 outlen : KCAPI_RNG_BUFSIZE;
301
302 ret = kcapi_rng_generate(rng, buf, todo);
303 if (ret < 0)
304 goto out;
305
306 if ((uint32_t)ret == 0) {
307 ret = -EFAULT;
308 goto out;
309 }
310
311 if (hexout) {
312 char hexbuf[2 * KCAPI_RNG_BUFSIZE];
313
314 bin2hex(buf, (size_t)ret, hexbuf, sizeof(hexbuf), 0);
315 fwrite(hexbuf, 2 * (size_t)ret, 1, stdout);
316 } else {
317 fwrite(buf, (size_t)ret, 1, stdout);
318 }
319
320 outlen -= (size_t)ret;
321 }
322
323 ret = 0;
324
325 out:
326 if (rng)
327 kcapi_rng_destroy(rng);
328 kcapi_memset_secure(buf, 0, sizeof(buf));
329
330 /* Free seedbuf if it was allocated. */
331 if (seedbuf && (seedbuf != buf)) {
332 kcapi_memset_secure(seedbuf, 0, seedsize);
333 free(seedbuf);
334 }
335
336 return (int)ret;
337 }