summaryrefslogtreecommitdiffstats
path: root/lib/crypto_backend/crypto_openssl.c
blob: 607ec3899c594c572bd82c4548bd90ce34d42b36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
/*
 * OPENSSL crypto backend implementation
 *
 * Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved.
 * Copyright (C) 2010-2023 Milan Broz
 *
 * This file is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this file; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 *
 * You must obey the GNU Lesser General Public License in all respects
 * for all of the code used other than OpenSSL.
 */

#include <string.h>
#include <errno.h>
#include <limits.h>
#include <openssl/crypto.h>
#include <openssl/evp.h>
#include <openssl/hmac.h>
#include <openssl/rand.h>
#include "crypto_backend_internal.h"
#if OPENSSL_VERSION_MAJOR >= 3
#include <openssl/provider.h>
#include <openssl/kdf.h>
#include <openssl/core_names.h>
static OSSL_PROVIDER *ossl_legacy = NULL;
static OSSL_PROVIDER *ossl_default = NULL;
static OSSL_LIB_CTX  *ossl_ctx = NULL;
static char backend_version[256] = "OpenSSL";
#endif

#define CONST_CAST(x) (x)(uintptr_t)

static int crypto_backend_initialised = 0;

struct crypt_hash {
	EVP_MD_CTX *md;
	const EVP_MD *hash_id;
	int hash_len;
};

struct crypt_hmac {
#if OPENSSL_VERSION_MAJOR >= 3
	EVP_MAC *mac;
	EVP_MAC_CTX *md;
	EVP_MAC_CTX *md_org;
#else
	HMAC_CTX *md;
	const EVP_MD *hash_id;
#endif
	int hash_len;
};

struct crypt_cipher {
	bool use_kernel;
	union {
	struct crypt_cipher_kernel kernel;
	struct {
		EVP_CIPHER_CTX *hd_enc;
		EVP_CIPHER_CTX *hd_dec;
		const EVP_CIPHER *cipher_type;
		size_t iv_length;
	} lib;
	} u;
};

struct hash_alg {
	const char *name;
	const char *openssl_name;
};

/*
 * Compatible wrappers for OpenSSL < 1.1.0 and LibreSSL < 2.7.0
 */
#if OPENSSL_VERSION_NUMBER < 0x10100000L || \
    (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)

static int openssl_backend_init(bool fips __attribute__((unused)))
{
	OpenSSL_add_all_algorithms();
	return 0;
}

static void openssl_backend_exit(void)
{
}

static const char *openssl_backend_version(void)
{
	return SSLeay_version(SSLEAY_VERSION);
}

static EVP_MD_CTX *EVP_MD_CTX_new(void)
{
	EVP_MD_CTX *md = malloc(sizeof(*md));

	if (md)
		EVP_MD_CTX_init(md);

	return md;
}

static void EVP_MD_CTX_free(EVP_MD_CTX *md)
{
	EVP_MD_CTX_cleanup(md);
	free(md);
}

static HMAC_CTX *HMAC_CTX_new(void)
{
	HMAC_CTX *md = malloc(sizeof(*md));

	if (md)
		HMAC_CTX_init(md);

	return md;
}

static void HMAC_CTX_free(HMAC_CTX *md)
{
	HMAC_CTX_cleanup(md);
	free(md);
}
#else
static void openssl_backend_exit(void)
{
#if OPENSSL_VERSION_MAJOR >= 3
	if (ossl_legacy)
		OSSL_PROVIDER_unload(ossl_legacy);
	if (ossl_default)
		OSSL_PROVIDER_unload(ossl_default);
	if (ossl_ctx)
		OSSL_LIB_CTX_free(ossl_ctx);

	ossl_legacy = NULL;
	ossl_default = NULL;
	ossl_ctx = NULL;
#endif
}

