blob: c78cc2333359a2c854a9cdf9dfa0726383d3f6b7 [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
Valerio Settic22e3ce2024-01-10 08:46:59 +010019/* ASN1 defines used in the ECDSA conversion functions.
20 * Note: intentionally not adding MBEDTLS_ASN1_[PARSE|WRITE]_C guards here
21 * otherwise error codes would be unknown in test_suite_psa_crypto_util.data.*/
Valerio Setti99c03692024-01-10 08:21:10 +010022#include <mbedtls/asn1write.h>
Valerio Setti99c03692024-01-10 08:21:10 +010023
Valerio Setti1a58e9a2024-02-29 16:14:29 +010024#if defined(MBEDTLS_PSA_CRYPTO_CLIENT)
Hanno Becker87837b22018-11-08 13:32:02 +000025
Gilles Peskinee3ed8022021-02-03 20:04:08 +010026/** The random generator function for the PSA subsystem.
27 *
28 * This function is suitable as the `f_rng` random generator function
Valerio Settia53e7a52024-02-26 12:03:59 +010029 * parameter of many `mbedtls_xxx` functions.
Gilles Peskinee3ed8022021-02-03 20:04:08 +010030 *
31 * The implementation of this function depends on the configuration of the
32 * library.
Gilles Peskine2cff7e22021-02-16 16:49:42 +010033 *
Gilles Peskinee3ed8022021-02-03 20:04:08 +010034 * \note This function may only be used if the PSA crypto subsystem is active.
35 * This means that you must call psa_crypto_init() before any call to
36 * this function, and you must not call this function after calling
37 * mbedtls_psa_crypto_free().
38 *
Valerio Settia53e7a52024-02-26 12:03:59 +010039 * \param p_rng This parameter is only kept for backward compatibility
40 * reasons with legacy `f_rng` functions and it's ignored.
41 * Set to #MBEDTLS_PSA_RANDOM_STATE or NULL.
Gilles Peskinee3ed8022021-02-03 20:04:08 +010042 * \param output The buffer to fill. It must have room for
43 * \c output_size bytes.
44 * \param output_size The number of bytes to write to \p output.
45 * This function may fail if \p output_size is too
46 * large. It is guaranteed to accept any output size
47 * requested by Mbed TLS library functions. The
48 * maximum request size depends on the library
49 * configuration.
50 *
51 * \return \c 0 on success.
52 * \return An `MBEDTLS_ERR_ENTROPY_xxx`,
Gilles Peskine2cff7e22021-02-16 16:49:42 +010053 * `MBEDTLS_ERR_PLATFORM_xxx,
Gilles Peskinee3ed8022021-02-03 20:04:08 +010054 * `MBEDTLS_ERR_CTR_DRBG_xxx` or
55 * `MBEDTLS_ERR_HMAC_DRBG_xxx` on error.
56 */
Gilles Peskine449bd832023-01-11 14:50:10 +010057int mbedtls_psa_get_random(void *p_rng,
58 unsigned char *output,
59 size_t output_size);
Gilles Peskinee3ed8022021-02-03 20:04:08 +010060
61/** The random generator state for the PSA subsystem.
62 *
Valerio Settia53e7a52024-02-26 12:03:59 +010063 * This macro always expands to NULL because the `p_rng` parameter is unused
64 * in mbedtls_psa_get_random(), but it's kept for interface's backward
65 * compatibility.
Gilles Peskinee3ed8022021-02-03 20:04:08 +010066 */
Valerio Settia53e7a52024-02-26 12:03:59 +010067#define MBEDTLS_PSA_RANDOM_STATE NULL
Gilles Peskinee3ed8022021-02-03 20:04:08 +010068
Joakim Anderssonb3491082023-12-11 21:29:19 +010069/** \defgroup psa_tls_helpers TLS helper functions
70 * @{
71 */
72#if defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
73#include <mbedtls/ecp.h>
74
75/** Convert an ECC curve identifier from the Mbed TLS encoding to PSA.
76 *
Joakim Anderssonb3491082023-12-11 21:29:19 +010077 * \param grpid An Mbed TLS elliptic curve identifier
78 * (`MBEDTLS_ECP_DP_xxx`).
Valerio Settieca07142024-01-04 13:17:31 +010079 * \param[out] bits On success the bit size of the curve; 0 on failure.
Joakim Anderssonb3491082023-12-11 21:29:19 +010080 *
Valerio Settid0aa9c12024-01-09 09:10:44 +010081 * \return If the curve is supported in the PSA API, this function
82 * returns the proper PSA curve identifier
83 * (`PSA_ECC_FAMILY_xxx`). This holds even if the curve is
84 * not supported by the ECP module.
85 * \return \c 0 if the curve is not supported in the PSA API.
Joakim Anderssonb3491082023-12-11 21:29:19 +010086 */
87psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid,
88 size_t *bits);
89
90/** Convert an ECC curve identifier from the PSA encoding to Mbed TLS.
91 *
Valerio Setti39faa9c2024-01-09 09:11:22 +010092 * \param family A PSA elliptic curve family identifier
Joakim Anderssonb3491082023-12-11 21:29:19 +010093 * (`PSA_ECC_FAMILY_xxx`).
94 * \param bits The bit-length of a private key on \p curve.
Joakim Anderssonb3491082023-12-11 21:29:19 +010095 *
Valerio Settid0aa9c12024-01-09 09:10:44 +010096 * \return If the curve is supported in the PSA API, this function
97 * returns the corresponding Mbed TLS elliptic curve
Valerio Settieca07142024-01-04 13:17:31 +010098 * identifier (`MBEDTLS_ECP_DP_xxx`).
Valerio Setti0e608802023-12-29 11:46:44 +010099 * \return #MBEDTLS_ECP_DP_NONE if the combination of \c curve
Valerio Settid0aa9c12024-01-09 09:10:44 +0100100 * and \p bits is not supported.
Joakim Anderssonb3491082023-12-11 21:29:19 +0100101 */
Valerio Setti39faa9c2024-01-09 09:11:22 +0100102mbedtls_ecp_group_id mbedtls_ecc_group_from_psa(psa_ecc_family_t family,
Valerio Settid36c3132023-12-21 14:03:51 +0100103 size_t bits);
Joakim Anderssonb3491082023-12-11 21:29:19 +0100104#endif /* PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY */
105
Valerio Setti45c3cae2024-01-02 13:26:04 +0100106/**
107 * \brief This function returns the PSA algorithm identifier
108 * associated with the given digest type.
109 *
Valerio Settidd2afcd2024-01-09 08:41:29 +0100110 * \param md_type The type of digest to search for. Must not be NONE.
Valerio Setti45c3cae2024-01-02 13:26:04 +0100111 *
Valerio Settidd2afcd2024-01-09 08:41:29 +0100112 * \warning If \p md_type is \c MBEDTLS_MD_NONE, this function will
113 * not return \c PSA_ALG_NONE, but an invalid algorithm.
114 *
115 * \warning This function does not check if the algorithm is
116 * supported, it always returns the corresponding identifier.
117 *
118 * \return The PSA algorithm identifier associated with \p md_type,
119 * regardless of whether it is supported or not.
Valerio Setti45c3cae2024-01-02 13:26:04 +0100120 */
Valerio Settidd2afcd2024-01-09 08:41:29 +0100121static inline psa_algorithm_t mbedtls_md_psa_alg_from_type(mbedtls_md_type_t md_type)
122{
123 return PSA_ALG_CATEGORY_HASH | (psa_algorithm_t) md_type;
124}
Valerio Setti45c3cae2024-01-02 13:26:04 +0100125
126/**
127 * \brief This function returns the given digest type
128 * associated with the PSA algorithm identifier.
129 *
130 * \param psa_alg The PSA algorithm identifier to search for.
131 *
Valerio Settidd2afcd2024-01-09 08:41:29 +0100132 * \warning This function does not check if the algorithm is
133 * supported, it always returns the corresponding identifier.
134 *
Valerio Setti45c3cae2024-01-02 13:26:04 +0100135 * \return The MD type associated with \p psa_alg,
Valerio Settidd2afcd2024-01-09 08:41:29 +0100136 * regardless of whether it is supported or not.
Valerio Setti45c3cae2024-01-02 13:26:04 +0100137 */
Valerio Settidd2afcd2024-01-09 08:41:29 +0100138static inline mbedtls_md_type_t mbedtls_md_type_from_psa_alg(psa_algorithm_t psa_alg)
139{
140 return (mbedtls_md_type_t) (psa_alg & PSA_ALG_HASH_MASK);
141}
Valerio Setti1a58e9a2024-02-29 16:14:29 +0100142#endif /* MBEDTLS_PSA_CRYPTO_CLIENT */
Valerio Setti45c3cae2024-01-02 13:26:04 +0100143
Valerio Settif4d2dc22024-01-16 10:57:48 +0100144#if defined(MBEDTLS_PSA_UTIL_HAVE_ECDSA)
145
Valerio Setti86451972024-02-05 09:50:20 +0100146/** Convert an ECDSA signature from raw format to DER ASN.1 format.
Valerio Setti75501f52024-01-08 16:49:17 +0100147 *
Valerio Setti315e4af2024-02-05 10:09:15 +0100148 * \param bits Size of each coordinate in bits.
Valerio Setti75501f52024-01-08 16:49:17 +0100149 * \param raw Buffer that contains the signature in raw format.
Valerio Setti6269f3b2024-02-06 16:55:18 +0100150 * \param raw_len Length of \p raw in bytes. This must be
Valerio Setti8334d002024-02-05 15:35:26 +0100151 * PSA_BITS_TO_BYTES(bits) bytes.
Valerio Setti75501f52024-01-08 16:49:17 +0100152 * \param[out] der Buffer that will be filled with the converted DER
153 * output. It can overlap with raw buffer.
Valerio Settiaffba302024-02-07 15:03:33 +0100154 * \param der_size Size of \p der in bytes. It is enough if \p der_size
155 * is at least the size of the actual output. (The size
156 * of the output can vary depending on the presence of
157 * leading zeros in the data.) You can use
158 * #MBEDTLS_ECDSA_MAX_SIG_LEN(\p bits) to determine a
159 * size that is large enough for all signatures for a
160 * given value of \p bits.
Valerio Setti75501f52024-01-08 16:49:17 +0100161 * \param[out] der_len On success it contains the amount of valid data
Valerio Setti86451972024-02-05 09:50:20 +0100162 * (in bytes) written to \p der. It's undefined
Valerio Setti75501f52024-01-08 16:49:17 +0100163 * in case of failure.
Valerio Setti75501f52024-01-08 16:49:17 +0100164 */
Valerio Setti315e4af2024-02-05 10:09:15 +0100165int mbedtls_ecdsa_raw_to_der(size_t bits, const unsigned char *raw, size_t raw_len,
166 unsigned char *der, size_t der_size, size_t *der_len);
Valerio Setti75501f52024-01-08 16:49:17 +0100167
Valerio Setti86451972024-02-05 09:50:20 +0100168/** Convert an ECDSA signature from DER ASN.1 format to raw format.
Valerio Setti75501f52024-01-08 16:49:17 +0100169 *
Valerio Setti315e4af2024-02-05 10:09:15 +0100170 * \param bits Size of each coordinate in bits.
Valerio Setti75501f52024-01-08 16:49:17 +0100171 * \param der Buffer that contains the signature in DER format.
Valerio Setti86451972024-02-05 09:50:20 +0100172 * \param der_len Size of \p der in bytes.
Valerio Setti75501f52024-01-08 16:49:17 +0100173 * \param[out] raw Buffer that will be filled with the converted raw
174 * signature. It can overlap with der buffer.
Valerio Settie01a2b02024-02-05 15:16:36 +0100175 * \param raw_size Size of \p raw in bytes. Must be at least
176 * 2 * PSA_BITS_TO_BYTES(bits) bytes.
Valerio Setti75501f52024-01-08 16:49:17 +0100177 * \param[out] raw_len On success it is updated with the amount of valid
Valerio Setti86451972024-02-05 09:50:20 +0100178 * data (in bytes) written to \p raw. It's undefined
Valerio Setti75501f52024-01-08 16:49:17 +0100179 * in case of failure.
Valerio Setti75501f52024-01-08 16:49:17 +0100180 */
Valerio Setti315e4af2024-02-05 10:09:15 +0100181int mbedtls_ecdsa_der_to_raw(size_t bits, const unsigned char *der, size_t der_len,
182 unsigned char *raw, size_t raw_size, size_t *raw_len);
Valerio Setti75501f52024-01-08 16:49:17 +0100183
Valerio Settif4d2dc22024-01-16 10:57:48 +0100184#endif /* MBEDTLS_PSA_UTIL_HAVE_ECDSA */
185
Joakim Anderssonb3491082023-12-11 21:29:19 +0100186/**@}*/
187
Hanno Becker186b65a2018-11-19 15:14:21 +0000188#endif /* MBEDTLS_PSA_UTIL_H */