Skip to content

Commit

Permalink
tests/sys/psa_crypto_hashes: sync with content from examples/psa_crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
mguetschow committed May 17, 2024
1 parent f58392a commit 6be95ca
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 41 deletions.
5 changes: 5 additions & 0 deletions tests/sys/psa_crypto_hashes/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ USEMODULE += ztimer_usec
USEMODULE += psa_crypto

USEMODULE += psa_hash
USEMODULE += psa_hash_sha_224
USEMODULE += psa_hash_sha_256
USEMODULE += psa_hash_sha_384
USEMODULE += psa_hash_sha_512
USEMODULE += psa_hash_sha_512_224
USEMODULE += psa_hash_sha_512_256

include $(RIOTBASE)/Makefile.include
1 change: 1 addition & 0 deletions tests/sys/psa_crypto_hashes/Makefile.ci
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
BOARD_INSUFFICIENT_MEMORY := \
arduino-duemilanove \
arduino-leonardo \
arduino-nano \
arduino-uno \
atmega328p \
Expand Down
105 changes: 105 additions & 0 deletions tests/sys/psa_crypto_hashes/example_hash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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 PSA hash configurations
* Contents have been copied from `examples/psa_crypto`
*
* @author Mikolai Gütschow <mikolai.guetschow@tu-dresden.de>
* @author Lena Boeckmann <lena.boeckmann@haw-hamburg.de>
*
* @}
*/

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

#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_sha224[] = {
0x45, 0x75, 0xbb, 0x4e, 0xc1, 0x29, 0xdf, 0x63, 0x80, 0xce, 0xdd, 0xe6, 0xd7,
0x12, 0x17, 0xfe, 0x05, 0x36, 0xf8, 0xff, 0xc4, 0xe1, 0x8b, 0xca, 0x53, 0x0a,
0x7a, 0x1b};

static const uint8_t hash_sha256[] = {
0x7f, 0x83, 0xb1, 0x65, 0x7f, 0xf1, 0xfc, 0x53, 0xb9, 0x2d, 0xc1, 0x81, 0x48,
0xa1, 0xd6, 0x5d, 0xfc, 0x2d, 0x4b, 0x1f, 0xa3, 0xd6, 0x77, 0x28, 0x4a, 0xdd,
0xd2, 0x00, 0x12, 0x6d, 0x90, 0x69};

static const uint8_t hash_sha384[] = {
0xbf, 0xd7, 0x6c, 0x0e, 0xbb, 0xd0, 0x06, 0xfe, 0xe5, 0x83, 0x41, 0x05, 0x47,
0xc1, 0x88, 0x7b, 0x02, 0x92, 0xbe, 0x76, 0xd5, 0x82, 0xd9, 0x6c, 0x24, 0x2d,
0x2a, 0x79, 0x27, 0x23, 0xe3, 0xfd, 0x6f, 0xd0, 0x61, 0xf9, 0xd5, 0xcf, 0xd1,
0x3b, 0x8f, 0x96, 0x13, 0x58, 0xe6, 0xad, 0xba, 0x4a};

static const uint8_t hash_sha512[] = {
0x86, 0x18, 0x44, 0xd6, 0x70, 0x4e, 0x85, 0x73, 0xfe, 0xc3, 0x4d, 0x96, 0x7e,
0x20, 0xbc, 0xfe, 0xf3, 0xd4, 0x24, 0xcf, 0x48, 0xbe, 0x04, 0xe6, 0xdc, 0x08,
0xf2, 0xbd, 0x58, 0xc7, 0x29, 0x74, 0x33, 0x71, 0x01, 0x5e, 0xad, 0x89, 0x1c,
0xc3, 0xcf, 0x1c, 0x9d, 0x34, 0xb4, 0x92, 0x64, 0xb5, 0x10, 0x75, 0x1b, 0x1f,
0xf9, 0xe5, 0x37, 0x93, 0x7b, 0xc4, 0x6b, 0x5d, 0x6f, 0xf4, 0xec, 0xc8};

static const uint8_t hash_sha512_224[] = {
0xba, 0x07, 0x02, 0xdd, 0x8d, 0xd2, 0x32, 0x80, 0xb6, 0x17, 0xef, 0x28, 0x8b,
0xcc, 0x7e, 0x27, 0x60, 0x60, 0xb8, 0xeb, 0xcd, 0xdf, 0x28, 0xf8, 0xe4, 0x35,
0x6e, 0xae};

static const uint8_t hash_sha512_256[] = {
0xf3, 0x71, 0x31, 0x9e, 0xee, 0x6b, 0x39, 0xb0, 0x58, 0xec, 0x26, 0x2d, 0x4e,
0x72, 0x3a, 0x26, 0x71, 0x0e, 0x46, 0x76, 0x13, 0x01, 0xc8, 0xb5, 0x4c, 0x56,
0xfa, 0x72, 0x22, 0x67, 0x58, 0x1a};

/**
* @brief Example function to use different hash algorithms
* with the PSA Crypto API.
*
* @return psa_status_t
*/
psa_status_t example_hash(void)
{
psa_status_t status = PSA_ERROR_DOES_NOT_EXIST;

status = psa_hash_compare(PSA_ALG_SHA_224, msg, msg_len, hash_sha224, sizeof(hash_sha224));
if (status != PSA_SUCCESS) {
return status;
}

status = psa_hash_compare(PSA_ALG_SHA_256, msg, msg_len, hash_sha256, sizeof(hash_sha256));
if (status != PSA_SUCCESS) {
return status;
}

status = psa_hash_compare(PSA_ALG_SHA_384, msg, msg_len, hash_sha384, sizeof(hash_sha384));
if (status != PSA_SUCCESS) {
return status;
}

status = psa_hash_compare(PSA_ALG_SHA_512, msg, msg_len, hash_sha512, sizeof(hash_sha512));
if (status != PSA_SUCCESS) {
return status;
}

status = psa_hash_compare(PSA_ALG_SHA_512_224, msg, msg_len, hash_sha512_224, sizeof(hash_sha512_224));
if (status != PSA_SUCCESS) {
return status;
}

status = psa_hash_compare(PSA_ALG_SHA_512_256, msg, msg_len, hash_sha512_256, sizeof(hash_sha512_256));
if (status != PSA_SUCCESS) {
return status;
}

return status;
}
37 changes: 0 additions & 37 deletions tests/sys/psa_crypto_hashes/example_hash_sha256.c

This file was deleted.

8 changes: 4 additions & 4 deletions tests/sys/psa_crypto_hashes/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "psa/crypto.h"
#include "ztimer.h"

extern psa_status_t example_hash_sha256(void);
extern psa_status_t example_hash(void);

int main(void)
{
Expand All @@ -34,11 +34,11 @@ int main(void)
ztimer_acquire(ZTIMER_USEC);
ztimer_now_t start = ztimer_now(ZTIMER_USEC);

status = example_hash_sha256();
printf("Hash SHA256 took %d us\n", (int)(ztimer_now(ZTIMER_USEC) - start));
status = example_hash();
printf("Hash took %d us\n", (int)(ztimer_now(ZTIMER_USEC) - start));
if (status != PSA_SUCCESS) {
failed = true;
printf("Hash SHA256 failed: %s\n", psa_status_to_humanly_readable(status));
printf("Hash failed: %s\n", psa_status_to_humanly_readable(status));
}

ztimer_release(ZTIMER_USEC);
Expand Down

0 comments on commit 6be95ca

Please sign in to comment.