static int openssl_backend_init(bool fips)
{
/*
 * OpenSSL >= 3.0.0 provides some algorithms in legacy provider
 */
#if OPENSSL_VERSION_MAJOR >= 3
	int r;

	/*
	 * In FIPS mode we keep default OpenSSL context & global config
	 */
	if (!fips) {
		ossl_ctx = OSSL_LIB_CTX_new();
		if (!ossl_ctx)
			return -EINVAL;

		ossl_default = OSSL_PROVIDER_try_load(ossl_ctx, "default", 0);
		if (!ossl_default) {
			OSSL_LIB_CTX_free(ossl_ctx);
			return -EINVAL;
		}

		/* Optional */
		ossl_legacy = OSSL_PROVIDER_try_load(ossl_ctx, "legacy", 0);
	}

	r = snprintf(backend_version, sizeof(backend_version), "%s %s%s%s",
		OpenSSL_version(OPENSSL_VERSION),
		ossl_default ? "[default]" : "",
		ossl_legacy  ? "[legacy]" : "",
		fips  ? "[fips]" : "");

	if (r < 0 || (size_t)r >= sizeof(backend_version)) {
		openssl_backend_exit();
		return -EINVAL;
	}
#endif
	return 0;
}

static const char *openssl_backend_version(void)
{
#if OPENSSL_VERSION_MAJOR >= 3
	return backend_version;
#else
	return OpenSSL_version(OPENSSL_VERSION);
#endif
}
#endif

int crypt_backend_init(bool fips)
{
	if (crypto_backend_initialised)
		return 0;

	if (openssl_backend_init(fips))
		return -EINVAL;

	crypto_backend_initialised = 1;
	return 0;
}

void crypt_backend_destroy(void)
{
	/*
	 * If Destructor was already called, we must not call it again
	 */
	if (!crypto_backend_initialised)
		return;

	crypto_backend_initialised = 0;

	openssl_backend_exit();
}

uint32_t crypt_backend_flags(void)
{
#if OPENSSL_VERSION_MAJOR >= 3
	return 0;
#else
	return CRYPT_BACKEND_PBKDF2_INT;
#endif
}

const char *crypt_backend_version(void)
{
	return openssl_backend_version();
}

static const char *crypt_hash_compat_name(const char *name)
{
	const char *hash_name = name;
	int i;
	static struct hash_alg hash_algs[] = {
	{ "blake2b-512", "blake2b512" },
	{ "blake2s-256", "blake2s256" },
	{ NULL,          NULL,         }};

	if (!name)
		return NULL;

	i = 0;
	while (hash_algs[i].name) {
		if (!strcasecmp(name, hash_algs[i].name)) {
			hash_name =  hash_algs[i].openssl_name;
			break;
		}
		i++;
	}

	return hash_name;
}

static const EVP_MD *hash_id_get(const char *name)
{
#if OPENSSL_VERSION_MAJOR >= 3
	return EVP_MD_fetch(ossl_ctx, crypt_hash_compat_name(name), NULL);
#else
	return EVP_get_digestbyname(crypt_hash_compat_name(name));
#endif
}

static void hash_id_free(const EVP_MD *hash_id)
{
#if OPENSSL_VERSION_MAJOR >= 3
	EVP_MD_free(CONST_CAST(EVP_MD*)hash_id);
#endif
}

static const EVP_CIPHER *cipher_type_get(const char *name)
{
#if OPENSSL_VERSION_MAJOR >= 3
	return EVP_CIPHER_fetch(ossl_ctx, name, NULL);
#else
	return EVP_get_cipherbyname(name);
#endif
}

static void cipher_type_free(const EVP_CIPHER *cipher_type)
{
#if OPENSSL_VERSION_MAJOR >= 3
	EVP_CIPHER_free(CONST_CAST(EVP_CIPHER*)cipher_type);
#endif
}

/* HASH */
int crypt_hash_size(const char *name)
{
	int size;
	const EVP_MD *hash_id;

	hash_id = hash_id_get(name);
	if (!hash_id)
		return -EINVAL;

	size = EVP_MD_size(hash_id);
	hash_id_free(hash_id);
	return size;
}

