2 * cryptsetup kernel RNG access functions
4 * Copyright (C) 2010-2012, Red Hat, Inc. All rights reserved.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/select.h>
28 #include "libcryptsetup.h"
31 static int random_initialised = 0;
33 #define URANDOM_DEVICE "/dev/urandom"
34 static int urandom_fd = -1;
36 #define RANDOM_DEVICE "/dev/random"
37 static int random_fd = -1;
39 /* Read random chunk - gathered data usually appears with this granularity */
40 #define RANDOM_DEVICE_CHUNK 8
42 /* Timeout to print warning if no random data (entropy) */
43 #define RANDOM_DEVICE_TIMEOUT 5
45 /* URANDOM_DEVICE access */
46 static int _get_urandom(struct crypt_device *ctx __attribute__((unused)),
47 char *buf, size_t len)
53 assert(urandom_fd != -1);
56 r = read(urandom_fd, buf, len);
57 if (r == -1 && errno != EINTR)
66 assert((size_t)(buf - old_buf) == old_len);
71 static void _get_random_progress(struct crypt_device *ctx, int warn,
72 size_t expected_len, size_t read_len)
76 _("System is out of entropy while generating volume key.\n"
77 "Please move mouse or type some text in another window "
78 "to gather some random events.\n"));
80 log_std(ctx, _("Generating key (%d%% done).\n"),
81 (int)((expected_len - read_len) * 100 / expected_len));
84 /* RANDOM_DEVICE access */
85 static int _get_random(struct crypt_device *ctx, char *buf, size_t len)
88 size_t n, old_len = len;
93 assert(random_fd != -1);
97 FD_SET(random_fd, &fds);
99 tv.tv_sec = RANDOM_DEVICE_TIMEOUT;
102 r = select(random_fd + 1, &fds, NULL, NULL, &tv);
107 _get_random_progress(ctx, warn_once, old_len, len);
113 n = RANDOM_DEVICE_CHUNK;
114 if (len < RANDOM_DEVICE_CHUNK)
117 r = read(random_fd, buf, n);
119 if (r == -1 && errno == EINTR) {
128 /* random device is opened with O_NONBLOCK, EAGAIN is expected */
129 if (r == -1 && (errno != EAGAIN && errno != EWOULDBLOCK))
136 } while (len && r > 0);
140 assert((size_t)(buf - old_buf) == old_len);
143 _get_random_progress(ctx, 0, old_len, len);
147 /* Initialisation of both RNG file descriptors is mandatory */
148 int crypt_random_init(struct crypt_device *ctx)
150 if (random_initialised)
153 /* Used for CRYPT_RND_NORMAL */
155 urandom_fd = open(URANDOM_DEVICE, O_RDONLY);
159 /* Used for CRYPT_RND_KEY */
161 random_fd = open(RANDOM_DEVICE, O_RDONLY | O_NONBLOCK);
165 random_initialised = 1;
169 log_err(ctx, _("Fatal error during RNG initialisation.\n"));
173 int crypt_random_get(struct crypt_device *ctx, char *buf, size_t len, int quality)
175 int status, rng_type;
178 case CRYPT_RND_NORMAL:
179 status = _get_urandom(ctx, buf, len);
182 if (crypt_fips_mode())
183 status = crypt_backend_rng(buf, len, quality, 1);
185 status = _get_urandom(ctx, buf, len);
188 if (crypt_fips_mode()) {
189 status = crypt_backend_rng(buf, len, quality, 1);
192 rng_type = ctx ? crypt_get_rng_type(ctx) :
193 crypt_random_default_key_rng();
195 case CRYPT_RNG_URANDOM:
196 status = _get_urandom(ctx, buf, len);
198 case CRYPT_RNG_RANDOM:
199 status = _get_random(ctx, buf, len);
206 log_err(ctx, _("Unknown RNG quality requested.\n"));
211 log_err(ctx, _("Error %d reading from RNG: %s\n"),
212 errno, strerror(errno));
217 void crypt_random_exit(void)
219 random_initialised = 0;
221 if(random_fd != -1) {
222 (void)close(random_fd);
226 if(urandom_fd != -1) {
227 (void)close(urandom_fd);
232 int crypt_random_default_key_rng(void)
234 if (!strcmp(DEFAULT_RNG, RANDOM_DEVICE))
235 return CRYPT_RNG_RANDOM;
237 if (!strcmp(DEFAULT_RNG, URANDOM_DEVICE))
238 return CRYPT_RNG_URANDOM;
240 /* RNG misconfiguration is fatal */