2 * OPENSSL crypto backend implementation
4 * Copyright (C) 2010-2023 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2010-2023 Milan Broz
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.
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.
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.
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
27 * You must obey the GNU Lesser General Public License in all respects
28 * for all of the code used other than OpenSSL.
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";
49 #define CONST_CAST(x) (x)(uintptr_t)
51 static int crypto_backend_initialised = 0;
55 const EVP_MD *hash_id;
60 #if OPENSSL_VERSION_MAJOR >= 3
66 const EVP_MD *hash_id;
74 struct crypt_cipher_kernel kernel;
76 EVP_CIPHER_CTX *hd_enc;
77 EVP_CIPHER_CTX *hd_dec;
78 const EVP_CIPHER *cipher_type;
86 const char *openssl_name;
90 * Compatible wrappers for OpenSSL < 1.1.0 and LibreSSL < 2.7.0
92 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
93 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
95 static int openssl_backend_init(bool fips __attribute__((unused)))
97 OpenSSL_add_all_algorithms();
101 static void openssl_backend_exit(void)
105 static const char *openssl_backend_version(void)
107 return SSLeay_version(SSLEAY_VERSION);
110 static EVP_MD_CTX *EVP_MD_CTX_new(void)
112 EVP_MD_CTX *md = malloc(sizeof(*md));
120 static void EVP_MD_CTX_free(EVP_MD_CTX *md)
122 EVP_MD_CTX_cleanup(md);
126 static HMAC_CTX *HMAC_CTX_new(void)
128 HMAC_CTX *md = malloc(sizeof(*md));
136 static void HMAC_CTX_free(HMAC_CTX *md)
138 HMAC_CTX_cleanup(md);
142 static void openssl_backend_exit(void)
144 #if OPENSSL_VERSION_MAJOR >= 3
146 OSSL_PROVIDER_unload(ossl_legacy);
148 OSSL_PROVIDER_unload(ossl_default);
150 OSSL_LIB_CTX_free(ossl_ctx);
158 static int openssl_backend_init(bool fips)
161 * OpenSSL >= 3.0.0 provides some algorithms in legacy provider
163 #if OPENSSL_VERSION_MAJOR >= 3
167 * In FIPS mode we keep default OpenSSL context & global config
170 ossl_ctx = OSSL_LIB_CTX_new();
174 ossl_default = OSSL_PROVIDER_try_load(ossl_ctx, "default", 0);
176 OSSL_LIB_CTX_free(ossl_ctx);
181 ossl_legacy = OSSL_PROVIDER_try_load(ossl_ctx, "legacy", 0);
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]" : "");
190 if (r < 0 || (size_t)r >= sizeof(backend_version)) {
191 openssl_backend_exit();
198 static const char *openssl_backend_version(void)
200 #if OPENSSL_VERSION_MAJOR >= 3
201 return backend_version;
203 return OpenSSL_version(OPENSSL_VERSION);
208 int crypt_backend_init(bool fips)
210 if (crypto_backend_initialised)
213 if (openssl_backend_init(fips))
216 crypto_backend_initialised = 1;
220 void crypt_backend_destroy(void)
223 * If Destructor was already called, we must not call it again
225 if (!crypto_backend_initialised)
228 crypto_backend_initialised = 0;
230 openssl_backend_exit();
233 uint32_t crypt_backend_flags(void)
235 #if OPENSSL_VERSION_MAJOR >= 3
238 return CRYPT_BACKEND_PBKDF2_INT;
242 const char *crypt_backend_version(void)
244 return openssl_backend_version();
247 static const char *crypt_hash_compat_name(const char *name)
249 const char *hash_name = name;
251 static struct hash_alg hash_algs[] = {
252 { "blake2b-512", "blake2b512" },
253 { "blake2s-256", "blake2s256" },
260 while (hash_algs[i].name) {
261 if (!strcasecmp(name, hash_algs[i].name)) {
262 hash_name = hash_algs[i].openssl_name;
271 static const EVP_MD *hash_id_get(const char *name)
273 #if OPENSSL_VERSION_MAJOR >= 3
274 return EVP_MD_fetch(ossl_ctx, crypt_hash_compat_name(name), NULL);
276 return EVP_get_digestbyname(crypt_hash_compat_name(name));
280 static void hash_id_free(const EVP_MD *hash_id)
282 #if OPENSSL_VERSION_MAJOR >= 3
283 EVP_MD_free(CONST_CAST(EVP_MD*)hash_id);
287 static const EVP_CIPHER *cipher_type_get(const char *name)
289 #if OPENSSL_VERSION_MAJOR >= 3
290 return EVP_CIPHER_fetch(ossl_ctx, name, NULL);
292 return EVP_get_cipherbyname(name);
296 static void cipher_type_free(const EVP_CIPHER *cipher_type)
298 #if OPENSSL_VERSION_MAJOR >= 3
299 EVP_CIPHER_free(CONST_CAST(EVP_CIPHER*)cipher_type);
304 int crypt_hash_size(const char *name)
307 const EVP_MD *hash_id;
309 hash_id = hash_id_get(name);
313 size = EVP_MD_size(hash_id);
314 hash_id_free(hash_id);
318 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
320 struct crypt_hash *h;
322 h = malloc(sizeof(*h));
326 h->md = EVP_MD_CTX_new();
332 h->hash_id = hash_id_get(name);
334 EVP_MD_CTX_free(h->md);
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);
346 h->hash_len = EVP_MD_size(h->hash_id);
351 static int crypt_hash_restart(struct crypt_hash *ctx)
353 if (EVP_DigestInit_ex(ctx->md, ctx->hash_id, NULL) != 1)
359 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
361 if (EVP_DigestUpdate(ctx->md, buffer, length) != 1)
367 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
369 unsigned char tmp[EVP_MAX_MD_SIZE];
370 unsigned int tmp_len = 0;
372 if (length > (size_t)ctx->hash_len)
375 if (EVP_DigestFinal_ex(ctx->md, tmp, &tmp_len) != 1)
378 memcpy(buffer, tmp, length);
379 crypt_backend_memzero(tmp, sizeof(tmp));
381 if (tmp_len < length)
384 if (crypt_hash_restart(ctx))
390 void crypt_hash_destroy(struct crypt_hash *ctx)
392 hash_id_free(ctx->hash_id);
393 EVP_MD_CTX_free(ctx->md);
394 memset(ctx, 0, sizeof(*ctx));
399 int crypt_hmac_size(const char *name)
401 return crypt_hash_size(name);
404 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
405 const void *key, size_t key_length)
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),
414 h = malloc(sizeof(*h));
418 h->mac = EVP_MAC_fetch(ossl_ctx, OSSL_MAC_NAME_HMAC, NULL);
424 h->md = EVP_MAC_CTX_new(h->mac);
426 EVP_MAC_free(h->mac);
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);
438 h->hash_len = EVP_MAC_CTX_get_mac_size(h->md);
439 h->md_org = EVP_MAC_CTX_dup(h->md);
441 h = malloc(sizeof(*h));
445 h->md = HMAC_CTX_new();
451 h->hash_id = hash_id_get(name);
453 HMAC_CTX_free(h->md);
458 HMAC_Init_ex(h->md, key, key_length, h->hash_id, NULL);
460 h->hash_len = EVP_MD_size(h->hash_id);
466 static int crypt_hmac_restart(struct crypt_hmac *ctx)
468 #if OPENSSL_VERSION_MAJOR >= 3
469 EVP_MAC_CTX_free(ctx->md);
470 ctx->md = EVP_MAC_CTX_dup(ctx->md_org);
474 HMAC_Init_ex(ctx->md, NULL, 0, ctx->hash_id, NULL);
479 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
481 #if OPENSSL_VERSION_MAJOR >= 3
482 return EVP_MAC_update(ctx->md, (const unsigned char *)buffer, length) == 1 ? 0 : -EINVAL;
484 HMAC_Update(ctx->md, (const unsigned char *)buffer, length);
489 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
491 unsigned char tmp[EVP_MAX_MD_SIZE];
492 #if OPENSSL_VERSION_MAJOR >= 3
495 if (length > (size_t)ctx->hash_len)
498 if (EVP_MAC_final(ctx->md, tmp, &tmp_len, sizeof(tmp)) != 1)
501 unsigned int tmp_len = 0;
503 if (length > (size_t)ctx->hash_len)
506 HMAC_Final(ctx->md, tmp, &tmp_len);
508 memcpy(buffer, tmp, length);
509 crypt_backend_memzero(tmp, sizeof(tmp));
511 if (tmp_len < length)
514 if (crypt_hmac_restart(ctx))
520 void crypt_hmac_destroy(struct crypt_hmac *ctx)
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);
527 hash_id_free(ctx->hash_id);
528 HMAC_CTX_free(ctx->md);
530 memset(ctx, 0, sizeof(*ctx));
535 int crypt_backend_rng(char *buffer, size_t length,
536 int quality __attribute__((unused)), int fips __attribute__((unused)))
538 if (RAND_bytes((unsigned char *)buffer, length) != 1)
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)
549 #if OPENSSL_VERSION_MAJOR >= 3
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),
563 pbkdf2 = EVP_KDF_fetch(ossl_ctx, "pbkdf2", NULL);
567 ctx = EVP_KDF_CTX_new(pbkdf2);
569 EVP_KDF_free(pbkdf2);
573 r = EVP_KDF_derive(ctx, (unsigned char*)key, key_length, params);
575 EVP_KDF_CTX_free(ctx);
576 EVP_KDF_free(pbkdf2);
578 const EVP_MD *hash_id = EVP_get_digestbyname(crypt_hash_compat_name(hash));
582 /* OpenSSL2 has iteration as signed int, avoid overflow */
583 if (iterations > INT_MAX)
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);
589 return r == 1 ? 0 : -EINVAL;
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)
596 return argon2(type, password, password_length, salt, salt_length,
597 key, key_length, iterations, memory, parallel);
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)
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);
620 static void _cipher_destroy(EVP_CIPHER_CTX **hd_enc, EVP_CIPHER_CTX **hd_dec, const EVP_CIPHER **cipher_type)
622 EVP_CIPHER_CTX_free(*hd_enc);
625 EVP_CIPHER_CTX_free(*hd_dec);
628 cipher_type_free(*cipher_type);
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)
635 char cipher_name[256];
636 const EVP_CIPHER *type;
639 key_bits = key_length * 8;
640 if (!strcmp(mode, "xts"))
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))
647 type = cipher_type_get(cipher_name);
651 if (EVP_CIPHER_key_length(type) != (int)key_length) {
652 cipher_type_free(type);
656 *hd_enc = EVP_CIPHER_CTX_new();
657 *hd_dec = EVP_CIPHER_CTX_new();
658 *iv_length = EVP_CIPHER_iv_length(type);
660 if (!*hd_enc || !*hd_dec) {
661 cipher_type_free(type);
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);
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);
682 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
683 const char *mode, const void *key, size_t key_length)
685 struct crypt_cipher *h;
688 h = malloc(sizeof(*h));
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;
699 r = crypt_cipher_init_kernel(&h->u.kernel, name, mode, key, key_length);
705 h->use_kernel = true;
710 void crypt_cipher_destroy(struct crypt_cipher *ctx)
713 crypt_cipher_destroy_kernel(&ctx->u.kernel);
715 _cipher_destroy(&ctx->u.lib.hd_enc, &ctx->u.lib.hd_dec, &ctx->u.lib.cipher_type);
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)
724 if (ctx->u.lib.iv_length != iv_length)
727 if (EVP_EncryptInit_ex(ctx->u.lib.hd_enc, NULL, NULL, NULL, iv) != 1)
730 if (EVP_EncryptUpdate(ctx->u.lib.hd_enc, out, &len, in, length) != 1)
733 if (EVP_EncryptFinal(ctx->u.lib.hd_enc, out + len, &len) != 1)
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)
744 if (ctx->u.lib.iv_length != iv_length)
747 if (EVP_DecryptInit_ex(ctx->u.lib.hd_dec, NULL, NULL, NULL, iv) != 1)
750 if (EVP_DecryptUpdate(ctx->u.lib.hd_dec, out, &len, in, length) != 1)
753 if (EVP_DecryptFinal(ctx->u.lib.hd_dec, out + len, &len) != 1)
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)
764 return crypt_cipher_encrypt_kernel(&ctx->u.kernel, in, out, length, iv, iv_length);
766 return _cipher_encrypt(ctx, (const unsigned char*)in,
767 (unsigned char *)out, length, (const unsigned char*)iv, iv_length);
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)
775 return crypt_cipher_decrypt_kernel(&ctx->u.kernel, in, out, length, iv, iv_length);
777 return _cipher_decrypt(ctx, (const unsigned char*)in,
778 (unsigned char *)out, length, (const unsigned char*)iv, iv_length);
781 bool crypt_cipher_kernel_only(struct crypt_cipher *ctx)
783 return ctx->use_kernel;
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)
791 #ifdef EVP_CTRL_CCM_SET_IVLEN
793 int len = 0, r = -EINVAL;
795 ctx = EVP_CIPHER_CTX_new();
799 if (EVP_DecryptInit_ex(ctx, EVP_aes_256_ccm(), NULL, NULL, NULL) != 1)
802 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, iv_length, NULL) != 1)
804 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, tag_length, CONST_CAST(void*)tag) != 1)
807 if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, (const unsigned char*)iv) != 1)
810 if (EVP_DecryptUpdate(ctx, (unsigned char*)out, &len, (const unsigned char*)in, length) == 1)
813 EVP_CIPHER_CTX_free(ctx);
820 int crypt_backend_memeq(const void *m1, const void *m2, size_t n)
822 return CRYPTO_memcmp(m1, m2, n);
826 bool crypt_fips_mode(void) { return false; }
828 static bool openssl_fips_mode(void)
830 #if OPENSSL_VERSION_MAJOR >= 3
831 return EVP_default_properties_is_fips_enabled(NULL);
837 bool crypt_fips_mode(void)
839 static bool fips_mode = false, fips_checked = false;
844 fips_mode = openssl_fips_mode();
849 #endif /* ENABLE FIPS */