blob: 4fa804d21ccd9b3a68d017e508341ca976b2443b [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
Hanno Becker87837b22018-11-08 13:32:02 +000028
29#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010030#include "mbedtls/config.h"
Hanno Becker87837b22018-11-08 13:32:02 +000031#else
32#include MBEDTLS_CONFIG_FILE
33#endif
34
35#if defined(MBEDTLS_USE_PSA_CRYPTO)
36
37#include "psa/crypto.h"
38
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010039#include "mbedtls/ecp.h"
40#include "mbedtls/md.h"
41#include "mbedtls/pk.h"
42#include "mbedtls/oid.h"
Hanno Becker87837b22018-11-08 13:32:02 +000043
Hanno Beckerf75f9122019-01-07 15:36:51 +000044#include <string.h>
45
Hanno Becker55251262018-11-12 09:29:12 +000046/* Translations for symmetric crypto. */
47
Hanno Beckerb26c1932018-11-12 10:18:57 +000048static inline psa_key_type_t mbedtls_psa_translate_cipher_type(
Hanno Becker55251262018-11-12 09:29:12 +000049 mbedtls_cipher_type_t cipher )
50{
51 switch( cipher )
52 {
53 case MBEDTLS_CIPHER_AES_128_CCM:
54 case MBEDTLS_CIPHER_AES_192_CCM:
55 case MBEDTLS_CIPHER_AES_256_CCM:
56 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:
62 return( PSA_KEY_TYPE_AES );
63
64 /* ARIA not yet supported in PSA. */
65 /* case MBEDTLS_CIPHER_ARIA_128_CCM:
66 case MBEDTLS_CIPHER_ARIA_192_CCM:
67 case MBEDTLS_CIPHER_ARIA_256_CCM:
68 case MBEDTLS_CIPHER_ARIA_128_GCM:
69 case MBEDTLS_CIPHER_ARIA_192_GCM:
70 case MBEDTLS_CIPHER_ARIA_256_GCM:
71 case MBEDTLS_CIPHER_ARIA_128_CBC:
72 case MBEDTLS_CIPHER_ARIA_192_CBC:
73 case MBEDTLS_CIPHER_ARIA_256_CBC:
74 return( PSA_KEY_TYPE_ARIA ); */
75
76 default:
77 return( 0 );
78 }
79}
80
Hanno Beckerb26c1932018-11-12 10:18:57 +000081static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode(
Hanno Becker010cf7e2018-11-15 15:48:57 +000082 mbedtls_cipher_mode_t mode, size_t taglen )
Hanno Becker55251262018-11-12 09:29:12 +000083{
84 switch( mode )
85 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +020086 case MBEDTLS_MODE_ECB:
Steven Cooremana6033e92020-08-25 11:47:50 +020087 return( PSA_ALG_ECB_NO_PADDING );
Hanno Becker55251262018-11-12 09:29:12 +000088 case MBEDTLS_MODE_GCM:
Hanno Becker010cf7e2018-11-15 15:48:57 +000089 return( PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, taglen ) );
Hanno Becker55251262018-11-12 09:29:12 +000090 case MBEDTLS_MODE_CCM:
Hanno Becker010cf7e2018-11-15 15:48:57 +000091 return( PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, taglen ) );
Hanno Becker55251262018-11-12 09:29:12 +000092 case MBEDTLS_MODE_CBC:
Hanno Becker010cf7e2018-11-15 15:48:57 +000093 if( taglen == 0 )
94 return( PSA_ALG_CBC_NO_PADDING );
95 /* Intentional fallthrough for taglen != 0 */
Jaeden Amero2d792662019-02-11 12:18:39 +000096 /* fallthrough */
Hanno Becker55251262018-11-12 09:29:12 +000097 default:
98 return( 0 );
99 }
100}
101
Hanno Beckerb26c1932018-11-12 10:18:57 +0000102static inline psa_key_usage_t mbedtls_psa_translate_cipher_operation(
103 mbedtls_operation_t op )
Hanno Becker55251262018-11-12 09:29:12 +0000104{
105 switch( op )
106 {
107 case MBEDTLS_ENCRYPT:
108 return( PSA_KEY_USAGE_ENCRYPT );
109 case MBEDTLS_DECRYPT:
110 return( PSA_KEY_USAGE_DECRYPT );
111 default:
112 return( 0 );
113 }
114}
115
116/* Translations for hashing. */
117
Hanno Beckerb26c1932018-11-12 10:18:57 +0000118static inline psa_algorithm_t mbedtls_psa_translate_md( mbedtls_md_type_t md_alg )
Hanno Becker87837b22018-11-08 13:32:02 +0000119{
120 switch( md_alg )
121 {
122#if defined(MBEDTLS_MD2_C)
123 case MBEDTLS_MD_MD2:
124 return( PSA_ALG_MD2 );
125#endif
126#if defined(MBEDTLS_MD4_C)
127 case MBEDTLS_MD_MD4:
128 return( PSA_ALG_MD4 );
129#endif
130#if defined(MBEDTLS_MD5_C)
131 case MBEDTLS_MD_MD5:
132 return( PSA_ALG_MD5 );
133#endif
134#if defined(MBEDTLS_SHA1_C)
135 case MBEDTLS_MD_SHA1:
136 return( PSA_ALG_SHA_1 );
137#endif
138#if defined(MBEDTLS_SHA256_C)
139 case MBEDTLS_MD_SHA224:
140 return( PSA_ALG_SHA_224 );
141 case MBEDTLS_MD_SHA256:
142 return( PSA_ALG_SHA_256 );
143#endif
144#if defined(MBEDTLS_SHA512_C)
145 case MBEDTLS_MD_SHA384:
146 return( PSA_ALG_SHA_384 );
147 case MBEDTLS_MD_SHA512:
148 return( PSA_ALG_SHA_512 );
149#endif
150#if defined(MBEDTLS_RIPEMD160_C)
151 case MBEDTLS_MD_RIPEMD160:
152 return( PSA_ALG_RIPEMD160 );
153#endif
154 case MBEDTLS_MD_NONE: /* Intentional fallthrough */
155 default:
156 return( 0 );
157 }
158}
159
Hanno Becker55251262018-11-12 09:29:12 +0000160/* Translations for ECC. */
161
Hanno Becker812e1242019-02-01 10:06:51 +0000162static inline int mbedtls_psa_get_ecc_oid_from_id(
Paul Elliott8ff510a2020-06-02 17:19:28 +0100163 psa_ecc_family_t curve, size_t bits,
Gilles Peskine89177e82019-12-03 21:19:09 +0100164 char const **oid, size_t *oid_len )
Hanno Becker812e1242019-02-01 10:06:51 +0000165{
166 switch( curve )
167 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100168 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine89177e82019-12-03 21:19:09 +0100169 switch( bits )
170 {
Hanno Becker812e1242019-02-01 10:06:51 +0000171#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100172 case 192:
173 *oid = MBEDTLS_OID_EC_GRP_SECP192R1;
174 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192R1 );
175 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000176#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
177#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100178 case 224:
179 *oid = MBEDTLS_OID_EC_GRP_SECP224R1;
180 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224R1 );
181 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000182#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
183#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100184 case 256:
185 *oid = MBEDTLS_OID_EC_GRP_SECP256R1;
186 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256R1 );
187 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000188#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
189#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100190 case 384:
191 *oid = MBEDTLS_OID_EC_GRP_SECP384R1;
192 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP384R1 );
193 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000194#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
195#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100196 case 521:
197 *oid = MBEDTLS_OID_EC_GRP_SECP521R1;
198 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP521R1 );
199 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000200#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Gilles Peskine89177e82019-12-03 21:19:09 +0100201 }
202 break;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100203 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine89177e82019-12-03 21:19:09 +0100204 switch( bits )
205 {
Hanno Becker812e1242019-02-01 10:06:51 +0000206#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100207 case 192:
208 *oid = MBEDTLS_OID_EC_GRP_SECP192K1;
209 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192K1 );
210 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000211#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
212#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100213 case 224:
214 *oid = MBEDTLS_OID_EC_GRP_SECP224K1;
215 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224K1 );
216 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000217#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
218#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100219 case 256:
220 *oid = MBEDTLS_OID_EC_GRP_SECP256K1;
221 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256K1 );
222 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000223#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
Gilles Peskine89177e82019-12-03 21:19:09 +0100224 }
225 break;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100226 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine89177e82019-12-03 21:19:09 +0100227 switch( bits )
228 {
Hanno Becker812e1242019-02-01 10:06:51 +0000229#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100230 case 256:
231 *oid = MBEDTLS_OID_EC_GRP_BP256R1;
232 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP256R1 );
233 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000234#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
235#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100236 case 384:
237 *oid = MBEDTLS_OID_EC_GRP_BP384R1;
238 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP384R1 );
239 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000240#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
241#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100242 case 512:
243 *oid = MBEDTLS_OID_EC_GRP_BP512R1;
244 *oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP512R1 );
245 return( 0 );
Hanno Becker812e1242019-02-01 10:06:51 +0000246#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
Gilles Peskine89177e82019-12-03 21:19:09 +0100247 }
248 break;
Hanno Becker812e1242019-02-01 10:06:51 +0000249 }
Gilles Peskine89177e82019-12-03 21:19:09 +0100250 (void) oid;
251 (void) oid_len;
252 return( -1 );
Hanno Becker812e1242019-02-01 10:06:51 +0000253}
254
Hanno Becker135baef2019-02-18 17:03:36 +0000255#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH 1
256
257#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
258#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
259#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
260#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
261#endif
262#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
263
264#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
265#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
266#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
267#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
268#endif
269#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
270
271#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
272#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
273#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
274#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
275#endif
276#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
277
278#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
279#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
280#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
281#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
282#endif
283#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
284
285#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
286#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 521 + 7 ) / 8 ) + 1 )
287#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
288#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 521 + 7 ) / 8 ) + 1 )
289#endif
290#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
291
292#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
293#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
294#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
295#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
296#endif
297#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
298
299#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
300#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
301#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
302#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
303#endif
304#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
305
306#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
307#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
308#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
309#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
310#endif
311#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
312
313#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
314#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
315#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
316#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
317#endif
318#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
319
320#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
321#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
322#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
323#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
324#endif
325#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
326
327#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
328#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 512 + 7 ) / 8 ) + 1 )
329#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
330#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 512 + 7 ) / 8 ) + 1 )
331#endif
332#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
333
Hanno Beckerdf51dbe2019-02-18 16:41:55 +0000334
Hanno Becker000334f2018-11-15 09:37:19 +0000335/* Translations for PK layer */
336
337static inline int mbedtls_psa_err_translate_pk( psa_status_t status )
338{
339 switch( status )
340 {
341 case PSA_SUCCESS:
342 return( 0 );
343 case PSA_ERROR_NOT_SUPPORTED:
344 return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
345 case PSA_ERROR_INSUFFICIENT_MEMORY:
346 return( MBEDTLS_ERR_PK_ALLOC_FAILED );
Hanno Becker000334f2018-11-15 09:37:19 +0000347 case PSA_ERROR_INSUFFICIENT_ENTROPY:
348 return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
349 case PSA_ERROR_BAD_STATE:
350 return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
Hanno Beckerf5f9ea22018-11-16 15:01:22 +0000351 /* All other failures */
352 case PSA_ERROR_COMMUNICATION_FAILURE:
353 case PSA_ERROR_HARDWARE_FAILURE:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200354 case PSA_ERROR_CORRUPTION_DETECTED:
Hanno Beckerf5f9ea22018-11-16 15:01:22 +0000355 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
356 default: /* We return the same as for the 'other failures',
357 * but list them separately nonetheless to indicate
358 * which failure conditions we have considered. */
Hanno Becker000334f2018-11-15 09:37:19 +0000359 return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
360 }
361}
362
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500363/* Translations for ECC */
364
365/* This function transforms an ECC group identifier from
366 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
367 * into a PSA ECC group identifier. */
Gilles Peskined8197cb2019-12-12 17:56:46 +0100368#if defined(MBEDTLS_ECP_C)
Gilles Peskined1959dc2019-12-18 20:44:49 +0100369static inline psa_key_type_t mbedtls_psa_parse_tls_ecc_group(
370 uint16_t tls_ecc_grp_reg_id, size_t *bits )
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500371{
Gilles Peskined8197cb2019-12-12 17:56:46 +0100372 const mbedtls_ecp_curve_info *curve_info =
373 mbedtls_ecp_curve_info_from_tls_id( tls_ecc_grp_reg_id );
374 if( curve_info == NULL )
375 return( 0 );
Gilles Peskined1959dc2019-12-18 20:44:49 +0100376 return( PSA_KEY_TYPE_ECC_KEY_PAIR(
377 mbedtls_ecc_group_to_psa( curve_info->grp_id, bits ) ) );
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500378}
Gilles Peskined8197cb2019-12-12 17:56:46 +0100379#endif /* MBEDTLS_ECP_C */
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500380
Hanno Beckerf75f9122019-01-07 15:36:51 +0000381/* This function takes a buffer holding an EC public key
382 * exported through psa_export_public_key(), and converts
383 * it into an ECPoint structure to be put into a ClientKeyExchange
384 * message in an ECDHE exchange.
385 *
386 * Both the present and the foreseeable future format of EC public keys
387 * used by PSA have the ECPoint structure contained in the exported key
388 * as a subbuffer, and the function merely selects this subbuffer instead
389 * of making a copy.
390 */
391static inline int mbedtls_psa_tls_psa_ec_to_ecpoint( unsigned char *src,
392 size_t srclen,
393 unsigned char **dst,
394 size_t *dstlen )
395{
396 *dst = src;
397 *dstlen = srclen;
398 return( 0 );
399}
400
401/* This function takes a buffer holding an ECPoint structure
402 * (as contained in a TLS ServerKeyExchange message for ECDHE
403 * exchanges) and converts it into a format that the PSA key
404 * agreement API understands.
405 */
Gilles Peskined1959dc2019-12-18 20:44:49 +0100406static inline int mbedtls_psa_tls_ecpoint_to_psa_ec( unsigned char const *src,
Hanno Beckerf75f9122019-01-07 15:36:51 +0000407 size_t srclen,
408 unsigned char *dst,
409 size_t dstlen,
410 size_t *olen )
411{
Hanno Beckerf75f9122019-01-07 15:36:51 +0000412 if( srclen > dstlen )
413 return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
414
415 memcpy( dst, src, srclen );
416 *olen = srclen;
417 return( 0 );
418}
419
Hanno Becker87837b22018-11-08 13:32:02 +0000420#endif /* MBEDTLS_USE_PSA_CRYPTO */
421
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100422/* Expose whatever RNG the PSA subsystem uses to applications using the
Gilles Peskine996f2162021-02-16 16:50:00 +0100423 * mbedtls_xxx API. The declarations and definitions here need to be
424 * consistent with the implementation in library/psa_crypto_random_impl.h.
425 * See that file for implementation documentation. */
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100426#if defined(MBEDTLS_PSA_CRYPTO_C)
427
428/* The type of a `f_rng` random generator function that many library functions
429 * take.
430 *
431 * This type name is not part of the Mbed TLS stable API. It may be renamed
432 * or moved without warning.
433 */
434typedef int mbedtls_f_rng_t( void *p_rng, unsigned char *output, size_t output_size );
435
436#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
437
438/** The random generator function for the PSA subsystem.
439 *
440 * This function is suitable as the `f_rng` random generator function
Gilles Peskine2cff7e22021-02-16 16:49:42 +0100441 * parameter of many `mbedtls_xxx` functions. Use #MBEDTLS_PSA_RANDOM_STATE
442 * to obtain the \p p_rng parameter.
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100443 *
444 * The implementation of this function depends on the configuration of the
445 * library.
Gilles Peskine2cff7e22021-02-16 16:49:42 +0100446 *
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100447 * \note Depending on the configuration, this may be a function or
448 * a pointer to a function.
449 *
450 * \note This function may only be used if the PSA crypto subsystem is active.
451 * This means that you must call psa_crypto_init() before any call to
452 * this function, and you must not call this function after calling
453 * mbedtls_psa_crypto_free().
454 *
455 * \param p_rng The random generator context. This must be
456 * #MBEDTLS_PSA_RANDOM_STATE. No other state is
457 * supported.
458 * \param output The buffer to fill. It must have room for
459 * \c output_size bytes.
460 * \param output_size The number of bytes to write to \p output.
461 * This function may fail if \p output_size is too
462 * large. It is guaranteed to accept any output size
463 * requested by Mbed TLS library functions. The
464 * maximum request size depends on the library
465 * configuration.
466 *
467 * \return \c 0 on success.
468 * \return An `MBEDTLS_ERR_ENTROPY_xxx`,
Gilles Peskine2cff7e22021-02-16 16:49:42 +0100469 * `MBEDTLS_ERR_PLATFORM_xxx,
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100470 * `MBEDTLS_ERR_CTR_DRBG_xxx` or
471 * `MBEDTLS_ERR_HMAC_DRBG_xxx` on error.
472 */
473int mbedtls_psa_get_random( void *p_rng,
474 unsigned char *output,
475 size_t output_size );
476
477/** The random generator state for the PSA subsystem.
478 *
479 * This macro expands to an expression which is suitable as the `p_rng`
480 * random generator state parameter of many `mbedtls_xxx` functions.
481 * It must be used in combination with the random generator function
482 * mbedtls_psa_get_random().
483 *
484 * The implementation of this macro depends on the configuration of the
485 * library. Do not make any assumption on its nature.
486 */
487#define MBEDTLS_PSA_RANDOM_STATE NULL
488
489#else /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
490
491#if defined(MBEDTLS_CTR_DRBG_C)
492#include "mbedtls/ctr_drbg.h"
493typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t;
494static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_ctr_drbg_random;
495#elif defined(MBEDTLS_HMAC_DRBG_C)
496#include "mbedtls/hmac_drbg.h"
497typedef mbedtls_hmac_drbg_context mbedtls_psa_drbg_context_t;
498static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_hmac_drbg_random;
499#endif
500extern mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state;
501
502#define MBEDTLS_PSA_RANDOM_STATE mbedtls_psa_random_state
503
504#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
505
506#endif /* MBEDTLS_PSA_CRYPTO_C */
507
Hanno Becker186b65a2018-11-19 15:14:21 +0000508#endif /* MBEDTLS_PSA_UTIL_H */