psa_crypto_random_impl: minor fixes

- define mbedtls_psa_random_context_t even when MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
  is defined
- define mbedtls_psa_legacy_get_random as pointer to function instead
  of #define to make "check_names" test happy.

Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
This commit is contained in:
Valerio Setti 2024-02-27 11:58:39 +01:00
parent 061d4e4655
commit 718180c7b5
2 changed files with 33 additions and 49 deletions

View File

@ -7373,12 +7373,16 @@ psa_status_t psa_generate_random(uint8_t *output,
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
while (output_size > 0) { while (output_size > 0) {
int ret = MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED;
size_t request_size = size_t request_size =
(output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ? (output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
MBEDTLS_PSA_RANDOM_MAX_REQUEST : MBEDTLS_PSA_RANDOM_MAX_REQUEST :
output_size); output_size);
int ret = mbedtls_psa_legacy_get_random(&global_data.rng.drbg, #if defined(MBEDTLS_CTR_DRBG_C)
output, request_size); ret = mbedtls_ctr_drbg_random(&global_data.rng.drbg, output, request_size);
#elif defined(MBEDTLS_HMAC_DRBG_C)
ret = mbedtls_hmac_drbg_random(&global_data.rng.drbg, output, request_size);
#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
if (ret != 0) { if (ret != 0) {
return mbedtls_to_psa_error(ret); return mbedtls_to_psa_error(ret);
} }

View File

@ -1,14 +1,6 @@
/** \file psa_crypto_random_impl.h /** \file psa_crypto_random_impl.h
* *
* \brief PSA crypto random generator implementation abstraction. * \brief PSA crypto random generator implementation abstraction.
*
* The definitions here need to be consistent with the declarations
* in include/psa_util_internal.h. This file contains some redundant
* declarations to increase the chance that a compiler will detect
* inconsistencies if one file is changed without updating the other,
* but not all potential inconsistencies can be enforced, so make sure
* to check the public declarations and contracts in
* include/psa_util_internal.h if you modify this file.
*/ */
/* /*
* Copyright The Mbed TLS Contributors * Copyright The Mbed TLS Contributors
@ -20,7 +12,11 @@
#include "psa_util_internal.h" #include "psa_util_internal.h"
#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
typedef mbedtls_psa_external_random_context_t mbedtls_psa_random_context_t;
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
#include "mbedtls/entropy.h" #include "mbedtls/entropy.h"
@ -54,7 +50,9 @@
#endif #endif
#else /* !MBEDTLS_PSA_HMAC_DRBG_MD_TYPE && !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C*/ #else /* !MBEDTLS_PSA_HMAC_DRBG_MD_TYPE && !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C*/
#error "No DRBG module available for the psa_crypto module." #error "No DRBG module available for the psa_crypto module."
#endif /* !MBEDTLS_PSA_HMAC_DRBG_MD_TYPE && !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C*/ #endif /* !MBEDTLS_PSA_HMAC_DRBG_MD_TYPE && !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C*/
#if defined(MBEDTLS_CTR_DRBG_C) #if defined(MBEDTLS_CTR_DRBG_C)
@ -63,14 +61,26 @@
#include "mbedtls/hmac_drbg.h" #include "mbedtls/hmac_drbg.h"
#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */ #endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
/* The maximum number of bytes that mbedtls_psa_get_random() is expected to return. */
#if defined(MBEDTLS_CTR_DRBG_C)
#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
#if defined(MBEDTLS_CTR_DRBG_C) #if defined(MBEDTLS_CTR_DRBG_C)
#define mbedtls_psa_legacy_get_random mbedtls_ctr_drbg_random
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) #elif defined(MBEDTLS_HMAC_DRBG_C)
#define mbedtls_psa_legacy_get_random mbedtls_hmac_drbg_random
typedef mbedtls_hmac_drbg_context mbedtls_psa_drbg_context_t; typedef mbedtls_hmac_drbg_context mbedtls_psa_drbg_context_t;
#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */ #endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
typedef struct {
void (* entropy_init)(mbedtls_entropy_context *ctx);
void (* entropy_free)(mbedtls_entropy_context *ctx);
mbedtls_entropy_context entropy;
mbedtls_psa_drbg_context_t drbg;
} mbedtls_psa_random_context_t;
/** Initialize the PSA DRBG. /** Initialize the PSA DRBG.
* *
* \param p_rng Pointer to the Mbed TLS DRBG state. * \param p_rng Pointer to the Mbed TLS DRBG state.
@ -97,27 +107,6 @@ static inline void mbedtls_psa_drbg_free(mbedtls_psa_drbg_context_t *p_rng)
#endif #endif
} }
/** The type of the PSA random generator context.
*
* The random generator context is composed of an entropy context and
* a DRBG context.
*/
typedef struct {
void (* entropy_init)(mbedtls_entropy_context *ctx);
void (* entropy_free)(mbedtls_entropy_context *ctx);
mbedtls_entropy_context entropy;
mbedtls_psa_drbg_context_t drbg;
} mbedtls_psa_random_context_t;
/** The maximum number of bytes that mbedtls_psa_get_random() is expected to
* return.
*/
#if defined(MBEDTLS_CTR_DRBG_C)
#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
/** Seed the PSA DRBG. /** Seed the PSA DRBG.
* *
* \param entropy An entropy context to read the seed from. * \param entropy An entropy context to read the seed from.
@ -129,24 +118,15 @@ typedef struct {
* \return \c 0 on success. * \return \c 0 on success.
* \return An Mbed TLS error code (\c MBEDTLS_ERR_xxx) on failure. * \return An Mbed TLS error code (\c MBEDTLS_ERR_xxx) on failure.
*/ */
static inline int mbedtls_psa_drbg_seed( static inline int mbedtls_psa_drbg_seed(mbedtls_psa_drbg_context_t *drbg_ctx,
mbedtls_psa_drbg_context_t *drbg_ctx, mbedtls_entropy_context *entropy,
mbedtls_entropy_context *entropy, const unsigned char *custom, size_t len)
const unsigned char *custom, size_t len)
{ {
#if defined(MBEDTLS_CTR_DRBG_C) #if defined(MBEDTLS_CTR_DRBG_C)
return mbedtls_ctr_drbg_seed(drbg_ctx, return mbedtls_ctr_drbg_seed(drbg_ctx, mbedtls_entropy_func, entropy, custom, len);
mbedtls_entropy_func,
entropy,
custom, len);
#elif defined(MBEDTLS_HMAC_DRBG_C) #elif defined(MBEDTLS_HMAC_DRBG_C)
const mbedtls_md_info_t *md_info = const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE);
mbedtls_md_info_from_type(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE); return mbedtls_hmac_drbg_seed(drbg_ctx, md_info, mbedtls_entropy_func, entropy, custom, len);
return mbedtls_hmac_drbg_seed(drbg_ctx,
md_info,
mbedtls_entropy_func,
entropy,
custom, len);
#endif #endif
} }