esp32 Hardware SHA256 function
Posted: Wed Nov 01, 2023 6:50 pm
I found the esp32 datasheet (at https://www.espressif.com/sites/default ... ual_en.pdf ) - which makes for interesting reading.
I managed to get the 'genuine' random number generator working easily but - as I need SHA256 function for a project - then tried to get that working by writing to registers / memory (it looks straightforward!)
This failed
- always returning 0.
So then - trawling through the espressif includes found mbedtls/sha256.h - which does exactly what's needed (other SHA schemes are also available!)
I wrote a simple program to test - and all seems well. It can probably be made into a component too. Here it does a very simple SHA256 calculation (4 bytes) - and I checked the result against https://xorbin.com/tools/sha256-hash-calculator (which doesn't need quotes)
Note that you can amend the mbedTLS config by using idf.py menuconfig (it's under component config).
SHA256_Update takes an array of bytes of any length and can also be called multiple times before calling SHA256_Finish.
I don't check the result bytes returned (0 is success)
There are a host of other 'tools' in the mbedTLS folder (sha512, RSA etc) which look similar to use....
Martin
I managed to get the 'genuine' random number generator working easily but - as I need SHA256 function for a project - then tried to get that working by writing to registers / memory (it looks straightforward!)
This failed

So then - trawling through the espressif includes found mbedtls/sha256.h - which does exactly what's needed (other SHA schemes are also available!)
I wrote a simple program to test - and all seems well. It can probably be made into a component too. Here it does a very simple SHA256 calculation (4 bytes) - and I checked the result against https://xorbin.com/tools/sha256-hash-calculator (which doesn't need quotes)
Note that you can amend the mbedTLS config by using idf.py menuconfig (it's under component config).
SHA256_Update takes an array of bytes of any length and can also be called multiple times before calling SHA256_Finish.
I don't check the result bytes returned (0 is success)
There are a host of other 'tools' in the mbedTLS folder (sha512, RSA etc) which look similar to use....
Martin