-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_sha3.c
153 lines (121 loc) · 4.76 KB
/
test_sha3.c
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
// test_sha3.c
// 2018-06-29 Markku-Juhani O. Saarinen <[email protected]>
// (c) 2018 Copyright Teserakt AG
// Originally from main.c in tiny_sha3 (dated 2011-11-19)
#include <stdio.h>
#include <string.h>
#include "sha3.h"
// read a hex string, return byte length or -1 on error.
static int test_hexdigit(char ch)
{
if (ch >= '0' && ch <= '9')
return ch - '0';
if (ch >= 'A' && ch <= 'F')
return ch - 'A' + 10;
if (ch >= 'a' && ch <= 'f')
return ch - 'a' + 10;
return -1;
}
static int test_readhex(uint8_t *buf, const char *str, int maxbytes)
{
int i, h, l;
for (i = 0; i < maxbytes; i++) {
h = test_hexdigit(str[2 * i]);
if (h < 0)
return i;
l = test_hexdigit(str[2 * i + 1]);
if (l < 0)
return i;
buf[i] = (h << 4) + l;
}
return i;
}
// returns zero on success, nonzero + stderr messages on failure
int test_sha3()
{
// message / digest pairs, lifted from ShortMsgKAT_SHA3-xxx.txt files
// in the official package: https://github.com/gvanas/KeccakCodePackage
const char *testvec[][2] = {
{ // SHA3-224, corner case with 0-length message
"",
"6B4E03423667DBB73B6E15454F0EB1ABD4597F9A1B078E3F5B5A6BC7"
},
{ // SHA3-256, short message
"9F2FCC7C90DE090D6B87CD7E9718C1EA6CB21118FC2D5DE9F97E5DB6AC1E9C10",
"2F1A5F7159E34EA19CDDC70EBF9B81F1A66DB40615D7EAD3CC1F1B954D82A3AF"
},
{ // SHA3-384, exact block size
"E35780EB9799AD4C77535D4DDB683CF33EF367715327CF4C4A58ED9CBDCDD486"
"F669F80189D549A9364FA82A51A52654EC721BB3AAB95DCEB4A86A6AFA93826D"
"B923517E928F33E3FBA850D45660EF83B9876ACCAFA2A9987A254B137C6E140A"
"21691E1069413848",
"D1C0FA85C8D183BEFF99AD9D752B263E286B477F79F0710B0103170173978133"
"44B99DAF3BB7B1BC5E8D722BAC85943A"
},
{ // SHA3-512, multiblock message
"3A3A819C48EFDE2AD914FBF00E18AB6BC4F14513AB27D0C178A188B61431E7F5"
"623CB66B23346775D386B50E982C493ADBBFC54B9A3CD383382336A1A0B2150A"
"15358F336D03AE18F666C7573D55C4FD181C29E6CCFDE63EA35F0ADF5885CFC0"
"A3D84A2B2E4DD24496DB789E663170CEF74798AA1BBCD4574EA0BBA40489D764"
"B2F83AADC66B148B4A0CD95246C127D5871C4F11418690A5DDF01246A0C80A43"
"C70088B6183639DCFDA4125BD113A8F49EE23ED306FAAC576C3FB0C1E256671D"
"817FC2534A52F5B439F72E424DE376F4C565CCA82307DD9EF76DA5B7C4EB7E08"
"5172E328807C02D011FFBF33785378D79DC266F6A5BE6BB0E4A92ECEEBAEB1",
"6E8B8BD195BDD560689AF2348BDC74AB7CD05ED8B9A57711E9BE71E9726FDA45"
"91FEE12205EDACAF82FFBBAF16DFF9E702A708862080166C2FF6BA379BC7FFC2"
}
};
int i, fails, msg_len, sha_len;
uint8_t sha[64], buf[64], msg[256];
fails = 0;
for (i = 0; i < 4; i++) {
memset(sha, 0, sizeof(sha));
memset(buf, 0, sizeof(buf));
memset(msg, 0, sizeof(msg));
msg_len = test_readhex(msg, testvec[i][0], sizeof(msg));
sha_len = test_readhex(sha, testvec[i][1], sizeof(sha));
sha3(msg, msg_len, buf, sha_len);
if (memcmp(sha, buf, sha_len) != 0)
fails++;
}
return fails;
}
// test for SHAKE128 and SHAKE256
int test_shake()
{
// Test vectors have bytes 480..511 of XOF output for given inputs.
// From http://csrc.nist.gov/groups/ST/toolkit/examples.html#aHashing
const char *testhex[4] = {
// SHAKE128, message of length 0
"43E41B45A653F2A5C4492C1ADD544512DDA2529833462B71A41A45BE97290B6F",
// SHAKE256, message of length 0
"AB0BAE316339894304E35877B0C28A9B1FD166C796B9CC258A064A8F57E27F2A",
// SHAKE128, 1600-bit test pattern
"44C9FB359FD56AC0A9A75A743CFF6862F17D7259AB075216C0699511643B6439",
// SHAKE256, 1600-bit test pattern
"6A1A9D7846436E4DCA5728B6F760EEF0CA92BF0BE5615E96959D767197A0BEEB"
};
int i, j, fails;
uint8_t buf[32], ref[32];
fails = 0;
for (i = 0; i < 4; i++) {
if ((i & 1) == 0) { // test each twice
shake128_init(&common_sha3_ctx);
} else {
shake256_init(&common_sha3_ctx);
}
if (i >= 2) { // 1600-bit test pattern
memset(buf, 0xA3, 20);
for (j = 0; j < 200; j += 20)
shake_update(&common_sha3_ctx, buf, 20);
}
shake_xof(&common_sha3_ctx); // switch to extensible output
for (j = 0; j < 512; j += 32) // output. discard bytes 0..479
shake_out(&common_sha3_ctx, buf, 32);
// compare to reference
test_readhex(ref, testhex[i], sizeof(ref));
if (memcmp(buf, ref, 32) != 0)
fails++;
}
return fails;
}