mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2026-07-29 15:27:47 +00:00
Merge pull request #9923 from mpg/use-psa-crypto
Rm dead !USE_PSA_CRYPTO code from the library
This commit is contained in:
@@ -99,20 +99,12 @@
|
||||
#error "MBEDTLS_KEY_EXCHANGE_RSA_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
|
||||
( !defined(PSA_WANT_ALG_JPAKE) || \
|
||||
!defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR_BASIC) || \
|
||||
!defined(PSA_WANT_ECC_SECP_R1_256) )
|
||||
#error "MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
#else /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
|
||||
( !defined(MBEDTLS_ECJPAKE_C) || \
|
||||
!defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) )
|
||||
#error "MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED defined, but not all prerequisites"
|
||||
#endif
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
/* Use of EC J-PAKE in TLS requires SHA-256. */
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
|
||||
@@ -209,11 +201,6 @@
|
||||
#error "MBEDTLS_SSL_ASYNC_PRIVATE defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_TLS_C) && !(defined(MBEDTLS_CIPHER_C) || \
|
||||
defined(MBEDTLS_USE_PSA_CRYPTO))
|
||||
#error "MBEDTLS_SSL_TLS_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
/* TLS 1.2 and 1.3 require SHA-256 or SHA-384 (running handshake hash) */
|
||||
#if defined(MBEDTLS_SSL_TLS_C) && \
|
||||
!(defined(PSA_WANT_ALG_SHA_256) || defined(PSA_WANT_ALG_SHA_384))
|
||||
@@ -288,11 +275,6 @@
|
||||
#error "MBEDTLS_SSL_RENEGOTIATION defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_TICKET_C) && ( !defined(MBEDTLS_CIPHER_C) && \
|
||||
!defined(MBEDTLS_USE_PSA_CRYPTO) )
|
||||
#error "MBEDTLS_SSL_TICKET_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_TICKET_C) && \
|
||||
!( defined(PSA_WANT_ALG_CCM) || defined(PSA_WANT_ALG_GCM) || \
|
||||
defined(PSA_WANT_ALG_CHACHA20_POLY1305) )
|
||||
@@ -315,15 +297,13 @@
|
||||
|
||||
#if defined(MBEDTLS_X509_USE_C) && \
|
||||
(!defined(MBEDTLS_OID_C) || !defined(MBEDTLS_ASN1_PARSE_C) || \
|
||||
!defined(MBEDTLS_PK_PARSE_C) || \
|
||||
( !defined(MBEDTLS_MD_C) && !defined(MBEDTLS_USE_PSA_CRYPTO) ) )
|
||||
!defined(MBEDTLS_PK_PARSE_C))
|
||||
#error "MBEDTLS_X509_USE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_X509_CREATE_C) && \
|
||||
(!defined(MBEDTLS_OID_C) || !defined(MBEDTLS_ASN1_WRITE_C) || \
|
||||
!defined(MBEDTLS_PK_PARSE_C) || \
|
||||
( !defined(MBEDTLS_MD_C) && !defined(MBEDTLS_USE_PSA_CRYPTO) ) )
|
||||
!defined(MBEDTLS_PK_PARSE_C))
|
||||
#error "MBEDTLS_X509_CREATE_C defined, but not all prerequisites"
|
||||
#endif
|
||||
|
||||
|
||||
@@ -867,8 +867,7 @@
|
||||
* Module: library/ssl_ticket.c
|
||||
* Caller:
|
||||
*
|
||||
* Requires: (MBEDTLS_CIPHER_C || MBEDTLS_USE_PSA_CRYPTO) &&
|
||||
* (MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C)
|
||||
* Requires: MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C
|
||||
*/
|
||||
#define MBEDTLS_SSL_TICKET_C
|
||||
|
||||
|
||||
@@ -1572,14 +1572,12 @@ struct mbedtls_ssl_config {
|
||||
|
||||
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
mbedtls_svc_key_id_t MBEDTLS_PRIVATE(psk_opaque); /*!< PSA key slot holding opaque PSK. This field
|
||||
* should only be set via
|
||||
* mbedtls_ssl_conf_psk_opaque().
|
||||
* If either no PSK or a raw PSK have been
|
||||
* configured, this has value \c 0.
|
||||
*/
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
unsigned char *MBEDTLS_PRIVATE(psk); /*!< The raw pre-shared key. This field should
|
||||
* only be set via mbedtls_ssl_conf_psk().
|
||||
* If either no PSK or an opaque PSK
|
||||
@@ -3628,7 +3626,6 @@ int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
|
||||
const unsigned char *psk, size_t psk_len,
|
||||
const unsigned char *psk_identity, size_t psk_identity_len);
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
/**
|
||||
* \brief Configure one or more opaque pre-shared keys (PSKs) and
|
||||
* their identities to be used in PSK-based ciphersuites.
|
||||
@@ -3670,7 +3667,6 @@ int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
|
||||
mbedtls_svc_key_id_t psk,
|
||||
const unsigned char *psk_identity,
|
||||
size_t psk_identity_len);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
/**
|
||||
* \brief Set the pre-shared Key (PSK) for the current handshake.
|
||||
@@ -3691,7 +3687,6 @@ int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
|
||||
int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
|
||||
const unsigned char *psk, size_t psk_len);
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
/**
|
||||
* \brief Set an opaque pre-shared Key (PSK) for the current handshake.
|
||||
*
|
||||
@@ -3714,7 +3709,6 @@ int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
|
||||
*/
|
||||
int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
|
||||
mbedtls_svc_key_id_t psk);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#if defined(MBEDTLS_SSL_SRV_C)
|
||||
/**
|
||||
|
||||
@@ -365,8 +365,7 @@ typedef enum {
|
||||
* - by TLS 1.3 to do ECDHE or FFDHE.
|
||||
* The following macros can be used to guard their declaration and use.
|
||||
*/
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) && \
|
||||
defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
|
||||
#define MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_1_2_ENABLED
|
||||
#endif
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_1_2_ENABLED) || \
|
||||
|
||||
@@ -15,11 +15,6 @@
|
||||
|
||||
#include "mbedtls/ssl.h"
|
||||
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
#include "mbedtls/threading.h"
|
||||
#endif
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
/**
|
||||
* \name SECTION: Module settings
|
||||
@@ -42,23 +37,14 @@ extern "C" {
|
||||
* \brief Context for the default cookie functions.
|
||||
*/
|
||||
typedef struct mbedtls_ssl_cookie_ctx {
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
mbedtls_svc_key_id_t MBEDTLS_PRIVATE(psa_hmac_key); /*!< key id for the HMAC portion */
|
||||
psa_algorithm_t MBEDTLS_PRIVATE(psa_hmac_alg); /*!< key algorithm for the HMAC portion */
|
||||
#else
|
||||
mbedtls_md_context_t MBEDTLS_PRIVATE(hmac_ctx); /*!< context for the HMAC portion */
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
#if !defined(MBEDTLS_HAVE_TIME)
|
||||
unsigned long MBEDTLS_PRIVATE(serial); /*!< serial number for expiration */
|
||||
#endif
|
||||
unsigned long MBEDTLS_PRIVATE(timeout); /*!< timeout delay, in seconds if HAVE_TIME,
|
||||
or in number of tickets issued */
|
||||
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
mbedtls_threading_mutex_t MBEDTLS_PRIVATE(mutex);
|
||||
#endif
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO */
|
||||
} mbedtls_ssl_cookie_ctx;
|
||||
|
||||
/**
|
||||
|
||||
@@ -26,9 +26,7 @@
|
||||
#include "mbedtls/platform_time.h"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#include "psa/crypto.h"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
#include "mbedtls/threading.h"
|
||||
@@ -54,14 +52,10 @@ typedef struct mbedtls_ssl_ticket_key {
|
||||
* tickets created under that key.
|
||||
*/
|
||||
uint32_t MBEDTLS_PRIVATE(lifetime);
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
mbedtls_cipher_context_t MBEDTLS_PRIVATE(ctx); /*!< context for auth enc/decryption */
|
||||
#else
|
||||
mbedtls_svc_key_id_t MBEDTLS_PRIVATE(key); /*!< key used for auth enc/decryption */
|
||||
psa_algorithm_t MBEDTLS_PRIVATE(alg); /*!< algorithm of auth enc/decryption */
|
||||
psa_key_type_t MBEDTLS_PRIVATE(key_type); /*!< key type */
|
||||
size_t MBEDTLS_PRIVATE(key_bits); /*!< key length in bits */
|
||||
#endif
|
||||
}
|
||||
mbedtls_ssl_ticket_key;
|
||||
|
||||
|
||||
@@ -16,9 +16,7 @@
|
||||
#include "mbedtls/ssl_ciphersuites.h"
|
||||
#include "mbedtls/ssl.h"
|
||||
#include "ssl_misc.h"
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#include "mbedtls/psa_util.h"
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
|
||||
@@ -1543,7 +1541,6 @@ int mbedtls_ssl_get_ciphersuite_id(const char *ciphersuite_name)
|
||||
|
||||
size_t mbedtls_ssl_ciphersuite_get_cipher_key_bitlen(const mbedtls_ssl_ciphersuite_t *info)
|
||||
{
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
psa_key_type_t key_type;
|
||||
psa_algorithm_t alg;
|
||||
@@ -1558,12 +1555,6 @@ size_t mbedtls_ssl_ciphersuite_get_cipher_key_bitlen(const mbedtls_ssl_ciphersui
|
||||
}
|
||||
|
||||
return key_bits;
|
||||
#else
|
||||
const mbedtls_cipher_info_t * const cipher_info =
|
||||
mbedtls_cipher_info_from_type((mbedtls_cipher_type_t) info->cipher);
|
||||
|
||||
return mbedtls_cipher_info_get_key_bitlen(cipher_info);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_PK_C)
|
||||
@@ -1587,7 +1578,6 @@ mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg(const mbedtls_ssl_ciphe
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_algorithm_t mbedtls_ssl_get_ciphersuite_sig_pk_psa_alg(const mbedtls_ssl_ciphersuite_t *info)
|
||||
{
|
||||
switch (info->key_exchange) {
|
||||
@@ -1628,7 +1618,6 @@ psa_key_usage_t mbedtls_ssl_get_ciphersuite_sig_pk_psa_usage(const mbedtls_ssl_c
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_alg(const mbedtls_ssl_ciphersuite_t *info)
|
||||
{
|
||||
|
||||
@@ -14,10 +14,8 @@
|
||||
|
||||
#if defined(MBEDTLS_PK_C)
|
||||
mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_pk_alg(const mbedtls_ssl_ciphersuite_t *info);
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_algorithm_t mbedtls_ssl_get_ciphersuite_sig_pk_psa_alg(const mbedtls_ssl_ciphersuite_t *info);
|
||||
psa_key_usage_t mbedtls_ssl_get_ciphersuite_sig_pk_psa_usage(const mbedtls_ssl_ciphersuite_t *info);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
mbedtls_pk_type_t mbedtls_ssl_get_ciphersuite_sig_alg(const mbedtls_ssl_ciphersuite_t *info);
|
||||
#endif /* MBEDTLS_PK_C */
|
||||
|
||||
|
||||
@@ -22,7 +22,6 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#include "mbedtls/psa_util.h"
|
||||
/* Define a local translating function to save code size by not using too many
|
||||
* arguments in each translating place. */
|
||||
@@ -33,7 +32,6 @@ static int local_err_translation(psa_status_t status)
|
||||
psa_generic_status_to_mbedtls);
|
||||
}
|
||||
#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* If DTLS is in use, then at least one of SHA-256 or SHA-384 is
|
||||
@@ -59,21 +57,12 @@ static int local_err_translation(psa_status_t status)
|
||||
|
||||
void mbedtls_ssl_cookie_init(mbedtls_ssl_cookie_ctx *ctx)
|
||||
{
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
ctx->psa_hmac_key = MBEDTLS_SVC_KEY_ID_INIT;
|
||||
#else
|
||||
mbedtls_md_init(&ctx->hmac_ctx);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
#if !defined(MBEDTLS_HAVE_TIME)
|
||||
ctx->serial = 0;
|
||||
#endif
|
||||
ctx->timeout = MBEDTLS_SSL_COOKIE_TIMEOUT;
|
||||
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
mbedtls_mutex_init(&ctx->mutex);
|
||||
#endif
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO */
|
||||
}
|
||||
|
||||
void mbedtls_ssl_cookie_set_timeout(mbedtls_ssl_cookie_ctx *ctx, unsigned long delay)
|
||||
@@ -87,15 +76,7 @@ void mbedtls_ssl_cookie_free(mbedtls_ssl_cookie_ctx *ctx)
|
||||
return;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_destroy_key(ctx->psa_hmac_key);
|
||||
#else
|
||||
mbedtls_md_free(&ctx->hmac_ctx);
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
mbedtls_mutex_free(&ctx->mutex);
|
||||
#endif
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_ssl_cookie_ctx));
|
||||
}
|
||||
@@ -104,7 +85,6 @@ int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx,
|
||||
int (*f_rng)(void *, unsigned char *, size_t),
|
||||
void *p_rng)
|
||||
{
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
psa_algorithm_t alg;
|
||||
@@ -130,57 +110,10 @@ int mbedtls_ssl_cookie_setup(mbedtls_ssl_cookie_ctx *ctx,
|
||||
&ctx->psa_hmac_key)) != PSA_SUCCESS) {
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
#else
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
unsigned char key[COOKIE_MD_OUTLEN];
|
||||
|
||||
if ((ret = f_rng(p_rng, key, sizeof(key))) != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = mbedtls_md_setup(&ctx->hmac_ctx, mbedtls_md_info_from_type(COOKIE_MD), 1);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = mbedtls_md_hmac_starts(&ctx->hmac_ctx, key, sizeof(key));
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
mbedtls_platform_zeroize(key, sizeof(key));
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
/*
|
||||
* Generate the HMAC part of a cookie
|
||||
*/
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
static int ssl_cookie_hmac(mbedtls_md_context_t *hmac_ctx,
|
||||
const unsigned char time[4],
|
||||
unsigned char **p, unsigned char *end,
|
||||
const unsigned char *cli_id, size_t cli_id_len)
|
||||
{
|
||||
unsigned char hmac_out[COOKIE_MD_OUTLEN];
|
||||
|
||||
MBEDTLS_SSL_CHK_BUF_PTR(*p, end, COOKIE_HMAC_LEN);
|
||||
|
||||
if (mbedtls_md_hmac_reset(hmac_ctx) != 0 ||
|
||||
mbedtls_md_hmac_update(hmac_ctx, time, 4) != 0 ||
|
||||
mbedtls_md_hmac_update(hmac_ctx, cli_id, cli_id_len) != 0 ||
|
||||
mbedtls_md_hmac_finish(hmac_ctx, hmac_out) != 0) {
|
||||
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
memcpy(*p, hmac_out, COOKIE_HMAC_LEN);
|
||||
*p += COOKIE_HMAC_LEN;
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
/*
|
||||
* Generate cookie for DTLS ClientHello verification
|
||||
@@ -189,11 +122,9 @@ int mbedtls_ssl_cookie_write(void *p_ctx,
|
||||
unsigned char **p, unsigned char *end,
|
||||
const unsigned char *cli_id, size_t cli_id_len)
|
||||
{
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
size_t sign_mac_length = 0;
|
||||
#endif
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
|
||||
unsigned long t;
|
||||
@@ -213,7 +144,6 @@ int mbedtls_ssl_cookie_write(void *p_ctx,
|
||||
MBEDTLS_PUT_UINT32_BE(t, *p, 0);
|
||||
*p += 4;
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
status = psa_mac_sign_setup(&operation, ctx->psa_hmac_key,
|
||||
ctx->psa_hmac_alg);
|
||||
if (status != PSA_SUCCESS) {
|
||||
@@ -243,31 +173,12 @@ int mbedtls_ssl_cookie_write(void *p_ctx,
|
||||
*p += COOKIE_HMAC_LEN;
|
||||
|
||||
ret = 0;
|
||||
#else
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
|
||||
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret);
|
||||
}
|
||||
#endif
|
||||
|
||||
ret = ssl_cookie_hmac(&ctx->hmac_ctx, *p - 4,
|
||||
p, end, cli_id, cli_id_len);
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
|
||||
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR,
|
||||
MBEDTLS_ERR_THREADING_MUTEX_ERROR);
|
||||
}
|
||||
#endif
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
exit:
|
||||
status = psa_mac_abort(&operation);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -278,13 +189,8 @@ int mbedtls_ssl_cookie_check(void *p_ctx,
|
||||
const unsigned char *cookie, size_t cookie_len,
|
||||
const unsigned char *cli_id, size_t cli_id_len)
|
||||
{
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
#else
|
||||
unsigned char ref_hmac[COOKIE_HMAC_LEN];
|
||||
unsigned char *p = ref_hmac;
|
||||
#endif
|
||||
int ret = 0;
|
||||
mbedtls_ssl_cookie_ctx *ctx = (mbedtls_ssl_cookie_ctx *) p_ctx;
|
||||
unsigned long cur_time, cookie_time;
|
||||
@@ -297,7 +203,6 @@ int mbedtls_ssl_cookie_check(void *p_ctx,
|
||||
return -1;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
status = psa_mac_verify_setup(&operation, ctx->psa_hmac_key,
|
||||
ctx->psa_hmac_alg);
|
||||
if (status != PSA_SUCCESS) {
|
||||
@@ -326,35 +231,6 @@ int mbedtls_ssl_cookie_check(void *p_ctx,
|
||||
}
|
||||
|
||||
ret = 0;
|
||||
#else
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
|
||||
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR, ret);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (ssl_cookie_hmac(&ctx->hmac_ctx, cookie,
|
||||
&p, p + sizeof(ref_hmac),
|
||||
cli_id, cli_id_len) != 0) {
|
||||
ret = -1;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
|
||||
ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_SSL_INTERNAL_ERROR,
|
||||
MBEDTLS_ERR_THREADING_MUTEX_ERROR);
|
||||
}
|
||||
#endif
|
||||
|
||||
if (ret != 0) {
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (mbedtls_ct_memcmp(cookie + 4, ref_hmac, sizeof(ref_hmac)) != 0) {
|
||||
ret = -1;
|
||||
goto exit;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#if defined(MBEDTLS_HAVE_TIME)
|
||||
cur_time = (unsigned long) mbedtls_time(NULL);
|
||||
@@ -370,14 +246,10 @@ int mbedtls_ssl_cookie_check(void *p_ctx,
|
||||
}
|
||||
|
||||
exit:
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
status = psa_mac_abort(&operation);
|
||||
if (status != PSA_SUCCESS) {
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
#else
|
||||
mbedtls_platform_zeroize(ref_hmac, sizeof(ref_hmac));
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_COOKIE_C */
|
||||
|
||||
+5
-96
@@ -18,10 +18,8 @@
|
||||
#include "mbedtls/ssl.h"
|
||||
#include "mbedtls/cipher.h"
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
||||
#include "psa/crypto.h"
|
||||
#include "psa_util_internal.h"
|
||||
#endif
|
||||
|
||||
#if defined(PSA_WANT_ALG_MD5)
|
||||
#include "mbedtls/md5.h"
|
||||
@@ -39,11 +37,6 @@
|
||||
#include "mbedtls/sha512.h"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
|
||||
!defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#include "mbedtls/ecjpake.h"
|
||||
#endif
|
||||
|
||||
#include "mbedtls/pk.h"
|
||||
#include "ssl_ciphersuites_internal.h"
|
||||
#include "x509_internal.h"
|
||||
@@ -773,12 +766,6 @@ struct mbedtls_ssl_handshake_params {
|
||||
mbedtls_dhm_context dhm_ctx; /*!< DHM key exchange */
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
|
||||
mbedtls_ecdh_context ecdh_ctx; /*!< ECDH key exchange */
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO &&
|
||||
MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED */
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED)
|
||||
psa_key_type_t xxdh_psa_type;
|
||||
size_t xxdh_psa_bits;
|
||||
@@ -789,13 +776,9 @@ struct mbedtls_ssl_handshake_params {
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_SOME_XXDH_PSA_ANY_ENABLED */
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_pake_operation_t psa_pake_ctx; /*!< EC J-PAKE key exchange */
|
||||
mbedtls_svc_key_id_t psa_pake_password;
|
||||
uint8_t psa_pake_ctx_is_ok;
|
||||
#else
|
||||
mbedtls_ecjpake_context ecjpake_ctx; /*!< EC J-PAKE key exchange */
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
#if defined(MBEDTLS_SSL_CLI_C)
|
||||
unsigned char *ecjpake_cache; /*!< Cache for ClientHello ext */
|
||||
size_t ecjpake_cache_len; /*!< Length of cached data */
|
||||
@@ -809,13 +792,8 @@ struct mbedtls_ssl_handshake_params {
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
mbedtls_svc_key_id_t psk_opaque; /*!< Opaque PSK from the callback */
|
||||
uint8_t psk_opaque_is_internal;
|
||||
#else
|
||||
unsigned char *psk; /*!< PSK from the callback */
|
||||
size_t psk_len; /*!< Length of PSK from callback */
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
uint16_t selected_identity;
|
||||
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
|
||||
|
||||
@@ -923,18 +901,10 @@ struct mbedtls_ssl_handshake_params {
|
||||
* Checksum contexts
|
||||
*/
|
||||
#if defined(PSA_WANT_ALG_SHA_256)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_hash_operation_t fin_sha256_psa;
|
||||
#else
|
||||
mbedtls_md_context_t fin_sha256;
|
||||
#endif
|
||||
#endif
|
||||
#if defined(PSA_WANT_ALG_SHA_384)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_hash_operation_t fin_sha384_psa;
|
||||
#else
|
||||
mbedtls_md_context_t fin_sha384;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
||||
@@ -1129,14 +1099,9 @@ struct mbedtls_ssl_transform {
|
||||
|
||||
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
mbedtls_svc_key_id_t psa_mac_enc; /*!< MAC (encryption) */
|
||||
mbedtls_svc_key_id_t psa_mac_dec; /*!< MAC (decryption) */
|
||||
psa_algorithm_t psa_mac_alg; /*!< psa MAC algorithm */
|
||||
#else
|
||||
mbedtls_md_context_t md_ctx_enc; /*!< MAC (encryption) */
|
||||
mbedtls_md_context_t md_ctx_dec; /*!< MAC (decryption) */
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
||||
int encrypt_then_mac; /*!< flag for EtM activation */
|
||||
@@ -1146,14 +1111,9 @@ struct mbedtls_ssl_transform {
|
||||
|
||||
mbedtls_ssl_protocol_version tls_version;
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
mbedtls_svc_key_id_t psa_key_enc; /*!< psa encryption key */
|
||||
mbedtls_svc_key_id_t psa_key_dec; /*!< psa decryption key */
|
||||
psa_algorithm_t psa_alg; /*!< psa algorithm */
|
||||
#else
|
||||
mbedtls_cipher_context_t cipher_ctx_enc; /*!< encryption context */
|
||||
mbedtls_cipher_context_t cipher_ctx_dec; /*!< decryption context */
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
||||
uint8_t in_cid_len;
|
||||
@@ -1498,20 +1458,11 @@ int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
|
||||
unsigned hs_type,
|
||||
size_t total_hs_len);
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl,
|
||||
mbedtls_key_exchange_type_t key_ex);
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO */
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
|
||||
|
||||
#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
|
||||
#if defined(MBEDTLS_SSL_CLI_C) || defined(MBEDTLS_SSL_SRV_C)
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf);
|
||||
#endif
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
/**
|
||||
* Get the first defined opaque PSK by order of precedence:
|
||||
* 1. handshake PSK set by \c mbedtls_ssl_set_hs_psk_opaque() in the PSK
|
||||
@@ -1532,31 +1483,6 @@ static inline mbedtls_svc_key_id_t mbedtls_ssl_get_opaque_psk(
|
||||
|
||||
return MBEDTLS_SVC_KEY_ID_INIT;
|
||||
}
|
||||
#else
|
||||
/**
|
||||
* Get the first defined PSK by order of precedence:
|
||||
* 1. handshake PSK set by \c mbedtls_ssl_set_hs_psk() in the PSK callback
|
||||
* 2. static PSK configured by \c mbedtls_ssl_conf_psk()
|
||||
* Return a code and update the pair (PSK, PSK length) passed to this function
|
||||
*/
|
||||
static inline int mbedtls_ssl_get_psk(const mbedtls_ssl_context *ssl,
|
||||
const unsigned char **psk, size_t *psk_len)
|
||||
{
|
||||
if (ssl->handshake->psk != NULL && ssl->handshake->psk_len > 0) {
|
||||
*psk = ssl->handshake->psk;
|
||||
*psk_len = ssl->handshake->psk_len;
|
||||
} else if (ssl->conf->psk != NULL && ssl->conf->psk_len > 0) {
|
||||
*psk = ssl->conf->psk;
|
||||
*psk_len = ssl->conf->psk_len;
|
||||
} else {
|
||||
*psk = NULL;
|
||||
*psk_len = 0;
|
||||
return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
|
||||
|
||||
@@ -2603,7 +2529,6 @@ static inline int mbedtls_ssl_sig_alg_is_supported(
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
|
||||
/* Corresponding PSA algorithm for MBEDTLS_CIPHER_NULL.
|
||||
* Same value is used for PSA_ALG_CATEGORY_CIPHER, hence it is
|
||||
* guaranteed to not be a valid PSA algorithm identifier.
|
||||
@@ -2664,10 +2589,8 @@ static inline MBEDTLS_DEPRECATED int psa_ssl_status_to_mbedtls(psa_status_t stat
|
||||
}
|
||||
}
|
||||
#endif /* !MBEDTLS_DEPRECATED_REMOVED */
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
|
||||
defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
||||
|
||||
typedef enum {
|
||||
MBEDTLS_ECJPAKE_ROUND_ONE,
|
||||
@@ -2710,7 +2633,7 @@ int mbedtls_psa_ecjpake_write_round(
|
||||
size_t len, size_t *olen,
|
||||
mbedtls_ecjpake_rounds_t round);
|
||||
|
||||
#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
|
||||
|
||||
/**
|
||||
* \brief TLS record protection modes
|
||||
@@ -2941,12 +2864,9 @@ int mbedtls_ssl_tls13_finalize_client_hello(mbedtls_ssl_context *ssl);
|
||||
* max_data_len. In particular, this function always reads exactly \p
|
||||
* max_data_len bytes from \p data.
|
||||
*
|
||||
* \param ctx The HMAC context. It must have keys configured
|
||||
* with mbedtls_md_hmac_starts() and use one of the
|
||||
* following hashes: SHA-384, SHA-256, SHA-1 or MD-5.
|
||||
* It is reset using mbedtls_md_hmac_reset() after
|
||||
* the computation is complete to prepare for the
|
||||
* next computation.
|
||||
* \param key The HMAC key.
|
||||
* \param mac_alg The hash algorithm.
|
||||
* Must be one of SHA-384, SHA-256, SHA-1 or MD-5.
|
||||
* \param add_data The first part of the message whose HMAC is being
|
||||
* calculated. This must point to a readable buffer
|
||||
* of \p add_data_len bytes.
|
||||
@@ -2969,7 +2889,6 @@ int mbedtls_ssl_tls13_finalize_client_hello(mbedtls_ssl_context *ssl);
|
||||
* \retval #MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED
|
||||
* The hardware accelerator failed.
|
||||
*/
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
int mbedtls_ct_hmac(mbedtls_svc_key_id_t key,
|
||||
psa_algorithm_t mac_alg,
|
||||
const unsigned char *add_data,
|
||||
@@ -2979,16 +2898,6 @@ int mbedtls_ct_hmac(mbedtls_svc_key_id_t key,
|
||||
size_t min_data_len,
|
||||
size_t max_data_len,
|
||||
unsigned char *output);
|
||||
#else
|
||||
int mbedtls_ct_hmac(mbedtls_md_context_t *ctx,
|
||||
const unsigned char *add_data,
|
||||
size_t add_data_len,
|
||||
const unsigned char *data,
|
||||
size_t data_len_secret,
|
||||
size_t min_data_len,
|
||||
size_t max_data_len,
|
||||
unsigned char *output);
|
||||
#endif /* defined(MBEDTLS_USE_PSA_CRYPTO) */
|
||||
#endif /* MBEDTLS_TEST_HOOKS && defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC) */
|
||||
|
||||
#endif /* ssl_misc.h */
|
||||
|
||||
@@ -26,16 +26,13 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#include "psa_util_internal.h"
|
||||
#include "psa/crypto.h"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
#include "mbedtls/oid.h"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
/* Define a local translating function to save code size by not using too many
|
||||
* arguments in each translating place. */
|
||||
static int local_err_translation(psa_status_t status)
|
||||
@@ -45,11 +42,9 @@ static int local_err_translation(psa_status_t status)
|
||||
psa_generic_status_to_mbedtls);
|
||||
}
|
||||
#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
|
||||
#if defined(PSA_WANT_ALG_SHA_384)
|
||||
#define MAX_HASH_BLOCK_LENGTH PSA_HASH_BLOCK_LENGTH(PSA_ALG_SHA_384)
|
||||
@@ -179,99 +174,6 @@ cleanup:
|
||||
|
||||
#undef MAX_HASH_BLOCK_LENGTH
|
||||
|
||||
#else
|
||||
MBEDTLS_STATIC_TESTABLE
|
||||
int mbedtls_ct_hmac(mbedtls_md_context_t *ctx,
|
||||
const unsigned char *add_data,
|
||||
size_t add_data_len,
|
||||
const unsigned char *data,
|
||||
size_t data_len_secret,
|
||||
size_t min_data_len,
|
||||
size_t max_data_len,
|
||||
unsigned char *output)
|
||||
{
|
||||
/*
|
||||
* This function breaks the HMAC abstraction and uses the md_clone()
|
||||
* extension to the MD API in order to get constant-flow behaviour.
|
||||
*
|
||||
* HMAC(msg) is defined as HASH(okey + HASH(ikey + msg)) where + means
|
||||
* concatenation, and okey/ikey are the XOR of the key with some fixed bit
|
||||
* patterns (see RFC 2104, sec. 2), which are stored in ctx->hmac_ctx.
|
||||
*
|
||||
* We'll first compute inner_hash = HASH(ikey + msg) by hashing up to
|
||||
* minlen, then cloning the context, and for each byte up to maxlen
|
||||
* finishing up the hash computation, keeping only the correct result.
|
||||
*
|
||||
* Then we only need to compute HASH(okey + inner_hash) and we're done.
|
||||
*/
|
||||
const mbedtls_md_type_t md_alg = mbedtls_md_get_type(ctx->md_info);
|
||||
/* TLS 1.2 only supports SHA-384, SHA-256, SHA-1, MD-5,
|
||||
* all of which have the same block size except SHA-384. */
|
||||
const size_t block_size = md_alg == MBEDTLS_MD_SHA384 ? 128 : 64;
|
||||
const unsigned char * const ikey = ctx->hmac_ctx;
|
||||
const unsigned char * const okey = ikey + block_size;
|
||||
const size_t hash_size = mbedtls_md_get_size(ctx->md_info);
|
||||
|
||||
unsigned char aux_out[MBEDTLS_MD_MAX_SIZE];
|
||||
mbedtls_md_context_t aux;
|
||||
size_t offset;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
mbedtls_md_init(&aux);
|
||||
|
||||
#define MD_CHK(func_call) \
|
||||
do { \
|
||||
ret = (func_call); \
|
||||
if (ret != 0) \
|
||||
goto cleanup; \
|
||||
} while (0)
|
||||
|
||||
MD_CHK(mbedtls_md_setup(&aux, ctx->md_info, 0));
|
||||
|
||||
/* After hmac_start() of hmac_reset(), ikey has already been hashed,
|
||||
* so we can start directly with the message */
|
||||
MD_CHK(mbedtls_md_update(ctx, add_data, add_data_len));
|
||||
MD_CHK(mbedtls_md_update(ctx, data, min_data_len));
|
||||
|
||||
/* Fill the hash buffer in advance with something that is
|
||||
* not a valid hash (barring an attack on the hash and
|
||||
* deliberately-crafted input), in case the caller doesn't
|
||||
* check the return status properly. */
|
||||
memset(output, '!', hash_size);
|
||||
|
||||
/* For each possible length, compute the hash up to that point */
|
||||
for (offset = min_data_len; offset <= max_data_len; offset++) {
|
||||
MD_CHK(mbedtls_md_clone(&aux, ctx));
|
||||
MD_CHK(mbedtls_md_finish(&aux, aux_out));
|
||||
/* Keep only the correct inner_hash in the output buffer */
|
||||
mbedtls_ct_memcpy_if(mbedtls_ct_uint_eq(offset, data_len_secret),
|
||||
output, aux_out, NULL, hash_size);
|
||||
|
||||
if (offset < max_data_len) {
|
||||
MD_CHK(mbedtls_md_update(ctx, data + offset, 1));
|
||||
}
|
||||
}
|
||||
|
||||
/* The context needs to finish() before it starts() again */
|
||||
MD_CHK(mbedtls_md_finish(ctx, aux_out));
|
||||
|
||||
/* Now compute HASH(okey + inner_hash) */
|
||||
MD_CHK(mbedtls_md_starts(ctx));
|
||||
MD_CHK(mbedtls_md_update(ctx, okey, block_size));
|
||||
MD_CHK(mbedtls_md_update(ctx, output, hash_size));
|
||||
MD_CHK(mbedtls_md_finish(ctx, output));
|
||||
|
||||
/* Done, get ready for next time */
|
||||
MD_CHK(mbedtls_md_hmac_reset(ctx));
|
||||
|
||||
#undef MD_CHK
|
||||
|
||||
cleanup:
|
||||
mbedtls_md_free(&aux);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
|
||||
|
||||
@@ -1039,17 +941,14 @@ int mbedtls_ssl_encrypt_buf(mbedtls_ssl_context *ssl,
|
||||
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
||||
unsigned char mac[MBEDTLS_SSL_MAC_ADD];
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
size_t sign_mac_length = 0;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
ssl_extract_add_data_from_record(add_data, &add_data_len, rec,
|
||||
transform->tls_version,
|
||||
transform->taglen);
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
status = psa_mac_sign_setup(&operation, transform->psa_mac_enc,
|
||||
transform->psa_mac_alg);
|
||||
if (status != PSA_SUCCESS) {
|
||||
@@ -1071,25 +970,6 @@ int mbedtls_ssl_encrypt_buf(mbedtls_ssl_context *ssl,
|
||||
if (status != PSA_SUCCESS) {
|
||||
goto hmac_failed_etm_disabled;
|
||||
}
|
||||
#else
|
||||
ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, add_data,
|
||||
add_data_len);
|
||||
if (ret != 0) {
|
||||
goto hmac_failed_etm_disabled;
|
||||
}
|
||||
ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, data, rec->data_len);
|
||||
if (ret != 0) {
|
||||
goto hmac_failed_etm_disabled;
|
||||
}
|
||||
ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac);
|
||||
if (ret != 0) {
|
||||
goto hmac_failed_etm_disabled;
|
||||
}
|
||||
ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc);
|
||||
if (ret != 0) {
|
||||
goto hmac_failed_etm_disabled;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
memcpy(data + rec->data_len, mac, transform->maclen);
|
||||
#endif
|
||||
@@ -1103,13 +983,11 @@ int mbedtls_ssl_encrypt_buf(mbedtls_ssl_context *ssl,
|
||||
|
||||
hmac_failed_etm_disabled:
|
||||
mbedtls_platform_zeroize(mac, transform->maclen);
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
status = psa_mac_abort(&operation);
|
||||
if (ret == 0 && status != PSA_SUCCESS) {
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_hmac_xxx", ret);
|
||||
return ret;
|
||||
@@ -1138,9 +1016,7 @@ hmac_failed_etm_disabled:
|
||||
size_t dynamic_iv_len;
|
||||
int dynamic_iv_is_explicit =
|
||||
ssl_transform_aead_dynamic_iv_is_explicit(transform);
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
/* Check that there's space for the authentication tag. */
|
||||
@@ -1192,7 +1068,6 @@ hmac_failed_etm_disabled:
|
||||
/*
|
||||
* Encrypt and authenticate
|
||||
*/
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
status = psa_aead_encrypt(transform->psa_key_enc,
|
||||
transform->psa_alg,
|
||||
iv, transform->ivlen,
|
||||
@@ -1206,18 +1081,6 @@ hmac_failed_etm_disabled:
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_encrypt_buf", ret);
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
if ((ret = mbedtls_cipher_auth_encrypt_ext(&transform->cipher_ctx_enc,
|
||||
iv, transform->ivlen,
|
||||
add_data, add_data_len,
|
||||
data, rec->data_len, /* src */
|
||||
data, rec->buf_len - (size_t) (data - rec->buf), /* dst */
|
||||
&rec->data_len,
|
||||
transform->taglen)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_encrypt_ext", ret);
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
MBEDTLS_SSL_DEBUG_BUF(4, "after encrypt: tag",
|
||||
data + rec->data_len - transform->taglen,
|
||||
@@ -1248,11 +1111,9 @@ hmac_failed_etm_disabled:
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t padlen, i;
|
||||
size_t olen;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
size_t part_len;
|
||||
psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
/* Currently we're always using minimal padding
|
||||
* (up to 255 bytes would be allowed). */
|
||||
@@ -1307,7 +1168,6 @@ hmac_failed_etm_disabled:
|
||||
rec->data_len, transform->ivlen,
|
||||
padlen + 1));
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
status = psa_cipher_encrypt_setup(&cipher_op,
|
||||
transform->psa_key_enc, transform->psa_alg);
|
||||
|
||||
@@ -1349,16 +1209,6 @@ hmac_failed_etm_disabled:
|
||||
}
|
||||
|
||||
olen += part_len;
|
||||
#else
|
||||
if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_enc,
|
||||
transform->iv_enc,
|
||||
transform->ivlen,
|
||||
data, rec->data_len,
|
||||
data, &olen)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
if (rec->data_len != olen) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
||||
@@ -1372,10 +1222,8 @@ hmac_failed_etm_disabled:
|
||||
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
||||
if (auth_done == 0) {
|
||||
unsigned char mac[MBEDTLS_SSL_MAC_ADD];
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
||||
size_t sign_mac_length = 0;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
/* MAC(MAC_write_key, add_data, IV, ENC(content + padding + padding_length))
|
||||
*/
|
||||
@@ -1392,7 +1240,6 @@ hmac_failed_etm_disabled:
|
||||
MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac"));
|
||||
MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data,
|
||||
add_data_len);
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
status = psa_mac_sign_setup(&operation, transform->psa_mac_enc,
|
||||
transform->psa_mac_alg);
|
||||
if (status != PSA_SUCCESS) {
|
||||
@@ -1414,27 +1261,6 @@ hmac_failed_etm_disabled:
|
||||
if (status != PSA_SUCCESS) {
|
||||
goto hmac_failed_etm_enabled;
|
||||
}
|
||||
#else
|
||||
|
||||
ret = mbedtls_md_hmac_update(&transform->md_ctx_enc, add_data,
|
||||
add_data_len);
|
||||
if (ret != 0) {
|
||||
goto hmac_failed_etm_enabled;
|
||||
}
|
||||
ret = mbedtls_md_hmac_update(&transform->md_ctx_enc,
|
||||
data, rec->data_len);
|
||||
if (ret != 0) {
|
||||
goto hmac_failed_etm_enabled;
|
||||
}
|
||||
ret = mbedtls_md_hmac_finish(&transform->md_ctx_enc, mac);
|
||||
if (ret != 0) {
|
||||
goto hmac_failed_etm_enabled;
|
||||
}
|
||||
ret = mbedtls_md_hmac_reset(&transform->md_ctx_enc);
|
||||
if (ret != 0) {
|
||||
goto hmac_failed_etm_enabled;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
memcpy(data + rec->data_len, mac, transform->maclen);
|
||||
|
||||
@@ -1444,13 +1270,11 @@ hmac_failed_etm_disabled:
|
||||
|
||||
hmac_failed_etm_enabled:
|
||||
mbedtls_platform_zeroize(mac, transform->maclen);
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
status = psa_mac_abort(&operation);
|
||||
if (ret == 0 && status != PSA_SUCCESS) {
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "HMAC calculation failed", ret);
|
||||
return ret;
|
||||
@@ -1547,9 +1371,7 @@ int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl,
|
||||
unsigned char iv[12];
|
||||
unsigned char *dynamic_iv;
|
||||
size_t dynamic_iv_len;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
/*
|
||||
* Extract dynamic part of nonce for AEAD decryption.
|
||||
@@ -1619,7 +1441,6 @@ int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl,
|
||||
/*
|
||||
* Decrypt and authenticate
|
||||
*/
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
status = psa_aead_decrypt(transform->psa_key_dec,
|
||||
transform->psa_alg,
|
||||
iv, transform->ivlen,
|
||||
@@ -1633,23 +1454,6 @@ int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl,
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_aead_decrypt", ret);
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
if ((ret = mbedtls_cipher_auth_decrypt_ext
|
||||
(&transform->cipher_ctx_dec,
|
||||
iv, transform->ivlen,
|
||||
add_data, add_data_len,
|
||||
data, rec->data_len + transform->taglen, /* src */
|
||||
data, rec->buf_len - (size_t) (data - rec->buf), &olen, /* dst */
|
||||
transform->taglen)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_auth_decrypt_ext", ret);
|
||||
|
||||
if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) {
|
||||
return MBEDTLS_ERR_SSL_INVALID_MAC;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
auth_done++;
|
||||
|
||||
@@ -1664,11 +1468,9 @@ int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl,
|
||||
if (ssl_mode == MBEDTLS_SSL_MODE_CBC ||
|
||||
ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
|
||||
size_t minlen = 0;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
size_t part_len;
|
||||
psa_cipher_operation_t cipher_op = PSA_CIPHER_OPERATION_INIT;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
/*
|
||||
* Check immediate ciphertext sanity
|
||||
@@ -1716,11 +1518,7 @@ int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl,
|
||||
*/
|
||||
#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
|
||||
if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_mac_operation_t operation = PSA_MAC_OPERATION_INIT;
|
||||
#else
|
||||
unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG(3, ("using encrypt then mac"));
|
||||
|
||||
@@ -1742,7 +1540,6 @@ int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl,
|
||||
/* Calculate expected MAC. */
|
||||
MBEDTLS_SSL_DEBUG_BUF(4, "MAC'd meta-data", add_data,
|
||||
add_data_len);
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
status = psa_mac_verify_setup(&operation, transform->psa_mac_dec,
|
||||
transform->psa_mac_alg);
|
||||
if (status != PSA_SUCCESS) {
|
||||
@@ -1765,51 +1562,14 @@ int mbedtls_ssl_decrypt_buf(mbedtls_ssl_context const *ssl,
|
||||
if (status != PSA_SUCCESS) {
|
||||
goto hmac_failed_etm_enabled;
|
||||
}
|
||||
#else
|
||||
ret = mbedtls_md_hmac_update(&transform->md_ctx_dec, add_data,
|
||||
add_data_len);
|
||||
if (ret != 0) {
|
||||
goto hmac_failed_etm_enabled;
|
||||
}
|
||||
ret = mbedtls_md_hmac_update(&transform->md_ctx_dec,
|
||||
data, rec->data_len);
|
||||
if (ret != 0) {
|
||||
goto hmac_failed_etm_enabled;
|
||||
}
|
||||
ret = mbedtls_md_hmac_finish(&transform->md_ctx_dec, mac_expect);
|
||||
if (ret != 0) {
|
||||
goto hmac_failed_etm_enabled;
|
||||
}
|
||||
ret = mbedtls_md_hmac_reset(&transform->md_ctx_dec);
|
||||
if (ret != 0) {
|
||||
goto hmac_failed_etm_enabled;
|
||||
}
|
||||
|
||||
MBEDTLS_SSL_DEBUG_BUF(4, "message mac", data + rec->data_len,
|
||||
transform->maclen);
|
||||
MBEDTLS_SSL_DEBUG_BUF(4, "expected mac", mac_expect,
|
||||
transform->maclen);
|
||||
|
||||
/* Compare expected MAC with MAC at the end of the record. */
|
||||
if (mbedtls_ct_memcmp(data + rec->data_len, mac_expect,
|
||||
transform->maclen) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("message mac does not match"));
|
||||
ret = MBEDTLS_ERR_SSL_INVALID_MAC;
|
||||
goto hmac_failed_etm_enabled;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
auth_done++;
|
||||
|
||||
hmac_failed_etm_enabled:
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
status = psa_mac_abort(&operation);
|
||||
if (ret == 0 && status != PSA_SUCCESS) {
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
#else
|
||||
mbedtls_platform_zeroize(mac_expect, transform->maclen);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
if (ret != 0) {
|
||||
if (ret != MBEDTLS_ERR_SSL_INVALID_MAC) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_hmac_xxx", ret);
|
||||
@@ -1847,7 +1607,6 @@ hmac_failed_etm_enabled:
|
||||
|
||||
/* We still have data_len % ivlen == 0 and data_len >= ivlen here. */
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
status = psa_cipher_decrypt_setup(&cipher_op,
|
||||
transform->psa_key_dec, transform->psa_alg);
|
||||
|
||||
@@ -1886,15 +1645,6 @@ hmac_failed_etm_enabled:
|
||||
}
|
||||
|
||||
olen += part_len;
|
||||
#else
|
||||
|
||||
if ((ret = mbedtls_cipher_crypt(&transform->cipher_ctx_dec,
|
||||
transform->iv_dec, transform->ivlen,
|
||||
data, rec->data_len, data, &olen)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_crypt", ret);
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
/* Double-check that length hasn't changed during decryption. */
|
||||
if (rec->data_len != olen) {
|
||||
@@ -2036,18 +1786,11 @@ hmac_failed_etm_enabled:
|
||||
const size_t max_len = rec->data_len + padlen;
|
||||
const size_t min_len = (max_len > 256) ? max_len - 256 : 0;
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
ret = mbedtls_ct_hmac(transform->psa_mac_dec,
|
||||
transform->psa_mac_alg,
|
||||
add_data, add_data_len,
|
||||
data, rec->data_len, min_len, max_len,
|
||||
mac_expect);
|
||||
#else
|
||||
ret = mbedtls_ct_hmac(&transform->md_ctx_dec,
|
||||
add_data, add_data_len,
|
||||
data, rec->data_len, min_len, max_len,
|
||||
mac_expect);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ct_hmac", ret);
|
||||
goto hmac_failed_etm_disabled;
|
||||
@@ -5440,10 +5183,8 @@ int mbedtls_ssl_get_record_expansion(const mbedtls_ssl_context *ssl)
|
||||
size_t transform_expansion = 0;
|
||||
const mbedtls_ssl_transform *transform = ssl->transform_out;
|
||||
unsigned block_size;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_key_attributes_t attr = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_key_type_t key_type;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
size_t out_hdr_len = mbedtls_ssl_out_hdr_len(ssl);
|
||||
|
||||
@@ -5452,7 +5193,6 @@ int mbedtls_ssl_get_record_expansion(const mbedtls_ssl_context *ssl)
|
||||
}
|
||||
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if (transform->psa_alg == PSA_ALG_GCM ||
|
||||
transform->psa_alg == PSA_ALG_CCM ||
|
||||
transform->psa_alg == PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, 8) ||
|
||||
@@ -5483,41 +5223,6 @@ int mbedtls_ssl_get_record_expansion(const mbedtls_ssl_context *ssl)
|
||||
("Unsupported psa_alg spotted in mbedtls_ssl_get_record_expansion()"));
|
||||
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
||||
}
|
||||
#else
|
||||
switch (mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)) {
|
||||
case MBEDTLS_MODE_GCM:
|
||||
case MBEDTLS_MODE_CCM:
|
||||
case MBEDTLS_MODE_CHACHAPOLY:
|
||||
case MBEDTLS_MODE_STREAM:
|
||||
transform_expansion = transform->minlen;
|
||||
break;
|
||||
|
||||
case MBEDTLS_MODE_CBC:
|
||||
|
||||
block_size = mbedtls_cipher_get_block_size(
|
||||
&transform->cipher_ctx_enc);
|
||||
|
||||
/* Expansion due to the addition of the MAC. */
|
||||
transform_expansion += transform->maclen;
|
||||
|
||||
/* Expansion due to the addition of CBC padding;
|
||||
* Theoretically up to 256 bytes, but we never use
|
||||
* more than the block size of the underlying cipher. */
|
||||
transform_expansion += block_size;
|
||||
|
||||
/* For TLS 1.2 or higher, an explicit IV is added
|
||||
* after the record header. */
|
||||
#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
|
||||
transform_expansion += block_size;
|
||||
#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
||||
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
|
||||
if (transform->out_cid_len != 0) {
|
||||
@@ -6202,22 +5907,12 @@ void mbedtls_ssl_transform_free(mbedtls_ssl_transform *transform)
|
||||
return;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_destroy_key(transform->psa_key_enc);
|
||||
psa_destroy_key(transform->psa_key_dec);
|
||||
#else
|
||||
mbedtls_cipher_free(&transform->cipher_ctx_enc);
|
||||
mbedtls_cipher_free(&transform->cipher_ctx_dec);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_destroy_key(transform->psa_mac_enc);
|
||||
psa_destroy_key(transform->psa_mac_dec);
|
||||
#else
|
||||
mbedtls_md_free(&transform->md_ctx_enc);
|
||||
mbedtls_md_free(&transform->md_ctx_dec);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
#endif
|
||||
|
||||
mbedtls_platform_zeroize(transform, sizeof(mbedtls_ssl_transform));
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
/* Define a local translating function to save code size by not using too many
|
||||
* arguments in each translating place. */
|
||||
static int local_err_translation(psa_status_t status)
|
||||
@@ -27,7 +26,6 @@ static int local_err_translation(psa_status_t status)
|
||||
psa_generic_status_to_mbedtls);
|
||||
}
|
||||
#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Initialize context
|
||||
@@ -67,9 +65,7 @@ static int ssl_ticket_gen_key(mbedtls_ssl_ticket_context *ctx,
|
||||
unsigned char buf[MAX_KEY_BYTES] = { 0 };
|
||||
mbedtls_ssl_ticket_key *key = ctx->keys + index;
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_HAVE_TIME)
|
||||
key->generation_time = mbedtls_time(NULL);
|
||||
@@ -87,7 +83,6 @@ static int ssl_ticket_gen_key(mbedtls_ssl_ticket_context *ctx,
|
||||
return ret;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_set_key_usage_flags(&attributes,
|
||||
PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT);
|
||||
psa_set_key_algorithm(&attributes, key->alg);
|
||||
@@ -98,12 +93,6 @@ static int ssl_ticket_gen_key(mbedtls_ssl_ticket_context *ctx,
|
||||
psa_import_key(&attributes, buf,
|
||||
PSA_BITS_TO_BYTES(key->key_bits),
|
||||
&key->key));
|
||||
#else
|
||||
/* With GCM and CCM, same context can encrypt & decrypt */
|
||||
ret = mbedtls_cipher_setkey(&key->ctx, buf,
|
||||
mbedtls_cipher_get_key_bitlen(&key->ctx),
|
||||
MBEDTLS_ENCRYPT);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
mbedtls_platform_zeroize(buf, sizeof(buf));
|
||||
|
||||
@@ -124,9 +113,7 @@ static int ssl_ticket_update_keys(mbedtls_ssl_ticket_context *ctx)
|
||||
mbedtls_time_t current_time = mbedtls_time(NULL);
|
||||
mbedtls_time_t key_time = key->generation_time;
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
#endif
|
||||
|
||||
if (current_time >= key_time &&
|
||||
(uint64_t) (current_time - key_time) < key->lifetime) {
|
||||
@@ -135,11 +122,9 @@ static int ssl_ticket_update_keys(mbedtls_ssl_ticket_context *ctx)
|
||||
|
||||
ctx->active = 1 - ctx->active;
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if ((status = psa_destroy_key(ctx->keys[ctx->active].key)) != PSA_SUCCESS) {
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
return ssl_ticket_gen_key(ctx, ctx->active);
|
||||
} else
|
||||
@@ -159,19 +144,14 @@ int mbedtls_ssl_ticket_rotate(mbedtls_ssl_ticket_context *ctx,
|
||||
mbedtls_ssl_ticket_key * const key = ctx->keys + idx;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
const size_t bitlen = key->key_bits;
|
||||
#else
|
||||
const int bitlen = mbedtls_cipher_get_key_bitlen(&key->ctx);
|
||||
#endif
|
||||
|
||||
if (nlength < TICKET_KEY_NAME_BYTES || klength * 8 < (size_t) bitlen) {
|
||||
return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if ((status = psa_destroy_key(key->key)) != PSA_SUCCESS) {
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
return ret;
|
||||
@@ -189,12 +169,6 @@ int mbedtls_ssl_ticket_rotate(mbedtls_ssl_ticket_context *ctx,
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
ret = mbedtls_cipher_setkey(&key->ctx, k, bitlen, MBEDTLS_ENCRYPT);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
ctx->active = idx;
|
||||
ctx->ticket_lifetime = lifetime;
|
||||
@@ -218,14 +192,9 @@ int mbedtls_ssl_ticket_setup(mbedtls_ssl_ticket_context *ctx,
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t key_bits;
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_algorithm_t alg;
|
||||
psa_key_type_t key_type;
|
||||
#else
|
||||
const mbedtls_cipher_info_t *cipher_info;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if (mbedtls_ssl_cipher_to_psa(cipher, TICKET_AUTH_TAG_BYTES,
|
||||
&alg, &key_type, &key_bits) != PSA_SUCCESS) {
|
||||
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
||||
@@ -234,17 +203,6 @@ int mbedtls_ssl_ticket_setup(mbedtls_ssl_ticket_context *ctx,
|
||||
if (PSA_ALG_IS_AEAD(alg) == 0) {
|
||||
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
||||
}
|
||||
#else
|
||||
cipher_info = mbedtls_cipher_info_from_type(cipher);
|
||||
|
||||
if (mbedtls_cipher_info_get_mode(cipher_info) != MBEDTLS_MODE_GCM &&
|
||||
mbedtls_cipher_info_get_mode(cipher_info) != MBEDTLS_MODE_CCM &&
|
||||
mbedtls_cipher_info_get_mode(cipher_info) != MBEDTLS_MODE_CHACHAPOLY) {
|
||||
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
key_bits = mbedtls_cipher_info_get_key_bitlen(cipher_info);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
if (key_bits > 8 * MAX_KEY_BYTES) {
|
||||
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
||||
@@ -255,7 +213,6 @@ int mbedtls_ssl_ticket_setup(mbedtls_ssl_ticket_context *ctx,
|
||||
|
||||
ctx->ticket_lifetime = lifetime;
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
ctx->keys[0].alg = alg;
|
||||
ctx->keys[0].key_type = key_type;
|
||||
ctx->keys[0].key_bits = key_bits;
|
||||
@@ -263,15 +220,6 @@ int mbedtls_ssl_ticket_setup(mbedtls_ssl_ticket_context *ctx,
|
||||
ctx->keys[1].alg = alg;
|
||||
ctx->keys[1].key_type = key_type;
|
||||
ctx->keys[1].key_bits = key_bits;
|
||||
#else
|
||||
if ((ret = mbedtls_cipher_setup(&ctx->keys[0].ctx, cipher_info)) != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = mbedtls_cipher_setup(&ctx->keys[1].ctx, cipher_info)) != 0) {
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
if ((ret = ssl_ticket_gen_key(ctx, 0)) != 0 ||
|
||||
(ret = ssl_ticket_gen_key(ctx, 1)) != 0) {
|
||||
@@ -311,9 +259,7 @@ int mbedtls_ssl_ticket_write(void *p_ticket,
|
||||
unsigned char *state = state_len_bytes + TICKET_CRYPT_LEN_BYTES;
|
||||
size_t clear_len, ciph_len;
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
#endif
|
||||
|
||||
*tlen = 0;
|
||||
|
||||
@@ -355,7 +301,6 @@ int mbedtls_ssl_ticket_write(void *p_ticket,
|
||||
MBEDTLS_PUT_UINT16_BE(clear_len, state_len_bytes, 0);
|
||||
|
||||
/* Encrypt and authenticate */
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if ((status = psa_aead_encrypt(key->key, key->alg, iv, TICKET_IV_BYTES,
|
||||
key_name, TICKET_ADD_DATA_LEN,
|
||||
state, clear_len,
|
||||
@@ -364,17 +309,6 @@ int mbedtls_ssl_ticket_write(void *p_ticket,
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
goto cleanup;
|
||||
}
|
||||
#else
|
||||
if ((ret = mbedtls_cipher_auth_encrypt_ext(&key->ctx,
|
||||
iv, TICKET_IV_BYTES,
|
||||
/* Additional data: key name, IV and length */
|
||||
key_name, TICKET_ADD_DATA_LEN,
|
||||
state, clear_len,
|
||||
state, (size_t) (end - state), &ciph_len,
|
||||
TICKET_AUTH_TAG_BYTES)) != 0) {
|
||||
goto cleanup;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
if (ciph_len != clear_len + TICKET_AUTH_TAG_BYTES) {
|
||||
ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
||||
@@ -428,9 +362,7 @@ int mbedtls_ssl_ticket_parse(void *p_ticket,
|
||||
unsigned char *ticket = enc_len_p + TICKET_CRYPT_LEN_BYTES;
|
||||
size_t enc_len, clear_len;
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
#endif
|
||||
|
||||
if (ctx == NULL || ctx->f_rng == NULL) {
|
||||
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
||||
@@ -466,7 +398,6 @@ int mbedtls_ssl_ticket_parse(void *p_ticket,
|
||||
}
|
||||
|
||||
/* Decrypt and authenticate */
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if ((status = psa_aead_decrypt(key->key, key->alg, iv, TICKET_IV_BYTES,
|
||||
key_name, TICKET_ADD_DATA_LEN,
|
||||
ticket, enc_len + TICKET_AUTH_TAG_BYTES,
|
||||
@@ -474,21 +405,6 @@ int mbedtls_ssl_ticket_parse(void *p_ticket,
|
||||
ret = PSA_TO_MBEDTLS_ERR(status);
|
||||
goto cleanup;
|
||||
}
|
||||
#else
|
||||
if ((ret = mbedtls_cipher_auth_decrypt_ext(&key->ctx,
|
||||
iv, TICKET_IV_BYTES,
|
||||
/* Additional data: key name, IV and length */
|
||||
key_name, TICKET_ADD_DATA_LEN,
|
||||
ticket, enc_len + TICKET_AUTH_TAG_BYTES,
|
||||
ticket, enc_len, &clear_len,
|
||||
TICKET_AUTH_TAG_BYTES)) != 0) {
|
||||
if (ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED) {
|
||||
ret = MBEDTLS_ERR_SSL_INVALID_MAC;
|
||||
}
|
||||
|
||||
goto cleanup;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
if (clear_len != enc_len) {
|
||||
ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
||||
@@ -537,13 +453,8 @@ void mbedtls_ssl_ticket_free(mbedtls_ssl_ticket_context *ctx)
|
||||
return;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_destroy_key(ctx->keys[0].key);
|
||||
psa_destroy_key(ctx->keys[1].key);
|
||||
#else
|
||||
mbedtls_cipher_free(&ctx->keys[0].ctx);
|
||||
mbedtls_cipher_free(&ctx->keys[1].ctx);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#if defined(MBEDTLS_THREADING_C)
|
||||
mbedtls_mutex_free(&ctx->mutex);
|
||||
|
||||
+6
-793
File diff suppressed because it is too large
Load Diff
+2
-267
@@ -17,7 +17,6 @@
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/constant_time.h"
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#include "psa_util_internal.h"
|
||||
#include "psa/crypto.h"
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
|
||||
@@ -31,7 +30,6 @@ static int local_err_translation(psa_status_t status)
|
||||
}
|
||||
#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#include <string.h>
|
||||
|
||||
@@ -136,15 +134,9 @@ static int ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
|
||||
*olen = 0;
|
||||
|
||||
/* Skip costly extension if we can't use EC J-PAKE anyway */
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if (ssl->handshake->psa_pake_ctx_is_ok != 1) {
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
if (mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0) {
|
||||
return 0;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG(3,
|
||||
("client hello, adding ecjpake_kkpp extension"));
|
||||
@@ -163,7 +155,6 @@ static int ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
|
||||
ssl->handshake->ecjpake_cache_len == 0) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(3, ("generating new ecjpake parameters"));
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
|
||||
p + 2, end - p - 2, &kkpp_len,
|
||||
MBEDTLS_ECJPAKE_ROUND_ONE);
|
||||
@@ -173,16 +164,6 @@ static int ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
ret = mbedtls_ecjpake_write_round_one(&ssl->handshake->ecjpake_ctx,
|
||||
p + 2, end - p - 2, &kkpp_len,
|
||||
ssl->conf->f_rng, ssl->conf->p_rng);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1,
|
||||
"mbedtls_ecjpake_write_round_one", ret);
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
ssl->handshake->ecjpake_cache = mbedtls_calloc(1, kkpp_len);
|
||||
if (ssl->handshake->ecjpake_cache == NULL) {
|
||||
@@ -828,15 +809,6 @@ static int ssl_parse_supported_point_formats_ext(mbedtls_ssl_context *ssl,
|
||||
while (list_size > 0) {
|
||||
if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
|
||||
p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
|
||||
ssl->handshake->ecdh_ctx.point_format = p[0];
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED */
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
||||
mbedtls_ecjpake_set_point_format(&ssl->handshake->ecjpake_ctx,
|
||||
p[0]);
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
|
||||
MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
|
||||
return 0;
|
||||
}
|
||||
@@ -873,7 +845,6 @@ static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
|
||||
ssl->handshake->ecjpake_cache = NULL;
|
||||
ssl->handshake->ecjpake_cache_len = 0;
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if ((ret = mbedtls_psa_ecjpake_read_round(
|
||||
&ssl->handshake->psa_pake_ctx, buf, len,
|
||||
MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) {
|
||||
@@ -889,19 +860,6 @@ static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
|
||||
}
|
||||
|
||||
return 0;
|
||||
#else
|
||||
if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx,
|
||||
buf, len)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret);
|
||||
mbedtls_ssl_send_alert_message(
|
||||
ssl,
|
||||
MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
||||
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
}
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
|
||||
|
||||
@@ -1700,7 +1658,6 @@ static int ssl_parse_server_dh_params(mbedtls_ssl_context *ssl,
|
||||
}
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED */
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
|
||||
@@ -1774,89 +1731,6 @@ static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
|
||||
MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
|
||||
MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
|
||||
#else
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
static int ssl_check_server_ecdh_params(const mbedtls_ssl_context *ssl)
|
||||
{
|
||||
uint16_t tls_id;
|
||||
mbedtls_ecp_group_id grp_id;
|
||||
#if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
|
||||
grp_id = ssl->handshake->ecdh_ctx.grp.id;
|
||||
#else
|
||||
grp_id = ssl->handshake->ecdh_ctx.grp_id;
|
||||
#endif
|
||||
|
||||
tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
|
||||
if (tls_id == 0) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
||||
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("ECDH curve: %s",
|
||||
mbedtls_ssl_get_curve_name_from_tls_id(tls_id)));
|
||||
|
||||
if (mbedtls_ssl_check_curve(ssl, grp_id) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
|
||||
MBEDTLS_DEBUG_ECDH_QP);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
|
||||
MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
|
||||
MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED ||
|
||||
MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
|
||||
MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED) || \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
static int ssl_parse_server_ecdh_params(mbedtls_ssl_context *ssl,
|
||||
unsigned char **p,
|
||||
unsigned char *end)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
|
||||
|
||||
/*
|
||||
* Ephemeral ECDH parameters:
|
||||
*
|
||||
* struct {
|
||||
* ECParameters curve_params;
|
||||
* ECPoint public;
|
||||
* } ServerECDHParams;
|
||||
*/
|
||||
if ((ret = mbedtls_ecdh_read_params(&ssl->handshake->ecdh_ctx,
|
||||
(const unsigned char **) p, end)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_read_params"), ret);
|
||||
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
|
||||
if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
|
||||
ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
|
||||
}
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (ssl_check_server_ecdh_params(ssl) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(1,
|
||||
("bad server key exchange message (ECDHE curve)"));
|
||||
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED || \
|
||||
MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED || \
|
||||
MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO */
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
static int ssl_parse_server_psk_hint(mbedtls_ssl_context *ssl,
|
||||
@@ -2005,7 +1879,6 @@ static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
|
||||
const mbedtls_ecp_keypair *peer_key = mbedtls_pk_ec_ro(*peer_pk);
|
||||
#endif /* !defined(MBEDTLS_PK_USE_PSA_EC_DATA) */
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
uint16_t tls_id = 0;
|
||||
psa_key_type_t key_type = PSA_KEY_TYPE_NONE;
|
||||
mbedtls_ecp_group_id grp_id = mbedtls_pk_get_ec_group_id(peer_pk);
|
||||
@@ -2047,18 +1920,6 @@ static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
|
||||
}
|
||||
ssl->handshake->xxdh_psa_peerkey_len = olen;
|
||||
#endif /* MBEDTLS_PK_USE_PSA_EC_DATA */
|
||||
#else /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx, peer_key,
|
||||
MBEDTLS_ECDH_THEIRS)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (ssl_check_server_ecdh_params(ssl) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("bad server certificate (ECDH curve)"));
|
||||
return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
|
||||
/* We don't need the peer's public key anymore. Free it,
|
||||
* so that more RAM is available for upcoming expensive
|
||||
@@ -2218,7 +2079,6 @@ start_processing:
|
||||
MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
||||
if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
/*
|
||||
* The first 3 bytes are:
|
||||
* [0] MBEDTLS_ECP_TLS_NAMED_CURVE
|
||||
@@ -2255,18 +2115,6 @@ start_processing:
|
||||
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
|
||||
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
||||
}
|
||||
#else
|
||||
ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
|
||||
p, end - p);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
|
||||
mbedtls_ssl_send_alert_message(
|
||||
ssl,
|
||||
MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
||||
MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE);
|
||||
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
} else
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
|
||||
{
|
||||
@@ -2739,7 +2587,6 @@ static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
|
||||
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
|
||||
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
|
||||
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
psa_key_attributes_t key_attributes;
|
||||
@@ -2811,74 +2658,12 @@ static int ssl_write_client_key_exchange(mbedtls_ssl_context *ssl)
|
||||
if (status != PSA_SUCCESS || destruction_status != PSA_SUCCESS) {
|
||||
return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
|
||||
}
|
||||
#else
|
||||
/*
|
||||
* ECDH key exchange -- send client public value
|
||||
*/
|
||||
header_len = 4;
|
||||
|
||||
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
|
||||
if (ssl->handshake->ecrs_enabled) {
|
||||
if (ssl->handshake->ecrs_state == ssl_ecrs_cke_ecdh_calc_secret) {
|
||||
goto ecdh_calc_secret;
|
||||
}
|
||||
|
||||
mbedtls_ecdh_enable_restart(&ssl->handshake->ecdh_ctx);
|
||||
}
|
||||
#endif
|
||||
|
||||
ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
|
||||
&content_len,
|
||||
&ssl->out_msg[header_len], 1000,
|
||||
ssl->conf->f_rng, ssl->conf->p_rng);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
|
||||
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
|
||||
if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
|
||||
ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
|
||||
}
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
|
||||
MBEDTLS_DEBUG_ECDH_Q);
|
||||
|
||||
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
|
||||
if (ssl->handshake->ecrs_enabled) {
|
||||
ssl->handshake->ecrs_n = content_len;
|
||||
ssl->handshake->ecrs_state = ssl_ecrs_cke_ecdh_calc_secret;
|
||||
}
|
||||
|
||||
ecdh_calc_secret:
|
||||
if (ssl->handshake->ecrs_enabled) {
|
||||
content_len = ssl->handshake->ecrs_n;
|
||||
}
|
||||
#endif
|
||||
if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
|
||||
&ssl->handshake->pmslen,
|
||||
ssl->handshake->premaster,
|
||||
MBEDTLS_MPI_MAX_SIZE,
|
||||
ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
|
||||
#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
|
||||
if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
|
||||
ret = MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
|
||||
}
|
||||
#endif
|
||||
return ret;
|
||||
}
|
||||
|
||||
MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
|
||||
MBEDTLS_DEBUG_ECDH_Z);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
} else
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
|
||||
MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
|
||||
MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED ||
|
||||
MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
|
||||
if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
@@ -3001,8 +2786,7 @@ ecdh_calc_secret:
|
||||
MBEDTLS_PUT_UINT16_BE(zlen, pms, 0);
|
||||
pms += zlen_size + zlen;
|
||||
} else
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO &&
|
||||
MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
|
||||
if (mbedtls_ssl_ciphersuite_uses_psk(ciphersuite_info)) {
|
||||
/*
|
||||
@@ -3037,40 +2821,11 @@ ecdh_calc_secret:
|
||||
content_len = 0;
|
||||
} else
|
||||
#endif
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
|
||||
if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
|
||||
/*
|
||||
* ClientECDiffieHellmanPublic public;
|
||||
*/
|
||||
ret = mbedtls_ecdh_make_public(&ssl->handshake->ecdh_ctx,
|
||||
&content_len,
|
||||
&ssl->out_msg[header_len],
|
||||
MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
|
||||
ssl->conf->f_rng, ssl->conf->p_rng);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_public", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
|
||||
MBEDTLS_DEBUG_ECDH_Q);
|
||||
} else
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
|
||||
{
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
|
||||
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
|
||||
(mbedtls_key_exchange_type_t) ciphersuite_info->
|
||||
key_exchange)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1,
|
||||
"mbedtls_ssl_psk_derive_premaster", ret);
|
||||
return ret;
|
||||
}
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO */
|
||||
} else
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
|
||||
@@ -3086,7 +2841,6 @@ ecdh_calc_secret:
|
||||
if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
|
||||
header_len = 4;
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
unsigned char *out_p = ssl->out_msg + header_len;
|
||||
unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
|
||||
header_len;
|
||||
@@ -3099,25 +2853,6 @@ ecdh_calc_secret:
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
ret = mbedtls_ecjpake_write_round_two(&ssl->handshake->ecjpake_ctx,
|
||||
ssl->out_msg + header_len,
|
||||
MBEDTLS_SSL_OUT_CONTENT_LEN - header_len,
|
||||
&content_len,
|
||||
ssl->conf->f_rng, ssl->conf->p_rng);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
|
||||
ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
|
||||
ssl->conf->f_rng, ssl->conf->p_rng);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
} else
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
|
||||
{
|
||||
|
||||
+1
-190
@@ -20,7 +20,6 @@
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
/* Define a local translating function to save code size by not using too many
|
||||
* arguments in each translating place. */
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_ENABLED) || \
|
||||
@@ -33,7 +32,6 @@ static int local_err_translation(psa_status_t status)
|
||||
}
|
||||
#define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_ECP_C)
|
||||
#include "mbedtls/ecp.h"
|
||||
@@ -88,11 +86,9 @@ static int ssl_conf_has_psk_or_cb(mbedtls_ssl_config const *conf)
|
||||
}
|
||||
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
|
||||
return 1;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
if (conf->psk != NULL && conf->psk_len != 0) {
|
||||
return 1;
|
||||
@@ -261,15 +257,6 @@ static int ssl_parse_supported_point_formats(mbedtls_ssl_context *ssl,
|
||||
while (list_size > 0) {
|
||||
if (p[0] == MBEDTLS_ECP_PF_UNCOMPRESSED ||
|
||||
p[0] == MBEDTLS_ECP_PF_COMPRESSED) {
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED)
|
||||
ssl->handshake->ecdh_ctx.point_format = p[0];
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED */
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
||||
mbedtls_ecjpake_set_point_format(&ssl->handshake->ecjpake_ctx,
|
||||
p[0]);
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
|
||||
MBEDTLS_SSL_DEBUG_MSG(4, ("point format selected: %d", p[0]));
|
||||
return 0;
|
||||
}
|
||||
@@ -292,17 +279,11 @@ static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if (ssl->handshake->psa_pake_ctx_is_ok != 1)
|
||||
#else
|
||||
if (mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
{
|
||||
if (ssl->handshake->psa_pake_ctx_is_ok != 1) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(3, ("skip ecjpake kkpp extension"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if ((ret = mbedtls_psa_ecjpake_read_round(
|
||||
&ssl->handshake->psa_pake_ctx, buf, len,
|
||||
MBEDTLS_ECJPAKE_ROUND_ONE)) != 0) {
|
||||
@@ -317,15 +298,6 @@ static int ssl_parse_ecjpake_kkpp(mbedtls_ssl_context *ssl,
|
||||
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
if ((ret = mbedtls_ecjpake_read_round_one(&ssl->handshake->ecjpake_ctx,
|
||||
buf, len)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_one", ret);
|
||||
mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
|
||||
MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER);
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
/* Only mark the extension as OK when we're sure it is */
|
||||
ssl->handshake->cli_exts |= MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK;
|
||||
@@ -687,15 +659,10 @@ static int ssl_pick_cert(mbedtls_ssl_context *ssl,
|
||||
const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
|
||||
{
|
||||
mbedtls_ssl_key_cert *cur, *list;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_algorithm_t pk_alg =
|
||||
mbedtls_ssl_get_ciphersuite_sig_pk_psa_alg(ciphersuite_info);
|
||||
psa_key_usage_t pk_usage =
|
||||
mbedtls_ssl_get_ciphersuite_sig_pk_psa_usage(ciphersuite_info);
|
||||
#else
|
||||
mbedtls_pk_type_t pk_alg =
|
||||
mbedtls_ssl_get_ciphersuite_sig_pk_alg(ciphersuite_info);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
uint32_t flags;
|
||||
|
||||
#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
|
||||
@@ -706,11 +673,7 @@ static int ssl_pick_cert(mbedtls_ssl_context *ssl,
|
||||
list = ssl->conf->key_cert;
|
||||
|
||||
int pk_alg_is_none = 0;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
pk_alg_is_none = (pk_alg == PSA_ALG_NONE);
|
||||
#else
|
||||
pk_alg_is_none = (pk_alg == MBEDTLS_PK_NONE);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
if (pk_alg_is_none) {
|
||||
return 0;
|
||||
}
|
||||
@@ -728,7 +691,6 @@ static int ssl_pick_cert(mbedtls_ssl_context *ssl,
|
||||
cur->cert);
|
||||
|
||||
int key_type_matches = 0;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
|
||||
key_type_matches = ((ssl->conf->f_async_sign_start != NULL ||
|
||||
ssl->conf->f_async_decrypt_start != NULL ||
|
||||
@@ -738,9 +700,6 @@ static int ssl_pick_cert(mbedtls_ssl_context *ssl,
|
||||
key_type_matches = (
|
||||
mbedtls_pk_can_do_ext(cur->key, pk_alg, pk_usage));
|
||||
#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
|
||||
#else
|
||||
key_type_matches = mbedtls_pk_can_do(&cur->cert->pk, pk_alg);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
if (!key_type_matches) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(3, ("certificate mismatch: key type"));
|
||||
continue;
|
||||
@@ -1948,7 +1907,6 @@ static void ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
|
||||
MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ECJPAKE_KKPP, p, 0);
|
||||
p += 2;
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
ret = mbedtls_psa_ecjpake_write_round(&ssl->handshake->psa_pake_ctx,
|
||||
p + 2, (size_t) (end - p - 2), &kkpp_len,
|
||||
MBEDTLS_ECJPAKE_ROUND_ONE);
|
||||
@@ -1958,15 +1916,6 @@ static void ssl_write_ecjpake_kkpp_ext(mbedtls_ssl_context *ssl,
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_output", ret);
|
||||
return;
|
||||
}
|
||||
#else
|
||||
ret = mbedtls_ecjpake_write_round_one(&ssl->handshake->ecjpake_ctx,
|
||||
p + 2, (size_t) (end - p - 2), &kkpp_len,
|
||||
ssl->conf->f_rng, ssl->conf->p_rng);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_one", ret);
|
||||
return;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
MBEDTLS_PUT_UINT16_BE(kkpp_len, p, 0);
|
||||
p += 2;
|
||||
@@ -2585,7 +2534,6 @@ static int ssl_write_certificate_request(mbedtls_ssl_context *ssl)
|
||||
|
||||
#if (defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
|
||||
defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED))
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
|
||||
{
|
||||
@@ -2732,33 +2680,6 @@ exit:
|
||||
|
||||
return ret;
|
||||
}
|
||||
#else /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
MBEDTLS_CHECK_RETURN_CRITICAL
|
||||
static int ssl_get_ecdh_params_from_cert(mbedtls_ssl_context *ssl)
|
||||
{
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
const mbedtls_pk_context *private_key = mbedtls_ssl_own_key(ssl);
|
||||
if (private_key == NULL) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("got no server private key"));
|
||||
return MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED;
|
||||
}
|
||||
|
||||
if (!mbedtls_pk_can_do(private_key, MBEDTLS_PK_ECKEY)) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("server key not ECDH capable"));
|
||||
return MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH;
|
||||
}
|
||||
|
||||
if ((ret = mbedtls_ecdh_get_params(&ssl->handshake->ecdh_ctx,
|
||||
mbedtls_pk_ec_ro(*mbedtls_ssl_own_key(ssl)),
|
||||
MBEDTLS_ECDH_OURS)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, ("mbedtls_ecdh_get_params"), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) ||
|
||||
MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED */
|
||||
|
||||
@@ -2831,7 +2752,6 @@ static int ssl_prepare_server_key_exchange(mbedtls_ssl_context *ssl,
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
||||
if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
unsigned char *out_p = ssl->out_msg + ssl->out_msglen;
|
||||
unsigned char *end_p = ssl->out_msg + MBEDTLS_SSL_OUT_CONTENT_LEN -
|
||||
ssl->out_msglen;
|
||||
@@ -2868,21 +2788,6 @@ static int ssl_prepare_server_key_exchange(mbedtls_ssl_context *ssl,
|
||||
|
||||
output_offset += output_len;
|
||||
ssl->out_msglen += output_offset;
|
||||
#else
|
||||
size_t len = 0;
|
||||
|
||||
ret = mbedtls_ecjpake_write_round_two(
|
||||
&ssl->handshake->ecjpake_ctx,
|
||||
ssl->out_msg + ssl->out_msglen,
|
||||
MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen, &len,
|
||||
ssl->conf->f_rng, ssl->conf->p_rng);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_write_round_two", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
ssl->out_msglen += len;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
}
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
|
||||
|
||||
@@ -2989,7 +2894,6 @@ curve_matching_done:
|
||||
MBEDTLS_SSL_DEBUG_MSG(2, ("ECDHE curve: %s",
|
||||
mbedtls_ssl_get_curve_name_from_tls_id(*curr_tls_id)));
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
||||
psa_key_attributes_t key_attributes;
|
||||
mbedtls_ssl_handshake_params *handshake = ssl->handshake;
|
||||
@@ -3071,28 +2975,6 @@ curve_matching_done:
|
||||
|
||||
/* Determine full message length. */
|
||||
len += header_size;
|
||||
#else
|
||||
mbedtls_ecp_group_id curr_grp_id =
|
||||
mbedtls_ssl_get_ecp_group_id_from_tls_id(*curr_tls_id);
|
||||
|
||||
if ((ret = mbedtls_ecdh_setup(&ssl->handshake->ecdh_ctx,
|
||||
curr_grp_id)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecp_group_load", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = mbedtls_ecdh_make_params(
|
||||
&ssl->handshake->ecdh_ctx, &len,
|
||||
ssl->out_msg + ssl->out_msglen,
|
||||
MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen,
|
||||
ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_make_params", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
|
||||
MBEDTLS_DEBUG_ECDH_Q);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_WITH_SERVER_SIGNATURE_ENABLED)
|
||||
dig_signed = ssl->out_msg + ssl->out_msglen;
|
||||
@@ -3723,7 +3605,6 @@ static int ssl_parse_client_key_exchange(mbedtls_ssl_context *ssl)
|
||||
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA ||
|
||||
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_RSA ||
|
||||
ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA) {
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
size_t data_len = (size_t) (*p++);
|
||||
size_t buf_len = (size_t) (end - p);
|
||||
psa_status_t status = PSA_ERROR_GENERIC_ERROR;
|
||||
@@ -3784,28 +3665,6 @@ static int ssl_parse_client_key_exchange(mbedtls_ssl_context *ssl)
|
||||
}
|
||||
}
|
||||
handshake->xxdh_psa_privkey = MBEDTLS_SVC_KEY_ID_INIT;
|
||||
#else
|
||||
if ((ret = mbedtls_ecdh_read_public(&ssl->handshake->ecdh_ctx,
|
||||
p, (size_t) (end - p))) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_read_public", ret);
|
||||
return MBEDTLS_ERR_SSL_DECODE_ERROR;
|
||||
}
|
||||
|
||||
MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
|
||||
MBEDTLS_DEBUG_ECDH_QP);
|
||||
|
||||
if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx,
|
||||
&ssl->handshake->pmslen,
|
||||
ssl->handshake->premaster,
|
||||
MBEDTLS_MPI_MAX_SIZE,
|
||||
ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
|
||||
return MBEDTLS_ERR_SSL_DECODE_ERROR;
|
||||
}
|
||||
|
||||
MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
|
||||
MBEDTLS_DEBUG_ECDH_Z);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
} else
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED ||
|
||||
MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED ||
|
||||
@@ -3823,19 +3682,10 @@ static int ssl_parse_client_key_exchange(mbedtls_ssl_context *ssl)
|
||||
return MBEDTLS_ERR_SSL_DECODE_ERROR;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
|
||||
(mbedtls_key_exchange_type_t) ciphersuite_info->
|
||||
key_exchange)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
|
||||
return ret;
|
||||
}
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO */
|
||||
} else
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
|
||||
if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
psa_status_t destruction_status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
size_t ecpoint_len;
|
||||
@@ -3917,28 +3767,6 @@ static int ssl_parse_client_key_exchange(mbedtls_ssl_context *ssl)
|
||||
MBEDTLS_PUT_UINT16_BE(zlen, psm, 0);
|
||||
psm += zlen_size + zlen;
|
||||
|
||||
#else /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
if ((ret = ssl_parse_client_psk_identity(ssl, &p, end)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, ("ssl_parse_client_psk_identity"), ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = mbedtls_ecdh_read_public(&ssl->handshake->ecdh_ctx,
|
||||
p, (size_t) (end - p))) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_read_public", ret);
|
||||
return MBEDTLS_ERR_SSL_DECODE_ERROR;
|
||||
}
|
||||
|
||||
MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
|
||||
MBEDTLS_DEBUG_ECDH_QP);
|
||||
|
||||
if ((ret = mbedtls_ssl_psk_derive_premaster(ssl,
|
||||
(mbedtls_key_exchange_type_t) ciphersuite_info->
|
||||
key_exchange)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_psk_derive_premaster", ret);
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
} else
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
|
||||
@@ -3951,7 +3779,6 @@ static int ssl_parse_client_key_exchange(mbedtls_ssl_context *ssl)
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_RSA_ENABLED */
|
||||
#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
|
||||
if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if ((ret = mbedtls_psa_ecjpake_read_round(
|
||||
&ssl->handshake->psa_pake_ctx, p, (size_t) (end - p),
|
||||
MBEDTLS_ECJPAKE_ROUND_TWO)) != 0) {
|
||||
@@ -3961,22 +3788,6 @@ static int ssl_parse_client_key_exchange(mbedtls_ssl_context *ssl)
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "psa_pake_input round two", ret);
|
||||
return ret;
|
||||
}
|
||||
#else
|
||||
ret = mbedtls_ecjpake_read_round_two(&ssl->handshake->ecjpake_ctx,
|
||||
p, (size_t) (end - p));
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_read_round_two", ret);
|
||||
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
||||
}
|
||||
|
||||
ret = mbedtls_ecjpake_derive_secret(&ssl->handshake->ecjpake_ctx,
|
||||
ssl->handshake->premaster, 32, &ssl->handshake->pmslen,
|
||||
ssl->conf->f_rng, ssl->conf->p_rng);
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecjpake_derive_secret", ret);
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
} else
|
||||
#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
|
||||
{
|
||||
|
||||
@@ -925,23 +925,17 @@ int mbedtls_ssl_tls13_populate_transform(
|
||||
mbedtls_ssl_key_set const *traffic_keys,
|
||||
mbedtls_ssl_context *ssl /* DEBUG ONLY */)
|
||||
{
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
int ret;
|
||||
mbedtls_cipher_info_t const *cipher_info;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
|
||||
unsigned char const *key_enc;
|
||||
unsigned char const *iv_enc;
|
||||
unsigned char const *key_dec;
|
||||
unsigned char const *iv_dec;
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_key_type_t key_type;
|
||||
psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_algorithm_t alg;
|
||||
size_t key_bits;
|
||||
psa_status_t status = PSA_SUCCESS;
|
||||
#endif
|
||||
|
||||
#if !defined(MBEDTLS_DEBUG_C)
|
||||
ssl = NULL; /* make sure we don't use it except for those cases */
|
||||
@@ -955,29 +949,6 @@ int mbedtls_ssl_tls13_populate_transform(
|
||||
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
|
||||
if (cipher_info == NULL) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
|
||||
ciphersuite_info->cipher));
|
||||
return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
|
||||
}
|
||||
|
||||
/*
|
||||
* Setup cipher contexts in target transform
|
||||
*/
|
||||
if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
|
||||
cipher_info)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
|
||||
cipher_info)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#if defined(MBEDTLS_SSL_SRV_C)
|
||||
if (endpoint == MBEDTLS_SSL_IS_SERVER) {
|
||||
@@ -1003,21 +974,6 @@ int mbedtls_ssl_tls13_populate_transform(
|
||||
memcpy(transform->iv_enc, iv_enc, traffic_keys->iv_len);
|
||||
memcpy(transform->iv_dec, iv_dec, traffic_keys->iv_len);
|
||||
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc,
|
||||
key_enc, (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
|
||||
MBEDTLS_ENCRYPT)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec,
|
||||
key_dec, (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
|
||||
MBEDTLS_DECRYPT)) != 0) {
|
||||
MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
/*
|
||||
* Setup other fields in SSL transform
|
||||
@@ -1041,7 +997,6 @@ int mbedtls_ssl_tls13_populate_transform(
|
||||
transform->minlen =
|
||||
transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
/*
|
||||
* Setup psa keys and alg
|
||||
*/
|
||||
@@ -1082,7 +1037,6 @@ int mbedtls_ssl_tls13_populate_transform(
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -1298,8 +1252,7 @@ int mbedtls_ssl_tls13_key_schedule_stage_early(mbedtls_ssl_context *ssl)
|
||||
|
||||
ret = mbedtls_ssl_tls13_evolve_secret(hash_alg, NULL, psk, psk_len,
|
||||
handshake->tls13_master_secrets.early);
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
|
||||
defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
|
||||
#if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
|
||||
mbedtls_free((void *) psk);
|
||||
#endif
|
||||
if (ret != 0) {
|
||||
@@ -1839,7 +1792,6 @@ int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
|
||||
unsigned char **psk,
|
||||
size_t *psk_len)
|
||||
{
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_key_attributes_t key_attributes = PSA_KEY_ATTRIBUTES_INIT;
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
@@ -1869,14 +1821,6 @@ int mbedtls_ssl_tls13_export_handshake_psk(mbedtls_ssl_context *ssl,
|
||||
return PSA_TO_MBEDTLS_ERR(status);
|
||||
}
|
||||
return 0;
|
||||
#else
|
||||
*psk = ssl->handshake->psk;
|
||||
*psk_len = ssl->handshake->psk_len;
|
||||
if (*psk == NULL) {
|
||||
return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
|
||||
}
|
||||
return 0;
|
||||
#endif /* !MBEDTLS_USE_PSA_CRYPTO */
|
||||
}
|
||||
#endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
|
||||
|
||||
|
||||
@@ -435,9 +435,7 @@ static int ssl_tls13_offered_psks_check_binder_match(
|
||||
psk, psk_len, psk_type,
|
||||
transcript,
|
||||
server_computed_binder);
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
mbedtls_free((void *) psk);
|
||||
#endif
|
||||
if (ret != 0) {
|
||||
MBEDTLS_SSL_DEBUG_MSG(1, ("PSK binder calculation failed."));
|
||||
return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
|
||||
@@ -739,11 +737,7 @@ static int ssl_tls13_write_server_pre_shared_key_ext(mbedtls_ssl_context *ssl,
|
||||
*olen = 0;
|
||||
|
||||
int not_using_psk = 0;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
not_using_psk = (mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque));
|
||||
#else
|
||||
not_using_psk = (ssl->handshake->psk == NULL);
|
||||
#endif
|
||||
if (not_using_psk) {
|
||||
/* We shouldn't have called this extension writer unless we've
|
||||
* chosen to use a PSK. */
|
||||
@@ -1078,7 +1072,6 @@ static int ssl_tls13_key_exchange_is_ephemeral_available(mbedtls_ssl_context *ss
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
|
||||
defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_EPHEMERAL_ENABLED)
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
|
||||
{
|
||||
switch (sig_alg) {
|
||||
@@ -1104,7 +1097,6 @@ static psa_algorithm_t ssl_tls13_iana_sig_alg_to_psa_alg(uint16_t sig_alg)
|
||||
return PSA_ALG_NONE;
|
||||
}
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
/*
|
||||
* Pick best ( private key, certificate chain ) pair based on the signature
|
||||
@@ -1139,9 +1131,7 @@ static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
|
||||
|
||||
for (key_cert = key_cert_list; key_cert != NULL;
|
||||
key_cert = key_cert->next) {
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_algorithm_t psa_alg = PSA_ALG_NONE;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
MBEDTLS_SSL_DEBUG_CRT(3, "certificate (chain) candidate",
|
||||
key_cert->cert);
|
||||
@@ -1165,17 +1155,13 @@ static int ssl_tls13_pick_key_cert(mbedtls_ssl_context *ssl)
|
||||
"check signature algorithm %s [%04x]",
|
||||
mbedtls_ssl_sig_alg_to_str(*sig_alg),
|
||||
*sig_alg));
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_alg = ssl_tls13_iana_sig_alg_to_psa_alg(*sig_alg);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
if (mbedtls_ssl_tls13_check_sig_alg_cert_key_match(
|
||||
*sig_alg, &key_cert->cert->pk)
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
&& psa_alg != PSA_ALG_NONE &&
|
||||
mbedtls_pk_can_do_ext(&key_cert->cert->pk, psa_alg,
|
||||
PSA_KEY_USAGE_SIGN_HASH) == 1
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
) {
|
||||
ssl->handshake->key_cert = key_cert;
|
||||
MBEDTLS_SSL_DEBUG_MSG(3,
|
||||
|
||||
@@ -32,11 +32,9 @@
|
||||
#include "mbedtls/pem.h"
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#include "psa/crypto.h"
|
||||
#include "psa_util_internal.h"
|
||||
#include "mbedtls/psa_util.h"
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
#include "pk_internal.h"
|
||||
|
||||
#include "mbedtls/platform.h"
|
||||
@@ -2013,11 +2011,7 @@ static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
|
||||
{
|
||||
int flags = 0;
|
||||
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_algorithm_t psa_algorithm;
|
||||
#else
|
||||
const mbedtls_md_info_t *md_info;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
size_t hash_length;
|
||||
|
||||
if (ca == NULL) {
|
||||
@@ -2051,7 +2045,6 @@ static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
|
||||
flags |= MBEDTLS_X509_BADCRL_BAD_PK;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_algorithm = mbedtls_md_psa_alg_from_type(crl_list->sig_md);
|
||||
if (psa_hash_compute(psa_algorithm,
|
||||
crl_list->tbs.p,
|
||||
@@ -2063,18 +2056,6 @@ static int x509_crt_verifycrl(mbedtls_x509_crt *crt, mbedtls_x509_crt *ca,
|
||||
flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
|
||||
break;
|
||||
}
|
||||
#else
|
||||
md_info = mbedtls_md_info_from_type(crl_list->sig_md);
|
||||
hash_length = mbedtls_md_get_size(md_info);
|
||||
if (mbedtls_md(md_info,
|
||||
crl_list->tbs.p,
|
||||
crl_list->tbs.len,
|
||||
hash) != 0) {
|
||||
/* Note: this can't happen except after an internal error */
|
||||
flags |= MBEDTLS_X509_BADCRL_NOT_TRUSTED;
|
||||
break;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
if (x509_profile_check_key(profile, &ca->pk) != 0) {
|
||||
flags |= MBEDTLS_X509_BADCERT_BAD_KEY;
|
||||
@@ -2126,16 +2107,6 @@ static int x509_crt_check_signature(const mbedtls_x509_crt *child,
|
||||
{
|
||||
size_t hash_len;
|
||||
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
|
||||
#if !defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
const mbedtls_md_info_t *md_info;
|
||||
md_info = mbedtls_md_info_from_type(child->sig_md);
|
||||
hash_len = mbedtls_md_get_size(md_info);
|
||||
|
||||
/* Note: hash errors can happen only after an internal error */
|
||||
if (mbedtls_md(md_info, child->tbs.p, child->tbs.len, hash) != 0) {
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(child->sig_md);
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
|
||||
@@ -2149,7 +2120,6 @@ static int x509_crt_check_signature(const mbedtls_x509_crt *child,
|
||||
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
|
||||
}
|
||||
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
/* Skip expensive computation on obvious mismatch */
|
||||
if (!mbedtls_pk_can_do(&parent->pk, child->sig_pk)) {
|
||||
return -1;
|
||||
|
||||
@@ -22,11 +22,9 @@
|
||||
#include "mbedtls/pem.h"
|
||||
#endif /* MBEDTLS_PEM_WRITE_C */
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#include "psa/crypto.h"
|
||||
#include "mbedtls/psa_util.h"
|
||||
#include "md_psa.h"
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#define CHECK_OVERFLOW_ADD(a, b) \
|
||||
do \
|
||||
|
||||
@@ -30,11 +30,9 @@
|
||||
#include "mbedtls/pem.h"
|
||||
#endif /* MBEDTLS_PEM_WRITE_C */
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#include "psa/crypto.h"
|
||||
#include "psa_util_internal.h"
|
||||
#include "mbedtls/psa_util.h"
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
void mbedtls_x509write_crt_init(mbedtls_x509write_cert *ctx)
|
||||
{
|
||||
@@ -179,10 +177,8 @@ static int mbedtls_x509write_crt_set_key_identifier(mbedtls_x509write_cert *ctx,
|
||||
unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
|
||||
unsigned char *c = buf + sizeof(buf);
|
||||
size_t len = 0;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
size_t hash_length;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
MBEDTLS_ASN1_CHK_ADD(len,
|
||||
@@ -193,7 +189,6 @@ static int mbedtls_x509write_crt_set_key_identifier(mbedtls_x509write_cert *ctx,
|
||||
ctx->subject_key));
|
||||
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
status = psa_hash_compute(PSA_ALG_SHA_1,
|
||||
buf + sizeof(buf) - len,
|
||||
len,
|
||||
@@ -203,14 +198,6 @@ static int mbedtls_x509write_crt_set_key_identifier(mbedtls_x509write_cert *ctx,
|
||||
if (status != PSA_SUCCESS) {
|
||||
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
|
||||
}
|
||||
#else
|
||||
ret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
|
||||
buf + sizeof(buf) - len, len,
|
||||
buf + sizeof(buf) - 20);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
c = buf + sizeof(buf) - 20;
|
||||
len = 20;
|
||||
@@ -403,10 +390,8 @@ int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx,
|
||||
unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
|
||||
size_t hash_length = 0;
|
||||
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
||||
psa_algorithm_t psa_algorithm;
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
|
||||
size_t len = 0;
|
||||
@@ -572,7 +557,6 @@ int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx,
|
||||
*/
|
||||
|
||||
/* Compute hash of CRT. */
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
psa_algorithm = mbedtls_md_psa_alg_from_type(ctx->md_alg);
|
||||
|
||||
status = psa_hash_compute(psa_algorithm,
|
||||
@@ -584,12 +568,6 @@ int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx,
|
||||
if (status != PSA_SUCCESS) {
|
||||
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
|
||||
}
|
||||
#else
|
||||
if ((ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c,
|
||||
len, hash)) != 0) {
|
||||
return ret;
|
||||
}
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
|
||||
if ((ret = mbedtls_pk_sign(ctx->issuer_key, ctx->md_alg,
|
||||
|
||||
@@ -20,11 +20,9 @@
|
||||
#include "mbedtls/oid.h"
|
||||
#include "mbedtls/platform_util.h"
|
||||
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
#include "psa/crypto.h"
|
||||
#include "psa_util_internal.h"
|
||||
#include "mbedtls/psa_util.h"
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
@@ -145,10 +143,8 @@ static int x509write_csr_der_internal(mbedtls_x509write_csr *ctx,
|
||||
size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
|
||||
size_t len = 0;
|
||||
mbedtls_pk_type_t pk_alg;
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
size_t hash_len;
|
||||
psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(ctx->md_alg);
|
||||
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
|
||||
/* Write the CSR backwards starting from the end of buf */
|
||||
c = buf + size;
|
||||
@@ -213,7 +209,6 @@ static int x509write_csr_der_internal(mbedtls_x509write_csr *ctx,
|
||||
* Sign the written CSR data into the sig buffer
|
||||
* Note: hash errors can happen only after an internal error
|
||||
*/
|
||||
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
||||
if (psa_hash_compute(hash_alg,
|
||||
c,
|
||||
len,
|
||||
@@ -222,12 +217,6 @@ static int x509write_csr_der_internal(mbedtls_x509write_csr *ctx,
|
||||
&hash_len) != PSA_SUCCESS) {
|
||||
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
|
||||
}
|
||||
#else /* MBEDTLS_USE_PSA_CRYPTO */
|
||||
ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c, len, hash);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
if ((ret = mbedtls_pk_sign(ctx->key, ctx->md_alg, hash, 0,
|
||||
sig, sig_size, &sig_len,
|
||||
f_rng, p_rng)) != 0) {
|
||||
|
||||
Reference in New Issue
Block a user