diff --git a/common/fips202.c b/common/fips202.c index 66e97ae2..d61c28fd 100644 --- a/common/fips202.c +++ b/common/fips202.c @@ -402,6 +402,157 @@ static void keccak_squeezeblocks(uint8_t *h, size_t nblocks, } } +/************************************************* + * Name: keccak_inc_init + * + * Description: Initializes the incremental Keccak state to zero. + * + * Arguments: - uint64_t *s_inc: pointer to input/output incremental state + * First 25 values represent Keccak state. + * 26th value represents either the number of absorbed bytes + * that have not been permuted, or not-yet-squeezed bytes. + **************************************************/ +static void keccak_inc_init(uint64_t *s_inc) { + size_t i; + + for (i = 0; i < 25; ++i) { + s_inc[i] = 0; + } + s_inc[25] = 0; +} + +/************************************************* + * Name: keccak_inc_absorb + * + * Description: Incremental keccak absorb + * Preceded by keccak_inc_init, succeeded by keccak_inc_finalize + * + * Arguments: - uint64_t *s_inc: pointer to input/output incremental state + * First 25 values represent Keccak state. + * 26th value represents either the number of absorbed bytes + * that have not been permuted, or not-yet-squeezed bytes. + * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128) + * - const uint8_t *m: pointer to input to be absorbed into s + * - size_t mlen: length of input in bytes + **************************************************/ +static void keccak_inc_absorb(uint64_t *s_inc, uint32_t r, const uint8_t *m, + size_t mlen) { + size_t i; + + /* Recall that s_inc[25] is the non-absorbed bytes xored into the state */ + while (mlen + s_inc[25] >= r) { + for (i = 0; i < r - s_inc[25]; i++) { + /* Take the i'th byte from message + xor with the s_inc[25] + i'th byte of the state; little-endian */ + s_inc[(s_inc[25] + i) >> 3] ^= (uint64_t)m[i] << (8 * ((s_inc[25] + i) & 0x07)); + } + mlen -= (size_t)(r - s_inc[25]); + m += r - s_inc[25]; + s_inc[25] = 0; + + KeccakF1600_StatePermute(s_inc); + } + + for (i = 0; i < mlen; i++) { + s_inc[(s_inc[25] + i) >> 3] ^= (uint64_t)m[i] << (8 * ((s_inc[25] + i) & 0x07)); + } + s_inc[25] += mlen; +} + +/************************************************* + * Name: keccak_inc_finalize + * + * Description: Finalizes Keccak absorb phase, prepares for squeezing + * + * Arguments: - uint64_t *s_inc: pointer to input/output incremental state + * First 25 values represent Keccak state. + * 26th value represents either the number of absorbed bytes + * that have not been permuted, or not-yet-squeezed bytes. + * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128) + * - uint8_t p: domain-separation byte for different + * Keccak-derived functions + **************************************************/ +static void keccak_inc_finalize(uint64_t *s_inc, uint32_t r, uint8_t p) { + /* After keccak_inc_absorb, we are guaranteed that s_inc[25] < r, + so we can always use one more byte for p in the current state. */ + s_inc[s_inc[25] >> 3] ^= (uint64_t)p << (8 * (s_inc[25] & 0x07)); + s_inc[(r - 1) >> 3] ^= (uint64_t)128 << (8 * ((r - 1) & 0x07)); + s_inc[25] = 0; +} + +/************************************************* + * Name: keccak_inc_squeeze + * + * Description: Incremental Keccak squeeze; can be called on byte-level + * + * Arguments: - uint8_t *h: pointer to output bytes + * - size_t outlen: number of bytes to be squeezed + * - uint64_t *s_inc: pointer to input/output incremental state + * First 25 values represent Keccak state. + * 26th value represents either the number of absorbed bytes + * that have not been permuted, or not-yet-squeezed bytes. + * - uint32_t r: rate in bytes (e.g., 168 for SHAKE128) + **************************************************/ +static void keccak_inc_squeeze(uint8_t *h, size_t outlen, + uint64_t *s_inc, uint32_t r) { + size_t i; + + /* First consume any bytes we still have sitting around */ + for (i = 0; i < outlen && i < s_inc[25]; i++) { + /* There are s_inc[25] bytes left, so r - s_inc[25] is the first + available byte. We consume from there, i.e., up to r. */ + h[i] = (uint8_t)(s_inc[(r - s_inc[25] + i) >> 3] >> (8 * ((r - s_inc[25] + i) & 0x07))); + } + h += i; + outlen -= i; + s_inc[25] -= i; + + /* Then squeeze the remaining necessary blocks */ + while (outlen > 0) { + KeccakF1600_StatePermute(s_inc); + + for (i = 0; i < outlen && i < r; i++) { + h[i] = (uint8_t)(s_inc[i >> 3] >> (8 * (i & 0x07))); + } + h += i; + outlen -= i; + s_inc[25] = r - i; + } +} + +void shake128_inc_init(uint64_t *s_inc) { + keccak_inc_init(s_inc); +} + +void shake128_inc_absorb(uint64_t *s_inc, const uint8_t *input, size_t inlen) { + keccak_inc_absorb(s_inc, SHAKE128_RATE, input, inlen); +} + +void shake128_inc_finalize(uint64_t *s_inc) { + keccak_inc_finalize(s_inc, SHAKE128_RATE, 0x1F); +} + +void shake128_inc_squeeze(uint8_t *output, size_t outlen, uint64_t *s_inc) { + keccak_inc_squeeze(output, outlen, s_inc, SHAKE128_RATE); +} + +void shake256_inc_init(uint64_t *s_inc) { + keccak_inc_init(s_inc); +} + +void shake256_inc_absorb(uint64_t *s_inc, const uint8_t *input, size_t inlen) { + keccak_inc_absorb(s_inc, SHAKE256_RATE, input, inlen); +} + +void shake256_inc_finalize(uint64_t *s_inc) { + keccak_inc_finalize(s_inc, SHAKE256_RATE, 0x1F); +} + +void shake256_inc_squeeze(uint8_t *output, size_t outlen, uint64_t *s_inc) { + keccak_inc_squeeze(output, outlen, s_inc, SHAKE256_RATE); +} + + /************************************************* * Name: shake128_absorb * diff --git a/common/fips202.h b/common/fips202.h index bcd5aa96..4a28d67b 100644 --- a/common/fips202.h +++ b/common/fips202.h @@ -13,10 +13,19 @@ void shake128_absorb(uint64_t *s, const uint8_t *input, size_t inlen); void shake128_squeezeblocks(uint8_t *output, size_t nblocks, uint64_t *s); -void shake256_absorb(uint64_t *s, const uint8_t *input, size_t inlen); +void shake128_inc_init(uint64_t *s_inc); +void shake128_inc_absorb(uint64_t *s_inc, const uint8_t *input, size_t inlen); +void shake128_inc_finalize(uint64_t *s_inc); +void shake128_inc_squeeze(uint8_t *output, size_t outlen, uint64_t *s_inc); +void shake256_absorb(uint64_t *s, const uint8_t *input, size_t inlen); void shake256_squeezeblocks(uint8_t *output, size_t nblocks, uint64_t *s); +void shake256_inc_init(uint64_t *s_inc); +void shake256_inc_absorb(uint64_t *s_inc, const uint8_t *input, size_t inlen); +void shake256_inc_finalize(uint64_t *s_inc); +void shake256_inc_squeeze(uint8_t *output, size_t outlen, uint64_t *s_inc); + void shake128(uint8_t *output, size_t outlen, const uint8_t *input, size_t inlen); diff --git a/test/Makefile b/test/Makefile index 5d184dff..0557b48e 100644 --- a/test/Makefile +++ b/test/Makefile @@ -17,7 +17,7 @@ DEST_DIR=../bin # This -Wall was supported by the European Commission through the ERC Starting Grant 805031 (EPOQUE) CFLAGS=-Wall -Wextra -Wpedantic -Werror -std=c99 -I$(COMMON_DIR) $(EXTRAFLAGS) -all: $(DEST_DIR)/functest_$(SCHEME)_$(IMPLEMENTATION) $(DEST_DIR)/testvectors_$(SCHEME)_$(IMPLEMENTATION) +all: $(DEST_DIR)/functest_$(SCHEME)_$(IMPLEMENTATION) $(DEST_DIR)/testvectors_$(SCHEME)_$(IMPLEMENTATION) $(DEST_DIR)/test_fips202 .PHONY: build-scheme build-scheme: @@ -33,6 +33,10 @@ functest: $(DEST_DIR)/functest_$(SCHEME)_$(IMPLEMENTATION) .PHONY: testvectors testvectors: $(DEST_DIR)/testvectors_$(SCHEME)_$(IMPLEMENTATION) +$(DEST_DIR)/test_fips202: common/fips202.c $(COMMON_FILES) + mkdir -p $(DEST_DIR) + $(CC) $(CFLAGS) $< $(COMMON_FILES) -o $@ + $(DEST_DIR)/functest_$(SCHEME)_$(IMPLEMENTATION): build-scheme crypto_$(TYPE)/functest.c $(COMMON_FILES) $(COMMON_DIR)/randombytes.c $(COMMON_HEADERS) mkdir -p $(DEST_DIR) $(CC) $(CFLAGS) -DPQCLEAN_NAMESPACE=PQCLEAN_$(SCHEME_UPPERCASE)_$(IMPLEMENTATION_UPPERCASE) -I$(SCHEME_DIR) crypto_$(TYPE)/functest.c $(COMMON_FILES) $(COMMON_DIR)/notrandombytes.c -o $@ -L$(SCHEME_DIR) -l$(SCHEME)_$(IMPLEMENTATION) @@ -45,3 +49,4 @@ $(DEST_DIR)/testvectors_$(SCHEME)_$(IMPLEMENTATION): build-scheme crypto_$(TYPE) clean: $(RM) $(DEST_DIR)/functest_$(SCHEME)_$(IMPLEMENTATION) $(RM) $(DEST_DIR)/testvectors_$(SCHEME)_$(IMPLEMENTATION) + $(RM) $(DEST_DIR)/test_fips202 diff --git a/test/common/fips202.c b/test/common/fips202.c new file mode 100644 index 00000000..e559d07f --- /dev/null +++ b/test/common/fips202.c @@ -0,0 +1,236 @@ +#include "fips202.h" + +#include +#include +#include +#include + +const unsigned char plaintext[44] = "The quick brown fox jumps over the lazy dog"; +const unsigned char expected[512] = { + 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, 0x37, 0x32, 0xd5, 0x0d, 0xb2, 0xb0, 0xb7, 0x99, + 0x8b, 0x41, 0x17, 0xae, 0x66, 0xa7, 0x61, 0xcc, 0xf1, 0x84, 0x7a, 0x16, + 0x16, 0xf4, 0xc0, 0x7d, 0x51, 0x78, 0xd0, 0xd9, 0x65, 0xf9, 0xfe, 0xba, + 0x35, 0x14, 0x20, 0xf8, 0xbf, 0xb6, 0xf5, 0xab, 0x9a, 0x0c, 0xb1, 0x02, + 0x56, 0x8e, 0xab, 0xf3, 0xdf, 0xa4, 0xe2, 0x22, 0x79, 0xf8, 0x08, 0x2d, + 0xce, 0x81, 0x43, 0xeb, 0x78, 0x23, 0x5a, 0x1a, 0x54, 0x91, 0x4a, 0xb7, + 0x1a, 0xbb, 0x07, 0xf2, 0xf3, 0x64, 0x84, 0x68, 0x37, 0x0b, 0x9f, 0xbb, + 0x07, 0x1e, 0x07, 0x4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b5, 0x82, 0x2d +}; + +static int test_shake128_incremental(void) { + unsigned char input[512]; + unsigned char check[512]; + unsigned char output[512]; + uint64_t s_inc_absorb[26]; + uint64_t s_inc_squeeze[26]; + uint64_t s_inc_squeeze_all[26]; + uint64_t s_inc_both[26]; + uint64_t s_combined[25]; + int i; + int absorbed; + int squeezed; + int returncode = 0; + + for (i = 0; i < 512; i++) { + input[i] = i; + } + + shake128(check, 512, input, 512); + + shake128_inc_init(s_inc_absorb); + + absorbed = 0; + for (i = 0; i < 512 && absorbed + i <= 512; i++) { + shake128_inc_absorb(s_inc_absorb, input + absorbed, i); + absorbed += i; + } + shake128_inc_absorb(s_inc_absorb, input + absorbed, 512 - absorbed); + + shake128_inc_finalize(s_inc_absorb); + + shake128_absorb(s_combined, input, 512); + + if (memcmp(s_inc_absorb, s_combined, 25 * sizeof(uint64_t))) { + printf("ERROR shake128 state after incremental absorb did not match all-at-once absorb.\n"); + printf(" Expected: "); + for (i = 0; i < 25; i++) { + printf("%016" PRIx64, s_combined[i]); + } + printf("\n"); + printf(" State: "); + for (i = 0; i < 25; i++) { + printf("%016" PRIx64, s_inc_absorb[i]); + } + printf("\n"); + for (i = 0; i < 8 * 25; i++) { + if (((s_combined[i >> 3] >> (8*(i & 0x7))) & 0xFF) != + ((s_inc_absorb[i >> 3] >> (8*(i & 0x7))) & 0xFF)) { + printf(" First occurred in int %d, byte %d (%02X should be %02X)\n", + i >> 3, i & 0x7, + (uint8_t)((s_inc_absorb[i >> 3] >> (8*(i & 0x7))) & 0xFF), + (uint8_t)((s_combined[i >> 3] >> (8*(i & 0x7))) & 0xFF)); + break; + } + } + returncode = 1; + } + + memcpy(s_inc_both, s_inc_absorb, 26 * sizeof(uint64_t)); + + shake128_squeezeblocks(output, 3, s_inc_absorb); + + if (memcmp(check, output, 3*SHAKE128_RATE)) { + printf("ERROR shake128 incremental absorb did not match shake128.\n"); + printf(" Expected: "); + for (i = 0; i < 3*SHAKE128_RATE; i++) { + printf("%02X", check[i]); + } + printf("\n"); + printf(" Received: "); + for (i = 0; i < 3*SHAKE128_RATE; i++) { + printf("%02X", output[i]); + } + printf("\n"); + returncode = 1; + } + + shake128_absorb(s_inc_squeeze, input, 512); + s_inc_squeeze[25] = 0; + + memcpy(s_inc_squeeze_all, s_inc_squeeze, 26 * sizeof(uint64_t)); + + shake128_inc_squeeze(output, 512, s_inc_squeeze_all); + + if (memcmp(check, output, 512)) { + printf("ERROR shake128 incremental squeeze-all did not match shake128.\n"); + printf(" Expected: "); + for (i = 0; i < 512; i++) { + printf("%02X", check[i]); + } + printf("\n"); + printf(" Received: "); + for (i = 0; i < 512; i++) { + printf("%02X", output[i]); + } + printf("\n"); + returncode = 1; + } + + squeezed = 0; + memset(output, 0, 512); + for (i = 0; i < 512 && squeezed + i <= 512; i++) { + shake128_inc_squeeze(output + squeezed, i, s_inc_squeeze); + squeezed += i; + } + shake128_inc_squeeze(output + squeezed, 512 - squeezed, s_inc_squeeze); + + if (memcmp(check, output, 512)) { + printf("ERROR shake128 incremental squeeze did not match shake128.\n"); + printf(" Expected: "); + for (i = 0; i < 512; i++) { + printf("%02X", check[i]); + } + printf("\n"); + printf(" Received: "); + for (i = 0; i < 512; i++) { + printf("%02X", output[i]); + } + printf("\n"); + returncode = 1; + } + + squeezed = 0; + memset(output, 0, 512); + for (i = 0; i < 512 && squeezed + i <= 512; i++) { + shake128_inc_squeeze(output + squeezed, i, s_inc_both); + squeezed += i; + } + shake128_inc_squeeze(output + squeezed, 512 - squeezed, s_inc_both); + + if (memcmp(check, output, 512)) { + printf("ERROR shake128 incremental absorb + squeeze did not match shake128.\n"); + printf(" Expected: "); + for (i = 0; i < 512; i++) { + printf("%02X", check[i]); + } + printf("\n"); + printf(" Received: "); + for (i = 0; i < 512; i++) { + printf("%02X", output[i]); + } + printf("\n"); + returncode = 1; + } + + return returncode; +} + +static int test_shake128(void) { + unsigned char output[32]; + int i = 0; + + shake128(output, 32, plaintext, 43); + + if (memcmp(expected, output, 32)) { + printf("ERROR shake128 output did not match test vector.\n"); + printf("Expected: "); + for (i = 0; i < 32; i++) { + printf("%02X", expected[i]); + } + printf("\n"); + printf("Received: "); + for (i = 0; i < 32; i++) { + printf("%02X", output[i]); + } + printf("\n"); + return 1; + } + + return 0; +} + +int main(void) { + int result = 0; + result += test_shake128(); + result += test_shake128_incremental(); + + if (result != 0) { + puts("Errors occurred"); + } + return result; +}