Do not ignore errors in crypto backend (LUKS AF).
[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 volume_key **vk,
71                      const char **input_keys,
72                      unsigned int keys_count,
73                      unsigned int key_len_output)
74 {
75         const char *hash_name;
76         char tweak, *key_ptr;
77         int r, i, key_len_input;
78
79         hash_name = get_hash(key_len_output);
80         tweak = get_tweak(keys_count);
81         key_len_input = strlen(input_keys[0]);
82
83         if (!keys_count || !key_len_output || !hash_name || !key_len_input)
84                 return -EINVAL;
85
86         *vk = crypt_alloc_volume_key(key_len_output * keys_count, NULL);
87         if (!*vk)
88                 return -ENOMEM;
89
90         for (i = 0; i < keys_count; i++) {
91                 key_ptr = &(*vk)->key[i * key_len_output];
92                 r = hash_key(input_keys[i], key_len_input, key_ptr,
93                              key_len_output, hash_name);
94                 if (r < 0)
95                         break;
96
97                 key_ptr[0] ^= tweak;
98         }
99
100         if (r < 0 && *vk) {
101                 crypt_free_volume_key(*vk);
102                 *vk = NULL;
103         }
104         return r;
105 }
106
107 static int keyfile_is_gpg(char *buffer, unsigned int buffer_len)
108 {
109         int r = 0;
110         int index = buffer_len < 100 ? buffer_len - 1 : 100;
111         char eos = buffer[index];
112
113         buffer[index] = '\0';
114         if (strstr(buffer, "BEGIN PGP MESSAGE"))
115                 r = 1;
116         buffer[index] = eos;
117         return r;
118 }
119
120 int LOOPAES_parse_keyfile(struct crypt_device *cd,
121                           struct volume_key **vk,
122                           unsigned int *keys_count,
123                           char *buffer,
124                           unsigned int buffer_len)
125
126 {
127         const char *keys[LOOPAES_KEYS_MAX];
128         int i, key_index, key_len, offset;
129
130         log_dbg("Parsing loop-AES keyfile of size %d.", buffer_len);
131
132         if (buffer_len < LOOPAES_KEYFILE_MINSIZE) {
133                 log_err(cd, _("Incompatible loop-AES keyfile detected.\n"));
134                 return -EINVAL;
135         }
136
137         if (keyfile_is_gpg(buffer, buffer_len)) {
138                 log_err(cd, "Detected not yet supported GPG encrypted keyfile.\n");
139                 log_std(cd, "Please use gpg --decrypt <KEYFILE> | cryptsetup --keyfile=- ...\n");
140                 return -EINVAL;
141         }
142
143         /* Remove EOL in buffer */
144         for (i = 0; i < buffer_len; i++)
145                 if (buffer[i] == '\n' || buffer[i] == '\r')
146                         buffer[i] = '\0';
147
148         offset = 0;
149         key_index = 0;
150         while (offset < buffer_len && key_index < LOOPAES_KEYS_MAX) {
151                 keys[key_index++] = &buffer[offset];
152                 while (offset < buffer_len && buffer[offset])
153                         offset++;
154                 while (offset < buffer_len && !buffer[offset])
155                         offset++;
156         }
157
158         /* All keys must be the same length */
159         key_len = key_index ? strlen(keys[0]) : 0;
160         for (i = 0; i < key_index; i++)
161                 if (key_len != strlen(keys[i])) {
162                         log_dbg("Unexpected length %d of key #%d (should be %d).",
163                                 strlen(keys[i]), i, key_len);
164                         key_len = 0;
165                         break;
166                 }
167
168         log_dbg("Keyfile: %d keys of length %d.", key_index, key_len);
169         if (offset != buffer_len || key_len == 0 ||
170            (key_index != 1 && key_index !=64 && key_index != 65)) {
171                 log_err(cd, _("Incompatible loop-AES keyfile detected.\n"));
172                 return -EINVAL;
173         }
174
175         *keys_count = key_index;
176         return hash_keys(vk, keys, key_index, crypt_get_volume_key_size(cd));
177 }
178
179 int LOOPAES_activate(struct crypt_device *cd,
180                      const char *name,
181                      const char *base_cipher,
182                      unsigned int keys_count,
183                      struct volume_key *vk,
184                      uint32_t flags)
185 {
186         uint64_t size, offset;
187         char *cipher;
188         const char *device;
189         int read_only, r;
190
191         size = 0;
192         offset = crypt_get_data_offset(cd);
193         device = crypt_get_device_name(cd);
194         read_only = flags & CRYPT_ACTIVATE_READONLY;
195
196         r = device_check_and_adjust(cd, device, 1, &size, &offset, &read_only);
197         if (r)
198                 return r;
199
200         if (keys_count == 1)
201                 r = asprintf(&cipher, "%s-%s", base_cipher, "cbc-plain");
202         else
203                 r = asprintf(&cipher, "%s:%d-%s", base_cipher, 64, "cbc-lmk");
204         if (r < 0)
205                 return -ENOMEM;
206
207         log_dbg("Trying to activate loop-AES device %s using cipher %s.", name, cipher);
208         r = dm_create_device(name, device,
209                              cipher, CRYPT_LOOPAES,
210                              crypt_get_uuid(cd),
211                              size, 0, offset, vk->keylength, vk->key,
212                              read_only, 0);
213
214         if (!r && keys_count != 1 && !(dm_flags() & DM_LMK_SUPPORTED)) {
215                 log_err(cd, _("Kernel doesn't support loop-AES compatible mapping.\n"));
216                 r = -ENOTSUP;
217         }
218
219         free(cipher);
220         return r;
221 }