Skip to content

Commit d15d243

Browse files
committed
Fix SHAKE with STM32MP13 and add simulator
This fixes the fact that you can't compile SHAKE when SHA-3 hardware acceleration is enabled for STM32. It also adds the STM32MP13 emulator with hardware support from the simulators repo.
1 parent be67bf8 commit d15d243

2 files changed

Lines changed: 37 additions & 7 deletions

File tree

.github/workflows/stm32-sim.yml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ concurrency:
1414

1515
# Build the STM32 software simulator (https://github.com/wolfSSL/simulators,
1616
# STM32Sim/ subdirectory) and run the wolfCrypt test suite on emulated
17-
# STM32H753 (Cortex-M7) and STM32U585 (Cortex-M33) hardware. Replaces the
18-
# previous Renode-based STM32H753 workflow and adds U5/PKA coverage.
17+
# STM32H753 (Cortex-M7), STM32U585 (Cortex-M33), and STM32MP135 (Cortex-A7)
18+
# hardware. Replaces the previous Renode-based STM32H753 workflow and adds
19+
# U5/PKA + MP135 (SHA3/SHAKE on HASH1) coverage.
1920
#
2021
# Dockerfile.wolfcrypt reads wolfSSL from /opt/wolfssl at runtime via a
2122
# bind mount, so unlike se050-sim.yml / stsafe-a120-sim.yml no Dockerfile
@@ -35,6 +36,8 @@ jobs:
3536
script: run-wolfcrypt-h7.sh
3637
- chip_label: U585
3738
script: run-wolfcrypt-u5.sh
39+
- chip_label: MP135
40+
script: run-wolfcrypt-mp135.sh
3841
steps:
3942
- name: Checkout wolfSSL (PR source)
4043
uses: actions/checkout@v4
@@ -44,6 +47,21 @@ jobs:
4447
- name: Clone STM32 simulator
4548
run: git clone --depth 1 https://github.com/wolfSSL/simulators simulators
4649

50+
# The MP135 firmware in the simulators repo currently disables SHAKE
51+
# in user_settings.h with a comment pointing at the wolfSSL build
52+
# break that this PR resolves. Once the simulators repo refreshes
53+
# that file, this patch step becomes a no-op (the grep below will
54+
# still pass) - drop it then.
55+
- name: Enable SHAKE in MP135 firmware user_settings.h
56+
if: matrix.chip_label == 'MP135'
57+
working-directory: simulators/STM32Sim/firmware/wolfcrypt-test-mp135
58+
run: |
59+
sed -i 's|^#define WOLFSSL_SHA3$|#define WOLFSSL_SHA3\n#define WOLFSSL_SHAKE128\n#define WOLFSSL_SHAKE256|' user_settings.h
60+
# Fail fast if the anchor line drifted - better than silently
61+
# building with SHAKE off and "passing" without exercising it.
62+
grep -q '^#define WOLFSSL_SHAKE128$' user_settings.h
63+
grep -q '^#define WOLFSSL_SHAKE256$' user_settings.h
64+
4765
- uses: docker/setup-buildx-action@v3
4866

4967
- name: Build stm32sim-wolfcrypt image

wolfcrypt/src/sha3.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@
7676
#include <wolfcrypt/src/misc.c>
7777
#endif
7878

79+
/* Software Keccak primitives are compiled when:
80+
* - No HW SHA-3 backend is selected (the original baseline), OR
81+
* - STM32 HW SHA-3 is selected and SHAKE is enabled - SHAKE on STM32MP13
82+
* runs in software because the HASH peripheral's SHAKE support is
83+
* fixed-length and does not match wolfSSL's variable-length / iterative
84+
* SqueezeBlocks API. SHA-3 still uses the HASH peripheral. */
85+
#if (!defined(STM32_HASH_SHA3) && !defined(PSOC6_HASH_SHA3)) || \
86+
(defined(STM32_HASH_SHA3) && \
87+
(defined(WOLFSSL_SHAKE128) || defined(WOLFSSL_SHAKE256)))
88+
#define WC_SHA3_SW_KECCAK
89+
#endif
90+
7991
#if FIPS_VERSION3_GE(6,0,0)
8092
const unsigned int wolfCrypt_FIPS_sha3_ro_sanity[2] =
8193
{ 0x1a2b3c4d, 0x00000016 };
@@ -320,7 +332,7 @@ void BlockSha3(word64* s)
320332
*/
321333
#define ROTL64(a, n) (((a)<<(n))|((a)>>(64-(n))))
322334

323-
#if !defined(STM32_HASH_SHA3) && !defined(PSOC6_HASH_SHA3)
335+
#ifdef WC_SHA3_SW_KECCAK
324336
/* An array of values to XOR for block operation. */
325337
static const word64 hash_keccak_r[24] =
326338
{
@@ -555,7 +567,7 @@ do { \
555567
while (0)
556568
#endif /* SHA3_BY_SPEC */
557569

558-
#if !defined(STM32_HASH_SHA3) && !defined(PSOC6_HASH_SHA3)
570+
#ifdef WC_SHA3_SW_KECCAK
559571
/* The block operation performed on the state.
560572
*
561573
* s The state.
@@ -582,10 +594,10 @@ void BlockSha3(word64* s)
582594
}
583595
}
584596
#endif /* WOLFSSL_SHA3_SMALL */
585-
#endif /* STM32_HASH_SHA3 */
597+
#endif /* WC_SHA3_SW_KECCAK */
586598
#endif /* !WOLFSSL_ARMASM && !WOLFSSL_RISCV_ASM */
587599

588-
#if !defined(STM32_HASH_SHA3) && !defined(PSOC6_HASH_SHA3)
600+
#ifdef WC_SHA3_SW_KECCAK
589601
#if defined(BIG_ENDIAN_ORDER)
590602
static WC_INLINE word64 Load64Unaligned(const unsigned char *a)
591603
{
@@ -929,7 +941,7 @@ static int Sha3Final(wc_Sha3* sha3, byte padChar, byte* hash, byte p, word32 l)
929941

930942
return 0;
931943
}
932-
#endif
944+
#endif /* WC_SHA3_SW_KECCAK */
933945
#if defined(STM32_HASH_SHA3)
934946

935947
/* Supports CubeMX HAL or Standard Peripheral Library */

0 commit comments

Comments
 (0)