From face8b6e43425d24f02b0260cdfa344c888b496c Mon Sep 17 00:00:00 2001 From: David Garske Date: Mon, 18 Mar 2024 13:40:10 -0700 Subject: [PATCH] Experimental support for Intel and ARM ASM with Zephyr. Related to issue #7116. --- zephyr/CMakeLists.txt | 31 ++++++++++++++++++++++ zephyr/Kconfig | 13 +++++++++ zephyr/samples/wolfssl_benchmark/prj.conf | 4 +++ zephyr/samples/wolfssl_test/prj.conf | 2 +- zephyr/samples/wolfssl_tls_thread/prj.conf | 1 + zephyr/user_settings.h | 27 +++++++++++++++++++ 6 files changed, 77 insertions(+), 1 deletion(-) diff --git a/zephyr/CMakeLists.txt b/zephyr/CMakeLists.txt index 376cbce6e..acc3c8543 100644 --- a/zephyr/CMakeLists.txt +++ b/zephyr/CMakeLists.txt @@ -128,6 +128,37 @@ if(CONFIG_WOLFSSL) zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/port/psa/psa_pkcbs.c) zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/port/st/stm32.c) + if(CONFIG_WOLFCRYPT_ARMASM) + # tested with board: "qemu_kvm_arm64" + zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/port/arm/armv8-aes.c) + zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/port/arm/armv8-32-aes-asm_c.c) + zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/port/arm/armv8-sha256.c) + zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/port/arm/armv8-32-sha256-asm_c.c) + zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/port/arm/armv8-sha512.c) + zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/port/arm/armv8-sha512-asm_c.c) + zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/port/arm/armv8-sha3-asm_c.c) + zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/port/arm/armv8-poly1305.c) + zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/port/arm/armv8-chacha.c) + zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/port/arm/armv8-curve25519_c.c) + + set(MCPU_FLAGS "-mcpu=cortex-a53+crypto -mstrict-align") + #set(MCPU_FLAGS "-mcpu=generic+crypto -mstrict-align") + endif() + + if(CONFIG_WOLFCRYPT_INTELASM) + # tested with board: "qemu_x86_64" + zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/sha256_asm.S) + zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/sha512_asm.S) + zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/sha3_asm.S) + zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/chacha_asm.S) + zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/poly1305_asm.S) + + # issues with aesni + #zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/aes_asm.S) + #zephyr_library_sources(${ZEPHYR_CURRENT_MODULE_DIR}/wolfcrypt/src/aes_gcm_x86_asm.S) + #set(MCPU_FLAGS "-march=native -maes -msse4 -mpclmul ") + endif() + zephyr_library_link_libraries(wolfSSL) target_compile_definitions(wolfSSL INTERFACE WOLFSSL_ZEPHYR) diff --git a/zephyr/Kconfig b/zephyr/Kconfig index 75b20ad47..5c6fa73ef 100644 --- a/zephyr/Kconfig +++ b/zephyr/Kconfig @@ -70,6 +70,19 @@ config WOLFCRYPT_FIPS Enables FIPS support in wolfCrypt. Requires the wolfSSL FIPS ready download that includes fips.c/fips_test.c. +config WOLFCRYPT_ARMASM + bool "wolfCrypt ARM Assembly support" + depends on WOLFSSL_BUILTIN + help + wolfCrypt ARM (ARMv8/ARMv7) assembly support for AES, SHA-2, SHA-3, + ChaCha20/Poly1305 and Curve25519 + +config WOLFCRYPT_INTELASM + bool "wolfCrypt Intel Assembly support" + depends on WOLFSSL_BUILTIN + help + wolfCrypt Intel Aassembly support (AVX/AVX2/AESNI) + config WOLFSSL_DEBUG bool "wolfSSL debug activation" depends on WOLFSSL_BUILTIN diff --git a/zephyr/samples/wolfssl_benchmark/prj.conf b/zephyr/samples/wolfssl_benchmark/prj.conf index b7e4eee43..41ccf7f94 100644 --- a/zephyr/samples/wolfssl_benchmark/prj.conf +++ b/zephyr/samples/wolfssl_benchmark/prj.conf @@ -26,6 +26,10 @@ CONFIG_LOG_BUFFER_SIZE=15360 #CONFIG_WOLFSSL_DEBUG=y # Entropy +CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_ENTROPY_GENERATOR=y CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR=y +# Optional ARM or Intel Assembly +#CONFIG_WOLFCRYPT_ARMASM=y +#CONFIG_WOLFCRYPT_INTELASM=y diff --git a/zephyr/samples/wolfssl_test/prj.conf b/zephyr/samples/wolfssl_test/prj.conf index a989213b4..6c8a5ca43 100644 --- a/zephyr/samples/wolfssl_test/prj.conf +++ b/zephyr/samples/wolfssl_test/prj.conf @@ -24,6 +24,6 @@ CONFIG_LOG_BUFFER_SIZE=15360 #CONFIG_WOLFSSL_DEBUG=y # Entropy +CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_ENTROPY_GENERATOR=y CONFIG_ENTROPY_DEVICE_RANDOM_GENERATOR=y - diff --git a/zephyr/samples/wolfssl_tls_thread/prj.conf b/zephyr/samples/wolfssl_tls_thread/prj.conf index fc0e01595..95ebedcbb 100644 --- a/zephyr/samples/wolfssl_tls_thread/prj.conf +++ b/zephyr/samples/wolfssl_tls_thread/prj.conf @@ -1,6 +1,7 @@ # Kernel options CONFIG_MAIN_STACK_SIZE=16384 CONFIG_ENTROPY_GENERATOR=y +CONFIG_TEST_RANDOM_GENERATOR=y CONFIG_INIT_STACKS=y CONFIG_COMMON_LIBC_MALLOC_ARENA_SIZE=65536 diff --git a/zephyr/user_settings.h b/zephyr/user_settings.h index 8b597144a..32bae9c8b 100644 --- a/zephyr/user_settings.h +++ b/zephyr/user_settings.h @@ -380,6 +380,33 @@ extern "C" { #endif #endif +/* ------------------------------------------------------------------------- */ +/* Assembly Speedups for Symmetric Algorithms */ +/* ------------------------------------------------------------------------- */ + +#ifdef CONFIG_WOLFCRYPT_ARMASM + #define WOLFSSL_ARMASM + #define WOLFSSL_NO_HASH_RAW + #define WOLFSSL_ARMASM_INLINE /* use inline .c versions */ + #define WOLFSSL_ARMASM_NO_HW_CRYPTO /* enable if processor does not support aes/sha instructions */ + #define WOLFSSL_ARMASM_NO_NEON + + /* Default is ARMv8 */ + + #if 0 /* ARMv7 */ + #define WOLFSSL_ARM_ARCH 7 + #endif +#endif + +#ifdef CONFIG_WOLFCRYPT_INTELASM + #define USE_INTEL_SPEEDUP + #define WOLFSSL_X86_64_BUILD /* 64-bit */ + //#define WOLFSSL_X86_BUILD /* 32-bit */ + + /* Issues with building AESNI "_mm_aesimc_si128" always_inline */ + //#define WOLFSSL_AESNI +#endif + /* ------------------------------------------------------------------------- */ /* Debugging */