5de4214703e654d0ff314297189c0a6ba282dadb
[platform/upstream/cryptsetup.git] / lib / random.c
1 /*
2  * cryptsetup kernel RNG access functions
3  *
4  * Copyright (C) 2010-2012, Red Hat, Inc. All rights reserved.
5  *
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.
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  *
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.
18  */
19
20 #include <stdlib.h>
21 #include <string.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <assert.h>
25
26 #include "libcryptsetup.h"
27 #include "internal.h"
28
29 static int random_initialised = 0;
30
31 #define URANDOM_DEVICE  "/dev/urandom"
32 static int urandom_fd = -1;
33
34 #define RANDOM_DEVICE   "/dev/random"
35 static int random_fd = -1;
36
37 /* Read random chunk - gathered data usually appears with this granularity */
38 #define RANDOM_DEVICE_CHUNK     8
39
40 /* Timeout to print warning if no random data (entropy) */
41 #define RANDOM_DEVICE_TIMEOUT   5
42
43 /* URANDOM_DEVICE access */
44 static int _get_urandom(struct crypt_device *ctx __attribute__((unused)),
45                         char *buf, size_t len)
46 {
47         int r;
48         size_t old_len = len;
49         char *old_buf = buf;
50
51         assert(urandom_fd != -1);
52
53         while(len) {
54                 r = read(urandom_fd, buf, len);
55                 if (r == -1 && errno != EINTR)
56                         return -EINVAL;
57                 if (r > 0) {
58                         len -= r;
59                         buf += r;
60                 }
61         }
62
63         assert(len == 0);
64         assert((size_t)(buf - old_buf) == old_len);
65
66         return 0;
67 }
68
69 static void _get_random_progress(struct crypt_device *ctx, int warn,
70                                  size_t expected_len, size_t read_len)
71 {
72         if (warn)
73                 log_std(ctx,
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"));
77
78         log_std(ctx, _("Generating key (%d%% done).\n"),
79                 (int)((expected_len - read_len) * 100 / expected_len));
80 }
81
82 /* RANDOM_DEVICE access */
83 static int _get_random(struct crypt_device *ctx, char *buf, size_t len)
84 {
85         int r, warn_once = 1;
86         size_t n, old_len = len;
87         char *old_buf = buf;
88         fd_set fds;
89         struct timeval tv;
90
91         assert(random_fd != -1);
92
93         while (len) {
94                 FD_ZERO(&fds);
95                 FD_SET(random_fd, &fds);
96
97                 tv.tv_sec = RANDOM_DEVICE_TIMEOUT;
98                 tv.tv_usec = 0;
99
100                 r = select(random_fd + 1, &fds, NULL, NULL, &tv);
101                 if(r == -1)
102                         return -EINVAL;
103
104                 if(!r) {
105                         _get_random_progress(ctx, warn_once, old_len, len);
106                         warn_once = 0;
107                         continue;
108                 }
109
110                 do {
111                         n = RANDOM_DEVICE_CHUNK;
112                         if (len < RANDOM_DEVICE_CHUNK)
113                                 n = len;
114
115                         r = read(random_fd, buf, n);
116
117                         if (r == -1 && errno == EINTR) {
118                                 r = 0;
119                                 continue;
120                         }
121
122                         /* bogus read? */
123                         if(r > (int)n)
124                                 return -EINVAL;
125
126                         /* random device is opened with O_NONBLOCK, EAGAIN is expected */
127                         if (r == -1 && (errno != EAGAIN && errno != EWOULDBLOCK))
128                                 return -EINVAL;
129
130                         if (r > 0) {
131                                 len -= r;
132                                 buf += r;
133                         }
134                 } while (len && r > 0);
135         }
136
137         assert(len == 0);
138         assert((size_t)(buf - old_buf) == old_len);
139
140         if (!warn_once)
141                 _get_random_progress(ctx, 0, old_len, len);
142
143         return 0;
144 }
145 /* Initialisation of both RNG file descriptors is mandatory */
146 int crypt_random_init(struct crypt_device *ctx)
147 {
148         if (random_initialised)
149                 return 0;
150
151         /* Used for CRYPT_RND_NORMAL */
152         if(urandom_fd == -1)
153                 urandom_fd = open(URANDOM_DEVICE, O_RDONLY);
154         if(urandom_fd == -1)
155                 goto fail;
156
157         /* Used for CRYPT_RND_KEY */
158         if(random_fd == -1)
159                 random_fd = open(RANDOM_DEVICE, O_RDONLY | O_NONBLOCK);
160         if(random_fd == -1)
161                 goto fail;
162
163         random_initialised = 1;
164         return 0;
165 fail:
166         crypt_random_exit();
167         log_err(ctx, _("Fatal error during RNG initialisation.\n"));
168         return -ENOSYS;
169 }
170
171 int crypt_random_get(struct crypt_device *ctx, char *buf, size_t len, int quality)
172 {
173         int status, rng_type;
174
175         switch(quality) {
176         case CRYPT_RND_NORMAL:
177                 status = _get_urandom(ctx, buf, len);
178                 break;
179         case CRYPT_RND_SALT:
180                 if (crypt_fips_mode())
181                         status = crypt_backend_rng(buf, len, quality, 1);
182                 else
183                         status = _get_urandom(ctx, buf, len);
184                 break;
185         case CRYPT_RND_KEY:
186                 if (crypt_fips_mode()) {
187                         status = crypt_backend_rng(buf, len, quality, 1);
188                         break;
189                 }
190                 rng_type = ctx ? crypt_get_rng_type(ctx) :
191                                  crypt_random_default_key_rng();
192                 switch (rng_type) {
193                 case CRYPT_RNG_URANDOM:
194                         status = _get_urandom(ctx, buf, len);
195                         break;
196                 case CRYPT_RNG_RANDOM:
197                         status = _get_random(ctx, buf, len);
198                         break;
199                 default:
200                         abort();
201                 }
202                 break;
203         default:
204                 log_err(ctx, _("Unknown RNG quality requested.\n"));
205                 return -EINVAL;
206         }
207
208         if (status)
209                 log_err(ctx, _("Error %d reading from RNG: %s\n"),
210                         errno, strerror(errno));
211
212         return status;
213 }
214
215 void crypt_random_exit(void)
216 {
217         random_initialised = 0;
218
219         if(random_fd != -1) {
220                 (void)close(random_fd);
221                 random_fd = -1;
222         }
223
224         if(urandom_fd != -1) {
225                 (void)close(urandom_fd);
226                 urandom_fd = -1;
227         }
228 }
229
230 int crypt_random_default_key_rng(void)
231 {
232         if (!strcmp(DEFAULT_RNG, RANDOM_DEVICE))
233                 return CRYPT_RNG_RANDOM;
234
235         if (!strcmp(DEFAULT_RNG, URANDOM_DEVICE))
236                 return CRYPT_RNG_URANDOM;
237
238         /* RNG misconfiguration is fatal */
239         abort();
240 }