2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 /** @file lib/random.c
20 * @brief Random number generator
36 static int random_count;
37 static int random_fd = -1;
38 static arcfour_context random_ctx[1];
40 /** @brief Rekey the RNG
42 * Resets the RNG's key to a random one read from /dev/urandom
44 static void random__rekey(void) {
49 if((random_fd = open("/dev/urandom", O_RDONLY)) < 0)
50 disorder_fatal(errno, "opening /dev/urandom");
52 if((n = read(random_fd, key, sizeof key)) < 0)
53 disorder_fatal(errno, "reading from /dev/urandom");
54 if((size_t)n < sizeof key)
55 disorder_fatal(0, "reading from /dev/urandom: short read");
56 arcfour_setkey(random_ctx, key, sizeof key);
57 random_count = 8 * 1024 * 1024;
60 /** @brief Get random bytes
61 * @param ptr Where to put random bytes
62 * @param bytes How many random bytes to generate
64 void random_get(void *ptr, size_t bytes) {
67 /* Encrypting 0s == just returning the keystream */
68 memset(ptr, 0, bytes);
69 arcfour_stream(random_ctx, (char *)ptr, (char *)ptr, bytes);
70 if(bytes > (size_t)random_count)
73 random_count -= bytes;
76 /** @brief Return a random ID string */
77 char *random_id(void) {
81 random_get(words, sizeof words);
82 basen(words, sizeof words / sizeof *words, id, sizeof id, 62);