2020-12-14 16:43:58 +01:00
|
|
|
/** \file psa_crypto_random_impl.h
|
2020-11-13 15:39:19 +01:00
|
|
|
*
|
2020-12-14 16:43:58 +01:00
|
|
|
* \brief PSA crypto random generator implementation abstraction.
|
2021-02-03 20:04:08 +01:00
|
|
|
*
|
|
|
|
* The definitions here need to be consistent with the declarations
|
2023-06-07 13:07:21 +02:00
|
|
|
* in include/psa_util_internal.h. This file contains some redundant
|
2021-02-16 16:50:00 +01:00
|
|
|
* 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
|
2023-06-07 13:07:21 +02:00
|
|
|
* include/psa_util_internal.h if you modify this file.
|
2020-11-13 15:39:19 +01:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2020-11-13 15:39:19 +01:00
|
|
|
*/
|
|
|
|
|
2020-12-14 16:43:58 +01:00
|
|
|
#ifndef PSA_CRYPTO_RANDOM_IMPL_H
|
|
|
|
#define PSA_CRYPTO_RANDOM_IMPL_H
|
2020-11-13 15:39:19 +01:00
|
|
|
|
2023-09-04 10:19:27 +02:00
|
|
|
#include "psa_util_internal.h"
|
2021-02-16 16:50:00 +01:00
|
|
|
|
2020-11-13 18:47:18 +01:00
|
|
|
#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;
|
|
|
|
|
2021-01-05 16:03:55 +01:00
|
|
|
/* Trivial wrapper around psa_generate_random(). */
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_psa_get_random(void *p_rng,
|
|
|
|
unsigned char *output,
|
|
|
|
size_t output_size);
|
2020-11-13 18:47:18 +01:00
|
|
|
|
2021-01-05 16:03:55 +01:00
|
|
|
/* The PSA RNG API doesn't need any externally maintained state. */
|
2020-12-14 14:54:06 +01:00
|
|
|
#define MBEDTLS_PSA_RANDOM_STATE NULL
|
2020-11-13 18:47:18 +01:00
|
|
|
|
|
|
|
#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
|
|
|
|
2020-11-13 21:31:17 +01:00
|
|
|
/* Choose a DRBG based on configuration and availability */
|
|
|
|
#if defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
|
|
|
|
|
|
|
|
#include "mbedtls/hmac_drbg.h"
|
|
|
|
|
|
|
|
#elif defined(MBEDTLS_CTR_DRBG_C)
|
2020-11-13 17:02:26 +01:00
|
|
|
|
|
|
|
#include "mbedtls/ctr_drbg.h"
|
2020-11-13 21:31:17 +01:00
|
|
|
|
|
|
|
#elif defined(MBEDTLS_HMAC_DRBG_C)
|
|
|
|
|
|
|
|
#include "mbedtls/hmac_drbg.h"
|
2023-03-21 17:10:45 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA512) && defined(MBEDTLS_MD_CAN_SHA256)
|
2020-11-13 21:31:17 +01:00
|
|
|
#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
|
2023-03-21 17:10:45 +01:00
|
|
|
#elif defined(MBEDTLS_MD_CAN_SHA512)
|
2020-11-13 21:31:17 +01:00
|
|
|
#define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA512
|
2023-03-21 17:10:45 +01:00
|
|
|
#elif defined(MBEDTLS_MD_CAN_SHA256)
|
2020-11-13 21:31:17 +01:00
|
|
|
#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
|
|
|
|
|
2020-11-13 17:02:26 +01:00
|
|
|
#include "mbedtls/entropy.h"
|
|
|
|
|
|
|
|
/** Initialize the PSA DRBG.
|
|
|
|
*
|
|
|
|
* \param p_rng Pointer to the Mbed TLS DRBG state.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static inline void mbedtls_psa_drbg_init(mbedtls_psa_drbg_context_t *p_rng)
|
2020-11-13 17:02:26 +01:00
|
|
|
{
|
2020-11-13 21:31:17 +01:00
|
|
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_init(p_rng);
|
2020-11-13 21:31:17 +01:00
|
|
|
#elif defined(MBEDTLS_HMAC_DRBG_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_hmac_drbg_init(p_rng);
|
2020-11-13 21:31:17 +01:00
|
|
|
#endif
|
2020-11-13 17:02:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Deinitialize the PSA DRBG.
|
|
|
|
*
|
|
|
|
* \param p_rng Pointer to the Mbed TLS DRBG state.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static inline void mbedtls_psa_drbg_free(mbedtls_psa_drbg_context_t *p_rng)
|
2020-11-13 17:02:26 +01:00
|
|
|
{
|
2020-11-13 21:31:17 +01:00
|
|
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_ctr_drbg_free(p_rng);
|
2020-11-13 21:31:17 +01:00
|
|
|
#elif defined(MBEDTLS_HMAC_DRBG_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_hmac_drbg_free(p_rng);
|
2020-11-13 21:31:17 +01:00
|
|
|
#endif
|
2020-11-13 17:02:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** The type of the PSA random generator context.
|
|
|
|
*
|
|
|
|
* The random generator context is composed of an entropy context and
|
|
|
|
* a DRBG context.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
typedef struct {
|
|
|
|
void (* entropy_init)(mbedtls_entropy_context *ctx);
|
|
|
|
void (* entropy_free)(mbedtls_entropy_context *ctx);
|
2020-11-13 17:02:26 +01:00
|
|
|
mbedtls_entropy_context entropy;
|
|
|
|
mbedtls_psa_drbg_context_t drbg;
|
|
|
|
} mbedtls_psa_random_context_t;
|
|
|
|
|
2023-06-07 13:07:21 +02:00
|
|
|
/* Defined in include/psa_util_internal.h so that it's visible to
|
2021-02-16 18:55:05 +01:00
|
|
|
* 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)
|
2021-02-16 16:50:00 +01:00
|
|
|
static mbedtls_f_rng_t *const mbedtls_psa_get_random;
|
2021-02-16 18:55:05 +01:00
|
|
|
#endif
|
2020-11-13 17:02:26 +01:00
|
|
|
|
|
|
|
/** The maximum number of bytes that mbedtls_psa_get_random() is expected to
|
|
|
|
* return.
|
|
|
|
*/
|
2020-11-13 21:31:17 +01:00
|
|
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
2020-11-13 17:02:26 +01:00
|
|
|
#define MBEDTLS_PSA_RANDOM_MAX_REQUEST MBEDTLS_CTR_DRBG_MAX_REQUEST
|
2020-11-13 21:31:17 +01:00
|
|
|
#elif defined(MBEDTLS_HMAC_DRBG_C)
|
|
|
|
#define MBEDTLS_PSA_RANDOM_MAX_REQUEST MBEDTLS_HMAC_DRBG_MAX_REQUEST
|
|
|
|
#endif
|
2020-11-13 17:02:26 +01:00
|
|
|
|
2020-12-14 14:54:06 +01:00
|
|
|
/** A pointer to the PSA DRBG state.
|
2020-11-13 17:02:26 +01:00
|
|
|
*
|
2020-12-14 14:54:06 +01:00
|
|
|
* This variable is only intended to be used through the macro
|
|
|
|
* #MBEDTLS_PSA_RANDOM_STATE.
|
|
|
|
*/
|
2021-01-05 16:03:55 +01:00
|
|
|
/* psa_crypto.c sets this variable to a pointer to the DRBG state in the
|
|
|
|
* global PSA crypto state. */
|
2021-02-16 16:50:00 +01:00
|
|
|
/* The type `mbedtls_psa_drbg_context_t` is defined in
|
2023-06-07 13:07:21 +02:00
|
|
|
* include/psa_util_internal.h so that `mbedtls_psa_random_state` can be
|
2021-02-16 16:50:00 +01:00
|
|
|
* declared there and be visible to application code. */
|
2020-12-14 14:54:06 +01:00
|
|
|
extern mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state;
|
|
|
|
|
|
|
|
/** A pointer to the PSA DRBG state.
|
2020-11-13 17:02:26 +01:00
|
|
|
*
|
2021-01-04 21:00:53 +01:00
|
|
|
* This macro expands to an expression that is suitable as the \c p_rng
|
2020-12-14 14:54:06 +01:00
|
|
|
* 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.
|
2020-11-13 17:02:26 +01:00
|
|
|
*/
|
2020-12-14 14:54:06 +01:00
|
|
|
#define MBEDTLS_PSA_RANDOM_STATE mbedtls_psa_random_state
|
2020-11-13 17:02:26 +01:00
|
|
|
|
|
|
|
/** Seed the PSA DRBG.
|
|
|
|
*
|
2020-12-14 14:54:06 +01:00
|
|
|
* \param entropy An entropy context to read the seed from.
|
2020-11-13 17:02:26 +01:00
|
|
|
* \param custom The personalization string.
|
|
|
|
* This can be \c NULL, in which case the personalization
|
|
|
|
* string is empty regardless of the value of \p len.
|
|
|
|
* \param len The length of the personalization string.
|
|
|
|
*
|
|
|
|
* \return \c 0 on success.
|
|
|
|
* \return An Mbed TLS error code (\c MBEDTLS_ERR_xxx) on failure.
|
|
|
|
*/
|
|
|
|
static inline int mbedtls_psa_drbg_seed(
|
2020-12-14 14:54:06 +01:00
|
|
|
mbedtls_entropy_context *entropy,
|
2023-01-11 14:50:10 +01:00
|
|
|
const unsigned char *custom, size_t len)
|
2020-11-13 17:02:26 +01:00
|
|
|
{
|
2020-11-13 21:31:17 +01:00
|
|
|
#if defined(MBEDTLS_CTR_DRBG_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_ctr_drbg_seed(MBEDTLS_PSA_RANDOM_STATE,
|
|
|
|
mbedtls_entropy_func,
|
|
|
|
entropy,
|
|
|
|
custom, len);
|
2020-11-13 21:31:17 +01:00
|
|
|
#elif defined(MBEDTLS_HMAC_DRBG_C)
|
|
|
|
const mbedtls_md_info_t *md_info =
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_md_info_from_type(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE);
|
|
|
|
return mbedtls_hmac_drbg_seed(MBEDTLS_PSA_RANDOM_STATE,
|
|
|
|
md_info,
|
|
|
|
mbedtls_entropy_func,
|
|
|
|
entropy,
|
|
|
|
custom, len);
|
2020-11-13 21:31:17 +01:00
|
|
|
#endif
|
2020-11-13 17:02:26 +01:00
|
|
|
}
|
2020-11-13 15:39:19 +01:00
|
|
|
|
2020-11-13 18:47:18 +01:00
|
|
|
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
|
|
|
|
|
2020-12-14 16:43:58 +01:00
|
|
|
#endif /* PSA_CRYPTO_RANDOM_IMPL_H */
|