From 7842c3dc6fa360871c67e1daa77de69c01a278e1 Mon Sep 17 00:00:00 2001 From: Einhornhool <26007369+Einhornhool@users.noreply.github.com> Date: Tue, 28 Jan 2020 18:32:47 +0100 Subject: [PATCH] tests: add pkg_cryptoauthlib_compare_sha256 --- .../pkg_cryptoauthlib_compare_sha256/Makefile | 11 +++ tests/pkg_cryptoauthlib_compare_sha256/main.c | 89 +++++++++++++++++++ 2 files changed, 100 insertions(+) create mode 100644 tests/pkg_cryptoauthlib_compare_sha256/Makefile create mode 100644 tests/pkg_cryptoauthlib_compare_sha256/main.c diff --git a/tests/pkg_cryptoauthlib_compare_sha256/Makefile b/tests/pkg_cryptoauthlib_compare_sha256/Makefile new file mode 100644 index 0000000000..99e6d5bd60 --- /dev/null +++ b/tests/pkg_cryptoauthlib_compare_sha256/Makefile @@ -0,0 +1,11 @@ +include ../Makefile.tests_common + +# Test fails to build for these boards fails due to +# redefinition of define AES_COUNT in library, which +# is also defined in efm32 header files +BOARD_BLACKLIST := stk3600 stk3700 + +USEPKG += cryptoauthlib +USEMODULE += hashes + +include $(RIOTBASE)/Makefile.include diff --git a/tests/pkg_cryptoauthlib_compare_sha256/main.c b/tests/pkg_cryptoauthlib_compare_sha256/main.c new file mode 100644 index 0000000000..6cd744e921 --- /dev/null +++ b/tests/pkg_cryptoauthlib_compare_sha256/main.c @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2019 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 + * @{ + * + * @file + * @brief This test was written to compare the runtime of the RIOT software + * implementation and the CryptoAuth hardware implementation of SHA-256. + * + * @author Lena Boeckmann + * + * @} + */ + +#include +#include +#include + +#include "hashes/sha256.h" +#include "atca.h" +#include "atca_params.h" + +#define SHA256_HASH_SIZE (32) + +/** + * Function to call RIOT software implementation of SHA-256 + */ +static int test_riot_sha256(uint8_t *teststring, uint16_t len, + uint8_t *expected, + uint8_t *result) +{ + sha256_context_t ctx; + + sha256_init(&ctx); + sha256_update(&ctx, (void *)teststring, len); + sha256_final(&ctx, result); + return memcmp(expected, result, SHA256_HASH_SIZE); +} + +/** + * Function to call CryptoAuth hardware implementation of SHA-256 + */ +static int test_atca_sha(uint8_t *teststring, uint16_t len, uint8_t *expected, + uint8_t *result) +{ + atcab_sha_start(); + atcab_sha_end(result, len, teststring); + return memcmp(expected, result, SHA256_HASH_SIZE); +} + +int main(void) +{ + uint8_t teststring[] = "chili cheese fries"; + uint8_t expected[] = + { 0x36, 0x46, 0xEF, 0xD6, 0x27, 0x6C, 0x0D, 0xCB, 0x4B, 0x07, 0x73, 0x41, + 0x88, 0xF4, 0x17, 0xB4, 0x38, 0xAA, 0xCF, 0xC6, 0xAE, 0xEF, 0xFA, 0xBE, + 0xF3, 0xA8, 0x5D, 0x67, 0x42, 0x0D, 0xFE, 0xE5 }; + + uint8_t result[SHA256_HASH_SIZE]; /* +3 to fit 1 byte length and 2 bytes checksum */ + + memset(result, 0, SHA256_HASH_SIZE); /* alles in result auf 0 setzen */ + + uint16_t test_string_size = (sizeof(teststring) - 1); /* -1 to ignore \0 */ + + if (test_riot_sha256(teststring, test_string_size, expected, result) == 0) { + printf("RIOT SHA256: Success\n"); + } + else { + printf("RIOT SHA256: Failure.\n"); + } + atca_delay_us(10); + memset(result, 0, SHA256_HASH_SIZE); + + if (test_atca_sha(teststring, test_string_size, expected, result) == 0) { + printf("ATCA SHA256: Success\n"); + } + else { + printf("ATCA SHA256: Failure.\n"); + } + + return 0; +}