Skip to content

Commit 6729670

Browse files
authored
Merge pull request #454 from tropicsquare/ETR01SDK-556-Add-WolfCrypt-support-to-STM32-tests
ETR01SDK-556: Add wolfCrypt support to STM32 tests
2 parents feace33 + 459f20e commit 6729670

File tree

9 files changed

+190
-13
lines changed

9 files changed

+190
-13
lines changed

tests/functional/stm32/download_deps.sh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,19 @@ fi
5353

5454
tar -xjf "$SCRIPT_DIR/_deps/mbedtls.tar.bz2" -C "$SCRIPT_DIR/_deps"
5555
rm "$SCRIPT_DIR/_deps/mbedtls.tar.bz2"
56-
mv "$SCRIPT_DIR/_deps/mbedtls-4.0.0" "$SCRIPT_DIR/_deps/mbedtls_v4"
56+
mv "$SCRIPT_DIR/_deps/mbedtls-4.0.0" "$SCRIPT_DIR/_deps/mbedtls_v4"
57+
58+
echo "Downloading WolfSSL..."
59+
curl -L -o "$SCRIPT_DIR/_deps/wolfssl.zip" "https://github.com/wolfSSL/wolfssl/archive/refs/tags/v5.8.4-stable.zip"
60+
61+
echo "Verifying wolfssl.zip checksum..."
62+
EXPECTED_WOLFSSL="9f52b92b2937acdbb03f2a731160d70f23f74a375f651de057214783c266fbeb"
63+
ACTUAL_WOLFSSL=$(sha256sum "$SCRIPT_DIR/_deps/wolfssl.zip" | awk '{print $1}')
64+
if [ "$EXPECTED_WOLFSSL" != "$ACTUAL_WOLFSSL" ]; then
65+
echo "Checksum mismatch for wolfssl.zip: expected $EXPECTED_WOLFSSL, got $ACTUAL_WOLFSSL" >&2
66+
exit 1
67+
fi
68+
69+
unzip "$SCRIPT_DIR/_deps/wolfssl.zip" -d "$SCRIPT_DIR/_deps"
70+
mv "$SCRIPT_DIR/_deps/wolfssl-5.8.4-stable" "$SCRIPT_DIR/_deps/wolfssl"
71+
rm "$SCRIPT_DIR/_deps/wolfssl.zip"

tests/functional/stm32/nucleo_f439zi/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ endif()
9090
# Add path to libtropic's repository root folder
9191
add_subdirectory(${PATH_FN_TESTS} "libtropic_functional_tests")
9292

93+
# Additional configuration for WolfCrypt.
94+
if(LT_CAL STREQUAL "wolfcrypt")
95+
target_compile_definitions(wolfssl PUBLIC WOLFSSL_USER_SETTINGS)
96+
# Use BUILD_INTERFACE to strictly limit this path to the build phase
97+
target_include_directories(wolfssl PUBLIC
98+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/Src/wolfcrypt>
99+
)
100+
endif()
101+
93102
###########################################################################
94103
# #
95104
# Sources #
@@ -131,6 +140,11 @@ if (LT_CAL STREQUAL "mbedtls_v4")
131140
# MbedTLS platform-specific implementations
132141
${CMAKE_CURRENT_SOURCE_DIR}/Src/mbedtls_v4/mbedtls_platform.c
133142
)
143+
elseif (LT_CAL STREQUAL "wolfcrypt")
144+
set(SOURCES ${SOURCES}
145+
# WolfCrypt platform-specific implementations
146+
${CMAKE_CURRENT_SOURCE_DIR}/Src/wolfcrypt/wolfcrypt_platform.c
147+
)
134148
endif()
135149

136150
# Include path for directories containing header files

tests/functional/stm32/nucleo_f439zi/Src/main.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,16 @@
2424

