dbbbbeef937c00e41c1b17accbc0f1e0c55e363f
[platform/upstream/cryptsetup.git] / lib / crypt_plain.c
1 /*
2  * cryptsetup plain device helper functions
3  *
4  * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5  * Copyright (C) 2010-2012 Red Hat, Inc. All rights reserved.
6  * Copyright (C) 2010-2012, Milan Broz
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <string.h>
23 #include <stdlib.h>
24 #include <errno.h>
25
26 #include "libcryptsetup.h"
27 #include "internal.h"
28
29 static int hash(const char *hash_name, size_t key_size, char *key,
30                 size_t passphrase_size, const char *passphrase)
31 {
32         struct crypt_hash *md = NULL;
33         size_t len;
34         int round, i, r = 0;
35
36         if (crypt_hash_init(&md, hash_name))
37                 return -ENOENT;
38
39         len = crypt_hash_size(hash_name);
40
41         for(round = 0; key_size && !r; round++) {
42                 /* hack from hashalot to avoid null bytes in key */
43                 for(i = 0; i < round; i++)
44                         if (crypt_hash_write(md, "A", 1))
45                                 r = 1;
46
47                 if (crypt_hash_write(md, passphrase, passphrase_size))
48                         r = 1;
49
50                 if (len > key_size)
51                         len = key_size;
52
53                 if (crypt_hash_final(md, key, len))
54                         r = 1;
55
56                 key += len;
57                 key_size -= len;
58         }
59
60         crypt_hash_destroy(md);
61         return r;
62 }
63
64 #define PLAIN_HASH_LEN_MAX 256
65
66 int crypt_plain_hash(struct crypt_device *ctx __attribute__((unused)),
67                      const char *hash_name,
68                      char *key, size_t key_size,
69                      const char *passphrase, size_t passphrase_size)
70 {
71         char hash_name_buf[PLAIN_HASH_LEN_MAX], *s;
72         size_t hash_size, pad_size;
73         int r;
74
75         log_dbg("Plain: hashing passphrase using %s.", hash_name);
76
77         if (strlen(hash_name) >= PLAIN_HASH_LEN_MAX)
78                 return -EINVAL;
79         strncpy(hash_name_buf, hash_name, PLAIN_HASH_LEN_MAX);
80         hash_name_buf[PLAIN_HASH_LEN_MAX - 1] = '\0';
81
82         /* hash[:hash_length] */
83         if ((s = strchr(hash_name_buf, ':'))) {
84                 *s = '\0';
85                 hash_size = atoi(++s);
86                 if (hash_size > key_size) {
87                         log_dbg("Hash length %zd > key length %zd",
88                                 hash_size, key_size);
89                         return -EINVAL;
90                 }
91                 pad_size = key_size - hash_size;
92         } else {
93                 hash_size = key_size;
94                 pad_size = 0;
95         }
96
97         r = hash(hash_name_buf, hash_size, key, passphrase_size, passphrase);
98
99         if (r == 0 && pad_size)
100                 memset(key + hash_size, 0, pad_size);
101
102         return r;
103 }