Skip to content

Add STM32 hardware TDES-ECB and fix STM32 DES ECB decryption - #12266

Open
LinuxJedi wants to merge 1 commit into
wolfSSL:masterfrom
LinuxJedi:stm32-des3-ecb-link
Open

LinuxJedi wants to merge 1 commit into
wolfSSL:masterfrom
LinuxJedi:stm32-des3-ecb-link

Conversation

@LinuxJedi

@LinuxJedi LinuxJedi commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Description

Fixes DES ECB on STM32 hardware crypto (STM32_CRYPTO on a CRYP that does DES) in des3.c / des3.h. Based on master; follows #12079.

  • Build failure. The STM32 arm defined wc_Des_EcbEncrypt() but no wc_Des3_EcbEncrypt(), so any build with WOLFSSL_DES_ECB failed to link (des3_test references it).
  • ECB "decryption" encrypted. des3.h maps wc_Des_EcbDecrypt / wc_Des3_EcbDecrypt to the encrypt functions. That works in software, where a decryption key schedule is reversed, but the STM32 SetKey functions ignore the direction and the CRYP takes the same key either way. So wc_Des_EcbDecrypt() on STM32 hardware encrypted.

Changes:

  • Des3Crypt() takes a mode, like DesCrypt(), and runs TDES-ECB as well as TDES-CBC on all three paths (STM32_HAL_V2, CubeMX HAL, StdPeriph).
  • The STM32 arm defines wc_Des_EcbDecrypt(), wc_Des3_EcbEncrypt() and wc_Des3_EcbDecrypt(). For these builds, des3.h declares separate ECB decrypt functions instead of the macros, as it already does for FREESCALE_MMCAU. Callers in ssl_crypto.c and evp.c already use the Decrypt names.
  • The STM32_HAL_V2 paths of DesCrypt() and Des3Crypt() only read the IV back from the CRYP after CBC. ECB doesn't use the IV registers, so reading them back replaced des->reg with stale contents.

Testing

  • wolfCrypt test on the STM32Sim STM32F417 target (STM32CubeF4 HAL, STM32_HAL_V2, WOLFSSL_DES_ECB): DES and 3DES, including des_ecb_test and the 3DES ECB vectors, pass, and so does the rest of the suite. On master the same firmware fails to link on wc_Des3_EcbEncrypt. The F417 target and firmware are in STM32Sim: add STM32F417/F407 targets with RNG fault injection simulators#27, which should merge after this one.
  • Only the STM32_HAL_V2 path was built and run. The CubeMX HAL (without STM32_HAL_V2) and StdPeriph ECB branches follow the existing single-DES code, but current STM32CubeF4 no longer ships the old HAL API, and the StdPeriph library isn't available here, so those weren't compiled.

Checklist

  • added tests
  • updated/added doxygen
  • updated appropriate READMEs
  • Updated manual and documentation

With STM32_CRYPTO (on a CRYP that does DES) the des3.c hardware arm
defined wc_Des_EcbEncrypt() but no wc_Des3_EcbEncrypt(), so a build with
WOLFSSL_DES_ECB failed to link. It also could not decrypt in ECB mode:
des3.h maps wc_Des_EcbDecrypt / wc_Des3_EcbDecrypt to the encrypt
functions, which works in software because the decryption key schedule
is reversed, but the STM32 key setup ignores the direction and the CRYP
takes the same key either way, so ECB "decryption" encrypted.

- Des3Crypt() takes a mode, like DesCrypt(), and runs TDES-ECB as well
  as TDES-CBC on all three paths (HAL_V2, CubeMX HAL, StdPeriph).
- The STM32 arm defines wc_Des_EcbDecrypt(), wc_Des3_EcbEncrypt() and
  wc_Des3_EcbDecrypt(), and des3.h declares separate ECB decrypt
  functions for these builds, as it already does for FREESCALE_MMCAU.
- The HAL_V2 paths of DesCrypt() and Des3Crypt() only read the IV back
  from the CRYP after CBC. ECB does not use the IV registers, so reading
  them back replaced des->reg with stale contents.

Tested with the wolfCrypt test (WOLFSSL_DES_ECB, STM32_HAL_V2) on the
STM32Sim STM32F417 target: the DES and 3DES tests, including ECB, pass.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The legacy CubeMX HAL and StdPeriph ECB paths were neither compiled nor run, so vendor API compatibility and behavior need maintainer validation beyond the reported HAL V2 simulator coverage.

No open issues 🎉

What changed in this PR

Adds hardware-backed 3DES ECB support and corrects DES ECB decryption in wolfCrypt’s STM32 crypto backend, building on the CBC fixes in #12079.

Changes:

  • Implements 3DES ECB encryption and decryption across STM32 HAL V2, legacy CubeMX HAL, and StdPeriph paths.
  • Exposes separate STM32 ECB decrypt functions so hardware receives the correct operation direction.
  • Preserves the context’s CBC IV during ECB operations, including on HAL V2.
File Description
wolfssl/​wolfcrypt/​des3.h Declares separate ECB decrypt functions for STM32 hardware with DES support.
wolfcrypt/​src/​des3.c Adds STM32 3DES ECB support, corrects ECB decryption, and limits IV updates to CBC operations.

Review effort: Balanced

@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

zephyr-frdm_rw612

  • FLASH: datas +16 B, log_const_area -8 B, rodata +108 B, text +13,832 B (+4.9%, 296,412 B / 67,108,864 B, total: 0% used)
  • RAM: bss +49 B, datas +16 B (+0.1%, 58,423 B / 983,040 B, total: 6% used)

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants