2 * cryptsetup kernel RNG access functions
4 * Copyright (C) 2010 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 after the warning is printed when there is random data (entropy) */
41 #define RANDOM_DEVICE_TIMEOUT 5
43 /* URANDOM_DEVICE access */
44 static int _get_urandom(struct crypt_device *ctx, char *buf, size_t len)
50 assert(urandom_fd != -1);
53 r = read(urandom_fd, buf, len);
54 if (r == -1 && errno != EINTR)
61 assert((size_t)(buf - old_buf) == old_len);
66 static void _get_random_progress(struct crypt_device *ctx, int warn,
67 size_t expected_len, size_t read_len)
71 _("System is out of entropy while generating volume key.\n"
72 "Please move mouse or type some text in another window "
73 "to gather some random events.\n"));
75 log_std(ctx, _("Generating key (%d%% done).\n"),
76 (int)((expected_len - read_len) * 100 / expected_len));
79 /* RANDOM_DEVICE access */
80 static int _get_random(struct crypt_device *ctx, char *buf, size_t len)
83 size_t n, old_len = len;
88 assert(random_fd != -1);
92 FD_SET(random_fd, &fds);
94 tv.tv_sec = RANDOM_DEVICE_TIMEOUT;
97 r = select(random_fd + 1, &fds, NULL, NULL, &tv);
102 _get_random_progress(ctx, warn_once, old_len, len);
108 n = RANDOM_DEVICE_CHUNK;
109 if (len < RANDOM_DEVICE_CHUNK)
112 r = read(random_fd, buf, n);
114 if (r == -1 && errno == EINTR)
121 /* random device is opened with O_NONBLOCK, EAGAIN is expected */
122 if (r == -1 && (errno != EAGAIN || errno != EWOULDBLOCK))
129 } while (len && r > 0);
133 assert((size_t)(buf - old_buf) == old_len);
136 _get_random_progress(ctx, 0, old_len, len);
140 /* Initialisation of both RNG file descriptors is mandatory */
141 int crypt_random_init(struct crypt_device *ctx)
143 /* Used for CRYPT_RND_NORMAL */
145 urandom_fd = open(URANDOM_DEVICE, O_RDONLY);
149 /* Used for CRYPT_RND_KEY */
151 random_fd = open(RANDOM_DEVICE, O_RDONLY | O_NONBLOCK);
155 random_initialised = 1;
159 log_err(ctx, _("Fatal error during RNG initialisation.\n"));
163 int crypt_random_get(struct crypt_device *ctx, char *buf, size_t len, int quality)
165 int status, rng_type;
168 case CRYPT_RND_NORMAL:
169 status = _get_urandom(ctx, buf, len);
172 rng_type = ctx ? crypt_get_rng_type(ctx) :
173 crypt_random_default_key_rng();
175 case CRYPT_RNG_URANDOM:
176 status = _get_urandom(ctx, buf, len);
178 case CRYPT_RNG_RANDOM:
179 status = _get_random(ctx, buf, len);
186 log_err(ctx, _("Unknown RNG quality requested.\n"));
191 log_err(ctx, _("Error %d reading from RNG: %s\n"),
192 errno, strerror(errno));
197 void crypt_random_exit()
199 random_initialised = 0;
201 if(random_fd != -1) {
202 (void)close(random_fd);
206 if(urandom_fd != -1) {
207 (void)close(urandom_fd);
212 int crypt_random_default_key_rng()
214 if (!strcmp(DEFAULT_RNG, RANDOM_DEVICE))
215 return CRYPT_RNG_RANDOM;
217 if (!strcmp(DEFAULT_RNG, URANDOM_DEVICE))
218 return CRYPT_RNG_URANDOM;
220 /* RNG misconfiguration is fatal */