2 * OPENSSL crypto backend implementation
4 * Copyright (C) 2010-2020 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2010-2020 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;
67 * Compatible wrappers for OpenSSL < 1.1.0 and LibreSSL < 2.7.0
69 #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
70 (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
72 static void openssl_backend_init(void)
74 OpenSSL_add_all_algorithms();
77 static const char *openssl_backend_version(void)
79 return SSLeay_version(SSLEAY_VERSION);
82 static EVP_MD_CTX *EVP_MD_CTX_new(void)
84 EVP_MD_CTX *md = malloc(sizeof(*md));
92 static void EVP_MD_CTX_free(EVP_MD_CTX *md)
94 EVP_MD_CTX_cleanup(md);
98 static HMAC_CTX *HMAC_CTX_new(void)
100 HMAC_CTX *md = malloc(sizeof(*md));
108 static void HMAC_CTX_free(HMAC_CTX *md)
110 HMAC_CTX_cleanup(md);
114 static void openssl_backend_init(void)
118 static const char *openssl_backend_version(void)
120 return OpenSSL_version(OPENSSL_VERSION);
124 int crypt_backend_init(void)
126 if (crypto_backend_initialised)
129 openssl_backend_init();
131 crypto_backend_initialised = 1;
135 void crypt_backend_destroy(void)
137 crypto_backend_initialised = 0;
140 uint32_t crypt_backend_flags(void)
145 const char *crypt_backend_version(void)
147 return openssl_backend_version();
151 int crypt_hash_size(const char *name)
153 const EVP_MD *hash_id = EVP_get_digestbyname(name);
158 return EVP_MD_size(hash_id);
161 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
163 struct crypt_hash *h;
165 h = malloc(sizeof(*h));
169 h->md = EVP_MD_CTX_new();
175 h->hash_id = EVP_get_digestbyname(name);
177 EVP_MD_CTX_free(h->md);
182 if (EVP_DigestInit_ex(h->md, h->hash_id, NULL) != 1) {
183 EVP_MD_CTX_free(h->md);
188 h->hash_len = EVP_MD_size(h->hash_id);
193 static int crypt_hash_restart(struct crypt_hash *ctx)
195 if (EVP_DigestInit_ex(ctx->md, ctx->hash_id, NULL) != 1)
201 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
203 if (EVP_DigestUpdate(ctx->md, buffer, length) != 1)
209 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
211 unsigned char tmp[EVP_MAX_MD_SIZE];
212 unsigned int tmp_len = 0;
214 if (length > (size_t)ctx->hash_len)
217 if (EVP_DigestFinal_ex(ctx->md, tmp, &tmp_len) != 1)
220 memcpy(buffer, tmp, length);
221 crypt_backend_memzero(tmp, sizeof(tmp));
223 if (tmp_len < length)
226 if (crypt_hash_restart(ctx))
232 void crypt_hash_destroy(struct crypt_hash *ctx)
234 EVP_MD_CTX_free(ctx->md);
235 memset(ctx, 0, sizeof(*ctx));
240 int crypt_hmac_size(const char *name)
242 return crypt_hash_size(name);
245 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
246 const void *key, size_t key_length)
248 struct crypt_hmac *h;
250 h = malloc(sizeof(*h));
254 h->md = HMAC_CTX_new();
260 h->hash_id = EVP_get_digestbyname(name);
262 HMAC_CTX_free(h->md);
267 HMAC_Init_ex(h->md, key, key_length, h->hash_id, NULL);
269 h->hash_len = EVP_MD_size(h->hash_id);
274 static void crypt_hmac_restart(struct crypt_hmac *ctx)
276 HMAC_Init_ex(ctx->md, NULL, 0, ctx->hash_id, NULL);
279 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
281 HMAC_Update(ctx->md, (const unsigned char *)buffer, length);
285 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
287 unsigned char tmp[EVP_MAX_MD_SIZE];
288 unsigned int tmp_len = 0;
290 if (length > (size_t)ctx->hash_len)
293 HMAC_Final(ctx->md, tmp, &tmp_len);
295 memcpy(buffer, tmp, length);
296 crypt_backend_memzero(tmp, sizeof(tmp));
298 if (tmp_len < length)
301 crypt_hmac_restart(ctx);
306 void crypt_hmac_destroy(struct crypt_hmac *ctx)
308 HMAC_CTX_free(ctx->md);
309 memset(ctx, 0, sizeof(*ctx));
314 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
316 if (RAND_bytes((unsigned char *)buffer, length) != 1)
323 int crypt_pbkdf(const char *kdf, const char *hash,
324 const char *password, size_t password_length,
325 const char *salt, size_t salt_length,
326 char *key, size_t key_length,
327 uint32_t iterations, uint32_t memory, uint32_t parallel)
330 const EVP_MD *hash_id;
335 if (!strcmp(kdf, "pbkdf2")) {
336 hash_id = EVP_get_digestbyname(hash);
340 if (!PKCS5_PBKDF2_HMAC(password, (int)password_length,
341 (const unsigned char *)salt, (int)salt_length,
342 (int)iterations, hash_id, (int)key_length, (unsigned char *)key))
345 } else if (!strncmp(kdf, "argon2", 6)) {
346 return argon2(kdf, password, password_length, salt, salt_length,
347 key, key_length, iterations, memory, parallel);
354 static void _cipher_destroy(EVP_CIPHER_CTX **hd_enc, EVP_CIPHER_CTX **hd_dec)
356 EVP_CIPHER_CTX_free(*hd_enc);
359 EVP_CIPHER_CTX_free(*hd_dec);
363 static int _cipher_init(EVP_CIPHER_CTX **hd_enc, EVP_CIPHER_CTX **hd_dec, const char *name,
364 const char *mode, const void *key, size_t key_length, size_t *iv_length)
366 char cipher_name[256];
367 const EVP_CIPHER *type;
370 key_bits = key_length * 8;
371 if (!strcmp(mode, "xts"))
374 r = snprintf(cipher_name, sizeof(cipher_name), "%s-%d-%s", name, key_bits, mode);
375 if (r < 0 || r >= (int)sizeof(cipher_name))
378 type = EVP_get_cipherbyname(cipher_name);
382 if (EVP_CIPHER_key_length(type) != (int)key_length)
385 *hd_enc = EVP_CIPHER_CTX_new();
386 *hd_dec = EVP_CIPHER_CTX_new();
387 *iv_length = EVP_CIPHER_iv_length(type);
389 if (!*hd_enc || !*hd_dec)
392 if (EVP_EncryptInit_ex(*hd_enc, type, NULL, key, NULL) != 1 ||
393 EVP_DecryptInit_ex(*hd_dec, type, NULL, key, NULL) != 1) {
394 _cipher_destroy(hd_enc, hd_dec);
398 if (EVP_CIPHER_CTX_set_padding(*hd_enc, 0) != 1 ||
399 EVP_CIPHER_CTX_set_padding(*hd_dec, 0) != 1) {
400 _cipher_destroy(hd_enc, hd_dec);
407 int crypt_cipher_init(struct crypt_cipher **ctx, const char *name,
408 const char *mode, const void *key, size_t key_length)
410 struct crypt_cipher *h;
413 h = malloc(sizeof(*h));
417 if (!_cipher_init(&h->u.lib.hd_enc, &h->u.lib.hd_dec, name, mode, key,
418 key_length, &h->u.lib.iv_length)) {
419 h->use_kernel = false;
424 r = crypt_cipher_init_kernel(&h->u.kernel, name, mode, key, key_length);
430 h->use_kernel = true;
435 void crypt_cipher_destroy(struct crypt_cipher *ctx)
438 crypt_cipher_destroy_kernel(&ctx->u.kernel);
440 _cipher_destroy(&ctx->u.lib.hd_enc, &ctx->u.lib.hd_dec);
444 static int _cipher_encrypt(struct crypt_cipher *ctx, const unsigned char *in, unsigned char *out,
445 int length, const unsigned char *iv, size_t iv_length)
449 if (ctx->u.lib.iv_length != iv_length)
452 if (EVP_EncryptInit_ex(ctx->u.lib.hd_enc, NULL, NULL, NULL, iv) != 1)
455 if (EVP_EncryptUpdate(ctx->u.lib.hd_enc, out, &len, in, length) != 1)
458 if (EVP_EncryptFinal(ctx->u.lib.hd_enc, out + len, &len) != 1)
464 static int _cipher_decrypt(struct crypt_cipher *ctx, const unsigned char *in, unsigned char *out,
465 int length, const unsigned char *iv, size_t iv_length)
469 if (ctx->u.lib.iv_length != iv_length)
472 if (EVP_DecryptInit_ex(ctx->u.lib.hd_dec, NULL, NULL, NULL, iv) != 1)
475 if (EVP_DecryptUpdate(ctx->u.lib.hd_dec, out, &len, in, length) != 1)
478 if (EVP_DecryptFinal(ctx->u.lib.hd_dec, out + len, &len) != 1)
484 int crypt_cipher_encrypt(struct crypt_cipher *ctx,
485 const char *in, char *out, size_t length,
486 const char *iv, size_t iv_length)
489 return crypt_cipher_encrypt_kernel(&ctx->u.kernel, in, out, length, iv, iv_length);
491 return _cipher_encrypt(ctx, (const unsigned char*)in,
492 (unsigned char *)out, length, (const unsigned char*)iv, iv_length);
495 int crypt_cipher_decrypt(struct crypt_cipher *ctx,
496 const char *in, char *out, size_t length,
497 const char *iv, size_t iv_length)
500 return crypt_cipher_decrypt_kernel(&ctx->u.kernel, in, out, length, iv, iv_length);
502 return _cipher_decrypt(ctx, (const unsigned char*)in,
503 (unsigned char *)out, length, (const unsigned char*)iv, iv_length);
506 bool crypt_cipher_kernel_only(struct crypt_cipher *ctx)
508 return ctx->use_kernel;
511 int crypt_bitlk_decrypt_key(const void *key, size_t key_length,
512 const char *in, char *out, size_t length,
513 const char *iv, size_t iv_length,
514 const char *tag, size_t tag_length)
516 #ifdef EVP_CTRL_CCM_SET_IVLEN
518 int len = 0, r = -EINVAL;
520 ctx = EVP_CIPHER_CTX_new();
524 if (EVP_DecryptInit_ex(ctx, EVP_aes_256_ccm(), NULL, NULL, NULL) != 1)
527 //EVP_CIPHER_CTX_key_length(ctx)
528 //EVP_CIPHER_CTX_iv_length(ctx)
530 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_IVLEN, iv_length, NULL) != 1)
532 if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_CCM_SET_TAG, tag_length, CONST_CAST(void*)tag) != 1)
535 if (EVP_DecryptInit_ex(ctx, NULL, NULL, key, (const unsigned char*)iv) != 1)
538 if (EVP_DecryptUpdate(ctx, (unsigned char*)out, &len, (const unsigned char*)in, length) == 1)
541 EVP_CIPHER_CTX_free(ctx);