diff --git a/tests/sys/psa_crypto_se_mac/Makefile b/tests/sys/psa_crypto_se_mac/Makefile index 3783b8062f495..f8e30e0843197 100644 --- a/tests/sys/psa_crypto_se_mac/Makefile +++ b/tests/sys/psa_crypto_se_mac/Makefile @@ -11,9 +11,7 @@ USEMODULE += psa_secure_element USEMODULE += psa_secure_element_ateccx08a USEMODULE += psa_secure_element_ateccx08a_hmac_sha256 -ifneq (1, $(SHOULD_RUN_KCONFIG)) - CFLAGS += -DCONFIG_PSA_PROTECTED_KEY_COUNT=1 -endif +CFLAGS += -DCONFIG_PSA_PROTECTED_KEY_COUNT=1 CFLAGS += -DSECURE_ELEMENT CFLAGS += -DCUSTOM_ATCA_PARAMS diff --git a/tests/sys/psa_crypto_se_mac/Makefile.ci b/tests/sys/psa_crypto_se_mac/Makefile.ci index 04da97e287d48..6e784f7ec2acc 100644 --- a/tests/sys/psa_crypto_se_mac/Makefile.ci +++ b/tests/sys/psa_crypto_se_mac/Makefile.ci @@ -8,5 +8,4 @@ BOARD_INSUFFICIENT_MEMORY := \ atmega8 \ nucleo-l011k4 \ samd10-xmini \ - stm32f030f4-demo \ # diff --git a/tests/sys/psa_crypto_se_mac/example_hmac_sha256.c b/tests/sys/psa_crypto_se_mac/example_hmac_sha256.c deleted file mode 120000 index 710efbeabcde1..0000000000000 --- a/tests/sys/psa_crypto_se_mac/example_hmac_sha256.c +++ /dev/null @@ -1 +0,0 @@ -../../../examples/psa_crypto/example_hmac_sha256.c \ No newline at end of file diff --git a/tests/sys/psa_crypto_se_mac/example_hmac_sha256.c b/tests/sys/psa_crypto_se_mac/example_hmac_sha256.c new file mode 100644 index 0000000000000..b6a7c181abc4f --- /dev/null +++ b/tests/sys/psa_crypto_se_mac/example_hmac_sha256.c @@ -0,0 +1,76 @@ +/* + * Copyright (C) 2022 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 examples + * @{ + * + * @brief Example functions for HMAC SHA256 with PSA Crypto + * + * @author Lena Boeckmann + * + * @} + */ + +#include +#include + +#include "psa/crypto.h" + +static const uint8_t HMAC_KEY[] = { + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, + 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b +}; +static size_t HMAC_KEY_LEN = 32; + +static const uint8_t HMAC_MSG[] = { + 0x54, 0x68, 0x69, 0x73, 0x20, 0x69, 0x73, 0x20, + 0x61, 0x20, 0x74, 0x65, 0x73, 0x74, 0x73, 0x74, + 0x72, 0x69, 0x6e, 0x67, 0x20, 0x66, 0x6f, 0x72, + 0x20, 0x68, 0x6d, 0x61, 0x63, 0x32, 0x35, 0x36 +}; +static size_t HMAC_MSG_LEN = 32; + +/** + * @brief Example function to perform an HMAC SHA-256 computation + * with the PSA Crypto API. + * + * @return psa_status_t + */ +psa_status_t example_hmac_sha256(void) +{ + psa_key_attributes_t attr = psa_key_attributes_init(); + psa_key_id_t key_id = 0; + psa_key_usage_t usage = PSA_KEY_USAGE_SIGN_MESSAGE; + + size_t digest_size = + PSA_MAC_LENGTH(PSA_KEY_TYPE_HMAC, HMAC_KEY_LEN, PSA_ALG_HMAC(PSA_ALG_SHA_256)); + uint8_t digest[digest_size]; + size_t output_len = 0; + + psa_set_key_algorithm(&attr, PSA_ALG_HMAC(PSA_ALG_SHA_256)); + psa_set_key_usage_flags(&attr, usage); + psa_set_key_bits(&attr, PSA_BYTES_TO_BITS(HMAC_KEY_LEN)); + psa_set_key_type(&attr, PSA_KEY_TYPE_HMAC); + + psa_key_lifetime_t lifetime = PSA_KEY_LIFETIME_FROM_PERSISTENCE_AND_LOCATION( + PSA_KEY_LIFETIME_VOLATILE, PSA_ATCA_LOCATION_DEV0); + psa_set_key_lifetime(&attr, lifetime); + + psa_status_t status = PSA_ERROR_DOES_NOT_EXIST; + status = psa_import_key(&attr, HMAC_KEY, HMAC_KEY_LEN, &key_id); + if (status != PSA_SUCCESS) { + return status; + } + + return psa_mac_compute(key_id, PSA_ALG_HMAC(PSA_ALG_SHA_256), + HMAC_MSG, HMAC_MSG_LEN, digest, digest_size, + &output_len); +} diff --git a/tests/sys/psa_crypto_se_mac/main.c b/tests/sys/psa_crypto_se_mac/main.c deleted file mode 120000 index a9fd2e2825758..0000000000000 --- a/tests/sys/psa_crypto_se_mac/main.c +++ /dev/null @@ -1 +0,0 @@ -../../../examples/psa_crypto/main.c \ No newline at end of file diff --git a/tests/sys/psa_crypto_se_mac/main.c b/tests/sys/psa_crypto_se_mac/main.c new file mode 100644 index 0000000000000..fbe2262e9240d --- /dev/null +++ b/tests/sys/psa_crypto_se_mac/main.c @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2022 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 examples + * @{ + * + * @brief Example application for PSA Crypto + * + * @author Lena Boeckmann + * + * @} + */ + +#include +#include "psa/crypto.h" +#include "ztimer.h" + +extern psa_status_t example_hmac_sha256(void); + +int main(void) +{ + bool failed = false; + psa_status_t status; + + psa_crypto_init(); + + ztimer_acquire(ZTIMER_USEC); + ztimer_now_t start = ztimer_now(ZTIMER_USEC); + + /* Needed in case only hashes are tested */ + (void)status; + (void)start; + + status = example_hmac_sha256(); + printf("HMAC SHA256 took %d us\n", (int)(ztimer_now(ZTIMER_USEC) - start)); + if (status != PSA_SUCCESS) { + failed = true; + printf("HMAC SHA256 failed: %s\n", psa_status_to_humanly_readable(status)); + } + + ztimer_release(ZTIMER_USEC); + + if (failed) { + puts("Tests failed..."); + } + else { + puts("All Done"); + } + return 0; +}