2525
#if LT_USE_TREZOR_CRYPTO
2626
#include "libtropic_trezor_crypto.h"
27+
#define CRYPTO_CTX_TYPE lt_ctx_trezor_crypto_t
2728
#elif LT_USE_MBEDTLS_V4
2829
#include "libtropic_mbedtls_v4.h"
2930
#include "psa/crypto.h"
31+
#define CRYPTO_CTX_TYPE lt_ctx_mbedtls_v4_t
32+
#elif LT_USE_WOLFCRYPT
33+
#include "libtropic_wolfcrypt.h"
34+
#include "wolfssl/wolfcrypt/error-crypt.h"
35+
#include "wolfssl/wolfcrypt/wc_port.h"
36+
#define CRYPTO_CTX_TYPE lt_ctx_wolfcrypt_t
3037
#endif
3138

3239
/** @addtogroup STM32F4xx_HAL_Examples
@@ -162,6 +169,12 @@ int main(void)
162169
LT_LOG_ERROR("PSA Crypto initialization failed, status=%d (psa_status_t)", status);
163170
return -1;
164171
}
172+
#elif LT_USE_WOLFCRYPT
173+
int ret = wolfCrypt_Init();
174+
if (ret != 0) {
175+
LT_LOG_ERROR("WolfCrypt initialization failed, ret=%d (%s)", ret, wc_GetErrorString(ret));
176+
return ret;
177+
}
165178
#endif
166179

167180
/* Libtropic handle initialization */
@@ -187,12 +200,7 @@ int main(void)
187200
lt_handle.l2.device = &device;
188201

189202
/* Crypto abstraction layer (CAL) context (selectable). */
190-
#if LT_USE_TREZOR_CRYPTO
191-
lt_ctx_trezor_crypto_t
192-
#elif LT_USE_MBEDTLS_V4
193-
lt_ctx_mbedtls_v4_t
194-
#endif
195-
crypto_ctx;
203+
CRYPTO_CTX_TYPE crypto_ctx;
196204
lt_handle.l3.crypto_ctx = &crypto_ctx;
197205

198206
/* Test code (correct test function is selected automatically per binary)
@@ -203,6 +211,12 @@ int main(void)
203211
/* Cryptographic function provider deinitialization. */
204212
#if LT_USE_MBEDTLS_V4
205213
mbedtls_psa_crypto_free();
214+
#elif LT_USE_WOLFCRYPT
215+
ret = wolfCrypt_Cleanup();
216+
if (ret != 0) {
217+
LT_LOG_ERROR("WolfCrypt cleanup failed, ret=%d (%s)", ret, wc_GetErrorString(ret));
218+
return ret;
219+
}
206220
#endif
207221

208222
/* Inform the test runner that the test finished */
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef USER_SETTINGS_H
2+
#define USER_SETTINGS_H
3+
4+
#define WOLFCRYPT_ONLY // Build only wolfCrypt library.
5+
#define NO_OLD_RNGNAME // Resolves collision between STM32 HAL 'RNG' and WolfSSL 'RNG'.
6+
// #define USE_FAST_MATH
7+
8+
// We will provide custom implementation for seed generation.
9+
extern int wolfcrypt_custom_seed_gen(unsigned char *output, unsigned int sz);
10+
#define CUSTOM_RAND_GENERATE_SEED wolfcrypt_custom_seed_gen
11+
12+
#define WOLFSSL_SMALL_STACK // Offload stack usage to heap where possible.
13+
#define WOLFSSL_MALLOC_CHECK // Optional: Safety check for malloc failures.
14+
15+
#define NO_FILESYSTEM // Prevents filesystem errors on bare metal.
16+
#undef WOLFSSL_SYS_CA_CERTS // Force disable system CA certs (fixes dirent.h / filesystem errors).
17+
#define NO_WRITEV // IO vector write support usually missing.
18+
#define NO_WRITE_TEMP_FILES
19+
#define NO_DEV_RANDOM // We use STM32's RNG, not /dev/random.
20+
#define NO_MAIN_DRIVER
21+
22+
#define WOLFSSL_USER_IO // Disable the default BSD socket callbacks.
23+
24+
#define SINGLE_THREADED // No threads.
25+
26+
#endif /* USER_SETTINGS_H */
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <stdint.h>
2+
#include <string.h>
3+
#include <wolfssl/wolfcrypt/error-crypt.h>
4+
5+
#include "main.h"
6+
#include "stm32f4xx_hal.h"
7+
8+
int wolfcrypt_custom_seed_gen(unsigned char *output, unsigned int sz)
9+
{
10+
HAL_StatusTypeDef hal_status = HAL_OK;
11+
uint32_t random_data;
12+
size_t bytes_left = sz;
13+
14+
while (bytes_left) {
15+
hal_status = HAL_RNG_GenerateRandomNumber(&RNGHandle, &random_data);
16+
if (hal_status != HAL_OK) {
17+
return RNG_FAILURE_E;
18+
}
19+
20+
size_t cpy_cnt = bytes_left < sizeof(random_data) ? bytes_left : sizeof(random_data);
21+
memcpy(output, &random_data, cpy_cnt);
22+
bytes_left -= cpy_cnt;
23+
output += cpy_cnt;
24+
}
25+
26+
return 0;
27+
}

