2 * OPENSSL crypto backend implementation
4 * Copyright (C) 2010-2021 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2010-2021 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.
33 #include <openssl/evp.h>
34 #include <openssl/hmac.h>
35 #include <openssl/rand.h>
36 #include "crypto_backend_internal.h"
38 #define CONST_CAST(x) (x)(uintptr_t)
40 static int crypto_backend_initialised = 0;
44 const EVP_MD *hash_id;
50 const EVP_MD *hash_id;
57 struct crypt_cipher_kernel kernel;
59 EVP_CIPHER_CTX *hd_enc;
60 EVP_CIPHER_CTX *hd_dec;
68 const char *openssl_name;
72 * Compatible wrappers for OpenSSL < 1.1.0 and LibreSSL < 2.7.0
74 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
75 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
77 static void openssl_backend_init(void)
79 OpenSSL_add_all_algorithms();
82 static const char *openssl_backend_version(void)
84 return SSLeay_version(SSLEAY_VERSION);
87 static EVP_MD_CTX *EVP_MD_CTX_new(void)
89 EVP_MD_CTX *md = malloc(sizeof(*md));
97 static void EVP_MD_CTX_free(EVP_MD_CTX *md)
99 EVP_MD_CTX_cleanup(md);
103 static HMAC_CTX *HMAC_CTX_new(void)
105 HMAC_CTX *md = malloc(sizeof(*md));
113 static void HMAC_CTX_free(HMAC_CTX *md)
115 HMAC_CTX_cleanup(md);
119 static void openssl_backend_init(void)
123 static const char *openssl_backend_version(void)
125 return OpenSSL_version(OPENSSL_VERSION);
129 int crypt_backend_init(void)
131 if (crypto_backend_initialised)
134 openssl_backend_init();
136 crypto_backend_initialised = 1;
140 void crypt_backend_destroy(void)
142 crypto_backend_initialised = 0;
145 uint32_t crypt_backend_flags(void)
150 const char *crypt_backend_version(void)
152 return openssl_backend_version();
155 static const char *crypt_hash_compat_name(const char *name)
157 const char *hash_name = name;
159 static struct hash_alg hash_algs[] = {
160 { "blake2b-512", "blake2b512" },
161 { "blake2s-256", "blake2s256" },
168 while (hash_algs[i].name) {
169 if (!strcasecmp(name, hash_algs[i].name)) {
170 hash_name = hash_algs[i].openssl_name;
180 int crypt_hash_size(const char *name)
182 const EVP_MD *hash_id;
184 hash_id = EVP_get_digestbyname(crypt_hash_compat_name(name));
188 return EVP_MD_size(hash_id);
191 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
193 struct crypt_hash *h;
195 h = malloc(sizeof(*h));
199 h->md = EVP_MD_CTX_new();
205 h->hash_id = EVP_get_digestbyname(crypt_hash_compat_name(name));
207 EVP_MD_CTX_free(h->md);
212 if (EVP_DigestInit_ex(h->md, h->hash_id, NULL) != 1) {
213 EVP_MD_CTX_free(h->md);
218 h->hash_len = EVP_MD_size(h->hash_id);
223 static int crypt_hash_restart(struct crypt_hash *ctx)
225 if (EVP_DigestInit_ex(ctx->md, ctx->hash_id, NULL) != 1)
231 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
233 if (EVP_DigestUpdate(ctx->md, buffer, length) != 1)
239 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
241 unsigned char tmp[EVP_MAX_MD_SIZE];
242 unsigned int tmp_len = 0;
244 if (length > (size_t)ctx->hash_len)
247 if (EVP_DigestFinal_ex(ctx->md, tmp, &tmp_len) != 1)
250 memcpy(buffer, tmp, length);
251 crypt_backend_memzero(tmp, sizeof(tmp));
253 if (tmp_len < length)
256 if (crypt_hash_restart(ctx))
262 void crypt_hash_destroy(struct crypt_hash *ctx)
264 EVP_MD_CTX_free(ctx->md);
265 memset(ctx, 0, sizeof(*ctx));
270 int crypt_hmac_size(const char *name)
272 return crypt_hash_size(name);
275 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
276 const void *key, size_t key_length)
278 struct crypt_hmac *h;
280 h = malloc(sizeof(*h));
284 h->md = HMAC_CTX_new();
290 h->hash_id = EVP_get_digestbyname(crypt_hash_compat_name(name));
292 HMAC_CTX_free(h->md);
297 HMAC_Init_ex(h->md, key, key_length, h->hash_id, NULL);
299 h->hash_len = EVP_MD_size(h->hash_id);
304 static void crypt_hmac_restart(struct crypt_hmac *ctx)
306 HMAC_Init_ex(ctx->md, NULL, 0, ctx->hash_id, NULL);
309 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
311 HMAC_Update(ctx->md, (const unsigned char *)buffer, length);
315 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
317 unsigned char tmp[EVP_MAX_MD_SIZE];
318 unsigned int tmp_len = 0;
320 if (length > (size_t)ctx->hash_len)
323 HMAC_Final(ctx->md, tmp, &tmp_len);
325 memcpy(buffer, tmp, length);
326 crypt_backend_memzero(tmp, sizeof(tmp));
328 if (tmp_len < length)
331 crypt_hmac_restart(ctx);
336 void crypt_hmac_destroy(struct crypt_hmac *ctx)
338 HMAC_CTX_free(ctx->md);
339 memset(ctx, 0, sizeof(*ctx));
344 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
346 if (RAND_bytes((unsigned char *)buffer, length) != 1)
353 int crypt_pbkdf(const char *kdf, const char *hash,
354 const char *password, size_t password_length,
355 const char *salt, size_t salt_length,
356 char *key, size_t key_length,
357 uint32_t iterations, uint32_t memory, uint32_t parallel)
360 const EVP_MD *hash_id;
365 if (!strcmp(kdf, "pbkdf2")) {
366 hash_id = EVP_get_digestbyname(crypt_hash_compat_name(hash));
370 if (!PKCS5_PBKDF2_HMAC(password, (int)password_length,
371 (const unsigned char *)salt, (int)salt_length,
372 (int)iterations, hash_id, (int)key_length, (unsigned char *)key))
375 } else if (!strncmp(kdf, "argon2", 6)) {
376 return argon2(kdf, password, password_length, salt, salt_length,
377 key, key_length, iterations, memory, parallel);
384 static void _cipher_destroy(EVP_CIPHER_CTX **hd_enc, EVP_CIPHER_CTX **hd_dec)
386 EVP_CIPHER_CTX_free(*hd_enc);
389 EVP_CIPHER_CTX_free(*hd_dec);
393 static int _cipher_init(EVP_CIPHER_CTX **hd_enc, EVP_CIPHER_CTX **hd_dec, const char *name,
394 const char *mode, const void *key, size_t key_length, size_t *iv_length)
396 char cipher_name[256];
397 const EVP_CIPHER *type;
400 key_bits = key_length * 8;
401 if (!strcmp(mode, "xts"))
404 r = snprintf(cipher_name, sizeof(cipher_name), "%s-%d-%s", name, key_bits, mode);
405 if (r < 0 || (size_t)r >= sizeof(cipher_name))
408 type = EVP_get_cipherbyname(cipher_name);
412 if (EVP_CIPHER_key_length(type) != (int)key_length)
415 *hd_enc = EVP_CIPHER_CTX_new();
416 *hd_dec = EVP_CIPHER_CTX_new();
417 *iv_length = EVP_CIPHER_iv_length(type);
419 if (!*hd_enc || !*hd_dec)
422 if (EVP_EncryptInit_ex(*hd_enc, type, NULL, key, NULL) != 1 ||
423 EVP_DecryptInit_ex(*hd_dec, type, NULL, key, NULL) != 1) {
424 _cipher_destroy(hd_enc, hd_dec);
428 if (EVP_CIPHER_CTX_set_padding(*hd_enc, 0) != 1 ||
429 EVP_CIPHER_CTX_set_padding(*hd_dec, 0) != 1) {
430 _cipher_destroy(hd_enc, hd_dec);
437 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
438 const char *mode, const void *key, size_t key_length)
440 struct crypt_cipher *h;
443 h = malloc(sizeof(*h));
447 if (!_cipher_init(&h->u.lib.hd_enc, &h->u.lib.hd_dec, name, mode, key,
448 key_length, &h->u.lib.iv_length)) {
449 h->use_kernel = false;
454 r = crypt_cipher_init_kernel(&h->u.kernel, name, mode, key, key_length);
460 h->use_kernel = true;
465 void crypt_cipher_destroy(struct crypt_cipher *ctx)
468 crypt_cipher_destroy_kernel(&ctx->u.kernel);
470 _cipher_destroy(&ctx->u.lib.hd_enc, &ctx->u.lib.hd_dec);
474 static int _cipher_encrypt(struct crypt_cipher *ctx, const unsigned char *in, unsigned char *out,
475 int length, const unsigned char *iv, size_t iv_length)
479 if (ctx->u.lib.iv_length != iv_length)
482 if (EVP_EncryptInit_ex(ctx->u.lib.hd_enc, NULL, NULL, NULL, iv) != 1)
485 if (EVP_EncryptUpdate(ctx->u.lib.hd_enc, out, &len, in, length) != 1)
488 if (EVP_EncryptFinal(ctx->u.lib.hd_enc, out + len, &len) != 1)
494 static int _cipher_decrypt(struct crypt_cipher *ctx, const unsigned char *in, unsigned char *out,
495 int length, const unsigned char *iv, size_t iv_length)
499 if (ctx->u.lib.iv_length != iv_length)
502 if (EVP_DecryptInit_ex(ctx->u.lib.hd_dec, NULL, NULL, NULL, iv) != 1)
505 if (EVP_DecryptUpdate(ctx->u.lib.hd_dec, out, &len, in, length) != 1)
508 if (EVP_DecryptFinal(ctx->u.lib.hd_dec, out + len, &len) != 1)
514 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
515 const char *in, char *out, size_t length,
516 const char *iv, size_t iv_length)
519 return crypt_cipher_encrypt_kernel(&ctx->u.kernel, in, out, length, iv, iv_length);
521 return _cipher_encrypt(ctx, (const unsigned char*)in,
522 (unsigned char *)out, length, (const unsigned char*)iv, iv_length);
525 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
526 const char *in, char *out, size_t length,
527 const char *iv, size_t iv_length)
530 return crypt_cipher_decrypt_kernel(&ctx->u.kernel, in, out, length, iv, iv_length);
532 return _cipher_decrypt(ctx, (const unsigned char*)in,
533 (unsigned char *)out, length, (const unsigned char*)iv, iv_length);
536 bool crypt_cipher_kernel_only(struct crypt_cipher *ctx)
538 return ctx->use_kernel;
541 int crypt_bitlk_decrypt_key(const void *key, size_t key_length,
542 const char *in, char *out, size_t length,
543 const char *iv, size_t iv_length,
544 const char *tag, size_t tag_length)
546 #ifdef EVP_CTRL_CCM_SET_IVLEN
548 int len = 0, r = -EINVAL;
550 ctx = EVP_CIPHER_CTX_new();
554 if (EVP_DecryptInit_ex(ctx, EVP_aes_256_ccm(), NULL, NULL, NULL) != 1)
557 //EVP_CIPHER_CTX_key_length(ctx)
558 //EVP_CIPHER_CTX_iv_length(ctx)
560 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, iv_length, NULL) != 1)
562 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, tag_length, CONST_CAST(void*)tag) != 1)
565 if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, (const unsigned char*)iv) != 1)
568 if (EVP_DecryptUpdate(ctx, (unsigned char*)out, &len, (const unsigned char*)in, length) == 1)
571 EVP_CIPHER_CTX_free(ctx);