From 447f945ee60832926fa23c99e34725367b37f6b8 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 3 Dec 2024 10:18:35 +0100 Subject: [PATCH 1/4] sha256: add tests --- extras/test/CMakeLists.txt | 3 + extras/test/src/sha256/test_sha256.cpp | 105 +++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 extras/test/src/sha256/test_sha256.cpp diff --git a/extras/test/CMakeLists.txt b/extras/test/CMakeLists.txt index 600839e..0916ea6 100644 --- a/extras/test/CMakeLists.txt +++ b/extras/test/CMakeLists.txt @@ -25,11 +25,14 @@ set(TEST_SRCS src/lzss/test_decoder.cpp src/crc32/test_crc32.cpp src/crc16/test_crc16.cpp + src/sha256/test_sha256.cpp ) set(TEST_DUT_SRCS ../../src/crc/crc32.cpp ../../src/crc/crc16.cpp + ../../src/sha256/SHA256.cpp + ../../src/sha256/sha2.c ) ########################################################################## diff --git a/extras/test/src/sha256/test_sha256.cpp b/extras/test/src/sha256/test_sha256.cpp new file mode 100644 index 0000000..de92cbb --- /dev/null +++ b/extras/test/src/sha256/test_sha256.cpp @@ -0,0 +1,105 @@ +/* + This file is part of the Arduino_CloudUtils library. + + Copyright (c) 2024 Arduino SA + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. +*/ + +#include + +#include +#include + +#define SHA256_DIGEST_SIZE ( 256 / 8) +#define SHA512_DIGEST_SIZE ( 512 / 8) + +#ifdef TEST_VECTORS_LONG +static void test_sha256_long_message(uint8_t *digest) +{ + sha256_ctx ctx; + uint8_t message[1000]; + int i; + + memset(message, 'a', sizeof (message)); + + arduino::sha256::begin(&ctx); + for (i = 0; i < 10000000; i++) { + arduino::sha256::update(&ctx, message, sizeof (message)); + } + arduino::sha256::finalize(&ctx, digest); +} +#endif + +static void test_sha256_message4(uint8_t *digest) +{ + /* Message of 929271 bytes */ + + sha256_ctx ctx; + uint8_t message[1000]; + int i; + + memset(message, 'a', sizeof (message)); + + arduino::sha256::begin(&ctx); + for (i = 0; i < 929; i++) { + arduino::sha256::update(&ctx, message, sizeof (message)); + } + arduino::sha256::update(&ctx, message, 271); + + arduino::sha256::finalize(&ctx, digest); +} + +static int test(const char *vector, uint8_t *digest, uint32_t digest_size) +{ + char output[2 * SHA512_DIGEST_SIZE + 1]; + int i; + + output[2 * digest_size] = '\0'; + + for (i = 0; i < (int) digest_size ; i++) { + sprintf(output + 2 * i, "%02x", digest[i]); + } + + //printf("H: %s\n", output); + return strcmp(vector, output); +} + +SCENARIO( "FIPS 180-2 Validation tests" ) { + static const char *vectors[1][5] = + { /* SHA-256 */ + { + "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad", + "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1", + "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0", + "8c14f43ad81026351e9b60025b5420e6072ff617f5c72145b179599211514947", + "53748286337dbe36f5df22e7ef1af3ad71530398cf569adc7eb5fefa7af7003c", + } + }; + + static const char message1[] = "abc"; + static const char message2[] = "abcdbcdecdefdefgefghfghighijhi" + "jkijkljklmklmnlmnomnopnopq"; + uint8_t *message3; + uint32_t message3_len = 1000000; + uint8_t digest[SHA512_DIGEST_SIZE]; + + message3 = (uint8_t *)malloc(message3_len); + REQUIRE(message3 != nullptr); + memset(message3, 'a', message3_len); + + arduino::sha256::sha256((const uint8_t *) message1, strlen(message1), digest); + REQUIRE(test(vectors[0][0], digest, SHA256_DIGEST_SIZE) == 0); + arduino::sha256::sha256((const uint8_t *) message2, strlen(message2), digest); + REQUIRE(test(vectors[0][1], digest, SHA256_DIGEST_SIZE) == 0); + arduino::sha256::sha256(message3, message3_len, digest); + REQUIRE(test(vectors[0][2], digest, SHA256_DIGEST_SIZE) == 0); + test_sha256_message4(digest); + REQUIRE(test(vectors[0][3], digest, SHA256_DIGEST_SIZE) == 0); +#ifdef TEST_VECTORS_LONG + test_sha256_long_message(digest); + REQUIRE(test(vectors[0][4], digest, SHA256_DIGEST_SIZE) == 0); +#endif +} From 0fd01c080c22eb7c2c24f2d01ae61c8e3313f0d7 Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 3 Dec 2024 10:18:56 +0100 Subject: [PATCH 2/4] sha256: rename oneshot to sha256 --- examples/sha256/sha256.ino | 2 +- src/bpid/bpid.cpp | 2 +- src/sha256/SHA256.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/sha256/sha256.ino b/examples/sha256/sha256.ino index 1e904d1..bddbd49 100644 --- a/examples/sha256/sha256.ino +++ b/examples/sha256/sha256.ino @@ -25,7 +25,7 @@ void setup() { Serial.println(hexEncode(sha, sizeof(sha))); /* One-shot */ - arduino::sha256::oneshot(buffer, sizeof(buffer), sha); + arduino::sha256::sha256(buffer, sizeof(buffer), sha); Serial.println(hexEncode(sha, sizeof(sha))); } diff --git a/src/bpid/bpid.cpp b/src/bpid/bpid.cpp index 74c5705..f7425c6 100644 --- a/src/bpid/bpid.cpp +++ b/src/bpid/bpid.cpp @@ -39,7 +39,7 @@ namespace arduino { namespace bpid { return String(""); } uint8_t out[SHA256::HASH_SIZE]; - arduino::sha256::oneshot(data, sizeof(data), out); + arduino::sha256::sha256(data, sizeof(data), out); return arduino::hex::encode(out, sizeof(out)); } diff --git a/src/sha256/SHA256.h b/src/sha256/SHA256.h index d10ed49..a69dda2 100644 --- a/src/sha256/SHA256.h +++ b/src/sha256/SHA256.h @@ -38,7 +38,7 @@ namespace arduino { namespace sha256 { inline void finalize(sha256_ctx * ctx, uint8_t digest[SHA256_DIGEST_SIZE]) { return sha256_final(ctx, digest); } - inline void oneshot(const unsigned char *input, unsigned int ilen, unsigned char *output) { + inline void sha256(const unsigned char *input, unsigned int ilen, unsigned char *output) { ::sha256(input, ilen, output); } From 2b468eb52c25310b176d3fbbb241db411a7a1b6d Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 3 Dec 2024 10:22:55 +0100 Subject: [PATCH 3/4] Unit tests use codecov token --- .github/workflows/unit-tests.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index 8857094..c55f4ac 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -37,7 +37,7 @@ jobs: # A token is used to avoid intermittent spurious job failures caused by rate limiting. - name: Set up Codecov upload token run: | - if [[ "${{ github.repository }}" == "arduino-libraries/ArduinoIoTCloud" ]]; then + if [[ "${{ github.repository }}" == "arduino-libraries/Arduino_CloudUtils" ]]; then # In order to avoid uploads of data from forks, only use the token for runs in the parent repo. # Token is intentionally exposed. # See: https://community.codecov.com/t/upload-issues-unable-to-locate-build-via-github-actions-api/3954 From b6003201e6df91e3145f23ce0af567734eb0b9cf Mon Sep 17 00:00:00 2001 From: pennam Date: Tue, 3 Dec 2024 10:44:52 +0100 Subject: [PATCH 4/4] sha256: tests add missing free --- extras/test/src/sha256/test_sha256.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/extras/test/src/sha256/test_sha256.cpp b/extras/test/src/sha256/test_sha256.cpp index de92cbb..216d732 100644 --- a/extras/test/src/sha256/test_sha256.cpp +++ b/extras/test/src/sha256/test_sha256.cpp @@ -36,19 +36,16 @@ static void test_sha256_long_message(uint8_t *digest) static void test_sha256_message4(uint8_t *digest) { /* Message of 929271 bytes */ - sha256_ctx ctx; uint8_t message[1000]; int i; memset(message, 'a', sizeof (message)); - arduino::sha256::begin(&ctx); for (i = 0; i < 929; i++) { arduino::sha256::update(&ctx, message, sizeof (message)); } arduino::sha256::update(&ctx, message, 271); - arduino::sha256::finalize(&ctx, digest); } @@ -58,7 +55,6 @@ static int test(const char *vector, uint8_t *digest, uint32_t digest_size) int i; output[2 * digest_size] = '\0'; - for (i = 0; i < (int) digest_size ; i++) { sprintf(output + 2 * i, "%02x", digest[i]); } @@ -102,4 +98,5 @@ SCENARIO( "FIPS 180-2 Validation tests" ) { test_sha256_long_message(digest); REQUIRE(test(vectors[0][4], digest, SHA256_DIGEST_SIZE) == 0); #endif + free(message3); }