diff --git a/benchmarks/sha3.cc b/benchmarks/sha3.cc index 63af8ef2..e69de29b 100644 --- a/benchmarks/sha3.cc +++ b/benchmarks/sha3.cc @@ -1,509 +0,0 @@ -#include "util.h" - -#include "Hacl_Hash_SHA3.h" -#include "Hacl_SHA3_Scalar.h" - -#ifdef HACL_CAN_COMPILE_VEC256 -#include "Hacl_SHA3_Vec256.h" -#endif - -const bytes input(1000, 0x37); - -static bytes digest224_0(28, 0); -static bytes digest224_1(28, 0); -static bytes digest224_2(28, 0); -static bytes digest224_3(28, 0); -static bytes digest256_0(32, 0); -static bytes digest256_1(32, 0); -static bytes digest256_2(32, 0); -static bytes digest256_3(32, 0); -static bytes digest384_0(48, 0); -static bytes digest384_1(48, 0); -static bytes digest384_2(48, 0); -static bytes digest384_3(48, 0); -static bytes digest512_0(64, 0); -static bytes digest512_1(64, 0); -static bytes digest512_2(64, 0); -static bytes digest512_3(64, 0); -static bytes digest_shake_0(42, 0); -static bytes digest_shake_1(42, 0); -static bytes digest_shake_2(42, 0); -static bytes digest_shake_3(42, 0); - -const size_t chunk_len = 135; - -const bytes expected_digest_sha3_224 = - from_hex("286c0137d80ed1fa81c06214ae451a665d554291aca2e5a6a48cf580"); -const bytes expected_digest_sha3_256 = - from_hex("7ac89d2c51ccf643bcaff3d747d79c0add61cbd46fb9439514e496154deae374"); -const bytes expected_digest_sha3_384 = - from_hex("7381e68f768394a730cdcbc9945aa8d8c701357605d48349545771f81ea94244c49" - "61c8a4ff6b5bfc4b98cafb31e645c"); -const bytes expected_digest_sha3_512 = - from_hex("826308628fabfe511ccd2db232f374737785144703735ad07ebf8e31c2247608a0f" - "ac23c4decd9a5264411c58c0b1591d084c0004b1ec86829a12dff96354ab5"); - -static void -Hacl_Sha3_224(benchmark::State& state) -{ - for (auto _ : state) { - Hacl_Hash_SHA3_sha3_224(digest224_0.data(), (uint8_t*)input.data(), input.size()); - } - if (digest224_0 != expected_digest_sha3_224) { - state.SkipWithError("Incorrect digest."); - return; - } -} - -BENCHMARK(Hacl_Sha3_224)->Setup(DoSetup); - -static void -Hacl_Scalar_Sha3_224(benchmark::State& state) -{ - for (auto _ : state) { - Hacl_SHA3_Scalar_sha3_224(input.size(), (uint8_t*)input.data(), digest224_0.data()); - } - if (digest224_0 != expected_digest_sha3_224) { - state.SkipWithError("Incorrect digest."); - return; - } -} - -BENCHMARK(Hacl_Scalar_Sha3_224)->Setup(DoSetup); - -#ifdef HACL_CAN_COMPILE_VEC256 -static void -Hacl_Vec256_Sha3_224(benchmark::State& state) -{ - for (auto _ : state) { - Hacl_SHA3_Vec256_sha3_224_vec256(input.size(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - digest224_0.data(), - digest224_1.data(), - digest224_2.data(), - digest224_3.data()); - } - if (digest224_0 != expected_digest_sha3_224 || - digest224_1 != expected_digest_sha3_224 || - digest224_2 != expected_digest_sha3_224 || - digest224_3 != expected_digest_sha3_224) { - state.SkipWithError("Incorrect digest."); - return; - } -} - -BENCHMARK(Hacl_Vec256_Sha3_224)->Setup(DoSetup); -#endif - -#ifndef NO_OPENSSL -BENCHMARK_CAPTURE(OpenSSL_hash_oneshot, - sha3_224, - EVP_sha3_224(), - input, - digest224_0.size(), - expected_digest_sha3_224) - ->Setup(DoSetup); -#endif - -static void -Hacl_Sha3_256(benchmark::State& state) -{ - for (auto _ : state) { - Hacl_Hash_SHA3_sha3_256(digest256_0.data(), (uint8_t*)input.data(), input.size()); - } - if (digest256_0 != expected_digest_sha3_256) { - state.SkipWithError("Incorrect digest."); - return; - } -} - -BENCHMARK(Hacl_Sha3_256)->Setup(DoSetup); - -static void -Hacl_Scalar_Sha3_256(benchmark::State& state) -{ - for (auto _ : state) { - Hacl_SHA3_Scalar_sha3_256(input.size(), (uint8_t*)input.data(), digest256_0.data()); - } - if (digest256_0 != expected_digest_sha3_256) { - state.SkipWithError("Incorrect digest."); - return; - } -} - -BENCHMARK(Hacl_Scalar_Sha3_256)->Setup(DoSetup); - -#ifdef HACL_CAN_COMPILE_VEC256 -static void -Hacl_Vec256_Sha3_256(benchmark::State& state) -{ - for (auto _ : state) { - Hacl_SHA3_Vec256_sha3_256_vec256(input.size(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - digest256_0.data(), - digest256_1.data(), - digest256_2.data(), - digest256_3.data()); - } - if (digest256_0 != expected_digest_sha3_256 || - digest256_1 != expected_digest_sha3_256 || - digest256_2 != expected_digest_sha3_256 || - digest256_3 != expected_digest_sha3_256) { - state.SkipWithError("Incorrect digest."); - return; - } -} - -BENCHMARK(Hacl_Vec256_Sha3_256)->Setup(DoSetup); -#endif - -#include "sha3.h" - -static void -Digestif_sha3_256(benchmark::State& state) -{ - bytes digest(32, 0); - - for (auto _ : state) { - - sha3_ctx ctx; - digestif_sha3_init(&ctx, 256); - - for (auto chunk : chunk(input, chunk_len)) { - digestif_sha3_update(&ctx, chunk.data(), chunk.size()); - } - - digestif_sha3_finalize(&ctx, digest.data(), 0x06); - } - - if (digest != expected_digest_sha3_256) { - state.SkipWithError("Incorrect digest."); - return; - } -} - -BENCHMARK(Digestif_sha3_256)->Setup(DoSetup); - -#ifndef NO_OPENSSL -BENCHMARK_CAPTURE(OpenSSL_hash_oneshot, - sha3_256, - EVP_sha3_256(), - input, - digest256_0.size(), - expected_digest_sha3_256) - ->Setup(DoSetup); -#endif - -static void -Hacl_Sha3_384(benchmark::State& state) -{ - for (auto _ : state) { - Hacl_Hash_SHA3_sha3_384(digest384_0.data(), (uint8_t*)input.data(), input.size()); - } - if (digest384_0 != expected_digest_sha3_384) { - state.SkipWithError("Incorrect digest."); - return; - } -} - -BENCHMARK(Hacl_Sha3_384)->Setup(DoSetup); - -static void -Hacl_Scalar_Sha3_384(benchmark::State& state) -{ - for (auto _ : state) { - Hacl_SHA3_Scalar_sha3_384(input.size(), (uint8_t*)input.data(), digest384_0.data()); - } - if (digest384_0 != expected_digest_sha3_384) { - state.SkipWithError("Incorrect digest."); - return; - } -} - -BENCHMARK(Hacl_Scalar_Sha3_384)->Setup(DoSetup); - -#ifdef HACL_CAN_COMPILE_VEC384 -static void -Hacl_Vec384_Sha3_384(benchmark::State& state) -{ - for (auto _ : state) { - Hacl_SHA3_Vec256_sha3_384_vec256(input.size(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - digest384_0.data(), - digest384_1.data(), - digest384_2.data(), - digest384_3.data()); - } - if (digest384_0 != expected_digest_sha3_384 || - digest384_1 != expected_digest_sha3_384 || - digest384_2 != expected_digest_sha3_384 || - digest384_3 != expected_digest_sha3_384) { - state.SkipWithError("Incorrect digest."); - return; - } -} - -BENCHMARK(Hacl_Vec384_Sha3_384)->Setup(DoSetup); -#endif - -#ifndef NO_OPENSSL -BENCHMARK_CAPTURE(OpenSSL_hash_oneshot, - sha3_384, - EVP_sha3_384(), - input, - digest384_0.size(), - expected_digest_sha3_384) - ->Setup(DoSetup); -#endif - -static void -Hacl_Sha3_512(benchmark::State& state) -{ - for (auto _ : state) { - Hacl_Hash_SHA3_sha3_512(digest512_0.data(), (uint8_t*)input.data(), input.size()); - } - if (digest512_0 != expected_digest_sha3_512) { - state.SkipWithError("Incorrect digest."); - return; - } -} - -BENCHMARK(Hacl_Sha3_512)->Setup(DoSetup); - -static void -Hacl_Scalar_Sha3_512(benchmark::State& state) -{ - for (auto _ : state) { - Hacl_SHA3_Scalar_sha3_512(input.size(), (uint8_t*)input.data(), digest512_0.data()); - } - if (digest512_0 != expected_digest_sha3_512) { - state.SkipWithError("Incorrect digest."); - return; - } -} - -BENCHMARK(Hacl_Scalar_Sha3_512)->Setup(DoSetup); - -#ifdef HACL_CAN_COMPILE_VEC256 -static void -Hacl_Vec512_Sha3_512(benchmark::State& state) -{ - for (auto _ : state) { - Hacl_SHA3_Vec256_sha3_512_vec256(input.size(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - digest512_0.data(), - digest512_1.data(), - digest512_2.data(), - digest512_3.data()); - } - if (digest512_0 != expected_digest_sha3_512 || - digest512_1 != expected_digest_sha3_512 || - digest512_2 != expected_digest_sha3_512 || - digest512_3 != expected_digest_sha3_512) { - state.SkipWithError("Incorrect digest."); - return; - } -} - -BENCHMARK(Hacl_Vec512_Sha3_512)->Setup(DoSetup); -#endif - -static void -Digestif_sha3_512(benchmark::State& state) -{ - bytes digest(64, 0); - - for (auto _ : state) { - - sha3_ctx ctx; - digestif_sha3_init(&ctx, 512); - - for (auto chunk : chunk(input, chunk_len)) { - digestif_sha3_update(&ctx, chunk.data(), chunk.size()); - } - - digestif_sha3_finalize(&ctx, digest.data(), 0x06); - } - - if (digest != expected_digest_sha3_512) { - state.SkipWithError("Incorrect digest."); - return; - } -} - -BENCHMARK(Digestif_sha3_512)->Setup(DoSetup); - -#ifndef NO_OPENSSL -BENCHMARK_CAPTURE(OpenSSL_hash_oneshot, - sha3_512, - EVP_sha3_512(), - input, - digest512_0.size(), - expected_digest_sha3_512) - ->Setup(DoSetup); -#endif - -static void -Hacl_Sha3_256_Streaming(benchmark::State& state) -{ - for (auto _ : state) { - // Init - Hacl_Hash_SHA3_state_t* sha_state = - Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_SHA3_256); - - // Update - for (size_t i = 0; i < input.size();) { - Hacl_Hash_SHA3_update(sha_state, - (uint8_t*)input.data() + i, - min(chunk_len, input.size() - i)); - i += chunk_len; - } - - // Finish - Hacl_Hash_SHA3_digest(sha_state, digest256_0.data()); - Hacl_Hash_SHA3_free(sha_state); - } - - if (digest256_0 != expected_digest_sha3_256) { - state.SkipWithError("Incorrect digest."); - return; - } -} - -BENCHMARK(Hacl_Sha3_256_Streaming)->Setup(DoSetup); - -#ifndef NO_OPENSSL -BENCHMARK_CAPTURE(OpenSSL_hash_streaming, - sha3_224, - EVP_sha3_224(), - input, - chunk_len, - digest224_0.size(), - expected_digest_sha3_224) - ->Setup(DoSetup); - -BENCHMARK_CAPTURE(OpenSSL_hash_streaming, - sha3_256, - EVP_sha3_256(), - input, - chunk_len, - digest256_0.size(), - expected_digest_sha3_256) - ->Setup(DoSetup); - -BENCHMARK_CAPTURE(OpenSSL_hash_streaming, - sha3_384, - EVP_sha3_384(), - input, - chunk_len, - digest384_0.size(), - expected_digest_sha3_384) - ->Setup(DoSetup); - -BENCHMARK_CAPTURE(OpenSSL_hash_streaming, - sha3_512, - EVP_sha3_512(), - input, - chunk_len, - digest512_0.size(), - expected_digest_sha3_512) - ->Setup(DoSetup); -#endif - -static void -Hacl_Sha3_shake128(benchmark::State& state) -{ - for (auto _ : state) { - Hacl_Hash_SHA3_shake128_hacl( - input.size(), (uint8_t*)input.data(), digest_shake_0.size(), digest_shake_0.data()); - } -} - -BENCHMARK(Hacl_Sha3_shake128)->Setup(DoSetup); - -static void -Hacl_Scalar_Sha3_shake128(benchmark::State& state) -{ - for (auto _ : state) { - Hacl_SHA3_Scalar_shake128_hacl(input.size(), (uint8_t*)input.data(), digest_shake_0.size(), digest_shake_0.data()); - } -} - -BENCHMARK(Hacl_Scalar_Sha3_shake128)->Setup(DoSetup); - -#ifdef HACL_CAN_COMPILE_VEC256 -static void -Hacl_Vec256_Sha3_shake128(benchmark::State& state) -{ - for (auto _ : state) { - Hacl_SHA3_Vec256_shake128_vec256(input.size(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - digest_shake_0.size(), - digest_shake_0.data(), - digest_shake_1.data(), - digest_shake_2.data(), - digest_shake_3.data()); - } -} - -BENCHMARK(Hacl_Vec256_Sha3_shake128)->Setup(DoSetup); -#endif - -static void -Hacl_Sha3_shake256(benchmark::State& state) -{ - for (auto _ : state) { - Hacl_Hash_SHA3_shake256_hacl( - input.size(), (uint8_t*)input.data(), digest_shake_0.size(), digest_shake_0.data()); - } -} - -BENCHMARK(Hacl_Sha3_shake256)->Setup(DoSetup); - -static void -Hacl_Scalar_Sha3_shake256(benchmark::State& state) -{ - for (auto _ : state) { - Hacl_SHA3_Scalar_shake256_hacl(input.size(), (uint8_t*)input.data(), digest_shake_0.size(), digest_shake_0.data()); - } -} - -BENCHMARK(Hacl_Scalar_Sha3_shake256)->Setup(DoSetup); - -#ifdef HACL_CAN_COMPILE_VEC256 -static void -Hacl_Vec256_Sha3_shake256(benchmark::State& state) -{ - for (auto _ : state) { - Hacl_SHA3_Vec256_shake256_vec256(input.size(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - digest_shake_0.size(), - digest_shake_0.data(), - digest_shake_1.data(), - digest_shake_2.data(), - digest_shake_3.data()); - } -} - -BENCHMARK(Hacl_Vec256_Sha3_shake256)->Setup(DoSetup); -#endif - -BENCHMARK_MAIN(); diff --git a/tests/sha3.cc b/tests/sha3.cc index 71304ab1..e69de29b 100644 --- a/tests/sha3.cc +++ b/tests/sha3.cc @@ -1,569 +0,0 @@ -/* - * Copyright 2022 Cryspen Sarl - * - * Licensed under the Apache License, Version 2.0 or MIT. - * - http://www.apache.org/licenses/LICENSE-2.0 - * - http://opensource.org/licenses/MIT - */ - -#include -#include -#include -#include - -#include "Hacl_Hash_SHA3.h" -#include "Hacl_SHA3_Scalar.h" -#include "hacl-cpu-features.h" - -#ifdef HACL_CAN_COMPILE_VEC256 -#include "Hacl_SHA3_Vec256.h" -#endif - -#include "config.h" -#include "util.h" - -using json = nlohmann::json; - -// ANCHOR(example define) -// Note: HACL Packages will provide this (or a similar) define in a later -// version. -#define HACL_HASH_SHA3_256_DIGEST_LENGTH 32 -// ANCHOR_END(example define) - -typedef struct -{ - bytes msg; - bytes md; -} TestCase; - -std::vector -read_json(char* test_file) -{ - // Read JSON test vector - std::ifstream json_test_file(test_file); - nlohmann::json test_vectors; - json_test_file >> test_vectors; - - std::vector tests_out; - - // Read tests - for (auto& test : test_vectors.items()) { - auto test_value = test.value(); - auto msg = from_hex(test_value["msg"]); - auto md = from_hex(test_value["md"]); - tests_out.push_back({ msg, md }); - } - - return tests_out; -} - -TEST(ApiSuite, ApiTest) -{ - // Documentation. - // Lines after START and before END are used in documentation. - { - // START OneShot - // This example uses SHA3-256. - // - - const char* message = "Hello, World!"; - uint32_t message_size = strlen(message); - - uint8_t digest[HACL_HASH_SHA3_256_DIGEST_LENGTH]; - - Hacl_Hash_SHA3_sha3_256(digest, (uint8_t*)message, message_size); - // END OneShot - - bytes expected_digest = from_hex( - "1af17a664e3fa8e419b8ba05c2a173169df76162a5a286e0c405b460d478f7ef"); - - EXPECT_EQ(strncmp((char*)digest, - (char*)expected_digest.data(), - HACL_HASH_SHA3_256_DIGEST_LENGTH), - 0); - } - - // Documentation. - // Lines after START and before END are used in documentation. - { - // ANCHOR(streaming) - // This example shows how to hash the byte sequence "Hello, World!" in two - // chunks. As a bonus, it also shows how to obtain intermediate results by - // calling `digest` more than once. - - const char* chunk_1 = "Hello, "; - const char* chunk_2 = "World!"; - uint32_t chunk_1_size = strlen(chunk_1); - uint32_t chunk_2_size = strlen(chunk_2); - - uint8_t digest_1[HACL_HASH_SHA3_256_DIGEST_LENGTH]; - uint8_t digest_2[HACL_HASH_SHA3_256_DIGEST_LENGTH]; - - // Init - Hacl_Hash_SHA3_state_t* state = - Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_SHA3_256); - - // 1/2 Include `Hello, ` into the hash calculation and - // obtain the intermediate hash of "Hello, ". - uint32_t update_res = - Hacl_Hash_SHA3_update(state, (uint8_t*)chunk_1, chunk_1_size); - ASSERT_EQ(0, update_res); - // This is optional when no intermediate results are required. - auto finish_res = Hacl_Hash_SHA3_digest(state, digest_1); - ASSERT_EQ(Hacl_Streaming_Types_Success, finish_res); - - // 2/2 Include `World!` into the hash calculation and - // obtain the final hash of "Hello, World!". - uint32_t update_res_2 = - Hacl_Hash_SHA3_update(state, (uint8_t*)chunk_2, chunk_2_size); - ASSERT_EQ(0, update_res_2); - auto finish_res_2 = Hacl_Hash_SHA3_digest(state, digest_2); - ASSERT_EQ(Hacl_Streaming_Types_Success, finish_res_2); - - // Cleanup - Hacl_Hash_SHA3_free(state); - - print_hex_ln(HACL_HASH_SHA3_256_DIGEST_LENGTH, digest_1); - print_hex_ln(HACL_HASH_SHA3_256_DIGEST_LENGTH, digest_2); - // ANCHOR_END(streaming) - - bytes expected_digest_1 = from_hex( - "c942846170cfdf995f56688c396ad6b82cb09ed3aa37801a6ad1d23274cfb6ae"); - bytes expected_digest_2 = from_hex( - "1af17a664e3fa8e419b8ba05c2a173169df76162a5a286e0c405b460d478f7ef"); - - EXPECT_EQ(strncmp((char*)digest_1, - (char*)expected_digest_1.data(), - HACL_HASH_SHA3_256_DIGEST_LENGTH), - 0); - EXPECT_EQ(strncmp((char*)digest_2, - (char*)expected_digest_2.data(), - HACL_HASH_SHA3_256_DIGEST_LENGTH), - 0); - } - - // Documentation. - // Lines after START and before END are used in documentation. - { - // ANCHOR(example shake128) - // This example uses SHAKE-128. - - const char* message = "Hello, World!"; - uint32_t message_size = strlen(message); - - // SHAKE will generate as many bytes as requested. - uint32_t digest_size = 42; - uint8_t digest[42]; - - Hacl_Hash_SHA3_shake128_hacl( - message_size, (uint8_t*)message, digest_size, digest); - // ANCHOR_END(example shake128) - - bytes expected_digest = - from_hex("2bf5e6dee6079fad604f573194ba8426bd4d30eb13e8ba2edae70e529b570cb" - "dd588f2c5dd4e465dfbaf"); - - EXPECT_EQ( - strncmp((char*)digest, (char*)expected_digest.data(), digest_size), 0); - } - - // Documentation. - // Lines after START and before END are used in documentation. - { - // ANCHOR(example scalar_sha3_256) - // This example uses Scalar SHA3-256. - - const char* message = "Hello, World!"; - uint32_t message_size = strlen(message); - - uint8_t digest[HACL_HASH_SHA3_256_DIGEST_LENGTH]; - - Hacl_SHA3_Scalar_sha3_256(message_size, (uint8_t*)message, digest); - // ANCHOR_END(example scalar_sha3_256) - - bytes expected_digest = from_hex( - "1af17a664e3fa8e419b8ba05c2a173169df76162a5a286e0c405b460d478f7ef"); - - EXPECT_EQ(strncmp((char*)digest, - (char*)expected_digest.data(), - HACL_HASH_SHA3_256_DIGEST_LENGTH), - 0); - } - - // Documentation. - // Lines after START and before END are used in documentation. - { - // ANCHOR(example scalar_shake128) - // This example uses Scalar SHAKE-128. - - const char* message = "Hello, World!"; - uint32_t message_size = strlen(message); - - // SHAKE will generate as many bytes as requested. - uint32_t digest_size = 42; - uint8_t digest[42]; - - Hacl_SHA3_Scalar_shake128_hacl( - message_size, (uint8_t*)message, digest_size, digest); - // ANCHOR_END(example scalar_shake128) - - bytes expected_digest = - from_hex("2bf5e6dee6079fad604f573194ba8426bd4d30eb13e8ba2edae70e529b570cb" - "dd588f2c5dd4e465dfbaf"); - - EXPECT_EQ( - strncmp((char*)digest, (char*)expected_digest.data(), digest_size), 0); - } - -#ifdef HACL_CAN_COMPILE_VEC256 - hacl_init_cpu_features(); - if (hacl_vec256_support()) - { - // Documentation. - // Lines after START and before END are used in documentation. - { - // ANCHOR(example vec256_sha3_256) - // This example uses Vec256 SHA3-256. - - const char* message = "Hello, World!"; - uint32_t message_size = strlen(message); - - uint8_t digest0[HACL_HASH_SHA3_256_DIGEST_LENGTH]; - uint8_t digest1[HACL_HASH_SHA3_256_DIGEST_LENGTH]; - uint8_t digest2[HACL_HASH_SHA3_256_DIGEST_LENGTH]; - uint8_t digest3[HACL_HASH_SHA3_256_DIGEST_LENGTH]; - - Hacl_SHA3_Vec256_sha3_256_vec256(message_size, - (uint8_t*)message, (uint8_t*)message, (uint8_t*)message, (uint8_t*)message, - digest0, digest1, digest2, digest3); - // ANCHOR_END(example vec256_sha3_256) - - bytes expected_digest = from_hex( - "1af17a664e3fa8e419b8ba05c2a173169df76162a5a286e0c405b460d478f7ef"); - - EXPECT_EQ(strncmp((char*)digest0, - (char*)expected_digest.data(), - HACL_HASH_SHA3_256_DIGEST_LENGTH), - 0); - EXPECT_EQ(strncmp((char*)digest1, - (char*)expected_digest.data(), - HACL_HASH_SHA3_256_DIGEST_LENGTH), - 0); - EXPECT_EQ(strncmp((char*)digest2, - (char*)expected_digest.data(), - HACL_HASH_SHA3_256_DIGEST_LENGTH), - 0); - EXPECT_EQ(strncmp((char*)digest3, - (char*)expected_digest.data(), - HACL_HASH_SHA3_256_DIGEST_LENGTH), - 0); - } - - // Documentation. - // Lines after START and before END are used in documentation. - { - // ANCHOR(example vec256_shake128) - // This example uses Vec256 SHAKE-128. - - const char* message0 = "Hello, World1!"; - const char* message1 = "Hello, World2!"; - const char* message2 = "Hello, World3!"; - const char* message3 = "Hello, World4!"; - uint32_t message_size = 14; - - // SHAKE will generate as many bytes as requested. - uint32_t digest_size = 42; - uint8_t digest0[42]; - uint8_t digest1[42]; - uint8_t digest2[42]; - uint8_t digest3[42]; - - Hacl_SHA3_Vec256_shake128_vec256(message_size, - (uint8_t*)message0, (uint8_t*)message1, (uint8_t*)message2, (uint8_t*)message3, - digest_size, digest0, digest1, digest2, digest3); - // ANCHOR_END(example vec256_shake128) - - bytes expected_digest0 = - from_hex("1b82c3db6cb958a09a7ea3dd82b67a9c994422c39616ec373afafcf2fca8bca" - "808881328f9ca03eb119a"); - bytes expected_digest1 = - from_hex("3c8f0ab13109dff341fbe0e7511bd8bdfa8d13335b36acdb391170017c6d45f" - "460964cab081699f6e45d"); - bytes expected_digest2 = - from_hex("86ee9003051369f1d5461b00263e01cac1c65defaf722e6ed648fba99743a14" - "9b39abc52d6fc746f5014"); - bytes expected_digest3 = - from_hex("0b9efd21050944cb5ba5df0cc35a176100201e3fd7c4f2b9f70a9dfd4a7228b" - "5d676451df013d3e22ac9"); - - EXPECT_EQ( - strncmp((char*)digest0, (char*)expected_digest0.data(), digest_size), 0); - EXPECT_EQ( - strncmp((char*)digest1, (char*)expected_digest1.data(), digest_size), 0); - EXPECT_EQ( - strncmp((char*)digest2, (char*)expected_digest2.data(), digest_size), 0); - EXPECT_EQ( - strncmp((char*)digest3, (char*)expected_digest3.data(), digest_size), 0); - } - } -#endif -} - -class Sha3KAT : public ::testing::TestWithParam -{}; - -TEST_P(Sha3KAT, TryKAT) -{ - auto test_case = GetParam(); - - { - bytes digest(test_case.md.size(), 0); - if (test_case.md.size() == 224 / 8) { - Hacl_Hash_SHA3_sha3_224( - digest.data(), test_case.msg.data(), test_case.msg.size()); - } else if (test_case.md.size() == 256 / 8) { - Hacl_Hash_SHA3_sha3_256( - digest.data(), test_case.msg.data(), test_case.msg.size()); - } else if (test_case.md.size() == 384 / 8) { - Hacl_Hash_SHA3_sha3_384( - digest.data(), test_case.msg.data(), test_case.msg.size()); - } else if (test_case.md.size() == 512 / 8) { - Hacl_Hash_SHA3_sha3_512( - digest.data(), test_case.msg.data(), test_case.msg.size()); - } - - EXPECT_EQ(test_case.md, digest) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest) << std::endl; - } - - { - bytes digest(test_case.md.size(), 0); - if (test_case.md.size() == 224 / 8) { - Hacl_SHA3_Scalar_sha3_224( - test_case.msg.size(), test_case.msg.data(), digest.data()); - } else if (test_case.md.size() == 256 / 8) { - Hacl_SHA3_Scalar_sha3_256( - test_case.msg.size(), test_case.msg.data(), digest.data()); - } else if (test_case.md.size() == 384 / 8) { - Hacl_SHA3_Scalar_sha3_384( - test_case.msg.size(), test_case.msg.data(), digest.data()); - } else if (test_case.md.size() == 512 / 8) { - Hacl_SHA3_Scalar_sha3_512( - test_case.msg.size(), test_case.msg.data(), digest.data()); - } - - EXPECT_EQ(test_case.md, digest) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest) << std::endl; - } - -#ifdef HACL_CAN_COMPILE_VEC256 - hacl_init_cpu_features(); - if (hacl_vec256_support()) - { - bytes digest0(test_case.md.size(), 0); - bytes digest1(test_case.md.size(), 0); - bytes digest2(test_case.md.size(), 0); - bytes digest3(test_case.md.size(), 0); - if (test_case.md.size() == 224 / 8) { - Hacl_SHA3_Vec256_sha3_224_vec256( - test_case.msg.size(), - test_case.msg.data(), test_case.msg.data(), test_case.msg.data(), test_case.msg.data(), - digest0.data(), digest1.data(), digest2.data(), digest3.data()); - } else if (test_case.md.size() == 256 / 8) { - Hacl_SHA3_Vec256_sha3_256_vec256( - test_case.msg.size(), - test_case.msg.data(), test_case.msg.data(), test_case.msg.data(), test_case.msg.data(), - digest0.data(), digest1.data(), digest2.data(), digest3.data()); - } else if (test_case.md.size() == 384 / 8) { - Hacl_SHA3_Vec256_sha3_384_vec256( - test_case.msg.size(), - test_case.msg.data(), test_case.msg.data(), test_case.msg.data(), test_case.msg.data(), - digest0.data(), digest1.data(), digest2.data(), digest3.data()); - } else if (test_case.md.size() == 512 / 8) { - Hacl_SHA3_Vec256_sha3_512_vec256( - test_case.msg.size(), - test_case.msg.data(), test_case.msg.data(), test_case.msg.data(), test_case.msg.data(), - digest0.data(), digest1.data(), digest2.data(), digest3.data()); - } - - EXPECT_EQ(test_case.md, digest0) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest0) << std::endl; - EXPECT_EQ(test_case.md, digest1) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest1) << std::endl; - EXPECT_EQ(test_case.md, digest2) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest2) << std::endl; - EXPECT_EQ(test_case.md, digest3) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest3) << std::endl; - } -#endif -} - -class ShakeKAT : public ::testing::TestWithParam -{}; - -TEST_P(ShakeKAT, TryKAT) -{ - auto test_case = GetParam(); - - { - if (test_case.md.size() == 128 / 8) { - bytes digest(test_case.md.size(), 128 / 8); - - Hacl_Hash_SHA3_shake128_hacl( - test_case.msg.size(), test_case.msg.data(), digest.size(), digest.data()); - - EXPECT_EQ(test_case.md, digest) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest) << std::endl; - } else if (test_case.md.size() == 256 / 8) { - bytes digest(test_case.md.size(), 256 / 8); - - Hacl_Hash_SHA3_shake256_hacl( - test_case.msg.size(), test_case.msg.data(), digest.size(), digest.data()); - - EXPECT_EQ(test_case.md, digest) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest) << std::endl; - } - } - - { - if (test_case.md.size() == 128 / 8) { - bytes digest(test_case.md.size(), 128 / 8); - - Hacl_SHA3_Scalar_shake128_hacl(test_case.msg.size(), - test_case.msg.data(), - digest.size(), - digest.data()); - - EXPECT_EQ(test_case.md, digest) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest) << std::endl; - } else if (test_case.md.size() == 256 / 8) { - bytes digest(test_case.md.size(), 256 / 8); - - Hacl_SHA3_Scalar_shake256_hacl(test_case.msg.size(), - test_case.msg.data(), - digest.size(), - digest.data()); - - EXPECT_EQ(test_case.md, digest) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest) << std::endl; - } - } - -#ifdef HACL_CAN_COMPILE_VEC256 - hacl_init_cpu_features(); - if (hacl_vec256_support()) - { - if (test_case.md.size() == 128 / 8) { - bytes digest0(test_case.md.size(), 128 / 8); - bytes digest1(test_case.md.size(), 128 / 8); - bytes digest2(test_case.md.size(), 128 / 8); - bytes digest3(test_case.md.size(), 128 / 8); - - Hacl_SHA3_Vec256_shake128_vec256(test_case.msg.size(), - test_case.msg.data(), - test_case.msg.data(), - test_case.msg.data(), - test_case.msg.data(), - digest0.size(), - digest0.data(), - digest1.data(), - digest2.data(), - digest3.data()); - - EXPECT_EQ(test_case.md, digest0) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest0) << std::endl; - EXPECT_EQ(test_case.md, digest1) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest1) << std::endl; - EXPECT_EQ(test_case.md, digest2) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest2) << std::endl; - EXPECT_EQ(test_case.md, digest3) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest3) << std::endl; - } else if (test_case.md.size() == 256 / 8) { - bytes digest0(test_case.md.size(), 256 / 8); - bytes digest1(test_case.md.size(), 256 / 8); - bytes digest2(test_case.md.size(), 256 / 8); - bytes digest3(test_case.md.size(), 256 / 8); - - Hacl_SHA3_Vec256_shake256_vec256(test_case.msg.size(), - test_case.msg.data(), - test_case.msg.data(), - test_case.msg.data(), - test_case.msg.data(), - digest0.size(), - digest0.data(), - digest1.data(), - digest2.data(), - digest3.data()); - - EXPECT_EQ(test_case.md, digest0) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest0) << std::endl; - EXPECT_EQ(test_case.md, digest1) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest1) << std::endl; - EXPECT_EQ(test_case.md, digest2) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest2) << std::endl; - EXPECT_EQ(test_case.md, digest3) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest3) << std::endl; - } - } -#endif -} - -INSTANTIATE_TEST_SUITE_P( - Sha3_224ShortKAT, - Sha3KAT, - ::testing::ValuesIn(read_json(const_cast("sha3-224-short.json")))); - -INSTANTIATE_TEST_SUITE_P( - Sha3_224LongKAT, - Sha3KAT, - ::testing::ValuesIn(read_json(const_cast("sha3-224-long.json")))); - -INSTANTIATE_TEST_SUITE_P( - Sha3_256ShortKAT, - Sha3KAT, - ::testing::ValuesIn(read_json(const_cast("sha3-256-short.json")))); - -INSTANTIATE_TEST_SUITE_P( - Sha3_256LongKAT, - Sha3KAT, - ::testing::ValuesIn(read_json(const_cast("sha3-256-long.json")))); - -INSTANTIATE_TEST_SUITE_P( - Sha3_384ShortKAT, - Sha3KAT, - ::testing::ValuesIn(read_json(const_cast("sha3-384-short.json")))); - -INSTANTIATE_TEST_SUITE_P( - Sha3_384LongKAT, - Sha3KAT, - ::testing::ValuesIn(read_json(const_cast("sha3-384-long.json")))); - -INSTANTIATE_TEST_SUITE_P( - Sha3_512ShortKAT, - Sha3KAT, - ::testing::ValuesIn(read_json(const_cast("sha3-512-short.json")))); - -INSTANTIATE_TEST_SUITE_P( - Sha3_512LongKAT, - Sha3KAT, - ::testing::ValuesIn(read_json(const_cast("sha3-512-long.json")))); - -INSTANTIATE_TEST_SUITE_P( - Shake128ShortKAT, - ShakeKAT, - ::testing::ValuesIn(read_json(const_cast("shake128-short.json")))); - -INSTANTIATE_TEST_SUITE_P( - Shake128LongKAT, - ShakeKAT, - ::testing::ValuesIn(read_json(const_cast("shake128-long.json")))); - -INSTANTIATE_TEST_SUITE_P( - Shake256ShortKAT, - ShakeKAT, - ::testing::ValuesIn(read_json(const_cast("shake256-short.json")))); - -INSTANTIATE_TEST_SUITE_P( - Shake256LongKAT, - ShakeKAT, - ::testing::ValuesIn(read_json(const_cast("shake256-long.json"))));