4
* \brief Utility functions for the use of the PSA Crypto library.
6
* \warning This function is not part of the public API and may
10
* Copyright The Mbed TLS Contributors
11
* SPDX-License-Identifier: Apache-2.0
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
17
* http://www.apache.org/licenses/LICENSE-2.0
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.
26
#ifndef MBEDTLS_PSA_UTIL_H
27
#define MBEDTLS_PSA_UTIL_H
29
#if !defined(MBEDTLS_CONFIG_FILE)
30
#include "mbedtls/config.h"
32
#include MBEDTLS_CONFIG_FILE
35
#if defined(MBEDTLS_USE_PSA_CRYPTO)
37
#include "psa/crypto.h"
39
#include "mbedtls/ecp.h"
40
#include "mbedtls/md.h"
41
#include "mbedtls/pk.h"
42
#include "mbedtls/oid.h"
46
/* Translations for symmetric crypto. */
48
static inline psa_key_type_t mbedtls_psa_translate_cipher_type(
49
mbedtls_cipher_type_t cipher )
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 );
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 ); */
81
static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode(
82
mbedtls_cipher_mode_t mode, size_t taglen )
86
case MBEDTLS_MODE_ECB:
87
return( PSA_ALG_ECB_NO_PADDING );
88
case MBEDTLS_MODE_GCM:
89
return( PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, taglen ) );
90
case MBEDTLS_MODE_CCM:
91
return( PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, taglen ) );
92
case MBEDTLS_MODE_CBC:
94
return( PSA_ALG_CBC_NO_PADDING );
95
/* Intentional fallthrough for taglen != 0 */
102
static inline psa_key_usage_t mbedtls_psa_translate_cipher_operation(
103
mbedtls_operation_t op )
107
case MBEDTLS_ENCRYPT:
108
return( PSA_KEY_USAGE_ENCRYPT );
109
case MBEDTLS_DECRYPT:
110
return( PSA_KEY_USAGE_DECRYPT );
116
/* Translations for hashing. */
118
static inline psa_algorithm_t mbedtls_psa_translate_md( mbedtls_md_type_t md_alg )
122
#if defined(MBEDTLS_MD2_C)
124
return( PSA_ALG_MD2 );
126
#if defined(MBEDTLS_MD4_C)
128
return( PSA_ALG_MD4 );
130
#if defined(MBEDTLS_MD5_C)
132
return( PSA_ALG_MD5 );
134
#if defined(MBEDTLS_SHA1_C)
135
case MBEDTLS_MD_SHA1:
136
return( PSA_ALG_SHA_1 );
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 );
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 );
150
#if defined(MBEDTLS_RIPEMD160_C)
151
case MBEDTLS_MD_RIPEMD160:
152
return( PSA_ALG_RIPEMD160 );
154
case MBEDTLS_MD_NONE: /* Intentional fallthrough */
160
/* Translations for ECC. */
162
static inline int mbedtls_psa_get_ecc_oid_from_id(
163
psa_ecc_family_t curve, size_t bits,
164
char const **oid, size_t *oid_len )
168
case PSA_ECC_FAMILY_SECP_R1:
171
#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
173
*oid = MBEDTLS_OID_EC_GRP_SECP192R1;
174
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192R1 );
176
#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
177
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
179
*oid = MBEDTLS_OID_EC_GRP_SECP224R1;
180
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224R1 );
182
#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
183
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
185
*oid = MBEDTLS_OID_EC_GRP_SECP256R1;
186
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256R1 );
188
#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
189
#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
191
*oid = MBEDTLS_OID_EC_GRP_SECP384R1;
192
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP384R1 );
194
#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
195
#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
197
*oid = MBEDTLS_OID_EC_GRP_SECP521R1;
198
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP521R1 );
200
#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
203
case PSA_ECC_FAMILY_SECP_K1:
206
#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
208
*oid = MBEDTLS_OID_EC_GRP_SECP192K1;
209
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP192K1 );
211
#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
212
#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
214
*oid = MBEDTLS_OID_EC_GRP_SECP224K1;
215
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP224K1 );
217
#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
218
#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
220
*oid = MBEDTLS_OID_EC_GRP_SECP256K1;
221
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_SECP256K1 );
223
#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
226
case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
229
#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
231
*oid = MBEDTLS_OID_EC_GRP_BP256R1;
232
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP256R1 );
234
#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
235
#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
237
*oid = MBEDTLS_OID_EC_GRP_BP384R1;
238
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP384R1 );
240
#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
241
#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
243
*oid = MBEDTLS_OID_EC_GRP_BP512R1;
244
*oid_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_EC_GRP_BP512R1 );
246
#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
255
#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH 1
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 )
262
#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
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 )
269
#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
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 )
276
#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
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 )
283
#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
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 )
290
#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
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 )
297
#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
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 )
304
#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
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 )
311
#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
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 )
318
#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
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 )
325
#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
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 )
332
#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
335
/* Translations for PK layer */
337
static inline int mbedtls_psa_err_translate_pk( psa_status_t status )
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 );
347
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 );
351
/* All other failures */
352
case PSA_ERROR_COMMUNICATION_FAILURE:
353
case PSA_ERROR_HARDWARE_FAILURE:
354
case PSA_ERROR_CORRUPTION_DETECTED:
355
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. */
359
return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
363
/* Translations for ECC */
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. */
368
#if defined(MBEDTLS_ECP_C)
369
static inline psa_key_type_t mbedtls_psa_parse_tls_ecc_group(
370
uint16_t tls_ecc_grp_reg_id, size_t *bits )
372
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 )
376
return( PSA_KEY_TYPE_ECC_KEY_PAIR(
377
mbedtls_ecc_group_to_psa( curve_info->grp_id, bits ) ) );
379
#endif /* MBEDTLS_ECP_C */
381
/* 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.
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
391
static inline int mbedtls_psa_tls_psa_ec_to_ecpoint( unsigned char *src,
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.
406
static inline int mbedtls_psa_tls_ecpoint_to_psa_ec( unsigned char const *src,
412
if( srclen > dstlen )
413
return( MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL );
415
memcpy( dst, src, srclen );
420
#endif /* MBEDTLS_USE_PSA_CRYPTO */
422
#endif /* MBEDTLS_PSA_UTIL_H */