Skip to content

Commit de2dd02

Browse files
committed
mbedtls: Initialize PSA Crypto if available
Mbed TLS uses PSA Crypto for cryptographic operations when available, but PSA Crypto needs to be initialized first. To do this, we set `MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT` to enable Mbed OS's override of `mbedtls_platform_setup()` and implement the latter's required `crypto_platform_setup()`.
1 parent fbca8e9 commit de2dd02

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

connectivity/mbedtls/platform/inc/platform_mbed.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050

5151
#define MBEDTLS_ENTROPY_HARDWARE_ALT
5252

53+
#define MBEDTLS_PLATFORM_SETUP_TEARDOWN_ALT
54+
5355
#endif // defined(FEATURE_EXPERIMENTAL_API) && defined(FEATURE_PSA)
5456

5557
#if DEVICE_TRNG

platform/FEATURE_EXPERIMENTAL_API/FEATURE_PSA/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ target_include_directories(mbed-psa
1717

1818
target_sources(mbed-psa
1919
INTERFACE
20+
src/mbedtls_crypto_setup.c
2021
src/psa_hrng.c
2122
)
2223

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2021 Arm Limited
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#ifndef __CRYPTO_PLATFORM_H_
20+
#define __CRYPTO_PLATFORM_H_
21+
22+
// Unused platform-defined crypto context
23+
// required by mbedtls_platform_context
24+
typedef struct {
25+
char unused;
26+
} crypto_platform_ctx;
27+
28+
#endif // __CRYPTO_PLATFORM_H_
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2021 Arm Limited
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#include "crypto_device_platform.h"
20+
#include "psa/crypto.h"
21+
22+
// Required by mbedtls_platform_setup()
23+
24+
int crypto_platform_setup(crypto_platform_ctx *unused)
25+
{
26+
psa_status_t status = psa_crypto_init();
27+
return (int) status;
28+
}
29+
30+
void crypto_platform_terminate(crypto_platform_ctx *unused)
31+
{
32+
// The PSA Crypto API does not provide a deinit function.
33+
// By specification, psa_crypto_init() can be called any
34+
// number of times.
35+
}

0 commit comments

Comments
 (0)