blob: 9a1a2eae2f7c0f432aaf3c52e6a673496abbd10d [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(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010049 mbedtls_cipher_type_t cipher)
Hanno Becker55251262018-11-12 09:29:12 +000050{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010051 switch (cipher) {
Hanno Becker55251262018-11-12 09:29:12 +000052 case MBEDTLS_CIPHER_AES_128_CCM:
53 case MBEDTLS_CIPHER_AES_192_CCM:
54 case MBEDTLS_CIPHER_AES_256_CCM:
55 case MBEDTLS_CIPHER_AES_128_GCM:
56 case MBEDTLS_CIPHER_AES_192_GCM:
57 case MBEDTLS_CIPHER_AES_256_GCM:
58 case MBEDTLS_CIPHER_AES_128_CBC:
59 case MBEDTLS_CIPHER_AES_192_CBC:
60 case MBEDTLS_CIPHER_AES_256_CBC:
Przemyslaw Stekielf0fa86e2021-09-29 12:13:11 +020061 case MBEDTLS_CIPHER_AES_128_ECB:
Przemyslaw Stekiel39f4e752021-09-29 19:43:40 +020062 case MBEDTLS_CIPHER_AES_192_ECB:
63 case MBEDTLS_CIPHER_AES_256_ECB:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064 return PSA_KEY_TYPE_AES;
Hanno Becker55251262018-11-12 09:29:12 +000065
66 /* ARIA not yet supported in PSA. */
67 /* case MBEDTLS_CIPHER_ARIA_128_CCM:
68 case MBEDTLS_CIPHER_ARIA_192_CCM:
69 case MBEDTLS_CIPHER_ARIA_256_CCM:
70 case MBEDTLS_CIPHER_ARIA_128_GCM:
71 case MBEDTLS_CIPHER_ARIA_192_GCM:
72 case MBEDTLS_CIPHER_ARIA_256_GCM:
73 case MBEDTLS_CIPHER_ARIA_128_CBC:
74 case MBEDTLS_CIPHER_ARIA_192_CBC:
75 case MBEDTLS_CIPHER_ARIA_256_CBC:
76 return( PSA_KEY_TYPE_ARIA ); */
77
78 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 return 0;
Hanno Becker55251262018-11-12 09:29:12 +000080 }
81}
82
Hanno Beckerb26c1932018-11-12 10:18:57 +000083static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 mbedtls_cipher_mode_t mode, size_t taglen)
Hanno Becker55251262018-11-12 09:29:12 +000085{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 switch (mode) {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +020087 case MBEDTLS_MODE_ECB:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088 return PSA_ALG_ECB_NO_PADDING;
Hanno Becker55251262018-11-12 09:29:12 +000089 case MBEDTLS_MODE_GCM:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010090 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_GCM, taglen);
Hanno Becker55251262018-11-12 09:29:12 +000091 case MBEDTLS_MODE_CCM:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 return PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen);
Hanno Becker55251262018-11-12 09:29:12 +000093 case MBEDTLS_MODE_CBC:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 if (taglen == 0) {
95 return PSA_ALG_CBC_NO_PADDING;
96 } else {
97 return 0;
98 }
Hanno Becker55251262018-11-12 09:29:12 +000099 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 return 0;
Hanno Becker55251262018-11-12 09:29:12 +0000101 }
102}
103
Hanno Beckerb26c1932018-11-12 10:18:57 +0000104static inline psa_key_usage_t mbedtls_psa_translate_cipher_operation(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100105 mbedtls_operation_t op)
Hanno Becker55251262018-11-12 09:29:12 +0000106{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107 switch (op) {
Hanno Becker55251262018-11-12 09:29:12 +0000108 case MBEDTLS_ENCRYPT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100109 return PSA_KEY_USAGE_ENCRYPT;
Hanno Becker55251262018-11-12 09:29:12 +0000110 case MBEDTLS_DECRYPT:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100111 return PSA_KEY_USAGE_DECRYPT;
Hanno Becker55251262018-11-12 09:29:12 +0000112 default:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 return 0;
Hanno Becker55251262018-11-12 09:29:12 +0000114 }
115}
116
117/* Translations for hashing. */
118
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119static inline psa_algorithm_t mbedtls_psa_translate_md(mbedtls_md_type_t md_alg)
Hanno Becker87837b22018-11-08 13:32:02 +0000120{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 switch (md_alg) {
Hanno Becker87837b22018-11-08 13:32:02 +0000122#if defined(MBEDTLS_MD2_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123 case MBEDTLS_MD_MD2:
124 return PSA_ALG_MD2;
Hanno Becker87837b22018-11-08 13:32:02 +0000125#endif
126#if defined(MBEDTLS_MD4_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127 case MBEDTLS_MD_MD4:
128 return PSA_ALG_MD4;
Hanno Becker87837b22018-11-08 13:32:02 +0000129#endif
130#if defined(MBEDTLS_MD5_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 case MBEDTLS_MD_MD5:
132 return PSA_ALG_MD5;
Hanno Becker87837b22018-11-08 13:32:02 +0000133#endif
134#if defined(MBEDTLS_SHA1_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 case MBEDTLS_MD_SHA1:
136 return PSA_ALG_SHA_1;
Hanno Becker87837b22018-11-08 13:32:02 +0000137#endif
138#if defined(MBEDTLS_SHA256_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 case MBEDTLS_MD_SHA224:
140 return PSA_ALG_SHA_224;
141 case MBEDTLS_MD_SHA256:
142 return PSA_ALG_SHA_256;
Hanno Becker87837b22018-11-08 13:32:02 +0000143#endif
144#if defined(MBEDTLS_SHA512_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100145 case MBEDTLS_MD_SHA384:
146 return PSA_ALG_SHA_384;
147 case MBEDTLS_MD_SHA512:
148 return PSA_ALG_SHA_512;
Hanno Becker87837b22018-11-08 13:32:02 +0000149#endif
150#if defined(MBEDTLS_RIPEMD160_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 case MBEDTLS_MD_RIPEMD160:
152 return PSA_ALG_RIPEMD160;
Hanno Becker87837b22018-11-08 13:32:02 +0000153#endif
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154 case MBEDTLS_MD_NONE:
155 return 0;
156 default:
157 return 0;
Hanno Becker87837b22018-11-08 13:32:02 +0000158 }
159}
160
Hanno Becker55251262018-11-12 09:29:12 +0000161/* Translations for ECC. */
162
Hanno Becker812e1242019-02-01 10:06:51 +0000163static inline int mbedtls_psa_get_ecc_oid_from_id(
Paul Elliott8ff510a2020-06-02 17:19:28 +0100164 psa_ecc_family_t curve, size_t bits,
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 char const **oid, size_t *oid_len)
Hanno Becker812e1242019-02-01 10:06:51 +0000166{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 switch (curve) {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100168 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169 switch (bits) {
Hanno Becker812e1242019-02-01 10:06:51 +0000170#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100171 case 192:
172 *oid = MBEDTLS_OID_EC_GRP_SECP192R1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100173 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP192R1);
174 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000175#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
176#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100177 case 224:
178 *oid = MBEDTLS_OID_EC_GRP_SECP224R1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP224R1);
180 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000181#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
182#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100183 case 256:
184 *oid = MBEDTLS_OID_EC_GRP_SECP256R1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP256R1);
186 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000187#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
188#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100189 case 384:
190 *oid = MBEDTLS_OID_EC_GRP_SECP384R1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP384R1);
192 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000193#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
194#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100195 case 521:
196 *oid = MBEDTLS_OID_EC_GRP_SECP521R1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100197 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP521R1);
198 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000199#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Gilles Peskine89177e82019-12-03 21:19:09 +0100200 }
201 break;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100202 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 switch (bits) {
Hanno Becker812e1242019-02-01 10:06:51 +0000204#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100205 case 192:
206 *oid = MBEDTLS_OID_EC_GRP_SECP192K1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP192K1);
208 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000209#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
210#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100211 case 224:
212 *oid = MBEDTLS_OID_EC_GRP_SECP224K1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP224K1);
214 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000215#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
216#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100217 case 256:
218 *oid = MBEDTLS_OID_EC_GRP_SECP256K1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100219 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_SECP256K1);
220 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000221#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
Gilles Peskine89177e82019-12-03 21:19:09 +0100222 }
223 break;
Paul Elliott8ff510a2020-06-02 17:19:28 +0100224 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100225 switch (bits) {
Hanno Becker812e1242019-02-01 10:06:51 +0000226#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100227 case 256:
228 *oid = MBEDTLS_OID_EC_GRP_BP256R1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_BP256R1);
230 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000231#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
232#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100233 case 384:
234 *oid = MBEDTLS_OID_EC_GRP_BP384R1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100235 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_BP384R1);
236 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000237#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
238#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Gilles Peskine89177e82019-12-03 21:19:09 +0100239 case 512:
240 *oid = MBEDTLS_OID_EC_GRP_BP512R1;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 *oid_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_EC_GRP_BP512R1);
242 return 0;
Hanno Becker812e1242019-02-01 10:06:51 +0000243#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
Gilles Peskine89177e82019-12-03 21:19:09 +0100244 }
245 break;
Hanno Becker812e1242019-02-01 10:06:51 +0000246 }
Gilles Peskine89177e82019-12-03 21:19:09 +0100247 (void) oid;
248 (void) oid_len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 return -1;
Hanno Becker812e1242019-02-01 10:06:51 +0000250}
251
Hanno Becker135baef2019-02-18 17:03:36 +0000252#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH 1
253
254#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100255#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < (2 * ((192 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000256#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100257#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH (2 * ((192 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000258#endif
259#endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
260
261#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < (2 * ((224 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000263#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH (2 * ((224 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000265#endif
266#endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
267
268#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100269#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < (2 * ((256 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000270#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH (2 * ((256 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000272#endif
273#endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
274
275#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < (2 * ((384 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000277#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH (2 * ((384 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000279#endif
280#endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
281
282#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < (2 * ((521 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000284#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100285#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH (2 * ((521 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000286#endif
287#endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
288
289#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100290#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < (2 * ((192 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000291#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH (2 * ((192 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000293#endif
294#endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
295
296#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < (2 * ((224 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000298#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH (2 * ((224 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000300#endif
301#endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
302
303#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100304#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < (2 * ((256 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000305#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH (2 * ((256 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000307#endif
308#endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
309
310#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < (2 * ((256 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000312#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH (2 * ((256 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000314#endif
315#endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
316
317#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < (2 * ((384 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000319#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100320#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH (2 * ((384 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000321#endif
322#endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
323
324#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100325#if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < (2 * ((512 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000326#undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327#define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH (2 * ((512 + 7) / 8) + 1)
Hanno Becker135baef2019-02-18 17:03:36 +0000328#endif
329#endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
330
Hanno Beckerdf51dbe2019-02-18 16:41:55 +0000331
Hanno Becker000334f2018-11-15 09:37:19 +0000332/* Translations for PK layer */
333
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100334static inline int mbedtls_psa_err_translate_pk(psa_status_t status)
Hanno Becker000334f2018-11-15 09:37:19 +0000335{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100336 switch (status) {
Hanno Becker000334f2018-11-15 09:37:19 +0000337 case PSA_SUCCESS:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338 return 0;
Hanno Becker000334f2018-11-15 09:37:19 +0000339 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100340 return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
Hanno Becker000334f2018-11-15 09:37:19 +0000341 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100342 return MBEDTLS_ERR_PK_ALLOC_FAILED;
Hanno Becker000334f2018-11-15 09:37:19 +0000343 case PSA_ERROR_INSUFFICIENT_ENTROPY:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 return MBEDTLS_ERR_ECP_RANDOM_FAILED;
Hanno Becker000334f2018-11-15 09:37:19 +0000345 case PSA_ERROR_BAD_STATE:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100346 return MBEDTLS_ERR_PK_BAD_INPUT_DATA;
Hanno Beckerf5f9ea22018-11-16 15:01:22 +0000347 /* All other failures */
348 case PSA_ERROR_COMMUNICATION_FAILURE:
349 case PSA_ERROR_HARDWARE_FAILURE:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200350 case PSA_ERROR_CORRUPTION_DETECTED:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100351 return MBEDTLS_ERR_PK_HW_ACCEL_FAILED;
Hanno Beckerf5f9ea22018-11-16 15:01:22 +0000352 default: /* We return the same as for the 'other failures',
353 * but list them separately nonetheless to indicate
354 * which failure conditions we have considered. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 return MBEDTLS_ERR_PK_HW_ACCEL_FAILED;
Hanno Becker000334f2018-11-15 09:37:19 +0000356 }
357}
358
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500359/* Translations for ECC */
360
361/* This function transforms an ECC group identifier from
362 * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
363 * into a PSA ECC group identifier. */
Gilles Peskined8197cb2019-12-12 17:56:46 +0100364#if defined(MBEDTLS_ECP_C)
Gilles Peskined1959dc2019-12-18 20:44:49 +0100365static inline psa_key_type_t mbedtls_psa_parse_tls_ecc_group(
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 uint16_t tls_ecc_grp_reg_id, size_t *bits)
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500367{
Gilles Peskined8197cb2019-12-12 17:56:46 +0100368 const mbedtls_ecp_curve_info *curve_info =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100369 mbedtls_ecp_curve_info_from_tls_id(tls_ecc_grp_reg_id);
370 if (curve_info == NULL) {
371 return 0;
372 }
373 return PSA_KEY_TYPE_ECC_KEY_PAIR(
374 mbedtls_ecc_group_to_psa(curve_info->grp_id, bits));
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500375}
Gilles Peskined8197cb2019-12-12 17:56:46 +0100376#endif /* MBEDTLS_ECP_C */
Andrzej Kurek93a38a32019-01-14 05:09:46 -0500377
Hanno Beckerf75f9122019-01-07 15:36:51 +0000378/* This function takes a buffer holding an EC public key
379 * exported through psa_export_public_key(), and converts
380 * it into an ECPoint structure to be put into a ClientKeyExchange
381 * message in an ECDHE exchange.
382 *
383 * Both the present and the foreseeable future format of EC public keys
384 * used by PSA have the ECPoint structure contained in the exported key
385 * as a subbuffer, and the function merely selects this subbuffer instead
386 * of making a copy.
387 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388static inline int mbedtls_psa_tls_psa_ec_to_ecpoint(unsigned char *src,
389 size_t srclen,
390 unsigned char **dst,
391 size_t *dstlen)
Hanno Beckerf75f9122019-01-07 15:36:51 +0000392{
393 *dst = src;
394 *dstlen = srclen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100395 return 0;
Hanno Beckerf75f9122019-01-07 15:36:51 +0000396}
397
398/* This function takes a buffer holding an ECPoint structure
399 * (as contained in a TLS ServerKeyExchange message for ECDHE
400 * exchanges) and converts it into a format that the PSA key
401 * agreement API understands.
402 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100403static inline int mbedtls_psa_tls_ecpoint_to_psa_ec(unsigned char const *src,
404 size_t srclen,
405 unsigned char *dst,
406 size_t dstlen,
407 size_t *olen)
Hanno Beckerf75f9122019-01-07 15:36:51 +0000408{
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 if (srclen > dstlen) {
410 return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
411 }
Hanno Beckerf75f9122019-01-07 15:36:51 +0000412
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100413 memcpy(dst, src, srclen);
Hanno Beckerf75f9122019-01-07 15:36:51 +0000414 *olen = srclen;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100415 return 0;
Hanno Beckerf75f9122019-01-07 15:36:51 +0000416}
417
Hanno Becker87837b22018-11-08 13:32:02 +0000418#endif /* MBEDTLS_USE_PSA_CRYPTO */
419
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100420/* Expose whatever RNG the PSA subsystem uses to applications using the
Gilles Peskine996f2162021-02-16 16:50:00 +0100421 * mbedtls_xxx API. The declarations and definitions here need to be
422 * consistent with the implementation in library/psa_crypto_random_impl.h.
423 * See that file for implementation documentation. */
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100424#if defined(MBEDTLS_PSA_CRYPTO_C)
425
426/* The type of a `f_rng` random generator function that many library functions
427 * take.
428 *
429 * This type name is not part of the Mbed TLS stable API. It may be renamed
430 * or moved without warning.
431 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100432typedef int mbedtls_f_rng_t(void *p_rng, unsigned char *output, size_t output_size);
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100433
434#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
435
436/** The random generator function for the PSA subsystem.
437 *
438 * This function is suitable as the `f_rng` random generator function
Gilles Peskine2cff7e22021-02-16 16:49:42 +0100439 * parameter of many `mbedtls_xxx` functions. Use #MBEDTLS_PSA_RANDOM_STATE
440 * to obtain the \p p_rng parameter.
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100441 *
442 * The implementation of this function depends on the configuration of the
443 * library.
Gilles Peskine2cff7e22021-02-16 16:49:42 +0100444 *
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100445 * \note Depending on the configuration, this may be a function or
446 * a pointer to a function.
447 *
448 * \note This function may only be used if the PSA crypto subsystem is active.
449 * This means that you must call psa_crypto_init() before any call to
450 * this function, and you must not call this function after calling
451 * mbedtls_psa_crypto_free().
452 *
453 * \param p_rng The random generator context. This must be
454 * #MBEDTLS_PSA_RANDOM_STATE. No other state is
455 * supported.
456 * \param output The buffer to fill. It must have room for
457 * \c output_size bytes.
458 * \param output_size The number of bytes to write to \p output.
459 * This function may fail if \p output_size is too
460 * large. It is guaranteed to accept any output size
461 * requested by Mbed TLS library functions. The
462 * maximum request size depends on the library
463 * configuration.
464 *
465 * \return \c 0 on success.
466 * \return An `MBEDTLS_ERR_ENTROPY_xxx`,
Gilles Peskine2cff7e22021-02-16 16:49:42 +0100467 * `MBEDTLS_ERR_PLATFORM_xxx,
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100468 * `MBEDTLS_ERR_CTR_DRBG_xxx` or
469 * `MBEDTLS_ERR_HMAC_DRBG_xxx` on error.
470 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100471int mbedtls_psa_get_random(void *p_rng,
472 unsigned char *output,
473 size_t output_size);
Gilles Peskinee3ed8022021-02-03 20:04:08 +0100474
475/** The random generator state for the PSA subsystem.
476 *
477 * This macro expands to an expression which is suitable as the `p_rng`
478 * random generator state parameter of many `mbedtls_xxx` functions.
479 * It must be used in combination with the random generator function
480 * mbedtls_psa_get_random().
481 *
482 * The implementation of this macro depends on the configuration of the
483 * library. Do not make any assumption on its nature.
484 */
485#define MBEDTLS_PSA_RANDOM_STATE NULL
486
487#else /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
488
489#if defined(MBEDTLS_CTR_DRBG_C)
490#include "mbedtls/ctr_drbg.h"
491typedef mbedtls_ctr_drbg_context mbedtls_psa_drbg_context_t;
492static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_ctr_drbg_random;
493#elif defined(MBEDTLS_HMAC_DRBG_C)
494#include "mbedtls/hmac_drbg.h"
495typedef mbedtls_hmac_drbg_context mbedtls_psa_drbg_context_t;
496static mbedtls_f_rng_t *const mbedtls_psa_get_random = mbedtls_hmac_drbg_random;
497#endif
498extern mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state;
499
500#define MBEDTLS_PSA_RANDOM_STATE mbedtls_psa_random_state
501
502#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
503
504#endif /* MBEDTLS_PSA_CRYPTO_C */
505
Hanno Becker186b65a2018-11-19 15:14:21 +0000506#endif /* MBEDTLS_PSA_UTIL_H */