blob: 2a75a439a2a25da0433c57438460c6434c12474f [file] [log] [blame]
Gilles Peskineb2b64d32020-12-14 16:43:58 +01001/** \file psa_crypto_random_impl.h
Gilles Peskine90edc992020-11-13 15:39:19 +01002 *
Gilles Peskineb2b64d32020-12-14 16:43:58 +01003 * \brief PSA crypto random generator implementation abstraction.
Gilles Peskinee3ed8022021-02-03 20:04:08 +01004 *
5 * The definitions here need to be consistent with the declarations
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +02006 * in include/psa_util_internal.h. This file contains some redundant
Gilles Peskine996f2162021-02-16 16:50:00 +01007 * declarations to increase the chance that a compiler will detect
8 * inconsistencies if one file is changed without updating the other,
9 * but not all potential inconsistencies can be enforced, so make sure
10 * to check the public declarations and contracts in
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020011 * include/psa_util_internal.h if you modify this file.
Gilles Peskine90edc992020-11-13 15:39:19 +010012 */
13/*
14 * Copyright The Mbed TLS Contributors
15 * SPDX-License-Identifier: Apache-2.0
16 *
17 * Licensed under the Apache License, Version 2.0 (the "License"); you may
18 * not use this file except in compliance with the License.
19 * You may obtain a copy of the License at
20 *
21 * http://www.apache.org/licenses/LICENSE-2.0
22 *
23 * Unless required by applicable law or agreed to in writing, software
24 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
25 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
26 * See the License for the specific language governing permissions and
27 * limitations under the License.
28 */
29
Gilles Peskineb2b64d32020-12-14 16:43:58 +010030#ifndef PSA_CRYPTO_RANDOM_IMPL_H
31#define PSA_CRYPTO_RANDOM_IMPL_H
Gilles Peskine90edc992020-11-13 15:39:19 +010032
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020033#include <psa_util_internal.h>
Gilles Peskine996f2162021-02-16 16:50:00 +010034
Gilles Peskine4fc21fd2020-11-13 18:47:18 +010035#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
36
37#include <string.h>
38#include <mbedtls/entropy.h> // only for error codes
39#include <psa/crypto.h>
40
41typedef mbedtls_psa_external_random_context_t mbedtls_psa_random_context_t;
42
Gilles Peskine9c3e0602021-01-05 16:03:55 +010043/* Trivial wrapper around psa_generate_random(). */
Gilles Peskine449bd832023-01-11 14:50:10 +010044int mbedtls_psa_get_random(void *p_rng,
45 unsigned char *output,
46 size_t output_size);
Gilles Peskine4fc21fd2020-11-13 18:47:18 +010047
Gilles Peskine9c3e0602021-01-05 16:03:55 +010048/* The PSA RNG API doesn't need any externally maintained state. */
Gilles Peskine5894e8e2020-12-14 14:54:06 +010049#define MBEDTLS_PSA_RANDOM_STATE NULL
Gilles Peskine4fc21fd2020-11-13 18:47:18 +010050
51#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
52
Gilles Peskine82e57d12020-11-13 21:31:17 +010053/* Choose a DRBG based on configuration and availability */
54#if defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
55
56#include "mbedtls/hmac_drbg.h"
57
58#elif defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine30524eb2020-11-13 17:02:26 +010059
60#include "mbedtls/ctr_drbg.h"
Gilles Peskine82e57d12020-11-13 21:31:17 +010061
62#elif defined(MBEDTLS_HMAC_DRBG_C)
63
64#include "mbedtls/hmac_drbg.h"
Manuel Pégourié-Gonnard4011eb42023-03-21 17:10:45 +010065#if defined(MBEDTLS_MD_CAN_SHA512) && defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine82e57d12020-11-13 21:31:17 +010066#include <limits.h>
67#if SIZE_MAX > 0xffffffff
68/* Looks like a 64-bit system, so prefer SHA-512. */
69#define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA512
70#else
71/* Looks like a 32-bit system, so prefer SHA-256. */
72#define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA256
73#endif
Manuel Pégourié-Gonnard4011eb42023-03-21 17:10:45 +010074#elif defined(MBEDTLS_MD_CAN_SHA512)
Gilles Peskine82e57d12020-11-13 21:31:17 +010075#define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA512
Manuel Pégourié-Gonnard4011eb42023-03-21 17:10:45 +010076#elif defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine82e57d12020-11-13 21:31:17 +010077#define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA256
78#else
79#error "No hash algorithm available for HMAC_DBRG."
80#endif
81
82#else
83#error "No DRBG module available for the psa_crypto module."
84#endif
85
Gilles Peskine30524eb2020-11-13 17:02:26 +010086#include "mbedtls/entropy.h"
87
Gilles Peskine30524eb2020-11-13 17:02:26 +010088/** Initialize the PSA DRBG.
89 *
90 * \param p_rng Pointer to the Mbed TLS DRBG state.
91 */
Gilles Peskine449bd832023-01-11 14:50:10 +010092static inline void mbedtls_psa_drbg_init(mbedtls_psa_drbg_context_t *p_rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +010093{
Gilles Peskine82e57d12020-11-13 21:31:17 +010094#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010095 mbedtls_ctr_drbg_init(p_rng);
Gilles Peskine82e57d12020-11-13 21:31:17 +010096#elif defined(MBEDTLS_HMAC_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010097 mbedtls_hmac_drbg_init(p_rng);
Gilles Peskine82e57d12020-11-13 21:31:17 +010098#endif
Gilles Peskine30524eb2020-11-13 17:02:26 +010099}
100
101/** Deinitialize the PSA DRBG.
102 *
103 * \param p_rng Pointer to the Mbed TLS DRBG state.
104 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100105static inline void mbedtls_psa_drbg_free(mbedtls_psa_drbg_context_t *p_rng)
Gilles Peskine30524eb2020-11-13 17:02:26 +0100106{
Gilles Peskine82e57d12020-11-13 21:31:17 +0100107#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 mbedtls_ctr_drbg_free(p_rng);
Gilles Peskine82e57d12020-11-13 21:31:17 +0100109#elif defined(MBEDTLS_HMAC_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 mbedtls_hmac_drbg_free(p_rng);
Gilles Peskine82e57d12020-11-13 21:31:17 +0100111#endif
Gilles Peskine30524eb2020-11-13 17:02:26 +0100112}
113
114/** The type of the PSA random generator context.
115 *
116 * The random generator context is composed of an entropy context and
117 * a DRBG context.
118 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100119typedef struct {
120 void (* entropy_init)(mbedtls_entropy_context *ctx);
121 void (* entropy_free)(mbedtls_entropy_context *ctx);
Gilles Peskine30524eb2020-11-13 17:02:26 +0100122 mbedtls_entropy_context entropy;
123 mbedtls_psa_drbg_context_t drbg;
124} mbedtls_psa_random_context_t;
125
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +0200126/* Defined in include/psa_util_internal.h so that it's visible to
Gilles Peskine277a3a62021-02-16 18:55:05 +0100127 * application code. The declaration here is redundant, but included
128 * as a safety net to make it more likely that a future change that
129 * accidentally causes the implementation to diverge from the interface
130 * will be noticed. */
131/* Do not include the declaration under MSVC because it doesn't accept it
132 * ("error C2370: 'mbedtls_psa_get_random' : redefinition; different storage class").
133 * Observed with Visual Studio 2013. A known bug apparently:
134 * https://stackoverflow.com/questions/8146541/duplicate-external-static-declarations-not-allowed-in-visual-studio
135 */
136#if !defined(_MSC_VER)
Gilles Peskine996f2162021-02-16 16:50:00 +0100137static mbedtls_f_rng_t *const mbedtls_psa_get_random;
Gilles Peskine277a3a62021-02-16 18:55:05 +0100138#endif
Gilles Peskine30524eb2020-11-13 17:02:26 +0100139
140/** The maximum number of bytes that mbedtls_psa_get_random() is expected to
141 * return.
142 */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100143#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine30524eb2020-11-13 17:02:26 +0100144#define MBEDTLS_PSA_RANDOM_MAX_REQUEST MBEDTLS_CTR_DRBG_MAX_REQUEST
Gilles Peskine82e57d12020-11-13 21:31:17 +0100145#elif defined(MBEDTLS_HMAC_DRBG_C)
146#define MBEDTLS_PSA_RANDOM_MAX_REQUEST MBEDTLS_HMAC_DRBG_MAX_REQUEST
147#endif
Gilles Peskine30524eb2020-11-13 17:02:26 +0100148
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100149/** A pointer to the PSA DRBG state.
Gilles Peskine30524eb2020-11-13 17:02:26 +0100150 *
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100151 * This variable is only intended to be used through the macro
152 * #MBEDTLS_PSA_RANDOM_STATE.
Gilles Peskine30524eb2020-11-13 17:02:26 +0100153 */
Gilles Peskine9c3e0602021-01-05 16:03:55 +0100154/* psa_crypto.c sets this variable to a pointer to the DRBG state in the
155 * global PSA crypto state. */
Gilles Peskine996f2162021-02-16 16:50:00 +0100156/* The type `mbedtls_psa_drbg_context_t` is defined in
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +0200157 * include/psa_util_internal.h so that `mbedtls_psa_random_state` can be
Gilles Peskine996f2162021-02-16 16:50:00 +0100158 * declared there and be visible to application code. */
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100159extern mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state;
160
161/** A pointer to the PSA DRBG state.
162 *
Gilles Peskine88fa5c42021-01-04 21:00:53 +0100163 * This macro expands to an expression that is suitable as the \c p_rng
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100164 * parameter to pass to mbedtls_psa_get_random().
165 *
166 * This macro exists in all configurations where the psa_crypto module is
167 * enabled. Its expansion depends on the configuration.
168 */
169#define MBEDTLS_PSA_RANDOM_STATE mbedtls_psa_random_state
Gilles Peskine30524eb2020-11-13 17:02:26 +0100170
Gilles Peskine30524eb2020-11-13 17:02:26 +0100171/** Seed the PSA DRBG.
172 *
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100173 * \param entropy An entropy context to read the seed from.
Gilles Peskine30524eb2020-11-13 17:02:26 +0100174 * \param custom The personalization string.
175 * This can be \c NULL, in which case the personalization
176 * string is empty regardless of the value of \p len.
177 * \param len The length of the personalization string.
178 *
179 * \return \c 0 on success.
180 * \return An Mbed TLS error code (\c MBEDTLS_ERR_xxx) on failure.
181 */
182static inline int mbedtls_psa_drbg_seed(
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100183 mbedtls_entropy_context *entropy,
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 const unsigned char *custom, size_t len)
Gilles Peskine30524eb2020-11-13 17:02:26 +0100185{
Gilles Peskine82e57d12020-11-13 21:31:17 +0100186#if defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 return mbedtls_ctr_drbg_seed(MBEDTLS_PSA_RANDOM_STATE,
188 mbedtls_entropy_func,
189 entropy,
190 custom, len);
Gilles Peskine82e57d12020-11-13 21:31:17 +0100191#elif defined(MBEDTLS_HMAC_DRBG_C)
192 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 mbedtls_md_info_from_type(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE);
194 return mbedtls_hmac_drbg_seed(MBEDTLS_PSA_RANDOM_STATE,
195 md_info,
196 mbedtls_entropy_func,
197 entropy,
198 custom, len);
Gilles Peskine82e57d12020-11-13 21:31:17 +0100199#endif
Gilles Peskine30524eb2020-11-13 17:02:26 +0100200}
Gilles Peskine90edc992020-11-13 15:39:19 +0100201
Gilles Peskine4fc21fd2020-11-13 18:47:18 +0100202#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
203
Gilles Peskineb2b64d32020-12-14 16:43:58 +0100204#endif /* PSA_CRYPTO_RANDOM_IMPL_H */