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 PSA_CRYPTO_RANDOM_IMPL_H
11 #define PSA_CRYPTO_RANDOM_IMPL_H
12 
13 #include "psa_util_internal.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/entropy.h"
22 
23 /* Choose a DRBG based on configuration and availability */
24 #if defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
25 
26 #include "mbedtls/hmac_drbg.h"
27 
28 #elif defined(MBEDTLS_CTR_DRBG_C)
29 
30 #include "mbedtls/ctr_drbg.h"
31 
32 #elif defined(MBEDTLS_HMAC_DRBG_C)
33 
34 #include "mbedtls/hmac_drbg.h"
35 #if defined(MBEDTLS_MD_CAN_SHA512) && defined(MBEDTLS_MD_CAN_SHA256)
36 #include <limits.h>
37 #if SIZE_MAX > 0xffffffff
38 /* Looks like a 64-bit system, so prefer SHA-512. */
39 #define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA512
40 #else
41 /* Looks like a 32-bit system, so prefer SHA-256. */
42 #define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA256
43 #endif
44 #elif defined(MBEDTLS_MD_CAN_SHA512)
45 #define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA512
46 #elif defined(MBEDTLS_MD_CAN_SHA256)
47 #define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA256
48 #else
49 #error "No hash algorithm available for HMAC_DBRG."
50 #endif
51 
52 #else /* !MBEDTLS_PSA_HMAC_DRBG_MD_TYPE && !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C*/
53 
54 #error "No DRBG module available for the psa_crypto module."
55 
56 #endif /* !MBEDTLS_PSA_HMAC_DRBG_MD_TYPE && !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C*/
57 
58 #if defined(MBEDTLS_CTR_DRBG_C)
59 #include "mbedtls/ctr_drbg.h"
60 #elif defined(MBEDTLS_HMAC_DRBG_C)
61 #include "mbedtls/hmac_drbg.h"
62 #endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
63 
64 /* The maximum number of bytes that mbedtls_psa_get_random() is expected to return. */
65 #if defined(MBEDTLS_CTR_DRBG_C)
66 #define MBEDTLS_PSA_RANDOM_MAX_REQUEST MBEDTLS_CTR_DRBG_MAX_REQUEST
67 #elif defined(MBEDTLS_HMAC_DRBG_C)
68 #define MBEDTLS_PSA_RANDOM_MAX_REQUEST MBEDTLS_HMAC_DRBG_MAX_REQUEST
69 #endif
70 
71 #if defined(MBEDTLS_CTR_DRBG_C)
72 typedef mbedtls_ctr_drbg_context            mbedtls_psa_drbg_context_t;
73 #elif defined(MBEDTLS_HMAC_DRBG_C)
74 typedef mbedtls_hmac_drbg_context           mbedtls_psa_drbg_context_t;
75 #endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
76 
77 typedef struct {
78     void (* entropy_init)(mbedtls_entropy_context *ctx);
79     void (* entropy_free)(mbedtls_entropy_context *ctx);
80     mbedtls_entropy_context entropy;
81     mbedtls_psa_drbg_context_t drbg;
82 } mbedtls_psa_random_context_t;
83 
84 /** Initialize the PSA DRBG.
85  *
86  * \param p_rng        Pointer to the Mbed TLS DRBG state.
87  */
mbedtls_psa_drbg_init(mbedtls_psa_drbg_context_t * p_rng)88 static inline void mbedtls_psa_drbg_init(mbedtls_psa_drbg_context_t *p_rng)
89 {
90 #if defined(MBEDTLS_CTR_DRBG_C)
91     mbedtls_ctr_drbg_init(p_rng);
92 #elif defined(MBEDTLS_HMAC_DRBG_C)
93     mbedtls_hmac_drbg_init(p_rng);
94 #endif
95 }
96 
97 /** Deinitialize the PSA DRBG.
98  *
99  * \param p_rng        Pointer to the Mbed TLS DRBG state.
100  */
mbedtls_psa_drbg_free(mbedtls_psa_drbg_context_t * p_rng)101 static inline void mbedtls_psa_drbg_free(mbedtls_psa_drbg_context_t *p_rng)
102 {
103 #if defined(MBEDTLS_CTR_DRBG_C)
104     mbedtls_ctr_drbg_free(p_rng);
105 #elif defined(MBEDTLS_HMAC_DRBG_C)
106     mbedtls_hmac_drbg_free(p_rng);
107 #endif
108 }
109 
110 /** Seed the PSA DRBG.
111  *
112  * \param entropy       An entropy context to read the seed from.
113  * \param custom        The personalization string.
114  *                      This can be \c NULL, in which case the personalization
115  *                      string is empty regardless of the value of \p len.
116  * \param len           The length of the personalization string.
117  *
118  * \return              \c 0 on success.
119  * \return              An Mbed TLS error code (\c MBEDTLS_ERR_xxx) on failure.
120  */
mbedtls_psa_drbg_seed(mbedtls_psa_drbg_context_t * drbg_ctx,mbedtls_entropy_context * entropy,const unsigned char * custom,size_t len)121 static inline int mbedtls_psa_drbg_seed(mbedtls_psa_drbg_context_t *drbg_ctx,
122                                         mbedtls_entropy_context *entropy,
123                                         const unsigned char *custom, size_t len)
124 {
125 #if defined(MBEDTLS_CTR_DRBG_C)
126     return mbedtls_ctr_drbg_seed(drbg_ctx, mbedtls_entropy_func, entropy, custom, len);
127 #elif defined(MBEDTLS_HMAC_DRBG_C)
128     const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE);
129     return mbedtls_hmac_drbg_seed(drbg_ctx, md_info, mbedtls_entropy_func, entropy, custom, len);
130 #endif
131 }
132 
133 #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
134 
135 #endif /* PSA_CRYPTO_RANDOM_IMPL_H */
136