Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alternative random generator support for PSA #3895

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
90edc99
Create a header file for PSA crypto random generator abstraction
gilles-peskine-arm Nov 13, 2020
30524eb
psa_crypto: create random and drbg abstraction
gilles-peskine-arm Nov 13, 2020
f08b3f8
Autonomous random driver: create configuration option
gilles-peskine-arm Nov 13, 2020
514a8fd
Create a file for PSA crypto test helpers
gilles-peskine-arm Nov 13, 2020
b8af228
Autonomous random driver: declare the type and function
gilles-peskine-arm Nov 13, 2020
1c49f1a
Include headers in psa_crypto.h for mbedtls_to_psa_error
gilles-peskine-arm Nov 13, 2020
4fc21fd
Implement MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
gilles-peskine-arm Nov 13, 2020
82e57d1
PSA: support HMAC_DRBG
gilles-peskine-arm Nov 13, 2020
ed03890
PSA: allow the configuration to favor HMAC_DRBG
gilles-peskine-arm Nov 13, 2020
68cc434
PSA support for HMAC_DRBG: changelog entry
gilles-peskine-arm Nov 13, 2020
14c332b
Fix a Doxygen warning
gilles-peskine-arm Nov 14, 2020
89ffb28
Fix option compatibility check
gilles-peskine-arm Nov 18, 2020
b663a60
Note the expectations on mbedtls_psa_external_get_random()
gilles-peskine-arm Nov 18, 2020
c096301
Document mbedtls_psa_external_random_context_t
gilles-peskine-arm Nov 18, 2020
c109b37
Test MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
gilles-peskine-arm Nov 23, 2020
bee96c8
Explain the conditions for checking DRBG error codes
gilles-peskine-arm Nov 23, 2020
b0a748e
Copyediting
gilles-peskine-arm Nov 30, 2020
e995b9b
Clarify statuses from mbedtls_psa_external_get_random
gilles-peskine-arm Nov 30, 2020
5894e8e
Replace mbedtls_psa_random_state( ... ) by MBEDTLS_PSA_RANDOM_STATE
gilles-peskine-arm Dec 14, 2020
8814fc4
Make mbedtls_psa_get_random more usable outside psa_crypto.c
gilles-peskine-arm Dec 14, 2020
b2b64d3
Rename psa_crypto_random.h to psa_crypto_random_impl.h
gilles-peskine-arm Dec 14, 2020
b3cd963
Pacify check-names.sh
gilles-peskine-arm Dec 14, 2020
88fa5c4
Minor documentation improvements
gilles-peskine-arm Jan 4, 2021
71ddab9
Simplify the chunk loop in psa_generate_random
gilles-peskine-arm Jan 4, 2021
0c59ba8
Fix the error detection in psa_generate_random
gilles-peskine-arm Jan 5, 2021
9c3e060
Explain the design of mbedtls_psa_get_random better
gilles-peskine-arm Jan 5, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions ChangeLog.d/psa-crypto-hmac-drbg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Features
* The PSA crypto subsystem can now use HMAC_DRBG instead of CTR_DRBG.
CTR_DRBG is used by default if it is available, but you can override
this choice by setting MBEDTLS_PSA_HMAC_DRBG_MD_TYPE at compile time.
Fix #3354.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, I'm deliberately not mentioning external RNG support in the changelog in this PR. I'll add a changelog entry in the follow-up that makes an external PSA RNG usable from TLS code.

14 changes: 10 additions & 4 deletions include/mbedtls/check_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,11 @@
#error "MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO and MBEDTLS_PLATFORM_STD_NV_SEED_WRITE cannot be defined simultaneously"
#endif

#if defined(MBEDTLS_PSA_CRYPTO_C) && \
!( defined(MBEDTLS_CTR_DRBG_C) && \
defined(MBEDTLS_ENTROPY_C) )
#error "MBEDTLS_PSA_CRYPTO_C defined, but not all prerequisites"
#if defined(MBEDTLS_PSA_CRYPTO_C) && \
!( ( ( defined(MBEDTLS_CTR_DRBG_C) || defined(MBEDTLS_HMAC_DRBG_C) ) && \
defined(MBEDTLS_ENTROPY_C) ) || \
defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) )
#error "MBEDTLS_PSA_CRYPTO_C defined, but not all prerequisites (missing RNG)"
#endif

#if defined(MBEDTLS_PSA_CRYPTO_SPM) && !defined(MBEDTLS_PSA_CRYPTO_C)
Expand Down Expand Up @@ -604,6 +605,11 @@
#error "MBEDTLS_PSA_INJECT_ENTROPY is not compatible with actual entropy sources"
#endif

#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
#error "MBEDTLS_PSA_INJECT_ENTROPY is not compatible with MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG"
#endif

#if defined(MBEDTLS_PSA_ITS_FILE_C) && \
!defined(MBEDTLS_FS_IO)
#error "MBEDTLS_PSA_ITS_FILE_C defined, but not all prerequisites"
Expand Down
55 changes: 54 additions & 1 deletion include/mbedtls/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,44 @@
*/
//#define MBEDTLS_PSA_CRYPTO_DRIVERS

