Skip to content

Commit

Permalink
tests/sys/psa_crypto_se_mac: Remove symlinks
Browse files Browse the repository at this point in the history
  • Loading branch information
Einhornhool committed Apr 5, 2024
1 parent 2b9e942 commit 4542f83
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 6 deletions.
4 changes: 1 addition & 3 deletions tests/sys/psa_crypto_se_mac/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion tests/sys/psa_crypto_se_mac/Makefile.ci
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ BOARD_INSUFFICIENT_MEMORY := \
atmega8 \
nucleo-l011k4 \
samd10-xmini \
stm32f030f4-demo \
#
1 change: 0 additions & 1 deletion tests/sys/psa_crypto_se_mac/example_hmac_sha256.c

This file was deleted.

76 changes: 76 additions & 0 deletions tests/sys/psa_crypto_se_mac/example_hmac_sha256.c
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*
* @}
*/

#include <stdio.h>
#include <stdint.h>

#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);
}
1 change: 0 additions & 1 deletion tests/sys/psa_crypto_se_mac/main.c

This file was deleted.

56 changes: 56 additions & 0 deletions tests/sys/psa_crypto_se_mac/main.c
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*
* @}
*/

#include <stdio.h>
#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;
}

0 comments on commit 4542f83

Please sign in to comment.