Skip to content

Commit d6d4547

Browse files
authored
Merge pull request #10 from pennam/sha-test
SHA256 add tests
2 parents 32922b4 + b600320 commit d6d4547

File tree

6 files changed

+109
-4
lines changed

6 files changed

+109
-4
lines changed

Diff for: .github/workflows/unit-tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ jobs:
3737
# A token is used to avoid intermittent spurious job failures caused by rate limiting.
3838
- name: Set up Codecov upload token
3939
run: |
40-
if [[ "${{ github.repository }}" == "arduino-libraries/ArduinoIoTCloud" ]]; then
40+
if [[ "${{ github.repository }}" == "arduino-libraries/Arduino_CloudUtils" ]]; then
4141
# In order to avoid uploads of data from forks, only use the token for runs in the parent repo.
4242
# Token is intentionally exposed.
4343
# See: https://community.codecov.com/t/upload-issues-unable-to-locate-build-via-github-actions-api/3954

Diff for: examples/sha256/sha256.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void setup() {
2525
Serial.println(hexEncode(sha, sizeof(sha)));
2626

2727
/* One-shot */
28-
arduino::sha256::oneshot(buffer, sizeof(buffer), sha);
28+
arduino::sha256::sha256(buffer, sizeof(buffer), sha);
2929
Serial.println(hexEncode(sha, sizeof(sha)));
3030
}
3131

Diff for: extras/test/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,14 @@ set(TEST_SRCS
2525
src/lzss/test_decoder.cpp
2626
src/crc32/test_crc32.cpp
2727
src/crc16/test_crc16.cpp
28+
src/sha256/test_sha256.cpp
2829
)
2930

3031
set(TEST_DUT_SRCS
3132
../../src/crc/crc32.cpp
3233
../../src/crc/crc16.cpp
34+
../../src/sha256/SHA256.cpp
35+
../../src/sha256/sha2.c
3336
)
3437

3538
##########################################################################

Diff for: extras/test/src/sha256/test_sha256.cpp

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#include <catch2/catch_test_macros.hpp>
12+
13+
#include <sha256/SHA256.h>
14+
#include <stdio.h>
15+
16+
#define SHA256_DIGEST_SIZE ( 256 / 8)
17+
#define SHA512_DIGEST_SIZE ( 512 / 8)
18+
19+
#ifdef TEST_VECTORS_LONG
20+
static void test_sha256_long_message(uint8_t *digest)
21+
{
22+
sha256_ctx ctx;
23+
uint8_t message[1000];
24+
int i;
25+
26+
memset(message, 'a', sizeof (message));
27+
28+
arduino::sha256::begin(&ctx);
29+
for (i = 0; i < 10000000; i++) {
30+
arduino::sha256::update(&ctx, message, sizeof (message));
31+
}
32+
arduino::sha256::finalize(&ctx, digest);
33+
}
34+
#endif
35+
36+
static void test_sha256_message4(uint8_t *digest)
37+
{
38+
/* Message of 929271 bytes */
39+
sha256_ctx ctx;
40+
uint8_t message[1000];
41+
int i;
42+
43+
memset(message, 'a', sizeof (message));
44+
arduino::sha256::begin(&ctx);
45+
for (i = 0; i < 929; i++) {
46+
arduino::sha256::update(&ctx, message, sizeof (message));
47+
}
48+
arduino::sha256::update(&ctx, message, 271);
49+
arduino::sha256::finalize(&ctx, digest);
50+
}
51+
52+
static int test(const char *vector, uint8_t *digest, uint32_t digest_size)
53+
{
54+
char output[2 * SHA512_DIGEST_SIZE + 1];
55+
int i;
56+
57+
output[2 * digest_size] = '\0';
58+
for (i = 0; i < (int) digest_size ; i++) {
59+
sprintf(output + 2 * i, "%02x", digest[i]);
60+
}
61+
62+
//printf("H: %s\n", output);
63+
return strcmp(vector, output);
64+
}
65+
66+
SCENARIO( "FIPS 180-2 Validation tests" ) {
67+
static const char *vectors[1][5] =
68+
{ /* SHA-256 */
69+
{
70+
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
71+
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
72+
"cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0",
73+
"8c14f43ad81026351e9b60025b5420e6072ff617f5c72145b179599211514947",
74+
"53748286337dbe36f5df22e7ef1af3ad71530398cf569adc7eb5fefa7af7003c",
75+
}
76+
};
77+
78+
static const char message1[] = "abc";
79+
static const char message2[] = "abcdbcdecdefdefgefghfghighijhi"
80+
"jkijkljklmklmnlmnomnopnopq";
81+
uint8_t *message3;
82+
uint32_t message3_len = 1000000;
83+
uint8_t digest[SHA512_DIGEST_SIZE];
84+
85+
message3 = (uint8_t *)malloc(message3_len);
86+
REQUIRE(message3 != nullptr);
87+
memset(message3, 'a', message3_len);
88+
89+
arduino::sha256::sha256((const uint8_t *) message1, strlen(message1), digest);
90+
REQUIRE(test(vectors[0][0], digest, SHA256_DIGEST_SIZE) == 0);
91+
arduino::sha256::sha256((const uint8_t *) message2, strlen(message2), digest);
92+
REQUIRE(test(vectors[0][1], digest, SHA256_DIGEST_SIZE) == 0);
93+
arduino::sha256::sha256(message3, message3_len, digest);
94+
REQUIRE(test(vectors[0][2], digest, SHA256_DIGEST_SIZE) == 0);
95+
test_sha256_message4(digest);
96+
REQUIRE(test(vectors[0][3], digest, SHA256_DIGEST_SIZE) == 0);
97+
#ifdef TEST_VECTORS_LONG
98+
test_sha256_long_message(digest);
99+
REQUIRE(test(vectors[0][4], digest, SHA256_DIGEST_SIZE) == 0);
100+
#endif
101+
free(message3);
102+
}

Diff for: src/bpid/bpid.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace arduino { namespace bpid {
3939
return String("");
4040
}
4141
uint8_t out[SHA256::HASH_SIZE];
42-
arduino::sha256::oneshot(data, sizeof(data), out);
42+
arduino::sha256::sha256(data, sizeof(data), out);
4343
return arduino::hex::encode(out, sizeof(out));
4444
return String("");
4545
}

Diff for: src/sha256/SHA256.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace arduino { namespace sha256 {
3838
inline void finalize(sha256_ctx * ctx, uint8_t digest[SHA256_DIGEST_SIZE]) {
3939
return sha256_final(ctx, digest);
4040
}
41-
inline void oneshot(const unsigned char *input, unsigned int ilen, unsigned char *output) {
41+
inline void sha256(const unsigned char *input, unsigned int ilen, unsigned char *output) {
4242
::sha256(input, ilen, output);
4343
}
4444

0 commit comments

Comments
 (0)