2 * utils_crypt - cipher utilities for cryptsetup
4 * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
5 * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * version 2 as published by the Free Software Foundation.
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.
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.
26 #include <sys/types.h>
32 #include "libcryptsetup.h"
34 #include "utils_crypt.h"
36 #define log_dbg(x) crypt_log(NULL, CRYPT_LOG_DEBUG, x)
37 #define log_err(cd, x) crypt_log(cd, CRYPT_LOG_ERROR, x)
39 struct safe_allocation {
44 int crypt_parse_name_and_mode(const char *s, char *cipher, int *key_nums,
47 if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]-%" MAX_CIPHER_LEN_STR "s",
48 cipher, cipher_mode) == 2) {
49 if (!strcmp(cipher_mode, "plain"))
50 strncpy(cipher_mode, "cbc-plain", 10);
52 char *tmp = strchr(cipher, ':');
53 *key_nums = tmp ? atoi(++tmp) : 1;
61 /* Short version for "empty" cipher */
62 if (!strcmp(s, "null")) {
63 strncpy(cipher, "cipher_null", MAX_CIPHER_LEN);
64 strncpy(cipher_mode, "ecb", 9);
70 if (sscanf(s, "%" MAX_CIPHER_LEN_STR "[^-]", cipher) == 1) {
71 strncpy(cipher_mode, "cbc-plain", 10);
80 /* safe allocations */
81 void *crypt_safe_alloc(size_t size)
83 struct safe_allocation *alloc;
88 alloc = malloc(size + offsetof(struct safe_allocation, data));
97 void crypt_safe_free(void *data)
99 struct safe_allocation *alloc;
104 alloc = (struct safe_allocation *)
105 ((char *)data - offsetof(struct safe_allocation, data));
107 memset(data, 0, alloc->size);
109 alloc->size = 0x55aa55aa;
113 void *crypt_safe_realloc(void *data, size_t size)
115 struct safe_allocation *alloc;
118 new_data = crypt_safe_alloc(size);
120 if (new_data && data) {
122 alloc = (struct safe_allocation *)
123 ((char *)data - offsetof(struct safe_allocation, data));
125 if (size > alloc->size)
128 memcpy(new_data, data, size);
131 crypt_safe_free(data);
135 /* Password reading helpers */
136 static int untimed_read(int fd, char *pass, size_t maxlen)
140 i = read(fd, pass, maxlen);
144 } else if (i == 0) { /* EOF */
151 static int timed_read(int fd, char *pass, size_t maxlen, long timeout)
162 if (select(fd+1, &fds, NULL, NULL, &t) > 0)
163 failed = untimed_read(fd, pass, maxlen);
168 static int interactive_pass(const char *prompt, char *pass, size_t maxlen,
171 struct termios orig, tmp;
173 int infd = STDIN_FILENO, outfd;
178 /* Read and write to /dev/tty if available */
179 if ((infd = outfd = open("/dev/tty", O_RDWR)) == -1) {
181 outfd = STDERR_FILENO;
184 if (tcgetattr(infd, &orig))
187 memcpy(&tmp, &orig, sizeof(tmp));
188 tmp.c_lflag &= ~ECHO;
190 if (prompt && write(outfd, prompt, strlen(prompt)) < 0)
193 tcsetattr(infd, TCSAFLUSH, &tmp);
195 failed = timed_read(infd, pass, maxlen, timeout);
197 failed = untimed_read(infd, pass, maxlen);
198 tcsetattr(infd, TCSAFLUSH, &orig);
201 if (!failed && write(outfd, "\n", 1)) {};
203 if (infd != STDIN_FILENO)
208 static int crypt_get_key_tty(const char *prompt,
209 char **key, size_t *key_size,
210 int timeout, int verify,
211 struct crypt_device *cd)
213 int key_size_max = DEFAULT_PASSPHRASE_SIZE_MAX;
215 char *pass = NULL, *pass_verify = NULL;
217 log_dbg("Interactive passphrase entry requested.");
219 pass = crypt_safe_alloc(key_size_max + 1);
221 log_err(cd, _("Out of memory while reading passphrase.\n"));
225 if (interactive_pass(prompt, pass, key_size_max, timeout)) {
226 log_err(cd, _("Error reading passphrase from terminal.\n"));
229 pass[key_size_max] = '\0';
232 pass_verify = crypt_safe_alloc(key_size_max);
234 log_err(cd, _("Out of memory while reading passphrase.\n"));
239 if (interactive_pass(_("Verify passphrase: "),
240 pass_verify, key_size_max, timeout)) {
241 log_err(cd, _("Error reading passphrase from terminal.\n"));
245 if (strncmp(pass, pass_verify, key_size_max)) {
246 log_err(cd, _("Passphrases do not match.\n"));
253 *key_size = strlen(pass);
256 crypt_safe_free(pass_verify);
258 crypt_safe_free(pass);
263 * Note: --key-file=- is interpreted as a read from a binary file (stdin)
264 * key_size_max == 0 means detect maximum according to input type (tty/file)
265 * timeout and verify options only applies to tty input
267 int crypt_get_key(const char *prompt,
268 char **key, size_t *key_size,
269 size_t keyfile_offset, size_t keyfile_size_max,
270 const char *key_file, int timeout, int verify,
271 struct crypt_device *cd)
273 int fd, regular_file, read_stdin, char_read, unlimited_read = 0;
275 char *pass = NULL, tmp;
276 size_t buflen, i, file_read_size;
282 /* Passphrase read from stdin? */
283 read_stdin = (!key_file || !strcmp(key_file, "-")) ? 1 : 0;
285 if (read_stdin && isatty(STDIN_FILENO)) {
286 if (keyfile_offset) {
287 log_err(cd, _("Cannot use offset with terminal input.\n"));
290 return crypt_get_key_tty(prompt, key, key_size, timeout, verify, cd);
294 log_dbg("STDIN descriptor passphrase entry requested.");
296 log_dbg("File descriptor passphrase entry requested.");
298 /* If not requsted otherwise, we limit input to prevent memory exhaustion */
299 if (keyfile_size_max == 0) {
300 keyfile_size_max = DEFAULT_KEYFILE_SIZE_MAXKB * 1024;
304 fd = read_stdin ? STDIN_FILENO : open(key_file, O_RDONLY);
306 log_err(cd, _("Failed to open key file.\n"));
310 /* use 4k for buffer (page divisor but avoid huge pages) */
311 buflen = 4096 - sizeof(struct safe_allocation);
314 if(stat(key_file, &st) < 0) {
315 log_err(cd, _("Failed to stat key file.\n"));
318 if(S_ISREG(st.st_mode)) {
320 file_read_size = (size_t)st.st_size;
322 if (keyfile_offset > file_read_size) {
323 log_err(cd, _("Cannot seek to requested keyfile offset.\n"));
326 file_read_size -= keyfile_offset;
328 /* known keyfile size, alloc it in one step */
329 if (file_read_size >= keyfile_size_max)
330 buflen = keyfile_size_max;
331 else if (file_read_size)
332 buflen = file_read_size;
336 pass = crypt_safe_alloc(buflen);
338 log_err(cd, _("Out of memory while reading passphrase.\n"));
342 /* Discard keyfile_offset bytes on input */
343 for(i = 0; i < keyfile_offset; i++)
344 if (read(fd, &tmp, 1) != 1) {
345 log_err(cd, _("Cannot seek to requested keyfile offset.\n"));
349 for(i = 0; i < keyfile_size_max; i++) {
352 pass = crypt_safe_realloc(pass, buflen);
354 log_err(cd, _("Out of memory while reading passphrase.\n"));
360 char_read = read(fd, &pass[i], 1);
362 log_err(cd, _("Error reading passphrase.\n"));
366 /* Stop on newline only if not requested read from keyfile */
367 if(char_read == 0 || (!key_file && pass[i] == '\n'))
371 /* Fail if piped input dies reading nothing */
372 if(!i && !regular_file) {
373 log_dbg("Nothing read on input.");
378 /* Fail if we exceeded internal default (no specified size) */
379 if (unlimited_read && i == keyfile_size_max) {
380 log_err(cd, _("Maximum keyfile size exceeded.\n"));
384 if (!unlimited_read && i != keyfile_size_max) {
385 log_err(cd, _("Cannot read requested amount of data.\n"));
393 if(fd != STDIN_FILENO)
397 crypt_safe_free(pass);