summaryrefslogtreecommitdiff
path: root/sha3.c
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2023-09-01 23:15:00 -0400
committerPaul Duncan <pabs@pablotron.org>2023-09-01 23:15:00 -0400
commite23ca862bbe2d97675277dde443fbef498db771f (patch)
treebeac0825566e38ea4b63728e407cb9cfd03edde0 /sha3.c
parentc7f630ed23144939a540219787c7be3ffe14b24c (diff)
downloadsha3-e23ca862bbe2d97675277dde443fbef498db771f.tar.bz2
sha3-e23ca862bbe2d97675277dde443fbef498db771f.zip
./sha3.h: add shake{128,256}_xof_*()
Diffstat (limited to 'sha3.c')
-rw-r--r--sha3.c372
1 files changed, 362 insertions, 10 deletions
diff --git a/sha3.c b/sha3.c
index 388bf24..d32a28b 100644
--- a/sha3.c
+++ b/sha3.c
@@ -1,10 +1,11 @@
//
-// Run self-tests:
+// FIPS202 (SHA-3) implementation.
//
-// cc -W -Wall -Wextra -Werror -pedantic -std=c2x -O3 -march=native -mtune=native -mavx -mavx2 -msse2 -msse -mavx512f -std=c11 -DSHA3_TEST % && ./a.out
+#include <stdbool.h> // true, false
#include <stdint.h> // uint64_t
#include <string.h> // memcpy()
+#include "sha3.h"
// 64-bit rotate left
#define ROL(v, n) (((v) << (n)) | ((v) >> (64-(n))))
@@ -143,12 +144,7 @@ static inline void permute(uint64_t a[static 25]) {
}
}
-typedef union {
- uint8_t u8[200];
- uint64_t u64[25];
-} state_t;
-
-static inline size_t keccak(state_t * const a, const uint8_t *m, size_t m_len, const size_t rate) {
+static inline size_t keccak(sha3_state_t * const a, const uint8_t *m, size_t m_len, const size_t rate) {
while (m_len >= rate) {
// absorb u64-sized chunks
for (size_t i = 0; i < rate / sizeof(uint64_t); i++) {
@@ -182,7 +178,7 @@ static inline void sha3(const uint8_t *m, size_t m_len, uint8_t * const dst, con
// capacity
const size_t rate = 200 - 2 * dst_len;
- state_t a = { .u64 = { 0 } };
+ sha3_state_t a = { .u64 = { 0 } };
const size_t len = keccak(&a, m, m_len, rate);
@@ -220,7 +216,7 @@ static inline void shake(const uint8_t *m, size_t m_len, uint8_t * const dst, co
// capacity
const size_t rate = 200 - 2 * dst_len;
- state_t a = { .u64 = { 0 } };
+ sha3_state_t a = { .u64 = { 0 } };
const size_t len = keccak(&a, m, m_len, rate);
@@ -244,6 +240,103 @@ void shake256(const uint8_t *m, size_t m_len, uint8_t dst[static 32]) {
shake(m, m_len, dst, 32);
}
+static inline void xof_init(sha3_xof_t * const xof) {
+ memset(xof, 0, sizeof(sha3_xof_t));
+}
+
+static inline _Bool xof_absorb(sha3_xof_t * const xof, const size_t rate, const uint8_t *m, size_t m_len) {
+ // check state
+ if (xof->squeezing) {
+ return false;
+ }
+
+ // absorb bytes
+ // (TODO: absorb larger chunks)
+ for (size_t i = 0; i < m_len; i++) {
+ xof->a.u8[xof->num_bytes++] ^= m[i];
+ if (xof->num_bytes == rate) {
+ permute(xof->a.u64);
+ xof->num_bytes = 0;
+ }
+ }
+
+ // return success
+ return true;
+}
+
+static inline _Bool xof_absorb_done(sha3_xof_t * const xof, const size_t rate) {
+ // check state
+ if (xof->squeezing) {
+ return false;
+ }
+
+ // append suffix (s6.2) and padding
+ // (note: suffix and padding are ambiguous in spec)
+ xof->a.u8[xof->num_bytes] ^= 0x1f;
+ xof->a.u8[rate - 1] ^= 0x80;
+
+ // permute, switch to squeeze mode
+ permute(xof->a.u64);
+ xof->num_bytes = 0;
+ xof->squeezing = true;
+
+ // return success
+ return true;
+}
+
+static inline _Bool xof_squeeze(sha3_xof_t * const xof, const size_t rate, uint8_t * const dst, const size_t dst_len) {
+ // check state
+ if (!xof->squeezing) {
+ return false;
+ }
+
+ for (size_t i = 0; i < dst_len; i++) {
+ dst[i] = xof->a.u8[xof->num_bytes++];
+ if (xof->num_bytes == rate) {
+ permute(xof->a.u64);
+ xof->num_bytes = 0;
+ }
+ }
+
+ // return success
+ return true;
+}
+
+#define SHAKE128_XOF_RATE (200 - 2 * 16)
+
+void shake128_xof_init(sha3_xof_t * const xof) {
+ xof_init(xof);
+}
+
+_Bool shake128_xof_absorb(sha3_xof_t * const xof, const uint8_t * const m, const size_t len) {
+ return xof_absorb(xof, SHAKE128_XOF_RATE, m, len);
+}
+
+_Bool shake128_xof_absorb_done(sha3_xof_t * const xof) {
+ return xof_absorb_done(xof, SHAKE128_XOF_RATE);
+}
+
+_Bool shake128_xof_squeeze(sha3_xof_t * const xof, uint8_t * const dst, const size_t dst_len) {
+ return xof_squeeze(xof, SHAKE128_XOF_RATE, dst, dst_len);
+}
+
+#define SHAKE256_XOF_RATE (200 - 2 * 32)
+
+void shake256_xof_init(sha3_xof_t * const xof) {
+ xof_init(xof);
+}
+
+_Bool shake256_xof_absorb(sha3_xof_t * const xof, const uint8_t * const m, const size_t len) {
+ return xof_absorb(xof, SHAKE256_XOF_RATE, m, len);
+}
+
+_Bool shake256_xof_absorb_done(sha3_xof_t * const xof) {
+ return xof_absorb_done(xof, SHAKE256_XOF_RATE);
+}
+
+_Bool shake256_xof_squeeze(sha3_xof_t * const xof, uint8_t * const dst, const size_t dst_len) {
+ return xof_squeeze(xof, SHAKE256_XOF_RATE, dst, dst_len);
+}
#ifdef SHA3_TEST
#include <stdio.h> // printf()
@@ -1157,6 +1250,263 @@ static void test_shake256(void) {
}
}
+static void test_shake128_xof(void) {
+ static const struct {
+ const char *name; // test name
+ const uint8_t msg[256]; // test message
+ const size_t len; // test message length
+ const uint8_t exp[16]; // expected hash
+ } tests[] = {{
+ .name = "empty",
+ .msg = "",
+ .len = 0,
+ .exp = {
+ 0x7f, 0x9c, 0x2b, 0xa4, 0xe8, 0x8f, 0x82, 0x7d,
+ 0x61, 0x60, 0x45, 0x50, 0x76, 0x05, 0x85, 0x3e,
+ },
+ }, {
+ .name = "asdf",
+ .msg = "asdf",
+ .len = 4,
+ .exp = {
+ 0xef, 0x02, 0x2c, 0xc5, 0x3c, 0x74, 0xb3, 0x28,
+ 0x43, 0xf9, 0xc1, 0xf1, 0x14, 0x13, 0xd5, 0x9c,
+ },
+ }, {
+ .name = "a-134",
+ .msg = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ .len = 134,
+ .exp = {
+ 0xc8, 0x73, 0x5e, 0x5f, 0x6f, 0x15, 0xaf, 0xe5,
+ 0x1a, 0x8c, 0x3b, 0x07, 0xc4, 0xc6, 0x8d, 0x86,
+ },
+ }, {
+ .name = "a-135",
+ .msg = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ .len = 135,
+ .exp = {
+ 0xa5, 0xe2, 0xb2, 0x27, 0x8d, 0x1b, 0x75, 0x86,
+ 0x6c, 0x78, 0x77, 0xa0, 0xff, 0xa2, 0x47, 0x37,
+ },
+ }, {
+ .name = "a-136",
+ .msg = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ .len = 136,
+ .exp = {
+ 0x0d, 0x01, 0x58, 0xd4, 0x46, 0x78, 0x3a, 0x9b,
+ 0x18, 0xa6, 0x90, 0x8c, 0x08, 0xbb, 0x5d, 0xe6,
+ },
+ }, {
+ .name = "ff-256",
+ .msg = {
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ },
+ .len = 256,
+ .exp = {
+ 0xf3, 0x06, 0x93, 0x04, 0x16, 0x5c, 0x0e, 0xad,
+ 0x13, 0x25, 0xb5, 0x26, 0x76, 0x05, 0x95, 0xed,
+ },
+ }, {
+ .name = "a-210",
+ .msg = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ .len = 210,
+ .exp = {
+ 0xb1, 0xb4, 0xf3, 0xad, 0x3a, 0x1f, 0x67, 0x60,
+ 0xe3, 0x08, 0x67, 0xdd, 0x71, 0xb3, 0x49, 0xfa,
+ },
+ }};
+
+ for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+ for (size_t len = 1; len < tests[i].len; len++) {
+ // init xof
+ sha3_xof_t xof;
+ shake128_xof_init(&xof);
+
+ // absorb
+ for (size_t ofs = 0; ofs < tests[i].len; ofs += len) {
+ const size_t absorb_len = (tests[i].len - ofs < len) ? tests[i].len - ofs : len;
+ if (!shake128_xof_absorb(&xof, tests[i].msg + ofs, absorb_len)) {
+ fprintf(stderr, "test_shake128_xof(\"%s\", %zu) failed: shake128_xof_absorb()\n", tests[i].name, len);
+ return;
+ }
+ }
+
+ // finish absorbing
+ if (!shake128_xof_absorb_done(&xof)) {
+ fprintf(stderr, "test_shake128_xof(\"%s\", %zu) failed: shake128_xof_absorb_done()\n", tests[i].name, len);
+ return;
+ }
+
+ // squeeze
+ uint8_t got[16] = { 0 };
+ if (!shake128_xof_squeeze(&xof, got, sizeof(got))) {
+ fprintf(stderr, "test_shake128_xof(\"%s\", %zu) failed: shake128_xof_squeeze()\n", tests[i].name, len);
+ return;
+ }
+
+ // check
+ if (memcmp(got, tests[i].exp, sizeof(got))) {
+ fprintf(stderr, "test_shake128_xof(\"%s\", %zu) failed, got:\n", tests[i].name, len);
+ dump_hex(stderr, got, 16);
+
+ fprintf(stderr, "exp:\n");
+ dump_hex(stderr, tests[i].exp, 16);
+ }
+ }
+ }
+}
+
+static void test_shake256_xof(void) {
+ static const struct {
+ const char *name; // test name
+ const uint8_t msg[256]; // test message
+ const size_t len; // test message length
+ const uint8_t exp[32]; // expected hash
+ } tests[] = {{
+ .name = "empty",
+ .msg = "",
+ .len = 0,
+ .exp = {
+ 0x46, 0xb9, 0xdd, 0x2b, 0x0b, 0xa8, 0x8d, 0x13,
+ 0x23, 0x3b, 0x3f, 0xeb, 0x74, 0x3e, 0xeb, 0x24,
+ 0x3f, 0xcd, 0x52, 0xea, 0x62, 0xb8, 0x1b, 0x82,
+ 0xb5, 0x0c, 0x27, 0x64, 0x6e, 0xd5, 0x76, 0x2f,
+ },
+ }, {
+ .name = "asdf",
+ .msg = "asdf",
+ .len = 4,
+ .exp = {
+ 0xf0, 0x0c, 0x15, 0x64, 0x33, 0x96, 0x61, 0x6a,
+ 0x89, 0xa0, 0xcb, 0x79, 0x03, 0x9f, 0x74, 0x05,
+ 0x75, 0xde, 0xfe, 0x9d, 0xbe, 0x30, 0x7c, 0xcc,
+ 0xda, 0xf8, 0xae, 0x21, 0x0e, 0x1c, 0x9c, 0xc6,
+ },
+ }, {
+ .name = "a-134",
+ .msg = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ .len = 134,
+ .exp = {
+ 0xf1, 0xe0, 0x3f, 0x37, 0x8e, 0xb7, 0x79, 0x04,
+ 0xba, 0x15, 0xbb, 0x64, 0x2a, 0x84, 0xb9, 0x0d,
+ 0xe5, 0x2e, 0x29, 0x3e, 0xaf, 0xc2, 0x7c, 0xef,
+ 0x05, 0x88, 0x3b, 0x16, 0x56, 0xae, 0xc3, 0x41,
+ },
+ }, {
+ .name = "a-135",
+ .msg = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ .len = 135,
+ .exp = {
+ 0x55, 0xb9, 0x91, 0xec, 0xe1, 0xe5, 0x67, 0xb6,
+ 0xe7, 0xc2, 0xc7, 0x14, 0x44, 0x4d, 0xd2, 0x01,
+ 0xcd, 0x51, 0xf4, 0xf3, 0x83, 0x2d, 0x08, 0xe1,
+ 0xd2, 0x6b, 0xeb, 0xc6, 0x3e, 0x07, 0xa3, 0xd7,
+ },
+ }, {
+ .name = "a-136",
+ .msg = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ .len = 136,
+ .exp = {
+ 0x8f, 0xcc, 0x5a, 0x08, 0xf0, 0xa1, 0xf6, 0x82,
+ 0x7c, 0x9c, 0xf6, 0x4e, 0xe8, 0xd1, 0x6e, 0x04,
+ 0x43, 0x10, 0x63, 0x59, 0xca, 0x6c, 0x8e, 0xfd,
+ 0x23, 0x07, 0x59, 0x25, 0x6f, 0x44, 0x99, 0x6a,
+ },
+ }, {
+ .name = "ff-256",
+ .msg = {
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
+ },
+ .len = 256,
+ .exp = {
+ 0x3f, 0x25, 0xdf, 0x0e, 0x37, 0x17, 0x14, 0xdf,
+ 0xb0, 0xcc, 0x3d, 0x96, 0x17, 0xe1, 0xa0, 0x71,
+ 0x75, 0xa0, 0xf0, 0x84, 0xc7, 0x00, 0x29, 0x23,
+ 0x5c, 0x72, 0x7c, 0x5a, 0x68, 0x5e, 0xf0, 0x14,
+ },
+ }, {
+ .name = "a-210",
+ .msg = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
+ .len = 210,
+ .exp = {
+ 0xc9, 0xe2, 0xd9, 0x58, 0xf2, 0xdd, 0x3d, 0x97,
+ 0x53, 0x8a, 0x1b, 0xac, 0x1b, 0x4e, 0xb3, 0x2e,
+ 0x28, 0x23, 0x6b, 0x5d, 0xfc, 0xe0, 0x29, 0xfc,
+ 0xc8, 0x73, 0xc0, 0xf2, 0x70, 0xe1, 0x3e, 0x9f,
+ },
+ }};
+
+ for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) {
+ for (size_t len = 1; len < tests[i].len; len++) {
+ // init xof
+ sha3_xof_t xof;
+ shake256_xof_init(&xof);
+
+ // absorb
+ for (size_t ofs = 0; ofs < tests[i].len; ofs += len) {
+ const size_t absorb_len = (tests[i].len - ofs < len) ? tests[i].len - ofs : len;
+ if (!shake256_xof_absorb(&xof, tests[i].msg + ofs, absorb_len)) {
+ fprintf(stderr, "test_shake256_xof(\"%s\", %zu) failed: shake256_xof_absorb()\n", tests[i].name, len);
+ return;
+ }
+ }
+
+ // finish absorbing
+ if (!shake256_xof_absorb_done(&xof)) {
+ fprintf(stderr, "test_shake256_xof(\"%s\", %zu) failed: shake256_xof_absorb_done()\n", tests[i].name, len);
+ return;
+ }
+
+ // squeeze
+ uint8_t got[32] = { 0 };
+ if (!shake256_xof_squeeze(&xof, got, sizeof(got))) {
+ fprintf(stderr, "test_shake256_xof(\"%s\", %zu) failed: shake256_xof_squeeze()\n", tests[i].name, len);
+ return;
+ }
+
+ // check
+ if (memcmp(got, tests[i].exp, sizeof(got))) {
+ fprintf(stderr, "test_shake256_xof(\"%s\", %zu) failed, got:\n", tests[i].name, len);
+ dump_hex(stderr, got, 16);
+
+ fprintf(stderr, "exp:\n");
+ dump_hex(stderr, tests[i].exp, 16);
+ }
+ }
+ }
+}
+
+
int main(void) {
test_theta();
test_rho();
@@ -1170,6 +1520,8 @@ int main(void) {
test_sha3_512();
test_shake128();
test_shake256();
+ test_shake128_xof();
+ test_shake256_xof();
printf("ok\n");
}