mbed TLS v2.24.0
psa_util.h
Go to the documentation of this file.
1 
9 /*
10  * Copyright The Mbed TLS Contributors
11  * 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.
24  */
25 
26 #ifndef MBEDTLS_PSA_UTIL_H
27 #define MBEDTLS_PSA_UTIL_H
28 
29 #if !defined(MBEDTLS_CONFIG_FILE)
30 #include "mbedtls/config.h"
31 #else
32 #include MBEDTLS_CONFIG_FILE
33 #endif
34 
35 #if defined(MBEDTLS_USE_PSA_CRYPTO)
36 
37 #include "psa/crypto.h"
38 
39 #include "mbedtls/ecp.h"
40 #include "mbedtls/md.h"
41 #include "mbedtls/pk.h"
42 #include "mbedtls/oid.h"
43 
44 #include <string.h>
45 
46 /* Translations for symmetric crypto. */
47 
49  mbedtls_cipher_type_t cipher )
50 {
51  switch( cipher )
52  {
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 
82  mbedtls_cipher_mode_t mode, size_t taglen )
83 {
84  switch( mode )
85  {
86  case MBEDTLS_MODE_GCM:
87  return( PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, taglen ) );
88  case MBEDTLS_MODE_CCM:
89  return( PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, taglen ) );
90  case MBEDTLS_MODE_CBC:
91  if( taglen == 0 )
92  return( PSA_ALG_CBC_NO_PADDING );
93  /* Intentional fallthrough for taglen != 0 */
94  /* fallthrough */
95  default:
96  return( 0 );
97  }
98 }
99 
102 {
103  switch( op )
104  {
105  case MBEDTLS_ENCRYPT:
106  return( PSA_KEY_USAGE_ENCRYPT );
107  case MBEDTLS_DECRYPT:
108  return( PSA_KEY_USAGE_DECRYPT );
109  default:
110  return( 0 );
111  }
112 }
113 
114 /* Translations for hashing. */
115 
117 {
118  switch( md_alg )
119  {
120 #if defined(MBEDTLS_MD2_C)
121  case MBEDTLS_MD_MD2:
122  return( PSA_ALG_MD2 );
123 #endif
124 #if defined(MBEDTLS_MD4_C)
125  case MBEDTLS_MD_MD4:
126  return( PSA_ALG_MD4 );
127 #endif
128 #if defined(MBEDTLS_MD5_C)
129  case MBEDTLS_MD_MD5:
130  return( PSA_ALG_MD5 );
131 #endif
132 #if defined(MBEDTLS_SHA1_C)
133  case MBEDTLS_MD_SHA1:
134  return( PSA_ALG_SHA_1 );
135 #endif
136 #if defined(MBEDTLS_SHA256_C)
137  case MBEDTLS_MD_SHA224:
138  return( PSA_ALG_SHA_224 );
139  case MBEDTLS_MD_SHA256:
140  return( PSA_ALG_SHA_256 );
141 #endif
142 #if defined(MBEDTLS_SHA512_C)
143  case MBEDTLS_MD_SHA384:
144  return( PSA_ALG_SHA_384 );
145  case MBEDTLS_MD_SHA512:
146  return( PSA_ALG_SHA_512 );
147 #endif
148 #if defined(MBEDTLS_RIPEMD160_C)
150  return( PSA_ALG_RIPEMD160 );
151 #endif
152  case MBEDTLS_MD_NONE: /* Intentional fallthrough */
153  default:
154  return( 0 );
155  }
156 }
157 
158 /* Translations for ECC. */
159 
161  psa_ecc_family_t curve, size_t bits,
162  char const **oid, size_t *oid_len )
163 {
164  switch( curve )
165  {
167  switch( bits )
168  {
169 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
170  case 192:
173  return( 0 );
174 #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
175 #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
176  case 224:
179  return( 0 );
180 #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
181 #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
182  case 256:
185  return( 0 );
186 #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
187 #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
188  case 384:
191  return( 0 );
192 #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
193 #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
194  case 521:
197  return( 0 );
198 #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
199  }
200  break;
202  switch( bits )
203  {
204 #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
205  case 192:
208  return( 0 );
209 #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
210 #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
211  case 224:
214  return( 0 );
215 #endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
216 #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
217  case 256:
220  return( 0 );
221 #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
222  }
223  break;
225  switch( bits )
226  {
227 #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
228  case 256:
231  return( 0 );
232 #endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
233 #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
234  case 384:
237  return( 0 );
238 #endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
239 #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
240  case 512:
243  return( 0 );
244 #endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
245  }
246  break;
247  }
248  (void) oid;
249  (void) oid_len;
250  return( -1 );
251 }
252 
253 #define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH 1
254 
255 #if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
256 #if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
257 #undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
258 #define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
259 #endif
260 #endif /* MBEDTLS_ECP_DP_SECP192R1_ENABLED */
261 
262 #if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
263 #if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
264 #undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
265 #define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
266 #endif
267 #endif /* MBEDTLS_ECP_DP_SECP224R1_ENABLED */
268 
269 #if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
270 #if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
271 #undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
272 #define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
273 #endif
274 #endif /* MBEDTLS_ECP_DP_SECP256R1_ENABLED */
275 
276 #if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
277 #if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
278 #undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
279 #define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
280 #endif
281 #endif /* MBEDTLS_ECP_DP_SECP384R1_ENABLED */
282 
283 #if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
284 #if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 521 + 7 ) / 8 ) + 1 )
285 #undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
286 #define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 521 + 7 ) / 8 ) + 1 )
287 #endif
288 #endif /* MBEDTLS_ECP_DP_SECP521R1_ENABLED */
289 
290 #if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
291 #if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
292 #undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
293 #define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 192 + 7 ) / 8 ) + 1 )
294 #endif
295 #endif /* MBEDTLS_ECP_DP_SECP192K1_ENABLED */
296 
297 #if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
298 #if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
299 #undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
300 #define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 224 + 7 ) / 8 ) + 1 )
301 #endif
302 #endif /* MBEDTLS_ECP_DP_SECP224K1_ENABLED */
303 
304 #if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
305 #if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
306 #undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
307 #define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
308 #endif
309 #endif /* MBEDTLS_ECP_DP_SECP256K1_ENABLED */
310 
311 #if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
312 #if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
313 #undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
314 #define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 256 + 7 ) / 8 ) + 1 )
315 #endif
316 #endif /* MBEDTLS_ECP_DP_BP256R1_ENABLED */
317 
318 #if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
319 #if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
320 #undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
321 #define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 384 + 7 ) / 8 ) + 1 )
322 #endif
323 #endif /* MBEDTLS_ECP_DP_BP384R1_ENABLED */
324 
325 #if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
326 #if MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH < ( 2 * ( ( 512 + 7 ) / 8 ) + 1 )
327 #undef MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH
328 #define MBEDTLS_PSA_MAX_EC_PUBKEY_LENGTH ( 2 * ( ( 512 + 7 ) / 8 ) + 1 )
329 #endif
330 #endif /* MBEDTLS_ECP_DP_BP512R1_ENABLED */
331 
332 
333 /* Translations for PK layer */
334 
335 static inline int mbedtls_psa_err_translate_pk( psa_status_t status )
336 {
337  switch( status )
338  {
339  case PSA_SUCCESS:
340  return( 0 );
344  return( MBEDTLS_ERR_PK_ALLOC_FAILED );
347  case PSA_ERROR_BAD_STATE:
349  /* All other failures */
354  default: /* We return the same as for the 'other failures',
355  * but list them separately nonetheless to indicate
356  * which failure conditions we have considered. */
358  }
359 }
360 
361 /* Translations for ECC */
362 
363 /* This function transforms an ECC group identifier from
364  * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-8
365  * into a PSA ECC group identifier. */
366 #if defined(MBEDTLS_ECP_C)
368  uint16_t tls_ecc_grp_reg_id, size_t *bits )
369 {
370  const mbedtls_ecp_curve_info *curve_info =
371  mbedtls_ecp_curve_info_from_tls_id( tls_ecc_grp_reg_id );
372  if( curve_info == NULL )
373  return( 0 );
375  mbedtls_ecc_group_to_psa( curve_info->grp_id, bits ) ) );
376 }
377 #endif /* MBEDTLS_ECP_C */
378 
379 /* This function takes a buffer holding an EC public key
380  * exported through psa_export_public_key(), and converts
381  * it into an ECPoint structure to be put into a ClientKeyExchange
382  * message in an ECDHE exchange.
383  *
384  * Both the present and the foreseeable future format of EC public keys
385  * used by PSA have the ECPoint structure contained in the exported key
386  * as a subbuffer, and the function merely selects this subbuffer instead
387  * of making a copy.
388  */
389 static inline int mbedtls_psa_tls_psa_ec_to_ecpoint( unsigned char *src,
390  size_t srclen,
391  unsigned char **dst,
392  size_t *dstlen )
393 {
394  *dst = src;
395  *dstlen = srclen;
396  return( 0 );
397 }
398 
399 /* This function takes a buffer holding an ECPoint structure
400  * (as contained in a TLS ServerKeyExchange message for ECDHE
401  * exchanges) and converts it into a format that the PSA key
402  * agreement API understands.
403  */
404 static inline int mbedtls_psa_tls_ecpoint_to_psa_ec( unsigned char const *src,
405  size_t srclen,
406  unsigned char *dst,
407  size_t dstlen,
408  size_t *olen )
409 {
410  if( srclen > dstlen )
412 
413  memcpy( dst, src, srclen );
414  *olen = srclen;
415  return( 0 );
416 }
417 
418 #endif /* MBEDTLS_USE_PSA_CRYPTO */
419 
420 #endif /* MBEDTLS_PSA_UTIL_H */
static psa_ecc_family_t mbedtls_ecc_group_to_psa(mbedtls_ecp_group_id grpid, size_t *bits)
Definition: crypto_extra.h:579
mbedtls_operation_t
Definition: cipher.h:212
#define MBEDTLS_OID_EC_GRP_SECP192K1
Definition: oid.h:367
#define PSA_ERROR_COMMUNICATION_FAILURE
mbedtls_cipher_mode_t
Definition: cipher.h:186
static psa_key_type_t mbedtls_psa_translate_cipher_type(mbedtls_cipher_type_t cipher)
Definition: psa_util.h:48
#define PSA_SUCCESS
Definition: crypto_values.h:44
This file provides an API for Elliptic Curves over GF(P) (ECP).
Platform Security Architecture cryptography module.
#define MBEDTLS_OID_EC_GRP_SECP192R1
Definition: oid.h:347
Configuration options (set of defines)
#define PSA_ALG_GCM
#define MBEDTLS_OID_EC_GRP_SECP384R1
Definition: oid.h:359
#define PSA_ALG_MD4
#define PSA_ALG_SHA_256
mbedtls_cipher_type_t
Supported {cipher type, cipher mode} pairs.
Definition: cipher.h:102
Object Identifier (OID) database.
Public Key abstraction layer.
#define PSA_ALG_SHA_512
#define MBEDTLS_OID_SIZE(x)
Definition: asn1.h:121
#define PSA_ERROR_INSUFFICIENT_MEMORY
#define MBEDTLS_ERR_PK_HW_ACCEL_FAILED
Definition: pk.h:71
#define MBEDTLS_OID_EC_GRP_SECP256R1
Definition: oid.h:355
#define PSA_ALG_SHA_224
#define PSA_ALG_RIPEMD160
#define MBEDTLS_OID_EC_GRP_BP256R1
Definition: oid.h:386
static psa_key_usage_t mbedtls_psa_translate_cipher_operation(mbedtls_operation_t op)
Definition: psa_util.h:100
#define MBEDTLS_OID_EC_GRP_SECP224K1
Definition: oid.h:371
#define PSA_ALG_SHA_384
#define PSA_KEY_USAGE_DECRYPT
static int mbedtls_psa_get_ecc_oid_from_id(psa_ecc_family_t curve, size_t bits, char const **oid, size_t *oid_len)
Definition: psa_util.h:160
#define PSA_ALG_CBC_NO_PADDING
#define PSA_ECC_FAMILY_BRAINPOOL_P_R1
static psa_algorithm_t mbedtls_psa_translate_md(mbedtls_md_type_t md_alg)
Definition: psa_util.h:116
static int mbedtls_psa_err_translate_pk(psa_status_t status)
Definition: psa_util.h:335
#define MBEDTLS_OID_EC_GRP_BP512R1
Definition: oid.h:392
#define PSA_KEY_TYPE_ECC_KEY_PAIR(curve)
#define MBEDTLS_OID_EC_GRP_SECP521R1
Definition: oid.h:363
uint32_t psa_algorithm_t
Encoding of a cryptographic algorithm.
Definition: crypto_types.h:96
#define MBEDTLS_ERR_PK_ALLOC_FAILED
Definition: pk.h:55
#define PSA_ECC_FAMILY_SECP_R1
#define MBEDTLS_ERR_PK_BAD_INPUT_DATA
Definition: pk.h:57
#define PSA_ERROR_CORRUPTION_DETECTED
uint32_t psa_key_usage_t
Encoding of permitted usage on a key.
Definition: crypto_types.h:246
#define PSA_KEY_TYPE_AES
uint16_t psa_key_type_t
Encoding of a key type.
Definition: crypto_types.h:64
const mbedtls_ecp_curve_info * mbedtls_ecp_curve_info_from_tls_id(uint16_t tls_id)
This function retrieves curve information from a TLS NamedCurve value.
#define MBEDTLS_OID_EC_GRP_SECP224R1
Definition: oid.h:351
static psa_algorithm_t mbedtls_psa_translate_cipher_mode(mbedtls_cipher_mode_t mode, size_t taglen)
Definition: psa_util.h:81
#define PSA_ERROR_HARDWARE_FAILURE
mbedtls_ecp_group_id grp_id
Definition: ecp.h:149
This file contains the generic message-digest wrapper.
#define PSA_ERROR_NOT_SUPPORTED
Definition: crypto_values.h:60
static int mbedtls_psa_tls_psa_ec_to_ecpoint(unsigned char *src, size_t srclen, unsigned char **dst, size_t *dstlen)
Definition: psa_util.h:389
#define MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE
Definition: pk.h:67
#define PSA_ALG_CCM
static int mbedtls_psa_tls_ecpoint_to_psa_ec(unsigned char const *src, size_t srclen, unsigned char *dst, size_t dstlen, size_t *olen)
Definition: psa_util.h:404
#define PSA_ALG_MD2
#define MBEDTLS_OID_EC_GRP_BP384R1
Definition: oid.h:389
#define PSA_ECC_FAMILY_SECP_K1
#define MBEDTLS_OID_EC_GRP_SECP256K1
Definition: oid.h:375
#define PSA_ALG_SHA_1
#define PSA_ALG_MD5
#define PSA_ERROR_BAD_STATE
#define MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL
Definition: ecp.h:49
uint8_t psa_ecc_family_t
Definition: crypto_types.h:75
#define MBEDTLS_ERR_ECP_RANDOM_FAILED
Definition: ecp.h:53
mbedtls_md_type_t
Supported message digests.
Definition: md.h:56
int32_t psa_status_t
Function return status.
Definition: crypto_types.h:53
#define PSA_KEY_USAGE_ENCRYPT
static psa_key_type_t mbedtls_psa_parse_tls_ecc_group(uint16_t tls_ecc_grp_reg_id, size_t *bits)
Definition: psa_util.h:367
#define PSA_ERROR_INSUFFICIENT_ENTROPY
#define PSA_ALG_AEAD_WITH_TAG_LENGTH(aead_alg, tag_length)