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 * version 2 as published by the Free Software Foundation.
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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "libcryptsetup.h"
29 static int random_initialised = 0;
31 #define URANDOM_DEVICE "/dev/urandom"
32 static int urandom_fd = -1;
34 #define RANDOM_DEVICE "/dev/random"
35 static int random_fd = -1;
37 /* Read random chunk - gathered data usually appears with this granularity */
38 #define RANDOM_DEVICE_CHUNK 8
40 /* Timeout to print warning if no random data (entropy) */
41 #define RANDOM_DEVICE_TIMEOUT 5
43 /* URANDOM_DEVICE access */
44 static int _get_urandom(struct crypt_device *ctx __attribute__((unused)),
45 char *buf, size_t len)
51 assert(urandom_fd != -1);
54 r = read(urandom_fd, buf, len);
55 if (r == -1 && errno != EINTR)
64 assert((size_t)(buf - old_buf) == old_len);
69 static void _get_random_progress(struct crypt_device *ctx, int warn,
70 size_t expected_len, size_t read_len)
74 _("System is out of entropy while generating volume key.\n"
75 "Please move mouse or type some text in another window "
76 "to gather some random events.\n"));
78 log_std(ctx, _("Generating key (%d%% done).\n"),
79 (int)((expected_len - read_len) * 100 / expected_len));
82 /* RANDOM_DEVICE access */
83 static int _get_random(struct crypt_device *ctx, char *buf, size_t len)
86 size_t n, old_len = len;
91 assert(random_fd != -1);
95 FD_SET(random_fd, &fds);
97 tv.tv_sec = RANDOM_DEVICE_TIMEOUT;
100 r = select(random_fd + 1, &fds, NULL, NULL, &tv);
105 _get_random_progress(ctx, warn_once, old_len, len);
111 n = RANDOM_DEVICE_CHUNK;
112 if (len < RANDOM_DEVICE_CHUNK)
115 r = read(random_fd, buf, n);
117 if (r == -1 && errno == EINTR) {
126 /* random device is opened with O_NONBLOCK, EAGAIN is expected */
127 if (r == -1 && (errno != EAGAIN && errno != EWOULDBLOCK))
134 } while (len && r > 0);
138 assert((size_t)(buf - old_buf) == old_len);
141 _get_random_progress(ctx, 0, old_len, len);
145 /* Initialisation of both RNG file descriptors is mandatory */
146 int crypt_random_init(struct crypt_device *ctx)
148 if (random_initialised)
151 /* Used for CRYPT_RND_NORMAL */
153 urandom_fd = open(URANDOM_DEVICE, O_RDONLY);
157 /* Used for CRYPT_RND_KEY */
159 random_fd = open(RANDOM_DEVICE, O_RDONLY | O_NONBLOCK);
163 random_initialised = 1;
167 log_err(ctx, _("Fatal error during RNG initialisation.\n"));
171 int crypt_random_get(struct crypt_device *ctx, char *buf, size_t len, int quality)
173 int status, rng_type;
176 case CRYPT_RND_NORMAL:
177 status = _get_urandom(ctx, buf, len);
180 if (crypt_fips_mode())
181 status = crypt_backend_rng(buf, len, quality, 1);
183 status = _get_urandom(ctx, buf, len);
186 if (crypt_fips_mode()) {
187 status = crypt_backend_rng(buf, len, quality, 1);
190 rng_type = ctx ? crypt_get_rng_type(ctx) :
191 crypt_random_default_key_rng();
193 case CRYPT_RNG_URANDOM:
194 status = _get_urandom(ctx, buf, len);
196 case CRYPT_RNG_RANDOM:
197 status = _get_random(ctx, buf, len);
204 log_err(ctx, _("Unknown RNG quality requested.\n"));
209 log_err(ctx, _("Error %d reading from RNG: %s\n"),
210 errno, strerror(errno));
215 void crypt_random_exit(void)
217 random_initialised = 0;
219 if(random_fd != -1) {
220 (void)close(random_fd);
224 if(urandom_fd != -1) {
225 (void)close(urandom_fd);
230 int crypt_random_default_key_rng(void)
232 if (!strcmp(DEFAULT_RNG, RANDOM_DEVICE))
233 return CRYPT_RNG_RANDOM;
235 if (!strcmp(DEFAULT_RNG, URANDOM_DEVICE))
236 return CRYPT_RNG_URANDOM;
238 /* RNG misconfiguration is fatal */