int crypt_hash_init(struct crypt_hash **ctx, const char *name)
{
	struct crypt_hash *h;

	h = malloc(sizeof(*h));
	if (!h)
		return -ENOMEM;

	h->md = EVP_MD_CTX_new();
	if (!h->md) {
		free(h);
		return -ENOMEM;
	}

	h->hash_id = hash_id_get(name);
	if (!h->hash_id) {
		EVP_MD_CTX_free(h->md);
		free(h);
		return -EINVAL;
	}

	if (EVP_DigestInit_ex(h->md, h->hash_id, NULL) != 1) {
		hash_id_free(h->hash_id);
		EVP_MD_CTX_free(h->md);
		free(h);
		return -EINVAL;
	}

	h->hash_len = EVP_MD_size(h->hash_id);
	*ctx = h;
	return 0;
}

static int crypt_hash_restart(struct crypt_hash *ctx)
{
	if (EVP_DigestInit_ex(ctx->md, ctx->hash_id, NULL) != 1)
		return -EINVAL;

	return 0;
}

int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
{
	if (EVP_DigestUpdate(ctx->md, buffer, length) != 1)
		return -EINVAL;

	return 0;
}

int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
{
	unsigned char tmp[EVP_MAX_MD_SIZE];
	unsigned int tmp_len = 0;

	if (length > (size_t)ctx->hash_len)
		return -EINVAL;

	if (EVP_DigestFinal_ex(ctx->md, tmp, &tmp_len) != 1)
		return -EINVAL;

	memcpy(buffer, tmp, length);
	crypt_backend_memzero(tmp, sizeof(tmp));

	if (tmp_len < length)
		return -EINVAL;

	if (crypt_hash_restart(ctx))
		return -EINVAL;

	return 0;
}

void crypt_hash_destroy(struct crypt_hash *ctx)
{
	hash_id_free(ctx->hash_id);
	EVP_MD_CTX_free(ctx->md);
	memset(ctx, 0, sizeof(*ctx));
	free(ctx);
}

/* HMAC */
int crypt_hmac_size(const char *name)
{
	return crypt_hash_size(name);
}

int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
		    const void *key, size_t key_length)
{
	struct crypt_hmac *h;
#if OPENSSL_VERSION_MAJOR >= 3
	OSSL_PARAM params[] = {
		OSSL_PARAM_utf8_string(OSSL_MAC_PARAM_DIGEST, CONST_CAST(void*)name, 0),
		OSSL_PARAM_END
	};

	h = malloc(sizeof(*h));
	if (!h)
		return -ENOMEM;

	h->mac = EVP_MAC_fetch(ossl_ctx, OSSL_MAC_NAME_HMAC, NULL);
	if (!h->mac) {
		free(h);
		return -EINVAL;
	}

	h->md = EVP_MAC_CTX_new(h->mac);
	if (!h->md) {
		EVP_MAC_free(h->mac);
		free(h);
		return -ENOMEM;
	}

	if (EVP_MAC_init(h->md, key, key_length, params) != 1) {
		EVP_MAC_CTX_free(h->md);
		EVP_MAC_free(h->mac);
		free(h);
		return -EINVAL;
	}

	h->hash_len = EVP_MAC_CTX_get_mac_size(h->md);
	h->md_org = EVP_MAC_CTX_dup(h->md);
#else
	h = malloc(sizeof(*h));
	if (!h)
		return -ENOMEM;

	h->md = HMAC_CTX_new();
	if (!h->md) {
		free(h);
		return -ENOMEM;
	}

	h->hash_id = hash_id_get(name);
	if (!h->hash_id) {
		HMAC_CTX_free(h->md);
		free(h);
		return -EINVAL;
	}

	HMAC_Init_ex(h->md, key, key_length, h->hash_id, NULL);

	h->hash_len = EVP_MD_size(h->hash_id);
#endif
	*ctx = h;
	return 0;
}

static int crypt_hmac_restart(struct crypt_hmac *ctx)
{
#if OPENSSL_VERSION_MAJOR >= 3
	EVP_MAC_CTX_free(ctx->md);
	ctx->md = EVP_MAC_CTX_dup(ctx->md_org);
	if (!ctx->md)
		return -EINVAL;
#else
	HMAC_Init_ex(ctx->md, NULL, 0, ctx->hash_id, NULL);
#endif
	return 0;
}

int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
{
#if OPENSSL_VERSION_MAJOR >= 3
	return EVP_MAC_update(ctx->md, (const unsigned char *)buffer, length) == 1 ? 0 : -EINVAL;
#else
	HMAC_Update(ctx->md, (const unsigned char *)buffer, length);
	return 0;
#endif
}

