Linker error when trying to use openssl library

Hey,
I’m trying to use the openssl library for SHA256 encryption within my platformio project. Including the header files by using the “build_flags” option in the platformio.ini file worked fine (“-I/usr/local/include”) and the project compiles without any errors. But I’m facing the following error when linking all the files:
src/senddata.cpp:99: undefined reference to 'SHA256'.

I tried adding linkflags “-L/usr/local/lib -lcrypto” as described in the openssl doc, but it did not do anything.
I also tried adding the path of the entire openssl library to “lib_deps” in platformio.ini, but this just caused more errors.

Does anybody know a similar problem and can help me finding a solution for this?

Thanks a lot in advice.
Paddlix

Some information:
openssl installed as described in the openssl doc.
IDE: VSCode
OS: macOS 13.4.1

Complete Error

Linking .pio/build/usb/firmware_ttgov21new_v3.6.1.elf

/Users/../.platformio/packages/toolchain-xtensa-esp32@8.4.0+2021r2-patch5/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: .pio/build/usb/src/senddata.cpp.o:(.literal._Z8sendDatav+0x4): undefined reference to 'SHA256'

/Users/../.platformio/packages/toolchain-xtensa-esp32@8.4.0+2021r2-patch5/bin/../lib/gcc/xtensa-esp32-elf/8.4.0/../../../../xtensa-esp32-elf/bin/ld: .pio/build/usb/src/senddata.cpp.o: in function 'sendData()':

/Users/../Library/Mobile Documents/com~apple~CloudDocs/Documents/../ESP32-Paxcounter-master/src/senddata.cpp:99: undefined reference to 'SHA256'

collect2: error: ld returned 1 exit status
*** [.pio/build/usb/firmware_ttgov21new_v3.6.1.elf] Error 1

SHA256 is a hash, not encryption.

You’re compiling for Xtensa32, aka an ESP32. You cannot link in anything from your /usr/local/lib, as that will have the precompiled library for your computer, most likely x86_64. Those are binary incompatible.

If you don’t want the troubles of compiling OpenSSL from source for the ESP32, just use mbedTLS. That is already built into Arduino-ESP32 (and ESP-IDF) with the integration for the hardware accelators. Calculating a SHA256 hash is as simple as

with uint8_t sha256[32] that holds the output hash, crt->raw.p being the data pointer, crt->raw.len being the length of the data to hash. And of course you’ll need

#include <mbedtls/sha256.h>
1 Like

Thanks, your explanation helped me a lot. That construct I was trying to create obviously cannot work.
And also thanks for the recommendation of using mbedtls. The library works just fine.