Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
variables:
GIT_SUBMODULE_STRATEGY: recursive
# PSA Crypto jobs need libmbedtls-dev. contrib/*_deps.sh install it, but
# images built before it was added lack it: install it if missing.
INSTALL_MBEDTLS: dpkg -s libmbedtls-dev >/dev/null 2>&1 || (apt-get update -qq && apt-get install -yqq --no-install-recommends libmbedtls-dev)

stages:
- test
Expand Down Expand Up @@ -91,9 +94,34 @@ test_amalgamation:
matrix:
- CC: [gcc, clang ]
BUILD_ARGS: [ "", -DBUILD_MINIMAL, -DBUILD_ELEMENTS, -DBUILD_ELEMENTS -DBUILD_MINIMAL ]
- CC: [gcc, clang ]
BUILD_ARGS: [ -DHAVE_PSA_CRYPTO_H, -DHAVE_PSA_CRYPTO_H -DBUILD_ELEMENTS -DBUILD_MINIMAL ]
LINK_ARGS: [ -lmbedcrypto ]
script:
- if [ -n "$LINK_ARGS" ]; then eval "$INSTALL_MBEDTLS"; fi
- touch config.h
- $CC $BUILD_ARGS -Wall -W -Wextra -Werror -Wno-unknown-attributes -I. -I./src -I./src/ccan -I./src/secp256k1/include src/ctest/amalgamation_compile_test.c
- $CC $BUILD_ARGS -Wall -W -Wextra -Werror -Wno-unknown-attributes -I. -I./src -I./src/ccan -I./src/secp256k1/include src/ctest/amalgamation_compile_test.c $LINK_ARGS

test_psa_crypto:
extends: .test_template
script:
- eval "$INSTALL_MBEDTLS"
- ./tools/cleanup.sh && ./tools/autogen.sh
- CFLAGS='-Wall -W -Wextra -Werror' ./configure --enable-psa-crypto --enable-export-all --enable-swig-python --disable-swig-java --enable-shared --disable-static
- make -j $(($(grep ^processor /proc/cpuinfo | wc -l) / 2))
- make check V=1
# PSA keeps global state that is reachable at exit by design; fail only on definite leaks
- for t in $(ls src/.libs/test_* | egrep -v '_clear|xml|json' | tr '\n' ' '); do LD_LIBRARY_PATH=./src/.libs/ valgrind --tool=memcheck --leak-check=full --errors-for-leak-kinds=definite --error-exitcode=1 --track-origins=true $t; done

test_esp_idf_psa_crypto:
stage: test
image: espressif/idf:v6.1
tags:
- ga
script:
- cd contrib/esp-idf-psa-check
- idf.py set-target esp32s3
- idf.py build

