846f17c441f1046824b3eedb528877adfb5e691a
[platform/upstream/cryptsetup.git] / lib / crypto_backend / crypto_storage.c
1 /*
2  * Generic wrapper for storage encryption modes and Initial Vectors
3  * (reimplementation of some functions from Linux dm-crypt kernel)
4  *
5  * Copyright (C) 2014-2020 Milan Broz
6  *
7  * This file is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This file 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 GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this file; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <stdlib.h>
23 #include <errno.h>
24 #include "bitops.h"
25 #include "crypto_backend.h"
26
27 #define SECTOR_SHIFT    9
28
29 /*
30  * Internal IV helper
31  * IV documentation: https://gitlab.com/cryptsetup/cryptsetup/wikis/DMCrypt
32  */
33 struct crypt_sector_iv {
34         enum { IV_NONE, IV_NULL, IV_PLAIN, IV_PLAIN64, IV_ESSIV, IV_BENBI, IV_PLAIN64BE, IV_EBOIV } type;
35         int iv_size;
36         char *iv;
37         struct crypt_cipher *cipher;
38         int shift;
39 };
40
41 /* Block encryption storage context */
42 struct crypt_storage {
43         unsigned sector_shift;
44         unsigned iv_shift;
45         struct crypt_cipher *cipher;
46         struct crypt_sector_iv cipher_iv;
47 };
48
49 static int int_log2(unsigned int x)
50 {
51         int r = 0;
52         for (x >>= 1; x > 0; x >>= 1)
53                 r++;
54         return r;
55 }
56
57 static int crypt_sector_iv_init(struct crypt_sector_iv *ctx,
58                          const char *cipher_name, const char *mode_name,
59                          const char *iv_name, const void *key, size_t key_length, size_t sector_size)
60 {
61         int r;
62
63         memset(ctx, 0, sizeof(*ctx));
64
65         ctx->iv_size = crypt_cipher_ivsize(cipher_name, mode_name);
66         if (ctx->iv_size < 8)
67                 return -ENOENT;
68
69         if (!strcmp(cipher_name, "cipher_null") ||
70             !strcmp(mode_name, "ecb")) {
71                 if (iv_name)
72                         return -EINVAL;
73                 ctx->type = IV_NONE;
74                 ctx->iv_size = 0;
75                 return 0;
76         } else if (!iv_name) {
77                 return -EINVAL;
78         } else if (!strcasecmp(iv_name, "null")) {
79                 ctx->type = IV_NULL;
80         } else if (!strcasecmp(iv_name, "plain64")) {
81                 ctx->type = IV_PLAIN64;
82         } else if (!strcasecmp(iv_name, "plain64be")) {
83                 ctx->type = IV_PLAIN64BE;
84         } else if (!strcasecmp(iv_name, "plain")) {
85                 ctx->type = IV_PLAIN;
86         } else if (!strncasecmp(iv_name, "essiv:", 6)) {
87                 struct crypt_hash *h = NULL;
88                 char *hash_name = strchr(iv_name, ':');
89                 int hash_size;
90                 char tmp[256];
91
92                 if (!hash_name)
93                         return -EINVAL;
94
95                 hash_size = crypt_hash_size(++hash_name);
96                 if (hash_size < 0)
97                         return -ENOENT;
98
99                 if ((unsigned)hash_size > sizeof(tmp))
100                         return -EINVAL;
101
102                 if (crypt_hash_init(&h, hash_name))
103                         return -EINVAL;
104
105                 r = crypt_hash_write(h, key, key_length);
106                 if (r) {
107                         crypt_hash_destroy(h);
108                         return r;
109                 }
110
111                 r = crypt_hash_final(h, tmp, hash_size);
112                 crypt_hash_destroy(h);
113                 if (r) {
114                         crypt_backend_memzero(tmp, sizeof(tmp));
115                         return r;
116                 }
117
118                 r = crypt_cipher_init(&ctx->cipher, cipher_name, "ecb",
119                                       tmp, hash_size);
120                 crypt_backend_memzero(tmp, sizeof(tmp));
121                 if (r)
122                         return r;
123
124                 ctx->type = IV_ESSIV;
125         } else if (!strncasecmp(iv_name, "benbi", 5)) {
126                 int log = int_log2(ctx->iv_size);
127                 if (log > SECTOR_SHIFT)
128                         return -EINVAL;
129
130                 ctx->type = IV_BENBI;
131                 ctx->shift = SECTOR_SHIFT - log;
132         } else if (!strncasecmp(iv_name, "eboiv", 5)) {
133                 r = crypt_cipher_init(&ctx->cipher, cipher_name, "ecb",
134                                       key, key_length);
135                 if (r)
136                         return r;
137
138                 ctx->type = IV_EBOIV;
139                 ctx->shift = int_log2(sector_size);
140         } else
141                 return -ENOENT;
142
143         ctx->iv = malloc(ctx->iv_size);
144         if (!ctx->iv)
145                 return -ENOMEM;
146
147         return 0;
148 }
149
150 static int crypt_sector_iv_generate(struct crypt_sector_iv *ctx, uint64_t sector)
151 {
152         uint64_t val;
153
154         switch (ctx->type) {
155         case IV_NONE:
156                 break;
157         case IV_NULL:
158                 memset(ctx->iv, 0, ctx->iv_size);
159                 break;
160         case IV_PLAIN:
161                 memset(ctx->iv, 0, ctx->iv_size);
162                 *(uint32_t *)ctx->iv = cpu_to_le32(sector & 0xffffffff);
163                 break;
164         case IV_PLAIN64:
165                 memset(ctx->iv, 0, ctx->iv_size);
166                 *(uint64_t *)ctx->iv = cpu_to_le64(sector);
167                 break;
168         case IV_PLAIN64BE:
169                 memset(ctx->iv, 0, ctx->iv_size);
170                 *(uint64_t *)&ctx->iv[ctx->iv_size - sizeof(uint64_t)] = cpu_to_be64(sector);
171                 break;
172         case IV_ESSIV:
173                 memset(ctx->iv, 0, ctx->iv_size);
174                 *(uint64_t *)ctx->iv = cpu_to_le64(sector);
175                 return crypt_cipher_encrypt(ctx->cipher,
176                         ctx->iv, ctx->iv, ctx->iv_size, NULL, 0);
177                 break;
178         case IV_BENBI:
179                 memset(ctx->iv, 0, ctx->iv_size);
180                 val = cpu_to_be64((sector << ctx->shift) + 1);
181                 memcpy(ctx->iv + ctx->iv_size - sizeof(val), &val, sizeof(val));
182                 break;
183         case IV_EBOIV:
184                 memset(ctx->iv, 0, ctx->iv_size);
185                 *(uint64_t *)ctx->iv = cpu_to_le64(sector << ctx->shift);
186                 return crypt_cipher_encrypt(ctx->cipher,
187                         ctx->iv, ctx->iv, ctx->iv_size, NULL, 0);
188                 break;
189         default:
190                 return -EINVAL;
191         }
192
193         return 0;
194 }
195
196 static void crypt_sector_iv_destroy(struct crypt_sector_iv *ctx)
197 {
198         if (ctx->type == IV_ESSIV || ctx->type == IV_EBOIV)
199                 crypt_cipher_destroy(ctx->cipher);
200
201         if (ctx->iv) {
202                 memset(ctx->iv, 0, ctx->iv_size);
203                 free(ctx->iv);
204         }
205
206         memset(ctx, 0, sizeof(*ctx));
207 }
208
209 /* Block encryption storage wrappers */
210
211 int crypt_storage_init(struct crypt_storage **ctx,
212                        size_t sector_size,
213                        const char *cipher,
214                        const char *cipher_mode,
215                        const void *key, size_t key_length)
216 {
217         struct crypt_storage *s;
218         char mode_name[64];
219         char *cipher_iv = NULL;
220         int r = -EIO;
221
222         if (sector_size < (1 << SECTOR_SHIFT) ||
223             sector_size > (1 << (SECTOR_SHIFT + 3)) ||
224             sector_size & (sector_size - 1))
225                 return -EINVAL;
226
227         s = malloc(sizeof(*s));
228         if (!s)
229                 return -ENOMEM;
230         memset(s, 0, sizeof(*s));
231
232         /* Remove IV if present */
233         strncpy(mode_name, cipher_mode, sizeof(mode_name));
234         mode_name[sizeof(mode_name) - 1] = 0;
235         cipher_iv = strchr(mode_name, '-');
236         if (cipher_iv) {
237                 *cipher_iv = '\0';
238                 cipher_iv++;
239         }
240
241         r = crypt_cipher_init(&s->cipher, cipher, mode_name, key, key_length);
242         if (r) {
243                 crypt_storage_destroy(s);
244                 return r;
245         }
246
247         r = crypt_sector_iv_init(&s->cipher_iv, cipher, mode_name, cipher_iv, key, key_length, sector_size);
248         if (r) {
249                 crypt_storage_destroy(s);
250                 return r;
251         }
252
253         s->sector_shift = int_log2(sector_size);
254         s->iv_shift = s->sector_shift - SECTOR_SHIFT;
255
256         *ctx = s;
257         return 0;
258 }
259
260 int crypt_storage_decrypt(struct crypt_storage *ctx,
261                        uint64_t iv_offset,
262                        uint64_t length, char *buffer)
263 {
264         uint64_t i;
265         int r = 0;
266
267         if (length & ((1 << ctx->sector_shift) - 1))
268                 return -EINVAL;
269
270         length >>= ctx->sector_shift;
271
272         for (i = 0; i < length; i++) {
273                 r = crypt_sector_iv_generate(&ctx->cipher_iv, iv_offset + (uint64_t)(i << ctx->iv_shift));
274                 if (r)
275                         break;
276                 r = crypt_cipher_decrypt(ctx->cipher,
277                                          &buffer[i << ctx->sector_shift],
278                                          &buffer[i << ctx->sector_shift],
279                                          1 << ctx->sector_shift,
280                                          ctx->cipher_iv.iv,
281                                          ctx->cipher_iv.iv_size);
282                 if (r)
283                         break;
284         }
285
286         return r;
287 }
288
289 int crypt_storage_encrypt(struct crypt_storage *ctx,
290                        uint64_t iv_offset,
291                        uint64_t length, char *buffer)
292 {
293         uint64_t i;
294         int r = 0;
295
296         if (length & ((1 << ctx->sector_shift) - 1))
297                 return -EINVAL;
298
299         length >>= ctx->sector_shift;
300
301         for (i = 0; i < length; i++) {
302                 r = crypt_sector_iv_generate(&ctx->cipher_iv, iv_offset + (i << ctx->iv_shift));
303                 if (r)
304                         break;
305                 r = crypt_cipher_encrypt(ctx->cipher,
306                                          &buffer[i << ctx->sector_shift],
307                                          &buffer[i << ctx->sector_shift],
308                                          1 << ctx->sector_shift,
309                                          ctx->cipher_iv.iv,
310                                          ctx->cipher_iv.iv_size);
311                 if (r)
312                         break;
313         }
314
315         return r;
316 }
317
318 void crypt_storage_destroy(struct crypt_storage *ctx)
319 {
320         if (!ctx)
321                 return;
322
323         crypt_sector_iv_destroy(&ctx->cipher_iv);
324
325         if (ctx->cipher)
326                 crypt_cipher_destroy(ctx->cipher);
327
328         memset(ctx, 0, sizeof(*ctx));
329         free(ctx);
330 }
331
332 bool crypt_storage_kernel_only(struct crypt_storage *ctx)
333 {
334         return crypt_cipher_kernel_only(ctx->cipher);
335 }