Skip to content

Commit

Permalink
examples/psa_crypto: hash example
Browse files Browse the repository at this point in the history
  • Loading branch information
mguetschow committed Apr 18, 2024
1 parent 9b355eb commit 618010f
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
8 changes: 8 additions & 0 deletions examples/psa_crypto/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ else
USEMODULE += psa_cipher
USEMODULE += psa_cipher_aes_128_cbc

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

USEMODULE += psa_mac
USEMODULE += psa_mac_hmac_sha_256

Expand Down
102 changes: 102 additions & 0 deletions examples/psa_crypto/example_hash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright (C) 2023 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 examples
* @{
*
* @brief Example functions for different hashing algorithms supported by PSA Crypto
*
* @author Mikolai Gütschow <mikolai.guetschow@tu-dresden.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));

Check warning on line 91 in examples/psa_crypto/example_hash.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
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));

Check warning on line 96 in examples/psa_crypto/example_hash.c

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
if (status != PSA_SUCCESS) {
return status;
}

return status;
}
9 changes: 9 additions & 0 deletions examples/psa_crypto/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ extern psa_status_t example_eddsa(void);
#endif
#endif

extern psa_status_t example_hash(void);

#ifdef MULTIPLE_SE
#if IS_USED(MODULE_PSA_CIPHER)
extern psa_status_t example_cipher_aes_128_sec_se(void);
Expand All @@ -63,6 +65,13 @@ int main(void)
(void)status;
(void)start;

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

#if IS_USED(MODULE_PSA_MAC)
status = example_hmac_sha256();
printf("HMAC SHA256 took %d us\n", (int)(ztimer_now(ZTIMER_USEC) - start));
Expand Down

0 comments on commit 618010f

Please sign in to comment.