From d6db9be598733f34c3734450bc943b5860b9a807 Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Thu, 10 Jan 2019 05:27:10 -0500 Subject: [PATCH 1/4] Adapt mbedtls_ssl_get_key_exchange_md_tls1_2 to PSA hashing --- include/mbedtls/ssl_internal.h | 1 + library/ssl_tls.c | 65 ++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) diff --git a/include/mbedtls/ssl_internal.h b/include/mbedtls/ssl_internal.h index fced2cbd71..a13feb55cf 100644 --- a/include/mbedtls/ssl_internal.h +++ b/include/mbedtls/ssl_internal.h @@ -765,6 +765,7 @@ int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl, #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ defined(MBEDTLS_SSL_PROTO_TLS1_2) +// The hash buffer must have at least MBEDTLS_MD_MAX_SIZE bytes of length. int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, unsigned char *hash, size_t *hashlen, unsigned char *data, size_t data_len, diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 8fe93141fd..99b99285e4 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -50,6 +50,11 @@ #include +#if defined(MBEDTLS_USE_PSA_CRYPTO) +#include "mbedtls/psa_util.h" +#include "psa/crypto.h" +#endif + #if defined(MBEDTLS_X509_CRT_PARSE_C) #include "mbedtls/oid.h" #endif @@ -9972,6 +9977,64 @@ exit: #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ defined(MBEDTLS_SSL_PROTO_TLS1_2) + +#if defined(MBEDTLS_USE_PSA_CRYPTO) +int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, + unsigned char *hash, size_t *hashlen, + unsigned char *data, size_t data_len, + mbedtls_md_type_t md_alg ) +{ + int ret = 0; + psa_hash_operation_t hash_operation; + psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); + + if( ( ret = psa_hash_setup( &hash_operation, hash_alg ) ) != PSA_SUCCESS ) + { + MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", ret ); + goto exit; + } + + if( ( ret = psa_hash_update( &hash_operation, ssl->handshake->randbytes, 64 ) ) != PSA_SUCCESS ) + { + MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", ret ); + goto exit; + } + + if( ( ret = psa_hash_update( &hash_operation, data, data_len ) ) != PSA_SUCCESS ) + { + MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", ret ); + goto exit; + } + + if( ( ret = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE, hashlen ) ) != PSA_SUCCESS ) + { + MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", ret ); + goto exit; + } + +exit: + if( ret != 0 ) + { + mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, + MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); + switch(ret) + { + case PSA_ERROR_NOT_SUPPORTED: + return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); + case PSA_ERROR_BAD_STATE: // Intentional fallthrough + case PSA_ERROR_BUFFER_TOO_SMALL: + return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); + case PSA_ERROR_INSUFFICIENT_MEMORY: + return( MBEDTLS_ERR_MD_ALLOC_FAILED ); + default: + return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED ); + } + } + return( 0 ); +} + +#else + int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, unsigned char *hash, size_t *hashlen, unsigned char *data, size_t data_len, @@ -10026,6 +10089,8 @@ exit: return( ret ); } +#endif /* MBEDTLS_USE_PSA_CRYPTO */ + #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \ MBEDTLS_SSL_PROTO_TLS1_2 */ From 814feffd15c9d2b9ccb5bb5449d4da356b35f8a4 Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Mon, 14 Jan 2019 04:35:19 -0500 Subject: [PATCH 2/4] Whitespace, logging and documentation fixes Introduce a psa_status_t status to handle return values. Add a debug message with API usage description. --- include/mbedtls/ssl_internal.h | 2 +- library/ssl_tls.c | 34 ++++++++++++++++++++++------------ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/include/mbedtls/ssl_internal.h b/include/mbedtls/ssl_internal.h index a13feb55cf..f1148af329 100644 --- a/include/mbedtls/ssl_internal.h +++ b/include/mbedtls/ssl_internal.h @@ -765,7 +765,7 @@ int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl, #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \ defined(MBEDTLS_SSL_PROTO_TLS1_2) -// The hash buffer must have at least MBEDTLS_MD_MAX_SIZE bytes of length. +/* The hash buffer must have at least MBEDTLS_MD_MAX_SIZE bytes of length. */ int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, unsigned char *hash, size_t *hashlen, unsigned char *data, size_t data_len, diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 99b99285e4..675150d56e 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -9984,44 +9984,51 @@ int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, unsigned char *data, size_t data_len, mbedtls_md_type_t md_alg ) { - int ret = 0; + psa_status_t status; psa_hash_operation_t hash_operation; psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); - if( ( ret = psa_hash_setup( &hash_operation, hash_alg ) ) != PSA_SUCCESS ) + MBEDTLS_SSL_DEBUG_MSG( 2, ( "Perform PSA-based computation of digest \ + of ServerKeyExchange" ) ); + + if( ( status = psa_hash_setup( &hash_operation, + hash_alg ) ) != PSA_SUCCESS ) { - MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", ret ); + MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status ); goto exit; } - if( ( ret = psa_hash_update( &hash_operation, ssl->handshake->randbytes, 64 ) ) != PSA_SUCCESS ) + if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes, + 64 ) ) != PSA_SUCCESS ) { - MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", ret ); + MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); goto exit; } - if( ( ret = psa_hash_update( &hash_operation, data, data_len ) ) != PSA_SUCCESS ) + if( ( status = psa_hash_update( &hash_operation, + data, data_len ) ) != PSA_SUCCESS ) { - MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", ret ); + MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status ); goto exit; } - if( ( ret = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE, hashlen ) ) != PSA_SUCCESS ) + if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE, + hashlen ) ) != PSA_SUCCESS ) { - MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", ret ); + MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status ); goto exit; } exit: - if( ret != 0 ) + if( status != PSA_SUCCESS ) { mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR ); - switch(ret) + switch( status ) { case PSA_ERROR_NOT_SUPPORTED: return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE ); - case PSA_ERROR_BAD_STATE: // Intentional fallthrough + case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */ case PSA_ERROR_BUFFER_TOO_SMALL: return( MBEDTLS_ERR_MD_BAD_INPUT_DATA ); case PSA_ERROR_INSUFFICIENT_MEMORY: @@ -10045,6 +10052,9 @@ int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); *hashlen = mbedtls_md_get_size( md_info ); + MBEDTLS_SSL_DEBUG_MSG( 2, ( "Perform mbedtls-based computation of digest \ + of ServerKeyExchange" ) ); + mbedtls_md_init( &ctx ); /* From e85414edd03e69d606cc0eb93ee46f6a50723da9 Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Tue, 15 Jan 2019 05:23:59 -0500 Subject: [PATCH 3/4] ssl-opt: add a check for PSA computation of digest of ServerKeyExchange --- library/ssl_tls.c | 8 ++++---- tests/ssl-opt.sh | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 675150d56e..3d87bfc9ea 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -9988,8 +9988,8 @@ int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, psa_hash_operation_t hash_operation; psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); - MBEDTLS_SSL_DEBUG_MSG( 2, ( "Perform PSA-based computation of digest \ - of ServerKeyExchange" ) ); + MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based computation of digest " + "of ServerKeyExchange" ) ); if( ( status = psa_hash_setup( &hash_operation, hash_alg ) ) != PSA_SUCCESS ) @@ -10052,8 +10052,8 @@ int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); *hashlen = mbedtls_md_get_size( md_info ); - MBEDTLS_SSL_DEBUG_MSG( 2, ( "Perform mbedtls-based computation of digest \ - of ServerKeyExchange" ) ); + MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform mbedtls-based computation of digest " + "of ServerKeyExchange" ) ); mbedtls_md_init( &ctx ); diff --git a/tests/ssl-opt.sh b/tests/ssl-opt.sh index 2ccecc4b1b..30753b779f 100755 --- a/tests/ssl-opt.sh +++ b/tests/ssl-opt.sh @@ -765,6 +765,7 @@ run_test_psa() { -C "Failed to setup PSA-based cipher context"\ -S "Failed to setup PSA-based cipher context"\ -s "Protocol is TLSv1.2" \ + -c "Perform PSA-based computation of digest of ServerKeyExchange" \ -S "error" \ -C "error" } From 5615dabeefdbb2af7a2285845cf66e6aa01fffb5 Mon Sep 17 00:00:00 2001 From: Andrzej Kurek Date: Wed, 16 Jan 2019 05:26:25 -0500 Subject: [PATCH 4/4] ssl_tls: remove line breaks from a debug message --- library/ssl_tls.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/library/ssl_tls.c b/library/ssl_tls.c index 3d87bfc9ea..a79999ac8b 100644 --- a/library/ssl_tls.c +++ b/library/ssl_tls.c @@ -9988,8 +9988,7 @@ int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, psa_hash_operation_t hash_operation; psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg ); - MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based computation of digest " - "of ServerKeyExchange" ) ); + MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) ); if( ( status = psa_hash_setup( &hash_operation, hash_alg ) ) != PSA_SUCCESS ) @@ -10052,8 +10051,7 @@ int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl, const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg ); *hashlen = mbedtls_md_get_size( md_info ); - MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform mbedtls-based computation of digest " - "of ServerKeyExchange" ) ); + MBEDTLS_SSL_DEBUG_MSG( 1, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) ); mbedtls_md_init( &ctx );