aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2023-09-02 12:22:21 -0400
committerPaul Duncan <pabs@pablotron.org>2023-09-02 12:22:21 -0400
commitbe371d5f35f783bdd73ec5714c6cbbd7153ed6f7 (patch)
tree15a57b8bb3800ba9da3208b55a11f407b3172d7f
parentcdd3961d4a370041176d27d97129f998d2c1f549 (diff)
downloadsha3-be371d5f35f783bdd73ec5714c6cbbd7153ed6f7.tar.bz2
sha3-be371d5f35f783bdd73ec5714c6cbbd7153ed6f7.zip
sha3.[hc]: add shake{128,256}_xof_once()
-rw-r--r--sha3.c238
-rw-r--r--sha3.h2
2 files changed, 240 insertions, 0 deletions
diff --git a/sha3.c b/sha3.c
index ac16418..d6c43d3 100644
--- a/sha3.c
+++ b/sha3.c
@@ -281,6 +281,7 @@ static inline void xof_absorb_done(sha3_xof_t * const xof, const size_t rate) {
static inline void 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) {
+ // finalize absorb
xof_absorb_done(xof, rate);
}
@@ -307,6 +308,18 @@ void shake128_xof_squeeze(sha3_xof_t * const xof, uint8_t * const dst, const siz
xof_squeeze(xof, SHAKE128_XOF_RATE, dst, dst_len);
}
+void shake128_xof_once(const uint8_t * const src, const size_t src_len, uint8_t * const dst, const size_t dst_len) {
+ // init
+ sha3_xof_t xof;
+ shake128_xof_init(&xof);
+
+ // absorb (ignore error)
+ (void) shake128_xof_absorb(&xof, src, src_len);
+
+ // squeeze
+ shake128_xof_squeeze(&xof, dst, dst_len);
+}
+
#define SHAKE256_XOF_RATE (200 - 2 * 32)
void shake256_xof_init(sha3_xof_t * const xof) {
@@ -321,6 +334,18 @@ void shake256_xof_squeeze(sha3_xof_t * const xof, uint8_t * const dst, const siz
xof_squeeze(xof, SHAKE256_XOF_RATE, dst, dst_len);
}
+void shake256_xof_once(const uint8_t * const src, const size_t src_len, uint8_t * const dst, const size_t dst_len) {
+ // init
+ sha3_xof_t xof;
+ shake256_xof_init(&xof);
+
+ // absorb (ignore error)
+ (void) shake256_xof_absorb(&xof, src, src_len);
+
+ // squeeze
+ shake256_xof_squeeze(&xof, dst, dst_len);
+}
+
#ifdef SHA3_TEST
#include <stdio.h> // printf()
@@ -1345,6 +1370,105 @@ static void test_shake128_xof(void) {
}
}
+static void test_shake128_xof_once(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++) {
+ // run
+ uint8_t got[16];
+ shake128_xof_once(tests[i].msg, tests[i].len, got, sizeof(got));
+
+ // check
+ if (memcmp(got, tests[i].exp, sizeof(got))) {
+ fprintf(stderr, "test_shake128_xof_once(\"%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
@@ -1471,6 +1595,118 @@ static void test_shake256_xof(void) {
}
}
+static void test_shake256_xof_once(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,
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
+ 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++) {
+ // run shake256
+ uint8_t got[32];
+ shake256_xof_once(tests[i].msg, tests[i].len, got, sizeof(got));
+
+ // check
+ if (memcmp(got, tests[i].exp, sizeof(got))) {
+ fprintf(stderr, "test_shake256_xof_once(\"%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();
@@ -1486,7 +1722,9 @@ int main(void) {
test_shake128();
test_shake256();
test_shake128_xof();
+ test_shake128_xof_once();
test_shake256_xof();
+ test_shake256_xof_once();
printf("ok\n");
}
diff --git a/sha3.h b/sha3.h
index 1be9970..c241758 100644
--- a/sha3.h
+++ b/sha3.h
@@ -29,10 +29,12 @@ void shake256(const uint8_t *m, size_t m_len, uint8_t dst[static 32]);
void shake128_xof_init(sha3_xof_t * const xof);
_Bool shake128_xof_absorb(sha3_xof_t * const xof, const uint8_t * const m, const size_t len);
void shake128_xof_squeeze(sha3_xof_t * const xof, uint8_t * const dst, const size_t dst_len);
+void shake128_xof_once(const uint8_t * const src, const size_t src_len, uint8_t * const dst, const size_t dst_len);
void shake256_xof_init(sha3_xof_t * const xof);
_Bool shake256_xof_absorb(sha3_xof_t * const xof, const uint8_t * const m, const size_t len);
void shake256_xof_squeeze(sha3_xof_t * const xof, uint8_t * const dst, const size_t dst_len);
+void shake256_xof_once(const uint8_t * const src, const size_t src_len, uint8_t * const dst, const size_t dst_len);
#ifdef __cplusplus
}