Skip to content

Commit 447f945

Browse files
committed
sha256: add tests
1 parent a09612c commit 447f945

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

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
##########################################################################
+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
40+
sha256_ctx ctx;
41+
uint8_t message[1000];
42+
int i;
43+
44+
memset(message, 'a', sizeof (message));
45+
46+
arduino::sha256::begin(&ctx);
47+
for (i = 0; i < 929; i++) {
48+
arduino::sha256::update(&ctx, message, sizeof (message));
49+
}
50+
arduino::sha256::update(&ctx, message, 271);
51+
52+
arduino::sha256::finalize(&ctx, digest);
53+
}
54+
55+
static int test(const char *vector, uint8_t *digest, uint32_t digest_size)
56+
{
57+
char output[2 * SHA512_DIGEST_SIZE + 1];
58+
int i;
59+
60+
output[2 * digest_size] = '\0';
61+
62+
for (i = 0; i < (int) digest_size ; i++) {
63+
sprintf(output + 2 * i, "%02x", digest[i]);
64+
}
65+
66+
//printf("H: %s\n", output);
67+
return strcmp(vector, output);
68+
}
69+
70+
SCENARIO( "FIPS 180-2 Validation tests" ) {
71+
static const char *vectors[1][5] =
72+
{ /* SHA-256 */
73+
{
74+
"ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
75+
"248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1",
76+
"cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0",
77+
"8c14f43ad81026351e9b60025b5420e6072ff617f5c72145b179599211514947",
78+
"53748286337dbe36f5df22e7ef1af3ad71530398cf569adc7eb5fefa7af7003c",
79+
}
80+
};
81+
82+
static const char message1[] = "abc";
83+
static const char message2[] = "abcdbcdecdefdefgefghfghighijhi"
84+
"jkijkljklmklmnlmnomnopnopq";
85+
uint8_t *message3;
86+
uint32_t message3_len = 1000000;
87+
uint8_t digest[SHA512_DIGEST_SIZE];
88+
89+
message3 = (uint8_t *)malloc(message3_len);
90+
REQUIRE(message3 != nullptr);
91+
memset(message3, 'a', message3_len);
92+
93+
arduino::sha256::sha256((const uint8_t *) message1, strlen(message1), digest);
94+
REQUIRE(test(vectors[0][0], digest, SHA256_DIGEST_SIZE) == 0);
95+
arduino::sha256::sha256((const uint8_t *) message2, strlen(message2), digest);
96+
REQUIRE(test(vectors[0][1], digest, SHA256_DIGEST_SIZE) == 0);
97+
arduino::sha256::sha256(message3, message3_len, digest);
98+
REQUIRE(test(vectors[0][2], digest, SHA256_DIGEST_SIZE) == 0);
99+
test_sha256_message4(digest);
100+
REQUIRE(test(vectors[0][3], digest, SHA256_DIGEST_SIZE) == 0);
101+
#ifdef TEST_VECTORS_LONG
102+
test_sha256_long_message(digest);
103+
REQUIRE(test(vectors[0][4], digest, SHA256_DIGEST_SIZE) == 0);
104+
#endif
105+
}

0 commit comments

Comments
 (0)