summaryrefslogtreecommitdiff
path: root/sha3.c
diff options
context:
space:
mode:
Diffstat (limited to 'sha3.c')
-rw-r--r--sha3.c192
1 files changed, 190 insertions, 2 deletions
diff --git a/sha3.c b/sha3.c
index 33294f6..972a77a 100644
--- a/sha3.c
+++ b/sha3.c
@@ -3304,7 +3304,7 @@ static void test_tuplehash128(void) {
.custom_len = 12,
.exp = {
0xE6, 0x0F, 0x20, 0x2C, 0x89, 0xA2, 0x63, 0x1E, 0xDA, 0x8D, 0x4C, 0x58, 0x8C, 0xA5, 0xFD, 0x07,
- 0xF3, 0x9E, 0x51, 0x51, 0x99, 0x8D, 0xEC, 0xCF, 0x97, 0x3A, 0xDB, 0x38, 0x04, 0xBB, 0x6E, 0x84,
+ 0xF3, 0x9E, 0x51, 0x51, 0x99, 0x8D, 0xEC, 0xCF, 0x97, 0x3A, 0xDB, 0x38, 0x04, 0xBB, 0x6E, 0x84,
},
}};
@@ -3339,6 +3339,96 @@ static void test_tuplehash128(void) {
}
}
+static void test_tuplehash128_xof(void) {
+ static const struct {
+ const char *name; // test name
+ const uint8_t data[256]; // string data
+ const size_t pairs[10]; // string ofs/len pairs
+ const size_t num_strs; // number of strings
+ const uint8_t custom[256]; // custom name
+ const size_t custom_len; // custom name length
+ const uint8_t exp[32]; // expected hash
+ } tests[] = {{
+ // src: https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/TupleHashXOF_samples.pdf
+ .name = "TupleHashXOF Sample #1",
+ .data = { 0x00, 0x01, 0x02, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15 },
+ .pairs = {
+ 0, 3,
+ 3, 6,
+ },
+ .num_strs = 2,
+ .custom = "",
+ .custom_len = 0,
+ .exp = {
+ 0x2F, 0x10, 0x3C, 0xD7, 0xC3, 0x23, 0x20, 0x35, 0x34, 0x95, 0xC6, 0x8D, 0xE1, 0xA8, 0x12, 0x92,
+ 0x45, 0xC6, 0x32, 0x5F, 0x6F, 0x2A, 0x3D, 0x60, 0x8D, 0x92, 0x17, 0x9C, 0x96, 0xE6, 0x84, 0x88,
+ },
+ }, {
+ .name = "TupleHashXOF Sample #2",
+ .data = { 0x00, 0x01, 0x02, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15 },
+ .pairs = {
+ 0, 3,
+ 3, 6,
+ },
+ .num_strs = 2,
+ .custom = "My Tuple App",
+ .custom_len = 12,
+ .exp = {
+ 0x3F, 0xC8, 0xAD, 0x69, 0x45, 0x31, 0x28, 0x29, 0x28, 0x59, 0xA1, 0x8B, 0x6C, 0x67, 0xD7, 0xAD,
+ 0x85, 0xF0, 0x1B, 0x32, 0x81, 0x5E, 0x22, 0xCE, 0x83, 0x9C, 0x49, 0xEC, 0x37, 0x4E, 0x9B, 0x9A,
+ },
+ }, {
+ .name = "TupleHashXOF Sample #3",
+ .data = {
+ 0x00, 0x01, 0x02,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
+ },
+ .pairs = {
+ 0, 3,
+ 3, 6,
+ 9, 9,
+ },
+ .num_strs = 3,
+ .custom = "My Tuple App",
+ .custom_len = 12,
+ .exp = {
+ 0x90, 0x0F, 0xE1, 0x6C, 0xAD, 0x09, 0x8D, 0x28, 0xE7, 0x4D, 0x63, 0x2E, 0xD8, 0x52, 0xF9, 0x9D,
+ 0xAA, 0xB7, 0xF7, 0xDF, 0x4D, 0x99, 0xE7, 0x75, 0x65, 0x78, 0x85, 0xB4, 0xBF, 0x76, 0xD6, 0xF8,
+ },
+ }};
+
+ for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+ // build strings
+ tuplehash_str_t strs[10] = { 0 };
+ for (size_t j = 0; j < tests[i].num_strs; j++) {
+ strs[j].ptr = tests[i].data + tests[i].pairs[2 * j];
+ strs[j].len = tests[i].pairs[2 * j + 1];
+ }
+
+ // build params
+ const tuplehash_params_t params = {
+ .strs = strs,
+ .num_strs = tests[i].num_strs,
+ .custom = tests[i].custom,
+ .custom_len = tests[i].custom_len,
+ };
+
+ // run
+ uint8_t got[32];
+ tuplehash128_xof_once(params, got, sizeof(got));
+
+ // check
+ if (memcmp(got, tests[i].exp, sizeof(got))) {
+ fprintf(stderr, "test_tuplehash128_xof(\"%s\") failed, got:\n", tests[i].name);
+ dump_hex(stderr, got, 32);
+
+ fprintf(stderr, "exp:\n");
+ dump_hex(stderr, tests[i].exp, 32);
+ }
+ }
+}
+
static void test_tuplehash256(void) {
static const struct {
const char *name; // test name
@@ -3379,7 +3469,7 @@ static void test_tuplehash256(void) {
0x14, 0x7C, 0x21, 0x91, 0xD5, 0xED, 0x7E, 0xFD, 0x98, 0xDB, 0xD9, 0x6D, 0x7A, 0xB5, 0xA1, 0x16,
0x92, 0x57, 0x6F, 0x5F, 0xE2, 0xA5, 0x06, 0x5F, 0x3E, 0x33, 0xDE, 0x6B, 0xBA, 0x9F, 0x3A, 0xA1,
0xC4, 0xE9, 0xA0, 0x68, 0xA2, 0x89, 0xC6, 0x1C, 0x95, 0xAA, 0xB3, 0x0A, 0xEE, 0x1E, 0x41, 0x0B,
- 0x0B, 0x60, 0x7D, 0xE3, 0x62, 0x0E, 0x24, 0xA4, 0xE3, 0xBF, 0x98, 0x52, 0xA1, 0xD4, 0x36, 0x7E,
+ 0x0B, 0x60, 0x7D, 0xE3, 0x62, 0x0E, 0x24, 0xA4, 0xE3, 0xBF, 0x98, 0x52, 0xA1, 0xD4, 0x36, 0x7E,
},
}, {
.name = "TupleHash Sample #6",
@@ -3435,6 +3525,102 @@ static void test_tuplehash256(void) {
}
}
+static void test_tuplehash256_xof(void) {
+ static const struct {
+ const char *name; // test name
+ const uint8_t data[256]; // string data
+ const size_t pairs[10]; // string ofs/len pairs
+ const size_t num_strs; // number of strings
+ const uint8_t custom[256]; // custom name
+ const size_t custom_len; // custom name length
+ const uint8_t exp[64]; // expected hash
+ } tests[] = {{
+ // src: https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/TupleHashXOF_samples.pdf
+ .name = "TupleHashXOF Sample #4",
+ .data = { 0x00, 0x01, 0x02, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15 },
+ .pairs = {
+ 0, 3,
+ 3, 6,
+ },
+ .num_strs = 2,
+ .custom = "",
+ .custom_len = 0,
+ .exp = {
+ 0x03, 0xDE, 0xD4, 0x61, 0x0E, 0xD6, 0x45, 0x0A, 0x1E, 0x3F, 0x8B, 0xC4, 0x49, 0x51, 0xD1, 0x4F,
+ 0xBC, 0x38, 0x4A, 0xB0, 0xEF, 0xE5, 0x7B, 0x00, 0x0D, 0xF6, 0xB6, 0xDF, 0x5A, 0xAE, 0x7C, 0xD5,
+ 0x68, 0xE7, 0x73, 0x77, 0xDA, 0xF1, 0x3F, 0x37, 0xEC, 0x75, 0xCF, 0x5F, 0xC5, 0x98, 0xB6, 0x84,
+ 0x1D, 0x51, 0xDD, 0x20, 0x7C, 0x99, 0x1C, 0xD4, 0x5D, 0x21, 0x0B, 0xA6, 0x0A, 0xC5, 0x2E, 0xB9,
+ },
+ }, {
+ .name = "TupleHashXOF Sample #5",
+ .data = { 0x00, 0x01, 0x02, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15 },
+ .pairs = {
+ 0, 3,
+ 3, 6,
+ },
+ .num_strs = 2,
+ .custom = "My Tuple App",
+ .custom_len = 12,
+ .exp = {
+ 0x64, 0x83, 0xCB, 0x3C, 0x99, 0x52, 0xEB, 0x20, 0xE8, 0x30, 0xAF, 0x47, 0x85, 0x85, 0x1F, 0xC5,
+ 0x97, 0xEE, 0x3B, 0xF9, 0x3B, 0xB7, 0x60, 0x2C, 0x0E, 0xF6, 0xA6, 0x5D, 0x74, 0x1A, 0xEC, 0xA7,
+ 0xE6, 0x3C, 0x3B, 0x12, 0x89, 0x81, 0xAA, 0x05, 0xC6, 0xD2, 0x74, 0x38, 0xC7, 0x9D, 0x27, 0x54,
+ 0xBB, 0x1B, 0x71, 0x91, 0xF1, 0x25, 0xD6, 0x62, 0x0F, 0xCA, 0x12, 0xCE, 0x65, 0x8B, 0x24, 0x42,
+ },
+ }, {
+ .name = "TupleHashXOF Sample #6",
+ .data = {
+ 0x00, 0x01, 0x02,
+ 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
+ 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
+ },
+ .pairs = {
+ 0, 3,
+ 3, 6,
+ 9, 9,
+ },
+ .num_strs = 3,
+ .custom = "My Tuple App",
+ .custom_len = 12,
+ .exp = {
+ 0x0C, 0x59, 0xB1, 0x14, 0x64, 0xF2, 0x33, 0x6C, 0x34, 0x66, 0x3E, 0xD5, 0x1B, 0x2B, 0x95, 0x0B,
+ 0xEC, 0x74, 0x36, 0x10, 0x85, 0x6F, 0x36, 0xC2, 0x8D, 0x1D, 0x08, 0x8D, 0x8A, 0x24, 0x46, 0x28,
+ 0x4D, 0xD0, 0x98, 0x30, 0xA6, 0xA1, 0x78, 0xDC, 0x75, 0x23, 0x76, 0x19, 0x9F, 0xAE, 0x93, 0x5D,
+ 0x86, 0xCF, 0xDE, 0xE5, 0x91, 0x3D, 0x49, 0x22, 0xDF, 0xD3, 0x69, 0xB6, 0x6A, 0x53, 0xC8, 0x97,
+ },
+ }};
+
+ for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+ // build strings
+ tuplehash_str_t strs[10] = { 0 };
+ for (size_t j = 0; j < tests[i].num_strs; j++) {
+ strs[j].ptr = tests[i].data + tests[i].pairs[2 * j];
+ strs[j].len = tests[i].pairs[2 * j + 1];
+ }
+
+ // build params
+ const tuplehash_params_t params = {
+ .strs = strs,
+ .num_strs = tests[i].num_strs,
+ .custom = tests[i].custom,
+ .custom_len = tests[i].custom_len,
+ };
+
+ // run
+ uint8_t got[64];
+ tuplehash256_xof_once(params, got, sizeof(got));
+
+ // check
+ if (memcmp(got, tests[i].exp, sizeof(got))) {
+ fprintf(stderr, "test_tuplehash256_xof(\"%s\") failed, got:\n", tests[i].name);
+ dump_hex(stderr, got, 32);
+
+ fprintf(stderr, "exp:\n");
+ dump_hex(stderr, tests[i].exp, 32);
+ }
+ }
+}
+
int main(void) {
test_theta();
test_rho();
@@ -3464,6 +3650,8 @@ int main(void) {
test_kmac256_xof();
test_tuplehash128();
test_tuplehash256();
+ test_tuplehash128_xof();
+ test_tuplehash256_xof();
printf("ok\n");
}