blob: 03c55e1664bbef3cb601fa654cc459e66092be8d [file] [log] [blame]
Hanno Becker87837b22018-11-08 13:32:02 +00001/**
2 * \file psa_compat.h
3 *
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/*
10 * Copyright (C) 2006-2018, ARM Limited, All Rights Reserved
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 * This file is part of mbed TLS (https://tls.mbed.org)
26 */
27
28#if !defined(MBEDTLS_PSA_COMPAT_H)
29#define MBEDTLS_PSA_COMPAT_H
30
31#if !defined(MBEDTLS_CONFIG_FILE)
32#include "config.h"
33#else
34#include MBEDTLS_CONFIG_FILE
35#endif
36
37#if defined(MBEDTLS_USE_PSA_CRYPTO)
38
39#include "psa/crypto.h"
40
41#include "ecp.h"
42#include "md.h"
43
44static psa_status_t mbedtls_psa_get_free_key_slot( psa_key_slot_t *key )
45{
46 for( psa_key_slot_t slot = 1; slot <= 32; slot++ )
47 {
48 if( psa_get_key_information( slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT )
49 {
50 *key = slot;
51 return( PSA_SUCCESS );
52 }
53 }
54 return( PSA_ERROR_INSUFFICIENT_MEMORY );
55}
56
57static psa_algorithm_t mbedtls_psa_translate_md( mbedtls_md_type_t md_alg )
58{
59 switch( md_alg )
60 {
61#if defined(MBEDTLS_MD2_C)
62 case MBEDTLS_MD_MD2:
63 return( PSA_ALG_MD2 );
64#endif
65#if defined(MBEDTLS_MD4_C)
66 case MBEDTLS_MD_MD4:
67 return( PSA_ALG_MD4 );
68#endif
69#if defined(MBEDTLS_MD5_C)
70 case MBEDTLS_MD_MD5:
71 return( PSA_ALG_MD5 );
72#endif
73#if defined(MBEDTLS_SHA1_C)
74 case MBEDTLS_MD_SHA1:
75 return( PSA_ALG_SHA_1 );
76#endif
77#if defined(MBEDTLS_SHA256_C)
78 case MBEDTLS_MD_SHA224:
79 return( PSA_ALG_SHA_224 );
80 case MBEDTLS_MD_SHA256:
81 return( PSA_ALG_SHA_256 );
82#endif
83#if defined(MBEDTLS_SHA512_C)
84 case MBEDTLS_MD_SHA384:
85 return( PSA_ALG_SHA_384 );
86 case MBEDTLS_MD_SHA512:
87 return( PSA_ALG_SHA_512 );
88#endif
89#if defined(MBEDTLS_RIPEMD160_C)
90 case MBEDTLS_MD_RIPEMD160:
91 return( PSA_ALG_RIPEMD160 );
92#endif
93 case MBEDTLS_MD_NONE: /* Intentional fallthrough */
94 default:
95 return( 0 );
96 }
97}
98
99static psa_ecc_curve_t mbedtls_psa_translate_ecc_group( mbedtls_ecp_group_id grpid )
100{
101 switch( grpid )
102 {
103#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
104 case MBEDTLS_ECP_DP_SECP192R1:
105 return( PSA_ECC_CURVE_SECP192R1 );
106#endif
107#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
108 case MBEDTLS_ECP_DP_SECP224R1:
109 return( PSA_ECC_CURVE_SECP224R1 );
110#endif
111#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
112 case MBEDTLS_ECP_DP_SECP256R1:
113 return( PSA_ECC_CURVE_SECP256R1 );
114#endif
115#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
116 case MBEDTLS_ECP_DP_SECP384R1:
117 return( PSA_ECC_CURVE_SECP384R1 );
118#endif
119#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
120 case MBEDTLS_ECP_DP_SECP521R1:
121 return( PSA_ECC_CURVE_SECP521R1 );
122#endif
123#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
124 case MBEDTLS_ECP_DP_BP256R1:
125 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
126#endif
127#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
128 case MBEDTLS_ECP_DP_BP384R1:
129 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
130#endif
131#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
132 case MBEDTLS_ECP_DP_BP512R1:
133 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
134#endif
135#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
136 case MBEDTLS_ECP_DP_CURVE25519:
137 return( PSA_ECC_CURVE_CURVE25519 );
138#endif
139#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
140 case MBEDTLS_ECP_DP_SECP192K1:
141 return( PSA_ECC_CURVE_SECP192K1 );
142#endif
143#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
144 case MBEDTLS_ECP_DP_SECP224K1:
145 return( PSA_ECC_CURVE_SECP224K1 );
146#endif
147#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
148 case MBEDTLS_ECP_DP_SECP256K1:
149 return( PSA_ECC_CURVE_SECP256K1 );
150#endif
151#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
152 case MBEDTLS_ECP_DP_CURVE448:
153 return( PSA_ECC_CURVE_CURVE448 );
154#endif
155 default:
156 return( 0 );
157 }
158}
159
160#endif /* MBEDTLS_USE_PSA_CRYPTO */
161
162#endif /* MBEDTLS_PSA_COMPAT_H */