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