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