tests/functional/stm32/nucleo_l432kc/CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,15 @@ endif()
9494
# Add path to libtropic's repository root folder
9595
add_subdirectory(${PATH_FN_TESTS} "libtropic_functional_tests")
9696

97+
# Additional configuration for WolfCrypt.
98+
if(LT_CAL STREQUAL "wolfcrypt")
99+
target_compile_definitions(wolfssl PUBLIC WOLFSSL_USER_SETTINGS)
100+
# Use BUILD_INTERFACE to strictly limit this path to the build phase
101+
target_include_directories(wolfssl PUBLIC
102+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/Src/wolfcrypt>
103+
)
104+
endif()
105+
97106
###########################################################################
98107
# #
99108
# Sources #
@@ -136,6 +145,11 @@ if (LT_CAL STREQUAL "mbedtls_v4")
136145
# MbedTLS platform-specific implementations
137146
${CMAKE_CURRENT_SOURCE_DIR}/Src/mbedtls_v4/mbedtls_platform.c
138147
)
148+
elseif (LT_CAL STREQUAL "wolfcrypt")
149+
set(SOURCES ${SOURCES}
150+
# WolfCrypt platform-specific implementations
151+
${CMAKE_CURRENT_SOURCE_DIR}/Src/wolfcrypt/wolfcrypt_platform.c
152+
)
139153
endif()
140154

141155
# Include path for directories containing header files

tests/functional/stm32/nucleo_l432kc/Src/main.c

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@
2323

2424
#if LT_USE_TREZOR_CRYPTO
2525
#include "libtropic_trezor_crypto.h"
26+
#define CRYPTO_CTX_TYPE lt_ctx_trezor_crypto_t
2627
#elif LT_USE_MBEDTLS_V4
2728
#include "libtropic_mbedtls_v4.h"
2829
#include "psa/crypto.h"
30+
#define CRYPTO_CTX_TYPE lt_ctx_mbedtls_v4_t
31+
#elif LT_USE_WOLFCRYPT
32+
#include "libtropic_wolfcrypt.h"
33+
#include "wolfssl/wolfcrypt/error-crypt.h"
34+
#include "wolfssl/wolfcrypt/wc_port.h"
35+
#define CRYPTO_CTX_TYPE lt_ctx_wolfcrypt_t
2936
#endif
3037

3138
/** @addtogroup STM32L4xx_HAL_Examples
@@ -198,6 +205,12 @@ int main(void)
198205
LT_LOG_ERROR("PSA Crypto initialization failed, status=%d (psa_status_t)", status);
199206
return -1;
200207
}
208+
#elif LT_USE_WOLFCRYPT
209+
int ret = wolfCrypt_Init();
210+
if (ret != 0) {
211+
LT_LOG_ERROR("WolfCrypt initialization failed, ret=%d (%s)", ret, wc_GetErrorString(ret));
212+
return ret;
213+
}
201214
#endif
202215

203216
/* Libtropic handle initialization */
@@ -215,12 +228,7 @@ int main(void)
215228
lt_handle.l2.device = &device;
216229

