trezor / trezor-firmware

:lock: Trezor Firmware Monorepo
https://trezor.io
Other
1.29k stars 637 forks source link

Use hardware-accelerated SHA-256 in STM32U5 #3638

Open andrewkozlik opened 3 months ago

andrewkozlik commented 3 months ago

The STM32U5 implements a hardware-accelerated version of SHA-256 and HMAC-SHA256, however not SHA-512. See Chapter 51 "Hash processor" in RM0456. They claim 66 clock cycles for processing one 512-bit block of data using SHA-256. I estimate this could be tens of times faster than our software implementation. It would probably give us a huge boost in speed for:

TychoVrahe commented 3 months ago

Note that we are already using this for firmware/bootloader integrity checks (without the HMAC). The speed-up was indeed as advertised. So this is more an issues of intergration into trezor-crypto rather than using the hash processor.

TychoVrahe commented 2 months ago

Before we integrate this everywhere, we need to solve context switching - possibly by means of functions HAL_HASH_ContextSaving and HAL_HASH_ContextRestoring. For now, every hash computation needs to call init, update, final and not be interrupted, otherwise it will not work properly. Also currently if final is not called at all but other hash computation starts, it will also result in incorrect hash.