Imported Upstream version 2.6.1
[platform/upstream/cryptsetup.git] / lib / crypto_backend / crypto_openssl.c
1 /*
2  * OPENSSL 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  * In addition, as a special exception, the copyright holders give
22  * permission to link the code of portions of this program with the
23  * OpenSSL library under certain conditions as described in each
24  * individual source file, and distribute linked combinations
25  * including the two.
26  *
27  * You must obey the GNU Lesser General Public License in all respects
28  * for all of the code used other than OpenSSL.
29  */
30
31 #include <string.h>
32 #include <errno.h>
33 #include <limits.h>
34 #include <openssl/crypto.h>
35 #include <openssl/evp.h>
36 #include <openssl/hmac.h>
37 #include <openssl/rand.h>
38 #include "crypto_backend_internal.h"
39 #if OPENSSL_VERSION_MAJOR >= 3
40 #include <openssl/provider.h>
41 #include <openssl/kdf.h>
42 #include <openssl/core_names.h>
43 static OSSL_PROVIDER *ossl_legacy = NULL;
44 static OSSL_PROVIDER *ossl_default = NULL;
45 static OSSL_LIB_CTX  *ossl_ctx = NULL;
46 static char backend_version[256] = "OpenSSL";
47 #endif
48
49 #define CONST_CAST(x) (x)(uintptr_t)
50
51 static int crypto_backend_initialised = 0;
52
53 struct crypt_hash {
54         EVP_MD_CTX *md;
55         const EVP_MD *hash_id;
56         int hash_len;
57 };
58
59 struct crypt_hmac {
60 #if OPENSSL_VERSION_MAJOR >= 3
61         EVP_MAC *mac;
62         EVP_MAC_CTX *md;
63         EVP_MAC_CTX *md_org;
64 #else
65         HMAC_CTX *md;
66         const EVP_MD *hash_id;
67 #endif
68         int hash_len;
69 };
70
71 struct crypt_cipher {
72         bool use_kernel;
73         union {
74         struct crypt_cipher_kernel kernel;
75         struct {
76                 EVP_CIPHER_CTX *hd_enc;
77                 EVP_CIPHER_CTX *hd_dec;
78                 const EVP_CIPHER *cipher_type;
79                 size_t iv_length;
80         } lib;
81         } u;
82 };
83
84 struct hash_alg {
85         const char *name;
86         const char *openssl_name;
87 };
88
89 /*
90  * Compatible wrappers for OpenSSL < 1.1.0 and LibreSSL < 2.7.0
91  */
92 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
93     (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
94
95 static int openssl_backend_init(bool fips __attribute__((unused)))
96 {
97         OpenSSL_add_all_algorithms();
98         return 0;
99 }
100
101 static void openssl_backend_exit(void)
102 {
103 }
104
105 static const char *openssl_backend_version(void)
106 {
107         return SSLeay_version(SSLEAY_VERSION);
108 }
109
110 static EVP_MD_CTX *EVP_MD_CTX_new(void)
111 {
112         EVP_MD_CTX *md = malloc(sizeof(*md));
113
114         if (md)
115                 EVP_MD_CTX_init(md);
116
117         return md;
118 }
119
120 static void EVP_MD_CTX_free(EVP_MD_CTX *md)
121 {
122         EVP_MD_CTX_cleanup(md);
123         free(md);
124 }
125
126 static HMAC_CTX *HMAC_CTX_new(void)
127 {
128         HMAC_CTX *md = malloc(sizeof(*md));
129
130         if (md)
131                 HMAC_CTX_init(md);
132
133         return md;
134 }
135
136 static void HMAC_CTX_free(HMAC_CTX *md)
137 {
138         HMAC_CTX_cleanup(md);
139         free(md);
140 }
141 #else
142 static void openssl_backend_exit(void)
143 {
144 #if OPENSSL_VERSION_MAJOR >= 3
145         if (ossl_legacy)
146                 OSSL_PROVIDER_unload(ossl_legacy);
147         if (ossl_default)
148                 OSSL_PROVIDER_unload(ossl_default);
149         if (ossl_ctx)
150                 OSSL_LIB_CTX_free(ossl_ctx);
151
152         ossl_legacy = NULL;
153         ossl_default = NULL;
154         ossl_ctx = NULL;
155 #endif
156 }
157
158 static int openssl_backend_init(bool fips)
159 {
160 /*
161  * OpenSSL >= 3.0.0 provides some algorithms in legacy provider
162  */
163 #if OPENSSL_VERSION_MAJOR >= 3
164         int r;
165
166         /*
167          * In FIPS mode we keep default OpenSSL context & global config
168          */
169         if (!fips) {
170                 ossl_ctx = OSSL_LIB_CTX_new();
171                 if (!ossl_ctx)
172                         return -EINVAL;
173
174                 ossl_default = OSSL_PROVIDER_try_load(ossl_ctx, "default", 0);
175                 if (!ossl_default) {
176                         OSSL_LIB_CTX_free(ossl_ctx);
177                         return -EINVAL;
178                 }
179
180                 /* Optional */
181                 ossl_legacy = OSSL_PROVIDER_try_load(ossl_ctx, "legacy", 0);
182         }
183
184         r = snprintf(backend_version, sizeof(backend_version), "%s %s%s%s",
185                 OpenSSL_version(OPENSSL_VERSION),
186                 ossl_default ? "[default]" : "",
187                 ossl_legacy  ? "[legacy]" : "",
188                 fips  ? "[fips]" : "");
189
190         if (r < 0 || (size_t)r >= sizeof(backend_version)) {
191                 openssl_backend_exit();
192                 return -EINVAL;
193         }
194 #endif
195         return 0;
196 }
197
198 static const char *openssl_backend_version(void)
199 {
200 #if OPENSSL_VERSION_MAJOR >= 3
201         return backend_version;
202 #else
203         return OpenSSL_version(OPENSSL_VERSION);
204 #endif
205 }
206 #endif
207
208 int crypt_backend_init(bool fips)
209 {
210         if (crypto_backend_initialised)
211                 return 0;
212
213         if (openssl_backend_init(fips))
214                 return -EINVAL;
215
216         crypto_backend_initialised = 1;
217         return 0;
218 }
219
220 void crypt_backend_destroy(void)
221 {
222         /*
223          * If Destructor was already called, we must not call it again
224          */
225         if (!crypto_backend_initialised)
226                 return;
227
228         crypto_backend_initialised = 0;
229
230         openssl_backend_exit();
231 }
232
233 uint32_t crypt_backend_flags(void)
234 {
235 #if OPENSSL_VERSION_MAJOR >= 3
236         return 0;
237 #else
238         return CRYPT_BACKEND_PBKDF2_INT;
239 #endif
240 }
241
242 const char *crypt_backend_version(void)
243 {
244         return openssl_backend_version();
245 }
246
247 static const char *crypt_hash_compat_name(const char *name)
248 {
249         const char *hash_name = name;
250         int i;
251         static struct hash_alg hash_algs[] = {
252         { "blake2b-512", "blake2b512" },
253         { "blake2s-256", "blake2s256" },
254         { NULL,          NULL,         }};
255
256         if (!name)
257                 return NULL;
258
259         i = 0;
260         while (hash_algs[i].name) {
261                 if (!strcasecmp(name, hash_algs[i].name)) {
262                         hash_name =  hash_algs[i].openssl_name;
263                         break;
264                 }
265                 i++;
266         }
267
268         return hash_name;
269 }
270
271 static const EVP_MD *hash_id_get(const char *name)
272 {
273 #if OPENSSL_VERSION_MAJOR >= 3
274         return EVP_MD_fetch(ossl_ctx, crypt_hash_compat_name(name), NULL);
275 #else
276         return EVP_get_digestbyname(crypt_hash_compat_name(name));
277 #endif
278 }
279
280 static void hash_id_free(const EVP_MD *hash_id)
281 {
282 #if OPENSSL_VERSION_MAJOR >= 3
283         EVP_MD_free(CONST_CAST(EVP_MD*)hash_id);
284 #endif
285 }
286
287 static const EVP_CIPHER *cipher_type_get(const char *name)
288 {
289 #if OPENSSL_VERSION_MAJOR >= 3
290         return EVP_CIPHER_fetch(ossl_ctx, name, NULL);
291 #else
292         return EVP_get_cipherbyname(name);
293 #endif
294 }
295
296 static void cipher_type_free(const EVP_CIPHER *cipher_type)
297 {
298 #if OPENSSL_VERSION_MAJOR >= 3
299         EVP_CIPHER_free(CONST_CAST(EVP_CIPHER*)cipher_type);
300 #endif
301 }
302
303 /* HASH */
304 int crypt_hash_size(const char *name)
305 {
306         int size;
307         const EVP_MD *hash_id;
308
309         hash_id = hash_id_get(name);
310         if (!hash_id)
311                 return -EINVAL;
312
313         size = EVP_MD_size(hash_id);
314         hash_id_free(hash_id);
315         return size;
316 }
317
318 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
319 {
320         struct crypt_hash *h;
321
322         h = malloc(sizeof(*h));
323         if (!h)
324                 return -ENOMEM;
325
326         h->md = EVP_MD_CTX_new();
327         if (!h->md) {
328                 free(h);
329                 return -ENOMEM;
330         }
331
332         h->hash_id = hash_id_get(name);
333         if (!h->hash_id) {
334                 EVP_MD_CTX_free(h->md);
335                 free(h);
336                 return -EINVAL;
337         }
338
339         if (EVP_DigestInit_ex(h->md, h->hash_id, NULL) != 1) {
340                 hash_id_free(h->hash_id);
341                 EVP_MD_CTX_free(h->md);
342                 free(h);
343                 return -EINVAL;
344         }
345
346         h->hash_len = EVP_MD_size(h->hash_id);
347         *ctx = h;
348         return 0;
349 }
350
351 static int crypt_hash_restart(struct crypt_hash *ctx)
352 {
353         if (EVP_DigestInit_ex(ctx->md, ctx->hash_id, NULL) != 1)
354                 return -EINVAL;
355
356         return 0;
357 }
358
359 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
360 {
361         if (EVP_DigestUpdate(ctx->md, buffer, length) != 1)
362                 return -EINVAL;
363
364         return 0;
365 }
366
367 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
368 {
369         unsigned char tmp[EVP_MAX_MD_SIZE];
370         unsigned int tmp_len = 0;
371
372         if (length > (size_t)ctx->hash_len)
373                 return -EINVAL;
374
375         if (EVP_DigestFinal_ex(ctx->md, tmp, &tmp_len) != 1)
376                 return -EINVAL;
377
378         memcpy(buffer, tmp, length);
379         crypt_backend_memzero(tmp, sizeof(tmp));
380
381         if (tmp_len < length)
382                 return -EINVAL;
383
384         if (crypt_hash_restart(ctx))
385                 return -EINVAL;
386
387         return 0;
388 }
389
390 void crypt_hash_destroy(struct crypt_hash *ctx)
391 {
392         hash_id_free(ctx->hash_id);
393         EVP_MD_CTX_free(ctx->md);
394         memset(ctx, 0, sizeof(*ctx));
395         free(ctx);
396 }
397
398 /* HMAC */
399 int crypt_hmac_size(const char *name)
400 {
401         return crypt_hash_size(name);
402 }
403
404 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
405                     const void *key, size_t key_length)
406 {
407         struct crypt_hmac *h;
408 #if OPENSSL_VERSION_MAJOR >= 3
409         OSSL_PARAM params[] = {
410                 OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, CONST_CAST(void*)name, 0),
411                 OSSL_PARAM_END
412         };
413
414         h = malloc(sizeof(*h));
415         if (!h)
416                 return -ENOMEM;
417
418         h->mac = EVP_MAC_fetch(ossl_ctx, OSSL_MAC_NAME_HMAC, NULL);
419         if (!h->mac) {
420                 free(h);
421                 return -EINVAL;
422         }
423
424         h->md = EVP_MAC_CTX_new(h->mac);
425         if (!h->md) {
426                 EVP_MAC_free(h->mac);
427                 free(h);
428                 return -ENOMEM;
429         }
430
431         if (EVP_MAC_init(h->md, key, key_length, params) != 1) {
432                 EVP_MAC_CTX_free(h->md);
433                 EVP_MAC_free(h->mac);
434                 free(h);
435                 return -EINVAL;
436         }
437
438         h->hash_len = EVP_MAC_CTX_get_mac_size(h->md);
439         h->md_org = EVP_MAC_CTX_dup(h->md);
440 #else
441         h = malloc(sizeof(*h));
442         if (!h)
443                 return -ENOMEM;
444
445         h->md = HMAC_CTX_new();
446         if (!h->md) {
447                 free(h);
448                 return -ENOMEM;
449         }
450
451         h->hash_id = hash_id_get(name);
452         if (!h->hash_id) {
453                 HMAC_CTX_free(h->md);
454                 free(h);
455                 return -EINVAL;
456         }
457
458         HMAC_Init_ex(h->md, key, key_length, h->hash_id, NULL);
459
460         h->hash_len = EVP_MD_size(h->hash_id);
461 #endif
462         *ctx = h;
463         return 0;
464 }
465
466 static int crypt_hmac_restart(struct crypt_hmac *ctx)
467 {
468 #if OPENSSL_VERSION_MAJOR >= 3
469         EVP_MAC_CTX_free(ctx->md);
470         ctx->md = EVP_MAC_CTX_dup(ctx->md_org);
471         if (!ctx->md)
472                 return -EINVAL;
473 #else
474         HMAC_Init_ex(ctx->md, NULL, 0, ctx->hash_id, NULL);
475 #endif
476         return 0;
477 }
478
479 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
480 {
481 #if OPENSSL_VERSION_MAJOR >= 3
482         return EVP_MAC_update(ctx->md, (const unsigned char *)buffer, length) == 1 ? 0 : -EINVAL;
483 #else
484         HMAC_Update(ctx->md, (const unsigned char *)buffer, length);
485         return 0;
486 #endif
487 }
488
489 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
490 {
491         unsigned char tmp[EVP_MAX_MD_SIZE];
492 #if OPENSSL_VERSION_MAJOR >= 3
493         size_t tmp_len = 0;
494
495         if (length > (size_t)ctx->hash_len)
496                 return -EINVAL;
497
498         if (EVP_MAC_final(ctx->md, tmp,  &tmp_len, sizeof(tmp)) != 1)
499                 return -EINVAL;
500 #else
501         unsigned int tmp_len = 0;
502
503         if (length > (size_t)ctx->hash_len)
504                 return -EINVAL;
505
506         HMAC_Final(ctx->md, tmp, &tmp_len);
507 #endif
508         memcpy(buffer, tmp, length);
509         crypt_backend_memzero(tmp, sizeof(tmp));
510
511         if (tmp_len < length)
512                 return -EINVAL;
513
514         if (crypt_hmac_restart(ctx))
515                 return -EINVAL;
516
517         return 0;
518 }
519
520 void crypt_hmac_destroy(struct crypt_hmac *ctx)
521 {
522 #if OPENSSL_VERSION_MAJOR >= 3
523         EVP_MAC_CTX_free(ctx->md);
524         EVP_MAC_CTX_free(ctx->md_org);
525         EVP_MAC_free(ctx->mac);
526 #else
527         hash_id_free(ctx->hash_id);
528         HMAC_CTX_free(ctx->md);
529 #endif
530         memset(ctx, 0, sizeof(*ctx));
531         free(ctx);
532 }
533
534 /* RNG */
535 int crypt_backend_rng(char *buffer, size_t length,
536         int quality __attribute__((unused)), int fips __attribute__((unused)))
537 {
538         if (RAND_bytes((unsigned char *)buffer, length) != 1)
539                 return -EINVAL;
540
541         return 0;
542 }
543
544 static int openssl_pbkdf2(const char *password, size_t password_length,
545         const char *salt, size_t salt_length, uint32_t iterations,
546         const char *hash, char *key, size_t key_length)
547 {
548         int r;
549 #if OPENSSL_VERSION_MAJOR >= 3
550         EVP_KDF_CTX *ctx;
551         EVP_KDF *pbkdf2;
552         OSSL_PARAM params[] = {
553                 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD,
554                         CONST_CAST(void*)password, password_length),
555                 OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT,
556                         CONST_CAST(void*)salt, salt_length),
557                 OSSL_PARAM_uint32(OSSL_KDF_PARAM_ITER, &iterations),
558                 OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST,
559                         CONST_CAST(void*)hash, 0),
560                 OSSL_PARAM_END
561         };
562
563         pbkdf2 = EVP_KDF_fetch(ossl_ctx, "pbkdf2", NULL);
564         if (!pbkdf2)
565                 return -EINVAL;
566
567         ctx = EVP_KDF_CTX_new(pbkdf2);
568         if (!ctx) {
569                 EVP_KDF_free(pbkdf2);
570                 return -EINVAL;
571         }
572
573         r = EVP_KDF_derive(ctx, (unsigned char*)key, key_length, params);
574
575         EVP_KDF_CTX_free(ctx);
576         EVP_KDF_free(pbkdf2);
577 #else
578         const EVP_MD *hash_id = EVP_get_digestbyname(crypt_hash_compat_name(hash));
579         if (!hash_id)
580                 return -EINVAL;
581
582         /* OpenSSL2 has iteration as signed int, avoid overflow */
583         if (iterations > INT_MAX)
584                 return -EINVAL;
585
586         r = PKCS5_PBKDF2_HMAC(password, (int)password_length, (const unsigned char *)salt,
587                 (int)salt_length, iterations, hash_id, (int)key_length, (unsigned char*) key);
588 #endif
589         return r == 1 ? 0 : -EINVAL;
590 }
591
592 static int openssl_argon2(const char *type, const char *password, size_t password_length,
593         const char *salt, size_t salt_length, char *key, size_t key_length,
594         uint32_t iterations, uint32_t memory, uint32_t parallel)
595 {
596         return argon2(type, password, password_length, salt, salt_length,
597                       key, key_length, iterations, memory, parallel);
598 }
599
600 /* PBKDF */
601 int crypt_pbkdf(const char *kdf, const char *hash,
602                 const char *password, size_t password_length,
603                 const char *salt, size_t salt_length,
604                 char *key, size_t key_length,
605                 uint32_t iterations, uint32_t memory, uint32_t parallel)
606 {
607         if (!kdf)
608                 return -EINVAL;
609
610         if (!strcmp(kdf, "pbkdf2"))
611                 return openssl_pbkdf2(password, password_length, salt, salt_length,
612                                       iterations, hash, key, key_length);
613         if (!strncmp(kdf, "argon2", 6))
614                 return openssl_argon2(kdf, password, password_length, salt, salt_length,
615                                       key, key_length, iterations, memory, parallel);
616         return -EINVAL;
617 }
618
619 /* Block ciphers */
620 static void _cipher_destroy(EVP_CIPHER_CTX **hd_enc, EVP_CIPHER_CTX **hd_dec, const EVP_CIPHER **cipher_type)
621 {
622         EVP_CIPHER_CTX_free(*hd_enc);
623         *hd_enc = NULL;
624
625         EVP_CIPHER_CTX_free(*hd_dec);
626         *hd_dec = NULL;
627
628         cipher_type_free(*cipher_type);
629         *cipher_type = NULL;
630 }
631
632 static int _cipher_init(EVP_CIPHER_CTX **hd_enc, EVP_CIPHER_CTX **hd_dec, const EVP_CIPHER **cipher_type, const char *name,
633                         const char *mode, const void *key, size_t key_length, size_t *iv_length)
634 {
635         char cipher_name[256];
636         const EVP_CIPHER *type;
637         int r, key_bits;
638
639         key_bits = key_length * 8;
640         if (!strcmp(mode, "xts"))
641                 key_bits /= 2;
642
643         r = snprintf(cipher_name, sizeof(cipher_name), "%s-%d-%s", name, key_bits, mode);
644         if (r < 0 || (size_t)r >= sizeof(cipher_name))
645                 return -EINVAL;
646
647         type = cipher_type_get(cipher_name);
648         if (!type)
649                 return -ENOENT;
650
651         if (EVP_CIPHER_key_length(type) != (int)key_length) {
652                 cipher_type_free(type);
653                 return -EINVAL;
654         }
655
656         *hd_enc = EVP_CIPHER_CTX_new();
657         *hd_dec = EVP_CIPHER_CTX_new();
658         *iv_length = EVP_CIPHER_iv_length(type);
659
660         if (!*hd_enc || !*hd_dec) {
661                 cipher_type_free(type);
662                 return -EINVAL;
663         }
664
665         if (EVP_EncryptInit_ex(*hd_enc, type, NULL, key, NULL) != 1 ||
666             EVP_DecryptInit_ex(*hd_dec, type, NULL, key, NULL) != 1) {
667                 _cipher_destroy(hd_enc, hd_dec, &type);
668                 return -EINVAL;
669         }
670
671         if (EVP_CIPHER_CTX_set_padding(*hd_enc, 0) != 1 ||
672             EVP_CIPHER_CTX_set_padding(*hd_dec, 0) != 1) {
673                 _cipher_destroy(hd_enc, hd_dec, &type);
674                 return -EINVAL;
675         }
676
677         *cipher_type = type;
678
679         return 0;
680 }
681
682 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
683                     const char *mode, const void *key, size_t key_length)
684 {
685         struct crypt_cipher *h;
686         int r;
687
688         h = malloc(sizeof(*h));
689         if (!h)
690                 return -ENOMEM;
691
692         if (!_cipher_init(&h->u.lib.hd_enc, &h->u.lib.hd_dec, &h->u.lib.cipher_type, name, mode, key,
693                           key_length, &h->u.lib.iv_length)) {
694                 h->use_kernel = false;
695                 *ctx = h;
696                 return 0;
697         }
698
699         r = crypt_cipher_init_kernel(&h->u.kernel, name, mode, key, key_length);
700         if (r < 0) {
701                 free(h);
702                 return r;
703         }
704
705         h->use_kernel = true;
706         *ctx = h;
707         return 0;
708 }
709
710 void crypt_cipher_destroy(struct crypt_cipher *ctx)
711 {
712         if (ctx->use_kernel)
713                 crypt_cipher_destroy_kernel(&ctx->u.kernel);
714         else
715                 _cipher_destroy(&ctx->u.lib.hd_enc, &ctx->u.lib.hd_dec, &ctx->u.lib.cipher_type);
716         free(ctx);
717 }
718
719 static int _cipher_encrypt(struct crypt_cipher *ctx, const unsigned char *in, unsigned char *out,
720                            int length, const unsigned char *iv, size_t iv_length)
721 {
722         int len;
723
724         if (ctx->u.lib.iv_length != iv_length)
725                 return -EINVAL;
726
727         if (EVP_EncryptInit_ex(ctx->u.lib.hd_enc, NULL, NULL, NULL, iv) != 1)
728                 return -EINVAL;
729
730         if (EVP_EncryptUpdate(ctx->u.lib.hd_enc, out, &len, in, length) != 1)
731                 return -EINVAL;
732
733         if (EVP_EncryptFinal(ctx->u.lib.hd_enc, out + len, &len) != 1)
734                 return -EINVAL;
735
736         return 0;
737 }
738
739 static int _cipher_decrypt(struct crypt_cipher *ctx, const unsigned char *in, unsigned char *out,
740                            int length, const unsigned char *iv, size_t iv_length)
741 {
742         int len;
743
744         if (ctx->u.lib.iv_length != iv_length)
745                 return -EINVAL;
746
747         if (EVP_DecryptInit_ex(ctx->u.lib.hd_dec, NULL, NULL, NULL, iv) != 1)
748                 return -EINVAL;
749
750         if (EVP_DecryptUpdate(ctx->u.lib.hd_dec, out, &len, in, length) != 1)
751                 return -EINVAL;
752
753         if (EVP_DecryptFinal(ctx->u.lib.hd_dec, out + len, &len) != 1)
754                 return -EINVAL;
755
756         return 0;
757 }
758
759 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
760                          const char *in, char *out, size_t length,
761                          const char *iv, size_t iv_length)
762 {
763         if (ctx->use_kernel)
764                 return crypt_cipher_encrypt_kernel(&ctx->u.kernel, in, out, length, iv, iv_length);
765
766         return _cipher_encrypt(ctx, (const unsigned char*)in,
767                                (unsigned char *)out, length, (const unsigned char*)iv, iv_length);
768 }
769
770 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
771                          const char *in, char *out, size_t length,
772                          const char *iv, size_t iv_length)
773 {
774         if (ctx->use_kernel)
775                 return crypt_cipher_decrypt_kernel(&ctx->u.kernel, in, out, length, iv, iv_length);
776
777         return _cipher_decrypt(ctx, (const unsigned char*)in,
778                                (unsigned char *)out, length, (const unsigned char*)iv, iv_length);
779 }
780
781 bool crypt_cipher_kernel_only(struct crypt_cipher *ctx)
782 {
783         return ctx->use_kernel;
784 }
785
786 int crypt_bitlk_decrypt_key(const void *key, size_t key_length __attribute__((unused)),
787                             const char *in, char *out, size_t length,
788                             const char *iv, size_t iv_length,
789                             const char *tag, size_t tag_length)
790 {
791 #ifdef EVP_CTRL_CCM_SET_IVLEN
792         EVP_CIPHER_CTX *ctx;
793         int len = 0, r = -EINVAL;
794
795         ctx = EVP_CIPHER_CTX_new();
796         if (!ctx)
797                 return -EINVAL;
798
799         if (EVP_DecryptInit_ex(ctx, EVP_aes_256_ccm(), NULL, NULL, NULL) != 1)
800                 goto out;
801
802         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, iv_length, NULL) != 1)
803                 goto out;
804         if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, tag_length, CONST_CAST(void*)tag) != 1)
805                 goto out;
806
807         if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, (const unsigned char*)iv) != 1)
808                 goto out;
809
810         if (EVP_DecryptUpdate(ctx, (unsigned char*)out, &len, (const unsigned char*)in, length) == 1)
811                 r = 0;
812 out:
813         EVP_CIPHER_CTX_free(ctx);
814         return r;
815 #else
816         return -ENOTSUP;
817 #endif
818 }
819
820 int crypt_backend_memeq(const void *m1, const void *m2, size_t n)
821 {
822         return CRYPTO_memcmp(m1, m2, n);
823 }
824
825 #if !ENABLE_FIPS
826 bool crypt_fips_mode(void) { return false; }
827 #else
828 static bool openssl_fips_mode(void)
829 {
830 #if OPENSSL_VERSION_MAJOR >= 3
831         return EVP_default_properties_is_fips_enabled(NULL);
832 #else
833         return FIPS_mode();
834 #endif
835 }
836
837 bool crypt_fips_mode(void)
838 {
839         static bool fips_mode = false, fips_checked = false;
840
841         if (fips_checked)
842                 return fips_mode;
843
844         fips_mode = openssl_fips_mode();
845         fips_checked = true;
846
847         return fips_mode;
848 }
849 #endif /* ENABLE FIPS */