int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
{
	unsigned char tmp[EVP_MAX_MD_SIZE];
#if OPENSSL_VERSION_MAJOR >= 3
	size_t tmp_len = 0;

	if (length > (size_t)ctx->hash_len)
		return -EINVAL;

	if (EVP_MAC_final(ctx->md, tmp,  &tmp_len, sizeof(tmp)) != 1)
		return -EINVAL;
#else
	unsigned int tmp_len = 0;

	if (length > (size_t)ctx->hash_len)
		return -EINVAL;

	HMAC_Final(ctx->md, tmp, &tmp_len);
#endif
	memcpy(buffer, tmp, length);
	crypt_backend_memzero(tmp, sizeof(tmp));

	if (tmp_len < length)
		return -EINVAL;

	if (crypt_hmac_restart(ctx))
		return -EINVAL;

	return 0;
}

void crypt_hmac_destroy(struct crypt_hmac *ctx)
{
#if OPENSSL_VERSION_MAJOR >= 3
	EVP_MAC_CTX_free(ctx->md);
	EVP_MAC_CTX_free(ctx->md_org);
	EVP_MAC_free(ctx->mac);
#else
	hash_id_free(ctx->hash_id);
	HMAC_CTX_free(ctx->md);
#endif
	memset(ctx, 0, sizeof(*ctx));
	free(ctx);
}

/* RNG */
int crypt_backend_rng(char *buffer, size_t length,
	int quality __attribute__((unused)), int fips __attribute__((unused)))
{
	if (RAND_bytes((unsigned char *)buffer, length) != 1)
		return -EINVAL;

	return 0;
}

static int openssl_pbkdf2(const char *password, size_t password_length,
	const char *salt, size_t salt_length, uint32_t iterations,
	const char *hash, char *key, size_t key_length)
{
	int r;
#if OPENSSL_VERSION_MAJOR >= 3
	EVP_KDF_CTX *ctx;
	EVP_KDF *pbkdf2;
	OSSL_PARAM params[] = {
		OSSL_PARAM_octet_string(OSSL_KDF_PARAM_PASSWORD,
			CONST_CAST(void*)password, password_length),
		OSSL_PARAM_octet_string(OSSL_KDF_PARAM_SALT,
			CONST_CAST(void*)salt, salt_length),
		OSSL_PARAM_uint32(OSSL_KDF_PARAM_ITER, &iterations),
		OSSL_PARAM_utf8_string(OSSL_KDF_PARAM_DIGEST,
			CONST_CAST(void*)hash, 0),
		OSSL_PARAM_END
	};

	pbkdf2 = EVP_KDF_fetch(ossl_ctx, "pbkdf2", NULL);
	if (!pbkdf2)
		return -EINVAL;

	ctx = EVP_KDF_CTX_new(pbkdf2);
	if (!ctx) {
		EVP_KDF_free(pbkdf2);
		return -EINVAL;
	}

	r = EVP_KDF_derive(ctx, (unsigned char*)key, key_length, params);

	EVP_KDF_CTX_free(ctx);
	EVP_KDF_free(pbkdf2);
#else
	const EVP_MD *hash_id = EVP_get_digestbyname(crypt_hash_compat_name(hash));
	if (!hash_id)
		return -EINVAL;

	/* OpenSSL2 has iteration as signed int, avoid overflow */
	if (iterations > INT_MAX)
		return -EINVAL;

	r = PKCS5_PBKDF2_HMAC(password, (int)password_length, (const unsigned char *)salt,
		(int)salt_length, iterations, hash_id, (int)key_length, (unsigned char*) key);
#endif
	return r == 1 ? 0 : -EINVAL;
}

static int openssl_argon2(const char *type, const char *password, size_t password_length,
	const char *salt, size_t salt_length, char *key, size_t key_length,
	uint32_t iterations, uint32_t memory, uint32_t parallel)
{
	return argon2(type, password, password_length, salt, salt_length,
		      key, key_length, iterations, memory, parallel);
}

