Skip to content

Commit

Permalink
Merge pull request #6244 from AndrzejKurek/pkcs5-no-md
Browse files Browse the repository at this point in the history
Driver-only hashes: PKCS5
  • Loading branch information
mpg authored Sep 5, 2022
2 parents 97fc247 + c502210 commit 52f83dc
Show file tree
Hide file tree
Showing 11 changed files with 442 additions and 295 deletions.
2 changes: 0 additions & 2 deletions docs/architecture/psa-migration/outcome-analysis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ reference_config () {
scripts/config.py set MBEDTLS_USE_PSA_CRYPTO
scripts/config.py unset MBEDTLS_PKCS1_V21
scripts/config.py unset MBEDTLS_X509_RSASSA_PSS_SUPPORT
scripts/config.py unset MBEDTLS_PKCS5_C
scripts/config.py unset MBEDTLS_ECDSA_DETERMINISTIC
}
# Space-separated list of test suites of interest.
Expand Down Expand Up @@ -115,4 +114,3 @@ compare_builds () {
compare_builds before-default after-default
compare_builds before-full after-full
compare_builds reference drivers

5 changes: 3 additions & 2 deletions include/mbedtls/check_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@
#error "MBEDTLS_PKCS12_C defined, but not all prerequisites"
#endif

#if defined(MBEDTLS_PKCS5_C) && (!defined(MBEDTLS_MD_C) || \
!defined(MBEDTLS_CIPHER_C))
#if defined(MBEDTLS_PKCS5_C) && \
( !( defined(MBEDTLS_MD_C) || defined(MBEDTLS_PSA_CRYPTO_C) ) || \
!defined(MBEDTLS_CIPHER_C) )
#error "MBEDTLS_PKCS5_C defined, but not all prerequisites"
#endif

Expand Down
3 changes: 2 additions & 1 deletion include/mbedtls/config_psa.h
Original file line number Diff line number Diff line change
Expand Up @@ -620,10 +620,11 @@ extern "C" {
#define PSA_WANT_ALG_HKDF_EXPAND 1
#endif /* MBEDTLS_HKDF_C */

#if defined(MBEDTLS_MD_C)
#define MBEDTLS_PSA_BUILTIN_ALG_HMAC 1
#define PSA_WANT_ALG_HMAC 1
#define PSA_WANT_KEY_TYPE_HMAC

#if defined(MBEDTLS_MD_C)
#define MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF 1
#define PSA_WANT_ALG_TLS12_PRF 1
#define MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS 1
Expand Down
30 changes: 28 additions & 2 deletions include/mbedtls/pkcs5.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,33 @@ int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode,

#endif /* MBEDTLS_ASN1_PARSE_C */

/**
* \brief PKCS#5 PBKDF2 using HMAC without using the HMAC context
*
* \param md_type Hash algorithm used
* \param password Password to use when generating key
* \param plen Length of password
* \param salt Salt to use when generating key
* \param slen Length of salt
* \param iteration_count Iteration count
* \param key_length Length of generated key in bytes
* \param output Generated key. Must be at least as big as key_length
*
* \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails.
*/
int mbedtls_pkcs5_pbkdf2_hmac_ext( mbedtls_md_type_t md_type,
const unsigned char *password,
size_t plen, const unsigned char *salt, size_t slen,
unsigned int iteration_count,
uint32_t key_length, unsigned char *output );

#if defined(MBEDTLS_MD_C)
#if !defined(MBEDTLS_DEPRECATED_REMOVED)
/**
* \brief PKCS#5 PBKDF2 using HMAC
*
* \deprecated Superseded by mbedtls_pkcs5_pbkdf2_hmac_ext().
*
* \param ctx Generic HMAC context
* \param password Password to use when generating key
* \param plen Length of password
Expand All @@ -84,11 +108,13 @@ int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode,
*
* \returns 0 on success, or a MBEDTLS_ERR_XXX code if verification fails.
*/
int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx, const unsigned char *password,
int MBEDTLS_DEPRECATED mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx,
const unsigned char *password,
size_t plen, const unsigned char *salt, size_t slen,
unsigned int iteration_count,
uint32_t key_length, unsigned char *output );

