forked from canokeys/canokey-crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sha.c
More file actions
195 lines (181 loc) · 7 KB
/
test_sha.c
File metadata and controls
195 lines (181 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// SPDX-License-Identifier: Apache-2.0
// clang-format off
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <cmocka.h>
// clang-format on
#include <hmac.h>
#include <sha.h>
#include <sha3.h>
static void test_sha1(void **state) {
(void)state;
uint8_t buf[20];
uint8_t expected[] = {0x5b, 0xa9, 0x3c, 0x9d, 0xb0, 0xcf, 0xf9, 0x3f, 0x52, 0xb5,
0x21, 0xd7, 0x42, 0x0e, 0x43, 0xf6, 0xed, 0xa2, 0x78, 0x4f};
buf[0] = 0;
sha1_raw(buf, 1, buf);
for (int i = 0; i != 20; ++i) {
assert_int_equal(buf[i], expected[i]);
}
}
static void test_sha256(void **state) {
(void)state;
uint8_t buf[32];
uint8_t expected[] = {0x6e, 0x34, 0x0b, 0x9c, 0xff, 0xb3, 0x7a, 0x98, 0x9c, 0xa5, 0x44, 0xe6, 0xbb, 0x78, 0x0a, 0x2c,
0x78, 0x90, 0x1d, 0x3f, 0xb3, 0x37, 0x38, 0x76, 0x85, 0x11, 0xa3, 0x06, 0x17, 0xaf, 0xa0, 0x1d};
buf[0] = 0;
sha256_raw(buf, 1, buf);
for (int i = 0; i != 32; ++i) {
assert_int_equal(buf[i], expected[i]);
}
}
static void test_keccak256(void **state) {
(void)state;
uint8_t buf[32];
uint8_t expected[] = {0xbc, 0x36, 0x78, 0x9e, 0x7a, 0x1e, 0x28, 0x14, 0x36, 0x46, 0x42, 0x29, 0x82, 0x8f, 0x81, 0x7d,
0x66, 0x12, 0xf7, 0xb4, 0x77, 0xd6, 0x65, 0x91, 0xff, 0x96, 0xa9, 0xe0, 0x64, 0xbc, 0xc9, 0x8a};
buf[0] = 0;
sha3_ctx_t ctx;
keccak_256_init(&ctx);
keccak_update(&ctx, buf, 1);
keccak_finalize(&ctx, buf);
for (int i = 0; i != 32; ++i) {
assert_int_equal(buf[i], expected[i]);
}
}
static void test_sha3_256(void **state) {
(void)state;
uint8_t buf[32];
uint8_t expected[] = {0x5d, 0x53, 0x46, 0x9f, 0x20, 0xfe, 0xf4, 0xf8, 0xea, 0xb5, 0x2b, 0x88, 0x04, 0x4e, 0xde, 0x69,
0xc7, 0x7a, 0x6a, 0x68, 0xa6, 0x07, 0x28, 0x60, 0x9f, 0xc4, 0xa6, 0x5f, 0xf5, 0x31, 0xe7, 0xd0};
buf[0] = 0;
sha3_ctx_t ctx;
sha3_256_init(&ctx);
sha3_update(&ctx, buf, 1);
sha3_finalize(&ctx, buf);
for (int i = 0; i != 32; ++i) {
assert_int_equal(buf[i], expected[i]);
}
}
static void test_sha3_256_abc(void **state) {
(void)state;
uint8_t expected[] = {0x3a, 0x98, 0x5d, 0xa7, 0x4f, 0xe2, 0x25, 0xb2, 0x04, 0x5c, 0x17, 0x2d, 0x6b, 0xd3, 0x90, 0xbd,
0x85, 0x5f, 0x08, 0x6e, 0x3e, 0x9d, 0x52, 0x5b, 0x46, 0xbf, 0xe2, 0x45, 0x11, 0x43, 0x15, 0x32};
uint8_t out[32];
sha3_256_raw((const unsigned char *)"abc", 3, out);
for (int i = 0; i != 32; ++i) {
assert_int_equal(out[i], expected[i]);
}
}
static void test_shake128_empty(void **state) {
(void)state;
uint8_t expected[] = {0x7f, 0x9c, 0x2b, 0xa4, 0xe8, 0x8f, 0x82, 0x7d, 0x61, 0x60, 0x45, 0x50, 0x76, 0x05, 0x85, 0x3e,
0xd7, 0x3b, 0x80, 0x93, 0xf6, 0xef, 0xbc, 0x88, 0xeb, 0x1a, 0x6e, 0xac, 0xfa, 0x66, 0xef, 0x26};
uint8_t out[32];
shake128_raw((const unsigned char *)"", 0, out, 32);
for (int i = 0; i != 32; ++i) {
assert_int_equal(out[i], expected[i]);
}
}
static void test_shake256_abc(void **state) {
(void)state;
uint8_t expected[] = {0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77, 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee, 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39};
uint8_t out[32];
shake256_raw((const unsigned char *)"abc", 3, out, 32);
for (int i = 0; i != 32; ++i) {
assert_int_equal(out[i], expected[i]);
}
}
static void test_shake256_xof_long(void **state) {
(void)state;
uint8_t expected[] = {
0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77, 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee, 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86, 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f, 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4,
};
uint8_t out[64];
shake256_raw((const unsigned char *)"abc", 3, out, 64);
for (int i = 0; i != 64; ++i) {
assert_int_equal(out[i], expected[i]);
}
}
static void test_shake256_incremental_squeeze(void **state) {
(void)state;
uint8_t expected[] = {
0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77, 0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee, 0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39,
0xd5, 0xa1, 0x5b, 0xef, 0x18, 0x6a, 0x53, 0x86, 0xc7, 0x57, 0x44, 0xc0, 0x52, 0x7e, 0x1f, 0xaa,
0x9f, 0x87, 0x26, 0xe4, 0x62, 0xa1, 0x2a, 0x4f, 0xeb, 0x06, 0xbd, 0x88, 0x01, 0xe7, 0x51, 0xe4,
};
uint8_t out[64];
sha3_ctx_t ctx;
shake256_init(&ctx);
shake_update(&ctx, (const unsigned char *)"abc", 3);
shake_finalize(&ctx);
shake_squeeze(&ctx, out, 32);
shake_squeeze(&ctx, out + 32, 32);
for (int i = 0; i != 64; ++i) {
assert_int_equal(out[i], expected[i]);
}
}
static void test_shake128_long_xof(void **state) {
(void)state;
uint8_t expected_head[] = {0x58, 0x81, 0x09, 0x2d, 0xd8, 0x18, 0xbf, 0x5c, 0xf8, 0xa3, 0xdd,
0xb7, 0x93, 0xfb, 0xcb, 0xa7, 0x40, 0x97, 0xd5, 0xc5, 0x26, 0xa6,
0xd3, 0x5f, 0x97, 0xb8, 0x33, 0x51, 0x94, 0x0f, 0x2c, 0xc8};
uint8_t out[256];
shake128_raw((const unsigned char *)"abc", 3, out, 256);
for (int i = 0; i != 32; ++i) {
assert_int_equal(out[i], expected_head[i]);
}
}
static void test_hmac_sha1(void **state) {
(void)state;
uint8_t key[4] = {0xde, 0xad, 0xbe, 0xef};
uint8_t buf[20];
uint8_t expected[] = {0xf0, 0xfb, 0x6b, 0x43, 0x7a, 0x6a, 0x18, 0x3b, 0xc3, 0x28,
0x8d, 0xc6, 0xd4, 0xa1, 0x03, 0x34, 0x26, 0x5e, 0x47, 0x0f};
buf[0] = 0;
HMAC_SHA1_CTX ctx;
hmac_sha1_Init(&ctx, key, sizeof(key));
hmac_sha1_Update(&ctx, buf, 1);
hmac_sha1_Final(&ctx, buf);
for (int i = 0; i != 20; ++i) {
assert_int_equal(buf[i], expected[i]);
}
}
static void test_hmac_sha256(void **state) {
(void)state;
uint8_t key[4] = {0xde, 0xad, 0xbe, 0xef};
uint8_t buf[32];
uint8_t expected[] = {0xc0, 0xd6, 0xf7, 0xec, 0x99, 0xb6, 0xfb, 0xfe, 0xc1, 0xe0, 0xbe, 0xd5, 0xa1, 0x0d, 0xd1, 0xe5,
0xcd, 0xe7, 0xbe, 0x11, 0x4c, 0x41, 0x71, 0x81, 0x69, 0xc7, 0xd6, 0x3e, 0x67, 0x05, 0x6d, 0x28};
buf[0] = 0;
HMAC_SHA256_CTX ctx;
hmac_sha256_Init(&ctx, key, sizeof(key));
hmac_sha256_Update(&ctx, buf, 1);
hmac_sha256_Final(&ctx, buf);
for (int i = 0; i != 32; ++i) {
assert_int_equal(buf[i], expected[i]);
}
}
int main() {
const struct CMUnitTest tests[] = {
cmocka_unit_test(test_sha1),
cmocka_unit_test(test_sha256),
cmocka_unit_test(test_keccak256),
cmocka_unit_test(test_sha3_256),
cmocka_unit_test(test_sha3_256_abc),
cmocka_unit_test(test_shake128_empty),
cmocka_unit_test(test_shake256_abc),
cmocka_unit_test(test_shake256_xof_long),
cmocka_unit_test(test_shake256_incremental_squeeze),
cmocka_unit_test(test_shake128_long_xof),
cmocka_unit_test(test_hmac_sha1),
cmocka_unit_test(test_hmac_sha256),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}