From 666305e15f0b250d3331fa1f08c0f32929a27334 Mon Sep 17 00:00:00 2001 From: Lena Boeckmann Date: Fri, 5 Apr 2024 16:56:14 +0200 Subject: [PATCH] tests/sys/psa_crypto_se_cipher: Remove symlinks --- tests/sys/psa_crypto_se_cipher/Makefile | 4 +- tests/sys/psa_crypto_se_cipher/Makefile.ci | 1 - .../example_cipher_aes_128.c | 89 ++++++++++++++++++- tests/sys/psa_crypto_se_cipher/main.c | 58 +++++++++++- 4 files changed, 146 insertions(+), 6 deletions(-) mode change 120000 => 100644 tests/sys/psa_crypto_se_cipher/example_cipher_aes_128.c mode change 120000 => 100644 tests/sys/psa_crypto_se_cipher/main.c diff --git a/tests/sys/psa_crypto_se_cipher/Makefile b/tests/sys/psa_crypto_se_cipher/Makefile index f0eca74b1cef2..bf9fd77b40d42 100644 --- a/tests/sys/psa_crypto_se_cipher/Makefile +++ b/tests/sys/psa_crypto_se_cipher/Makefile @@ -11,9 +11,7 @@ USEMODULE += psa_secure_element USEMODULE += psa_secure_element_ateccx08a USEMODULE += psa_secure_element_ateccx08a_cipher_aes_128 -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_cipher/Makefile.ci b/tests/sys/psa_crypto_se_cipher/Makefile.ci index 04da97e287d48..6e784f7ec2acc 100644 --- a/tests/sys/psa_crypto_se_cipher/Makefile.ci +++ b/tests/sys/psa_crypto_se_cipher/Makefile.ci @@ -8,5 +8,4 @@ BOARD_INSUFFICIENT_MEMORY := \ atmega8 \ nucleo-l011k4 \ samd10-xmini \ - stm32f030f4-demo \ # diff --git a/tests/sys/psa_crypto_se_cipher/example_cipher_aes_128.c b/tests/sys/psa_crypto_se_cipher/example_cipher_aes_128.c deleted file mode 120000 index 3b052c133690a..0000000000000 --- a/tests/sys/psa_crypto_se_cipher/example_cipher_aes_128.c +++ /dev/null @@ -1 +0,0 @@ -../../../examples/psa_crypto/example_cipher_aes_128.c \ No newline at end of file diff --git a/tests/sys/psa_crypto_se_cipher/example_cipher_aes_128.c b/tests/sys/psa_crypto_se_cipher/example_cipher_aes_128.c new file mode 100644 index 0000000000000..c363304778ea9 --- /dev/null +++ b/tests/sys/psa_crypto_se_cipher/example_cipher_aes_128.c @@ -0,0 +1,88 @@ +/* + * 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 AES CBC encryption with PSA Crypto + * + * @author Lena Boeckmann + * + * @} + */ + +#include +#include + +#include "psa/crypto.h" + +#define AES_128_KEY_SIZE (16) +#define AES_256_KEY_SIZE (32) + +static const uint8_t KEY_128[] = { + 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, + 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c +}; + +static uint8_t PLAINTEXT[] = { + 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, + 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a, + 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, + 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51 +}; +static uint8_t PLAINTEXT_LEN = 32; + +/** + * @brief Example function to perform an AES-128 CBC encryption and decryption + * with the PSA Crypto API. + * + * @return psa_status_t + */ +psa_status_t example_cipher_aes_128(void) +{ + psa_status_t status = PSA_ERROR_DOES_NOT_EXIST; + psa_key_id_t key_id = 0; + psa_key_attributes_t attr = psa_key_attributes_init(); + psa_key_usage_t usage = PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT; + + size_t encr_output_size = PSA_CIPHER_ENCRYPT_OUTPUT_SIZE(PSA_KEY_TYPE_AES, + PSA_ALG_CBC_NO_PADDING, PLAINTEXT_LEN); + + uint8_t cipher_out[encr_output_size]; + uint8_t plain_out[sizeof(PLAINTEXT)]; + size_t output_len = 0; + + psa_set_key_algorithm(&attr, PSA_ALG_CBC_NO_PADDING); + psa_set_key_usage_flags(&attr, usage); + psa_set_key_bits(&attr, 128); + psa_set_key_type(&attr, PSA_KEY_TYPE_AES); + + + 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); + + status = psa_import_key(&attr, KEY_128, AES_128_KEY_SIZE, &key_id); + if (status != PSA_SUCCESS) { + return status; + } + + status = psa_cipher_encrypt(key_id, PSA_ALG_CBC_NO_PADDING, PLAINTEXT, + PLAINTEXT_LEN, cipher_out, encr_output_size, &output_len); + if (status != PSA_SUCCESS) { + return status; + } + + status = psa_cipher_decrypt(key_id, PSA_ALG_CBC_NO_PADDING, cipher_out, + sizeof(cipher_out), plain_out, sizeof(plain_out), &output_len); + if (status == PSA_SUCCESS) { + return (memcmp(PLAINTEXT, plain_out, sizeof(plain_out)) ? -1 : 0); + } + return status; +} diff --git a/tests/sys/psa_crypto_se_cipher/main.c b/tests/sys/psa_crypto_se_cipher/main.c deleted file mode 120000 index a9fd2e2825758..0000000000000 --- a/tests/sys/psa_crypto_se_cipher/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_cipher/main.c b/tests/sys/psa_crypto_se_cipher/main.c new file mode 100644 index 0000000000000..cdb982339f58f --- /dev/null +++ b/tests/sys/psa_crypto_se_cipher/main.c @@ -0,0 +1,57 @@ +/* + * 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_cipher_aes_128(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; + + start = ztimer_now(ZTIMER_USEC); + status = example_cipher_aes_128(); + printf("Cipher AES 128 took %d us\n", (int)(ztimer_now(ZTIMER_USEC) - start)); + if (status != PSA_SUCCESS) { + failed = true; + printf("Cipher AES 128 failed: %s\n", psa_status_to_humanly_readable(status)); + } + + ztimer_release(ZTIMER_USEC); + + if (failed) { + puts("Tests failed..."); + } + else { + puts("All Done"); + } + return 0; +}