#endif /* !MBEDTLS_DEPRECATED_REMOVED */
#endif /* MBEDTLS_MD_C */
#if defined(MBEDTLS_SELF_TEST)

/**
Expand Down
195 changes: 155 additions & 40 deletions library/pkcs5.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@
#define mbedtls_printf printf
#endif

#include "hash_info.h"
#include "mbedtls/psa_util.h"

#if defined(MBEDTLS_ASN1_PARSE_C)
static int pkcs5_parse_pbkdf2_params( const mbedtls_asn1_buf *params,
mbedtls_asn1_buf *salt, int *iterations,
Expand Down Expand Up @@ -118,9 +121,7 @@ int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode,
mbedtls_md_type_t md_type = MBEDTLS_MD_SHA1;
unsigned char key[32], iv[32];
size_t olen = 0;
const mbedtls_md_info_t *md_info;
const mbedtls_cipher_info_t *cipher_info;
mbedtls_md_context_t md_ctx;
mbedtls_cipher_type_t cipher_alg;
mbedtls_cipher_context_t cipher_ctx;

Expand Down Expand Up @@ -153,10 +154,6 @@ int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode,
return( ret );
}

md_info = mbedtls_md_info_from_type( md_type );
if( md_info == NULL )
return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );

if( ( ret = mbedtls_asn1_get_alg( &p, end, &enc_scheme_oid,
&enc_scheme_params ) ) != 0 )
{
Expand All @@ -182,16 +179,13 @@ int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode,
return( MBEDTLS_ERR_PKCS5_INVALID_FORMAT );
}

mbedtls_md_init( &md_ctx );
mbedtls_cipher_init( &cipher_ctx );

memcpy( iv, enc_scheme_params.p, enc_scheme_params.len );

if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
goto exit;

if( ( ret = mbedtls_pkcs5_pbkdf2_hmac( &md_ctx, pwd, pwdlen, salt.p, salt.len,
iterations, keylen, key ) ) != 0 )
if( ( ret = mbedtls_pkcs5_pbkdf2_hmac_ext( md_type, pwd, pwdlen, salt.p,
salt.len, iterations, keylen,
key ) ) != 0 )
{
goto exit;
}
Expand All @@ -208,18 +202,18 @@ int mbedtls_pkcs5_pbes2( const mbedtls_asn1_buf *pbe_params, int mode,
ret = MBEDTLS_ERR_PKCS5_PASSWORD_MISMATCH;

exit:
mbedtls_md_free( &md_ctx );
mbedtls_cipher_free( &cipher_ctx );

return( ret );
}
#endif /* MBEDTLS_ASN1_PARSE_C */

int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx,
const unsigned char *password,
size_t plen, const unsigned char *salt, size_t slen,
unsigned int iteration_count,
uint32_t key_length, unsigned char *output )
#if defined(MBEDTLS_MD_C)
static int pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx,
const unsigned char *password,
size_t plen, const unsigned char *salt, size_t slen,
unsigned int iteration_count,
uint32_t key_length, unsigned char *output )
{
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
int j;
Expand Down Expand Up @@ -297,9 +291,149 @@ int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx,
return( ret );
}

#if !defined(MBEDTLS_DEPRECATED_REMOVED)
int mbedtls_pkcs5_pbkdf2_hmac( mbedtls_md_context_t *ctx,
const unsigned char *password,
size_t plen, const unsigned char *salt, size_t slen,
unsigned int iteration_count,
uint32_t key_length, unsigned char *output )
{
return( pkcs5_pbkdf2_hmac( ctx, password, plen, salt, slen, iteration_count,
key_length, output ) );
}
#endif
#endif /* MBEDTLS_MD_C */

