mirror of
https://gitlab.com/cryptsetup/cryptsetup.git
synced 2025-12-13 03:40:05 +01:00
Mbed-TLS: implement PBKDF2
PBKDF2 has been implemented since 2.0.0 and a new API was introduced in 3.3.0 deprecating the old one. This implementation will use the new API if detected.
This commit is contained in:
@@ -406,11 +406,12 @@ AC_DEFUN([CONFIGURE_MBEDTLS], [
|
|||||||
saved_LIBS=$LIBS
|
saved_LIBS=$LIBS
|
||||||
AC_CHECK_LIB(mbedcrypto, mbedtls_md_init,,
|
AC_CHECK_LIB(mbedcrypto, mbedtls_md_init,,
|
||||||
[AC_MSG_ERROR([You need mbedTLS cryptographic library.])])
|
[AC_MSG_ERROR([You need mbedTLS cryptographic library.])])
|
||||||
|
AC_CHECK_FUNCS(mbedtls_pkcs5_pbkdf2_hmac_ext)
|
||||||
CRYPTO_LIBS=$LIBS
|
CRYPTO_LIBS=$LIBS
|
||||||
LIBS=$saved_LIBS
|
LIBS=$saved_LIBS
|
||||||
|
|
||||||
CRYPTO_STATIC_LIBS=$CRYPTO_LIBS
|
CRYPTO_STATIC_LIBS=$CRYPTO_LIBS
|
||||||
use_internal_pbkdf2=1
|
use_internal_pbkdf2=0
|
||||||
use_internal_argon2=1
|
use_internal_argon2=1
|
||||||
NO_FIPS([])
|
NO_FIPS([])
|
||||||
])
|
])
|
||||||
|
|||||||
@@ -16,24 +16,11 @@
|
|||||||
#include <mbedtls/ctr_drbg.h>
|
#include <mbedtls/ctr_drbg.h>
|
||||||
#include <mbedtls/entropy.h>
|
#include <mbedtls/entropy.h>
|
||||||
#include <mbedtls/md.h>
|
#include <mbedtls/md.h>
|
||||||
|
#include <mbedtls/pkcs5.h>
|
||||||
#include <mbedtls/version.h>
|
#include <mbedtls/version.h>
|
||||||
|
|
||||||
#include "crypto_backend_internal.h"
|
#include "crypto_backend_internal.h"
|
||||||
|
|
||||||
static const struct hash_alg {
|
|
||||||
const char *name;
|
|
||||||
mbedtls_md_type_t type;
|
|
||||||
unsigned int block_length;
|
|
||||||
} kHash[] = {
|
|
||||||
{"sha1", MBEDTLS_MD_SHA1, 64},
|
|
||||||
{"sha224", MBEDTLS_MD_SHA224, 64},
|
|
||||||
{"sha256", MBEDTLS_MD_SHA256, 64},
|
|
||||||
{"sha384", MBEDTLS_MD_SHA384, 128},
|
|
||||||
{"sha512", MBEDTLS_MD_SHA512, 128},
|
|
||||||
{"ripemd160", MBEDTLS_MD_RIPEMD160, 64},
|
|
||||||
{NULL, 0, 0}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct crypt_hash {
|
struct crypt_hash {
|
||||||
const mbedtls_md_info_t *info;
|
const mbedtls_md_info_t *info;
|
||||||
mbedtls_md_context_t md;
|
mbedtls_md_context_t md;
|
||||||
@@ -58,6 +45,19 @@ static mbedtls_ctr_drbg_context g_ctr_drbg;
|
|||||||
|
|
||||||
static const mbedtls_md_info_t *crypt_get_hash(const char *name)
|
static const mbedtls_md_info_t *crypt_get_hash(const char *name)
|
||||||
{
|
{
|
||||||
|
static const struct hash_alg {
|
||||||
|
const char *name;
|
||||||
|
mbedtls_md_type_t type;
|
||||||
|
} kHash[] = {
|
||||||
|
{"sha1", MBEDTLS_MD_SHA1 },
|
||||||
|
{"sha224", MBEDTLS_MD_SHA224 },
|
||||||
|
{"sha256", MBEDTLS_MD_SHA256 },
|
||||||
|
{"sha384", MBEDTLS_MD_SHA384 },
|
||||||
|
{"sha512", MBEDTLS_MD_SHA512 },
|
||||||
|
{"ripemd160", MBEDTLS_MD_RIPEMD160},
|
||||||
|
{NULL, 0, }
|
||||||
|
};
|
||||||
|
|
||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
|
|
||||||
while (name && kHash[i].name) {
|
while (name && kHash[i].name) {
|
||||||
@@ -69,19 +69,6 @@ static const mbedtls_md_info_t *crypt_get_hash(const char *name)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static unsigned int crypt_get_hash_block_length(const char *name)
|
|
||||||
{
|
|
||||||
size_t i = 0;
|
|
||||||
|
|
||||||
while (name && kHash[i].name) {
|
|
||||||
if (strcmp(kHash[i].name, name) == 0)
|
|
||||||
return kHash[i].block_length;
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int crypt_backend_init(bool fips)
|
int crypt_backend_init(bool fips)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
@@ -467,18 +454,44 @@ int crypt_pbkdf(const char *kdf, const char *hash,
|
|||||||
char *key, size_t key_length,
|
char *key, size_t key_length,
|
||||||
uint32_t iterations, uint32_t memory, uint32_t parallel)
|
uint32_t iterations, uint32_t memory, uint32_t parallel)
|
||||||
{
|
{
|
||||||
unsigned int block_length;
|
const mbedtls_md_info_t *info;
|
||||||
|
#if !HAVE_MBEDTLS_PKCS5_PBKDF2_HMAC_EXT
|
||||||
|
mbedtls_md_context_t md;
|
||||||
|
#endif
|
||||||
|
|
||||||
if (!kdf)
|
if (!kdf)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
if (strcmp(kdf, "pbkdf2") == 0) {
|
if (strcmp(kdf, "pbkdf2") == 0) {
|
||||||
block_length = crypt_get_hash_block_length(hash);
|
info = crypt_get_hash(hash);
|
||||||
if (!block_length)
|
if (!info)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
return pkcs5_pbkdf2(hash, password, password_length, salt, salt_length,
|
#if HAVE_MBEDTLS_PKCS5_PBKDF2_HMAC_EXT
|
||||||
iterations, key_length, key, block_length);
|
if (mbedtls_pkcs5_pbkdf2_hmac_ext(mbedtls_md_get_type(info),
|
||||||
|
(const unsigned char *)password, password_length,
|
||||||
|
(const unsigned char *)salt, salt_length,
|
||||||
|
iterations, key_length, (unsigned char *)key)) {
|
||||||
|
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
mbedtls_md_init(&md);
|
||||||
|
if (mbedtls_md_setup(&md, info, 1))
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (mbedtls_pkcs5_pbkdf2_hmac(&md,
|
||||||
|
(const unsigned char *)password, password_length,
|
||||||
|
(const unsigned char *)salt, salt_length,
|
||||||
|
iterations, key_length, (unsigned char *)key)) {
|
||||||
|
|
||||||
|
mbedtls_md_free(&md);
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
mbedtls_md_free(&md);
|
||||||
|
#endif
|
||||||
|
return 0;
|
||||||
|
|
||||||
} else if (strncmp(kdf, "argon2", 6) == 0) {
|
} else if (strncmp(kdf, "argon2", 6) == 0) {
|
||||||
return argon2(kdf, password, password_length, salt, salt_length,
|
return argon2(kdf, password, password_length, salt, salt_length,
|
||||||
|
|||||||
@@ -559,9 +559,9 @@ elif get_option('crypto-backend') == 'mbedtls'
|
|||||||
if get_option('fips')
|
if get_option('fips')
|
||||||
error('mbedtls crypto backend is not supported with FIPS enabled')
|
error('mbedtls crypto backend is not supported with FIPS enabled')
|
||||||
endif
|
endif
|
||||||
|
|
||||||
assert(cc.has_header('mbedtls/version.h'),
|
assert(cc.has_header('mbedtls/version.h'),
|
||||||
'You need mbedTLS cryptographic library.')
|
'You need mbedTLS cryptographic library.')
|
||||||
conf.set10('HAVE_MBEDTLS_VERSION_H', cc.has_header('mbedtls/version.h'))
|
|
||||||
|
|
||||||
mbedcrypto = cc.find_library('mbedcrypto',
|
mbedcrypto = cc.find_library('mbedcrypto',
|
||||||
static: enable_static)
|
static: enable_static)
|
||||||
@@ -569,8 +569,13 @@ elif get_option('crypto-backend') == 'mbedtls'
|
|||||||
prefix: '#include <mbedtls/md.h>', dependencies: mbedcrypto),
|
prefix: '#include <mbedtls/md.h>', dependencies: mbedcrypto),
|
||||||
'You need mbedcrypto library.')
|
'You need mbedcrypto library.')
|
||||||
|
|
||||||
|
conf.set10('HAVE_MBEDTLS_PKCS5_PBKDF2_HMAC_EXT',
|
||||||
|
cc.has_function('mbedtls_pkcs5_pbkdf2_hmac_ext',
|
||||||
|
prefix: '#include <mbedtls/pkcs5.h>', dependencies: mbedcrypto),
|
||||||
|
description: 'Define to 1 if you have the `mbedtls_pkcs5_pbkdf2_hmac_ext\' function.')
|
||||||
|
|
||||||
crypto_backend_library = mbedcrypto
|
crypto_backend_library = mbedcrypto
|
||||||
use_internal_pbkdf2 = true
|
use_internal_pbkdf2 = false
|
||||||
use_internal_argon2 = true
|
use_internal_argon2 = true
|
||||||
endif
|
endif
|
||||||
conf.set10('USE_INTERNAL_PBKDF2', use_internal_pbkdf2)
|
conf.set10('USE_INTERNAL_PBKDF2', use_internal_pbkdf2)
|
||||||
|
|||||||
Reference in New Issue
Block a user