blob: f6070dcba8d5630f674221fa3459cedadbcd17c5 [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 *
6 * \warning This function is not part of the public API and may
7 * 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
Jerry Yub02ee182022-03-16 10:30:41 +080032#if defined(MBEDTLS_PSA_CRYPTO_C)
Hanno Becker87837b22018-11-08 13:32:02 +000033
34#include "psa/crypto.h"
35
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010036#include "mbedtls/ecp.h"
37#include "mbedtls/md.h"
38#include "mbedtls/pk.h"
39#include "mbedtls/oid.h"
TRodziewicz3408d602021-04-21 13:25:25 +020040#include "mbedtls/error.h"
Hanno Becker87837b22018-11-08 13:32:02 +000041
Hanno Beckerf75f9122019-01-07 15:36:51 +000042#include <string.h>
43
Hanno Becker55251262018-11-12 09:29:12 +000044/* Translations for symmetric crypto. */
45
Hanno Beckerb26c1932018-11-12 10:18:57 +000046static inline psa_key_type_t mbedtls_psa_translate_cipher_type(
Gilles Peskine449bd832023-01-11 14:50:10 +010047 mbedtls_cipher_type_t cipher)
Hanno Becker55251262018-11-12 09:29:12 +000048{
Gilles Peskine449bd832023-01-11 14:50:10 +010049 switch (cipher) {
Hanno Becker55251262018-11-12 09:29:12 +000050 case MBEDTLS_CIPHER_AES_128_CCM:
51 case MBEDTLS_CIPHER_AES_192_CCM:
52 case MBEDTLS_CIPHER_AES_256_CCM:
Mateusz Starzyka706e5e2021-10-28 17:59:06 +020053 case MBEDTLS_CIPHER_AES_128_CCM_STAR_NO_TAG:
54 case MBEDTLS_CIPHER_AES_192_CCM_STAR_NO_TAG:
55 case MBEDTLS_CIPHER_AES_256_CCM_STAR_NO_TAG:
Hanno Becker55251262018-11-12 09:29:12 +000056 case MBEDTLS_CIPHER_AES_128_GCM:
57 case MBEDTLS_CIPHER_AES_192_GCM:
58 case MBEDTLS_CIPHER_AES_256_GCM:
59 case MBEDTLS_CIPHER_AES_128_CBC:
60 case MBEDTLS_CIPHER_AES_192_CBC:
61 case MBEDTLS_CIPHER_AES_256_CBC:
Przemyslaw Stekiel80c6a8e2021-09-29 12:13:11 +020062 case MBEDTLS_CIPHER_AES_128_ECB:
Przemyslaw Stekiel86de1b72021-09-29 19:43:40 +020063 case MBEDTLS_CIPHER_AES_192_ECB:
64 case MBEDTLS_CIPHER_AES_256_ECB:
Gilles Peskine449bd832023-01-11 14:50:10 +010065 return PSA_KEY_TYPE_AES;
Hanno Becker55251262018-11-12 09:29:12 +000066
67 /* ARIA not yet supported in PSA. */
68 /* case MBEDTLS_CIPHER_ARIA_128_CCM:
69 case MBEDTLS_CIPHER_ARIA_192_CCM:
70 case MBEDTLS_CIPHER_ARIA_256_CCM:
Mateusz Starzyka706e5e2021-10-28 17:59:06 +020071 case MBEDTLS_CIPHER_ARIA_128_CCM_STAR_NO_TAG:
72 case MBEDTLS_CIPHER_ARIA_192_CCM_STAR_NO_TAG:
73 case MBEDTLS_CIPHER_ARIA_256_CCM_STAR_NO_TAG:
Hanno Becker55251262018-11-12 09:29:12 +000074 case MBEDTLS_CIPHER_ARIA_128_GCM:
75 case MBEDTLS_CIPHER_ARIA_192_GCM:
76 case MBEDTLS_CIPHER_ARIA_256_GCM:
77 case MBEDTLS_CIPHER_ARIA_128_CBC:
78 case MBEDTLS_CIPHER_ARIA_192_CBC:
79 case MBEDTLS_CIPHER_ARIA_256_CBC:
80 return( PSA_KEY_TYPE_ARIA ); */
81
82 default:
Gilles Peskine449bd832023-01-11 14:50:10 +010083 return 0;
Hanno Becker55251262018-11-12 09:29:12 +000084 }
85}
86
Hanno Beckerb26c1932018-11-12 10:18:57 +000087static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +010088 mbedtls_cipher_mode_t mode, size_t taglen)
Hanno Becker55251262018-11-12 09:29:12 +000089{
Gilles Peskine449bd832023-01-11 14:50:10 +010090 switch (mode) {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +020091 case MBEDTLS_MODE_ECB:
Gilles Peskine449bd832023-01-11 14:50:10 +010092 return PSA_ALG_ECB_NO_PADDING;
Hanno Becker55251262018-11-12 09:29:12 +000093 case MBEDTLS_MODE_GCM:
Gilles Peskine449bd832023-01-11 14:50:10 +010094 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, taglen);
Hanno Becker55251262018-11-12 09:29:12 +000095 case MBEDTLS_MODE_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +010096 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen);
Mateusz Starzyka706e5e2021-10-28 17:59:06 +020097 case MBEDTLS_MODE_CCM_STAR_NO_TAG:
98 return PSA_ALG_CCM_STAR_NO_TAG;
Hanno Becker55251262018-11-12 09:29:12 +000099 case MBEDTLS_MODE_CBC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 if (taglen == 0) {
101 return PSA_ALG_CBC_NO_PADDING;
102 } else {
103 return 0;
104 }
Hanno Becker55251262018-11-12 09:29:12 +0000105 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 return 0;
Hanno Becker55251262018-11-12 09:29:12 +0000107 }
108}
109
Hanno Beckerb26c1932018-11-12 10:18:57 +0000110static inline psa_key_usage_t mbedtls_psa_translate_cipher_operation(
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 mbedtls_operation_t op)
Hanno Becker55251262018-11-12 09:29:12 +0000112{
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 switch (op) {
Hanno Becker55251262018-11-12 09:29:12 +0000114 case MBEDTLS_ENCRYPT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 return PSA_KEY_USAGE_ENCRYPT;
Hanno Becker55251262018-11-12 09:29:12 +0000116 case MBEDTLS_DECRYPT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 return PSA_KEY_USAGE_DECRYPT;
Hanno Becker55251262018-11-12 09:29:12 +0000118 default:
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 return 0;
Hanno Becker55251262018-11-12 09:29:12 +0000120 }
121}
122
123/* Translations for hashing. */
124
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200125/* Note: this function should not be used from inside the library, use
126 * mbedtls_hash_info_psa_from_md() from the internal hash_info.h instead.
127 * It is kept only for compatibility in case applications were using it. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100128static inline psa_algorithm_t mbedtls_psa_translate_md(mbedtls_md_type_t md_alg)
Hanno Becker87837b22018-11-08 13:32:02 +0000129{
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 switch (md_alg) {
Manuel Pégourié-Gonnardbab73ab2022-07-08 09:53:17 +0200131#if defined(MBEDTLS_MD5_C) || defined(PSA_WANT_ALG_MD5)
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 case MBEDTLS_MD_MD5:
133 return PSA_ALG_MD5;
Hanno Becker87837b22018-11-08 13:32:02 +0000134#endif
Manuel Pégourié-Gonnardbab73ab2022-07-08 09:53:17 +0200135#if defined(MBEDTLS_SHA1_C) || defined(PSA_WANT_ALG_SHA_1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 case MBEDTLS_MD_SHA1:
137 return PSA_ALG_SHA_1;
Hanno Becker87837b22018-11-08 13:32:02 +0000138#endif
Manuel Pégourié-Gonnardbab73ab2022-07-08 09:53:17 +0200139#if defined(MBEDTLS_SHA224_C) || defined(PSA_WANT_ALG_SHA_224)
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 case MBEDTLS_MD_SHA224:
141 return PSA_ALG_SHA_224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +0200142#endif
Manuel Pégourié-Gonnardbab73ab2022-07-08 09:53:17 +0200143#if defined(MBEDTLS_SHA256_C) || defined(PSA_WANT_ALG_SHA_256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 case MBEDTLS_MD_SHA256:
145 return PSA_ALG_SHA_256;
Hanno Becker87837b22018-11-08 13:32:02 +0000146#endif
Manuel Pégourié-Gonnardbab73ab2022-07-08 09:53:17 +0200147#if defined(MBEDTLS_SHA384_C) || defined(PSA_WANT_ALG_SHA_384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 case MBEDTLS_MD_SHA384:
149 return PSA_ALG_SHA_384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +0200150#endif
Manuel Pégourié-Gonnardbab73ab2022-07-08 09:53:17 +0200151#if defined(MBEDTLS_SHA512_C) || defined(PSA_WANT_ALG_SHA_512)
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 case MBEDTLS_MD_SHA512:
153 return PSA_ALG_SHA_512;
Hanno Becker87837b22018-11-08 13:32:02 +0000154#endif
Manuel Pégourié-Gonnardbab73ab2022-07-08 09:53:17 +0200155#if defined(MBEDTLS_RIPEMD160_C) || defined(PSA_WANT_ALG_RIPEMD160)
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 case MBEDTLS_MD_RIPEMD160:
157 return PSA_ALG_RIPEMD160;
Hanno Becker87837b22018-11-08 13:32:02 +0000158#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 case MBEDTLS_MD_NONE:
160 return 0;
161 default:
162 return 0;
Hanno Becker87837b22018-11-08 13:32:02 +0000163 }
164}
165
Hanno Becker55251262018-11-12 09:29:12 +0000166/* Translations for ECC. */
167
Hanno Becker812e1242019-02-01 10:06:51 +0000168static inline int mbedtls_psa_get_ecc_oid_from_id(
Paul Elliott8ff510a2020-06-02 17:19:28 +0100169 psa_ecc_family_t curve, size_t bits,
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 char const **oid, size_t *oid_len)
Hanno Becker812e1242019-02-01 10:06:51 +0000171{
Gilles Peskine449bd832023-01-11 14:50:10 +0100172 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100173 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 switch (bits) {
Hanno Becker812e1242019-02-01 10:06:51 +0000175#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100176 case 192:
177 *oid = MBEDTLS_OID_EC_GRP_SECP192R1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP192R1);
179 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000180#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
181#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100182 case 224:
183 *oid = MBEDTLS_OID_EC_GRP_SECP224R1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP224R1);
185 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000186#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
187#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100188 case 256:
189 *oid = MBEDTLS_OID_EC_GRP_SECP256R1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP256R1);
191 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000192#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
193#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100194 case 384:
195 *oid = MBEDTLS_OID_EC_GRP_SECP384R1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP384R1);
197 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000198#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
199#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100200 case 521:
201 *oid = MBEDTLS_OID_EC_GRP_SECP521R1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP521R1);
203 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000204#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Gilles Peskine89177e82019-12-03 21:19:09 +0100205 }
206 break;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100207 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 switch (bits) {
Hanno Becker812e1242019-02-01 10:06:51 +0000209#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100210 case 192:
211 *oid = MBEDTLS_OID_EC_GRP_SECP192K1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP192K1);
213 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000214#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
215#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100216 case 224:
217 *oid = MBEDTLS_OID_EC_GRP_SECP224K1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP224K1);
219 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000220#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
221#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100222 case 256:
223 *oid = MBEDTLS_OID_EC_GRP_SECP256K1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP256K1);
225 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000226#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
Gilles Peskine89177e82019-12-03 21:19:09 +0100227 }
228 break;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100229 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 switch (bits) {
Hanno Becker812e1242019-02-01 10:06:51 +0000231#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100232 case 256:
233 *oid = MBEDTLS_OID_EC_GRP_BP256R1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_BP256R1);
235 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000236#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
237#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100238 case 384:
239 *oid = MBEDTLS_OID_EC_GRP_BP384R1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_BP384R1);
241 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000242#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
243#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100244 case 512:
245 *oid = MBEDTLS_OID_EC_GRP_BP512R1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_BP512R1);
247 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000248#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
Gilles Peskine89177e82019-12-03 21:19:09 +0100249 }
250 break;
Hanno Becker812e1242019-02-01 10:06:51 +0000251 }
Gilles Peskine89177e82019-12-03 21:19:09 +0100252 (void) oid;
253 (void) oid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 return -1;
Hanno Becker812e1242019-02-01 10:06:51 +0000255}
256
Manuel Pégourié-Gonnard59753762022-01-18 11:52:11 +0100257#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH \
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
Hanno Beckerdf51dbe2019-02-18 16:41:55 +0000259
Valerio Setti1337a4f2023-01-30 15:54:55 +0100260#define MBEDTLS_PSA_MAX_EC_KEY_PAIR_LENGTH \
261 PSA_KEY_EXPORT_ECC_KEY_PAIR_MAX_SIZE(PSA_VENDOR_ECC_MAX_CURVE_BITS)
262
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100263/* Expose whatever RNG the PSA subsystem uses to applications using the
Gilles Peskine996f2162021-02-16 16:50:00 +0100264 * mbedtls_xxx API. The declarations and definitions here need to be
265 * consistent with the implementation in library/psa_crypto_random_impl.h.
266 * See that file for implementation documentation. */
Jerry Yu406cf272022-03-22 11:33:42 +0800267
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100268
269/* The type of a `f_rng` random generator function that many library functions
270 * take.
271 *
272 * This type name is not part of the Mbed TLS stable API. It may be renamed
273 * or moved without warning.
274 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100275typedef int mbedtls_f_rng_t(void *p_rng, unsigned char *output, size_t output_size);
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100276
277#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
278
279/** The random generator function for the PSA subsystem.
280 *
281 * This function is suitable as the `f_rng` random generator function
Gilles Peskine2cff7e22021-02-16 16:49:42 +0100282 * parameter of many `mbedtls_xxx` functions. Use #MBEDTLS_PSA_RANDOM_STATE
283 * to obtain the \p p_rng parameter.
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100284 *
285 * The implementation of this function depends on the configuration of the
286 * library.
Gilles Peskine2cff7e22021-02-16 16:49:42 +0100287 *
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100288 * \note Depending on the configuration, this may be a function or
289 * a pointer to a function.
290 *
291 * \note This function may only be used if the PSA crypto subsystem is active.
292 * This means that you must call psa_crypto_init() before any call to
293 * this function, and you must not call this function after calling
294 * mbedtls_psa_crypto_free().
295 *
296 * \param p_rng The random generator context. This must be
297 * #MBEDTLS_PSA_RANDOM_STATE. No other state is
298 * supported.
299 * \param output The buffer to fill. It must have room for
300 * \c output_size bytes.
301 * \param output_size The number of bytes to write to \p output.
302 * This function may fail if \p output_size is too
303 * large. It is guaranteed to accept any output size
304 * requested by Mbed TLS library functions. The
305 * maximum request size depends on the library
306 * configuration.
307 *
308 * \return \c 0 on success.
309 * \return An `MBEDTLS_ERR_ENTROPY_xxx`,
Gilles Peskine2cff7e22021-02-16 16:49:42 +0100310 * `MBEDTLS_ERR_PLATFORM_xxx,
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100311 * `MBEDTLS_ERR_CTR_DRBG_xxx` or
312 * `MBEDTLS_ERR_HMAC_DRBG_xxx` on error.
313 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100314int mbedtls_psa_get_random(void *p_rng,
315 unsigned char *output,
316 size_t output_size);
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100317
318/** The random generator state for the PSA subsystem.
319 *
320 * This macro expands to an expression which is suitable as the `p_rng`
321 * random generator state parameter of many `mbedtls_xxx` functions.
322 * It must be used in combination with the random generator function
323 * mbedtls_psa_get_random().
324 *
325 * The implementation of this macro depends on the configuration of the
326 * library. Do not make any assumption on its nature.
327 */
328#define MBEDTLS_PSA_RANDOM_STATE NULL
329
330#else /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
331
332#if defined(MBEDTLS_CTR_DRBG_C)
333#include "mbedtls/ctr_drbg.h"
334typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t;
335static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_ctr_drbg_random;
336#elif defined(MBEDTLS_HMAC_DRBG_C)
337#include "mbedtls/hmac_drbg.h"
338typedef mbedtls_hmac_drbg_context mbedtls_psa_drbg_context_t;
339static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_hmac_drbg_random;
340#endif
341extern mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state;
342
343#define MBEDTLS_PSA_RANDOM_STATE mbedtls_psa_random_state
344
345#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
346
Jerry Yu406cf272022-03-22 11:33:42 +0800347#endif /* MBEDTLS_PSA_CRYPTO_C */
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100348
Hanno Becker186b65a2018-11-19 15:14:21 +0000349#endif /* MBEDTLS_PSA_UTIL_H */