217230
/* Crypto abstraction layer (CAL) context (selectable). */
218-
#if LT_USE_TREZOR_CRYPTO
219-
lt_ctx_trezor_crypto_t
220-
#elif LT_USE_MBEDTLS_V4
221-
lt_ctx_mbedtls_v4_t
222-
#endif
223-
crypto_ctx;
231+
CRYPTO_CTX_TYPE crypto_ctx;
224232
lt_handle.l3.crypto_ctx = &crypto_ctx;
225233

226234
/* Test code (correct test function is selected automatically per binary)
@@ -231,6 +239,12 @@ int main(void)
231239
/* Cryptographic function provider deinitialization. */
232240
#if LT_USE_MBEDTLS_V4
233241
mbedtls_psa_crypto_free();
242+
#elif LT_USE_WOLFCRYPT
243+
ret = wolfCrypt_Cleanup();
244+
if (ret != 0) {
245+
LT_LOG_ERROR("WolfCrypt cleanup failed, ret=%d (%s)", ret, wc_GetErrorString(ret));
246+
return ret;
247+
}
234248
#endif
235249

236250
/* Inform the test runner that the test finished */
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef USER_SETTINGS_H
2+
#define USER_SETTINGS_H
3+
4+
#define WOLFCRYPT_ONLY // Build only wolfCrypt library.
5+
#define NO_OLD_RNGNAME // Resolves collision between STM32 HAL 'RNG' and WolfSSL 'RNG'.
6+
// #define USE_FAST_MATH
7+
8+
// We will provide custom implementation for seed generation.
9+
extern int wolfcrypt_custom_seed_gen(unsigned char *output, unsigned int sz);
10+
#define CUSTOM_RAND_GENERATE_SEED wolfcrypt_custom_seed_gen
11+
12+
#define WOLFSSL_SMALL_STACK // Offload stack usage to heap where possible.
13+
#define WOLFSSL_MALLOC_CHECK // Optional: Safety check for malloc failures.
14+
15+
#define NO_FILESYSTEM // Prevents filesystem errors on bare metal.
16+
#undef WOLFSSL_SYS_CA_CERTS // Force disable system CA certs (fixes dirent.h / filesystem errors).
17+
#define NO_WRITEV // IO vector write support usually missing.
18+
#define NO_WRITE_TEMP_FILES
19+
#define NO_DEV_RANDOM // We use STM32's RNG, not /dev/random.
20+
#define NO_MAIN_DRIVER
21+
22+
#define WOLFSSL_USER_IO // Disable the default BSD socket callbacks.
23+
24+
#define SINGLE_THREADED // No threads.
25+
26+
#endif /* USER_SETTINGS_H */
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <stdint.h>
2+
#include <string.h>
3+
#include <wolfssl/wolfcrypt/error-crypt.h>
4+
5+
#include "main.h"
6+
#include "stm32l4xx_hal.h"
7+
8+
int wolfcrypt_custom_seed_gen(unsigned char *output, unsigned int sz)
9+
{
10+
HAL_StatusTypeDef hal_status = HAL_OK;
11+
uint32_t random_data;
12+
size_t bytes_left = sz;
13+
14+
while (bytes_left) {
15+
hal_status = HAL_RNG_GenerateRandomNumber(&RNGHandle, &random_data);
16+
if (hal_status != HAL_OK) {
17+
return RNG_FAILURE_E;
18+
}
19+
20+
size_t cpy_cnt = bytes_left < sizeof(random_data) ? bytes_left : sizeof(random_data);
21+
memcpy(output, &random_data, cpy_cnt);
22+
bytes_left -= cpy_cnt;
23+
output += cpy_cnt;
24+
}
25+
26+
return 0;
27+
}

0 commit comments

Comments
 (0)