/* PBKDF */
int crypt_pbkdf(const char *kdf, const char *hash,
		const char *password, size_t password_length,
		const char *salt, size_t salt_length,
		char *key, size_t key_length,
		uint32_t iterations, uint32_t memory, uint32_t parallel)
{
	if (!kdf)
		return -EINVAL;

	if (!strcmp(kdf, "pbkdf2"))
		return openssl_pbkdf2(password, password_length, salt, salt_length,
				      iterations, hash, key, key_length);
	if (!strncmp(kdf, "argon2", 6))
		return openssl_argon2(kdf, password, password_length, salt, salt_length,
				      key, key_length, iterations, memory, parallel);
	return -EINVAL;
}

/* Block ciphers */
static void _cipher_destroy(EVP_CIPHER_CTX **hd_enc, EVP_CIPHER_CTX **hd_dec, const EVP_CIPHER **cipher_type)
{
	EVP_CIPHER_CTX_free(*hd_enc);
	*hd_enc = NULL;

	EVP_CIPHER_CTX_free(*hd_dec);
	*hd_dec = NULL;

	cipher_type_free(*cipher_type);
	*cipher_type = NULL;
}

static int _cipher_init(EVP_CIPHER_CTX **hd_enc, EVP_CIPHER_CTX **hd_dec, const EVP_CIPHER **cipher_type, const char *name,
			const char *mode, const void *key, size_t key_length, size_t *iv_length)
{
	char cipher_name[256];
	const EVP_CIPHER *type;
	int r, key_bits;

	key_bits = key_length * 8;
	if (!strcmp(mode, "xts"))
		key_bits /= 2;

	r = snprintf(cipher_name, sizeof(cipher_name), "%s-%d-%s", name, key_bits, mode);
	if (r < 0 || (size_t)r >= sizeof(cipher_name))
		return -EINVAL;

	type = cipher_type_get(cipher_name);
	if (!type)
		return -ENOENT;

	if (EVP_CIPHER_key_length(type) != (int)key_length) {
		cipher_type_free(type);
		return -EINVAL;
	}

	*hd_enc = EVP_CIPHER_CTX_new();
	*hd_dec = EVP_CIPHER_CTX_new();
	*iv_length = EVP_CIPHER_iv_length(type);

	if (!*hd_enc || !*hd_dec) {
		cipher_type_free(type);
		return -EINVAL;
	}

	if (EVP_EncryptInit_ex(*hd_enc, type, NULL, key, NULL) != 1 ||
	    EVP_DecryptInit_ex(*hd_dec, type, NULL, key, NULL) != 1) {
		_cipher_destroy(hd_enc, hd_dec, &type);
		return -EINVAL;
	}

	if (EVP_CIPHER_CTX_set_padding(*hd_enc, 0) != 1 ||
	    EVP_CIPHER_CTX_set_padding(*hd_dec, 0) != 1) {
		_cipher_destroy(hd_enc, hd_dec, &type);
		return -EINVAL;
	}

	*cipher_type = type;

	return 0;
}

int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
		    const char *mode, const void *key, size_t key_length)
{
	struct crypt_cipher *h;
	int r;

	h = malloc(sizeof(*h));
	if (!h)
		return -ENOMEM;

	if (!_cipher_init(&h->u.lib.hd_enc, &h->u.lib.hd_dec, &h->u.lib.cipher_type, name, mode, key,
			  key_length, &h->u.lib.iv_length)) {
		h->use_kernel = false;
		*ctx = h;
		return 0;
	}

	r = crypt_cipher_init_kernel(&h->u.kernel, name, mode, key, key_length);
	if (r < 0) {
		free(h);
		return r;
	}

	h->use_kernel = true;
	*ctx = h;
	return 0;
}

void crypt_cipher_destroy(struct crypt_cipher *ctx)
{
	if (ctx->use_kernel)
		crypt_cipher_destroy_kernel(&ctx->u.kernel);
	else
		_cipher_destroy(&ctx->u.lib.hd_enc, &ctx->u.lib.hd_dec, &ctx->u.lib.cipher_type);
	free(ctx);
}

