Coverage Report

Created: 2024-11-21 07:03

/src/mbedtls/library/psa_crypto_random_impl.h
Line
Count
Source (jump to first uncovered line)
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_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
0
#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)
63
typedef mbedtls_ctr_drbg_context            mbedtls_psa_drbg_context_t;
64
#elif defined(MBEDTLS_HMAC_DRBG_C)
65
typedef mbedtls_hmac_drbg_context           mbedtls_psa_drbg_context_t;
66
#endif /* !MBEDTLS_CTR_DRBG_C && !MBEDTLS_HMAC_DRBG_C */
67
68
typedef struct {
69
    void (* entropy_init)(mbedtls_entropy_context *ctx);
70
    void (* entropy_free)(mbedtls_entropy_context *ctx);
71
    mbedtls_entropy_context entropy;
72
    mbedtls_psa_drbg_context_t drbg;
73
} mbedtls_psa_random_context_t;
74
75
/** Initialize the PSA DRBG.
76
 *
77
 * \param p_rng        Pointer to the Mbed TLS DRBG state.
78
 */
79
static inline void mbedtls_psa_drbg_init(mbedtls_psa_drbg_context_t *p_rng)
80
10
{
81
10
#if defined(MBEDTLS_CTR_DRBG_C)
82
10
    mbedtls_ctr_drbg_init(p_rng);
83
#elif defined(MBEDTLS_HMAC_DRBG_C)
84
    mbedtls_hmac_drbg_init(p_rng);
85
#endif
86
10
}
psa_crypto.c:mbedtls_psa_drbg_init
Line
Count
Source
80
10
{
81
10
#if defined(MBEDTLS_CTR_DRBG_C)
82
10
    mbedtls_ctr_drbg_init(p_rng);
83
#elif defined(MBEDTLS_HMAC_DRBG_C)
84
    mbedtls_hmac_drbg_init(p_rng);
85
#endif
86
10
}
Unexecuted instantiation: psa_crypto_cipher.c:mbedtls_psa_drbg_init
Unexecuted instantiation: psa_crypto_ecp.c:mbedtls_psa_drbg_init
Unexecuted instantiation: psa_crypto_ffdh.c:mbedtls_psa_drbg_init
Unexecuted instantiation: psa_crypto_rsa.c:mbedtls_psa_drbg_init
87
88
/** Deinitialize the PSA DRBG.
89
 *
90
 * \param p_rng        Pointer to the Mbed TLS DRBG state.
91
 */
92
static inline void mbedtls_psa_drbg_free(mbedtls_psa_drbg_context_t *p_rng)
93
0
{
94
0
#if defined(MBEDTLS_CTR_DRBG_C)
95
0
    mbedtls_ctr_drbg_free(p_rng);
96
#elif defined(MBEDTLS_HMAC_DRBG_C)
97
    mbedtls_hmac_drbg_free(p_rng);
98
#endif
99
0
}
Unexecuted instantiation: psa_crypto.c:mbedtls_psa_drbg_free
Unexecuted instantiation: psa_crypto_cipher.c:mbedtls_psa_drbg_free
Unexecuted instantiation: psa_crypto_ecp.c:mbedtls_psa_drbg_free
Unexecuted instantiation: psa_crypto_ffdh.c:mbedtls_psa_drbg_free
Unexecuted instantiation: psa_crypto_rsa.c:mbedtls_psa_drbg_free
100
101
/** Seed the PSA DRBG.
102
 *
103
 * \param entropy       An entropy context to read the seed from.
104
 * \param custom        The personalization string.
105
 *                      This can be \c NULL, in which case the personalization
106
 *                      string is empty regardless of the value of \p len.
107
 * \param len           The length of the personalization string.
108
 *
109
 * \return              \c 0 on success.
110
 * \return              An Mbed TLS error code (\c MBEDTLS_ERR_xxx) on failure.
111
 */
112
static inline int mbedtls_psa_drbg_seed(mbedtls_psa_drbg_context_t *drbg_ctx,
113
                                        mbedtls_entropy_context *entropy,
114
                                        const unsigned char *custom, size_t len)
115
10
{
116
10
#if defined(MBEDTLS_CTR_DRBG_C)
117
10
    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
10
}
psa_crypto.c:mbedtls_psa_drbg_seed
Line
Count
Source
115
10
{
116
10
#if defined(MBEDTLS_CTR_DRBG_C)
117
10
    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
10
}
Unexecuted instantiation: psa_crypto_cipher.c:mbedtls_psa_drbg_seed
Unexecuted instantiation: psa_crypto_ecp.c:mbedtls_psa_drbg_seed
Unexecuted instantiation: psa_crypto_ffdh.c:mbedtls_psa_drbg_seed
Unexecuted instantiation: psa_crypto_rsa.c:mbedtls_psa_drbg_seed
123
124
#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
125
126
#endif /* PSA_CRYPTO_RANDOM_IMPL_H */