test_mingw_static_build:
extends: .test_template
Expand All @@ -113,7 +141,7 @@ test_no_elements_abi:
build_wally_release_files:
extends: .test_template
stage: release
needs: [test_mingw_static_build,test_with_valgrind,test_asan_ubsan_gcc,test_scan_build_clang,test_cmake,test_amalgamation]
needs: [test_mingw_static_build,test_with_valgrind,test_asan_ubsan_gcc,test_scan_build_clang,test_cmake,test_amalgamation,test_psa_crypto]
artifacts:
expire_in: 7 days
name: wallycore-bindings
Expand Down
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,22 @@ $ brew install swig
results in faster hashing via hardware on embedded platforms such as ESP32.
Note that the caller must ensure that ``sdkconfig.h`` and ``soc/soc_caps.h``
are available when compiling, e.g. by setting the `CFLAGS` environment variable
before calling configure. (default: no)
before calling configure. This option targets the legacy Mbed TLS 2.x/3.x
API as shipped with ESP-IDF 5.x; for ESP-IDF 6.x and Mbed TLS 4.x use
`--enable-psa-crypto` instead. (default: no)
- `--enable-psa-crypto`. Use the [PSA Crypto API](https://arm-software.github.io/psa-api/crypto/)
for SHA-256/SHA-512 hashing, as provided by TF-PSA-Crypto 1.0 / Mbed TLS 4.x,
Mbed TLS 2.28+/3.x, and ESP-IDF 6.x. This enables hardware accelerated hashing
via the platform's PSA drivers. Mutually exclusive with `--enable-mbed-tls`.
`wally_init()` calls `psa_crypto_init()`; callers that hash before calling
`wally_init()` must call `psa_crypto_init()` themselves (ESP-IDF does so at
boot). As the internal hashing API cannot report errors, a failed PSA hash
operation (e.g. a PSA driver failing to allocate memory) calls `abort()`.
Mbed TLS 3.6+ and TF-PSA-Crypto copy every hash input to the heap unless
`MBEDTLS_PSA_ASSUME_EXCLUSIVE_BUFFERS` is defined (ESP-IDF defines it);
consider defining it if wally is not called across a trust boundary.
Amalgamation builds should define `HAVE_PSA_CRYPTO_H` instead of
`HAVE_MBEDTLS_SHA256_H`/`HAVE_MBEDTLS_SHA512_H`. (default: no)
- `--enable-coverage`. Enables code coverage (default: no) Note that you will
need [lcov](http://ltp.sourceforge.net/coverage/lcov.php) installed to
build with this option enabled and generate coverage reports.
Expand Down
2 changes: 2 additions & 0 deletions _CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ option(WALLYCORE_INSTALL "Enable install" OFF)
option(WALLYCORE_COVERAGE "Enable coverage" OFF)
option(WALLYCORE_BUILD_ELEMENTS "Build elements" ON)
option(WALLYCORE_ENABLE_FUZZING "Enable fuzzing support" OFF)
option(WALLYCORE_ENABLE_MBED_TLS "Use mbed-tls hashing functions where available" OFF)
option(WALLYCORE_ENABLE_PSA_CRYPTO "Use PSA Crypto (TF-PSA-Crypto/Mbed TLS) hashing functions" OFF)
option(WALLYCORE_ENABLE_ADDRESS_SANITIZER "Enable address sanitizer" OFF)
option(WALLYCORE_ENABLE_UB_SANITIZER "Enable undefined behavior sanitizer" OFF)

Expand Down
5 changes: 4 additions & 1 deletion _cmake/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
#cmakedefine HAVE_INLINE_ASM @HAVE_INLINE_ASM@

/* Define to 1 if you have the <mbedtls/sha256.h,> header file. */
#cmakedefine HAVE_MBEDTLS_SHA256_H_ @HAVE_MBEDTLS_SHA256_H@
#cmakedefine HAVE_MBEDTLS_SHA256_H @HAVE_MBEDTLS_SHA256_H@

/* Define to 1 if you have the <mbedtls/sha512.h> header file. */
#cmakedefine HAVE_MBEDTLS_SHA512_H @HAVE_MBEDTLS_SHA512_H@

/* Define to 1 if you have the <psa/crypto.h> header file. */
#cmakedefine HAVE_PSA_CRYPTO_H @HAVE_PSA_CRYPTO_H@

/* Define to 1 if you have the `memset_s' function. */
#cmakedefine HAVE_MEMSET_S @HAVE_MEMSET_S@

Expand Down
15 changes: 13 additions & 2 deletions _cmake/utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,19 @@ function(generate_config_file)
"int main(void) {int a = 42; int *pnt = &a; __asm__ __volatile__ (\"\" : : \"r\"(pnt) : \"memory\");}"
HAVE_INLINE_ASM
)
check_include_file("mbedtls/sha256.h" HAVE_MBEDTLS_SHA256_H)
check_include_file("mbedtls/sha512.h" HAVE_MBEDTLS_SHA512_H)
if(WALLYCORE_ENABLE_MBED_TLS AND WALLYCORE_ENABLE_PSA_CRYPTO)
message(FATAL_ERROR "WALLYCORE_ENABLE_MBED_TLS and WALLYCORE_ENABLE_PSA_CRYPTO are mutually exclusive")
endif()
if(WALLYCORE_ENABLE_PSA_CRYPTO)
check_include_file("psa/crypto.h" HAVE_PSA_CRYPTO_H)
if(NOT HAVE_PSA_CRYPTO_H)
message(FATAL_ERROR "psa/crypto.h not found")
endif()
endif()
if(WALLYCORE_ENABLE_MBED_TLS)
check_include_file("mbedtls/sha256.h" HAVE_MBEDTLS_SHA256_H)
check_include_file("mbedtls/sha512.h" HAVE_MBEDTLS_SHA512_H)
endif()
check_function_exists("memset_s" HAVE_MEMSET_S)
check_function_exists("mmap" HAVE_MMAP)
check_function_exists("posix_memalign" HAVE_POSIX_MEMALIGN)
Expand Down
13 changes: 12 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ AC_ARG_ENABLE(minimal,
AS_HELP_STRING([--enable-minimal],[enable minimal size/memory footprint build (default: no)]),
[minimal=$enableval], [minimal=no])
AC_ARG_ENABLE(mbed-tls,
AS_HELP_STRING([--enable-mbed-tls],[enable minimal size/memory footprint build (default: no)]),
AS_HELP_STRING([--enable-mbed-tls],[use mbed-tls hashing functions where available (default: no)]),
[mbedtls=$enableval], [mbedtls=no])
AC_ARG_ENABLE(psa-crypto,
AS_HELP_STRING([--enable-psa-crypto],[use PSA Crypto (TF-PSA-Crypto/Mbed TLS) hashing functions (default: no)]),
[psa_crypto=$enableval], [psa_crypto=no])
AC_ARG_ENABLE(secp256k1-tests,
AS_HELP_STRING([--enable-secp256k1-tests],[enable secp256k1 tests (default: no)]),
[secp256k1_tests=$enableval], [secp256k1_tests=no])
Expand Down Expand Up @@ -268,9 +271,17 @@ if test "x$asm" = "xyes"; then
fi

AC_CHECK_HEADERS([byteswap.h sys/mman.h])
if test "x$mbedtls" = "xyes" -a "x$psa_crypto" = "xyes"; then
AC_MSG_ERROR([--enable-mbed-tls and --enable-psa-crypto are mutually exclusive])
fi
if test "x$mbedtls" = "xyes"; then
AC_CHECK_HEADERS([mbedtls/sha256.h mbedtls/sha512.h])
fi
if test "x$psa_crypto" = "xyes"; then
AC_CHECK_HEADERS([psa/crypto.h], [], [AC_MSG_ERROR([psa/crypto.h not found])])
AC_SEARCH_LIBS([psa_crypto_init], [tfpsacrypto mbedcrypto], [],
[AC_MSG_ERROR([no library providing psa_crypto_init() found])])
fi

AC_ARG_ENABLE(clear-tests,
AS_HELP_STRING([--enable-clear-tests],[enable tests for memory clearing (default: yes)]),
Expand Down
2 changes: 1 addition & 1 deletion contrib/bookworm_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ windows_packages=
if [ -z "$skip_windows" ]; then
windows_packages="g++-mingw-w64-x86-64"
fi
apt install --no-install-recommends unzip autoconf automake autotools-dev pkg-config build-essential libtool python3{,-dev,-pip,-virtualenv} python{,-dev}-is-python3 clang{,-format,-tidy} git swig curl cmake libssl-dev libtool-bin $java_packages curl $windows_packages valgrind jq -yqq
apt install --no-install-recommends unzip autoconf automake autotools-dev pkg-config build-essential libtool python3{,-dev,-pip,-virtualenv} python{,-dev}-is-python3 clang{,-format,-tidy} git swig curl cmake libssl-dev libmbedtls-dev libtool-bin $java_packages curl $windows_packages valgrind jq -yqq

if [ -z "$skip_java" ]; then
update-java-alternatives -s $(basename ${JAVA_HOME})
Expand Down
2 changes: 2 additions & 0 deletions contrib/esp-idf-psa-check/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build/
sdkconfig*
10 changes: 10 additions & 0 deletions contrib/esp-idf-psa-check/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Build-only check that the libwally amalgamation compiles and links against
# ESP-IDF 6.x with the PSA Crypto hashing backend (HAVE_PSA_CRYPTO_H).
#
# . $IDF_PATH/export.sh
# cd contrib/esp-idf-psa-check
# idf.py set-target esp32s3
# idf.py build
cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(wally_psa_check)
12 changes: 12 additions & 0 deletions contrib/esp-idf-psa-check/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
idf_component_register(SRCS main.c wally_amalgamation.c INCLUDE_DIRS . PRIV_REQUIRES mbedtls)
get_filename_component(WALLY "${CMAKE_CURRENT_SOURCE_DIR}/../../.." ABSOLUTE)
# The empty config.h in this directory must be found before ${WALLY}/src/config.h
target_include_directories(${COMPONENT_LIB} PRIVATE ${WALLY} ${WALLY}/src ${WALLY}/src/ccan ${WALLY}/src/secp256k1/include ${WALLY}/include)
# Mirrors the definitions used by Jade's libwally-core component
target_compile_definitions(${COMPONENT_LIB} PRIVATE
HAVE_PSA_CRYPTO_H=1 BUILD_ELEMENTS=1 BUILD_MINIMAL=1 HAVE_EXPLICIT_BZERO=1 HAVE_BUILTIN_POPCOUNT=1
ENABLE_MODULE_ECDH=1 ENABLE_MODULE_RECOVERY=1 ENABLE_MODULE_SCHNORRSIG=1 ENABLE_MODULE_EXTRAKEYS=1
ENABLE_MODULE_GENERATOR=1 ENABLE_MODULE_RANGEPROOF=1 ENABLE_MODULE_SURJECTIONPROOF=1
ENABLE_MODULE_WHITELIST=1 ENABLE_MODULE_ECDSA_S2C=1 ECMULT_WINDOW_SIZE=8)
target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-nonnull-compare -Wno-unused-function
-Wno-error=maybe-uninitialized -Wno-type-limits -Wno-error=array-parameter -Wno-unused-parameter)
Empty file.
18 changes: 18 additions & 0 deletions contrib/esp-idf-psa-check/main/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <stdio.h>
#include "wally_core.h"
#include "wally_crypto.h"

/* Reference the hashing entry points so that they are linked in */
void app_main(void)
{
static const unsigned char msg[] = "abc";
unsigned char h32[32], h64[64];
int ret = wally_init(0);
ret |= wally_sha256(msg, 3, h32, sizeof(h32));
ret |= wally_sha512(msg, 3, h64, sizeof(h64));
ret |= wally_sha256_midstate(msg, 3, h32, sizeof(h32));
ret |= wally_hmac_sha512(msg, 3, msg, 3, h64, sizeof(h64));
ret |= wally_bip340_tagged_hash(msg, 3, "TapLeaf", h32, sizeof(h32));
ret |= wally_pbkdf2_hmac_sha512(msg, 3, msg, 3, 0, 2, h64, sizeof(h64));
printf("wally psa check ret=%d %02x%02x\n", ret, h32[0], h32[1]);
}
1 change: 1 addition & 0 deletions contrib/esp-idf-psa-check/main/wally_amalgamation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "src/amalgamation/combined.c"
2 changes: 1 addition & 1 deletion contrib/trixie_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ windows_packages=
if [ -z "$skip_windows" ]; then
windows_packages="g++-mingw-w64-x86-64"
fi
apt install --no-install-recommends unzip autoconf automake autotools-dev pkg-config build-essential libtool python3{,-dev,-pip,-virtualenv} python{,-dev}-is-python3 clang{,-format,-tidy} git swig curl cmake libssl-dev libtool-bin $jdk_package $jre_package $windows_packages valgrind jq -yqq
apt install --no-install-recommends unzip autoconf automake autotools-dev pkg-config build-essential libtool python3{,-dev,-pip,-virtualenv} python{,-dev}-is-python3 clang{,-format,-tidy} git swig curl cmake libssl-dev libmbedtls-dev libtool-bin $jdk_package $jre_package $windows_packages valgrind jq -yqq

# Note --break-system-packages to allow installing our requirements system-wide
pip install valgrind-codequality gcovr -r contrib/requirements.txt --break-system-packages
Expand Down
3 changes: 3 additions & 0 deletions include/wally_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ extern "C" {
* Initialize wally.
*
* This function must be called once before threads are created by the application.
* When wally is built with the PSA Crypto hashing backend (``--enable-psa-crypto``),
* this function calls ``psa_crypto_init()``. Callers that hash before calling
* it must call ``psa_crypto_init()`` themselves.
*
* :param flags: Flags controlling what to initialize. Currently must be zero.
*/
Expand Down
25 changes: 17 additions & 8 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -224,28 +224,29 @@ SUBDIRS = secp256k1
endif

TESTS =
TESTS_CFLAGS = -I$(top_srcdir) -I$(top_srcdir)/include -I$(srcdir)/ccan -I$(top_srcdir)/src/secp256k1/include $(AM_CFLAGS)
noinst_PROGRAMS =
if RUN_TESTS
TESTS += test_bech32
noinst_PROGRAMS += test_bech32
test_bech32_SOURCES = ctest/test_bech32.c
test_bech32_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS)
test_bech32_CFLAGS = $(TESTS_CFLAGS)
test_bech32_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@
if PYTHON_MANYLINUX
test_bech32_LDADD += $(PYTHON_LIBS)
endif
TESTS += test_psbt
noinst_PROGRAMS += test_psbt
test_psbt_SOURCES = ctest/test_psbt.c ccan/ccan/str/hex/hex.c
test_psbt_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS) -I$(srcdir)/ccan
test_psbt_CFLAGS = $(TESTS_CFLAGS)
test_psbt_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@
if PYTHON_MANYLINUX
test_psbt_LDADD += $(PYTHON_LIBS)
endif
TESTS += test_psbt_limits
noinst_PROGRAMS += test_psbt_limits
test_psbt_limits_SOURCES = ctest/test_psbt_limits.c ccan/ccan/str/hex/hex.c
test_psbt_limits_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS) -I$(srcdir)/ccan
test_psbt_limits_CFLAGS = $(TESTS_CFLAGS)
test_psbt_limits_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@
if PYTHON_MANYLINUX
test_psbt_limits_LDADD += $(PYTHON_LIBS)
Expand All @@ -254,7 +255,7 @@ if USE_PTHREAD
TESTS += test_clear
noinst_PROGRAMS += test_clear
test_clear_SOURCES = ctest/test_clear.c
test_clear_CFLAGS = -I$(top_srcdir)/include -I$(top_srcdir) -I$(top_srcdir)/src/secp256k1/include $(PTHREAD_CFLAGS) $(AM_CFLAGS) $(NOOPT_CFLAGS) $(NOBUILTIN_CFLAGS)
test_clear_CFLAGS = $(TESTS_CFLAGS) $(PTHREAD_CFLAGS) $(AM_CFLAGS) $(NOOPT_CFLAGS) $(NOBUILTIN_CFLAGS)
test_clear_LIBS = $(PTHREAD_LIBS)
test_clear_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@
if PYTHON_MANYLINUX
Expand All @@ -264,23 +265,31 @@ endif
TESTS += test_coinselection
noinst_PROGRAMS += test_coinselection
test_coinselection_SOURCES = ctest/test_coinselection.c
test_coinselection_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS)
test_coinselection_CFLAGS = $(TESTS_CFLAGS)
test_coinselection_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@
if PYTHON_MANYLINUX
test_coinselection_LDADD += $(PYTHON_LIBS)
endif
TESTS += test_script
noinst_PROGRAMS += test_script
test_script_SOURCES = ctest/test_script.c
test_script_CFLAGS = $(TESTS_CFLAGS)
test_script_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@
if PYTHON_MANYLINUX
test_script_LDADD += $(PYTHON_LIBS)
endif
TESTS += test_tx
noinst_PROGRAMS += test_tx
test_tx_SOURCES = ctest/test_tx.c
test_tx_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS)
test_tx_CFLAGS = $(TESTS_CFLAGS)
test_tx_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@
if PYTHON_MANYLINUX
test_tx_LDADD += $(PYTHON_LIBS)
endif
TESTS += test_descriptor
noinst_PROGRAMS += test_descriptor
test_descriptor_SOURCES = ctest/test_descriptor.c
test_descriptor_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS)
test_descriptor_CFLAGS = $(TESTS_CFLAGS)
test_descriptor_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@
if PYTHON_MANYLINUX
test_descriptor_LDADD += $(PYTHON_LIBS)
Expand All @@ -289,7 +298,7 @@ if BUILD_ELEMENTS
TESTS += test_elements_tx
noinst_PROGRAMS += test_elements_tx
test_elements_tx_SOURCES = ctest/test_elements_tx.c
test_elements_tx_CFLAGS = -I$(top_srcdir)/include $(AM_CFLAGS)
test_elements_tx_CFLAGS = $(TESTS_CFLAGS)
test_elements_tx_LDADD = $(lib_LTLIBRARIES) @CTEST_EXTRA_STATIC@
if PYTHON_MANYLINUX
test_elements_tx_LDADD += $(PYTHON_LIBS)
Expand Down
4 changes: 4 additions & 0 deletions src/_CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ target_include_directories(
PRIVATE ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/ccan
)
target_link_libraries(wallycore PUBLIC secp256k1)
if(WALLYCORE_ENABLE_PSA_CRYPTO)
find_library(WALLYCORE_PSA_CRYPTO_LIB NAMES tfpsacrypto mbedcrypto REQUIRED)
target_link_libraries(wallycore PUBLIC ${WALLYCORE_PSA_CRYPTO_LIB})
endif()
if(WALLYCORE_ENABLE_COVERAGE AND CMAKE_BUILD_TYPE STREQUAL "Debug")
target_compile_options(wallycore PRIVATE --coverage)
target_link_options(wallycore PUBLIC --coverage)
Expand Down
Loading
Loading