Gilles Peskine | 90edc99 | 2020-11-13 15:39:19 +0100 | [diff] [blame] | 1 | /** \file psa_crypto_random.h |
| 2 | * |
| 3 | * \brief PSA crypto random generator abstraction. |
| 4 | */ |
| 5 | /* |
| 6 | * Copyright The Mbed TLS Contributors |
| 7 | * SPDX-License-Identifier: Apache-2.0 |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | * not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | */ |
| 21 | |
| 22 | #ifndef PSA_CRYPTO_RANDOM_H |
| 23 | #define PSA_CRYPTO_RANDOM_H |
| 24 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame^] | 25 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 26 | |
| 27 | #include <string.h> |
| 28 | #include <mbedtls/entropy.h> // only for error codes |
| 29 | #include <psa/crypto.h> |
| 30 | |
| 31 | typedef mbedtls_psa_external_random_context_t mbedtls_psa_random_context_t; |
| 32 | |
| 33 | static inline int mbedtls_psa_get_random( void *p_rng, |
| 34 | unsigned char *output, |
| 35 | size_t output_size ) |
| 36 | { |
| 37 | (void) p_rng; |
| 38 | psa_status_t status = psa_generate_random( output, output_size ); |
| 39 | if( status == PSA_SUCCESS ) |
| 40 | return( 0 ); |
| 41 | else |
| 42 | return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED ); |
| 43 | } |
| 44 | |
| 45 | static inline void *mbedtls_psa_random_state( mbedtls_psa_random_context_t *rng ) |
| 46 | { |
| 47 | (void) rng; |
| 48 | return( NULL ); |
| 49 | } |
| 50 | |
| 51 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 52 | |
Gilles Peskine | 30524eb | 2020-11-13 17:02:26 +0100 | [diff] [blame] | 53 | /* Currently, the only supported RNG is Mbed TLS's CTR_DRBG seeded with |
| 54 | * mbedtls_entropy_func(). */ |
| 55 | |
| 56 | #include "mbedtls/ctr_drbg.h" |
| 57 | #include "mbedtls/entropy.h" |
| 58 | |
| 59 | /** The type of the PSA DRBG context. |
| 60 | */ |
| 61 | typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t; |
| 62 | |
| 63 | /** Initialize the PSA DRBG. |
| 64 | * |
| 65 | * \param p_rng Pointer to the Mbed TLS DRBG state. |
| 66 | */ |
| 67 | static inline void mbedtls_psa_drbg_init( mbedtls_psa_drbg_context_t *p_rng ) |
| 68 | { |
| 69 | mbedtls_ctr_drbg_init( p_rng ); |
| 70 | } |
| 71 | |
| 72 | /** Deinitialize the PSA DRBG. |
| 73 | * |
| 74 | * \param p_rng Pointer to the Mbed TLS DRBG state. |
| 75 | */ |
| 76 | static inline void mbedtls_psa_drbg_free( mbedtls_psa_drbg_context_t *p_rng ) |
| 77 | { |
| 78 | mbedtls_ctr_drbg_free( p_rng ); |
| 79 | } |
| 80 | |
| 81 | /** The type of the PSA random generator context. |
| 82 | * |
| 83 | * The random generator context is composed of an entropy context and |
| 84 | * a DRBG context. |
| 85 | */ |
| 86 | typedef struct |
| 87 | { |
| 88 | void (* entropy_init )( mbedtls_entropy_context *ctx ); |
| 89 | void (* entropy_free )( mbedtls_entropy_context *ctx ); |
| 90 | mbedtls_entropy_context entropy; |
| 91 | mbedtls_psa_drbg_context_t drbg; |
| 92 | } mbedtls_psa_random_context_t; |
| 93 | |
| 94 | /** Return random data. |
| 95 | * |
| 96 | * This function is suitable as the \p f_rng parameter to Mbed TLS functions |
| 97 | * that require a random generator. Use mbedtls_psa_random_state() to |
| 98 | * obtain the \p p_rng parameter. |
| 99 | * |
| 100 | * \param p_rng The CTR_DRBG context. This must be |
| 101 | * mbedtls_psa_random_state( \c rng ) |
| 102 | * where \c rng is a pointer to a |
| 103 | * ::mbedtls_psa_random_context_t structure. |
| 104 | * \param output The buffer to fill. |
| 105 | * \param output_len The length of the buffer in bytes. |
| 106 | * It must be at most #MBEDTLS_PSA_RANDOM_MAX_REQUEST. |
| 107 | * |
| 108 | * \return \c 0 on success. |
| 109 | * \return #MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or |
| 110 | * #MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG on failure. |
| 111 | */ |
| 112 | static inline int mbedtls_psa_get_random( void *p_rng, |
| 113 | unsigned char *output, |
| 114 | size_t output_len ) |
| 115 | { |
| 116 | return( mbedtls_ctr_drbg_random( p_rng, output, output_len ) ); |
| 117 | } |
| 118 | |
| 119 | /** The maximum number of bytes that mbedtls_psa_get_random() is expected to |
| 120 | * return. |
| 121 | */ |
| 122 | #define MBEDTLS_PSA_RANDOM_MAX_REQUEST MBEDTLS_CTR_DRBG_MAX_REQUEST |
| 123 | |
| 124 | /** Retrieve the DRBG state from the PSA RNG state. |
| 125 | * |
| 126 | * \param rng Pointer to the PSA random generator state. |
| 127 | * |
| 128 | * \return The DRBG state (\c p_rng argument ). |
| 129 | */ |
| 130 | static inline mbedtls_psa_drbg_context_t *mbedtls_psa_random_state( |
| 131 | mbedtls_psa_random_context_t *rng ) |
| 132 | { |
| 133 | return( &rng->drbg ); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /** Seed the PSA DRBG. |
| 138 | * |
| 139 | * \param rng DRBG context to be seeded. |
| 140 | * \param custom The personalization string. |
| 141 | * This can be \c NULL, in which case the personalization |
| 142 | * string is empty regardless of the value of \p len. |
| 143 | * \param len The length of the personalization string. |
| 144 | * |
| 145 | * \return \c 0 on success. |
| 146 | * \return An Mbed TLS error code (\c MBEDTLS_ERR_xxx) on failure. |
| 147 | */ |
| 148 | static inline int mbedtls_psa_drbg_seed( |
| 149 | mbedtls_psa_random_context_t *rng, |
| 150 | const unsigned char *custom, size_t len ) |
| 151 | { |
| 152 | return( mbedtls_ctr_drbg_seed( mbedtls_psa_random_state( rng ), |
| 153 | mbedtls_entropy_func, |
| 154 | &rng->entropy, |
| 155 | custom, len ) ); |
| 156 | } |
Gilles Peskine | 90edc99 | 2020-11-13 15:39:19 +0100 | [diff] [blame] | 157 | |
Gilles Peskine | 4fc21fd | 2020-11-13 18:47:18 +0100 | [diff] [blame^] | 158 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 159 | |
Gilles Peskine | 90edc99 | 2020-11-13 15:39:19 +0100 | [diff] [blame] | 160 | #endif /* PSA_CRYPTO_RANDOM_H */ |