Skip to content

Commit 565e0bf

Browse files
authored
Merge pull request #212 from ARMmbed/psa-integration-utilities_CRYPTO
Mbed TLS integration: Shared code between module-specific integration work
2 parents 30b4641 + 12bd57b commit 565e0bf

File tree

10 files changed

+376
-1
lines changed

10 files changed

+376
-1
lines changed

include/mbedtls/check_config.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,10 @@
670670
#endif
671671
#undef MBEDTLS_THREADING_IMPL
672672

673+
#if defined(MBEDTLS_USE_PSA_CRYPTO) && !defined(MBEDTLS_PSA_CRYPTO_C)
674+
#error "MBEDTLS_USE_PSA_CRYPTO defined, but not all prerequisites"
675+
#endif
676+
673677
#if defined(MBEDTLS_VERSION_FEATURES) && !defined(MBEDTLS_VERSION_C)
674678
#error "MBEDTLS_VERSION_FEATURES defined, but not all prerequisites"
675679
#endif

include/mbedtls/config.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,6 +1616,24 @@
16161616
*/
16171617
//#define MBEDTLS_THREADING_PTHREAD
16181618

1619+
/**
1620+
* \def MBEDTLS_USE_PSA_CRYPTO
1621+
*
1622+
* Make the X.509 and TLS library use PSA for cryptographic operations, see
1623+
* #MBEDTLS_PSA_CRYPTO_C.
1624+
*
1625+
* Note: this option is still in progress, the full X.509 and TLS modules are
1626+
* not covered yet, but parts that are not ported to PSA yet will still work
1627+
* as usual, so enabling this option should not break backwards compatibility.
1628+
*
1629+
* \warning Support for PSA is still an experimental feature.
1630+
* Any public API that depends on this option may change
1631+
* at any time until this warning is removed.
1632+
*
1633+
* Requires: MBEDTLS_PSA_CRYPTO_C.
1634+
*/
1635+
//#define MBEDTLS_USE_PSA_CRYPTO
1636+
16191637
/**
16201638
* \def MBEDTLS_VERSION_FEATURES
16211639
*

include/mbedtls/psa_util.h

Lines changed: 264 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,264 @@
1+
/**
2+
* \file psa_util.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+
#ifndef MBEDTLS_PSA_UTIL_H
29+
#define MBEDTLS_PSA_UTIL_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+
#include "pk.h"
44+
45+
/* Slot allocation */
46+
47+
static inline psa_status_t mbedtls_psa_get_free_key_slot( psa_key_slot_t *key )
48+
{
49+
for( psa_key_slot_t slot = 1; slot <= 32; slot++ )
50+
{
51+
if( psa_get_key_information( slot, NULL, NULL ) == PSA_ERROR_EMPTY_SLOT )
52+
{
53+
*key = slot;
54+
return( PSA_SUCCESS );
55+
}
56+
}
57+
return( PSA_ERROR_INSUFFICIENT_MEMORY );
58+
}
59+
60+
/* Translations for symmetric crypto. */
61+
62+
static inline psa_key_type_t mbedtls_psa_translate_cipher_type(
63+
mbedtls_cipher_type_t cipher )
64+
{
65+
switch( cipher )
66+
{
67+
case MBEDTLS_CIPHER_AES_128_CCM:
68+
case MBEDTLS_CIPHER_AES_192_CCM:
69+
case MBEDTLS_CIPHER_AES_256_CCM:
70+
case MBEDTLS_CIPHER_AES_128_GCM:
71+
case MBEDTLS_CIPHER_AES_192_GCM:
72+
case MBEDTLS_CIPHER_AES_256_GCM:
73+
case MBEDTLS_CIPHER_AES_128_CBC:
74+
case MBEDTLS_CIPHER_AES_192_CBC:
75+
case MBEDTLS_CIPHER_AES_256_CBC:
76+
return( PSA_KEY_TYPE_AES );
77+
78+
/* ARIA not yet supported in PSA. */
79+
/* case MBEDTLS_CIPHER_ARIA_128_CCM:
80+
case MBEDTLS_CIPHER_ARIA_192_CCM:
81+
case MBEDTLS_CIPHER_ARIA_256_CCM:
82+
case MBEDTLS_CIPHER_ARIA_128_GCM:
83+
case MBEDTLS_CIPHER_ARIA_192_GCM:
84+
case MBEDTLS_CIPHER_ARIA_256_GCM:
85+
case MBEDTLS_CIPHER_ARIA_128_CBC:
86+
case MBEDTLS_CIPHER_ARIA_192_CBC:
87+
case MBEDTLS_CIPHER_ARIA_256_CBC:
88+
return( PSA_KEY_TYPE_ARIA ); */
89+
90+
default:
91+
return( 0 );
92+
}
93+
}
94+
95+
static inline psa_algorithm_t mbedtls_psa_translate_cipher_mode(
96+
mbedtls_cipher_mode_t mode, size_t taglen )
97+
{
98+
switch( mode )
99+
{
100+
case MBEDTLS_MODE_GCM:
101+
return( PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, taglen ) );
102+
case MBEDTLS_MODE_CCM:
103+
return( PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, taglen ) );
104+
case MBEDTLS_MODE_CBC:
105+
if( taglen == 0 )
106+
return( PSA_ALG_CBC_NO_PADDING );
107+
/* Intentional fallthrough for taglen != 0 */
108+
default:
109+
return( 0 );
110+
}
111+
}
112+
113+
static inline psa_key_usage_t mbedtls_psa_translate_cipher_operation(
114+
mbedtls_operation_t op )
115+
{
116+
switch( op )
117+
{
118+
case MBEDTLS_ENCRYPT:
119+
return( PSA_KEY_USAGE_ENCRYPT );
120+
case MBEDTLS_DECRYPT:
121+
return( PSA_KEY_USAGE_DECRYPT );
122+
default:
123+
return( 0 );
124+
}
125+
}
126+
127+
/* Translations for hashing. */
128+
129+
static inline psa_algorithm_t mbedtls_psa_translate_md( mbedtls_md_type_t md_alg )
130+
{
131+
switch( md_alg )
132+
{
133+
#if defined(MBEDTLS_MD2_C)
134+
case MBEDTLS_MD_MD2:
135+
return( PSA_ALG_MD2 );
136+
#endif
137+
#if defined(MBEDTLS_MD4_C)
138+
case MBEDTLS_MD_MD4:
139+
return( PSA_ALG_MD4 );
140+
#endif
141+
#if defined(MBEDTLS_MD5_C)
142+
case MBEDTLS_MD_MD5:
143+
return( PSA_ALG_MD5 );
144+
#endif
145+
#if defined(MBEDTLS_SHA1_C)
146+
case MBEDTLS_MD_SHA1:
147+
return( PSA_ALG_SHA_1 );
148+
#endif
149+
#if defined(MBEDTLS_SHA256_C)
150+
case MBEDTLS_MD_SHA224:
151+
return( PSA_ALG_SHA_224 );
152+
case MBEDTLS_MD_SHA256:
153+
return( PSA_ALG_SHA_256 );
154+
#endif
155+
#if defined(MBEDTLS_SHA512_C)
156+
case MBEDTLS_MD_SHA384:
157+
return( PSA_ALG_SHA_384 );
158+
case MBEDTLS_MD_SHA512:
159+
return( PSA_ALG_SHA_512 );
160+
#endif
161+
#if defined(MBEDTLS_RIPEMD160_C)
162+
case MBEDTLS_MD_RIPEMD160:
163+
return( PSA_ALG_RIPEMD160 );
164+
#endif
165+
case MBEDTLS_MD_NONE: /* Intentional fallthrough */
166+
default:
167+
return( 0 );
168+
}
169+
}
170+
171+
/* Translations for ECC. */
172+
173+
static inline psa_ecc_curve_t mbedtls_psa_translate_ecc_group( mbedtls_ecp_group_id grpid )
174+
{
175+
switch( grpid )
176+
{
177+
#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED)
178+
case MBEDTLS_ECP_DP_SECP192R1:
179+
return( PSA_ECC_CURVE_SECP192R1 );
180+
#endif
181+
#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED)
182+
case MBEDTLS_ECP_DP_SECP224R1:
183+
return( PSA_ECC_CURVE_SECP224R1 );
184+
#endif
185+
#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
186+
case MBEDTLS_ECP_DP_SECP256R1:
187+
return( PSA_ECC_CURVE_SECP256R1 );
188+
#endif
189+
#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
190+
case MBEDTLS_ECP_DP_SECP384R1:
191+
return( PSA_ECC_CURVE_SECP384R1 );
192+
#endif
193+
#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
194+
case MBEDTLS_ECP_DP_SECP521R1:
195+
return( PSA_ECC_CURVE_SECP521R1 );
196+
#endif
197+
#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
198+
case MBEDTLS_ECP_DP_BP256R1:
199+
return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
200+
#endif
201+
#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
202+
case MBEDTLS_ECP_DP_BP384R1:
203+
return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
204+
#endif
205+
#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
206+
case MBEDTLS_ECP_DP_BP512R1:
207+
return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
208+
#endif
209+
#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
210+
case MBEDTLS_ECP_DP_CURVE25519:
211+
return( PSA_ECC_CURVE_CURVE25519 );
212+
#endif
213+
#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED)
214+
case MBEDTLS_ECP_DP_SECP192K1:
215+
return( PSA_ECC_CURVE_SECP192K1 );
216+
#endif
217+
#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED)
218+
case MBEDTLS_ECP_DP_SECP224K1:
219+
return( PSA_ECC_CURVE_SECP224K1 );
220+
#endif
221+
#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED)
222+
case MBEDTLS_ECP_DP_SECP256K1:
223+
return( PSA_ECC_CURVE_SECP256K1 );
224+
#endif
225+
#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
226+
case MBEDTLS_ECP_DP_CURVE448:
227+
return( PSA_ECC_CURVE_CURVE448 );
228+
#endif
229+
default:
230+
return( 0 );
231+
}
232+
}
233+
234+
/* Translations for PK layer */
235+
236+
static inline int mbedtls_psa_err_translate_pk( psa_status_t status )
237+
{
238+
switch( status )
239+
{
240+
case PSA_SUCCESS:
241+
return( 0 );
242+
case PSA_ERROR_NOT_SUPPORTED:
243+
return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
244+
case PSA_ERROR_INSUFFICIENT_MEMORY:
245+
return( MBEDTLS_ERR_PK_ALLOC_FAILED );
246+
case PSA_ERROR_INSUFFICIENT_ENTROPY:
247+
return( MBEDTLS_ERR_ECP_RANDOM_FAILED );
248+
case PSA_ERROR_BAD_STATE:
249+
return( MBEDTLS_ERR_PK_BAD_INPUT_DATA );
250+
/* All other failures */
251+
case PSA_ERROR_COMMUNICATION_FAILURE:
252+
case PSA_ERROR_HARDWARE_FAILURE:
253+
case PSA_ERROR_TAMPERING_DETECTED:
254+
return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
255+
default: /* We return the same as for the 'other failures',
256+
* but list them separately nonetheless to indicate
257+
* which failure conditions we have considered. */
258+
return( MBEDTLS_ERR_PK_HW_ACCEL_FAILED );
259+
}
260+
}
261+
262+
#endif /* MBEDTLS_USE_PSA_CRYPTO */
263+
264+
#endif /* MBEDTLS_PSA_UTIL_H */