int mbedtls_pkcs5_pbkdf2_hmac_ext( mbedtls_md_type_t md_alg,
const unsigned char *password,
size_t plen, const unsigned char *salt, size_t slen,
unsigned int iteration_count,
uint32_t key_length, unsigned char *output )
{
#if defined(MBEDTLS_MD_C)
mbedtls_md_context_t md_ctx;
const mbedtls_md_info_t *md_info = NULL;
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;

md_info = mbedtls_md_info_from_type( md_alg );
if( md_info == NULL )
return( MBEDTLS_ERR_PKCS5_FEATURE_UNAVAILABLE );

mbedtls_md_init( &md_ctx );

if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
goto exit;
ret = pkcs5_pbkdf2_hmac( &md_ctx, password, plen, salt, slen,
iteration_count, key_length, output );
exit:
mbedtls_md_free( &md_ctx );
return( ret );
#else
int j;
unsigned int i;
unsigned char md1[PSA_HASH_MAX_SIZE];
unsigned char work[PSA_HASH_MAX_SIZE];
const unsigned char md_size = mbedtls_hash_info_get_size( md_alg );
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;

psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
psa_status_t status_destruction = PSA_ERROR_CORRUPTION_DETECTED;
size_t use_len, out_len;
unsigned char *out_p = output;
unsigned char counter[4];
mbedtls_svc_key_id_t psa_hmac_key = MBEDTLS_SVC_KEY_ID_INIT;
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
const psa_algorithm_t alg = PSA_ALG_HMAC( mbedtls_hash_info_psa_from_md( md_alg ) );
const size_t out_size = PSA_MAC_LENGTH( PSA_KEY_TYPE_HMAC, 0, alg );

memset( counter, 0, sizeof( counter ) );
counter[3] = 1;

psa_set_key_usage_flags( &attributes, PSA_KEY_USAGE_SIGN_MESSAGE );
psa_set_key_algorithm( &attributes, alg );
psa_set_key_type( &attributes, PSA_KEY_TYPE_HMAC );

if( key_length == 0 )
return 0;
if( ( status = psa_import_key( &attributes,
password, plen,
&psa_hmac_key ) ) != PSA_SUCCESS )
{
return MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA;
}

#if UINT_MAX > 0xFFFFFFFF
if( iteration_count > 0xFFFFFFFF )
return( MBEDTLS_ERR_PKCS5_BAD_INPUT_DATA );
#endif

while( key_length )
{
status = psa_mac_sign_setup( &operation, psa_hmac_key,
PSA_ALG_HMAC( alg ) );
if( status != PSA_SUCCESS )
goto cleanup;
// U1 ends up in work
if( ( status = psa_mac_update( &operation, salt, slen ) ) != PSA_SUCCESS )
goto cleanup;

if( ( status = psa_mac_update( &operation, counter, sizeof( counter ) ) ) != PSA_SUCCESS )
goto cleanup;

if( ( status = psa_mac_sign_finish( &operation, work, out_size, &out_len ) )
!= PSA_SUCCESS )
goto cleanup;

memcpy( md1, work, out_len );

for( i = 1; i < iteration_count; i++ )
{
// U2 ends up in md1
//
status = psa_mac_sign_setup( &operation, psa_hmac_key,
PSA_ALG_HMAC( alg ) );
if( status != PSA_SUCCESS )
goto cleanup;
if( ( status = psa_mac_update( &operation, md1, md_size ) ) != PSA_SUCCESS )
goto cleanup;
if( ( status = psa_mac_sign_finish( &operation, md1, out_size, &out_len ) ) != PSA_SUCCESS )
goto cleanup;

// U1 xor U2
//
for( j = 0; j < md_size; j++ )
work[j] ^= md1[j];
}

use_len = ( key_length < md_size ) ? key_length : md_size;
memcpy( out_p, work, use_len );

key_length -= (uint32_t) use_len;
out_p += use_len;

for( i = 4; i > 0; i-- )
if( ++counter[i - 1] != 0 )
break;
}

cleanup:
/* Zeroise buffers to clear sensitive data from memory. */
mbedtls_platform_zeroize( work, PSA_HASH_MAX_SIZE );
mbedtls_platform_zeroize( md1, PSA_HASH_MAX_SIZE );
status_destruction = psa_destroy_key( psa_hmac_key );
if( status == PSA_SUCCESS && status_destruction != PSA_SUCCESS )
status = status_destruction;
status_destruction = psa_mac_abort( &operation );
if( status == PSA_SUCCESS && status_destruction != PSA_SUCCESS )
status = status_destruction;

return( mbedtls_md_error_from_psa( status ) );
#endif /* !MBEDTLS_MD_C */
}

