mirror of
https://github.com/yuzu-emu/mbedtls.git
synced 2024-11-23 02:55:41 +01:00
PSA: support HMAC_DRBG
Support using HMAC_DRBG instead of CTR_DRBG in the PSA subsystem. Use HMAC_DRBG if CTR_DRBG is available. Choose between SHA-256 and SHA-512 based on availability. Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
parent
4fc21fdeb6
commit
82e57d1611
@ -573,7 +573,7 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(MBEDTLS_PSA_CRYPTO_C) && \
|
#if defined(MBEDTLS_PSA_CRYPTO_C) && \
|
||||||
!( ( defined(MBEDTLS_CTR_DRBG_C) && \
|
!( ( ( defined(MBEDTLS_CTR_DRBG_C) || defined(MBEDTLS_HMAC_DRBG_C) ) && \
|
||||||
defined(MBEDTLS_ENTROPY_C) ) || \
|
defined(MBEDTLS_ENTROPY_C) ) || \
|
||||||
defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) )
|
defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) )
|
||||||
#error "MBEDTLS_PSA_CRYPTO_C defined, but not all prerequisites (missing RNG)"
|
#error "MBEDTLS_PSA_CRYPTO_C defined, but not all prerequisites (missing RNG)"
|
||||||
|
@ -3146,6 +3146,7 @@
|
|||||||
* Module: library/psa_crypto.c
|
* Module: library/psa_crypto.c
|
||||||
*
|
*
|
||||||
* Requires: either MBEDTLS_CTR_DRBG_C and MBEDTLS_ENTROPY_C,
|
* Requires: either MBEDTLS_CTR_DRBG_C and MBEDTLS_ENTROPY_C,
|
||||||
|
* or MBEDTLS_HMAC_DRBG_C and MBEDTLS_ENTROPY_C,
|
||||||
* or MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG.
|
* or MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
@ -63,7 +63,6 @@
|
|||||||
#include "mbedtls/cipher.h"
|
#include "mbedtls/cipher.h"
|
||||||
#include "mbedtls/ccm.h"
|
#include "mbedtls/ccm.h"
|
||||||
#include "mbedtls/cmac.h"
|
#include "mbedtls/cmac.h"
|
||||||
#include "mbedtls/ctr_drbg.h"
|
|
||||||
#include "mbedtls/des.h"
|
#include "mbedtls/des.h"
|
||||||
#include "mbedtls/ecdh.h"
|
#include "mbedtls/ecdh.h"
|
||||||
#include "mbedtls/ecp.h"
|
#include "mbedtls/ecp.h"
|
||||||
@ -214,6 +213,8 @@ psa_status_t mbedtls_to_psa_error( int ret )
|
|||||||
case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
|
case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
|
||||||
return( PSA_ERROR_HARDWARE_FAILURE );
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
||||||
|
|
||||||
|
#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
|
||||||
|
defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
|
||||||
case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
|
case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
|
||||||
return( PSA_ERROR_INSUFFICIENT_ENTROPY );
|
return( PSA_ERROR_INSUFFICIENT_ENTROPY );
|
||||||
case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
|
case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
|
||||||
@ -221,6 +222,7 @@ psa_status_t mbedtls_to_psa_error( int ret )
|
|||||||
return( PSA_ERROR_NOT_SUPPORTED );
|
return( PSA_ERROR_NOT_SUPPORTED );
|
||||||
case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
|
case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
|
||||||
return( PSA_ERROR_INSUFFICIENT_ENTROPY );
|
return( PSA_ERROR_INSUFFICIENT_ENTROPY );
|
||||||
|
#endif
|
||||||
|
|
||||||
case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
|
case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
|
||||||
return( PSA_ERROR_NOT_SUPPORTED );
|
return( PSA_ERROR_NOT_SUPPORTED );
|
||||||
@ -239,6 +241,17 @@ psa_status_t mbedtls_to_psa_error( int ret )
|
|||||||
case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
|
case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
|
||||||
return( PSA_ERROR_HARDWARE_FAILURE );
|
return( PSA_ERROR_HARDWARE_FAILURE );
|
||||||
|
|
||||||
|
#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
|
||||||
|
defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
|
||||||
|
case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
|
||||||
|
return( PSA_ERROR_INSUFFICIENT_ENTROPY );
|
||||||
|
case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
|
||||||
|
case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
|
||||||
|
return( PSA_ERROR_NOT_SUPPORTED );
|
||||||
|
case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
|
||||||
|
return( PSA_ERROR_INSUFFICIENT_ENTROPY );
|
||||||
|
#endif
|
||||||
|
|
||||||
case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
|
case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
|
||||||
case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
|
case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
|
||||||
case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
|
case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
|
||||||
|
@ -50,15 +50,48 @@ static inline void *mbedtls_psa_random_state( mbedtls_psa_random_context_t *rng
|
|||||||
|
|
||||||
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
||||||
|
|
||||||
/* Currently, the only supported RNG is Mbed TLS's CTR_DRBG seeded with
|
/* Choose a DRBG based on configuration and availability */
|
||||||
* mbedtls_entropy_func(). */
|
#if defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
|
||||||
|
|
||||||
|
#include "mbedtls/hmac_drbg.h"
|
||||||
|
|
||||||
|
#elif defined(MBEDTLS_CTR_DRBG_C)
|
||||||
|
|
||||||
#include "mbedtls/ctr_drbg.h"
|
#include "mbedtls/ctr_drbg.h"
|
||||||
|
|
||||||
|
#elif defined(MBEDTLS_HMAC_DRBG_C)
|
||||||
|
|
||||||
|
#include "mbedtls/hmac_drbg.h"
|
||||||
|
#if defined(MBEDTLS_SHA512_C) && defined(MBEDTLS_SHA256_C)
|
||||||
|
#include <limits.h>
|
||||||
|
#if SIZE_MAX > 0xffffffff
|
||||||
|
/* Looks like a 64-bit system, so prefer SHA-512. */
|
||||||
|
#define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA512
|
||||||
|
#else
|
||||||
|
/* Looks like a 32-bit system, so prefer SHA-256. */
|
||||||
|
#define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA256
|
||||||
|
#endif
|
||||||
|
#elif defined(MBEDTLS_SHA512_C)
|
||||||
|
#define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA512
|
||||||
|
#elif defined(MBEDTLS_SHA256_C)
|
||||||
|
#define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA256
|
||||||
|
#else
|
||||||
|
#error "No hash algorithm available for HMAC_DBRG."
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#else
|
||||||
|
#error "No DRBG module available for the psa_crypto module."
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "mbedtls/entropy.h"
|
#include "mbedtls/entropy.h"
|
||||||
|
|
||||||
/** The type of the PSA DRBG context.
|
/** The type of the PSA DRBG context.
|
||||||
*/
|
*/
|
||||||
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
||||||
typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t;
|
typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t;
|
||||||
|
#elif defined(MBEDTLS_HMAC_DRBG_C)
|
||||||
|
typedef mbedtls_hmac_drbg_context mbedtls_psa_drbg_context_t;
|
||||||
|
#endif
|
||||||
|
|
||||||
/** Initialize the PSA DRBG.
|
/** Initialize the PSA DRBG.
|
||||||
*
|
*
|
||||||
@ -66,7 +99,11 @@ typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t;
|
|||||||
*/
|
*/
|
||||||
static inline void mbedtls_psa_drbg_init( mbedtls_psa_drbg_context_t *p_rng )
|
static inline void mbedtls_psa_drbg_init( mbedtls_psa_drbg_context_t *p_rng )
|
||||||
{
|
{
|
||||||
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
||||||
mbedtls_ctr_drbg_init( p_rng );
|
mbedtls_ctr_drbg_init( p_rng );
|
||||||
|
#elif defined(MBEDTLS_HMAC_DRBG_C)
|
||||||
|
mbedtls_hmac_drbg_init( p_rng );
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Deinitialize the PSA DRBG.
|
/** Deinitialize the PSA DRBG.
|
||||||
@ -75,7 +112,11 @@ static inline void mbedtls_psa_drbg_init( mbedtls_psa_drbg_context_t *p_rng )
|
|||||||
*/
|
*/
|
||||||
static inline void mbedtls_psa_drbg_free( mbedtls_psa_drbg_context_t *p_rng )
|
static inline void mbedtls_psa_drbg_free( mbedtls_psa_drbg_context_t *p_rng )
|
||||||
{
|
{
|
||||||
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
||||||
mbedtls_ctr_drbg_free( p_rng );
|
mbedtls_ctr_drbg_free( p_rng );
|
||||||
|
#elif defined(MBEDTLS_HMAC_DRBG_C)
|
||||||
|
mbedtls_hmac_drbg_free( p_rng );
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The type of the PSA random generator context.
|
/** The type of the PSA random generator context.
|
||||||
@ -97,7 +138,7 @@ typedef struct
|
|||||||
* that require a random generator. Use mbedtls_psa_random_state() to
|
* that require a random generator. Use mbedtls_psa_random_state() to
|
||||||
* obtain the \p p_rng parameter.
|
* obtain the \p p_rng parameter.
|
||||||
*
|
*
|
||||||
* \param p_rng The CTR_DRBG context. This must be
|
* \param p_rng The DRBG context. This must be
|
||||||
* mbedtls_psa_random_state( \c rng )
|
* mbedtls_psa_random_state( \c rng )
|
||||||
* where \c rng is a pointer to a
|
* where \c rng is a pointer to a
|
||||||
* ::mbedtls_psa_random_context_t structure.
|
* ::mbedtls_psa_random_context_t structure.
|
||||||
@ -105,21 +146,29 @@ typedef struct
|
|||||||
* \param output_len The length of the buffer in bytes.
|
* \param output_len The length of the buffer in bytes.
|
||||||
* It must be at most #MBEDTLS_PSA_RANDOM_MAX_REQUEST.
|
* It must be at most #MBEDTLS_PSA_RANDOM_MAX_REQUEST.
|
||||||
*
|
*
|
||||||
* \return \c 0 on success.
|
* \retval \c 0 on success.
|
||||||
* \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
|
* \return \c MBEDTLS_ERR_xxx_DRBG_xxx or
|
||||||
* #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure.
|
* \c MBEDTLS_ERR_PLATFORM_xxx on failure.
|
||||||
*/
|
*/
|
||||||
static inline int mbedtls_psa_get_random( void *p_rng,
|
static inline int mbedtls_psa_get_random( void *p_rng,
|
||||||
unsigned char *output,
|
unsigned char *output,
|
||||||
size_t output_len )
|
size_t output_len )
|
||||||
{
|
{
|
||||||
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
||||||
return( mbedtls_ctr_drbg_random( p_rng, output, output_len ) );
|
return( mbedtls_ctr_drbg_random( p_rng, output, output_len ) );
|
||||||
|
#elif defined(MBEDTLS_HMAC_DRBG_C)
|
||||||
|
return( mbedtls_hmac_drbg_random( p_rng, output, output_len ) );
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The maximum number of bytes that mbedtls_psa_get_random() is expected to
|
/** The maximum number of bytes that mbedtls_psa_get_random() is expected to
|
||||||
* return.
|
* return.
|
||||||
*/
|
*/
|
||||||
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
||||||
#define MBEDTLS_PSA_RANDOM_MAX_REQUEST MBEDTLS_CTR_DRBG_MAX_REQUEST
|
#define MBEDTLS_PSA_RANDOM_MAX_REQUEST MBEDTLS_CTR_DRBG_MAX_REQUEST
|
||||||
|
#elif defined(MBEDTLS_HMAC_DRBG_C)
|
||||||
|
#define MBEDTLS_PSA_RANDOM_MAX_REQUEST MBEDTLS_HMAC_DRBG_MAX_REQUEST
|
||||||
|
#endif
|
||||||
|
|
||||||
/** Retrieve the DRBG state from the PSA RNG state.
|
/** Retrieve the DRBG state from the PSA RNG state.
|
||||||
*
|
*
|
||||||
@ -133,7 +182,6 @@ static inline mbedtls_psa_drbg_context_t *mbedtls_psa_random_state(
|
|||||||
return( &rng->drbg );
|
return( &rng->drbg );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/** Seed the PSA DRBG.
|
/** Seed the PSA DRBG.
|
||||||
*
|
*
|
||||||
* \param rng DRBG context to be seeded.
|
* \param rng DRBG context to be seeded.
|
||||||
@ -149,10 +197,20 @@ static inline int mbedtls_psa_drbg_seed(
|
|||||||
mbedtls_psa_random_context_t *rng,
|
mbedtls_psa_random_context_t *rng,
|
||||||
const unsigned char *custom, size_t len )
|
const unsigned char *custom, size_t len )
|
||||||
{
|
{
|
||||||
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
||||||
return( mbedtls_ctr_drbg_seed( mbedtls_psa_random_state( rng ),
|
return( mbedtls_ctr_drbg_seed( mbedtls_psa_random_state( rng ),
|
||||||
mbedtls_entropy_func,
|
mbedtls_entropy_func,
|
||||||
&rng->entropy,
|
&rng->entropy,
|
||||||
custom, len ) );
|
custom, len ) );
|
||||||
|
#elif defined(MBEDTLS_HMAC_DRBG_C)
|
||||||
|
const mbedtls_md_info_t *md_info =
|
||||||
|
mbedtls_md_info_from_type( MBEDTLS_PSA_HMAC_DRBG_MD_TYPE );
|
||||||
|
return( mbedtls_hmac_drbg_seed( mbedtls_psa_random_state( rng ),
|
||||||
|
md_info,
|
||||||
|
mbedtls_entropy_func,
|
||||||
|
&rng->entropy,
|
||||||
|
custom, len ) );
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
||||||
|
@ -915,10 +915,6 @@ component_test_no_ctr_drbg () {
|
|||||||
msg "build: Full minus CTR_DRBG"
|
msg "build: Full minus CTR_DRBG"
|
||||||
scripts/config.py full
|
scripts/config.py full
|
||||||
scripts/config.py unset MBEDTLS_CTR_DRBG_C
|
scripts/config.py unset MBEDTLS_CTR_DRBG_C
|
||||||
scripts/config.py unset MBEDTLS_PSA_CRYPTO_C # requires CTR_DRBG
|
|
||||||
scripts/config.py unset MBEDTLS_PSA_CRYPTO_STORAGE_C # requires PSA Crypto
|
|
||||||
scripts/config.py unset MBEDTLS_PSA_CRYPTO_SE_C # requires PSA Crypto
|
|
||||||
scripts/config.py unset MBEDTLS_USE_PSA_CRYPTO # requires PSA Crypto
|
|
||||||
|
|
||||||
CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
|
CC=gcc cmake -D CMAKE_BUILD_TYPE:String=Asan .
|
||||||
make
|
make
|
||||||
|
@ -11,11 +11,21 @@
|
|||||||
#define ENTROPY_MIN_NV_SEED_SIZE \
|
#define ENTROPY_MIN_NV_SEED_SIZE \
|
||||||
MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, MBEDTLS_ENTROPY_BLOCK_SIZE)
|
MAX(MBEDTLS_ENTROPY_MIN_PLATFORM, MBEDTLS_ENTROPY_BLOCK_SIZE)
|
||||||
|
|
||||||
|
#include "psa_crypto_random.h"
|
||||||
|
#if defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
|
||||||
|
/* PSA crypto uses the HMAC_DRBG module. It reads from the entropy source twice:
|
||||||
|
* once for the initial entropy and once for a nonce. The nonce length is
|
||||||
|
* half the entropy length. For SHA-256, SHA-384 or SHA-512, the
|
||||||
|
* entropy length is 256 per the documentation of mbedtls_hmac_drbg_seed(),
|
||||||
|
* and PSA crypto doesn't support other hashes for HMAC_DRBG. */
|
||||||
|
#define ENTROPY_NONCE_LEN ( 256 / 2 )
|
||||||
|
#else
|
||||||
/* PSA crypto uses the CTR_DRBG module. In some configurations, it needs
|
/* PSA crypto uses the CTR_DRBG module. In some configurations, it needs
|
||||||
* to read from the entropy source twice: once for the initial entropy
|
* to read from the entropy source twice: once for the initial entropy
|
||||||
* and once for a nonce. */
|
* and once for a nonce. */
|
||||||
#include "mbedtls/ctr_drbg.h"
|
#include "mbedtls/ctr_drbg.h"
|
||||||
#define ENTROPY_NONCE_LEN MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN
|
#define ENTROPY_NONCE_LEN MBEDTLS_CTR_DRBG_ENTROPY_NONCE_LEN
|
||||||
|
#endif
|
||||||
|
|
||||||
#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user