f2cf3c687c514758c8894ed6625a6d70788d7d5f
[platform/upstream/cryptsetup.git] / lib / crypto_backend / crypto_gcrypt.c
1 /*
2  * GCRYPT crypto backend implementation
3  *
4  * Copyright (C) 2010-2020 Red Hat, Inc. All rights reserved.
5  * Copyright (C) 2010-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 <string.h>
23 #include <stdio.h>
24 #include <errno.h>
25 #include <assert.h>
26 #include <gcrypt.h>
27 #include "crypto_backend_internal.h"
28
29 static int crypto_backend_initialised = 0;
30 static int crypto_backend_secmem = 1;
31 static int crypto_backend_whirlpool_bug = -1;
32 static char version[64];
33
34 struct crypt_hash {
35         gcry_md_hd_t hd;
36         int hash_id;
37         int hash_len;
38 };
39
40 struct crypt_hmac {
41         gcry_md_hd_t hd;
42         int hash_id;
43         int hash_len;
44 };
45
46 struct crypt_cipher {
47         bool use_kernel;
48         union {
49         struct crypt_cipher_kernel kernel;
50         gcry_cipher_hd_t hd;
51         } u;
52 };
53
54 /*
55  * Test for wrong Whirlpool variant,
56  * Ref: http://lists.gnupg.org/pipermail/gcrypt-devel/2014-January/002889.html
57  */
58 static void crypt_hash_test_whirlpool_bug(void)
59 {
60         struct crypt_hash *h;
61         char buf[2] = "\0\0", hash_out1[64], hash_out2[64];
62         int r;
63
64         if (crypto_backend_whirlpool_bug >= 0)
65                 return;
66
67         crypto_backend_whirlpool_bug = 0;
68         if (crypt_hash_init(&h, "whirlpool"))
69                 return;
70
71         /* One shot */
72         if ((r = crypt_hash_write(h, &buf[0], 2)) ||
73             (r = crypt_hash_final(h, hash_out1, 64))) {
74                 crypt_hash_destroy(h);
75                 return;
76         }
77
78         /* Split buf (crypt_hash_final resets hash state) */
79         if ((r = crypt_hash_write(h, &buf[0], 1)) ||
80             (r = crypt_hash_write(h, &buf[1], 1)) ||
81             (r = crypt_hash_final(h, hash_out2, 64))) {
82                 crypt_hash_destroy(h);
83                 return;
84         }
85
86         crypt_hash_destroy(h);
87
88         if (memcmp(hash_out1, hash_out2, 64))
89                 crypto_backend_whirlpool_bug = 1;
90 }
91
92 int crypt_backend_init(void)
93 {
94         if (crypto_backend_initialised)
95                 return 0;
96
97         if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P)) {
98                 if (!gcry_check_version (GCRYPT_REQ_VERSION)) {
99                         return -ENOSYS;
100                 }
101
102 /* FIXME: If gcrypt compiled to support POSIX 1003.1e capabilities,
103  * it drops all privileges during secure memory initialisation.
104  * For now, the only workaround is to disable secure memory in gcrypt.
105  * cryptsetup always need at least cap_sys_admin privilege for dm-ioctl
106  * and it locks its memory space anyway.
107  */
108 #if 0
109                 gcry_control (GCRYCTL_DISABLE_SECMEM);
110                 crypto_backend_secmem = 0;
111 #else
112
113                 gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
114                 gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
115                 gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
116 #endif
117                 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
118         }
119
120         crypto_backend_initialised = 1;
121         crypt_hash_test_whirlpool_bug();
122
123         snprintf(version, 64, "gcrypt %s%s%s",
124                  gcry_check_version(NULL),
125                  crypto_backend_secmem ? "" : ", secmem disabled",
126                  crypto_backend_whirlpool_bug > 0 ? ", flawed whirlpool" : ""
127                 );
128
129         return 0;
130 }
131
132 void crypt_backend_destroy(void)
133 {
134         if (crypto_backend_initialised)
135                 gcry_control(GCRYCTL_TERM_SECMEM);
136
137         crypto_backend_initialised = 0;
138 }
139
140 const char *crypt_backend_version(void)
141 {
142         return crypto_backend_initialised ? version : "";
143 }
144
145 uint32_t crypt_backend_flags(void)
146 {
147         return 0;
148 }
149
150 static const char *crypt_hash_compat_name(const char *name, unsigned int *flags)
151 {
152         const char *hash_name = name;
153
154         /* "whirlpool_gcryptbug" is out shortcut to flawed whirlpool
155          * in libgcrypt < 1.6.0 */
156         if (name && !strcasecmp(name, "whirlpool_gcryptbug")) {
157 #if GCRYPT_VERSION_NUMBER >= 0x010601
158                 if (flags)
159                         *flags |= GCRY_MD_FLAG_BUGEMU1;
160 #endif
161                 hash_name = "whirlpool";
162         }
163
164         return hash_name;
165 }
166
167 /* HASH */
168 int crypt_hash_size(const char *name)
169 {
170         int hash_id;
171
172         assert(crypto_backend_initialised);
173
174         hash_id = gcry_md_map_name(crypt_hash_compat_name(name, NULL));
175         if (!hash_id)
176                 return -EINVAL;
177
178         return gcry_md_get_algo_dlen(hash_id);
179 }
180
181 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
182 {
183         struct crypt_hash *h;
184         unsigned int flags = 0;
185
186         assert(crypto_backend_initialised);
187
188         h = malloc(sizeof(*h));
189         if (!h)
190                 return -ENOMEM;
191
192         h->hash_id = gcry_md_map_name(crypt_hash_compat_name(name, &flags));
193         if (!h->hash_id) {
194                 free(h);
195                 return -EINVAL;
196         }
197
198         if (gcry_md_open(&h->hd, h->hash_id, flags)) {
199                 free(h);
200                 return -EINVAL;
201         }
202
203         h->hash_len = gcry_md_get_algo_dlen(h->hash_id);
204         *ctx = h;
205         return 0;
206 }
207
208 static void crypt_hash_restart(struct crypt_hash *ctx)
209 {
210         gcry_md_reset(ctx->hd);
211 }
212
213 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
214 {
215         gcry_md_write(ctx->hd, buffer, length);
216         return 0;
217 }
218
219 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
220 {
221         unsigned char *hash;
222
223         if (length > (size_t)ctx->hash_len)
224                 return -EINVAL;
225
226         hash = gcry_md_read(ctx->hd, ctx->hash_id);
227         if (!hash)
228                 return -EINVAL;
229
230         memcpy(buffer, hash, length);
231         crypt_hash_restart(ctx);
232
233         return 0;
234 }
235
236 void crypt_hash_destroy(struct crypt_hash *ctx)
237 {
238         gcry_md_close(ctx->hd);
239         memset(ctx, 0, sizeof(*ctx));
240         free(ctx);
241 }
242
243 /* HMAC */
244 int crypt_hmac_size(const char *name)
245 {
246         return crypt_hash_size(name);
247 }
248
249 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
250                     const void *key, size_t key_length)
251 {
252         struct crypt_hmac *h;
253         unsigned int flags = GCRY_MD_FLAG_HMAC;
254
255         assert(crypto_backend_initialised);
256
257         h = malloc(sizeof(*h));
258         if (!h)
259                 return -ENOMEM;
260
261         h->hash_id = gcry_md_map_name(crypt_hash_compat_name(name, &flags));
262         if (!h->hash_id) {
263                 free(h);
264                 return -EINVAL;
265         }
266
267         if (gcry_md_open(&h->hd, h->hash_id, flags)) {
268                 free(h);
269                 return -EINVAL;
270         }
271
272         if (gcry_md_setkey(h->hd, key, key_length)) {
273                 gcry_md_close(h->hd);
274                 free(h);
275                 return -EINVAL;
276         }
277
278         h->hash_len = gcry_md_get_algo_dlen(h->hash_id);
279         *ctx = h;
280         return 0;
281 }
282
283 static void crypt_hmac_restart(struct crypt_hmac *ctx)
284 {
285         gcry_md_reset(ctx->hd);
286 }
287
288 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
289 {
290         gcry_md_write(ctx->hd, buffer, length);
291         return 0;
292 }
293
294 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
295 {
296         unsigned char *hash;
297
298         if (length > (size_t)ctx->hash_len)
299                 return -EINVAL;
300
301         hash = gcry_md_read(ctx->hd, ctx->hash_id);
302         if (!hash)
303                 return -EINVAL;
304
305         memcpy(buffer, hash, length);
306         crypt_hmac_restart(ctx);
307
308         return 0;
309 }
310
311 void crypt_hmac_destroy(struct crypt_hmac *ctx)
312 {
313         gcry_md_close(ctx->hd);
314         memset(ctx, 0, sizeof(*ctx));
315         free(ctx);
316 }
317
318 /* RNG */
319 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
320 {
321         switch(quality) {
322         case CRYPT_RND_NORMAL:
323                 gcry_randomize(buffer, length, GCRY_STRONG_RANDOM);
324                 break;
325         case CRYPT_RND_SALT:
326         case CRYPT_RND_KEY:
327         default:
328                 gcry_randomize(buffer, length, GCRY_VERY_STRONG_RANDOM);
329                 break;
330         }
331         return 0;
332 }
333
334 static int pbkdf2(const char *hash,
335                   const char *password, size_t password_length,
336                   const char *salt, size_t salt_length,
337                   char *key, size_t key_length,
338                   uint32_t iterations)
339 {
340         const char *hash_name = crypt_hash_compat_name(hash, NULL);
341
342 #if USE_INTERNAL_PBKDF2
343         return pkcs5_pbkdf2(hash_name, password, password_length, salt, salt_length,
344                             iterations, key_length, key, 0);
345 #else /* USE_INTERNAL_PBKDF2 */
346         int hash_id = gcry_md_map_name(hash_name);
347
348         if (!hash_id)
349                 return -EINVAL;
350
351         if (gcry_kdf_derive(password, password_length, GCRY_KDF_PBKDF2, hash_id,
352             salt, salt_length, iterations, key_length, key))
353                 return -EINVAL;
354
355         return 0;
356 #endif /* USE_INTERNAL_PBKDF2 */
357 }
358
359 /* PBKDF */
360 int crypt_pbkdf(const char *kdf, const char *hash,
361                 const char *password, size_t password_length,
362                 const char *salt, size_t salt_length,
363                 char *key, size_t key_length,
364                 uint32_t iterations, uint32_t memory, uint32_t parallel)
365 {
366         if (!kdf)
367                 return -EINVAL;
368
369         if (!strcmp(kdf, "pbkdf2"))
370                 return pbkdf2(hash, password, password_length, salt, salt_length,
371                               key, key_length, iterations);
372         else if (!strncmp(kdf, "argon2", 6))
373                 return argon2(kdf, password, password_length, salt, salt_length,
374                               key, key_length, iterations, memory, parallel);
375         return -EINVAL;
376 }
377
378 /* Block ciphers */
379 static int _cipher_init(gcry_cipher_hd_t *hd, const char *name,
380                         const char *mode, const void *buffer, size_t length)
381 {
382         int cipher_id, mode_id;
383
384         cipher_id = gcry_cipher_map_name(name);
385         if (cipher_id == GCRY_CIPHER_MODE_NONE)
386                 return -ENOENT;
387
388         if (!strcmp(mode, "ecb"))
389                 mode_id = GCRY_CIPHER_MODE_ECB;
390         else if (!strcmp(mode, "cbc"))
391                 mode_id = GCRY_CIPHER_MODE_CBC;
392 #if HAVE_DECL_GCRY_CIPHER_MODE_XTS
393         else if (!strcmp(mode, "xts"))
394                 mode_id = GCRY_CIPHER_MODE_XTS;
395 #endif
396         else
397                 return -ENOENT;
398
399         if (gcry_cipher_open(hd, cipher_id, mode_id, 0))
400                 return -EINVAL;
401
402         if (gcry_cipher_setkey(*hd, buffer, length)) {
403                 gcry_cipher_close(*hd);
404                 return -EINVAL;
405         }
406
407         return 0;
408 }
409
410 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
411                     const char *mode, const void *key, size_t key_length)
412 {
413         struct crypt_cipher *h;
414         int r;
415
416         h = malloc(sizeof(*h));
417         if (!h)
418                 return -ENOMEM;
419
420         if (!_cipher_init(&h->u.hd, name, mode, key, key_length)) {
421                 h->use_kernel = false;
422                 *ctx = h;
423                 return 0;
424         }
425
426         r = crypt_cipher_init_kernel(&h->u.kernel, name, mode, key, key_length);
427         if (r < 0) {
428                 free(h);
429                 return r;
430         }
431
432         h->use_kernel = true;
433         *ctx = h;
434         return 0;
435 }
436
437 void crypt_cipher_destroy(struct crypt_cipher *ctx)
438 {
439         if (ctx->use_kernel)
440                 crypt_cipher_destroy_kernel(&ctx->u.kernel);
441         else
442                 gcry_cipher_close(ctx->u.hd);
443         free(ctx);
444 }
445
446 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
447                          const char *in, char *out, size_t length,
448                          const char *iv, size_t iv_length)
449 {
450         if (ctx->use_kernel)
451                 return crypt_cipher_encrypt_kernel(&ctx->u.kernel, in, out, length, iv, iv_length);
452
453         if (iv && gcry_cipher_setiv(ctx->u.hd, iv, iv_length))
454                 return -EINVAL;
455
456         if (gcry_cipher_encrypt(ctx->u.hd, out, length, in, length))
457                 return -EINVAL;
458
459         return 0;
460 }
461
462 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
463                          const char *in, char *out, size_t length,
464                          const char *iv, size_t iv_length)
465 {
466         if (ctx->use_kernel)
467                 return crypt_cipher_decrypt_kernel(&ctx->u.kernel, in, out, length, iv, iv_length);
468
469         if (iv && gcry_cipher_setiv(ctx->u.hd, iv, iv_length))
470                 return -EINVAL;
471
472         if (gcry_cipher_decrypt(ctx->u.hd, out, length, in, length))
473                 return -EINVAL;
474
475         return 0;
476 }
477
478 bool crypt_cipher_kernel_only(struct crypt_cipher *ctx)
479 {
480         return ctx->use_kernel;
481 }
482
483 int crypt_bitlk_decrypt_key(const void *key, size_t key_length,
484                             const char *in, char *out, size_t length,
485                             const char *iv, size_t iv_length,
486                             const char *tag, size_t tag_length)
487 {
488 #ifdef GCRY_CCM_BLOCK_LEN
489         gcry_cipher_hd_t hd;
490         uint64_t l[3];
491         int r = -EINVAL;
492
493         if (gcry_cipher_open(&hd, GCRY_CIPHER_AES256, GCRY_CIPHER_MODE_CCM, 0))
494                 return -EINVAL;
495
496         if (gcry_cipher_setkey(hd, key, key_length))
497                 goto out;
498
499         if (gcry_cipher_setiv(hd, iv, iv_length))
500                 goto out;
501
502         l[0] = length;
503         l[1] = 0;
504         l[2] = tag_length;
505         if (gcry_cipher_ctl(hd, GCRYCTL_SET_CCM_LENGTHS, l, sizeof(l)))
506                 goto out;
507
508         if (gcry_cipher_decrypt(hd, out, length, in, length))
509                 goto out;
510
511         if (gcry_cipher_checktag(hd, tag, tag_length))
512                 goto out;
513
514         r = 0;
515 out:
516         gcry_cipher_close(hd);
517         return r;
518 #else
519         return -ENOTSUP;
520 #endif
521 }