From 81d31002503f0cdc735176279e913106155666c4 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 30 Jan 2020 20:12:03 +0100 Subject: [PATCH 1/2] Update crypto submodule Previously in d8752858fc22fa3ee4be21bc27e04d8fedaeaea4: * #333: Streamline PSA key type encodings: prepare * #323: Initialise return values to an error Previously in dbcb44202c9b67184a72afd21c7191c17eee352e: * #291: Test MBEDTLS_CTR_DRBG_USE_128_BIT_KEY * #334: Fix some pylint warnings Previously in ceceedb532a4bc9e569f02261ab841b102a8c167: * #348: Bump version to Mbed TLS 2.20.0 and crypto SO version to 4 * #354: Fix incrementing pointer instead of value In this commit: * #349: Fix minor defects found by Coverity * #179: Add option to build SHA-512 without SHA-384 * #327: Implement psa_hash_compute and psa_hash_compare * #330: Streamline PSA key type and curve encodings --- crypto | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crypto b/crypto index 1146b4e06..819799cfc 160000 --- a/crypto +++ b/crypto @@ -1 +1 @@ -Subproject commit 1146b4e06011b69a6437e6b728f2af043a06ec19 +Subproject commit 819799cfc68e4c4381673a8a27af19802c8263f2 From 42459805ce18c7dfe4e3ddefa777210fc0cba411 Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Thu, 19 Dec 2019 13:31:53 +0100 Subject: [PATCH 2/2] USE_PSA_CRYPTO: don't rely on the curve encoding Adapt to the change of encoding of elliptic curve key types in PSA crypto. Before, an EC key type encoded the TLS curve identifier. Now the EC key type only includes an ad hoc curve family identifier, and determining the exact curve requires both the key type and size. This commit moves from the old encoding and old definitions from crypto/include/mbedtls/psa_util.h to the new encoding and definitions from the immediately preceding crypto submodule update. --- include/mbedtls/ssl_internal.h | 3 ++- library/ssl_cli.c | 17 +++++++++-------- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/include/mbedtls/ssl_internal.h b/include/mbedtls/ssl_internal.h index f703da99b..b8c7f0a77 100644 --- a/include/mbedtls/ssl_internal.h +++ b/include/mbedtls/ssl_internal.h @@ -319,7 +319,8 @@ struct mbedtls_ssl_handshake_params mbedtls_ecdh_context ecdh_ctx; /*!< ECDH key exchange */ #if defined(MBEDTLS_USE_PSA_CRYPTO) - psa_ecc_curve_t ecdh_psa_curve; + psa_key_type_t ecdh_psa_type; + uint16_t ecdh_bits; psa_key_handle_t ecdh_psa_privkey; unsigned char ecdh_psa_peerkey[MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH]; size_t ecdh_psa_peerkey_len; diff --git a/library/ssl_cli.c b/library/ssl_cli.c index 1005bd97f..0f6a26b18 100644 --- a/library/ssl_cli.c +++ b/library/ssl_cli.c @@ -2244,6 +2244,7 @@ static int ssl_parse_server_ecdh_params_psa( mbedtls_ssl_context *ssl, unsigned char *end ) { uint16_t tls_id; + size_t ecdh_bits = 0; uint8_t ecpoint_len; mbedtls_ssl_handshake_params *handshake = ssl->handshake; @@ -2264,11 +2265,14 @@ static int ssl_parse_server_ecdh_params_psa( mbedtls_ssl_context *ssl, tls_id |= *(*p)++; /* Convert EC group to PSA key type. */ - if( ( handshake->ecdh_psa_curve = - mbedtls_psa_parse_tls_ecc_group( tls_id ) ) == 0 ) + if( ( handshake->ecdh_psa_type = + mbedtls_psa_parse_tls_ecc_group( tls_id, &ecdh_bits ) ) == 0 ) { return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); } + if( ecdh_bits > 0xffff ) + return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); + handshake->ecdh_bits = (uint16_t) ecdh_bits; /* * Put peer's ECDH public key in the format understood by PSA. @@ -2278,7 +2282,7 @@ static int ssl_parse_server_ecdh_params_psa( mbedtls_ssl_context *ssl, if( (size_t)( end - *p ) < ecpoint_len ) return( MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE ); - if( mbedtls_psa_tls_ecpoint_to_psa_ec( handshake->ecdh_psa_curve, + if( mbedtls_psa_tls_ecpoint_to_psa_ec( *p, ecpoint_len, handshake->ecdh_psa_peerkey, sizeof( handshake->ecdh_psa_peerkey ), @@ -3257,11 +3261,8 @@ static int ssl_write_client_key_exchange( mbedtls_ssl_context *ssl ) key_attributes = psa_key_attributes_init(); psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE ); psa_set_key_algorithm( &key_attributes, PSA_ALG_ECDH ); - psa_set_key_type( &key_attributes, - PSA_KEY_TYPE_ECC_KEY_PAIR( handshake->ecdh_psa_curve ) - ); - psa_set_key_bits( &key_attributes, - PSA_ECC_CURVE_BITS( handshake->ecdh_psa_curve ) ); + psa_set_key_type( &key_attributes, handshake->ecdh_psa_type ); + psa_set_key_bits( &key_attributes, handshake->ecdh_bits ); /* Generate ECDH private key. */ status = psa_generate_key( &key_attributes,