* Implement --use-random and --use-urandom for luksFormat to allow setting of RNG...
[platform/upstream/cryptsetup.git] / lib / random.c
1 /*
2  * cryptsetup kernel RNG access functions
3  *
4  * Copyright (C) 2010 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 after the warning is printed when there is random data (entropy) */
41 #define RANDOM_DEVICE_TIMEOUT   5
42
43 /* URANDOM_DEVICE access */
44 static int _get_urandom(struct crypt_device *ctx, char *buf, size_t len)
45 {
46         int r;
47         size_t old_len = len;
48         char *old_buf = buf;
49
50         assert(urandom_fd != -1);
51
52         while(len) {
53                 r = read(urandom_fd, buf, len);
54                 if (r == -1 && errno != EINTR)
55                         return -EINVAL;
56                 len -= r;
57                 buf += r;
58         }
59
60         assert(len == 0);
61         assert((size_t)(buf - old_buf) == old_len);
62
63         return 0;
64 }
65
66 static void _get_random_progress(struct crypt_device *ctx, int warn,
67                                  size_t expected_len, size_t read_len)
68 {
69         if (warn)
70                 log_std(ctx,
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"));
74
75         log_std(ctx, _("Generating key (%d%% done).\n"),
76                 (int)((expected_len - read_len) * 100 / expected_len));
77 }
78
79 /* RANDOM_DEVICE access */
80 static int _get_random(struct crypt_device *ctx, char *buf, size_t len)
81 {
82         int r, warn_once = 1;
83         size_t n, old_len = len;
84         char *old_buf = buf;
85         fd_set fds;
86         struct timeval tv;
87
88         assert(random_fd != -1);
89
90         while (len) {
91                 FD_ZERO(&fds);
92                 FD_SET(random_fd, &fds);
93
94                 tv.tv_sec = RANDOM_DEVICE_TIMEOUT;
95                 tv.tv_usec = 0;
96
97                 r = select(random_fd + 1, &fds, NULL, NULL, &tv);
98                 if(r == -1)
99                         return -EINVAL;
100
101                 if(!r) {
102                         _get_random_progress(ctx, warn_once, old_len, len);
103                         warn_once = 0;
104                         continue;
105                 }
106
107                 do {
108                         n = RANDOM_DEVICE_CHUNK;
109                         if (len < RANDOM_DEVICE_CHUNK)
110                                 n = len;
111
112                         r = read(random_fd, buf, n);
113
114                         if (r == -1 && errno == EINTR)
115                                 continue;
116
117                         /* bogus read? */
118                         if(r > (int)n)
119                                 return -EINVAL;
120
121                         /* random device is opened with O_NONBLOCK, EAGAIN is expected */
122                         if (r == -1 && (errno != EAGAIN || errno != EWOULDBLOCK))
123                                 return -EINVAL;
124
125                         if (r > 0) {
126                                 len -= r;
127                                 buf += r;
128                         }
129                 } while (len && r > 0);
130         }
131
132         assert(len == 0);
133         assert((size_t)(buf - old_buf) == old_len);
134
135         if (!warn_once)
136                 _get_random_progress(ctx, 0, old_len, len);
137
138         return 0;
139 }
140 /* Initialisation of both RNG file descriptors is mandatory */
141 int crypt_random_init(struct crypt_device *ctx)
142 {
143         /* Used for CRYPT_RND_NORMAL */
144         if(urandom_fd == -1)
145                 urandom_fd = open(URANDOM_DEVICE, O_RDONLY);
146         if(urandom_fd == -1)
147                 goto fail;
148
149         /* Used for CRYPT_RND_KEY */
150         if(random_fd == -1)
151                 random_fd = open(RANDOM_DEVICE, O_RDONLY | O_NONBLOCK);
152         if(random_fd == -1)
153                 goto fail;
154
155         random_initialised = 1;
156         return 0;
157 fail:
158         crypt_random_exit();
159         log_err(ctx, _("Fatal error during RNG initialisation.\n"));
160         return -ENOSYS;
161 }
162
163 int crypt_random_get(struct crypt_device *ctx, char *buf, size_t len, int quality)
164 {
165         int status, rng_type;
166
167         switch(quality) {
168         case CRYPT_RND_NORMAL:
169                 status = _get_urandom(ctx, buf, len);
170                 break;
171         case CRYPT_RND_KEY:
172                 rng_type = ctx ? crypt_get_rng_type(ctx) :
173                                  crypt_random_default_key_rng();
174                 switch (rng_type) {
175                 case CRYPT_RNG_URANDOM:
176                         status = _get_urandom(ctx, buf, len);
177                         break;
178                 case CRYPT_RNG_RANDOM:
179                         status = _get_random(ctx, buf, len);
180                         break;
181                 default:
182                         abort();
183                 }
184                 break;
185         default:
186                 log_err(ctx, _("Unknown RNG quality requested.\n"));
187                 return -EINVAL;
188         }
189
190         if (status)
191                 log_err(ctx, _("Error %d reading from RNG: %s\n"),
192                         errno, strerror(errno));
193
194         return status;
195 }
196
197 void crypt_random_exit()
198 {
199         random_initialised = 0;
200
201         if(random_fd != -1) {
202                 (void)close(random_fd);
203                 random_fd = -1;
204         }
205
206         if(urandom_fd != -1) {
207                 (void)close(urandom_fd);
208                 urandom_fd = -1;
209         }
210 }
211
212 int crypt_random_default_key_rng()
213 {
214         if (!strcmp(DEFAULT_RNG, RANDOM_DEVICE))
215                 return CRYPT_RNG_RANDOM;
216
217         if (!strcmp(DEFAULT_RNG, URANDOM_DEVICE))
218                 return CRYPT_RNG_URANDOM;
219
220         /* RNG misconfiguration is fatal */
221         abort();
222 }