Commit | Line | Data |
---|---|---|
fcdff139 RK |
1 | /* |
2 | * This file is part of DisOrder | |
3 | * Copyright (C) 2008 Richard Kettlewell | |
4 | * | |
e7eb3a27 | 5 | * This program is free software: you can redistribute it and/or modify |
fcdff139 | 6 | * it under the terms of the GNU General Public License as published by |
e7eb3a27 | 7 | * the Free Software Foundation, either version 3 of the License, or |
fcdff139 | 8 | * (at your option) any later version. |
e7eb3a27 RK |
9 | * |
10 | * This program 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 | |
13 | * GNU General Public License for more details. | |
14 | * | |
fcdff139 | 15 | * You should have received a copy of the GNU General Public License |
e7eb3a27 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
fcdff139 RK |
17 | */ |
18 | ||
19 | /** @file lib/random.c | |
20 | * @brief Random number generator | |
21 | * | |
22 | */ | |
23 | ||
05b75f8d | 24 | #include "common.h" |
fcdff139 RK |
25 | |
26 | #include <fcntl.h> | |
27 | #include <unistd.h> | |
28 | #include <errno.h> | |
fcdff139 RK |
29 | |
30 | #include "random.h" | |
31 | #include "log.h" | |
ed8e4373 | 32 | #include "salsa208.h" |
fdca70ee RK |
33 | #include "basen.h" |
34 | #include "mem.h" | |
fcdff139 RK |
35 | |
36 | static int random_count; | |
37 | static int random_fd = -1; | |
ed8e4373 | 38 | static salsa208_context random_ctx[1]; |
fcdff139 RK |
39 | |
40 | /** @brief Rekey the RNG | |
41 | * | |
42 | * Resets the RNG's key to a random one read from /dev/urandom | |
43 | */ | |
44 | static void random__rekey(void) { | |
ed8e4373 | 45 | char key[32]; |
fcdff139 RK |
46 | int n; |
47 | ||
48 | if(random_fd < 0) { | |
49 | if((random_fd = open("/dev/urandom", O_RDONLY)) < 0) | |
2e9ba080 | 50 | disorder_fatal(errno, "opening /dev/urandom"); |
fcdff139 RK |
51 | } |
52 | if((n = read(random_fd, key, sizeof key)) < 0) | |
2e9ba080 | 53 | disorder_fatal(errno, "reading from /dev/urandom"); |
fcdff139 | 54 | if((size_t)n < sizeof key) |
2e9ba080 | 55 | disorder_fatal(0, "reading from /dev/urandom: short read"); |
ed8e4373 MW |
56 | salsa208_setkey(random_ctx, key, sizeof key); |
57 | random_count = 256 * 1024 * 1024; | |
fcdff139 RK |
58 | } |
59 | ||
60 | /** @brief Get random bytes | |
61 | * @param ptr Where to put random bytes | |
62 | * @param bytes How many random bytes to generate | |
63 | */ | |
fdca70ee | 64 | void random_get(void *ptr, size_t bytes) { |
fcdff139 RK |
65 | if(random_count == 0) |
66 | random__rekey(); | |
ed8e4373 | 67 | salsa208_stream(random_ctx, 0, ptr, bytes); |
fcdff139 RK |
68 | if(bytes > (size_t)random_count) |
69 | random_count = 0; | |
70 | else | |
71 | random_count -= bytes; | |
72 | } | |
73 | ||
fdca70ee RK |
74 | /** @brief Return a random ID string */ |
75 | char *random_id(void) { | |
1c814807 | 76 | uint32_t words[2]; |
fdca70ee RK |
77 | char id[128]; |
78 | ||
79 | random_get(words, sizeof words); | |
067eeb5f | 80 | basen(words, sizeof words / sizeof *words, id, sizeof id, 62); |
fdca70ee RK |
81 | return xstrdup(id); |
82 | } | |
83 | ||
fcdff139 RK |
84 | /* |
85 | Local Variables: | |
86 | c-basic-offset:2 | |
87 | comment-column:40 | |
88 | fill-column:79 | |
89 | indent-tabs-mode:nil | |
90 | End: | |
91 | */ |