0.2.0-dev Download ZIP
215 lines ·7.8 KB Raw
| 1 | /** \file psa_crypto_random_impl.h |
| 2 | * |
| 3 | * \brief PSA crypto random generator implementation abstraction. |
| 4 | */ |
| 5 | /* |
| 6 | * Copyright The Mbed TLS Contributors |
| 7 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 8 | */ |
| 9 | |
| 10 | #ifndef TF_PSA_CRYPTO_PSA_CRYPTO_RANDOM_IMPL_H |
| 11 | #define TF_PSA_CRYPTO_PSA_CRYPTO_RANDOM_IMPL_H |
| 12 | |
| 13 | #include "mbedtls/psa_util.h" |
| 14 | |
| 15 | #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) |
| 16 | |
| 17 | typedef mbedtls_psa_external_random_context_t mbedtls_psa_random_context_t; |
| 18 | |
| 19 | #else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 20 | |
| 21 | #include "mbedtls/private/entropy.h" |
| 22 | #include "mbedtls/private/error_common.h" |
| 23 | |
| 24 | #if !defined(PSA_WANT_ALG_SHA_256) |
| 25 | MBEDTLS_STATIC_ASSERT(MBEDTLS_PSA_CRYPTO_RNG_HASH != PSA_ALG_SHA_256, |
| 26 | "SHA_256 used as the hash for the random generator, but not enabled"); |
| 27 | #endif |
| 28 | |
| 29 | #if !defined(PSA_WANT_ALG_SHA_512) |
| 30 | MBEDTLS_STATIC_ASSERT(MBEDTLS_PSA_CRYPTO_RNG_HASH != PSA_ALG_SHA_512, |
| 31 | "SHA_512 used as the hash for the random generator, but not enabled"); |
| 32 | #endif |
| 33 | |
| 34 | MBEDTLS_STATIC_ASSERT((MBEDTLS_PSA_CRYPTO_RNG_HASH == PSA_ALG_SHA_256) || \ |
| 35 | (MBEDTLS_PSA_CRYPTO_RNG_HASH == PSA_ALG_SHA_512), |
| 36 | "Invalid hashing algorithm for MBEDTLS_PSA_CRYPTO_RNG_HASH"); |
| 37 | |
| 38 | MBEDTLS_STATIC_ASSERT(PSA_BYTES_TO_BITS(PSA_HASH_LENGTH( |
| 39 | MBEDTLS_PSA_CRYPTO_RNG_HASH)) |
| 40 | >= MBEDTLS_PSA_CRYPTO_RNG_STRENGTH, |
| 41 | "The hash size (in bits) of MBEDTLS_PSA_CRYPTO_RNG_HASH must be at least MBEDTLS_PSA_CRYPTO_RNG_STRENGTH"); |
| 42 | |
| 43 | /* Choose a DRBG based on configuration and availability */ |
| 44 | #if defined(MBEDTLS_CTR_DRBG_C) |
| 45 | |
| 46 | #include "mbedtls/private/ctr_drbg.h" |
| 47 | |
| 48 | #if (MBEDTLS_PSA_CRYPTO_RNG_STRENGTH > 128) && \ |
| 49 | defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH) |
| 50 | #error "CTR_DRBG cannot meet the configured RNG strength using only 128-bit AES keys." |
| 51 | #endif |
| 52 | |
| 53 | #if MBEDTLS_PSA_CRYPTO_RNG_STRENGTH > PSA_BYTES_TO_BITS(MBEDTLS_CTR_DRBG_KEYSIZE) |
| 54 | #error "The CTR_DRBG key size (in bits) must be at least MBEDTLS_PSA_CRYPTO_RNG_STRENGTH" |
| 55 | #endif |
| 56 | |
| 57 | #undef MBEDTLS_PSA_HMAC_DRBG_MD_TYPE |
| 58 | |
| 59 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 60 | |
| 61 | #include "mbedtls/private/hmac_drbg.h" |
| 62 | #define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_ENTROPY_MD |
| 63 | |
| 64 | #else /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C*/ |
| 65 | |
| 66 | #error "No DRBG module available for the psa_crypto module." |
| 67 | |
| 68 | #endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C*/ |
| 69 | |
| 70 | /* The maximum number of bytes that mbedtls_psa_get_random() is expected to return. */ |
| 71 | #if defined(MBEDTLS_CTR_DRBG_C) |
| 72 | #define MBEDTLS_PSA_RANDOM_MAX_REQUEST MBEDTLS_CTR_DRBG_MAX_REQUEST |
| 73 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 74 | #define MBEDTLS_PSA_RANDOM_MAX_REQUEST MBEDTLS_HMAC_DRBG_MAX_REQUEST |
| 75 | #endif |
| 76 | |
| 77 | #if defined(MBEDTLS_CTR_DRBG_C) |
| 78 | typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t; |
| 79 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 80 | typedef mbedtls_hmac_drbg_context mbedtls_psa_drbg_context_t; |
| 81 | #endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */ |
| 82 | |
| 83 | typedef struct { |
| 84 | void (* entropy_init)(mbedtls_entropy_context *ctx); |
| 85 | void (* entropy_free)(mbedtls_entropy_context *ctx); |
| 86 | mbedtls_entropy_context entropy; |
| 87 | mbedtls_psa_drbg_context_t drbg; |
| 88 | #if defined(MBEDTLS_PLATFORM_IS_UNIXLIKE) |
| 89 | /* Fork protection: normally pid = getpid(). If the value changes, |
| 90 | * we are in a (grand)*child of the original process, so reseed |
| 91 | * the RNG to ensure that the child and the original process have |
| 92 | * distinct RNG states. See psa_random_internal_generate(). |
| 93 | * |
| 94 | * The type is intmax_t, not pid_t, for portability reasons: |
| 95 | * pid_t is defined in `unistd.h`, but on some platforms, it may |
| 96 | * only be defined if a certain compatibility level is requested |
| 97 | * by defining a macro such as _POSIX_C_SOURCE or _XOPEN_SOURCE. |
| 98 | * The macro needs to be defined before any system header, which |
| 99 | * may be hard to do in some C files that include this header |
| 100 | * (e.g. test suites). So we sidestep this complication, at the |
| 101 | * cost of possibly a few more instructions to compare pid values. |
| 102 | */ |
| 103 | intmax_t pid; |
| 104 | #endif |
| 105 | } mbedtls_psa_random_context_t; |
| 106 | |
| 107 | /** Initialize the PSA DRBG. |
| 108 | * |
| 109 | * \param p_rng Pointer to the Mbed TLS DRBG state. |
| 110 | */ |
| 111 | static inline void mbedtls_psa_drbg_init(mbedtls_psa_drbg_context_t *p_rng) |
| 112 | { |
| 113 | #if defined(MBEDTLS_CTR_DRBG_C) |
| 114 | mbedtls_ctr_drbg_init(p_rng); |
| 115 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 116 | mbedtls_hmac_drbg_init(p_rng); |
| 117 | #endif |
| 118 | } |
| 119 | |
| 120 | /** Deinitialize the PSA DRBG. |
| 121 | * |
| 122 | * \param p_rng Pointer to the Mbed TLS DRBG state. |
| 123 | */ |
| 124 | static inline void mbedtls_psa_drbg_free(mbedtls_psa_drbg_context_t *p_rng) |
| 125 | { |
| 126 | #if defined(MBEDTLS_CTR_DRBG_C) |
| 127 | mbedtls_ctr_drbg_free(p_rng); |
| 128 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 129 | mbedtls_hmac_drbg_free(p_rng); |
| 130 | #endif |
| 131 | } |
| 132 | |
| 133 | /** Seed the PSA DRBG. |
| 134 | * |
| 135 | * \param drbg_ctx The DRBG context to seed. |
| 136 | * It must be initialized but not active. |
| 137 | * \param entropy An entropy context to read the seed from. |
| 138 | * \param custom The personalization string. |
| 139 | * This can be \c NULL, in which case the personalization |
| 140 | * string is empty regardless of the value of \p len. |
| 141 | * \param len The length of the personalization string. |
| 142 | * |
| 143 | * \return \c 0 on success. |
| 144 | * \return An Mbed TLS error code (\c MBEDTLS_ERR_xxx) on failure. |
| 145 | */ |
| 146 | static inline int mbedtls_psa_drbg_seed(mbedtls_psa_drbg_context_t *drbg_ctx, |
| 147 | mbedtls_entropy_context *entropy, |
| 148 | const unsigned char *custom, size_t len) |
| 149 | { |
| 150 | #if defined(MBEDTLS_CTR_DRBG_C) |
| 151 | return mbedtls_ctr_drbg_seed(drbg_ctx, mbedtls_entropy_func, entropy, custom, len); |
| 152 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 153 | const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE); |
| 154 | return mbedtls_hmac_drbg_seed(drbg_ctx, md_info, mbedtls_entropy_func, entropy, custom, len); |
| 155 | #endif /* MBEDTLS_HMAC_DRBG_C */ |
| 156 | } |
| 157 | |
| 158 | /** Reseed the PSA DRBG. |
| 159 | * |
| 160 | * \param drbg_ctx The DRBG context to reseed. |
| 161 | * It must be active. |
| 162 | * \param additional Additional data to inject. |
| 163 | * \param len The length of \p additional in bytes. |
| 164 | * This can be 0 to simply reseed from the entropy source. |
| 165 | * |
| 166 | * \return \c 0 on success. |
| 167 | * \return An Mbed TLS error code (\c MBEDTLS_ERR_xxx) on failure. |
| 168 | */ |
| 169 | static inline int mbedtls_psa_drbg_reseed(mbedtls_psa_drbg_context_t *drbg_ctx, |
| 170 | const unsigned char *additional, |
| 171 | size_t len) |
| 172 | { |
| 173 | #if defined(MBEDTLS_CTR_DRBG_C) |
| 174 | return mbedtls_ctr_drbg_reseed(drbg_ctx, additional, len); |
| 175 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 176 | return mbedtls_hmac_drbg_reseed(drbg_ctx, additional, len); |
| 177 | #endif |
| 178 | } |
| 179 | |
| 180 | /** Deplete the PSA DRBG, i.e. cause it to reseed the next time it is used. |
| 181 | * |
| 182 | * \note This function is not thread-safe. |
| 183 | * |
| 184 | * \param drbg_ctx The DRBG context to deplete. |
| 185 | * It must be active. |
| 186 | */ |
| 187 | static inline void mbedtls_psa_drbg_deplete(mbedtls_psa_drbg_context_t *drbg_ctx) |
| 188 | { |
| 189 | drbg_ctx->reseed_counter = drbg_ctx->reseed_interval; |
| 190 | } |
| 191 | |
| 192 | #if MBEDTLS_ENTROPY_TRUE_SOURCES > 0 |
| 193 | /** Set prediction resistance in the PSA DRBG. |
| 194 | * |
| 195 | * \note This function is not thread-safe. |
| 196 | * |
| 197 | * \param drbg_ctx The DRBG context to reconfigure. |
| 198 | * It must be active. |
| 199 | * \param enabled \c 1 to enable, or \c 0 to disable. |
| 200 | */ |
| 201 | static inline void mbedtls_psa_drbg_set_prediction_resistance( |
| 202 | mbedtls_psa_drbg_context_t *drbg_ctx, |
| 203 | unsigned enabled) |
| 204 | { |
| 205 | #if defined(MBEDTLS_CTR_DRBG_C) |
| 206 | mbedtls_ctr_drbg_set_prediction_resistance(drbg_ctx, enabled); |
| 207 | #elif defined(MBEDTLS_HMAC_DRBG_C) |
| 208 | mbedtls_hmac_drbg_set_prediction_resistance(drbg_ctx, enabled); |
| 209 | #endif |
| 210 | } |
| 211 | #endif /* MBEDTLS_ENTROPY_TRUE_SOURCES > 0 */ |
| 212 | |
| 213 | #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */ |
| 214 | |
| 215 | #endif /* TF_PSA_CRYPTO_PSA_CRYPTO_RANDOM_IMPL_H */ |