From b806ec3beaea1d66c9b5abca2df1dd5d795d10a8 Mon Sep 17 00:00:00 2001 From: Paul Duncan Date: Mon, 4 Sep 2023 02:02:43 -0400 Subject: sha3.[hc]: add parallelhash256, parallelhash256_xof_{init,absorb,squeeze,once}(), test_parallelhash256(), and test_parallelhash256_xof() --- sha3.c | 311 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 311 insertions(+) (limited to 'sha3.c') diff --git a/sha3.c b/sha3.c index 6ab3064..01dd4f8 100644 --- a/sha3.c +++ b/sha3.c @@ -1158,6 +1158,143 @@ void parallelhash128_xof_once(const parallelhash_params_t params, const uint8_t parallelhash128_xof_squeeze(&hash, dst, dst_len); } +static void parallelhash256_emit_block(parallelhash_t * const hash) { + // squeeze curr xof, absorb into root xof + uint8_t buf[64]; + shake256_xof_squeeze(&(hash->curr_xof), buf, sizeof(buf)); + (void) cshake256_xof_absorb(&(hash->root_xof), buf, sizeof(buf)); + + // increment block count + hash->num_blocks++; +} + +static inline void parallelhash256_reset_curr_xof(parallelhash_t *hash) { + // init curr xof + shake256_xof_init(&(hash->curr_xof)); + hash->ofs = 0; +} + +static inline void parallelhash256_init(parallelhash_t *hash, const parallelhash_params_t params) { + static const uint8_t NAME[] = { 'P', 'a', 'r', 'a', 'l', 'l', 'e', 'l', 'H', 'a', 's', 'h' }; + + // build root xof cshake256 params + const cshake_params_t root_cshake_params = { + .name = NAME, + .name_len = sizeof(NAME), + .custom = params.custom, + .custom_len = params.custom_len, + }; + + // init root xof + cshake256_xof_init(&(hash->root_xof), root_cshake_params); + + // build block size + uint8_t buf[9] = { 0 }; + const size_t buf_len = left_encode(buf, params.block_len); + + // absorb block length into root xof + (void) cshake256_xof_absorb(&(hash->root_xof), buf, buf_len); + + // set parameters + hash->block_len = params.block_len; + hash->num_blocks = 0; + hash->squeezing = false; + + // init curr xof + parallelhash256_reset_curr_xof(hash); +} + +static inline void parallelhash256_absorb(parallelhash_t * const hash, const uint8_t *msg, size_t msg_len) { + while (msg_len > 0) { + const size_t len = MIN(msg_len, hash->block_len - hash->ofs); + (void) shake256_xof_absorb(&(hash->curr_xof), msg, len); + msg += len; + msg_len -= len; + + hash->ofs += len; + if (hash->ofs == hash->block_len) { + // emit block, reset curr xof + parallelhash256_emit_block(hash); + parallelhash256_reset_curr_xof(hash); + } + } +} + +static inline void parallelhash256_squeeze(parallelhash_t * const hash, uint8_t * const dst, const size_t dst_len) { + if (!hash->squeezing) { + // mark as squeezing + hash->squeezing = true; + + if (hash->ofs > 0) { + // squeeze curr xof, absorb into root xof + parallelhash256_emit_block(hash); + } + + { + // build num blocks suffix + uint8_t buf[9] = { 0 }; + const size_t len = right_encode(buf, hash->num_blocks); + + // absorb num blocks suffix into root xof + (void) cshake256_xof_absorb(&(hash->root_xof), buf, len); + } + + { + // build output size suffix + uint8_t buf[9] = { 0 }; + const size_t len = right_encode(buf, dst_len << 3); + + // absorb output size suffix into root xof + (void) cshake256_xof_absorb(&(hash->root_xof), buf, len); + } + } + + if (dst_len > 0) { + cshake256_xof_squeeze(&(hash->root_xof), dst, dst_len); + } +} + +void parallelhash256(const parallelhash_params_t params, const uint8_t * const src, const size_t src_len, uint8_t * const dst, const size_t dst_len) { + // init + parallelhash_t hash; + parallelhash256_init(&hash, params); + + // absorb + parallelhash256_absorb(&hash, src, src_len); + + // squeeze + parallelhash256_squeeze(&hash, dst, dst_len); +} + +void parallelhash256_xof_init(parallelhash_t *hash, const parallelhash_params_t params) { + parallelhash256_init(hash, params); +} + +void parallelhash256_xof_absorb(parallelhash_t *hash, const uint8_t *msg, const size_t msg_len) { + parallelhash256_absorb(hash, msg, msg_len); +} + +void parallelhash256_xof_squeeze(parallelhash_t *hash, uint8_t *dst, const size_t dst_len) { + if (!hash->squeezing) { + // emit zero length + parallelhash256_squeeze(hash, dst, 0); + } + + parallelhash256_squeeze(hash, dst, dst_len); +} + +void parallelhash256_xof_once(const parallelhash_params_t params, const uint8_t * const src, const size_t src_len, uint8_t * const dst, const size_t dst_len) { + // init + parallelhash_t hash; + parallelhash256_xof_init(&hash, params); + + // absorb + parallelhash256_xof_absorb(&hash, src, src_len); + + // squeeze + parallelhash256_xof_squeeze(&hash, dst, dst_len); +} + #ifdef SHA3_TEST #include // printf() @@ -3918,6 +4055,178 @@ static void test_parallelhash128_xof(void) { } } +static void test_parallelhash256(void) { + static const struct { + const char *name; // test name + const uint8_t custom[256]; // custom name + const size_t custom_len; // custom name length + const size_t block_len; // block size, in bytes + const uint8_t msg[256]; // input data + const size_t len; // message length, in bytes + const uint8_t exp[64]; // expected hash + } tests[] = {{ + // src: https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/ParallelHash_samples.pdf + .name = "ParallelHash Sample #4", + .custom = "", + .custom_len = 0, + .block_len = 8, + .msg = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + }, + .len = 24, + .exp = { + 0xBC, 0x1E, 0xF1, 0x24, 0xDA, 0x34, 0x49, 0x5E, 0x94, 0x8E, 0xAD, 0x20, 0x7D, 0xD9, 0x84, 0x22, + 0x35, 0xDA, 0x43, 0x2D, 0x2B, 0xBC, 0x54, 0xB4, 0xC1, 0x10, 0xE6, 0x4C, 0x45, 0x11, 0x05, 0x53, + 0x1B, 0x7F, 0x2A, 0x3E, 0x0C, 0xE0, 0x55, 0xC0, 0x28, 0x05, 0xE7, 0xC2, 0xDE, 0x1F, 0xB7, 0x46, + 0xAF, 0x97, 0xA1, 0xDD, 0x01, 0xF4, 0x3B, 0x82, 0x4E, 0x31, 0xB8, 0x76, 0x12, 0x41, 0x04, 0x29, + }, + }, { + .name = "ParallelHash Sample #5", + .custom = "Parallel Data", + .custom_len = 13, + .block_len = 8, + .msg = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + }, + .len = 24, + .exp = { + 0xCD, 0xF1, 0x52, 0x89, 0xB5, 0x4F, 0x62, 0x12, 0xB4, 0xBC, 0x27, 0x05, 0x28, 0xB4, 0x95, 0x26, + 0x00, 0x6D, 0xD9, 0xB5, 0x4E, 0x2B, 0x6A, 0xDD, 0x1E, 0xF6, 0x90, 0x0D, 0xDA, 0x39, 0x63, 0xBB, + 0x33, 0xA7, 0x24, 0x91, 0xF2, 0x36, 0x96, 0x9C, 0xA8, 0xAF, 0xAE, 0xA2, 0x9C, 0x68, 0x2D, 0x47, + 0xA3, 0x93, 0xC0, 0x65, 0xB3, 0x8E, 0x29, 0xFA, 0xE6, 0x51, 0xA2, 0x09, 0x1C, 0x83, 0x31, 0x10, + }, + }, { + .name = "ParallelHash Sample #6", + .custom = "Parallel Data", + .custom_len = 13, + .block_len = 12, + .msg = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x10, 0x11, 0x12, 0x13, + 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2A, 0x2B, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x50, 0x51, 0x52, 0x53, + 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, + }, + .len = 72, + .exp = { + 0x69, 0xD0, 0xFC, 0xB7, 0x64, 0xEA, 0x05, 0x5D, 0xD0, 0x93, 0x34, 0xBC, 0x60, 0x21, 0xCB, 0x7E, + 0x4B, 0x61, 0x34, 0x8D, 0xFF, 0x37, 0x5D, 0xA2, 0x62, 0x67, 0x1C, 0xDE, 0xC3, 0xEF, 0xFA, 0x8D, + 0x1B, 0x45, 0x68, 0xA6, 0xCC, 0xE1, 0x6B, 0x1C, 0xAD, 0x94, 0x6D, 0xDD, 0xE2, 0x7F, 0x6C, 0xE2, + 0xB8, 0xDE, 0xE4, 0xCD, 0x1B, 0x24, 0x85, 0x1E, 0xBF, 0x00, 0xEB, 0x90, 0xD4, 0x38, 0x13, 0xE9, + }, + }}; + + for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { + // build params + const parallelhash_params_t params = { + .block_len = tests[i].block_len, + .custom = tests[i].custom, + .custom_len = tests[i].custom_len, + }; + + // run + uint8_t got[64]; + parallelhash256(params, tests[i].msg, tests[i].len, got, sizeof(got)); + + // check + if (memcmp(got, tests[i].exp, sizeof(got))) { + fprintf(stderr, "test_parallelhash256(\"%s\") failed, got:\n", tests[i].name); + dump_hex(stderr, got, sizeof(got)); + + fprintf(stderr, "exp:\n"); + dump_hex(stderr, tests[i].exp, sizeof(got)); + } + } +} + +static void test_parallelhash256_xof(void) { + static const struct { + const char *name; // test name + const uint8_t custom[256]; // custom name + const size_t custom_len; // custom name length + const size_t block_len; // block size, in bytes + const uint8_t msg[256]; // input data + const size_t len; // message length, in bytes + const uint8_t exp[64]; // expected hash + } tests[] = {{ + // src: https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/ParallelHashXOF_samples.pdf + .name = "ParallelHashXOF Sample #4", + .custom = "", + .custom_len = 0, + .block_len = 8, + .msg = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + }, + .len = 24, + .exp = { + 0xC1, 0x0A, 0x05, 0x27, 0x22, 0x61, 0x46, 0x84, 0x14, 0x4D, 0x28, 0x47, 0x48, 0x50, 0xB4, 0x10, + 0x75, 0x7E, 0x3C, 0xBA, 0x87, 0x65, 0x1B, 0xA1, 0x67, 0xA5, 0xCB, 0xDD, 0xFF, 0x7F, 0x46, 0x66, + 0x75, 0xFB, 0xF8, 0x4B, 0xCA, 0xE7, 0x37, 0x8A, 0xC4, 0x44, 0xBE, 0x68, 0x1D, 0x72, 0x94, 0x99, + 0xAF, 0xCA, 0x66, 0x7F, 0xB8, 0x79, 0x34, 0x8B, 0xFD, 0xDA, 0x42, 0x78, 0x63, 0xC8, 0x2F, 0x1C, + }, + }, { + .name = "ParallelHashXOF Sample #5", + .custom = "Parallel Data", + .custom_len = 13, + .block_len = 8, + .msg = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, + 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + }, + .len = 24, + .exp = { + 0x53, 0x8E, 0x10, 0x5F, 0x1A, 0x22, 0xF4, 0x4E, 0xD2, 0xF5, 0xCC, 0x16, 0x74, 0xFB, 0xD4, 0x0B, + 0xE8, 0x03, 0xD9, 0xC9, 0x9B, 0xF5, 0xF8, 0xD9, 0x0A, 0x2C, 0x81, 0x93, 0xF3, 0xFE, 0x6E, 0xA7, + 0x68, 0xE5, 0xC1, 0xA2, 0x09, 0x87, 0xE2, 0xC9, 0xC6, 0x5F, 0xEB, 0xED, 0x03, 0x88, 0x7A, 0x51, + 0xD3, 0x56, 0x24, 0xED, 0x12, 0x37, 0x75, 0x94, 0xB5, 0x58, 0x55, 0x41, 0xDC, 0x37, 0x7E, 0xFC, + }, + }, { + .name = "ParallelHashXOF Sample #6", + .custom = "Parallel Data", + .custom_len = 13, + .block_len = 12, + .msg = { + 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x10, 0x11, 0x12, 0x13, + 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, + 0x28, 0x29, 0x2A, 0x2B, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, + 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x50, 0x51, 0x52, 0x53, + 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x5B, + }, + .len = 72, + .exp = { + 0x6B, 0x3E, 0x79, 0x0B, 0x33, 0x0C, 0x88, 0x9A, 0x20, 0x4C, 0x2F, 0xBC, 0x72, 0x8D, 0x80, 0x9F, + 0x19, 0x36, 0x73, 0x28, 0xD8, 0x52, 0xF4, 0x00, 0x2D, 0xC8, 0x29, 0xF7, 0x3A, 0xFD, 0x6B, 0xCE, + 0xFB, 0x7F, 0xE5, 0xB6, 0x07, 0xB1, 0x3A, 0x80, 0x1C, 0x0B, 0xE5, 0xC1, 0x17, 0x0B, 0xDB, 0x79, + 0x4E, 0x33, 0x94, 0x58, 0xFD, 0xB0, 0xE6, 0x2A, 0x6A, 0xF3, 0xD4, 0x25, 0x58, 0x97, 0x02, 0x49, + }, + }}; + + for (size_t i = 0; i < sizeof(tests) / sizeof(tests[0]); i++) { + // build params + const parallelhash_params_t params = { + .block_len = tests[i].block_len, + .custom = tests[i].custom, + .custom_len = tests[i].custom_len, + }; + + // run + uint8_t got[64]; + parallelhash256_xof_once(params, tests[i].msg, tests[i].len, got, sizeof(got)); + + // check + if (memcmp(got, tests[i].exp, sizeof(got))) { + fprintf(stderr, "test_parallelhash256_xof(\"%s\") failed, got:\n", tests[i].name); + dump_hex(stderr, got, sizeof(got)); + + fprintf(stderr, "exp:\n"); + dump_hex(stderr, tests[i].exp, sizeof(got)); + } + } +} + int main(void) { test_theta(); test_rho(); @@ -3951,6 +4260,8 @@ int main(void) { test_tuplehash256_xof(); test_parallelhash128(); test_parallelhash128_xof(); + test_parallelhash256(); + test_parallelhash256_xof(); printf("ok\n"); } -- cgit v1.2.3