Also support --hash option for loopaesOpen.
[platform/upstream/cryptsetup.git] / lib / loopaes / loopaes.c
1 /*
2  * loop-AES compatible volume handling
3  *
4  * Copyright (C) 2011, 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 <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "crypto_backend.h"
26 #include "loopaes.h"
27
28 static const char *get_hash(unsigned int key_size)
29 {
30         char *hash;
31
32         switch (key_size) {
33                 case 16: hash = "sha256"; break;
34                 case 24: hash = "sha384"; break;
35                 case 32: hash = "sha512"; break;
36                 default: hash = NULL;
37         }
38
39         return hash;
40 }
41
42 static unsigned char get_tweak(unsigned int keys_count)
43 {
44         switch (keys_count) {
45                 case 64: return 0x55;
46                 case 65: return 0xF4;
47                 default: break;
48         }
49         return 0x00;
50 }
51
52 static int hash_key(const char *src, size_t src_len,
53                     char *dst, size_t dst_len,
54                     const char *hash_name)
55 {
56         struct crypt_hash *hd = NULL;
57         int r;
58
59         if (crypt_hash_init(&hd, hash_name))
60                 return -EINVAL;
61
62         r = crypt_hash_write(hd, src, src_len);
63         if (!r)
64                 r = crypt_hash_final(hd, dst, dst_len);
65
66         crypt_hash_destroy(hd);
67         return r;
68 }
69
70 static int hash_keys(struct crypt_device *cd,
71                      struct volume_key **vk,
72                      const char *hash_override,
73                      const char **input_keys,
74                      unsigned int keys_count,
75                      unsigned int key_len_output)
76 {
77         const char *hash_name;
78         char tweak, *key_ptr;
79         int r, i, key_len_input;
80
81         hash_name = hash_override ?: get_hash(key_len_output);
82         tweak = get_tweak(keys_count);
83         key_len_input = strlen(input_keys[0]);
84
85         if (!keys_count || !key_len_output || !hash_name || !key_len_input) {
86                 log_err(cd, _("Key processing error (using hash %s).\n"),
87                         hash_name ?: "[none]");
88                 return -EINVAL;
89         }
90
91         *vk = crypt_alloc_volume_key(key_len_output * keys_count, NULL);
92         if (!*vk)
93                 return -ENOMEM;
94
95         for (i = 0; i < keys_count; i++) {
96                 key_ptr = &(*vk)->key[i * key_len_output];
97                 r = hash_key(input_keys[i], key_len_input, key_ptr,
98                              key_len_output, hash_name);
99                 if (r < 0)
100                         break;
101
102                 key_ptr[0] ^= tweak;
103         }
104
105         if (r < 0 && *vk) {
106                 crypt_free_volume_key(*vk);
107                 *vk = NULL;
108         }
109         return r;
110 }
111
112 static int keyfile_is_gpg(char *buffer, size_t buffer_len)
113 {
114         int r = 0;
115         int index = buffer_len < 100 ? buffer_len - 1 : 100;
116         char eos = buffer[index];
117
118         buffer[index] = '\0';
119         if (strstr(buffer, "BEGIN PGP MESSAGE"))
120                 r = 1;
121         buffer[index] = eos;
122         return r;
123 }
124
125 int LOOPAES_parse_keyfile(struct crypt_device *cd,
126                           struct volume_key **vk,
127                           const char *hash,
128                           unsigned int *keys_count,
129                           char *buffer,
130                           size_t buffer_len)
131 {
132         const char *keys[LOOPAES_KEYS_MAX];
133         int i, key_index, key_len, offset;
134
135         log_dbg("Parsing loop-AES keyfile of size %d.", buffer_len);
136
137         if (!buffer_len)
138                 return -EINVAL;
139
140         if (keyfile_is_gpg(buffer, buffer_len)) {
141                 log_err(cd, _("Detected not yet supported GPG encrypted keyfile.\n"));
142                 log_std(cd, _("Please use gpg --decrypt <KEYFILE> | cryptsetup --keyfile=- ...\n"));
143                 return -EINVAL;
144         }
145
146         /* Remove EOL in buffer */
147         for (i = 0; i < buffer_len; i++)
148                 if (buffer[i] == '\n' || buffer[i] == '\r')
149                         buffer[i] = '\0';
150
151         offset = 0;
152         key_index = 0;
153         while (offset < buffer_len && key_index < LOOPAES_KEYS_MAX) {
154                 keys[key_index++] = &buffer[offset];
155                 while (offset < buffer_len && buffer[offset])
156                         offset++;
157                 while (offset < buffer_len && !buffer[offset])
158                         offset++;
159         }
160
161         /* All keys must be the same length */
162         key_len = key_index ? strlen(keys[0]) : 0;
163         for (i = 0; i < key_index; i++)
164                 if (key_len != strlen(keys[i])) {
165                         log_dbg("Unexpected length %d of key #%d (should be %d).",
166                                 strlen(keys[i]), i, key_len);
167                         key_len = 0;
168                         break;
169                 }
170
171         log_dbg("Keyfile: %d keys of length %d.", key_index, key_len);
172         if (offset != buffer_len || key_len == 0 ||
173            (key_index != 1 && key_index !=64 && key_index != 65)) {
174                 log_err(cd, _("Incompatible loop-AES keyfile detected.\n"));
175                 return -EINVAL;
176         }
177
178         *keys_count = key_index;
179         return hash_keys(cd, vk, hash, keys, key_index, crypt_get_volume_key_size(cd));
180 }
181
182 int LOOPAES_activate(struct crypt_device *cd,
183                      const char *name,
184                      const char *base_cipher,
185                      unsigned int keys_count,
186                      struct volume_key *vk,
187                      uint64_t offset,
188                      uint64_t skip,
189                      uint32_t flags)
190 {
191         uint64_t size;
192         uint32_t req_flags;
193         char *cipher;
194         const char *device;
195         int read_only, r;
196
197         size = 0;
198         /* Initial IV (skip) is always the same as offset */
199         device = crypt_get_device_name(cd);
200         read_only = flags & CRYPT_ACTIVATE_READONLY;
201
202         r = device_check_and_adjust(cd, device, 1, &size, &offset, &read_only);
203         if (r)
204                 return r;
205
206         if (keys_count == 1) {
207                 req_flags = DM_PLAIN64_SUPPORTED;
208                 r = asprintf(&cipher, "%s-%s", base_cipher, "cbc-plain64");
209         } else {
210                 req_flags = DM_LMK_SUPPORTED;
211                 r = asprintf(&cipher, "%s:%d-%s", base_cipher, 64, "cbc-lmk");
212         }
213         if (r < 0)
214                 return -ENOMEM;
215
216         log_dbg("Trying to activate loop-AES device %s using cipher %s.", name, cipher);
217         r = dm_create_device(name, device,
218                              cipher, CRYPT_LOOPAES,
219                              crypt_get_uuid(cd),
220                              size, skip, offset, vk->keylength, vk->key,
221                              read_only, 0);
222
223         if (!r && !(dm_flags() & req_flags)) {
224                 log_err(cd, _("Kernel doesn't support loop-AES compatible mapping.\n"));
225                 r = -ENOTSUP;
226         }
227
228         free(cipher);
229         return r;
230 }