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