blob: 7707a8d8c0fd753d42ce48df5513a86381603a72 [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.
5 *
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +02006 * \warning These functions are not part of the public API and may
Hanno Becker87837b22018-11-08 13:32:02 +00007 * change at any time.
8 */
9/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020010 * Copyright The Mbed TLS Contributors
Hanno Becker87837b22018-11-08 13:32:02 +000011 * SPDX-License-Identifier: Apache-2.0
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
Hanno Becker87837b22018-11-08 13:32:02 +000024 */
25
Hanno Becker186b65a2018-11-19 15:14:21 +000026#ifndef MBEDTLS_PSA_UTIL_H
27#define MBEDTLS_PSA_UTIL_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020028#include "mbedtls/private_access.h"
Hanno Becker87837b22018-11-08 13:32:02 +000029
Bence Szépkútic662b362021-05-27 11:25:03 +020030#include "mbedtls/build_info.h"
Hanno Becker87837b22018-11-08 13:32:02 +000031
Manuel Pégourié-Gonnard1c32e372023-03-28 11:57:09 +020032#include "mbedtls/platform_util.h"
33
Jerry Yub02ee182022-03-16 10:30:41 +080034#if defined(MBEDTLS_PSA_CRYPTO_C)
Hanno Becker87837b22018-11-08 13:32:02 +000035
36#include "psa/crypto.h"
37
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010038#include "mbedtls/ecp.h"
39#include "mbedtls/md.h"
40#include "mbedtls/pk.h"
41#include "mbedtls/oid.h"
TRodziewicz3408d602021-04-21 13:25:25 +020042#include "mbedtls/error.h"
Hanno Beckerf75f9122019-01-07 15:36:51 +000043#include <string.h>
44
Hanno Becker55251262018-11-12 09:29:12 +000045/* Translations for ECC. */
46
Manuel Pégourié-Gonnard59753762022-01-18 11:52:11 +010047#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH \
Gilles Peskine449bd832023-01-11 14:50:10 +010048 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
Hanno Beckerdf51dbe2019-02-18 16:41:55 +000049
Valerio Setti1337a4f2023-01-30 15:54:55 +010050#define MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH \
51 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
52
Przemek Stekielcceb9332023-05-18 14:31:10 +020053#define MBEDTLS_PSA_MAX_FFDH_PUBKEY_LENGTH \
54 PSA_KEY_EXPORT_FFDH_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_FFDH_MAX_KEY_BITS)
55
Gilles Peskinee3ed8022021-02-03 20:04:08 +010056/* Expose whatever RNG the PSA subsystem uses to applications using the
Gilles Peskine996f2162021-02-16 16:50:00 +010057 * mbedtls_xxx API. The declarations and definitions here need to be
58 * consistent with the implementation in library/psa_crypto_random_impl.h.
59 * See that file for implementation documentation. */
Jerry Yu406cf272022-03-22 11:33:42 +080060
Gilles Peskinee3ed8022021-02-03 20:04:08 +010061
62/* The type of a `f_rng` random generator function that many library functions
63 * take.
64 *
65 * This type name is not part of the Mbed TLS stable API. It may be renamed
66 * or moved without warning.
67 */
Gilles Peskine449bd832023-01-11 14:50:10 +010068typedef int mbedtls_f_rng_t(void *p_rng, unsigned char *output, size_t output_size);
Gilles Peskinee3ed8022021-02-03 20:04:08 +010069
70#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
71
72/** The random generator function for the PSA subsystem.
73 *
74 * This function is suitable as the `f_rng` random generator function
Gilles Peskine2cff7e22021-02-16 16:49:42 +010075 * parameter of many `mbedtls_xxx` functions. Use #MBEDTLS_PSA_RANDOM_STATE
76 * to obtain the \p p_rng parameter.
Gilles Peskinee3ed8022021-02-03 20:04:08 +010077 *
78 * The implementation of this function depends on the configuration of the
79 * library.
Gilles Peskine2cff7e22021-02-16 16:49:42 +010080 *
Gilles Peskinee3ed8022021-02-03 20:04:08 +010081 * \note Depending on the configuration, this may be a function or
82 * a pointer to a function.
83 *
84 * \note This function may only be used if the PSA crypto subsystem is active.
85 * This means that you must call psa_crypto_init() before any call to
86 * this function, and you must not call this function after calling
87 * mbedtls_psa_crypto_free().
88 *
89 * \param p_rng The random generator context. This must be
90 * #MBEDTLS_PSA_RANDOM_STATE. No other state is
91 * supported.
92 * \param output The buffer to fill. It must have room for
93 * \c output_size bytes.
94 * \param output_size The number of bytes to write to \p output.
95 * This function may fail if \p output_size is too
96 * large. It is guaranteed to accept any output size
97 * requested by Mbed TLS library functions. The
98 * maximum request size depends on the library
99 * configuration.
100 *
101 * \return \c 0 on success.
102 * \return An `MBEDTLS_ERR_ENTROPY_xxx`,
Gilles Peskine2cff7e22021-02-16 16:49:42 +0100103 * `MBEDTLS_ERR_PLATFORM_xxx,
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100104 * `MBEDTLS_ERR_CTR_DRBG_xxx` or
105 * `MBEDTLS_ERR_HMAC_DRBG_xxx` on error.
106 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107int mbedtls_psa_get_random(void *p_rng,
108 unsigned char *output,
109 size_t output_size);
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100110
111/** The random generator state for the PSA subsystem.
112 *
113 * This macro expands to an expression which is suitable as the `p_rng`
114 * random generator state parameter of many `mbedtls_xxx` functions.
115 * It must be used in combination with the random generator function
116 * mbedtls_psa_get_random().
117 *
118 * The implementation of this macro depends on the configuration of the
119 * library. Do not make any assumption on its nature.
120 */
121#define MBEDTLS_PSA_RANDOM_STATE NULL
122
123#else /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
124
125#if defined(MBEDTLS_CTR_DRBG_C)
126#include "mbedtls/ctr_drbg.h"
127typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t;
128static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_ctr_drbg_random;
129#elif defined(MBEDTLS_HMAC_DRBG_C)
130#include "mbedtls/hmac_drbg.h"
131typedef mbedtls_hmac_drbg_context mbedtls_psa_drbg_context_t;
132static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_hmac_drbg_random;
133#endif
134extern mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state;
135
136#define MBEDTLS_PSA_RANDOM_STATE mbedtls_psa_random_state
137
138#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
139
Andrzej Kurek747ab4e2023-02-28 10:32:47 -0500140typedef struct {
Gilles Peskineb567f832023-05-02 21:38:11 +0200141 /* Error codes used by PSA crypto are in -255..-128, fitting in 16 bits. */
142 int16_t psa_status;
143 /* Error codes used by Mbed TLS are in one of the ranges
Gilles Peskine4837e9d2023-05-07 20:27:13 +0200144 * -127..-1 (low-level) or -32767..-4096 (high-level with a low-level
145 * code optionally added), fitting in 16 bits. */
Andrzej Kurek747ab4e2023-02-28 10:32:47 -0500146 int16_t mbedtls_error;
Andrzej Kurek270b3f92023-03-03 05:54:13 -0500147} mbedtls_error_pair_t;
Andrzej Kurek747ab4e2023-02-28 10:32:47 -0500148
Manuel Pégourié-Gonnard725d2e22023-03-29 12:38:37 +0200149#if defined(MBEDTLS_MD_LIGHT)
Andrzej Kurek270b3f92023-03-03 05:54:13 -0500150extern const mbedtls_error_pair_t psa_to_md_errors[4];
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500151#endif
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100152
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500153#if defined(MBEDTLS_LMS_C)
Andrzej Kurek270b3f92023-03-03 05:54:13 -0500154extern const mbedtls_error_pair_t psa_to_lms_errors[3];
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500155#endif
156
157#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Andrzej Kurek270b3f92023-03-03 05:54:13 -0500158extern const mbedtls_error_pair_t psa_to_ssl_errors[7];
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500159#endif
160
161#if defined(PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY) || \
Valerio Setti8bb57632023-05-26 13:48:07 +0200162 defined(MBEDTLS_PSA_WANT_KEY_TYPE_RSA_KEY_PAIR_LEGACY)
Andrzej Kurek270b3f92023-03-03 05:54:13 -0500163extern const mbedtls_error_pair_t psa_to_pk_rsa_errors[8];
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500164#endif
165
166#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
167 defined(PSA_WANT_KEY_TYPE_ECC_PUBLIC_KEY)
Andrzej Kurek270b3f92023-03-03 05:54:13 -0500168extern const mbedtls_error_pair_t psa_to_pk_ecdsa_errors[7];
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500169#endif
170
Andrzej Kurek138b30a2023-02-28 10:06:42 -0500171/* Generic fallback function for error translation,
172 * when the received state was not module-specific. */
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500173int psa_generic_status_to_mbedtls(psa_status_t status);
174
Andrzej Kurek138b30a2023-02-28 10:06:42 -0500175/* This function iterates over provided local error translations,
176 * and if no match was found - calls the fallback error translation function. */
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500177int psa_status_to_mbedtls(psa_status_t status,
Andrzej Kurek270b3f92023-03-03 05:54:13 -0500178 const mbedtls_error_pair_t *local_translations,
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500179 size_t local_errors_num,
180 int (*fallback_f)(psa_status_t));
181
Andrzej Kurek138b30a2023-02-28 10:06:42 -0500182/* The second out of three-stage error handling functions of the pk module,
183 * acts as a fallback after RSA / ECDSA error translation, and if no match
184 * is found, it itself calls psa_generic_status_to_mbedtls. */
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500185int psa_pk_status_to_mbedtls(psa_status_t status);
186
Andrzej Kurek138b30a2023-02-28 10:06:42 -0500187/* Utility macro to shorten the defines of error translator in modules. */
Valerio Settiab9dc662023-03-27 14:02:08 +0200188#define PSA_TO_MBEDTLS_ERR_LIST(status, error_list, fallback_f) \
189 psa_status_to_mbedtls(status, error_list, \
190 sizeof(error_list)/sizeof(error_list[0]), \
191 fallback_f)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -0500192
193#endif /* MBEDTLS_PSA_CRYPTO_C */
Hanno Becker186b65a2018-11-19 15:14:21 +0000194#endif /* MBEDTLS_PSA_UTIL_H */