mirror of
https://github.com/ARMmbed/mbedtls.git
synced 2025-05-16 03:37:49 +08:00
Make use of PSA crypto hash if MBEDTLS_MD_C isn't defined
Signed-off-by: Neil Armstrong <narmstrong@baylibre.com>
This commit is contained in:
parent
0d76341eac
commit
ecaba1c9b2
@ -108,7 +108,8 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(MBEDTLS_ECJPAKE_C) && \
|
#if defined(MBEDTLS_ECJPAKE_C) && \
|
||||||
( !defined(MBEDTLS_ECP_C) || !defined(MBEDTLS_MD_C) )
|
( !defined(MBEDTLS_ECP_C) || \
|
||||||
|
!( defined(MBEDTLS_MD_C) || defined(MBEDTLS_PSA_CRYPTO_C) ) )
|
||||||
#error "MBEDTLS_ECJPAKE_C defined, but not all prerequisites"
|
#error "MBEDTLS_ECJPAKE_C defined, but not all prerequisites"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -2330,6 +2330,9 @@
|
|||||||
* ECJPAKE
|
* ECJPAKE
|
||||||
*
|
*
|
||||||
* Requires: MBEDTLS_ECP_C, MBEDTLS_MD_C
|
* Requires: MBEDTLS_ECP_C, MBEDTLS_MD_C
|
||||||
|
*
|
||||||
|
* \warning If building without MBEDTLS_MD_C, you must call psa_crypto_init()
|
||||||
|
* before doing any EC J-PAKE operations.
|
||||||
*/
|
*/
|
||||||
#define MBEDTLS_ECJPAKE_C
|
#define MBEDTLS_ECJPAKE_C
|
||||||
|
|
||||||
|
@ -30,6 +30,13 @@
|
|||||||
#include "mbedtls/platform_util.h"
|
#include "mbedtls/platform_util.h"
|
||||||
#include "mbedtls/error.h"
|
#include "mbedtls/error.h"
|
||||||
|
|
||||||
|
/* We use MD first if it's available (for compatibility reasons)
|
||||||
|
* and "fall back" to PSA otherwise (which needs psa_crypto_init()). */
|
||||||
|
#if !defined(MBEDTLS_MD_C)
|
||||||
|
#include "psa/crypto.h"
|
||||||
|
#include "mbedtls/psa_util.h"
|
||||||
|
#endif /* !MBEDTLS_MD_C */
|
||||||
|
|
||||||
#include "hash_info.h"
|
#include "hash_info.h"
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -47,6 +54,28 @@ static const char * const ecjpake_id[] = {
|
|||||||
#define ID_MINE ( ecjpake_id[ ctx->role ] )
|
#define ID_MINE ( ecjpake_id[ ctx->role ] )
|
||||||
#define ID_PEER ( ecjpake_id[ 1 - ctx->role ] )
|
#define ID_PEER ( ecjpake_id[ 1 - ctx->role ] )
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper to Compute a hash from md_type
|
||||||
|
*/
|
||||||
|
static int mbedtls_ecjpake_compute_hash( mbedtls_md_type_t md_type,
|
||||||
|
const unsigned char *input, size_t ilen,
|
||||||
|
unsigned char *output )
|
||||||
|
{
|
||||||
|
#if defined(MBEDTLS_MD_C)
|
||||||
|
return( mbedtls_md( mbedtls_md_info_from_type( md_type ),
|
||||||
|
input, ilen, output ) );
|
||||||
|
#else
|
||||||
|
psa_algorithm_t alg = mbedtls_psa_translate_md( md_type );
|
||||||
|
psa_status_t status;
|
||||||
|
size_t out_size = PSA_HASH_LENGTH( alg );
|
||||||
|
size_t out_len;
|
||||||
|
|
||||||
|
status = psa_hash_compute( alg, input, ilen, output, out_size, &out_len );
|
||||||
|
|
||||||
|
return( mbedtls_md_error_from_psa( status ) );
|
||||||
|
#endif /* !MBEDTLS_MD_C */
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Initialize context
|
* Initialize context
|
||||||
*/
|
*/
|
||||||
@ -106,8 +135,13 @@ int mbedtls_ecjpake_setup( mbedtls_ecjpake_context *ctx,
|
|||||||
|
|
||||||
ctx->role = role;
|
ctx->role = role;
|
||||||
|
|
||||||
|
#if defined(MBEDTLS_MD_C)
|
||||||
if( ( mbedtls_md_info_from_type( hash ) ) == NULL )
|
if( ( mbedtls_md_info_from_type( hash ) ) == NULL )
|
||||||
return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
|
return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
|
||||||
|
#else
|
||||||
|
if( mbedtls_psa_translate_md( hash ) == MBEDTLS_MD_NONE )
|
||||||
|
return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
|
||||||
|
#endif
|
||||||
|
|
||||||
ctx->md_type = hash;
|
ctx->md_type = hash;
|
||||||
|
|
||||||
@ -222,7 +256,7 @@ static int ecjpake_hash( const mbedtls_md_type_t md_type,
|
|||||||
p += id_len;
|
p += id_len;
|
||||||
|
|
||||||
/* Compute hash */
|
/* Compute hash */
|
||||||
MBEDTLS_MPI_CHK( mbedtls_md( mbedtls_md_info_from_type( md_type ),
|
MBEDTLS_MPI_CHK( mbedtls_ecjpake_compute_hash( md_type,
|
||||||
buf, p - buf, hash ) );
|
buf, p - buf, hash ) );
|
||||||
|
|
||||||
/* Turn it into an integer mod n */
|
/* Turn it into an integer mod n */
|
||||||
@ -763,7 +797,7 @@ int mbedtls_ecjpake_derive_secret( mbedtls_ecjpake_context *ctx,
|
|||||||
/* PMS = SHA-256( K.X ) */
|
/* PMS = SHA-256( K.X ) */
|
||||||
x_bytes = ( ctx->grp.pbits + 7 ) / 8;
|
x_bytes = ( ctx->grp.pbits + 7 ) / 8;
|
||||||
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &K.X, kx, x_bytes ) );
|
MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &K.X, kx, x_bytes ) );
|
||||||
MBEDTLS_MPI_CHK( mbedtls_md( mbedtls_md_info_from_type( ctx->md_type ),
|
MBEDTLS_MPI_CHK( mbedtls_ecjpake_compute_hash( ctx->md_type,
|
||||||
kx, x_bytes, buf ) );
|
kx, x_bytes, buf ) );
|
||||||
|
|
||||||
cleanup:
|
cleanup:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user