library/version_features.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,9 @@ static const char *features[] = {
522522
#if defined(MBEDTLS_THREADING_PTHREAD)
523523
"MBEDTLS_THREADING_PTHREAD",
524524
#endif /* MBEDTLS_THREADING_PTHREAD */
525+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
526+
"MBEDTLS_USE_PSA_CRYPTO",
527+
#endif /* MBEDTLS_USE_PSA_CRYPTO */
525528
#if defined(MBEDTLS_VERSION_FEATURES)
526529
"MBEDTLS_VERSION_FEATURES",
527530
#endif /* MBEDTLS_VERSION_FEATURES */

programs/ssl/ssl_client2.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ int main( void )
5959
#include "mbedtls/debug.h"
6060
#include "mbedtls/timing.h"
6161

62+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
63+
#include "psa/crypto.h"
64+
#endif
65+
6266
#include <stdio.h>
6367
#include <stdlib.h>
6468
#include <string.h>
@@ -555,6 +559,9 @@ int main( int argc, char *argv[] )
555559
#endif
556560
char *p, *q;
557561
const int *list;
562+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
563+
psa_status_t status;
564+
#endif
558565

559566
/*
560567
* Make sure memory references are valid.
@@ -573,6 +580,17 @@ int main( int argc, char *argv[] )
573580
memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
574581
#endif
575582

583+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
584+
status = psa_crypto_init();
585+
if( status != PSA_SUCCESS )
586+
{
587+
mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n",
588+
(int) status );
589+
ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
590+
goto exit;
591+
}
592+
#endif
593+
576594
if( argc == 0 )
577595
{
578596
usage:

programs/ssl/ssl_server2.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ int main( void )
6060
#include "mbedtls/debug.h"
6161
#include "mbedtls/timing.h"
6262

63+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
64+
#include "psa/crypto.h"
65+
#endif
66+
6367
#include <stdio.h>
6468
#include <stdlib.h>
6569
#include <string.h>
@@ -1238,6 +1242,9 @@ int main( int argc, char *argv[] )
12381242
int i;
12391243
char *p, *q;
12401244
const int *list;
1245+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1246+
psa_status_t status;
1247+
#endif
12411248

12421249
#if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
12431250
mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
@@ -1277,6 +1284,17 @@ int main( int argc, char *argv[] )
12771284
mbedtls_ssl_cookie_init( &cookie_ctx );
12781285
#endif
12791286

1287+
#if defined(MBEDTLS_USE_PSA_CRYPTO)
1288+
status = psa_crypto_init();
1289+
if( status != PSA_SUCCESS )
1290+
{
1291+
mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n",
1292+
(int) status );
1293+
ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1294+
goto exit;
1295+
}
1296+
#endif
1297+
12801298
#if !defined(_WIN32)
12811299
/* Abort cleanly on SIGTERM and SIGINT */
12821300
signal( SIGTERM, term_handler );

0 commit comments

Comments
 (0)