#if defined(MBEDTLS_SELF_TEST)

#if !defined(MBEDTLS_SHA1_C)
#if !defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA)
int mbedtls_pkcs5_self_test( int verbose )
{
if( verbose != 0 )
Expand Down Expand Up @@ -362,32 +496,15 @@ static const unsigned char result_key_test_data[MAX_TESTS][32] =

int mbedtls_pkcs5_self_test( int verbose )
{
mbedtls_md_context_t sha1_ctx;
const mbedtls_md_info_t *info_sha1;
int ret, i;
unsigned char key[64];

mbedtls_md_init( &sha1_ctx );

info_sha1 = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 );
if( info_sha1 == NULL )
{
ret = 1;
goto exit;
}

if( ( ret = mbedtls_md_setup( &sha1_ctx, info_sha1, 1 ) ) != 0 )
{
ret = 1;
goto exit;
}

for( i = 0; i < MAX_TESTS; i++ )
{
if( verbose != 0 )
mbedtls_printf( " PBKDF2 (SHA1) #%d: ", i );

ret = mbedtls_pkcs5_pbkdf2_hmac( &sha1_ctx, password_test_data[i],
ret = mbedtls_pkcs5_pbkdf2_hmac_ext( MBEDTLS_MD_SHA1, password_test_data[i],
plen_test_data[i], salt_test_data[i],
slen_test_data[i], it_cnt_test_data[i],
key_len_test_data[i], key );
Expand All @@ -409,11 +526,9 @@ int mbedtls_pkcs5_self_test( int verbose )
mbedtls_printf( "\n" );

exit:
mbedtls_md_free( &sha1_ctx );

return( ret );
}
#endif /* MBEDTLS_SHA1_C */
#endif /* MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA */

#endif /* MBEDTLS_SELF_TEST */

Expand Down
8 changes: 8 additions & 0 deletions tests/include/test/psa_crypto_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,16 @@ psa_key_usage_t mbedtls_test_update_key_usage_flags( psa_key_usage_t usage_flags
} \
while( 0 )

#if !defined(MBEDTLS_MD_C)
#define PSA_INIT_IF_NO_MD( ) PSA_INIT( )
#define PSA_DONE_IF_NO_MD( ) PSA_DONE( )
#endif
#endif /* MBEDTLS_PSA_CRYPTO_C */

#if defined(MBEDTLS_MD_C)
#define PSA_INIT_IF_NO_MD( ) ( (void) 0 )
#define PSA_DONE_IF_NO_MD( ) ( (void) 0 )
#endif
/** \def USE_PSA_INIT
*
* Call this macro to initialize the PSA subsystem if #MBEDTLS_USE_PSA_CRYPTO
Expand Down
2 changes: 0 additions & 2 deletions tests/scripts/all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1210,7 +1210,6 @@ component_test_crypto_full_no_md () {
# Direct dependencies
scripts/config.py unset MBEDTLS_HKDF_C
scripts/config.py unset MBEDTLS_HMAC_DRBG_C
scripts/config.py unset MBEDTLS_PKCS5_C
# Indirect dependencies
scripts/config.py unset MBEDTLS_ECDSA_DETERMINISTIC
make
Expand Down Expand Up @@ -1869,7 +1868,6 @@ component_test_psa_crypto_config_accel_hash_use_psa () {
scripts/config.py unset MBEDTLS_MD_C
scripts/config.py unset MBEDTLS_HKDF_C
scripts/config.py unset MBEDTLS_HMAC_DRBG_C
scripts/config.py unset MBEDTLS_PKCS5_C
scripts/config.py unset MBEDTLS_ECDSA_DETERMINISTIC
scripts/config.py -f include/psa/crypto_config.h unset PSA_WANT_ALG_DETERMINISTIC_ECDSA
# TLS 1.3 currently depends on SHA256_C || SHA384_C
Expand Down
Loading

0 comments on commit 52f83dc

Please sign in to comment.