XenevaOS
Loading...
Searching...
No Matches
psa_crypto_random_impl.h
Go to the documentation of this file.
1
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
17typedef 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_CTR_DRBG_C)
25
26#include "mbedtls/ctr_drbg.h"
27#undef MBEDTLS_PSA_HMAC_DRBG_MD_TYPE
28
29#elif defined(MBEDTLS_HMAC_DRBG_C)
30
31#include "mbedtls/hmac_drbg.h"
32#if defined(MBEDTLS_MD_CAN_SHA512) && defined(MBEDTLS_MD_CAN_SHA256)
33#include <limits.h>
34#if SIZE_MAX > 0xffffffff
35/* Looks like a 64-bit system, so prefer SHA-512. */
36#define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA512
37#else
38/* Looks like a 32-bit system, so prefer SHA-256. */
39#define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA256
40#endif
41#elif defined(MBEDTLS_MD_CAN_SHA512)
42#define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA512
43#elif defined(MBEDTLS_MD_CAN_SHA256)
44#define MBEDTLS_PSA_HMAC_DRBG_MD_TYPE MBEDTLS_MD_SHA256
45#else
46#error "No hash algorithm available for HMAC_DBRG."
47#endif
48
49#else /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C*/
50
51#error "No DRBG module available for the psa_crypto module."
52
53#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C*/
54
55/* The maximum number of bytes that mbedtls_psa_get_random() is expected to return. */
56#if defined(MBEDTLS_CTR_DRBG_C)
57#define MBEDTLS_PSA_RANDOM_MAX_REQUEST MBEDTLS_CTR_DRBG_MAX_REQUEST
58#elif defined(MBEDTLS_HMAC_DRBG_C)
59#define MBEDTLS_PSA_RANDOM_MAX_REQUEST MBEDTLS_HMAC_DRBG_MAX_REQUEST
60#endif
61
62#if defined(MBEDTLS_CTR_DRBG_C)
63typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t;
64#elif defined(MBEDTLS_HMAC_DRBG_C)
65typedef mbedtls_hmac_drbg_context mbedtls_psa_drbg_context_t;
66#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
67
68typedef struct {
69 void (* entropy_init)(mbedtls_entropy_context *ctx);
70 void (* entropy_free)(mbedtls_entropy_context *ctx);
72 mbedtls_psa_drbg_context_t drbg;
74
79static inline void mbedtls_psa_drbg_init(mbedtls_psa_drbg_context_t *p_rng)
80{
81#if defined(MBEDTLS_CTR_DRBG_C)
83#elif defined(MBEDTLS_HMAC_DRBG_C)
85#endif
86}
87
92static inline void mbedtls_psa_drbg_free(mbedtls_psa_drbg_context_t *p_rng)
93{
94#if defined(MBEDTLS_CTR_DRBG_C)
96#elif defined(MBEDTLS_HMAC_DRBG_C)
98#endif
99}
100
112static inline int mbedtls_psa_drbg_seed(mbedtls_psa_drbg_context_t *drbg_ctx,
114 const unsigned char *custom, size_t len)
115{
116#if defined(MBEDTLS_CTR_DRBG_C)
117 return mbedtls_ctr_drbg_seed(drbg_ctx, mbedtls_entropy_func, entropy, custom, len);
118#elif defined(MBEDTLS_HMAC_DRBG_C)
119 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE);
120 return mbedtls_hmac_drbg_seed(drbg_ctx, md_info, mbedtls_entropy_func, entropy, custom, len);
121#endif
122}
123
124#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
125
126#endif /* PSA_CRYPTO_RANDOM_IMPL_H */
This file contains definitions and functions for the CTR_DRBG pseudorandom generator.
void mbedtls_ctr_drbg_free(mbedtls_ctr_drbg_context *ctx)
This function resets CTR_DRBG context to the state immediately after initial call of mbedtls_ctr_drbg...
void mbedtls_ctr_drbg_init(mbedtls_ctr_drbg_context *ctx)
This function initializes the CTR_DRBG context, and prepares it for mbedtls_ctr_drbg_seed() or mbedtl...
int mbedtls_ctr_drbg_seed(mbedtls_ctr_drbg_context *ctx, int(*f_entropy)(void *, unsigned char *, size_t), void *p_entropy, const unsigned char *custom, size_t len)
This function seeds and sets up the CTR_DRBG entropy source for future reseeds.
Entropy accumulator implementation.
int mbedtls_entropy_func(void *data, unsigned char *output, size_t len)
Retrieve entropy from the accumulator (Maximum length: MBEDTLS_ENTROPY_BLOCK_SIZE) (Thread-safe if MB...
The HMAC_DRBG pseudorandom generator.
int mbedtls_hmac_drbg_seed(mbedtls_hmac_drbg_context *ctx, const mbedtls_md_info_t *md_info, int(*f_entropy)(void *, unsigned char *, size_t), void *p_entropy, const unsigned char *custom, size_t len)
HMAC_DRBG initial seeding.
void mbedtls_hmac_drbg_init(mbedtls_hmac_drbg_context *ctx)
HMAC_DRBG context initialization.
void mbedtls_hmac_drbg_free(mbedtls_hmac_drbg_context *ctx)
This function resets HMAC_DRBG context to the state immediately after initial call of mbedtls_hmac_dr...
size_t len
Definition initial.c:23
const mbedtls_md_info_t * mbedtls_md_info_from_type(mbedtls_md_type_t md_type)
This function returns the message-digest information associated with the given digest type.
Internal utility functions for use of PSA Crypto.
The CTR_DRBG context structure.
Definition ctr_drbg.h:185
Entropy context structure.
Definition entropy.h:106
Definition hmac_drbg.h:74
Definition md_wrap.h:29
Definition psa_crypto_random_impl.h:68
mbedtls_psa_drbg_context_t drbg
Definition psa_crypto_random_impl.h:72
mbedtls_entropy_context entropy
Definition psa_crypto_random_impl.h:71