b0452a336c329b1903fc7e93903d6952c2f62d2f
[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, 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 #define SECTOR_SIZE     (1 << SECTOR_SHIFT)
29
30 /*
31  * Internal IV helper
32  * IV documentation: https://gitlab.com/cryptsetup/cryptsetup/wikis/DMCrypt
33  */
34 struct crypt_sector_iv {
35         enum { IV_NONE, IV_NULL, IV_PLAIN, IV_PLAIN64, IV_ESSIV, IV_BENBI } type;
36         int iv_size;
37         char *iv;
38         struct crypt_cipher *essiv_cipher;
39         int benbi_shift;
40 };
41
42 /* Block encryption storage context */
43 struct crypt_storage {
44         uint64_t sector_start;
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, char *key, size_t key_length)
60 {
61         memset(ctx, 0, sizeof(*ctx));
62
63         ctx->iv_size = crypt_cipher_blocksize(cipher_name);
64         if (ctx->iv_size < 0)
65                 return -ENOENT;
66
67         if (!iv_name ||
68             !strcmp(cipher_name, "cipher_null") ||
69             !strcmp(mode_name, "ecb")) {
70                 ctx->type = IV_NONE;
71                 ctx->iv_size = 0;
72                 return 0;
73         } else if (!strcasecmp(iv_name, "null")) {
74                 ctx->type = IV_NULL;
75         } else if (!strcasecmp(iv_name, "plain64")) {
76                 ctx->type = IV_PLAIN64;
77         } else if (!strcasecmp(iv_name, "plain")) {
78                 ctx->type = IV_PLAIN;
79         } else if (!strncasecmp(iv_name, "essiv:", 6)) {
80                 struct crypt_hash *h = NULL;
81                 char *hash_name = strchr(iv_name, ':');
82                 int hash_size;
83                 char tmp[256];
84                 int r;
85
86                 if (!hash_name)
87                         return -EINVAL;
88
89                 hash_size = crypt_hash_size(++hash_name);
90                 if (hash_size < 0)
91                         return -ENOENT;
92
93                 if ((unsigned)hash_size > sizeof(tmp))
94                         return -EINVAL;
95
96                 if (crypt_hash_init(&h, hash_name))
97                         return -EINVAL;
98
99                 r = crypt_hash_write(h, key, key_length);
100                 if (r) {
101                         crypt_hash_destroy(h);
102                         return r;
103                 }
104
105                 r = crypt_hash_final(h, tmp, hash_size);
106                 crypt_hash_destroy(h);
107                 if (r) {
108                         crypt_backend_memzero(tmp, sizeof(tmp));
109                         return r;
110                 }
111
112                 r = crypt_cipher_init(&ctx->essiv_cipher, cipher_name, "ecb",
113                                       tmp, hash_size);
114                 crypt_backend_memzero(tmp, sizeof(tmp));
115                 if (r)
116                         return r;
117
118                 ctx->type = IV_ESSIV;
119         } else if (!strncasecmp(iv_name, "benbi", 5)) {
120                 int log = int_log2(ctx->iv_size);
121                 if (log > SECTOR_SHIFT)
122                         return -EINVAL;
123
124                 ctx->type = IV_BENBI;
125                 ctx->benbi_shift = SECTOR_SHIFT - log;
126         } else
127                 return -ENOENT;
128
129         ctx->iv = malloc(ctx->iv_size);
130         if (!ctx->iv)
131                 return -ENOMEM;
132
133         return 0;
134 }
135
136 static int crypt_sector_iv_generate(struct crypt_sector_iv *ctx, uint64_t sector)
137 {
138         uint64_t val;
139
140         switch (ctx->type) {
141         case IV_NONE:
142                 break;
143         case IV_NULL:
144                 memset(ctx->iv, 0, ctx->iv_size);
145                 break;
146         case IV_PLAIN:
147                 memset(ctx->iv, 0, ctx->iv_size);
148                 *(uint32_t *)ctx->iv = cpu_to_le32(sector & 0xffffffff);
149                 break;
150         case IV_PLAIN64:
151                 memset(ctx->iv, 0, ctx->iv_size);
152                 *(uint64_t *)ctx->iv = cpu_to_le64(sector);
153                 break;
154         case IV_ESSIV:
155                 memset(ctx->iv, 0, ctx->iv_size);
156                 *(uint64_t *)ctx->iv = cpu_to_le64(sector);
157                 return crypt_cipher_encrypt(ctx->essiv_cipher,
158                         ctx->iv, ctx->iv, ctx->iv_size, NULL, 0);
159                 break;
160         case IV_BENBI:
161                 memset(ctx->iv, 0, ctx->iv_size);
162                 val = cpu_to_be64((sector << ctx->benbi_shift) + 1);
163                 memcpy(ctx->iv + ctx->iv_size - sizeof(val), &val, sizeof(val));
164                 break;
165         default:
166                 return -EINVAL;
167         }
168
169         return 0;
170 }
171
172 static int crypt_sector_iv_destroy(struct crypt_sector_iv *ctx)
173 {
174         if (ctx->type == IV_ESSIV)
175                 crypt_cipher_destroy(ctx->essiv_cipher);
176
177         if (ctx->iv) {
178                 memset(ctx->iv, 0, ctx->iv_size);
179                 free(ctx->iv);
180         }
181
182         memset(ctx, 0, sizeof(*ctx));
183         return 0;
184 }
185
186 /* Block encryption storage wrappers */
187
188 int crypt_storage_init(struct crypt_storage **ctx,
189                        uint64_t sector_start,
190                        const char *cipher,
191                        const char *cipher_mode,
192                        char *key, size_t key_length)
193 {
194         struct crypt_storage *s;
195         char mode_name[64];
196         char *cipher_iv = NULL;
197         int r = -EIO;
198
199         s = malloc(sizeof(*s));
200         if (!s)
201                 return -ENOMEM;
202         memset(s, 0, sizeof(*s));
203
204         /* Remove IV if present */
205         strncpy(mode_name, cipher_mode, sizeof(mode_name));
206         mode_name[sizeof(mode_name) - 1] = 0;
207         cipher_iv = strchr(mode_name, '-');
208         if (cipher_iv) {
209                 *cipher_iv = '\0';
210                 cipher_iv++;
211         }
212
213         r = crypt_cipher_init(&s->cipher, cipher, mode_name, key, key_length);
214         if (r) {
215                 crypt_storage_destroy(s);
216                 return r;
217         }
218
219         r = crypt_sector_iv_init(&s->cipher_iv, cipher, mode_name, cipher_iv, key, key_length);
220         if (r) {
221                 crypt_storage_destroy(s);
222                 return r;
223         }
224
225         s->sector_start = sector_start;
226
227         *ctx = s;
228         return 0;
229 }
230
231 int crypt_storage_decrypt(struct crypt_storage *ctx,
232                        uint64_t sector, size_t count,
233                        char *buffer)
234 {
235         unsigned int i;
236         int r = 0;
237
238         for (i = 0; i < count; i++) {
239                 r = crypt_sector_iv_generate(&ctx->cipher_iv, sector + i);
240                 if (r)
241                         break;
242                 r = crypt_cipher_decrypt(ctx->cipher,
243                                          &buffer[i * SECTOR_SIZE],
244                                          &buffer[i * SECTOR_SIZE],
245                                          SECTOR_SIZE,
246                                          ctx->cipher_iv.iv,
247                                          ctx->cipher_iv.iv_size);
248                 if (r)
249                         break;
250         }
251
252         return r;
253 }
254
255 int crypt_storage_encrypt(struct crypt_storage *ctx,
256                        uint64_t sector, size_t count,
257                        char *buffer)
258 {
259         unsigned int i;
260         int r = 0;
261
262         for (i = 0; i < count; i++) {
263                 r = crypt_sector_iv_generate(&ctx->cipher_iv, sector + i);
264                 if (r)
265                         break;
266                 r = crypt_cipher_encrypt(ctx->cipher,
267                                          &buffer[i * SECTOR_SIZE],
268                                          &buffer[i * SECTOR_SIZE],
269                                          SECTOR_SIZE,
270                                          ctx->cipher_iv.iv,
271                                          ctx->cipher_iv.iv_size);
272                 if (r)
273                         break;
274         }
275
276         return r;
277 }
278
279 int crypt_storage_destroy(struct crypt_storage *ctx)
280 {
281         if (!ctx)
282                 return 0;
283
284         crypt_sector_iv_destroy(&ctx->cipher_iv);
285
286         if (ctx->cipher)
287                 crypt_cipher_destroy(ctx->cipher);
288
289         memset(ctx, 0, sizeof(*ctx));
290         free(ctx);
291
292         return 0;
293 }