mirror of
https://gitlab.com/cryptsetup/cryptsetup.git
synced 2025-12-11 19:00:02 +01:00
Move PBKDF2 into crypto backend wrapper.
Implement new KDF bechmark check. Use internal openssl kdf (and prepare gcrypt one).
This commit is contained in:
@@ -4,22 +4,26 @@ noinst_LTLIBRARIES = libcrypto_backend.la
|
||||
|
||||
libcrypto_backend_la_CFLAGS = -Wall @CRYPTO_CFLAGS@
|
||||
|
||||
libcrypto_backend_la_SOURCES = crypto_backend.h
|
||||
libcrypto_backend_la_SOURCES = crypto_backend.h pbkdf_check.c
|
||||
|
||||
if CRYPTO_BACKEND_GCRYPT
|
||||
libcrypto_backend_la_SOURCES += crypto_gcrypt.c
|
||||
libcrypto_backend_la_SOURCES += pbkdf2_generic.c
|
||||
endif
|
||||
if CRYPTO_BACKEND_OPENSSL
|
||||
libcrypto_backend_la_SOURCES += crypto_openssl.c
|
||||
endif
|
||||
if CRYPTO_BACKEND_NSS
|
||||
libcrypto_backend_la_SOURCES += crypto_nss.c
|
||||
libcrypto_backend_la_SOURCES += pbkdf2_generic.c
|
||||
endif
|
||||
if CRYPTO_BACKEND_KERNEL
|
||||
libcrypto_backend_la_SOURCES += crypto_kernel.c
|
||||
libcrypto_backend_la_SOURCES += pbkdf2_generic.c
|
||||
endif
|
||||
if CRYPTO_BACKEND_NETTLE
|
||||
libcrypto_backend_la_SOURCES += crypto_nettle.c
|
||||
libcrypto_backend_la_SOURCES += pbkdf2_generic.c
|
||||
endif
|
||||
|
||||
INCLUDES = -D_GNU_SOURCE -I$(top_srcdir)/lib
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
#define _CRYPTO_BACKEND_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "config.h"
|
||||
|
||||
struct crypt_device;
|
||||
@@ -52,4 +53,19 @@ int crypt_hmac_destroy(struct crypt_hmac *ctx);
|
||||
enum { CRYPT_RND_NORMAL = 0, CRYPT_RND_KEY = 1, CRYPT_RND_SALT = 2 };
|
||||
int crypt_backend_rng(char *buffer, size_t length, int quality, int fips);
|
||||
|
||||
/* PBKDF*/
|
||||
int crypt_pbkdf_check(const char *kdf, const char *hash, uint64_t *iter_secs);
|
||||
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,
|
||||
unsigned int iterations);
|
||||
|
||||
/* internal PBKDF2 implementation */
|
||||
int pkcs5_pbkdf2(const char *hash,
|
||||
const char *P, size_t Plen,
|
||||
const char *S, size_t Slen,
|
||||
unsigned int c,
|
||||
unsigned int dkLen,char *DK);
|
||||
|
||||
#endif /* _CRYPTO_BACKEND_H */
|
||||
|
||||
@@ -251,3 +251,44 @@ int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 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,
|
||||
unsigned int iterations)
|
||||
{
|
||||
if (!kdf || strncmp(kdf, "pbkdf2", 6))
|
||||
return -EINVAL;
|
||||
|
||||
return pkcs5_pbkdf2(hash, password, password_length, salt, salt_length,
|
||||
iterations, key_length, key);
|
||||
}
|
||||
|
||||
#if 0
|
||||
/* Until bug in gcrypt related to empty password is fixed, cannot use this */
|
||||
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,
|
||||
unsigned int iterations)
|
||||
{
|
||||
int hash_id = gcry_md_map_name(hash);
|
||||
int kdf_id;
|
||||
|
||||
if (!hash_id)
|
||||
return -EINVAL;
|
||||
|
||||
if (kdf && !strncmp(kdf, "pbkdf2", 6))
|
||||
kdf_id = GCRY_KDF_PBKDF2;
|
||||
else
|
||||
return -EINVAL;
|
||||
|
||||
if (gcry_kdf_derive(password, password_length, kdf_id, hash_id,
|
||||
salt, salt_length, iterations, key_length, key))
|
||||
return -EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -302,3 +302,17 @@ int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
/* 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,
|
||||
unsigned int iterations)
|
||||
{
|
||||
if (!kdf || strncmp(kdf, "pbkdf2", 6))
|
||||
return -EINVAL;
|
||||
|
||||
return pkcs5_pbkdf2(hash, password, password_length, salt, salt_length,
|
||||
iterations, key_length, key);
|
||||
}
|
||||
|
||||
@@ -274,3 +274,18 @@ int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
/* 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,
|
||||
unsigned int iterations)
|
||||
{
|
||||
if (!kdf || strncmp(kdf, "pbkdf2", 6))
|
||||
return -EINVAL;
|
||||
|
||||
/* FIXME: switch to internal implementation in Nettle 2.6 */
|
||||
return pkcs5_pbkdf2(hash, password, password_length, salt, salt_length,
|
||||
iterations, key_length, key);
|
||||
}
|
||||
|
||||
@@ -298,3 +298,17 @@ int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 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,
|
||||
unsigned int iterations)
|
||||
{
|
||||
if (!kdf || strncmp(kdf, "pbkdf2", 6))
|
||||
return -EINVAL;
|
||||
|
||||
return pkcs5_pbkdf2(hash, password, password_length, salt, salt_length,
|
||||
iterations, key_length, key);
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ int crypt_backend_init(struct crypt_device *ctx)
|
||||
if (crypto_backend_initialised)
|
||||
return 0;
|
||||
|
||||
OpenSSL_add_all_digests();
|
||||
OpenSSL_add_all_algorithms();
|
||||
|
||||
crypto_backend_initialised = 1;
|
||||
return 0;
|
||||
@@ -230,3 +230,27 @@ int crypt_backend_rng(char *buffer, size_t length, int quality, int fips)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 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,
|
||||
unsigned int iterations)
|
||||
{
|
||||
const EVP_MD *hash_id;
|
||||
|
||||
if (!kdf || strncmp(kdf, "pbkdf2", 6))
|
||||
return -EINVAL;
|
||||
|
||||
hash_id = EVP_get_digestbyname(hash);
|
||||
if (!hash_id)
|
||||
return -EINVAL;
|
||||
|
||||
if (!PKCS5_PBKDF2_HMAC(password, (int)password_length,
|
||||
(unsigned char *)salt, (int)salt_length,
|
||||
(int)iterations, hash_id, (int)key_length, (unsigned char *)key))
|
||||
return -EINVAL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
189
lib/crypto_backend/pbkdf2_generic.c
Normal file
189
lib/crypto_backend/pbkdf2_generic.c
Normal file
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
* Implementation of Password-Based Cryptography as per PKCS#5
|
||||
* Copyright (C) 2002,2003 Simon Josefsson
|
||||
* Copyright (C) 2004 Free Software Foundation
|
||||
*
|
||||
* cryptsetup related changes
|
||||
* Copyright (C) 2012, Red Hat, Inc. All rights reserved.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <errno.h>
|
||||
#include <alloca.h>
|
||||
#include "crypto_backend.h"
|
||||
|
||||
/*
|
||||
* 5.2 PBKDF2
|
||||
*
|
||||
* PBKDF2 applies a pseudorandom function (see Appendix B.1 for an
|
||||
* example) to derive keys. The length of the derived key is essentially
|
||||
* unbounded. (However, the maximum effective search space for the
|
||||
* derived key may be limited by the structure of the underlying
|
||||
* pseudorandom function. See Appendix B.1 for further discussion.)
|
||||
* PBKDF2 is recommended for new applications.
|
||||
*
|
||||
* PBKDF2 (P, S, c, dkLen)
|
||||
*
|
||||
* Options: PRF underlying pseudorandom function (hLen
|
||||
* denotes the length in octets of the
|
||||
* pseudorandom function output)
|
||||
*
|
||||
* Input: P password, an octet string (ASCII or UTF-8)
|
||||
* S salt, an octet string
|
||||
* c iteration count, a positive integer
|
||||
* dkLen intended length in octets of the derived
|
||||
* key, a positive integer, at most
|
||||
* (2^32 - 1) * hLen
|
||||
*
|
||||
* Output: DK derived key, a dkLen-octet string
|
||||
*/
|
||||
|
||||
#define MAX_PRF_BLOCK_LEN 80
|
||||
|
||||
int pkcs5_pbkdf2(const char *hash,
|
||||
const char *P, size_t Plen,
|
||||
const char *S, size_t Slen,
|
||||
unsigned int c, unsigned int dkLen,
|
||||
char *DK)
|
||||
{
|
||||
struct crypt_hmac *hmac;
|
||||
char U[MAX_PRF_BLOCK_LEN];
|
||||
char T[MAX_PRF_BLOCK_LEN];
|
||||
int i, k, rc = -EINVAL;
|
||||
unsigned int u, hLen, l, r;
|
||||
size_t tmplen = Slen + 4;
|
||||
char *tmp;
|
||||
|
||||
tmp = alloca(tmplen);
|
||||
if (tmp == NULL)
|
||||
return -ENOMEM;
|
||||
|
||||
hLen = crypt_hmac_size(hash);
|
||||
if (hLen == 0 || hLen > MAX_PRF_BLOCK_LEN)
|
||||
return -EINVAL;
|
||||
|
||||
if (c == 0)
|
||||
return -EINVAL;
|
||||
|
||||
if (dkLen == 0)
|
||||
return -EINVAL;
|
||||
|
||||
/*
|
||||
*
|
||||
* Steps:
|
||||
*
|
||||
* 1. If dkLen > (2^32 - 1) * hLen, output "derived key too long" and
|
||||
* stop.
|
||||
*/
|
||||
|
||||
if (dkLen > 4294967295U)
|
||||
return -EINVAL;
|
||||
|
||||
/*
|
||||
* 2. Let l be the number of hLen-octet blocks in the derived key,
|
||||
* rounding up, and let r be the number of octets in the last
|
||||
* block:
|
||||
*
|
||||
* l = CEIL (dkLen / hLen) ,
|
||||
* r = dkLen - (l - 1) * hLen .
|
||||
*
|
||||
* Here, CEIL (x) is the "ceiling" function, i.e. the smallest
|
||||
* integer greater than, or equal to, x.
|
||||
*/
|
||||
|
||||
l = dkLen / hLen;
|
||||
if (dkLen % hLen)
|
||||
l++;
|
||||
r = dkLen - (l - 1) * hLen;
|
||||
|
||||
/*
|
||||
* 3. For each block of the derived key apply the function F defined
|
||||
* below to the password P, the salt S, the iteration count c, and
|
||||
* the block index to compute the block:
|
||||
*
|
||||
* T_1 = F (P, S, c, 1) ,
|
||||
* T_2 = F (P, S, c, 2) ,
|
||||
* ...
|
||||
* T_l = F (P, S, c, l) ,
|
||||
*
|
||||
* where the function F is defined as the exclusive-or sum of the
|
||||
* first c iterates of the underlying pseudorandom function PRF
|
||||
* applied to the password P and the concatenation of the salt S
|
||||
* and the block index i:
|
||||
*
|
||||
* F (P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c
|
||||
*
|
||||
* where
|
||||
*
|
||||
* U_1 = PRF (P, S || INT (i)) ,
|
||||
* U_2 = PRF (P, U_1) ,
|
||||
* ...
|
||||
* U_c = PRF (P, U_{c-1}) .
|
||||
*
|
||||
* Here, INT (i) is a four-octet encoding of the integer i, most
|
||||
* significant octet first.
|
||||
*
|
||||
* 4. Concatenate the blocks and extract the first dkLen octets to
|
||||
* produce a derived key DK:
|
||||
*
|
||||
* DK = T_1 || T_2 || ... || T_l<0..r-1>
|
||||
*
|
||||
* 5. Output the derived key DK.
|
||||
*
|
||||
* Note. The construction of the function F follows a "belt-and-
|
||||
* suspenders" approach. The iterates U_i are computed recursively to
|
||||
* remove a degree of parallelism from an opponent; they are exclusive-
|
||||
* ored together to reduce concerns about the recursion degenerating
|
||||
* into a small set of values.
|
||||
*
|
||||
*/
|
||||
|
||||
if (crypt_hmac_init(&hmac, hash, P, Plen))
|
||||
return -EINVAL;
|
||||
|
||||
for (i = 1; (unsigned int) i <= l; i++) {
|
||||
memset(T, 0, hLen);
|
||||
|
||||
for (u = 1; u <= c ; u++) {
|
||||
if (u == 1) {
|
||||
memcpy(tmp, S, Slen);
|
||||
tmp[Slen + 0] = (i & 0xff000000) >> 24;
|
||||
tmp[Slen + 1] = (i & 0x00ff0000) >> 16;
|
||||
tmp[Slen + 2] = (i & 0x0000ff00) >> 8;
|
||||
tmp[Slen + 3] = (i & 0x000000ff) >> 0;
|
||||
|
||||
if (crypt_hmac_write(hmac, tmp, tmplen))
|
||||
goto out;
|
||||
} else {
|
||||
if (crypt_hmac_write(hmac, U, hLen))
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (crypt_hmac_final(hmac, U, hLen))
|
||||
goto out;
|
||||
|
||||
for (k = 0; (unsigned int) k < hLen; k++)
|
||||
T[k] ^= U[k];
|
||||
}
|
||||
|
||||
memcpy(DK + (i - 1) * hLen, T, (unsigned int) i == l ? r : hLen);
|
||||
}
|
||||
rc = 0;
|
||||
out:
|
||||
crypt_hmac_destroy(hmac);
|
||||
return rc;
|
||||
}
|
||||
Reference in New Issue
Block a user