Make mbedtls_psa_get_random more usable outside psa_crypto.c

In the external RNG case, don't make mbedtls_psa_get_random() a
static inline function: this would likely result in identical
instances of this function in every module that uses it. Instead, make
it a single function with external linkage.

In the non-external case, instead of a trivial wrapper function, make
mbedtls_psa_get_random a constant pointer to whichever DRBG function
is being used.

Signed-off-by: Gilles Peskine <Gilles.Peskine@arm.com>
This commit is contained in:
Gilles Peskine 2020-12-14 15:33:44 +01:00
parent 5894e8e7a4
commit 8814fc4a34
2 changed files with 27 additions and 18 deletions

View File

@ -6413,6 +6413,24 @@ psa_status_t psa_generate_random( uint8_t *output,
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
}
/* Wrapper function allowing the classic API to use the PSA RNG.
* In the non-external case, mbedtls_psa_get_random is defined
* as a constant function pointer in psa_crypto_random.h.
*/
#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
int mbedtls_psa_get_random( void *p_rng,
unsigned char *output,
size_t output_size )
{
(void) p_rng;
psa_status_t status = psa_generate_random( output, output_size );
if( status == PSA_SUCCESS )
return( 0 );
else
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
}
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
#include "mbedtls/entropy_poll.h"

View File

@ -30,17 +30,9 @@
typedef mbedtls_psa_external_random_context_t mbedtls_psa_random_context_t;
static inline int mbedtls_psa_get_random( void *p_rng,
int mbedtls_psa_get_random( void *p_rng,
unsigned char *output,
size_t output_size )
{
(void) p_rng;
psa_status_t status = psa_generate_random( output, output_size );
if( status == PSA_SUCCESS )
return( 0 );
else
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
}
size_t output_size );
#define MBEDTLS_PSA_RANDOM_STATE NULL
@ -144,16 +136,15 @@ typedef struct
* \return \c MBEDTLS_ERR_xxx_DRBG_xxx or
* \c MBEDTLS_ERR_PLATFORM_xxx on failure.
*/
static inline int mbedtls_psa_get_random( void *p_rng,
unsigned char *output,
size_t output_len )
{
#if defined(MBEDTLS_CTR_DRBG_C)
return( mbedtls_ctr_drbg_random( p_rng, output, output_len ) );
static int ( *const mbedtls_psa_get_random )(
void *p_rng, unsigned char *output, size_t output_size ) =
mbedtls_ctr_drbg_random;
#elif defined(MBEDTLS_HMAC_DRBG_C)
return( mbedtls_hmac_drbg_random( p_rng, output, output_len ) );
static int ( *const mbedtls_psa_get_random )(
void *p_rng, unsigned char *output, size_t output_size ) =
mbedtls_hmac_drbg_random;
#endif
}
/** The maximum number of bytes that mbedtls_psa_get_random() is expected to
* return.