mirror of
https://github.com/jedisct1/libsodium.git
synced 2026-08-25 08:37:13 +09:00
Add SHAKE and TurboSHAKE
This commit is contained in:
@@ -37,6 +37,7 @@ EXTRA_DIST = \
|
||||
core4.exp \
|
||||
core5.exp \
|
||||
core6.exp \
|
||||
core_keccak1600.exp \
|
||||
ed25519_convert.exp \
|
||||
generichash.exp \
|
||||
generichash2.exp \
|
||||
@@ -86,7 +87,11 @@ EXTRA_DIST = \
|
||||
stream3.exp \
|
||||
stream4.exp \
|
||||
verify1.exp \
|
||||
xchacha20.exp
|
||||
xchacha20.exp \
|
||||
xof_shake128.exp \
|
||||
xof_shake256.exp \
|
||||
xof_turboshake128.exp \
|
||||
xof_turboshake256.exp
|
||||
|
||||
DISTCLEANFILES = \
|
||||
aead_aegis128l.res \
|
||||
@@ -121,6 +126,7 @@ DISTCLEANFILES = \
|
||||
core4.res \
|
||||
core5.res \
|
||||
core6.res \
|
||||
core_keccak1600.res \
|
||||
ed25519_convert.res \
|
||||
generichash.res \
|
||||
generichash2.res \
|
||||
@@ -171,7 +177,11 @@ DISTCLEANFILES = \
|
||||
stream3.res \
|
||||
stream4.res \
|
||||
verify1.res \
|
||||
xchacha20.res
|
||||
xchacha20.res \
|
||||
xof_shake128.res \
|
||||
xof_shake256.res \
|
||||
xof_turboshake128.res \
|
||||
xof_turboshake256.res
|
||||
|
||||
AM_CPPFLAGS = \
|
||||
-DTEST_SRCDIR=\"@srcdir@\" \
|
||||
@@ -213,6 +223,7 @@ TESTS_TARGETS = \
|
||||
core4 \
|
||||
core5 \
|
||||
core6 \
|
||||
core_keccak1600 \
|
||||
ed25519_convert \
|
||||
generichash \
|
||||
generichash2 \
|
||||
@@ -253,7 +264,11 @@ TESTS_TARGETS = \
|
||||
stream2 \
|
||||
stream3 \
|
||||
stream4 \
|
||||
verify1
|
||||
verify1 \
|
||||
xof_shake128 \
|
||||
xof_shake256 \
|
||||
xof_turboshake128 \
|
||||
xof_turboshake256
|
||||
|
||||
if !EMSCRIPTEN
|
||||
TESTS_TARGETS += \
|
||||
@@ -364,6 +379,9 @@ core5_LDADD = $(TESTS_LDADD)
|
||||
core6_SOURCE = cmptest.h core6.c
|
||||
core6_LDADD = $(TESTS_LDADD)
|
||||
|
||||
core_keccak1600_SOURCE = cmptest.h core_keccak1600.c
|
||||
core_keccak1600_LDADD = $(TESTS_LDADD)
|
||||
|
||||
ed25519_convert_SOURCE = cmptest.h ed25519_convert.c
|
||||
ed25519_convert_LDADD = $(TESTS_LDADD)
|
||||
|
||||
@@ -514,6 +532,18 @@ verify1_LDADD = $(TESTS_LDADD)
|
||||
xchacha20_SOURCE = cmptest.h xchacha20.c
|
||||
xchacha20_LDADD = $(TESTS_LDADD)
|
||||
|
||||
xof_shake128_SOURCE = cmptest.h xof_shake128.c
|
||||
xof_shake128_LDADD = $(TESTS_LDADD)
|
||||
|
||||
xof_shake256_SOURCE = cmptest.h xof_shake256.c
|
||||
xof_shake256_LDADD = $(TESTS_LDADD)
|
||||
|
||||
xof_turboshake128_SOURCE = cmptest.h xof_turboshake128.c
|
||||
xof_turboshake128_LDADD = $(TESTS_LDADD)
|
||||
|
||||
xof_turboshake256_SOURCE = cmptest.h xof_turboshake256.c
|
||||
xof_turboshake256_LDADD = $(TESTS_LDADD)
|
||||
|
||||
if !MINIMAL
|
||||
TESTS_TARGETS += \
|
||||
core_ed25519 \
|
||||
|
||||
@@ -0,0 +1,248 @@
|
||||
|
||||
#define TEST_NAME "core_keccak1600"
|
||||
#include "cmptest.h"
|
||||
|
||||
static void
|
||||
print_state(const unsigned char *state, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < len; i++) {
|
||||
if (i > 0 && i % 16 == 0) {
|
||||
printf("\n");
|
||||
}
|
||||
printf("%02x", state[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static void
|
||||
print_hex(const char *label, const unsigned char *data, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
printf("%s", label);
|
||||
for (i = 0; i < len; i++) {
|
||||
printf("%02x", data[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
static int
|
||||
compare_states(const char *label, const unsigned char *actual, const unsigned char *expected,
|
||||
size_t len)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < len; i++) {
|
||||
if (actual[i] != expected[i]) {
|
||||
printf("FAIL: %s mismatch at byte %u\n", label, (unsigned int) i);
|
||||
printf(" Expected: ");
|
||||
for (size_t j = 0; j < len; j++) {
|
||||
printf("%02x", expected[j]);
|
||||
}
|
||||
printf("\n Got: ");
|
||||
for (size_t j = 0; j < len; j++) {
|
||||
printf("%02x", actual[j]);
|
||||
}
|
||||
printf("\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
printf("PASS: %s\n", label);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
unsigned char state[crypto_core_keccak1600_STATEBYTES];
|
||||
unsigned char extracted[64];
|
||||
size_t i;
|
||||
int test_failures = 0;
|
||||
|
||||
/* Test vectors for Keccak-f[1600] (24 rounds) */
|
||||
/* Test vector 1: All-zero input for Keccak-f[1600] */
|
||||
static const unsigned char keccak_f_1600_zero_input[200] = { 0 };
|
||||
static const unsigned char keccak_f_1600_zero_expected[200] = {
|
||||
0xe7, 0xdd, 0xe1, 0x40, 0x79, 0x8f, 0x25, 0xf1, 0x8a, 0x47, 0xc0, 0x33, 0xf9, 0xcc, 0xd5,
|
||||
0x84, 0xee, 0xa9, 0x5a, 0xa6, 0x1e, 0x26, 0x98, 0xd5, 0x4d, 0x49, 0x80, 0x6f, 0x30, 0x47,
|
||||
0x15, 0xbd, 0x57, 0xd0, 0x53, 0x62, 0x05, 0x4e, 0x28, 0x8b, 0xd4, 0x6f, 0x8e, 0x7f, 0x2d,
|
||||
0xa4, 0x97, 0xff, 0xc4, 0x47, 0x46, 0xa4, 0xa0, 0xe5, 0xfe, 0x90, 0x76, 0x2e, 0x19, 0xd6,
|
||||
0x0c, 0xda, 0x5b, 0x8c, 0x9c, 0x05, 0x19, 0x1b, 0xf7, 0xa6, 0x30, 0xad, 0x64, 0xfc, 0x8f,
|
||||
0xd0, 0xb7, 0x5a, 0x93, 0x30, 0x35, 0xd6, 0x17, 0x23, 0x3f, 0xa9, 0x5a, 0xeb, 0x03, 0x21,
|
||||
0x71, 0x0d, 0x26, 0xe6, 0xa6, 0xa9, 0x5f, 0x55, 0xcf, 0xdb, 0x16, 0x7c, 0xa5, 0x81, 0x26,
|
||||
0xc8, 0x47, 0x03, 0xcd, 0x31, 0xb8, 0x43, 0x9f, 0x56, 0xa5, 0x11, 0x1a, 0x2f, 0xf2, 0x01,
|
||||
0x61, 0xae, 0xd9, 0x21, 0x5a, 0x63, 0xe5, 0x05, 0xf2, 0x70, 0xc9, 0x8c, 0xf2, 0xfe, 0xbe,
|
||||
0x64, 0x11, 0x66, 0xc4, 0x7b, 0x95, 0x70, 0x36, 0x61, 0xcb, 0x0e, 0xd0, 0x4f, 0x55, 0x5a,
|
||||
0x7c, 0xb8, 0xc8, 0x32, 0xcf, 0x1c, 0x8a, 0xe8, 0x3e, 0x8c, 0x14, 0x26, 0x3a, 0xae, 0x22,
|
||||
0x79, 0x0c, 0x94, 0xe4, 0x09, 0xc5, 0xa2, 0x24, 0xf9, 0x41, 0x18, 0xc2, 0x65, 0x04, 0xe7,
|
||||
0x26, 0x35, 0xf5, 0x16, 0x3b, 0xa1, 0x30, 0x7f, 0xe9, 0x44, 0xf6, 0x75, 0x49, 0xa2, 0xec,
|
||||
0x5c, 0x7b, 0xff, 0xf1, 0xea
|
||||
};
|
||||
|
||||
/* Test vector 2: Pattern input for Keccak-f[1600] */
|
||||
static const unsigned char keccak_f_1600_pattern_input[200] = {
|
||||
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
|
||||
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
|
||||
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
|
||||
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
|
||||
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
|
||||
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
|
||||
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
|
||||
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
|
||||
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
|
||||
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
|
||||
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
|
||||
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
|
||||
0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
|
||||
0xa3, 0xa3, 0xa3, 0xa3, 0xa3
|
||||
};
|
||||
static const unsigned char keccak_f_1600_pattern_expected[200] = {
|
||||
0xcc, 0x44, 0x83, 0xfe, 0xb5, 0x5e, 0x43, 0xf4, 0x6d, 0x88, 0x81, 0xbd, 0x35, 0x8e, 0xbf,
|
||||
0x75, 0x9a, 0x06, 0xe7, 0xcd, 0x81, 0xf5, 0xca, 0x48, 0xbf, 0xb7, 0x1b, 0x19, 0xc8, 0x02,
|
||||
0x61, 0x45, 0x00, 0x0a, 0x17, 0x39, 0xb1, 0x18, 0x89, 0xc7, 0x3d, 0x53, 0xa9, 0x78, 0x17,
|
||||
0xd2, 0x82, 0x4e, 0x52, 0xe6, 0x76, 0xbd, 0xe5, 0xce, 0xee, 0x9a, 0x86, 0x6e, 0x8b, 0x4c,
|
||||
0xca, 0x8c, 0xf2, 0x99, 0xd8, 0x18, 0x53, 0xa0, 0x06, 0x15, 0x02, 0xff, 0x1a, 0x7b, 0x11,
|
||||
0x82, 0x7c, 0x96, 0x7d, 0x8c, 0xf2, 0xc6, 0x21, 0xa6, 0x24, 0xda, 0x96, 0x75, 0xd2, 0xab,
|
||||
0xc9, 0x30, 0x89, 0x22, 0x18, 0x14, 0x7e, 0xa9, 0x07, 0xa6, 0xf0, 0x88, 0x7a, 0x86, 0x6c,
|
||||
0x7b, 0x79, 0x89, 0xe0, 0x6c, 0xcf, 0x82, 0x66, 0x62, 0x63, 0x79, 0x98, 0x37, 0x44, 0xce,
|
||||
0x1d, 0xe1, 0xb1, 0xb5, 0x12, 0xca, 0x63, 0x6f, 0x25, 0x47, 0x14, 0x1d, 0xef, 0x48, 0x47,
|
||||
0xac, 0x64, 0x6a, 0x76, 0xbc, 0x25, 0xbc, 0xed, 0x98, 0xb7, 0x34, 0xfd, 0xd6, 0x65, 0x15,
|
||||
0x4d, 0x85, 0xe8, 0x1e, 0x65, 0x08, 0x09, 0x28, 0x19, 0x39, 0x04, 0xf7, 0x6f, 0xec, 0x96,
|
||||
0x27, 0xa2, 0x23, 0x3b, 0x2f, 0x75, 0x02, 0x09, 0x25, 0x0d, 0x46, 0xb9, 0x77, 0x65, 0xd0,
|
||||
0x19, 0xa5, 0x5a, 0x97, 0xc8, 0xf9, 0x81, 0x4b, 0xfd, 0xb5, 0x38, 0xb9, 0xbc, 0x68, 0x55,
|
||||
0xac, 0x35, 0x1b, 0xf1, 0xe4
|
||||
};
|
||||
|
||||
/* Test vectors for Keccak-p[1600,12] (12 rounds, TurboSHAKE) */
|
||||
/* Test vector 3: All-zero input for Keccak-p[1600,12] */
|
||||
static const unsigned char keccak_p_12_zero_expected[200] = {
|
||||
0x17, 0x86, 0xa7, 0xb9, 0x38, 0x54, 0x5e, 0x8e, 0x1e, 0xd0, 0x59, 0xf2, 0x50, 0x6a, 0xcd,
|
||||
0xd9, 0x35, 0x1f, 0xa9, 0x52, 0xc6, 0xe7, 0xb8, 0x87, 0xc5, 0xe0, 0xe4, 0xcd, 0x67, 0xe0,
|
||||
0x93, 0x10, 0x45, 0x5a, 0xd9, 0xf2, 0x90, 0xab, 0x33, 0xb0, 0x45, 0x1a, 0xdd, 0xa8, 0x72,
|
||||
0x2f, 0xa7, 0xe0, 0x9c, 0x2f, 0x67, 0x14, 0xaa, 0x80, 0x37, 0xc5, 0x1d, 0x07, 0x51, 0x00,
|
||||
0xf5, 0x47, 0xdd, 0x3e, 0xcc, 0x8a, 0x17, 0x0c, 0x31, 0x1d, 0xa3, 0xb3, 0xa0, 0xaa, 0x57,
|
||||
0x92, 0xa5, 0x86, 0xb5, 0x79, 0x9b, 0xf9, 0xb1, 0xb3, 0x3d, 0x7c, 0x4a, 0xbc, 0x93, 0x67,
|
||||
0x8a, 0xe6, 0x63, 0x40, 0x87, 0x68, 0x66, 0x25, 0x0e, 0x2e, 0x33, 0x03, 0x6c, 0x5c, 0xda,
|
||||
0x30, 0xf0, 0xb9, 0x02, 0x12, 0xaa, 0x9c, 0x9f, 0x7a, 0xcf, 0x2b, 0x78, 0x9a, 0x3b, 0x5f,
|
||||
0x23, 0x79, 0xae, 0x61, 0xe0, 0xc1, 0x36, 0xe5, 0xec, 0x87, 0x3c, 0xb7, 0x18, 0xb6, 0xe9,
|
||||
0x6d, 0xc2, 0x8a, 0x91, 0x70, 0xf1, 0xd1, 0xbe, 0x2a, 0xb7, 0x24, 0xed, 0xda, 0x53, 0xbd,
|
||||
0xab, 0x6a, 0x5a, 0xe1, 0x2e, 0x2c, 0x6a, 0x41, 0xc1, 0xbf, 0xaf, 0x52, 0x09, 0xb9, 0x36,
|
||||
0xe0, 0xcf, 0xc6, 0xd7, 0x60, 0x70, 0xdc, 0x17, 0x36, 0x50, 0x45, 0xe4, 0x7a, 0x9f, 0xc2,
|
||||
0xb2, 0x11, 0x56, 0x62, 0x7a, 0x64, 0x30, 0x2c, 0xdb, 0x71, 0x36, 0xd4, 0x1c, 0xa0, 0x2c,
|
||||
0x22, 0x76, 0x0d, 0xfd, 0xcf
|
||||
};
|
||||
|
||||
printf("=== Keccak-1600 Core Function Tests ===\n\n");
|
||||
|
||||
/* Basic API tests */
|
||||
printf("Test 1: API constants\n");
|
||||
printf(" statebytes: %u\n", (unsigned int) crypto_core_keccak1600_statebytes());
|
||||
assert(crypto_core_keccak1600_statebytes() == crypto_core_keccak1600_STATEBYTES);
|
||||
assert(crypto_core_keccak1600_STATEBYTES == 200U);
|
||||
printf(" PASS: statebytes = 200\n\n");
|
||||
|
||||
/* Test 2: Init function */
|
||||
printf("Test 2: crypto_core_keccak1600_init\n");
|
||||
memset(state, 0xFF, sizeof state);
|
||||
crypto_core_keccak1600_init(state);
|
||||
for (i = 0; i < crypto_core_keccak1600_STATEBYTES; i++) {
|
||||
if (state[i] != 0) {
|
||||
printf(" FAIL: State not zeroed at byte %u\n", (unsigned int) i);
|
||||
test_failures++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i == crypto_core_keccak1600_STATEBYTES) {
|
||||
printf(" PASS: State initialized to zeros\n\n");
|
||||
}
|
||||
|
||||
/* Test 3: XOR and extract functions */
|
||||
printf("Test 3: crypto_core_keccak1600_xor_bytes and extract_bytes\n");
|
||||
crypto_core_keccak1600_init(state);
|
||||
unsigned char test_data[64];
|
||||
for (i = 0; i < sizeof test_data; i++) {
|
||||
test_data[i] = (unsigned char) i;
|
||||
}
|
||||
crypto_core_keccak1600_xor_bytes(state, test_data, 0, sizeof test_data);
|
||||
crypto_core_keccak1600_extract_bytes(state, extracted, 0, sizeof test_data);
|
||||
if (memcmp(extracted, test_data, sizeof test_data) == 0) {
|
||||
printf(" PASS: XOR and extract work correctly\n\n");
|
||||
} else {
|
||||
printf(" FAIL: XOR/extract mismatch\n\n");
|
||||
test_failures++;
|
||||
}
|
||||
|
||||
/* Test 4: Keccak-f[1600] with all-zero input (24 rounds) */
|
||||
printf("Test 4: Keccak-f[1600] (24 rounds) - Zero input\n");
|
||||
memcpy(state, keccak_f_1600_zero_input, 200);
|
||||
crypto_core_keccak1600_permute_24(state);
|
||||
test_failures +=
|
||||
compare_states(" Keccak-f[1600] zero", state, keccak_f_1600_zero_expected, 200);
|
||||
printf("\n");
|
||||
|
||||
/* Test 5: Keccak-f[1600] with pattern input (24 rounds) */
|
||||
printf("Test 5: Keccak-f[1600] (24 rounds) - Pattern 0xa3 input\n");
|
||||
memcpy(state, keccak_f_1600_pattern_input, 200);
|
||||
crypto_core_keccak1600_permute_24(state);
|
||||
test_failures +=
|
||||
compare_states(" Keccak-f[1600] pattern", state, keccak_f_1600_pattern_expected, 200);
|
||||
printf("\n");
|
||||
|
||||
/* Test 6: Keccak-p[1600,12] with all-zero input (12 rounds) */
|
||||
printf("Test 6: Keccak-p[1600,12] (12 rounds) - Zero input\n");
|
||||
crypto_core_keccak1600_init(state);
|
||||
crypto_core_keccak1600_permute_12(state);
|
||||
test_failures +=
|
||||
compare_states(" Keccak-p[1600,12] zero", state, keccak_p_12_zero_expected, 200);
|
||||
printf("\n");
|
||||
|
||||
/* Test 7: Verify 12 and 24 rounds produce different outputs */
|
||||
printf("Test 7: Verify 12-round and 24-round differ\n");
|
||||
unsigned char state_12[200], state_24[200];
|
||||
crypto_core_keccak1600_init(state_12);
|
||||
crypto_core_keccak1600_init(state_24);
|
||||
crypto_core_keccak1600_permute_12(state_12);
|
||||
crypto_core_keccak1600_permute_24(state_24);
|
||||
|
||||
int differs = 0;
|
||||
for (i = 0; i < 200; i++) {
|
||||
if (state_12[i] != state_24[i]) {
|
||||
differs = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (differs) {
|
||||
printf(" PASS: 12-round and 24-round produce different outputs\n");
|
||||
printf(" First difference at byte %u: 12-round=0x%02x, 24-round=0x%02x\n\n",
|
||||
(unsigned int) i, state_12[i], state_24[i]);
|
||||
} else {
|
||||
printf(" FAIL: 12-round and 24-round produce identical outputs\n\n");
|
||||
test_failures++;
|
||||
}
|
||||
|
||||
/* Test 8: Multiple permutations */
|
||||
printf("Test 8: Double permutation consistency\n");
|
||||
crypto_core_keccak1600_init(state);
|
||||
crypto_core_keccak1600_permute_24(state);
|
||||
memcpy(state_24, state, 200);
|
||||
crypto_core_keccak1600_permute_24(state);
|
||||
printf(" After 24+24 rounds: ");
|
||||
print_hex("", state, 32);
|
||||
|
||||
crypto_core_keccak1600_init(state);
|
||||
crypto_core_keccak1600_permute_12(state);
|
||||
memcpy(state_12, state, 200);
|
||||
crypto_core_keccak1600_permute_12(state);
|
||||
printf(" After 12+12 rounds: ");
|
||||
print_hex("", state, 32);
|
||||
printf("\n");
|
||||
|
||||
/* Final summary */
|
||||
printf("=== Test Summary ===\n");
|
||||
if (test_failures == 0) {
|
||||
printf("All tests PASSED!\n");
|
||||
return 0;
|
||||
} else {
|
||||
printf("FAILED: %d test(s) failed\n", test_failures);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
=== Keccak-1600 Core Function Tests ===
|
||||
|
||||
Test 1: API constants
|
||||
statebytes: 200
|
||||
PASS: statebytes = 200
|
||||
|
||||
Test 2: crypto_core_keccak1600_init
|
||||
PASS: State initialized to zeros
|
||||
|
||||
Test 3: crypto_core_keccak1600_xor_bytes and extract_bytes
|
||||
PASS: XOR and extract work correctly
|
||||
|
||||
Test 4: Keccak-f[1600] (24 rounds) - Zero input
|
||||
PASS: Keccak-f[1600] zero
|
||||
|
||||
Test 5: Keccak-f[1600] (24 rounds) - Pattern 0xa3 input
|
||||
PASS: Keccak-f[1600] pattern
|
||||
|
||||
Test 6: Keccak-p[1600,12] (12 rounds) - Zero input
|
||||
PASS: Keccak-p[1600,12] zero
|
||||
|
||||
Test 7: Verify 12-round and 24-round differ
|
||||
PASS: 12-round and 24-round produce different outputs
|
||||
First difference at byte 0: 12-round=0x17, 24-round=0xe7
|
||||
|
||||
Test 8: Double permutation consistency
|
||||
After 24+24 rounds: 3ccb6ef94d955c2d6db55770d02c336a6c6bd770128d3d0994d06955b2d9208a
|
||||
After 12+12 rounds: 048cbb36dc66034bc96a2de69835165f46e73b55de051b436c7a6154c9469f48
|
||||
|
||||
=== Test Summary ===
|
||||
All tests PASSED!
|
||||
@@ -0,0 +1,134 @@
|
||||
|
||||
#define TEST_NAME "xof_shake128"
|
||||
#include "cmptest.h"
|
||||
|
||||
typedef struct {
|
||||
const unsigned char *msg;
|
||||
size_t msg_len;
|
||||
const unsigned char *out;
|
||||
size_t out_len;
|
||||
} testvector;
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
/* Test vectors from NIST and various sources */
|
||||
static const unsigned char msg_empty[] = "";
|
||||
static const unsigned char msg_abc[] = { 0x61, 0x62, 0x63 };
|
||||
static const unsigned char msg_fox[] = { 0x54, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b,
|
||||
0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f,
|
||||
0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x73, 0x20, 0x6f,
|
||||
0x76, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c,
|
||||
0x61, 0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67 };
|
||||
|
||||
static const unsigned char out_empty_32[] = { 0x7f, 0x9c, 0x2b, 0xa4, 0xe8, 0x8f, 0x82, 0x7d,
|
||||
0x61, 0x60, 0x45, 0x50, 0x76, 0x05, 0x85, 0x3e,
|
||||
0xd7, 0x3b, 0x80, 0x93, 0xf6, 0xef, 0xbc, 0x88,
|
||||
0xeb, 0x1a, 0x6e, 0xac, 0xfa, 0x66, 0xef, 0x26 };
|
||||
|
||||
static const unsigned char out_empty_64[] = {
|
||||
0x7f, 0x9c, 0x2b, 0xa4, 0xe8, 0x8f, 0x82, 0x7d, 0x61, 0x60, 0x45, 0x50, 0x76,
|
||||
0x05, 0x85, 0x3e, 0xd7, 0x3b, 0x80, 0x93, 0xf6, 0xef, 0xbc, 0x88, 0xeb, 0x1a,
|
||||
0x6e, 0xac, 0xfa, 0x66, 0xef, 0x26, 0x3c, 0xb1, 0xee, 0xa9, 0x88, 0x00, 0x4b,
|
||||
0x93, 0x10, 0x3c, 0xfb, 0x0a, 0xee, 0xfd, 0x2a, 0x68, 0x6e, 0x01, 0xfa, 0x4a,
|
||||
0x58, 0xe8, 0xa3, 0x63, 0x9c, 0xa8, 0xa1, 0xe3, 0xf9, 0xae, 0x57, 0xe2
|
||||
};
|
||||
|
||||
static const unsigned char out_abc_32[] = { 0x58, 0x81, 0x09, 0x2d, 0xd8, 0x18, 0xbf, 0x5c,
|
||||
0xf8, 0xa3, 0xdd, 0xb7, 0x93, 0xfb, 0xcb, 0xa7,
|
||||
0x40, 0x97, 0xd5, 0xc5, 0x26, 0xa6, 0xd3, 0x5f,
|
||||
0x97, 0xb8, 0x33, 0x51, 0x94, 0x0f, 0x2c, 0xc8 };
|
||||
|
||||
static const unsigned char out_fox_32[] = { 0xf4, 0x20, 0x2e, 0x3c, 0x58, 0x52, 0xf9, 0x18,
|
||||
0x2a, 0x04, 0x30, 0xfd, 0x81, 0x44, 0xf0, 0xa7,
|
||||
0x4b, 0x95, 0xe7, 0x41, 0x7e, 0xca, 0xe1, 0x7d,
|
||||
0xb0, 0xf8, 0xcf, 0xee, 0xd0, 0xe3, 0xe6, 0x6e };
|
||||
|
||||
static const unsigned char out_fox_64[] = {
|
||||
0xf4, 0x20, 0x2e, 0x3c, 0x58, 0x52, 0xf9, 0x18, 0x2a, 0x04, 0x30, 0xfd, 0x81,
|
||||
0x44, 0xf0, 0xa7, 0x4b, 0x95, 0xe7, 0x41, 0x7e, 0xca, 0xe1, 0x7d, 0xb0, 0xf8,
|
||||
0xcf, 0xee, 0xd0, 0xe3, 0xe6, 0x6e, 0xb5, 0x58, 0x5e, 0xc6, 0xf8, 0x60, 0x21,
|
||||
0xca, 0xcf, 0x27, 0x2c, 0x79, 0x8b, 0xcf, 0x97, 0xd3, 0x68, 0xb8, 0x86, 0xb1,
|
||||
0x8f, 0xec, 0x3a, 0x57, 0x1f, 0x09, 0x60, 0x86, 0xa5, 0x23, 0x71, 0x7a
|
||||
};
|
||||
|
||||
testvector vectors[] = { { msg_empty, 0, out_empty_32, 32 },
|
||||
{ msg_empty, 0, out_empty_64, 64 },
|
||||
{ msg_abc, 3, out_abc_32, 32 },
|
||||
{ msg_fox, 43, out_fox_32, 32 },
|
||||
{ msg_fox, 43, out_fox_64, 64 } };
|
||||
|
||||
unsigned char out[256];
|
||||
crypto_xof_shake128_state state;
|
||||
size_t i, j;
|
||||
|
||||
/* Test constants */
|
||||
assert(crypto_xof_shake128_blockbytes() == 168);
|
||||
assert(crypto_xof_shake128_statebytes() == 256);
|
||||
assert(crypto_xof_shake128_domain_standard() == 0x1F);
|
||||
|
||||
/* Test one-shot API */
|
||||
for (i = 0; i < sizeof(vectors) / sizeof(vectors[0]); i++) {
|
||||
crypto_xof_shake128(out, vectors[i].out_len, vectors[i].msg, vectors[i].msg_len);
|
||||
if (memcmp(out, vectors[i].out, vectors[i].out_len) != 0) {
|
||||
printf("Test vector %zu failed (one-shot)\n", i);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Test streaming API */
|
||||
for (i = 0; i < sizeof(vectors) / sizeof(vectors[0]); i++) {
|
||||
crypto_xof_shake128_init(&state);
|
||||
crypto_xof_shake128_update(&state, vectors[i].msg, vectors[i].msg_len);
|
||||
crypto_xof_shake128_final(&state, out, vectors[i].out_len);
|
||||
if (memcmp(out, vectors[i].out, vectors[i].out_len) != 0) {
|
||||
printf("Test vector %zu failed (streaming)\n", i);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Test multiple squeeze calls */
|
||||
crypto_xof_shake128_init(&state);
|
||||
crypto_xof_shake128_update(&state, msg_abc, 3);
|
||||
crypto_xof_shake128_final(&state, out, 16);
|
||||
crypto_xof_shake128_squeeze(&state, out + 16, 16);
|
||||
if (memcmp(out, out_abc_32, 32) != 0) {
|
||||
printf("Multiple squeeze test failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Test custom domain byte produces different output */
|
||||
crypto_xof_shake128_init(&state);
|
||||
crypto_xof_shake128_update(&state, msg_abc, 3);
|
||||
crypto_xof_shake128_final(&state, out, 32);
|
||||
|
||||
crypto_xof_shake128_init_with_domain(&state, 0x99);
|
||||
crypto_xof_shake128_update(&state, msg_abc, 3);
|
||||
crypto_xof_shake128_final(&state, out + 32, 32);
|
||||
|
||||
if (memcmp(out, out + 32, 32) == 0) {
|
||||
printf("Custom domain byte test failed (outputs should differ)\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Test standard domain constant */
|
||||
crypto_xof_shake128_init_with_domain(&state, crypto_xof_shake128_domain_standard());
|
||||
crypto_xof_shake128_update(&state, msg_abc, 3);
|
||||
crypto_xof_shake128_final(&state, out + 64, 32);
|
||||
|
||||
if (memcmp(out, out + 64, 32) != 0) {
|
||||
printf("Domain constant test failed (should match standard init)\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Test cannot absorb after squeezing */
|
||||
crypto_xof_shake128_init(&state);
|
||||
crypto_xof_shake128_final(&state, out, 32);
|
||||
if (crypto_xof_shake128_update(&state, msg_abc, 3) == 0) {
|
||||
printf("Should not be able to absorb after squeezing\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("All SHAKE-128 tests passed\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
All SHAKE-128 tests passed
|
||||
@@ -0,0 +1,135 @@
|
||||
|
||||
#define TEST_NAME "xof_shake256"
|
||||
#include "cmptest.h"
|
||||
|
||||
typedef struct {
|
||||
const unsigned char *msg;
|
||||
size_t msg_len;
|
||||
const unsigned char *out;
|
||||
size_t out_len;
|
||||
} testvector;
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
/* Test vectors from NIST FIPS 202 and various sources */
|
||||
static const unsigned char msg_empty[] = "";
|
||||
static const unsigned char msg_abc[] = { 0x61, 0x62, 0x63 };
|
||||
static const unsigned char msg_fox[] = { 0x54, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b,
|
||||
0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f,
|
||||
0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x73, 0x20, 0x6f,
|
||||
0x76, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c,
|
||||
0x61, 0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67 };
|
||||
|
||||
/* SHAKE-256 test vectors */
|
||||
static const unsigned char out_empty_32[] = { 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 };
|
||||
|
||||
static const unsigned char out_empty_64[] = {
|
||||
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, 0xd7, 0x5d, 0xc4, 0xdd, 0xd8, 0xc0, 0xf2,
|
||||
0x00, 0xcb, 0x05, 0x01, 0x9d, 0x67, 0xb5, 0x92, 0xf6, 0xfc, 0x82, 0x1c, 0x49,
|
||||
0x47, 0x9a, 0xb4, 0x86, 0x40, 0x29, 0x2e, 0xac, 0xb3, 0xb7, 0xc4, 0xbe
|
||||
};
|
||||
|
||||
static const unsigned char out_abc_32[] = { 0x48, 0x33, 0x66, 0x60, 0x13, 0x60, 0xa8, 0x77,
|
||||
0x1c, 0x68, 0x63, 0x08, 0x0c, 0xc4, 0x11, 0x4d,
|
||||
0x8d, 0xb4, 0x45, 0x30, 0xf8, 0xf1, 0xe1, 0xee,
|
||||
0x4f, 0x94, 0xea, 0x37, 0xe7, 0x8b, 0x57, 0x39 };
|
||||
|
||||
static const unsigned char out_fox_32[] = { 0x2f, 0x67, 0x13, 0x43, 0xd9, 0xb2, 0xe1, 0x60,
|
||||
0x4d, 0xc9, 0xdc, 0xf0, 0x75, 0x3e, 0x5f, 0xe1,
|
||||
0x5c, 0x7c, 0x64, 0xa0, 0xd2, 0x83, 0xcb, 0xbf,
|
||||
0x72, 0x2d, 0x41, 0x1a, 0x0e, 0x36, 0xf6, 0xca };
|
||||
|
||||
static const unsigned char out_fox_64[] = {
|
||||
0x2f, 0x67, 0x13, 0x43, 0xd9, 0xb2, 0xe1, 0x60, 0x4d, 0xc9, 0xdc, 0xf0, 0x75,
|
||||
0x3e, 0x5f, 0xe1, 0x5c, 0x7c, 0x64, 0xa0, 0xd2, 0x83, 0xcb, 0xbf, 0x72, 0x2d,
|
||||
0x41, 0x1a, 0x0e, 0x36, 0xf6, 0xca, 0x1d, 0x01, 0xd1, 0x36, 0x9a, 0x23, 0x53,
|
||||
0x9c, 0xd8, 0x0f, 0x7c, 0x05, 0x4b, 0x6e, 0x5d, 0xaf, 0x9c, 0x96, 0x2c, 0xad,
|
||||
0x5b, 0x8e, 0xd5, 0xbd, 0x11, 0x99, 0x8b, 0x40, 0xd5, 0x73, 0x44, 0x42
|
||||
};
|
||||
|
||||
testvector vectors[] = { { msg_empty, 0, out_empty_32, 32 },
|
||||
{ msg_empty, 0, out_empty_64, 64 },
|
||||
{ msg_abc, 3, out_abc_32, 32 },
|
||||
{ msg_fox, 43, out_fox_32, 32 },
|
||||
{ msg_fox, 43, out_fox_64, 64 } };
|
||||
|
||||
unsigned char out[256];
|
||||
crypto_xof_shake256_state state;
|
||||
size_t i;
|
||||
|
||||
/* Test constants */
|
||||
assert(crypto_xof_shake256_blockbytes() == 136);
|
||||
assert(crypto_xof_shake256_statebytes() == 256);
|
||||
assert(crypto_xof_shake256_domain_standard() == 0x1F);
|
||||
|
||||
/* Test one-shot API */
|
||||
for (i = 0; i < sizeof(vectors) / sizeof(vectors[0]); i++) {
|
||||
crypto_xof_shake256(out, vectors[i].out_len, vectors[i].msg, vectors[i].msg_len);
|
||||
if (memcmp(out, vectors[i].out, vectors[i].out_len) != 0) {
|
||||
printf("Test vector %zu failed (one-shot)\n", i);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Test streaming API */
|
||||
for (i = 0; i < sizeof(vectors) / sizeof(vectors[0]); i++) {
|
||||
crypto_xof_shake256_init(&state);
|
||||
crypto_xof_shake256_update(&state, vectors[i].msg, vectors[i].msg_len);
|
||||
crypto_xof_shake256_final(&state, out, vectors[i].out_len);
|
||||
if (memcmp(out, vectors[i].out, vectors[i].out_len) != 0) {
|
||||
printf("Test vector %zu failed (streaming)\n", i);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Test multiple squeeze calls */
|
||||
crypto_xof_shake256_init(&state);
|
||||
crypto_xof_shake256_update(&state, msg_abc, 3);
|
||||
crypto_xof_shake256_final(&state, out, 16);
|
||||
crypto_xof_shake256_squeeze(&state, out + 16, 16);
|
||||
if (memcmp(out, out_abc_32, 32) != 0) {
|
||||
printf("Multiple squeeze test failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Test custom domain byte produces different output */
|
||||
crypto_xof_shake256_init(&state);
|
||||
crypto_xof_shake256_update(&state, msg_abc, 3);
|
||||
crypto_xof_shake256_final(&state, out, 32);
|
||||
|
||||
crypto_xof_shake256_init_with_domain(&state, 0x99);
|
||||
crypto_xof_shake256_update(&state, msg_abc, 3);
|
||||
crypto_xof_shake256_final(&state, out + 32, 32);
|
||||
|
||||
if (memcmp(out, out + 32, 32) == 0) {
|
||||
printf("Custom domain byte test failed (outputs should differ)\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Test standard domain constant */
|
||||
crypto_xof_shake256_init_with_domain(&state, crypto_xof_shake256_domain_standard());
|
||||
crypto_xof_shake256_update(&state, msg_abc, 3);
|
||||
crypto_xof_shake256_final(&state, out + 64, 32);
|
||||
|
||||
if (memcmp(out, out + 64, 32) != 0) {
|
||||
printf("Domain constant test failed (should match standard init)\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Test cannot absorb after squeezing */
|
||||
crypto_xof_shake256_init(&state);
|
||||
crypto_xof_shake256_final(&state, out, 32);
|
||||
if (crypto_xof_shake256_update(&state, msg_abc, 3) == 0) {
|
||||
printf("Should not be able to absorb after squeezing\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("All SHAKE-256 tests passed\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
All SHAKE-256 tests passed
|
||||
@@ -0,0 +1,134 @@
|
||||
|
||||
#define TEST_NAME "xof_turboshake128"
|
||||
#include "cmptest.h"
|
||||
|
||||
typedef struct {
|
||||
const unsigned char *msg;
|
||||
size_t msg_len;
|
||||
const unsigned char *out;
|
||||
size_t out_len;
|
||||
} testvector;
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
/* Test vectors from TurboSHAKE reference implementation */
|
||||
static const unsigned char msg_empty[] = "";
|
||||
static const unsigned char msg_abc[] = { 0x61, 0x62, 0x63 };
|
||||
static const unsigned char msg_fox[] = { 0x54, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b,
|
||||
0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f,
|
||||
0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x73, 0x20, 0x6f,
|
||||
0x76, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c,
|
||||
0x61, 0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67 };
|
||||
|
||||
static const unsigned char out_empty_32[] = { 0x86, 0x8c, 0xbd, 0x53, 0xb0, 0x78, 0x20, 0x5a,
|
||||
0xbb, 0x85, 0x81, 0x5d, 0x94, 0x1f, 0x7d, 0x03,
|
||||
0x76, 0xbf, 0xf5, 0xb8, 0x88, 0x8a, 0x6a, 0x2d,
|
||||
0x03, 0x48, 0x3a, 0xfb, 0xaf, 0x83, 0x96, 0x7f };
|
||||
|
||||
static const unsigned char out_empty_64[] = {
|
||||
0x86, 0x8c, 0xbd, 0x53, 0xb0, 0x78, 0x20, 0x5a, 0xbb, 0x85, 0x81, 0x5d, 0x94,
|
||||
0x1f, 0x7d, 0x03, 0x76, 0xbf, 0xf5, 0xb8, 0x88, 0x8a, 0x6a, 0x2d, 0x03, 0x48,
|
||||
0x3a, 0xfb, 0xaf, 0x83, 0x96, 0x7f, 0x22, 0x6e, 0x2c, 0xad, 0x5e, 0x7b, 0x1e,
|
||||
0xc4, 0xca, 0x72, 0x23, 0x6f, 0x07, 0x64, 0x62, 0x19, 0x9f, 0xea, 0x48, 0xc9,
|
||||
0x34, 0x38, 0xad, 0x4c, 0x49, 0xc7, 0x67, 0xf9, 0x41, 0x7b, 0xe7, 0xc5
|
||||
};
|
||||
|
||||
static const unsigned char out_abc_32[] = { 0x59, 0xcc, 0xfc, 0x22, 0xa3, 0xb8, 0x47, 0x42,
|
||||
0x58, 0x6b, 0x41, 0xf5, 0x1f, 0x8a, 0x94, 0x73,
|
||||
0x8d, 0xd0, 0x2b, 0xc7, 0x45, 0x51, 0xeb, 0x0e,
|
||||
0xf5, 0x0f, 0xc4, 0x09, 0x4e, 0xb0, 0xfc, 0x7b };
|
||||
|
||||
static const unsigned char out_fox_32[] = { 0x6f, 0x16, 0x24, 0x47, 0xdd, 0xd3, 0x30, 0xc8,
|
||||
0xee, 0x8b, 0x21, 0x89, 0x88, 0xca, 0x1b, 0xef,
|
||||
0x35, 0xd9, 0x02, 0xa5, 0xfd, 0x6b, 0xaa, 0xf3,
|
||||
0x44, 0x20, 0x48, 0x32, 0x26, 0xa1, 0x72, 0x4a };
|
||||
|
||||
static const unsigned char out_fox_64[] = {
|
||||
0x6f, 0x16, 0x24, 0x47, 0xdd, 0xd3, 0x30, 0xc8, 0xee, 0x8b, 0x21, 0x89, 0x88,
|
||||
0xca, 0x1b, 0xef, 0x35, 0xd9, 0x02, 0xa5, 0xfd, 0x6b, 0xaa, 0xf3, 0x44, 0x20,
|
||||
0x48, 0x32, 0x26, 0xa1, 0x72, 0x4a, 0xb9, 0x02, 0xc6, 0x85, 0x7e, 0xbd, 0x0b,
|
||||
0xa1, 0x76, 0x00, 0xdf, 0x9e, 0xe4, 0x9a, 0x06, 0x18, 0x03, 0x36, 0x92, 0x2d,
|
||||
0x9b, 0x61, 0x80, 0x7e, 0x8f, 0xc8, 0x03, 0xec, 0xb0, 0x1a, 0x81, 0xf9
|
||||
};
|
||||
|
||||
testvector vectors[] = { { msg_empty, 0, out_empty_32, 32 },
|
||||
{ msg_empty, 0, out_empty_64, 64 },
|
||||
{ msg_abc, 3, out_abc_32, 32 },
|
||||
{ msg_fox, 43, out_fox_32, 32 },
|
||||
{ msg_fox, 43, out_fox_64, 64 } };
|
||||
|
||||
unsigned char out[256];
|
||||
crypto_xof_turboshake128_state state;
|
||||
size_t i;
|
||||
|
||||
/* Test constants */
|
||||
assert(crypto_xof_turboshake128_blockbytes() == 168);
|
||||
assert(crypto_xof_turboshake128_statebytes() == 256);
|
||||
assert(crypto_xof_turboshake128_domain_standard() == 0x01);
|
||||
|
||||
/* Test one-shot API */
|
||||
for (i = 0; i < sizeof(vectors) / sizeof(vectors[0]); i++) {
|
||||
crypto_xof_turboshake128(out, vectors[i].out_len, vectors[i].msg, vectors[i].msg_len);
|
||||
if (memcmp(out, vectors[i].out, vectors[i].out_len) != 0) {
|
||||
printf("Test vector %zu failed (one-shot)\n", i);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Test streaming API */
|
||||
for (i = 0; i < sizeof(vectors) / sizeof(vectors[0]); i++) {
|
||||
crypto_xof_turboshake128_init(&state);
|
||||
crypto_xof_turboshake128_update(&state, vectors[i].msg, vectors[i].msg_len);
|
||||
crypto_xof_turboshake128_final(&state, out, vectors[i].out_len);
|
||||
if (memcmp(out, vectors[i].out, vectors[i].out_len) != 0) {
|
||||
printf("Test vector %zu failed (streaming)\n", i);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Test multiple squeeze calls */
|
||||
crypto_xof_turboshake128_init(&state);
|
||||
crypto_xof_turboshake128_update(&state, msg_abc, 3);
|
||||
crypto_xof_turboshake128_final(&state, out, 16);
|
||||
crypto_xof_turboshake128_squeeze(&state, out + 16, 16);
|
||||
if (memcmp(out, out_abc_32, 32) != 0) {
|
||||
printf("Multiple squeeze test failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Test custom domain byte produces different output */
|
||||
crypto_xof_turboshake128_init(&state);
|
||||
crypto_xof_turboshake128_update(&state, msg_abc, 3);
|
||||
crypto_xof_turboshake128_final(&state, out, 32);
|
||||
|
||||
crypto_xof_turboshake128_init_with_domain(&state, 0x99);
|
||||
crypto_xof_turboshake128_update(&state, msg_abc, 3);
|
||||
crypto_xof_turboshake128_final(&state, out + 32, 32);
|
||||
|
||||
if (memcmp(out, out + 32, 32) == 0) {
|
||||
printf("Custom domain byte test failed (outputs should differ)\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Test standard domain constant */
|
||||
crypto_xof_turboshake128_init_with_domain(&state, crypto_xof_turboshake128_domain_standard());
|
||||
crypto_xof_turboshake128_update(&state, msg_abc, 3);
|
||||
crypto_xof_turboshake128_final(&state, out + 64, 32);
|
||||
|
||||
if (memcmp(out, out + 64, 32) != 0) {
|
||||
printf("Domain constant test failed (should match standard init)\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Test cannot absorb after squeezing */
|
||||
crypto_xof_turboshake128_init(&state);
|
||||
crypto_xof_turboshake128_final(&state, out, 32);
|
||||
if (crypto_xof_turboshake128_update(&state, msg_abc, 3) == 0) {
|
||||
printf("Should not be able to absorb after squeezing\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("All TurboSHAKE-128 tests passed\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
All TurboSHAKE-128 tests passed
|
||||
@@ -0,0 +1,134 @@
|
||||
|
||||
#define TEST_NAME "xof_turboshake256"
|
||||
#include "cmptest.h"
|
||||
|
||||
typedef struct {
|
||||
const unsigned char *msg;
|
||||
size_t msg_len;
|
||||
const unsigned char *out;
|
||||
size_t out_len;
|
||||
} testvector;
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
/* Test vectors from TurboSHAKE reference implementation */
|
||||
static const unsigned char msg_empty[] = "";
|
||||
static const unsigned char msg_abc[] = { 0x61, 0x62, 0x63 };
|
||||
static const unsigned char msg_fox[] = { 0x54, 0x68, 0x65, 0x20, 0x71, 0x75, 0x69, 0x63, 0x6b,
|
||||
0x20, 0x62, 0x72, 0x6f, 0x77, 0x6e, 0x20, 0x66, 0x6f,
|
||||
0x78, 0x20, 0x6a, 0x75, 0x6d, 0x70, 0x73, 0x20, 0x6f,
|
||||
0x76, 0x65, 0x72, 0x20, 0x74, 0x68, 0x65, 0x20, 0x6c,
|
||||
0x61, 0x7a, 0x79, 0x20, 0x64, 0x6f, 0x67 };
|
||||
|
||||
static const unsigned char out_empty_32[] = { 0xe3, 0xdd, 0x2d, 0xf0, 0x94, 0x3b, 0xde, 0x6d,
|
||||
0x82, 0xe3, 0x9e, 0xc3, 0x60, 0x59, 0xf3, 0x5c,
|
||||
0xd7, 0x67, 0x20, 0xe2, 0xdf, 0x38, 0xcc, 0x6b,
|
||||
0x10, 0xb6, 0x9f, 0xdd, 0xfc, 0xaa, 0x3a, 0x4a };
|
||||
|
||||
static const unsigned char out_empty_64[] = {
|
||||
0xe3, 0xdd, 0x2d, 0xf0, 0x94, 0x3b, 0xde, 0x6d, 0x82, 0xe3, 0x9e, 0xc3, 0x60,
|
||||
0x59, 0xf3, 0x5c, 0xd7, 0x67, 0x20, 0xe2, 0xdf, 0x38, 0xcc, 0x6b, 0x10, 0xb6,
|
||||
0x9f, 0xdd, 0xfc, 0xaa, 0x3a, 0x4a, 0x72, 0xfb, 0xbb, 0xe4, 0x2c, 0x00, 0xce,
|
||||
0xd7, 0xaa, 0x88, 0xe2, 0x6d, 0x46, 0x75, 0xdd, 0x6e, 0x2c, 0x43, 0xc4, 0x41,
|
||||
0x3c, 0x4e, 0xa4, 0xd4, 0x4b, 0xb1, 0x70, 0xf0, 0x3a, 0x98, 0x1c, 0xab
|
||||
};
|
||||
|
||||
static const unsigned char out_abc_32[] = { 0x88, 0xfb, 0x36, 0x9d, 0x5d, 0x85, 0x6b, 0x22,
|
||||
0xcc, 0xe4, 0xd6, 0xa2, 0x40, 0x56, 0x60, 0x0b,
|
||||
0xa7, 0x27, 0x44, 0xcd, 0xb3, 0x26, 0x37, 0x49,
|
||||
0x07, 0x91, 0xcc, 0xd9, 0x85, 0x3b, 0xc9, 0x14 };
|
||||
|
||||
static const unsigned char out_fox_32[] = { 0xe9, 0x17, 0xad, 0xfe, 0x65, 0x54, 0x6d, 0x28,
|
||||
0xa1, 0x64, 0x57, 0x61, 0x07, 0xe5, 0xd4, 0x77,
|
||||
0x4d, 0x46, 0x64, 0x42, 0xc5, 0xe8, 0x86, 0xf1,
|
||||
0xb8, 0xc9, 0xee, 0xab, 0x5d, 0x3f, 0xbf, 0xa8 };
|
||||
|
||||
static const unsigned char out_fox_64[] = {
|
||||
0xe9, 0x17, 0xad, 0xfe, 0x65, 0x54, 0x6d, 0x28, 0xa1, 0x64, 0x57, 0x61, 0x07,
|
||||
0xe5, 0xd4, 0x77, 0x4d, 0x46, 0x64, 0x42, 0xc5, 0xe8, 0x86, 0xf1, 0xb8, 0xc9,
|
||||
0xee, 0xab, 0x5d, 0x3f, 0xbf, 0xa8, 0x96, 0x53, 0xea, 0xd9, 0x8b, 0x2a, 0x52,
|
||||
0x05, 0x78, 0x38, 0x9b, 0x24, 0xeb, 0xab, 0x8f, 0xcf, 0xe9, 0x12, 0x3e, 0x71,
|
||||
0x42, 0x1a, 0x14, 0xfb, 0xe2, 0x4e, 0x13, 0xe6, 0x27, 0x31, 0x3a, 0xa1
|
||||
};
|
||||
|
||||
testvector vectors[] = { { msg_empty, 0, out_empty_32, 32 },
|
||||
{ msg_empty, 0, out_empty_64, 64 },
|
||||
{ msg_abc, 3, out_abc_32, 32 },
|
||||
{ msg_fox, 43, out_fox_32, 32 },
|
||||
{ msg_fox, 43, out_fox_64, 64 } };
|
||||
|
||||
unsigned char out[256];
|
||||
crypto_xof_turboshake256_state state;
|
||||
size_t i;
|
||||
|
||||
/* Test constants */
|
||||
assert(crypto_xof_turboshake256_blockbytes() == 136);
|
||||
assert(crypto_xof_turboshake256_statebytes() == 256);
|
||||
assert(crypto_xof_turboshake256_domain_standard() == 0x01);
|
||||
|
||||
/* Test one-shot API */
|
||||
for (i = 0; i < sizeof(vectors) / sizeof(vectors[0]); i++) {
|
||||
crypto_xof_turboshake256(out, vectors[i].out_len, vectors[i].msg, vectors[i].msg_len);
|
||||
if (memcmp(out, vectors[i].out, vectors[i].out_len) != 0) {
|
||||
printf("Test vector %zu failed (one-shot)\n", i);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Test streaming API */
|
||||
for (i = 0; i < sizeof(vectors) / sizeof(vectors[0]); i++) {
|
||||
crypto_xof_turboshake256_init(&state);
|
||||
crypto_xof_turboshake256_update(&state, vectors[i].msg, vectors[i].msg_len);
|
||||
crypto_xof_turboshake256_final(&state, out, vectors[i].out_len);
|
||||
if (memcmp(out, vectors[i].out, vectors[i].out_len) != 0) {
|
||||
printf("Test vector %zu failed (streaming)\n", i);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
/* Test multiple squeeze calls */
|
||||
crypto_xof_turboshake256_init(&state);
|
||||
crypto_xof_turboshake256_update(&state, msg_abc, 3);
|
||||
crypto_xof_turboshake256_final(&state, out, 16);
|
||||
crypto_xof_turboshake256_squeeze(&state, out + 16, 16);
|
||||
if (memcmp(out, out_abc_32, 32) != 0) {
|
||||
printf("Multiple squeeze test failed\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Test custom domain byte produces different output */
|
||||
crypto_xof_turboshake256_init(&state);
|
||||
crypto_xof_turboshake256_update(&state, msg_abc, 3);
|
||||
crypto_xof_turboshake256_final(&state, out, 32);
|
||||
|
||||
crypto_xof_turboshake256_init_with_domain(&state, 0x99);
|
||||
crypto_xof_turboshake256_update(&state, msg_abc, 3);
|
||||
crypto_xof_turboshake256_final(&state, out + 32, 32);
|
||||
|
||||
if (memcmp(out, out + 32, 32) == 0) {
|
||||
printf("Custom domain byte test failed (outputs should differ)\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Test standard domain constant */
|
||||
crypto_xof_turboshake256_init_with_domain(&state, crypto_xof_turboshake256_domain_standard());
|
||||
crypto_xof_turboshake256_update(&state, msg_abc, 3);
|
||||
crypto_xof_turboshake256_final(&state, out + 64, 32);
|
||||
|
||||
if (memcmp(out, out + 64, 32) != 0) {
|
||||
printf("Domain constant test failed (should match standard init)\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Test cannot absorb after squeezing */
|
||||
crypto_xof_turboshake256_init(&state);
|
||||
crypto_xof_turboshake256_final(&state, out, 32);
|
||||
if (crypto_xof_turboshake256_update(&state, msg_abc, 3) == 0) {
|
||||
printf("Should not be able to absorb after squeezing\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("All TurboSHAKE-256 tests passed\n");
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
All TurboSHAKE-256 tests passed
|
||||
Reference in New Issue
Block a user