Skip to content

Commit 8c07b96

Browse files
brosahayrosahay-silabs
authored andcommitted
Refactor SiWx917 for mbedtls usage
1 parent 4d5e2ee commit 8c07b96

File tree

5 files changed

+389
-187
lines changed

5 files changed

+389
-187
lines changed

src/platform/silabs/PlatformManagerImpl.cpp

+7-8
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace DeviceLayer {
4646

4747
PlatformManagerImpl PlatformManagerImpl::sInstance;
4848

49-
#if defined(TINYCRYPT_PRIMITIVES)
49+
#if defined(SL_MBEDTLS_USE_TINYCRYPT)
5050
sys_mutex_t PlatformManagerImpl::rngMutexHandle = NULL;
5151

5252
int PlatformManagerImpl::uECC_RNG_Function(uint8_t * dest, unsigned int size)
@@ -75,8 +75,7 @@ static int app_entropy_source(void * data, unsigned char * output, size_t len, s
7575

7676
return 0;
7777
}
78-
#endif // !SLI_SI91X_MCU_INTERFACE
79-
#endif // TINYCRYPT_PRIMITIVES
78+
#endif // SL_MBEDTLS_USE_TINYCRYPT
8079

8180
CHIP_ERROR PlatformManagerImpl::_InitChipStack(void)
8281
{
@@ -93,14 +92,14 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack(void)
9392

9493
ReturnErrorOnFailure(System::Clock::InitClock_RealTime());
9594

96-
#if defined(TINYCRYPT_PRIMITIVES)
97-
#if !(SLI_SI91X_MCU_INTERFACE)
98-
ReturnErrorOnFailure(chip::Crypto::add_entropy_source(app_entropy_source, NULL, 16 /*Threshold value*/));
99-
#endif // !SLI_SI91X_MCU_INTERFACE
95+
#if defined(SL_MBEDTLS_USE_TINYCRYPT)
96+
// 16 : Threshold value
97+
ReturnErrorOnFailure(chip::Crypto::add_entropy_source(app_entropy_source, NULL, 16));
98+
10099
/* Set RNG function for tinycrypt operations. */
101100
VerifyOrExit(sys_mutex_new(&rngMutexHandle) == ERR_OK, err = CHIP_ERROR_NO_MEMORY);
102101
uECC_set_rng(PlatformManagerImpl::uECC_RNG_Function);
103-
#endif // TINYCRYPT_PRIMITIVES
102+
#endif // SL_MBEDTLS_USE_TINYCRYPT
104103

105104
// Call _InitChipStack() on the generic implementation base class
106105
// to finish the initialization process.

src/platform/silabs/SiWx917/BUILD.gn

+11-1
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ import("${chip_root}/src/platform/device.gni")
1818

1919
import("${chip_root}/build/chip/buildconfig_header.gni")
2020
import("${chip_root}/src/crypto/crypto.gni")
21+
import("${chip_root}/src/platform/silabs/wifi_args.gni")
22+
import("${chip_root}/third_party/silabs/SiWx917_sdk.gni")
2123
import("${chip_root}/third_party/silabs/silabs_board.gni")
2224

2325
silabs_platform_dir = "${chip_root}/src/platform/silabs"
2426

2527
assert(chip_device_platform == "SiWx917")
28+
2629
if (chip_crypto == "platform") {
2730
import("//build_overrides/mbedtls.gni")
2831
}
@@ -81,7 +84,14 @@ static_library("SiWx917") {
8184

8285
# Add platform crypto implementation
8386
if (chip_crypto == "platform") {
84-
sources += [ "CHIPCryptoPALTinyCrypt.cpp" ]
87+
if (sl_si91x_crypto_flavor == "tinycrypt") {
88+
sources += [ "CHIPCryptoPALTinyCrypt.cpp" ]
89+
}
90+
91+
if (sl_si91x_crypto_flavor == "psa") {
92+
sources += [ "${silabs_platform_dir}/efr32/CHIPCryptoPALPsaEfr32.cpp" ]
93+
}
94+
8595
public_deps += [
8696
"${chip_root}/src/crypto",
8797
"${mbedtls_root}:mbedtls",
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,87 @@
11
/*
2-
* Copyright (c) 2021, The OpenThread Authors.
3-
* All rights reserved.
42
*
5-
* Redistribution and use in source and binary forms, with or without
6-
* modification, are permitted provided that the following conditions are met:
7-
* 1. Redistributions of source code must retain the above copyright
8-
* notice, this list of conditions and the following disclaimer.
9-
* 2. Redistributions in binary form must reproduce the above copyright
10-
* notice, this list of conditions and the following disclaimer in the
11-
* documentation and/or other materials provided with the distribution.
12-
* 3. Neither the name of the copyright holder nor the
13-
* names of its contributors may be used to endorse or promote products
14-
* derived from this software without specific prior written permission.
3+
* Copyright (c) 2024 Project CHIP Authors
4+
* All rights reserved.
155
*
16-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17-
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19-
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20-
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21-
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22-
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23-
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24-
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25-
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26-
* POSSIBILITY OF SUCH DAMAGE.
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.
2717
*/
2818

2919
#pragma once
3020

31-
#include <limits.h>
32-
#include <stdio.h>
33-
#include <stdlib.h>
34-
/**
35-
* Enable FreeRTOS threading support
36-
*/
37-
#define MBEDTLS_FREERTOS
21+
#define MBEDTLS_PLATFORM_C
22+
#define MBEDTLS_PLATFORM_MEMORY
23+
#define MBEDTLS_VERSION_C
24+
#define MBEDTLS_VERSION_FEATURES
3825

39-
/**
40-
* Enable H Crypto and Entropy modules
41-
*/
42-
#define MBEDTLS_AES_C
43-
#define MBEDTLS_ECP_C
44-
#define MBEDTLS_ECDH_C
45-
#define MBEDTLS_ENTROPY_C
46-
#define MBEDTLS_SHA256_C
47-
#define MBEDTLS_CIPHER_MODE_CTR
48-
#define MBEDTLS_TRNG_C
26+
#define MBEDTLS_HKDF_C
27+
#define MBEDTLS_PKCS5_C
4928

50-
#define MBEDTLS_NO_PLATFORM_ENTROPY
51-
#define MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
29+
// DIC Specific Configurations
30+
#ifdef DIC_ENABLE
5231

53-
#if defined(MBEDTLS_ECP_ALT) && !defined(MBEDTLS_ECP_RESTARTABLE)
54-
typedef void mbedtls_ecp_restart_ctx;
55-
#endif
32+
#define MBEDTLS_MPI_MAX_SIZE 512
5633

57-
#ifdef DIC_ENABLE
58-
#define MBEDTLS_PKCS1_V15
59-
#define MBEDTLS_RSA_NO_CRT
6034
#define MBEDTLS_CIPHER_MODE_CBC
35+
#define MBEDTLS_CIPHER_MODE_WITH_PADDING
6136
#define MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED
37+
#define MBEDTLS_PKCS1_V15
6238
#define MBEDTLS_RSA_C
39+
#define MBEDTLS_RSA_NO_CRT
6340
#define MBEDTLS_SHA1_C
6441
#define MBEDTLS_SSL_IN_CONTENT_LEN 5120
42+
#define MBEDTLS_SSL_MAX_CONTENT_LEN 5120
6543
#define MBEDTLS_SSL_OUT_CONTENT_LEN 1560
66-
#define MBEDTLS_DEBUG_C
67-
#define MBEDTLS_CIPHER_MODE_WITH_PADDING
68-
#endif
44+
45+
#endif // DIC_ENABLE
46+
47+
#ifdef SL_MBEDTLS_USE_TINYCRYPT
48+
#define TINYCRYPT_PRIMITIVES
49+
#define OPTIMIZE_TINYCRYPT_ASM
50+
51+
#define MBEDTLS_FREERTOS
52+
53+
#if (!(DIC_ENABLE) || !defined(DIC_ENABLE))
54+
/**< Maximum windows size used. */
55+
#define MBEDTLS_MPI_WINDOW_SIZE 1
56+
57+
/**< Maximum number of bytes for usable MPIs. */
58+
#define MBEDTLS_MPI_MAX_SIZE 32
59+
/**< Maxium fragment length in bytes */
60+
#define MBEDTLS_SSL_MAX_CONTENT_LEN 768
61+
#endif // !(DIC_ENABLE)
62+
63+
#define MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
6964
#define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf
7065

7166
#define MBEDTLS_AES_ROM_TABLES
72-
#define MBEDTLS_ASN1_PARSE_C
73-
#define MBEDTLS_ASN1_WRITE_C
74-
#define MBEDTLS_BASE64_C
75-
#define MBEDTLS_BIGNUM_C
7667
#define MBEDTLS_CCM_C
77-
#define MBEDTLS_CIPHER_C
7868
#define MBEDTLS_CIPHER_MODE_CBC
7969
#define MBEDTLS_CIPHER_MODE_CFB
80-
#define MBEDTLS_CMAC_C
81-
#define MBEDTLS_CTR_DRBG_C
70+
#define MBEDTLS_CIPHER_MODE_CTR
71+
#define MBEDTLS_ECDH_C
8272
#define MBEDTLS_ECDH_LEGACY_CONTEXT
83-
#define MBEDTLS_ECDSA_C
8473
#define MBEDTLS_ECDSA_DETERMINISTIC
8574
#define MBEDTLS_ECJPAKE_C
86-
#define MBEDTLS_ECP_DP_SECP256R1_ENABLED
75+
#define MBEDTLS_ECP_FIXED_POINT_OPTIM 0
8776
#define MBEDTLS_ECP_NIST_OPTIM
88-
#define MBEDTLS_ENTROPY_FORCE_SHA256
89-
// TODO: 3R
77+
#define MBEDTLS_ECP_WINDOW_SIZE 2
9078
// #define MBEDTLS_ENTROPY_HARDWARE_ALT
91-
9279
#define MBEDTLS_ERROR_STRERROR_DUMMY
9380
#define MBEDTLS_HAVE_ASM
94-
#define MBEDTLS_HKDF_C
9581
#define MBEDTLS_HMAC_DRBG_C
9682
#define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED
9783
#define MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED
9884
#define MBEDTLS_KEY_EXCHANGE_PSK_ENABLED
99-
#define MBEDTLS_MD_C
100-
#define MBEDTLS_OID_C
101-
#define MBEDTLS_PEM_PARSE_C
102-
#define MBEDTLS_PEM_WRITE_C
103-
#define MBEDTLS_PK_C
104-
#define MBEDTLS_PK_PARSE_C
105-
#define MBEDTLS_PK_WRITE_C
106-
#define MBEDTLS_PKCS5_C
107-
#define MBEDTLS_PLATFORM_C
108-
#define MBEDTLS_PLATFORM_MEMORY
10985
#define MBEDTLS_SHA256_SMALLER
11086
#define MBEDTLS_SHA512_C
11187
#define MBEDTLS_SSL_CLI_C
@@ -115,38 +91,21 @@ typedef void mbedtls_ecp_restart_ctx;
11591
#define MBEDTLS_SSL_EXPORT_KEYS
11692
#define MBEDTLS_SSL_KEEP_PEER_CERTIFICATE
11793
#define MBEDTLS_SSL_MAX_FRAGMENT_LENGTH
118-
#define MBEDTLS_SSL_PROTO_TLS1_2
11994
#define MBEDTLS_SSL_PROTO_DTLS
95+
#define MBEDTLS_SSL_PROTO_TLS1_2
12096
#define MBEDTLS_SSL_SRV_C
12197
#define MBEDTLS_SSL_TLS_C
122-
#define MBEDTLS_X509_CREATE_C
123-
#define MBEDTLS_X509_CSR_WRITE_C
124-
#define MBEDTLS_X509_CRL_PARSE_C
125-
#define MBEDTLS_X509_CRT_PARSE_C
126-
#define MBEDTLS_X509_CSR_PARSE_C
127-
#define MBEDTLS_X509_USE_C
128-
129-
#define MBEDTLS_MPI_WINDOW_SIZE 1 /**< Maximum windows size used. */
130-
#ifdef DIC_ENABLE
131-
#define MBEDTLS_MPI_MAX_SIZE 512 /**< Maximum number of bytes for usable MPIs. */
132-
#else
133-
#define MBEDTLS_MPI_MAX_SIZE 32 /**< Maximum number of bytes for usable MPIs. */
134-
#endif
135-
#define MBEDTLS_ECP_MAX_BITS 256 /**< Maximum bit size of groups */
136-
#define MBEDTLS_ECP_WINDOW_SIZE 2 /**< Maximum window size used */
137-
#define MBEDTLS_ECP_FIXED_POINT_OPTIM 0 /**< Enable fixed-point speed-up */
138-
#define MBEDTLS_ENTROPY_MAX_SOURCES 2 /**< Maximum number of sources supported */
139-
140-
#if OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE
141-
#define MBEDTLS_SSL_MAX_CONTENT_LEN 900 /**< Maxium fragment length in bytes */
142-
#else
143-
#ifdef DIC_ENABLE
144-
#define MBEDTLS_SSL_MAX_CONTENT_LEN 5120 /**< Maxium fragment length in bytes */
145-
#else
146-
#define MBEDTLS_SSL_MAX_CONTENT_LEN 768 /**< Maxium fragment length in bytes */
147-
#endif // DIC
98+
#define MBEDTLS_TRNG_C
99+
100+
#if defined(MBEDTLS_ECP_ALT) && !defined(MBEDTLS_ECP_RESTARTABLE)
101+
typedef void mbedtls_ecp_restart_ctx;
148102
#endif
149103

150-
#define MBEDTLS_SSL_CIPHERSUITES MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8
104+
#endif // SL_MBEDTLS_USE_TINYCRYPT
105+
106+
// SLC GENERATED
107+
#include "sl_mbedtls_config.h"
151108

109+
#if SL_MBEDTLS_USE_TINYCRYPT
152110
#include "mbedtls/check_config.h"
111+
#endif // SL_MBEDTLS_USE_TINYCRYPT
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
* All rights reserved.
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+
#pragma once
20+
21+
// DIC Specific Configurations
22+
#ifdef DIC_ENABLE
23+
#define MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF
24+
25+
#define PSA_WANT_ALG_CBC_NO_PADDING
26+
#define PSA_WANT_ALG_RSA_PKCS1V15_SIGN
27+
#define PSA_WANT_ALG_SOME_PAKE
28+
#define PSA_WANT_KEY_TYPE_RSA_PUBLIC_KEY
29+
#endif // DIC_ENABLE
30+
31+
// SLC GENERATED
32+
#include "psa_crypto_config.h"

0 commit comments

Comments
 (0)