static int _cipher_encrypt(struct crypt_cipher *ctx, const unsigned char *in, unsigned char *out,
			   int length, const unsigned char *iv, size_t iv_length)
{
	int len;

	if (ctx->u.lib.iv_length != iv_length)
		return -EINVAL;

	if (EVP_EncryptInit_ex(ctx->u.lib.hd_enc, NULL, NULL, NULL, iv) != 1)
		return -EINVAL;

	if (EVP_EncryptUpdate(ctx->u.lib.hd_enc, out, &len, in, length) != 1)
		return -EINVAL;

	if (EVP_EncryptFinal(ctx->u.lib.hd_enc, out + len, &len) != 1)
		return -EINVAL;

	return 0;
}

static int _cipher_decrypt(struct crypt_cipher *ctx, const unsigned char *in, unsigned char *out,
			   int length, const unsigned char *iv, size_t iv_length)
{
	int len;

	if (ctx->u.lib.iv_length != iv_length)
		return -EINVAL;

	if (EVP_DecryptInit_ex(ctx->u.lib.hd_dec, NULL, NULL, NULL, iv) != 1)
		return -EINVAL;

	if (EVP_DecryptUpdate(ctx->u.lib.hd_dec, out, &len, in, length) != 1)
		return -EINVAL;

	if (EVP_DecryptFinal(ctx->u.lib.hd_dec, out + len, &len) != 1)
		return -EINVAL;

	return 0;
}

int crypt_cipher_encrypt(struct crypt_cipher *ctx,
			 const char *in, char *out, size_t length,
			 const char *iv, size_t iv_length)
{
	if (ctx->use_kernel)
		return crypt_cipher_encrypt_kernel(&ctx->u.kernel, in, out, length, iv, iv_length);

	return _cipher_encrypt(ctx, (const unsigned char*)in,
			       (unsigned char *)out, length, (const unsigned char*)iv, iv_length);
}

int crypt_cipher_decrypt(struct crypt_cipher *ctx,
			 const char *in, char *out, size_t length,
			 const char *iv, size_t iv_length)
{
	if (ctx->use_kernel)
		return crypt_cipher_decrypt_kernel(&ctx->u.kernel, in, out, length, iv, iv_length);

	return _cipher_decrypt(ctx, (const unsigned char*)in,
			       (unsigned char *)out, length, (const unsigned char*)iv, iv_length);
}

bool crypt_cipher_kernel_only(struct crypt_cipher *ctx)
{
	return ctx->use_kernel;
}

int crypt_bitlk_decrypt_key(const void *key, size_t key_length __attribute__((unused)),
			    const char *in, char *out, size_t length,
			    const char *iv, size_t iv_length,
			    const char *tag, size_t tag_length)
{
#ifdef EVP_CTRL_CCM_SET_IVLEN
	EVP_CIPHER_CTX *ctx;
	int len = 0, r = -EINVAL;

	ctx = EVP_CIPHER_CTX_new();
	if (!ctx)
		return -EINVAL;

	if (EVP_DecryptInit_ex(ctx, EVP_aes_256_ccm(), NULL, NULL, NULL) != 1)
		goto out;

	if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, iv_length, NULL) != 1)
		goto out;
	if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, tag_length, CONST_CAST(void*)tag) != 1)
		goto out;

	if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, (const unsigned char*)iv) != 1)
		goto out;

	if (EVP_DecryptUpdate(ctx, (unsigned char*)out, &len, (const unsigned char*)in, length) == 1)
		r = 0;
out:
	EVP_CIPHER_CTX_free(ctx);
	return r;
#else
	return -ENOTSUP;
#endif
}

int crypt_backend_memeq(const void *m1, const void *m2, size_t n)
{
	return CRYPTO_memcmp(m1, m2, n);
}

#if !ENABLE_FIPS
bool crypt_fips_mode(void) { return false; }
#else
static bool openssl_fips_mode(void)
{
#if OPENSSL_VERSION_MAJOR >= 3
	return EVP_default_properties_is_fips_enabled(NULL);
#else
	return FIPS_mode();
#endif
}

bool crypt_fips_mode(void)
{
	static bool fips_mode = false, fips_checked = false;

	if (fips_checked)
		return fips_mode;

	fips_mode = openssl_fips_mode();
	fips_checked = true;

	return fips_mode;
}
#endif /* ENABLE FIPS */