2 * OPENSSL crypto backend implementation
4 * Copyright (C) 2010-2012, Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2010-2014, 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.h"
38 static int crypto_backend_initialised = 0;
42 const EVP_MD *hash_id;
48 const EVP_MD *hash_id;
52 int crypt_backend_init(struct crypt_device *ctx)
54 if (crypto_backend_initialised)
57 OpenSSL_add_all_algorithms();
59 crypto_backend_initialised = 1;
63 uint32_t crypt_backend_flags(void)
68 const char *crypt_backend_version(void)
70 return SSLeay_version(SSLEAY_VERSION);
74 int crypt_hash_size(const char *name)
76 const EVP_MD *hash_id = EVP_get_digestbyname(name);
81 return EVP_MD_size(hash_id);
84 int crypt_hash_init(struct crypt_hash **ctx, const char *name)
88 h = malloc(sizeof(*h));
92 h->hash_id = EVP_get_digestbyname(name);
98 if (EVP_DigestInit(&h->md, h->hash_id) != 1) {
103 h->hash_len = EVP_MD_size(h->hash_id);
108 static int crypt_hash_restart(struct crypt_hash *ctx)
110 if (EVP_DigestInit(&ctx->md, ctx->hash_id) != 1)
116 int crypt_hash_write(struct crypt_hash *ctx, const char *buffer, size_t length)
118 if (EVP_DigestUpdate(&ctx->md, buffer, length) != 1)
124 int crypt_hash_final(struct crypt_hash *ctx, char *buffer, size_t length)
126 unsigned char tmp[EVP_MAX_MD_SIZE];
127 unsigned int tmp_len = 0;
129 if (length > (size_t)ctx->hash_len)
132 if (EVP_DigestFinal_ex(&ctx->md, tmp, &tmp_len) != 1)
135 memcpy(buffer, tmp, length);
136 crypt_backend_memzero(tmp, sizeof(tmp));
138 if (tmp_len < length)
141 if (crypt_hash_restart(ctx))
147 int crypt_hash_destroy(struct crypt_hash *ctx)
149 EVP_MD_CTX_cleanup(&ctx->md);
150 memset(ctx, 0, sizeof(*ctx));
156 int crypt_hmac_size(const char *name)
158 return crypt_hash_size(name);
161 int crypt_hmac_init(struct crypt_hmac **ctx, const char *name,
162 const void *buffer, size_t length)
164 struct crypt_hmac *h;
166 h = malloc(sizeof(*h));
170 h->hash_id = EVP_get_digestbyname(name);
176 HMAC_CTX_init(&h->md);
177 HMAC_Init_ex(&h->md, buffer, length, h->hash_id, NULL);
179 h->hash_len = EVP_MD_size(h->hash_id);
184 static void crypt_hmac_restart(struct crypt_hmac *ctx)
186 HMAC_Init_ex(&ctx->md, NULL, 0, ctx->hash_id, NULL);
189 int crypt_hmac_write(struct crypt_hmac *ctx, const char *buffer, size_t length)
191 HMAC_Update(&ctx->md, (const unsigned char *)buffer, length);
195 int crypt_hmac_final(struct crypt_hmac *ctx, char *buffer, size_t length)
197 unsigned char tmp[EVP_MAX_MD_SIZE];
198 unsigned int tmp_len = 0;
200 if (length > (size_t)ctx->hash_len)
203 HMAC_Final(&ctx->md, tmp, &tmp_len);
205 memcpy(buffer, tmp, length);
206 crypt_backend_memzero(tmp, sizeof(tmp));
208 if (tmp_len < length)
211 crypt_hmac_restart(ctx);
216 int crypt_hmac_destroy(struct crypt_hmac *ctx)
218 HMAC_CTX_cleanup(&ctx->md);
219 memset(ctx, 0, sizeof(*ctx));
225 int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
230 if (RAND_bytes((unsigned char *)buffer, length) != 1)
237 int crypt_pbkdf(const char *kdf, const char *hash,
238 const char *password, size_t password_length,
239 const char *salt, size_t salt_length,
240 char *key, size_t key_length,
241 unsigned int iterations)
243 const EVP_MD *hash_id;
245 if (!kdf || strncmp(kdf, "pbkdf2", 6))
248 hash_id = EVP_get_digestbyname(hash);
252 if (!PKCS5_PBKDF2_HMAC(password, (int)password_length,
253 (unsigned char *)salt, (int)salt_length,
254 (int)iterations, hash_id, (int)key_length, (unsigned char *)key))