mirror of
https://github.com/Mbed-TLS/mbedtls.git
synced 2025-04-25 00:02:42 +00:00
Merge pull request #8862 from valeriosetti/issue8825
Improve support of mbedtls_psa_get_random in client-only builds
This commit is contained in:
commit
af3e574f5f
6
ChangeLog.d/8825.txt
Normal file
6
ChangeLog.d/8825.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
Features
|
||||||
|
* mbedtls_psa_get_random() is always available as soon as
|
||||||
|
MBEDTLS_PSA_CRYPTO_CLIENT is enabled at build time and psa_crypto_init() is
|
||||||
|
called at runtime. This together with MBEDTLS_PSA_RANDOM_STATE can be
|
||||||
|
used as random number generator function (f_rng) and context (p_rng) in
|
||||||
|
legacy functions.
|
@ -21,44 +21,24 @@
|
|||||||
* otherwise error codes would be unknown in test_suite_psa_crypto_util.data.*/
|
* otherwise error codes would be unknown in test_suite_psa_crypto_util.data.*/
|
||||||
#include <mbedtls/asn1write.h>
|
#include <mbedtls/asn1write.h>
|
||||||
|
|
||||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
|
||||||
|
|
||||||
/* Expose whatever RNG the PSA subsystem uses to applications using the
|
|
||||||
* mbedtls_xxx API. The declarations and definitions here need to be
|
|
||||||
* consistent with the implementation in library/psa_crypto_random_impl.h.
|
|
||||||
* See that file for implementation documentation. */
|
|
||||||
|
|
||||||
|
|
||||||
/* The type of a `f_rng` random generator function that many library functions
|
|
||||||
* take.
|
|
||||||
*
|
|
||||||
* This type name is not part of the Mbed TLS stable API. It may be renamed
|
|
||||||
* or moved without warning.
|
|
||||||
*/
|
|
||||||
typedef int mbedtls_f_rng_t(void *p_rng, unsigned char *output, size_t output_size);
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
|
||||||
|
|
||||||
/** The random generator function for the PSA subsystem.
|
/** The random generator function for the PSA subsystem.
|
||||||
*
|
*
|
||||||
* This function is suitable as the `f_rng` random generator function
|
* This function is suitable as the `f_rng` random generator function
|
||||||
* parameter of many `mbedtls_xxx` functions. Use #MBEDTLS_PSA_RANDOM_STATE
|
* parameter of many `mbedtls_xxx` functions.
|
||||||
* to obtain the \p p_rng parameter.
|
|
||||||
*
|
*
|
||||||
* The implementation of this function depends on the configuration of the
|
* The implementation of this function depends on the configuration of the
|
||||||
* library.
|
* library.
|
||||||
*
|
*
|
||||||
* \note Depending on the configuration, this may be a function or
|
|
||||||
* a pointer to a function.
|
|
||||||
*
|
|
||||||
* \note This function may only be used if the PSA crypto subsystem is active.
|
* \note This function may only be used if the PSA crypto subsystem is active.
|
||||||
* This means that you must call psa_crypto_init() before any call to
|
* This means that you must call psa_crypto_init() before any call to
|
||||||
* this function, and you must not call this function after calling
|
* this function, and you must not call this function after calling
|
||||||
* mbedtls_psa_crypto_free().
|
* mbedtls_psa_crypto_free().
|
||||||
*
|
*
|
||||||
* \param p_rng The random generator context. This must be
|
* \param p_rng This parameter is only kept for backward compatibility
|
||||||
* #MBEDTLS_PSA_RANDOM_STATE. No other state is
|
* reasons with legacy `f_rng` functions and it's ignored.
|
||||||
* supported.
|
* Set to #MBEDTLS_PSA_RANDOM_STATE or NULL.
|
||||||
* \param output The buffer to fill. It must have room for
|
* \param output The buffer to fill. It must have room for
|
||||||
* \c output_size bytes.
|
* \c output_size bytes.
|
||||||
* \param output_size The number of bytes to write to \p output.
|
* \param output_size The number of bytes to write to \p output.
|
||||||
@ -80,33 +60,12 @@ int mbedtls_psa_get_random(void *p_rng,
|
|||||||
|
|
||||||
/** The random generator state for the PSA subsystem.
|
/** The random generator state for the PSA subsystem.
|
||||||
*
|
*
|
||||||
* This macro expands to an expression which is suitable as the `p_rng`
|
* This macro always expands to NULL because the `p_rng` parameter is unused
|
||||||
* random generator state parameter of many `mbedtls_xxx` functions.
|
* in mbedtls_psa_get_random(), but it's kept for interface's backward
|
||||||
* It must be used in combination with the random generator function
|
* compatibility.
|
||||||
* mbedtls_psa_get_random().
|
|
||||||
*
|
|
||||||
* The implementation of this macro depends on the configuration of the
|
|
||||||
* library. Do not make any assumption on its nature.
|
|
||||||
*/
|
*/
|
||||||
#define MBEDTLS_PSA_RANDOM_STATE NULL
|
#define MBEDTLS_PSA_RANDOM_STATE NULL
|
||||||
|
|
||||||
#else /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
|
|
||||||
|
|
||||||
#if defined(MBEDTLS_CTR_DRBG_C)
|
|
||||||
#include "mbedtls/ctr_drbg.h"
|
|
||||||
typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t;
|
|
||||||
static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_ctr_drbg_random;
|
|
||||||
#elif defined(MBEDTLS_HMAC_DRBG_C)
|
|
||||||
#include "mbedtls/hmac_drbg.h"
|
|
||||||
typedef mbedtls_hmac_drbg_context mbedtls_psa_drbg_context_t;
|
|
||||||
static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_hmac_drbg_random;
|
|
||||||
#endif
|
|
||||||
extern mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state;
|
|
||||||
|
|
||||||
#define MBEDTLS_PSA_RANDOM_STATE mbedtls_psa_random_state
|
|
||||||
|
|
||||||
#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
|
|
||||||
|
|
||||||
/** \defgroup psa_tls_helpers TLS helper functions
|
/** \defgroup psa_tls_helpers TLS helper functions
|
||||||
* @{
|
* @{
|
||||||
*/
|
*/
|
||||||
@ -180,7 +139,7 @@ static inline mbedtls_md_type_t mbedtls_md_type_from_psa_alg(psa_algorithm_t psa
|
|||||||
{
|
{
|
||||||
return (mbedtls_md_type_t) (psa_alg & PSA_ALG_HASH_MASK);
|
return (mbedtls_md_type_t) (psa_alg & PSA_ALG_HASH_MASK);
|
||||||
}
|
}
|
||||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
|
||||||
|
|
||||||
#if defined(MBEDTLS_PSA_UTIL_HAVE_ECDSA)
|
#if defined(MBEDTLS_PSA_UTIL_HAVE_ECDSA)
|
||||||
|
|
||||||
|
@ -101,11 +101,6 @@ typedef struct {
|
|||||||
|
|
||||||
static psa_global_data_t global_data;
|
static psa_global_data_t global_data;
|
||||||
|
|
||||||
#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
|
||||||
mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
|
|
||||||
&global_data.rng.drbg;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#define GUARD_MODULE_INITIALIZED \
|
#define GUARD_MODULE_INITIALIZED \
|
||||||
if (global_data.initialized == 0) \
|
if (global_data.initialized == 0) \
|
||||||
return PSA_ERROR_BAD_STATE;
|
return PSA_ERROR_BAD_STATE;
|
||||||
@ -7103,7 +7098,7 @@ static void mbedtls_psa_random_init(mbedtls_psa_random_context_t *rng)
|
|||||||
MBEDTLS_ENTROPY_SOURCE_STRONG);
|
MBEDTLS_ENTROPY_SOURCE_STRONG);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
mbedtls_psa_drbg_init(MBEDTLS_PSA_RANDOM_STATE);
|
mbedtls_psa_drbg_init(&rng->drbg);
|
||||||
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -7114,7 +7109,7 @@ static void mbedtls_psa_random_free(mbedtls_psa_random_context_t *rng)
|
|||||||
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
||||||
memset(rng, 0, sizeof(*rng));
|
memset(rng, 0, sizeof(*rng));
|
||||||
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
||||||
mbedtls_psa_drbg_free(MBEDTLS_PSA_RANDOM_STATE);
|
mbedtls_psa_drbg_free(&rng->drbg);
|
||||||
rng->entropy_free(&rng->entropy);
|
rng->entropy_free(&rng->entropy);
|
||||||
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
||||||
}
|
}
|
||||||
@ -7129,7 +7124,7 @@ static psa_status_t mbedtls_psa_random_seed(mbedtls_psa_random_context_t *rng)
|
|||||||
return PSA_SUCCESS;
|
return PSA_SUCCESS;
|
||||||
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
||||||
const unsigned char drbg_seed[] = "PSA";
|
const unsigned char drbg_seed[] = "PSA";
|
||||||
int ret = mbedtls_psa_drbg_seed(&rng->entropy,
|
int ret = mbedtls_psa_drbg_seed(&rng->drbg, &rng->entropy,
|
||||||
drbg_seed, sizeof(drbg_seed) - 1);
|
drbg_seed, sizeof(drbg_seed) - 1);
|
||||||
return mbedtls_to_psa_error(ret);
|
return mbedtls_to_psa_error(ret);
|
||||||
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
||||||
@ -7159,12 +7154,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_get_random(MBEDTLS_PSA_RANDOM_STATE,
|
#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);
|
||||||
}
|
}
|
||||||
@ -7175,39 +7174,6 @@ psa_status_t psa_generate_random(uint8_t *output,
|
|||||||
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Wrapper function allowing the classic API to use the PSA RNG.
|
|
||||||
*
|
|
||||||
* `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
|
|
||||||
* `psa_generate_random(...)`. The state parameter is ignored since the
|
|
||||||
* PSA API doesn't support passing an explicit state.
|
|
||||||
*
|
|
||||||
* In the non-external case, psa_generate_random() calls an
|
|
||||||
* `mbedtls_xxx_drbg_random` function which has exactly the same signature
|
|
||||||
* and semantics as mbedtls_psa_get_random(). As an optimization,
|
|
||||||
* instead of doing this back-and-forth between the PSA API and the
|
|
||||||
* classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
|
|
||||||
* as a constant function pointer to `mbedtls_xxx_drbg_random`.
|
|
||||||
*/
|
|
||||||
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
|
||||||
int mbedtls_psa_get_random(void *p_rng,
|
|
||||||
unsigned char *output,
|
|
||||||
size_t output_size)
|
|
||||||
{
|
|
||||||
/* This function takes a pointer to the RNG state because that's what
|
|
||||||
* classic mbedtls functions using an RNG expect. The PSA RNG manages
|
|
||||||
* its own state internally and doesn't let the caller access that state.
|
|
||||||
* So we just ignore the state parameter, and in practice we'll pass
|
|
||||||
* NULL. */
|
|
||||||
(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)
|
#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
|
||||||
psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
|
psa_status_t mbedtls_psa_inject_entropy(const uint8_t *seed,
|
||||||
size_t seed_size)
|
size_t seed_size)
|
||||||
|
@ -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
|
||||||
@ -22,22 +14,12 @@
|
|||||||
|
|
||||||
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
|
||||||
|
|
||||||
#include <string.h>
|
|
||||||
#include <mbedtls/entropy.h> // only for error codes
|
|
||||||
#include <psa/crypto.h>
|
|
||||||
|
|
||||||
typedef mbedtls_psa_external_random_context_t mbedtls_psa_random_context_t;
|
typedef mbedtls_psa_external_random_context_t mbedtls_psa_random_context_t;
|
||||||
|
|
||||||
/* Trivial wrapper around psa_generate_random(). */
|
|
||||||
int mbedtls_psa_get_random(void *p_rng,
|
|
||||||
unsigned char *output,
|
|
||||||
size_t output_size);
|
|
||||||
|
|
||||||
/* The PSA RNG API doesn't need any externally maintained state. */
|
|
||||||
#define MBEDTLS_PSA_RANDOM_STATE NULL
|
|
||||||
|
|
||||||
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
||||||
|
|
||||||
|
#include "mbedtls/entropy.h"
|
||||||
|
|
||||||
/* Choose a DRBG based on configuration and availability */
|
/* Choose a DRBG based on configuration and availability */
|
||||||
#if defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
|
#if defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
|
||||||
|
|
||||||
@ -67,11 +49,37 @@ int mbedtls_psa_get_random(void *p_rng,
|
|||||||
#error "No hash algorithm available for HMAC_DBRG."
|
#error "No hash algorithm available for HMAC_DBRG."
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#else
|
#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*/
|
||||||
|
|
||||||
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
||||||
|
#include "mbedtls/ctr_drbg.h"
|
||||||
|
#elif defined(MBEDTLS_HMAC_DRBG_C)
|
||||||
|
#include "mbedtls/hmac_drbg.h"
|
||||||
|
#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
|
#endif
|
||||||
|
|
||||||
#include "mbedtls/entropy.h"
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
||||||
|
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 /* !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.
|
||||||
*
|
*
|
||||||
@ -99,63 +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;
|
|
||||||
|
|
||||||
/* Defined in include/psa_util_internal.h so that it's visible to
|
|
||||||
* application code. The declaration here is redundant, but included
|
|
||||||
* as a safety net to make it more likely that a future change that
|
|
||||||
* accidentally causes the implementation to diverge from the interface
|
|
||||||
* will be noticed. */
|
|
||||||
/* Do not include the declaration under MSVC because it doesn't accept it
|
|
||||||
* ("error C2370: 'mbedtls_psa_get_random' : redefinition; different storage class").
|
|
||||||
* Observed with Visual Studio 2013. A known bug apparently:
|
|
||||||
* https://stackoverflow.com/questions/8146541/duplicate-external-static-declarations-not-allowed-in-visual-studio
|
|
||||||
*/
|
|
||||||
#if !defined(_MSC_VER)
|
|
||||||
static mbedtls_f_rng_t *const mbedtls_psa_get_random;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
/** 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
|
|
||||||
|
|
||||||
/** A pointer to the PSA DRBG state.
|
|
||||||
*
|
|
||||||
* This variable is only intended to be used through the macro
|
|
||||||
* #MBEDTLS_PSA_RANDOM_STATE.
|
|
||||||
*/
|
|
||||||
/* psa_crypto.c sets this variable to a pointer to the DRBG state in the
|
|
||||||
* global PSA crypto state. */
|
|
||||||
/* The type `mbedtls_psa_drbg_context_t` is defined in
|
|
||||||
* include/psa_util_internal.h so that `mbedtls_psa_random_state` can be
|
|
||||||
* declared there and be visible to application code. */
|
|
||||||
extern mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state;
|
|
||||||
|
|
||||||
/** A pointer to the PSA DRBG state.
|
|
||||||
*
|
|
||||||
* This macro expands to an expression that is suitable as the \c p_rng
|
|
||||||
* parameter to pass to mbedtls_psa_get_random().
|
|
||||||
*
|
|
||||||
* This macro exists in all configurations where the psa_crypto module is
|
|
||||||
* enabled. Its expansion depends on the configuration.
|
|
||||||
*/
|
|
||||||
#define MBEDTLS_PSA_RANDOM_STATE mbedtls_psa_random_state
|
|
||||||
|
|
||||||
/** 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.
|
||||||
@ -167,23 +118,15 @@ extern mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state;
|
|||||||
* \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_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(MBEDTLS_PSA_RANDOM_STATE,
|
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(MBEDTLS_PSA_RANDOM_STATE,
|
|
||||||
md_info,
|
|
||||||
mbedtls_entropy_func,
|
|
||||||
entropy,
|
|
||||||
custom, len);
|
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,7 @@
|
|||||||
|
|
||||||
#include "psa_util_internal.h"
|
#include "psa_util_internal.h"
|
||||||
|
|
||||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
|
||||||
|
|
||||||
#include <psa/crypto.h>
|
#include <psa/crypto.h>
|
||||||
|
|
||||||
@ -46,6 +46,7 @@
|
|||||||
#if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA)
|
#if defined(MBEDTLS_BLOCK_CIPHER_SOME_PSA)
|
||||||
#include <mbedtls/cipher.h>
|
#include <mbedtls/cipher.h>
|
||||||
#endif
|
#endif
|
||||||
|
#include <mbedtls/entropy.h>
|
||||||
|
|
||||||
/* PSA_SUCCESS is kept at the top of each error table since
|
/* PSA_SUCCESS is kept at the top of each error table since
|
||||||
* it's the most common status when everything functions properly. */
|
* it's the most common status when everything functions properly. */
|
||||||
@ -338,7 +339,31 @@ mbedtls_ecp_group_id mbedtls_ecc_group_from_psa(psa_ecc_family_t family,
|
|||||||
}
|
}
|
||||||
#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
|
#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
|
||||||
|
|
||||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
/* Wrapper function allowing the classic API to use the PSA RNG.
|
||||||
|
*
|
||||||
|
* `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
|
||||||
|
* `psa_generate_random(...)`. The state parameter is ignored since the
|
||||||
|
* PSA API doesn't support passing an explicit state.
|
||||||
|
*/
|
||||||
|
int mbedtls_psa_get_random(void *p_rng,
|
||||||
|
unsigned char *output,
|
||||||
|
size_t output_size)
|
||||||
|
{
|
||||||
|
/* This function takes a pointer to the RNG state because that's what
|
||||||
|
* classic mbedtls functions using an RNG expect. The PSA RNG manages
|
||||||
|
* its own state internally and doesn't let the caller access that state.
|
||||||
|
* So we just ignore the state parameter, and in practice we'll pass
|
||||||
|
* NULL. */
|
||||||
|
(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_CLIENT */
|
||||||
|
|
||||||
#if defined(MBEDTLS_PSA_UTIL_HAVE_ECDSA)
|
#if defined(MBEDTLS_PSA_UTIL_HAVE_ECDSA)
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
#include "psa/crypto.h"
|
#include "psa/crypto.h"
|
||||||
|
|
||||||
#if defined(MBEDTLS_PSA_CRYPTO_C)
|
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
|
||||||
|
|
||||||
/*************************************************************************
|
/*************************************************************************
|
||||||
* FFDH
|
* FFDH
|
||||||
@ -96,5 +96,5 @@ int psa_pk_status_to_mbedtls(psa_status_t status);
|
|||||||
sizeof(error_list)/sizeof(error_list[0]), \
|
sizeof(error_list)/sizeof(error_list[0]), \
|
||||||
fallback_f)
|
fallback_f)
|
||||||
|
|
||||||
#endif /* MBEDTLS_PSA_CRYPTO_C */
|
#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
|
||||||
#endif /* MBEDTLS_PSA_UTIL_INTERNAL_H */
|
#endif /* MBEDTLS_PSA_UTIL_INTERNAL_H */
|
||||||
|
25
tests/src/psa_crypto_stubs.c
Normal file
25
tests/src/psa_crypto_stubs.c
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
/** \file psa_crypto_stubs.c
|
||||||
|
*
|
||||||
|
* \brief Stub functions when MBEDTLS_PSA_CRYPTO_CLIENT is enabled but
|
||||||
|
* MBEDTLS_PSA_CRYPTO_C is disabled.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright The Mbed TLS Contributors
|
||||||
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <psa/crypto.h>
|
||||||
|
|
||||||
|
#if defined(MBEDTLS_PSA_CRYPTO_CLIENT) && !defined(MBEDTLS_PSA_CRYPTO_C)
|
||||||
|
|
||||||
|
psa_status_t psa_generate_random(uint8_t *output,
|
||||||
|
size_t output_size)
|
||||||
|
{
|
||||||
|
(void) output;
|
||||||
|
(void) output_size;
|
||||||
|
|
||||||
|
return PSA_ERROR_COMMUNICATION_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* MBEDTLS_PSA_CRYPTO_CLIENT && !MBEDTLS_PSA_CRYPTO_C */
|
Loading…
x
Reference in New Issue
Block a user