blob: 249b8d421c0032705d6e1cd3ab8b507450b7fed4 [file] [log] [blame]
Hanno Becker87837b22018-11-08 13:32:02 +00001/**
Hanno Beckerafebf5a2018-11-13 21:01:41 +00002 * \file psa_util.h
Hanno Becker87837b22018-11-08 13:32:02 +00003 *
4 * \brief Utility functions for the use of the PSA Crypto library.
Hanno Becker87837b22018-11-08 13:32:02 +00005 */
6/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02007 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00008 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Hanno Becker87837b22018-11-08 13:32:02 +00009 */
10
Hanno Becker186b65a2018-11-19 15:14:21 +000011#ifndef MBEDTLS_PSA_UTIL_H
12#define MBEDTLS_PSA_UTIL_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020013#include "mbedtls/private_access.h"
Hanno Becker87837b22018-11-08 13:32:02 +000014
Bence Szépkútic662b362021-05-27 11:25:03 +020015#include "mbedtls/build_info.h"
Hanno Becker87837b22018-11-08 13:32:02 +000016
Joakim Anderssonb3491082023-12-11 21:29:19 +010017#include "psa/crypto.h"
18
Jerry Yub02ee182022-03-16 10:30:41 +080019#if defined(MBEDTLS_PSA_CRYPTO_C)
Hanno Becker87837b22018-11-08 13:32:02 +000020
Gilles Peskinee3ed8022021-02-03 20:04:08 +010021/* Expose whatever RNG the PSA subsystem uses to applications using the
Gilles Peskine996f2162021-02-16 16:50:00 +010022 * mbedtls_xxx API. The declarations and definitions here need to be
23 * consistent with the implementation in library/psa_crypto_random_impl.h.
24 * See that file for implementation documentation. */
Jerry Yu406cf272022-03-22 11:33:42 +080025
Gilles Peskinee3ed8022021-02-03 20:04:08 +010026
27/* The type of a `f_rng` random generator function that many library functions
28 * take.
29 *
30 * This type name is not part of the Mbed TLS stable API. It may be renamed
31 * or moved without warning.
32 */
Gilles Peskine449bd832023-01-11 14:50:10 +010033typedef int mbedtls_f_rng_t(void *p_rng, unsigned char *output, size_t output_size);
Gilles Peskinee3ed8022021-02-03 20:04:08 +010034
35#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
36
37/** The random generator function for the PSA subsystem.
38 *
39 * This function is suitable as the `f_rng` random generator function
Gilles Peskine2cff7e22021-02-16 16:49:42 +010040 * parameter of many `mbedtls_xxx` functions. Use #MBEDTLS_PSA_RANDOM_STATE
41 * to obtain the \p p_rng parameter.
Gilles Peskinee3ed8022021-02-03 20:04:08 +010042 *
43 * The implementation of this function depends on the configuration of the
44 * library.
Gilles Peskine2cff7e22021-02-16 16:49:42 +010045 *
Gilles Peskinee3ed8022021-02-03 20:04:08 +010046 * \note Depending on the configuration, this may be a function or
47 * a pointer to a function.
48 *
49 * \note This function may only be used if the PSA crypto subsystem is active.
50 * This means that you must call psa_crypto_init() before any call to
51 * this function, and you must not call this function after calling
52 * mbedtls_psa_crypto_free().
53 *
54 * \param p_rng The random generator context. This must be
55 * #MBEDTLS_PSA_RANDOM_STATE. No other state is
56 * supported.
57 * \param output The buffer to fill. It must have room for
58 * \c output_size bytes.
59 * \param output_size The number of bytes to write to \p output.
60 * This function may fail if \p output_size is too
61 * large. It is guaranteed to accept any output size
62 * requested by Mbed TLS library functions. The
63 * maximum request size depends on the library
64 * configuration.
65 *
66 * \return \c 0 on success.
67 * \return An `MBEDTLS_ERR_ENTROPY_xxx`,
Gilles Peskine2cff7e22021-02-16 16:49:42 +010068 * `MBEDTLS_ERR_PLATFORM_xxx,
Gilles Peskinee3ed8022021-02-03 20:04:08 +010069 * `MBEDTLS_ERR_CTR_DRBG_xxx` or
70 * `MBEDTLS_ERR_HMAC_DRBG_xxx` on error.
71 */
Gilles Peskine449bd832023-01-11 14:50:10 +010072int mbedtls_psa_get_random(void *p_rng,
73 unsigned char *output,
74 size_t output_size);
Gilles Peskinee3ed8022021-02-03 20:04:08 +010075
76/** The random generator state for the PSA subsystem.
77 *
78 * This macro expands to an expression which is suitable as the `p_rng`
79 * random generator state parameter of many `mbedtls_xxx` functions.
80 * It must be used in combination with the random generator function
81 * mbedtls_psa_get_random().
82 *
83 * The implementation of this macro depends on the configuration of the
84 * library. Do not make any assumption on its nature.
85 */
86#define MBEDTLS_PSA_RANDOM_STATE NULL
87
88#else /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
89
90#if defined(MBEDTLS_CTR_DRBG_C)
91#include "mbedtls/ctr_drbg.h"
92typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t;
93static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_ctr_drbg_random;
94#elif defined(MBEDTLS_HMAC_DRBG_C)
95#include "mbedtls/hmac_drbg.h"
96typedef mbedtls_hmac_drbg_context mbedtls_psa_drbg_context_t;
97static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_hmac_drbg_random;
98#endif
99extern mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state;
100
101#define MBEDTLS_PSA_RANDOM_STATE mbedtls_psa_random_state
102
103#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
104
Joakim Anderssonb3491082023-12-11 21:29:19 +0100105/** \defgroup psa_tls_helpers TLS helper functions
106 * @{
107 */
108#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
109#include <mbedtls/ecp.h>
110
111/** Convert an ECC curve identifier from the Mbed TLS encoding to PSA.
112 *
113 * \note This function is provided solely for the convenience of
114 * Mbed TLS and may be removed at any time without notice.
115 *
116 * \param grpid An Mbed TLS elliptic curve identifier
117 * (`MBEDTLS_ECP_DP_xxx`).
118 * \param[out] bits On success, the bit size of the curve.
119 *
120 * \return The corresponding PSA elliptic curve identifier
121 * (`PSA_ECC_FAMILY_xxx`).
122 * \return \c 0 on failure (\p grpid is not recognized).
123 */
124psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
125 size_t *bits);
126
127/** Convert an ECC curve identifier from the PSA encoding to Mbed TLS.
128 *
129 * \note This function is provided solely for the convenience of
130 * Mbed TLS and may be removed at any time without notice.
131 *
132 * \param curve A PSA elliptic curve identifier
133 * (`PSA_ECC_FAMILY_xxx`).
134 * \param bits The bit-length of a private key on \p curve.
135 * \param bits_is_sloppy If true, \p bits may be the bit-length rounded up
136 * to the nearest multiple of 8. This allows the caller
137 * to infer the exact curve from the length of a key
138 * which is supplied as a byte string.
139 *
140 * \return The corresponding Mbed TLS elliptic curve identifier
141 * (`MBEDTLS_ECP_DP_xxx`).
142 * \return #MBEDTLS_ECP_DP_NONE if \c curve is not recognized.
143 * \return #MBEDTLS_ECP_DP_NONE if \p bits is not
144 * correct for \p curve.
145 */
146mbedtls_ecp_group_id mbedtls_ecc_group_of_psa(psa_ecc_family_t curve,
147 size_t bits,
148 int bits_is_sloppy);
149#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
150
Valerio Setti45c3cae2024-01-02 13:26:04 +0100151/**
152 * \brief This function returns the PSA algorithm identifier
153 * associated with the given digest type.
154 *
Valerio Settidd2afcd2024-01-09 08:41:29 +0100155 * \param md_type The type of digest to search for. Must not be NONE.
Valerio Setti45c3cae2024-01-02 13:26:04 +0100156 *
Valerio Settidd2afcd2024-01-09 08:41:29 +0100157 * \warning If \p md_type is \c MBEDTLS_MD_NONE, this function will
158 * not return \c PSA_ALG_NONE, but an invalid algorithm.
159 *
160 * \warning This function does not check if the algorithm is
161 * supported, it always returns the corresponding identifier.
162 *
163 * \return The PSA algorithm identifier associated with \p md_type,
164 * regardless of whether it is supported or not.
Valerio Setti45c3cae2024-01-02 13:26:04 +0100165 */
Valerio Settidd2afcd2024-01-09 08:41:29 +0100166static inline psa_algorithm_t mbedtls_md_psa_alg_from_type(mbedtls_md_type_t md_type)
167{
168 return PSA_ALG_CATEGORY_HASH | (psa_algorithm_t) md_type;
169}
Valerio Setti45c3cae2024-01-02 13:26:04 +0100170
171/**
172 * \brief This function returns the given digest type
173 * associated with the PSA algorithm identifier.
174 *
175 * \param psa_alg The PSA algorithm identifier to search for.
176 *
Valerio Settidd2afcd2024-01-09 08:41:29 +0100177 * \warning This function does not check if the algorithm is
178 * supported, it always returns the corresponding identifier.
179 *
Valerio Setti45c3cae2024-01-02 13:26:04 +0100180 * \return The MD type associated with \p psa_alg,
Valerio Settidd2afcd2024-01-09 08:41:29 +0100181 * regardless of whether it is supported or not.
Valerio Setti45c3cae2024-01-02 13:26:04 +0100182 */
Valerio Settidd2afcd2024-01-09 08:41:29 +0100183static inline mbedtls_md_type_t mbedtls_md_type_from_psa_alg(psa_algorithm_t psa_alg)
184{
185 return (mbedtls_md_type_t) (psa_alg & PSA_ALG_HASH_MASK);
186}
Valerio Setti45c3cae2024-01-02 13:26:04 +0100187
Joakim Anderssonb3491082023-12-11 21:29:19 +0100188/**@}*/
189
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500190#endif /* MBEDTLS_PSA_CRYPTO_C */
Hanno Becker186b65a2018-11-19 15:14:21 +0000191#endif /* MBEDTLS_PSA_UTIL_H */