diff --git a/features.yaml b/features.yaml index 7cd1641c1696..4013f303d6d9 100644 --- a/features.yaml +++ b/features.yaml @@ -839,6 +839,12 @@ groups: help: SHA-512/224 hardware acceleration present. - name: periph_hash_sha_512_256 help: SHA-512/256 hardware acceleration present. + - name: periph_hash_sha3_256 + help: SHA-3/256 hardware acceleration present. + - name: periph_hash_sha3_384 + help: SHA-3/384 hardware acceleration present. + - name: periph_hash_sha3_512 + help: SHA-3/512 hardware acceleration present. - name: periph_hmac_sha_256 help: HMAC SHA-256 hardware acceleration present. - name: periph_hwrng diff --git a/makefiles/features_existing.inc.mk b/makefiles/features_existing.inc.mk index acf01e62f9ae..79c055c6fc67 100644 --- a/makefiles/features_existing.inc.mk +++ b/makefiles/features_existing.inc.mk @@ -180,6 +180,9 @@ FEATURES_EXISTING := \ periph_gpio_ll_switch_dir \ periph_gpio_tamper_wake \ periph_hash_md5 \ + periph_hash_sha3_256 \ + periph_hash_sha3_384 \ + periph_hash_sha3_512 \ periph_hash_sha_1 \ periph_hash_sha_224 \ periph_hash_sha_256 \ diff --git a/makefiles/features_modules.inc.mk b/makefiles/features_modules.inc.mk index 3b999311d227..df45d4ef4729 100644 --- a/makefiles/features_modules.inc.mk +++ b/makefiles/features_modules.inc.mk @@ -27,6 +27,9 @@ PERIPH_IGNORE_MODULES := \ periph_gpio_ll% \ periph_gpio_mux \ periph_hash_sha_1 \ + periph_hash_sha3_256 \ + periph_hash_sha3_384 \ + periph_hash_sha3_512 \ periph_hash_sha_224 \ periph_hash_sha_256 \ periph_hash_sha_384 \ diff --git a/makefiles/pseudomodules.inc.mk b/makefiles/pseudomodules.inc.mk index b87ee1c9641f..fb28302282a9 100644 --- a/makefiles/pseudomodules.inc.mk +++ b/makefiles/pseudomodules.inc.mk @@ -363,6 +363,9 @@ PSEUDOMODULES += psa_riot_hashes_sha_224 PSEUDOMODULES += psa_riot_hashes_sha_256 PSEUDOMODULES += psa_riot_hashes_sha_384 PSEUDOMODULES += psa_riot_hashes_sha_512 +PSEUDOMODULES += psa_riot_hashes_sha3_256 +PSEUDOMODULES += psa_riot_hashes_sha3_384 +PSEUDOMODULES += psa_riot_hashes_sha3_512 PSEUDOMODULES += psa_riot_hashes_sha_512_224 PSEUDOMODULES += psa_riot_hashes_sha_512_256 PSEUDOMODULES += psa_riot_hashes_hmac_sha256 diff --git a/sys/hashes/psa_riot_hashes/sha3_256.c b/sys/hashes/psa_riot_hashes/sha3_256.c new file mode 100644 index 000000000000..64acbd5b2193 --- /dev/null +++ b/sys/hashes/psa_riot_hashes/sha3_256.c @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2024 TU Dresden + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup sys_psa_crypto + * @{ + * + * @brief Glue code translating between PSA Crypto and the RIOT SHA3 Hash module + * + * @author Lennard Melling + * + * @} + */ +#include "psa/crypto.h" +#include "hashes/psa/riot_hashes.h" + +psa_status_t psa_hashes_sha3_256_update(psa_hashes_sha3_ctx_t *ctx, + const uint8_t *input, + size_t input_length) +{ + sha3_update((keccak_state_t *)ctx, input, input_length); + return PSA_SUCCESS; +} + +psa_status_t psa_hashes_sha3_256_setup(psa_hashes_sha3_ctx_t *ctx) +{ + sha3_256_init((keccak_state_t *)ctx); + return PSA_SUCCESS; +} + +psa_status_t psa_hashes_sha3_256_finish(psa_hashes_sha3_ctx_t *ctx, + uint8_t *hash) +{ + sha3_256_final((keccak_state_t *)ctx, hash); + return PSA_SUCCESS; +} diff --git a/sys/hashes/psa_riot_hashes/sha3_384.c b/sys/hashes/psa_riot_hashes/sha3_384.c new file mode 100644 index 000000000000..ea42bf302975 --- /dev/null +++ b/sys/hashes/psa_riot_hashes/sha3_384.c @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2024 TU Dresden + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup sys_psa_crypto + * @{ + * + * @brief Glue code translating between PSA Crypto and the RIOT SHA3 Hash module + * + * @author Lennard Melling + * + * @} + */ +#include "psa/crypto.h" +#include "hashes/psa/riot_hashes.h" + +psa_status_t psa_hashes_sha3_384_update(psa_hashes_sha3_ctx_t *ctx, + const uint8_t *input, + size_t input_length) +{ + sha3_update((keccak_state_t *)ctx, input, input_length); + return PSA_SUCCESS; +} + +psa_status_t psa_hashes_sha3_384_setup(psa_hashes_sha3_ctx_t *ctx) +{ + sha3_384_init((keccak_state_t *)ctx); + return PSA_SUCCESS; +} + +psa_status_t psa_hashes_sha3_384_finish(psa_hashes_sha3_ctx_t *ctx, + uint8_t *hash) +{ + sha3_384_final((keccak_state_t *)ctx, hash); + return PSA_SUCCESS; +} diff --git a/sys/hashes/psa_riot_hashes/sha3_512.c b/sys/hashes/psa_riot_hashes/sha3_512.c new file mode 100644 index 000000000000..cf8cbe36149b --- /dev/null +++ b/sys/hashes/psa_riot_hashes/sha3_512.c @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2024 TU Dresden + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup sys_psa_crypto + * @{ + * + * @brief Glue code translating between PSA Crypto and the RIOT SHA3 Hash module + * + * @author Lennard Melling + * + * @} + */ +#include "psa/crypto.h" +#include "hashes/psa/riot_hashes.h" + +psa_status_t psa_hashes_sha3_512_update(psa_hashes_sha3_ctx_t *ctx, + const uint8_t *input, + size_t input_length) +{ + sha3_update((keccak_state_t *)ctx, input, input_length); + return PSA_SUCCESS; +} + +psa_status_t psa_hashes_sha3_512_setup(psa_hashes_sha3_ctx_t *ctx) +{ + sha3_512_init((keccak_state_t *)ctx); + return PSA_SUCCESS; +} + +psa_status_t psa_hashes_sha3_512_finish(psa_hashes_sha3_ctx_t *ctx, + uint8_t *hash) +{ + sha3_512_final((keccak_state_t *)ctx, hash); + return PSA_SUCCESS; +} diff --git a/sys/include/hashes/psa/riot_hashes.h b/sys/include/hashes/psa/riot_hashes.h index b5754c48edbe..6e2bfd6e5aab 100644 --- a/sys/include/hashes/psa/riot_hashes.h +++ b/sys/include/hashes/psa/riot_hashes.h @@ -77,6 +77,14 @@ typedef sha512_256_context_t psa_hashes_sha512_256_ctx_t; #include "hashes/sha256.h" #endif +#if (IS_USED(MODULE_PSA_RIOT_HASHES_SHA3_256) \ +|| IS_USED(MODULE_PSA_RIOT_HASHES_SHA3_384) \ +|| IS_USED(MODULE_PSA_RIOT_HASHES_SHA3_512)) +#include "hashes/sha3.h" + +typedef keccak_state_t psa_hashes_sha3_ctx_t; +#endif + #ifdef __cplusplus } #endif diff --git a/sys/include/psa_crypto/psa/crypto_contexts.h b/sys/include/psa_crypto/psa/crypto_contexts.h index e0bafb0d403c..eea3e0041b22 100644 --- a/sys/include/psa_crypto/psa/crypto_contexts.h +++ b/sys/include/psa_crypto/psa/crypto_contexts.h @@ -52,6 +52,10 @@ typedef union { #if IS_USED(MODULE_PSA_HASH_SHA_512) || defined(DOXYGEN) psa_hashes_sha512_ctx_t sha512; /**< SHA-512 context */ #endif +#if IS_USED(MODULE_PSA_HASH_SHA3_256) || IS_USED(MODULE_PSA_HASH_SHA3_384) \ +|| IS_USED(MODULE_PSA_HASH_SHA3_512) || defined(DOXYGEN) + psa_hashes_sha3_ctx_t sha3; /**< SHA-3 context */ +#endif #if IS_USED(MODULE_PSA_HASH_SHA_512_224) || defined(DOXYGEN) psa_hashes_sha512_224_ctx_t sha512_224; /**< SHA-512/224 context */ #endif diff --git a/sys/include/psa_crypto/psa/crypto_includes.h b/sys/include/psa_crypto/psa/crypto_includes.h index b7360c9fd2a9..3b18f36bcfe6 100644 --- a/sys/include/psa_crypto/psa/crypto_includes.h +++ b/sys/include/psa_crypto/psa/crypto_includes.h @@ -33,7 +33,8 @@ extern "C" { IS_USED(MODULE_PSA_RIOT_HASHES_SHA_1) || IS_USED(MODULE_PSA_RIOT_HASHES_SHA_224) || \ IS_USED(MODULE_PSA_RIOT_HASHES_SHA_256) || IS_USED(MODULE_PSA_RIOT_HASHES_SHA_384) || \ IS_USED(MODULE_PSA_RIOT_HASHES_SHA_512) || IS_USED(MODULE_PSA_RIOT_HASHES_SHA_512_224) || \ - IS_USED(MODULE_PSA_RIOT_HASHES_SHA_512_256) + IS_USED(MODULE_PSA_RIOT_HASHES_SHA_512_256) || IS_USED(MODULE_PSA_RIOT_HASHES_SHA3_256) || \ + IS_USED(MODULE_PSA_RIOT_HASHES_SHA3_384) || IS_USED(MODULE_PSA_RIOT_HASHES_SHA3_512) #include "hashes/psa/riot_hashes.h" #endif diff --git a/sys/psa_crypto/Makefile.dep b/sys/psa_crypto/Makefile.dep index 2bb76124b1a8..cba3fb23c8d5 100644 --- a/sys/psa_crypto/Makefile.dep +++ b/sys/psa_crypto/Makefile.dep @@ -324,6 +324,78 @@ ifneq (,$(filter psa_hash_sha_512_256_backend_riot,$(USEMODULE))) USEMODULE += psa_riot_hashes_sha_512_256 endif +## SHA-3/256 +ifneq (,$(filter psa_hash_sha3_256,$(USEMODULE))) + ifeq (,$(filter psa_hash_sha3_256_custom_backend,$(USEMODULE))) + FEATURES_OPTIONAL += periph_hash_sha3_256 + include $(RIOTMAKE)/features_check.inc.mk + # HACK: Due to kconfig migration, may cause problems + ifneq (,$(filter periph_hash_sha3_256,$(FEATURES_USED))) + USEMODULE += psa_hash_sha3_256_backend_periph + else + USEMODULE += psa_hash_sha3_256_backend_riot + endif + endif +endif + +ifneq (,$(filter psa_hash_sha3_256_backend_periph,$(USEMODULE))) + FEATURES_REQUIRED += periph_hash_sha3_256 +endif + +ifneq (,$(filter psa_hash_sha3_256_backend_riot,$(USEMODULE))) + USEMODULE += hashes + USEMODULE += psa_riot_hashes + USEMODULE += psa_riot_hashes_sha3_256 +endif + +## SHA-3/384 +ifneq (,$(filter psa_hash_sha3_384,$(USEMODULE))) + ifeq (,$(filter psa_hash_sha3_384_custom_backend,$(USEMODULE))) + FEATURES_OPTIONAL += periph_hash_sha3_384 + include $(RIOTMAKE)/features_check.inc.mk + # HACK: Due to kconfig migration, may cause problems + ifneq (,$(filter periph_hash_sha3_384,$(FEATURES_USED))) + USEMODULE += psa_hash_sha3_384_backend_periph + else + USEMODULE += psa_hash_sha3_384_backend_riot + endif + endif +endif + +ifneq (,$(filter psa_hash_sha3_384_backend_periph,$(USEMODULE))) + FEATURES_REQUIRED += periph_hash_sha3_384 +endif + +ifneq (,$(filter psa_hash_sha3_384_backend_riot,$(USEMODULE))) + USEMODULE += hashes + USEMODULE += psa_riot_hashes + USEMODULE += psa_riot_hashes_sha3_384 +endif + +## SHA-3/512 +ifneq (,$(filter psa_hash_sha3_512,$(USEMODULE))) + ifeq (,$(filter psa_hash_sha3_512_custom_backend,$(USEMODULE))) + FEATURES_OPTIONAL += periph_hash_sha3_512 + include $(RIOTMAKE)/features_check.inc.mk + # HACK: Due to kconfig migration, may cause problems + ifneq (,$(filter periph_hash_sha3_512,$(FEATURES_USED))) + USEMODULE += psa_hash_sha3_512_backend_periph + else + USEMODULE += psa_hash_sha3_512_backend_riot + endif + endif +endif + +ifneq (,$(filter psa_hash_sha3_512_backend_periph,$(USEMODULE))) + FEATURES_REQUIRED += periph_hash_sha3_512 +endif + +ifneq (,$(filter psa_hash_sha3_512_backend_riot,$(USEMODULE))) + USEMODULE += hashes + USEMODULE += psa_riot_hashes + USEMODULE += psa_riot_hashes_sha3_512 +endif + # Key Management ifneq (,$(filter psa_key_management,$(USEMODULE))) USEMODULE += psa_key_slot_mgmt diff --git a/sys/psa_crypto/Makefile.include b/sys/psa_crypto/Makefile.include index d7ba3521d77d..2fa1d4aab93a 100644 --- a/sys/psa_crypto/Makefile.include +++ b/sys/psa_crypto/Makefile.include @@ -158,6 +158,42 @@ ifneq (,$(filter psa_hash_sha_512,$(USEMODULE))) endif endif +PSEUDOMODULES += psa_hash_sha3_256 +PSEUDOMODULES += psa_hash_sha3_256_backend_periph +PSEUDOMODULES += psa_hash_sha3_256_backend_riot +PSEUDOMODULES += psa_hash_sha3_256_custom_backend + +# check that one and only one backend has been selected +ifneq (,$(filter psa_hash_sha3_256,$(USEMODULE))) + ifneq (1,$(call backends,psa_hash_sha3_256)) + $(error "One (and only one) backend should be selected for psa_hash_sha3_256") + endif +endif + +PSEUDOMODULES += psa_hash_sha3_384 +PSEUDOMODULES += psa_hash_sha3_384_backend_periph +PSEUDOMODULES += psa_hash_sha3_384_backend_riot +PSEUDOMODULES += psa_hash_sha3_384_custom_backend + +# check that one and only one backend has been selected +ifneq (,$(filter psa_hash_sha3_384,$(USEMODULE))) + ifneq (1,$(call backends,psa_hash_sha3_384)) + $(error "One (and only one) backend should be selected for psa_hash_sha3_384") + endif +endif + +PSEUDOMODULES += psa_hash_sha3_512 +PSEUDOMODULES += psa_hash_sha3_512_backend_periph +PSEUDOMODULES += psa_hash_sha3_512_backend_riot +PSEUDOMODULES += psa_hash_sha3_512_custom_backend + +# check that one and only one backend has been selected +ifneq (,$(filter psa_hash_sha3_512,$(USEMODULE))) + ifneq (1,$(call backends,psa_hash_sha3_512)) + $(error "One (and only one) backend should be selected for psa_hash_sha3_512") + endif +endif + PSEUDOMODULES += psa_hash_sha_512_224 PSEUDOMODULES += psa_hash_sha_512_224_backend_periph PSEUDOMODULES += psa_hash_sha_512_224_backend_riot diff --git a/sys/psa_crypto/doc.txt b/sys/psa_crypto/doc.txt index ec2e40b09d26..773f22d8937f 100644 --- a/sys/psa_crypto/doc.txt +++ b/sys/psa_crypto/doc.txt @@ -336,6 +336,24 @@ * - psa_hash_sha_512_256_custom_backend * - psa_hash_sha_512_256_backend_riot * + * #### SHA 3/256 + * - psa_hash_sha3_256 + * - psa_hash_sha3_256_backend_periph + * - psa_hash_sha3_256_custom_backend + * - psa_hash_sha3_256_backend_riot + * + * #### SHA 3/384 + * - psa_hash_sha3_384 + * - psa_hash_sha3_384_backend_periph + * - psa_hash_sha3_384_custom_backend + * - psa_hash_sha3_384_backend_riot + * + * #### SHA 3/512 + * - psa_hash_sha3_512 + * - psa_hash_sha3_512_backend_periph + * - psa_hash_sha3_512_custom_backend + * - psa_hash_sha3_512_backend_riot + * * ### MAC * - Base: psa_mac * diff --git a/sys/psa_crypto/include/psa_hashes.h b/sys/psa_crypto/include/psa_hashes.h index f30c1df987f6..550bc9faa8c5 100644 --- a/sys/psa_crypto/include/psa_hashes.h +++ b/sys/psa_crypto/include/psa_hashes.h @@ -341,6 +341,111 @@ psa_status_t psa_hashes_sha512_256_finish(psa_hashes_sha512_256_ctx_t *ctx, size_t *hash_length); #endif /* MODULE_PSA_HASH_SHA_512_256 */ +#if IS_USED(MODULE_PSA_HASH_SHA3_256) || defined(DOXYGEN) +/** + * @brief Low level wrapper function to call a driver for a general SHA3 hash update + * See @ref psa_hash_update() + * + * @param ctx + * @param input + * @param input_length + * @return psa_status_t + */ +psa_status_t psa_hashes_sha3_256_update(psa_hashes_sha3_ctx_t *ctx, + const uint8_t *input, + size_t input_length); + +/** + * @brief Low level wrapper function to call a driver for a SHA3-256 hash setup + * See @ref psa_hash_setup() + * + * @param ctx + * @return psa_status_t + */ +psa_status_t psa_hashes_sha3_256_setup(psa_hashes_sha3_ctx_t *ctx); + +/** + * @brief Low level wrapper function to call a driver for a SHA3-256 hash finish + * See @ref psa_hash_finish() + * + * @param ctx + * @param hash + * @return psa_status_t + */ +psa_status_t psa_hashes_sha3_256_finish(psa_hashes_sha3_ctx_t *ctx, + uint8_t *hash); +#endif /* MODULE_PSA_HASH_SHA3_256 */ + +#if IS_USED(MODULE_PSA_HASH_SHA3_384) || defined(DOXYGEN) +/** + * @brief Low level wrapper function to call a driver for a general SHA3 hash update + * See @ref psa_hash_update() + * + * @param ctx + * @param input + * @param input_length + * @return psa_status_t + */ +psa_status_t psa_hashes_sha3_384_update(psa_hashes_sha3_ctx_t *ctx, + const uint8_t *input, + size_t input_length); + +/** + * @brief Low level wrapper function to call a driver for a SHA3-384 hash setup + * See @ref psa_hash_setup() + * + * @param ctx + * @return psa_status_t + */ +psa_status_t psa_hashes_sha3_384_setup(psa_hashes_sha3_ctx_t *ctx); + +/** + * @brief Low level wrapper function to call a driver for a SHA3-384 hash finish + * See @ref psa_hash_finish() + * + * @param ctx + * @param hash + * @return psa_status_t + */ +psa_status_t psa_hashes_sha3_384_finish(psa_hashes_sha3_ctx_t *ctx, + uint8_t *hash); +#endif /* MODULE_PSA_HASH_SHA3_384 */ + +#if IS_USED(MODULE_PSA_HASH_SHA3_512) || defined(DOXYGEN) +/** + * @brief Low level wrapper function to call a driver for a general SHA3 hash update + * See @ref psa_hash_update() + * + * @param ctx + * @param input + * @param input_length + * @return psa_status_t + */ +psa_status_t psa_hashes_sha3_512_update(psa_hashes_sha3_ctx_t *ctx, + const uint8_t *input, + size_t input_length); + +/** + * @brief Low level wrapper function to call a driver for a SHA3-512 hash setup + * See @ref psa_hash_setup() + * + * @param ctx + * @return psa_status_t + */ +psa_status_t psa_hashes_sha3_512_setup(psa_hashes_sha3_ctx_t *ctx); + +/** + * @brief Low level wrapper function to call a driver for a SHA3-512 hash finish + * See @ref psa_hash_finish() + * + * @param ctx + * @param hash + * @return psa_status_t + */ +psa_status_t psa_hashes_sha3_512_finish(psa_hashes_sha3_ctx_t *ctx, + uint8_t *hash); +#endif /* MODULE_PSA_HASH_SHA3_512 */ + #ifdef __cplusplus } #endif diff --git a/sys/psa_crypto/psa_crypto_algorithm_dispatch.c b/sys/psa_crypto/psa_crypto_algorithm_dispatch.c index 064a2ba46b0c..eda636094843 100644 --- a/sys/psa_crypto/psa_crypto_algorithm_dispatch.c +++ b/sys/psa_crypto/psa_crypto_algorithm_dispatch.c @@ -97,6 +97,30 @@ psa_status_t psa_algorithm_dispatch_hash_setup(psa_hash_operation_t *operation, } break; #endif + #if (IS_USED(MODULE_PSA_HASH_SHA3_256)) + case PSA_ALG_SHA3_256: + status = psa_hashes_sha3_256_setup(&operation->ctx.sha3); + if (status != PSA_SUCCESS) { + return status; + } + break; + #endif + #if (IS_USED(MODULE_PSA_HASH_SHA3_384)) + case PSA_ALG_SHA3_384: + status = psa_hashes_sha3_384_setup(&operation->ctx.sha3); + if (status != PSA_SUCCESS) { + return status; + } + break; + #endif + #if (IS_USED(MODULE_PSA_HASH_SHA3_512)) + case PSA_ALG_SHA3_512: + status = psa_hashes_sha3_512_setup(&operation->ctx.sha3); + if (status != PSA_SUCCESS) { + return status; + } + break; + #endif #if (IS_USED(MODULE_PSA_HASH_SHA_512_224)) case PSA_ALG_SHA_512_224: status = psa_hashes_sha512_224_setup(&operation->ctx.sha512_224); @@ -152,6 +176,18 @@ psa_status_t psa_algorithm_dispatch_hash_update(psa_hash_operation_t *operation, case PSA_ALG_SHA_512: return psa_hashes_sha512_update(&operation->ctx.sha512, input, input_length); #endif + #if (IS_USED(MODULE_PSA_HASH_SHA3_256)) + case PSA_ALG_SHA3_256: + return psa_hashes_sha3_256_update(&operation->ctx.sha3, input, input_length); + #endif + #if (IS_USED(MODULE_PSA_HASH_SHA3_384)) + case PSA_ALG_SHA3_384: + return psa_hashes_sha3_384_update(&operation->ctx.sha3, input, input_length); + #endif + #if (IS_USED(MODULE_PSA_HASH_SHA3_512)) + case PSA_ALG_SHA3_512: + return psa_hashes_sha3_512_update(&operation->ctx.sha3, input, input_length); + #endif #if (IS_USED(MODULE_PSA_HASH_SHA_512_224)) case PSA_ALG_SHA_512_224: return psa_hashes_sha512_224_update(&operation->ctx.sha512_224, input, input_length); @@ -206,6 +242,18 @@ psa_status_t psa_algorithm_dispatch_hash_finish(psa_hash_operation_t *operation, case PSA_ALG_SHA_512_256: return psa_hashes_sha512_256_finish(&operation->ctx.sha512_256, hash, hash_size, hash_length); #endif + #if (IS_USED(MODULE_PSA_HASH_SHA3_256)) + case PSA_ALG_SHA3_256: + return psa_hashes_sha3_256_finish(&operation->ctx.sha3, hash); + #endif + #if (IS_USED(MODULE_PSA_HASH_SHA3_384)) + case PSA_ALG_SHA3_384: + return psa_hashes_sha3_384_finish(&operation->ctx.sha3, hash); + #endif + #if (IS_USED(MODULE_PSA_HASH_SHA3_512)) + case PSA_ALG_SHA3_512: + return psa_hashes_sha3_512_finish(&operation->ctx.sha3, hash); + #endif default: (void)operation; (void)hash; diff --git a/tests/sys/psa_crypto_hashes/Makefile b/tests/sys/psa_crypto_hashes/Makefile index 535db6710521..c8c5f795ce91 100644 --- a/tests/sys/psa_crypto_hashes/Makefile +++ b/tests/sys/psa_crypto_hashes/Makefile @@ -6,6 +6,10 @@ USEMODULE += ztimer_usec USEMODULE += psa_crypto USEMODULE += psa_hash +USEMODULE += psa_hash_sha3_256 +USEMODULE += psa_hash_sha3_384 +USEMODULE += psa_hash_sha3_512 + USEMODULE += psa_hash_sha_224 USEMODULE += psa_hash_sha_256 USEMODULE += psa_hash_sha_384 diff --git a/tests/sys/psa_crypto_hashes/example_sha3_glue.c b/tests/sys/psa_crypto_hashes/example_sha3_glue.c new file mode 100644 index 000000000000..8f8a5bc9a2e5 --- /dev/null +++ b/tests/sys/psa_crypto_hashes/example_sha3_glue.c @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2023 TU Dresden + * 2024 HAW Hamburg + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup tests + * @{ + * + * @brief Tests the SHA3 gluecode for all variants (SHA3-256, SHA3-384, SHA3-512) + * + * @author Katharina Volkenand + * + * @} + */ + +#include "psa/crypto.h" + +static const uint8_t msg[] = "Hello World!"; +static const size_t msg_len = sizeof(msg)-1; // exclude NULL-byte + +static const uint8_t hash_sha3_256[] ={ + 0xd0, 0xe4, 0x74, 0x86, 0xbb, 0xf4, 0xc1, 0x6a, 0xca, 0xc2, 0x6f, 0x8b, + 0x65, 0x35, 0x92, 0x97, 0x3c, 0x13, 0x62, 0x90, 0x9f, 0x90, 0x26, 0x28, + 0x77, 0x08, 0x9f, 0x9c, 0x8a, 0x45, 0x36, 0xaf +}; + +static const uint8_t hash_sha3_384[] = { + 0xf3, 0x24, 0xcb, 0xd4, 0x21, 0x32, 0x6a, 0x2a, 0xba, 0xed, 0xf6, 0xf3, + 0x95, 0xd1, 0xa5,0x1e, 0x18, 0x9d, 0x4a, 0x71, 0xc7, 0x55, 0xf5, 0x31, + 0x28, 0x9e, 0x51, 0x9f, 0x07, 0x9b, 0x22, 0x46, 0x64, 0x96, 0x1e, 0x38, + 0x5a, 0xfc, 0xc3, 0x7d, 0xa3, 0x48, 0xbd, 0x85, 0x9f, 0x34, 0xfd, 0x1c +}; + +static const uint8_t hash_sha3_512[] = { + 0x32, 0x40, 0x0b, 0x5e, 0x89, 0x82, 0x2d, 0xe2, 0x54, 0xe8, 0xd5, 0xd9, + 0x42, 0x52, 0xc5, 0x2b, 0xdc, 0xb2, 0x7a, 0x35, 0x62, 0xca, 0x59, 0x3e, + 0x98, 0x03, 0x64, 0xd9, 0x84, 0x8b, 0x80, 0x41, 0xb9, 0x8e, 0xab, 0xe1, + 0x6c, 0x1a, 0x67, 0x97, 0x48, 0x49, 0x41, 0xd2, 0x37, 0x68, 0x64, 0xa1, + 0xb0, 0xe2, 0x48, 0xb0, 0xf7, 0xaf, 0x8b, 0x15, 0x55, 0xa7, 0x78, 0xc3, + 0x36, 0xa5, 0xbf, 0x48 +}; + +/** + * @brief Compares the computed hashes with the expected hashes. + * Hashes are computed using the PSA algorithm dispatch. + * + * @return psa_status_t + */ +psa_status_t example_sha3_glue(void) { + + psa_status_t status = PSA_ERROR_DOES_NOT_EXIST; + + status = psa_hash_compare( + PSA_ALG_SHA3_256, + msg, + msg_len, + hash_sha3_256, + sizeof(hash_sha3_256)); + if (status != PSA_SUCCESS) { + return status; + } + + status = psa_hash_compare( + PSA_ALG_SHA3_384, + msg, + msg_len, + hash_sha3_384, + sizeof(hash_sha3_384)); + if (status != PSA_SUCCESS) { + return status; + } + + status = psa_hash_compare( + PSA_ALG_SHA3_512, + msg, + msg_len, + hash_sha3_512, + sizeof(hash_sha3_512)); + if (status != PSA_SUCCESS) { + return status; + } + return PSA_SUCCESS; +} diff --git a/tests/sys/psa_crypto_hashes/main.c b/tests/sys/psa_crypto_hashes/main.c index 4536c60fc185..56b8f3961fd6 100644 --- a/tests/sys/psa_crypto_hashes/main.c +++ b/tests/sys/psa_crypto_hashes/main.c @@ -23,6 +23,7 @@ #include "ztimer.h" extern psa_status_t example_hash(void); +extern psa_status_t example_sha3_glue(void); int main(void) { @@ -41,6 +42,14 @@ int main(void) printf("Hash failed: %s\n", psa_status_to_humanly_readable(status)); } + start = ztimer_now(ZTIMER_USEC); + status = example_sha3_glue(); + printf("SHA3 glue code test took %d us\n", (int)(ztimer_now(ZTIMER_USEC) - start)); + if (status != PSA_SUCCESS) { + failed = true; + printf("SHA3 glue code failed: %s\n", psa_status_to_humanly_readable(status)); + } + ztimer_release(ZTIMER_USEC); if (failed) {