/** \def MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG
*
* Make the PSA Crypto module use an external random generator provided
* by a driver, instead of Mbed TLS's entropy and DRBG modules.
*
* \note This random generator must deliver random numbers with cryptographic
* quality and high performance. It must supply unpredictable numbers
* with a uniform distribution. The implementation of this function
* is responsible for ensuring that the random generator is seeded
* with sufficient entropy. If you have a hardware TRNG which is slow
* or delivers non-uniform output, declare it as an entropy source
* with mbedtls_entropy_add_source() instead of enabling this option.
*
* If you enable this option, you must configure the type
* ::mbedtls_psa_external_random_context_t in psa/crypto_platform.h
* and define a function called mbedtls_psa_external_get_random()
* with the following prototype:
* ```
* psa_status_t mbedtls_psa_external_get_random(
* mbedtls_psa_external_random_context_t *context,
* uint8_t *output, size_t output_size, size_t *output_length);
* );
* ```
* The \c context value is initialized to 0 before the first call.
* The function must fill the \c output buffer with \p output_size bytes
* of random data and set \c *output_length to \p output_size.
ronald-cron-arm marked this conversation as resolved.
Show resolved Hide resolved
*
* Requires: MBEDTLS_PSA_CRYPTO_C
*
* \warning If you enable this option, code that uses the PSA cryptography
* interface will not use any of the entropy sources set up for
* the entropy module, nor the NV seed that MBEDTLS_ENTROPY_NV_SEED
* enables.
*
* \note This option is experimental and may be removed without notice.
*/
//#define MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG

/**
* \def MBEDTLS_PSA_CRYPTO_SPM
*
Expand Down Expand Up @@ -3115,7 +3153,9 @@
*
* Module: library/psa_crypto.c
*
* Requires: MBEDTLS_CTR_DRBG_C, 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.
*
*/
#define MBEDTLS_PSA_CRYPTO_C
Expand Down Expand Up @@ -3603,6 +3643,19 @@
*/
//#define MBEDTLS_PARAM_FAILED( cond ) assert( cond )

/* PSA options */
/**
* Use HMAC_DRBG with the specified hash algorithm for HMAC_DRBG for the
* PSA crypto subsystem.
*
* If this option is unset:
* - If CTR_DRBG is available, the PSA subsystem uses it rather than HMAC_DRBG.
* - Otherwise, the PSA subsystem uses HMAC_DRBG with either
* #MBEDTLS_MD_SHA512 or #MBEDTLS_MD_SHA256 based on availability and
* on unspecified heuristics.
*/
//#define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA256

/* SSL Cache options */
//#define MBEDTLS_SSL_CACHE_DEFAULT_TIMEOUT 86400 /**< 1 day */
//#define MBEDTLS_SSL_CACHE_DEFAULT_MAX_ENTRIES 50 /**< Maximum entries in cache */
Expand Down
51 changes: 51 additions & 0 deletions include/psa/crypto_extra.h
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,57 @@ mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,

/**@}*/

/** \defgroup psa_external_rng External random generator
* @{
*/

#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
/** External random generator function, implemented by the platform.
*
* When the compile-time option #MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled,
* this function replaces Mbed TLS's entropy and DRBG modules for all
* random generation triggered via PSA crypto interfaces.
*
* \note This random generator must deliver random numbers with cryptographic
* quality and high performance. It must supply unpredictable numbers
* with a uniform distribution. The implementation of this function
* is responsible for ensuring that the random generator is seeded
* with sufficient entropy. If you have a hardware TRNG which is slow
* or delivers non-uniform output, declare it as an entropy source
* with mbedtls_entropy_add_source() instead of enabling this option.
*
* \param[in,out] context Pointer to the random generator context.
* This is all-bits-zero on the first call
* and preserved between successive calls.
* \param[out] output Output buffer. On success, this buffer
* contains random data with a uniform
* distribution.
* \param output_size The size of the \p output buffer in bytes.
ronald-cron-arm marked this conversation as resolved.
Show resolved Hide resolved
* \param[out] output_length On success, set this value to \p output_size.
ronald-cron-arm marked this conversation as resolved.
Show resolved Hide resolved
*
* \retval #PSA_SUCCESS
* Success. The output buffer contains \p output_size bytes of
* cryptographic-quality random data, and \c *output_length is
* set to \p output_size.
* \retval #PSA_ERROR_INSUFFICIENT_ENTROPY
* The random generator requires extra entropy and there is no
* way to obtain entropy under current environment conditions.
* This error should not happen under normal circumstances since
* this function is responsible for obtaining as much entropy as
* it needs. However implementations of this function may return
* #PSA_ERROR_INSUFFICIENT_ENTROPY if there is no way to obtain
* entropy without blocking indefinitely.
* \retval #PSA_ERROR_HARDWARE_FAILURE
ronald-cron-arm marked this conversation as resolved.
Show resolved Hide resolved
* A failure of the random generator hardware that isn't covered
* by #PSA_ERROR_INSUFFICIENT_ENTROPY.
*/
psa_status_t mbedtls_psa_external_get_random(
mbedtls_psa_external_random_context_t *context,
uint8_t *output, size_t output_size, size_t *output_length );
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */

/**@}*/

#ifdef __cplusplus
}
#endif
Expand Down
15 changes: 15 additions & 0 deletions include/psa/crypto_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,19 @@ static inline int mbedtls_key_owner_id_equal( mbedtls_key_owner_id_t id1,

#endif /* MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER */

#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
/** The type of the context passed to mbedtls_psa_external_get_random().
*
* Mbed TLS initializes the context to all-bits-zero before calling
* mbedtls_psa_external_get_random() for the first time.
*
* The definition of this type in the Mbed TLS source code is for
* demonstration purposes. Implementers of mbedtls_psa_external_get_random()
* are expected to replace it with a custom definition.
*/
typedef struct {
uintptr_t opaque[2];
} mbedtls_psa_external_random_context_t;
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */

#endif /* PSA_CRYPTO_PLATFORM_H */
Loading