From a97a58da8da6cea569fd04432b6e09291b26e3ab Mon Sep 17 00:00:00 2001 From: Falko Strenzke Date: Wed, 7 Jun 2023 13:48:27 +0200 Subject: [PATCH] made keccak_fips final class; some code formatting --- src/lib/hash/keccak/keccak.cpp | 19 +++++++------------ src/lib/hash/keccak/keccak.h | 7 +++---- src/lib/hash/sha3/keccak_fips.h | 2 +- src/lib/hash/sha3/sha3.cpp | 27 +++++++++------------------ src/lib/hash/sha3/sha3.h | 10 +--------- src/lib/mac/kmac/kmac.cpp | 7 ++----- 6 files changed, 23 insertions(+), 49 deletions(-) diff --git a/src/lib/hash/keccak/keccak.cpp b/src/lib/hash/keccak/keccak.cpp index 74f09a666c1..50747a41ad1 100644 --- a/src/lib/hash/keccak/keccak.cpp +++ b/src/lib/hash/keccak/keccak.cpp @@ -16,8 +16,7 @@ namespace Botan { std::unique_ptr Keccak_1600::copy_state() const { return std::make_unique(*this); } -Keccak_1600::Keccak_1600(size_t output_bits) : - m_keccak(output_bits, 2 * output_bits, 0, 0) { +Keccak_1600::Keccak_1600(size_t output_bits) : m_keccak(output_bits, 2 * output_bits, 0, 0) { // We only support the parameters for the SHA-3 proposal if(output_bits != 224 && output_bits != 256 && output_bits != 384 && output_bits != 512) { @@ -27,18 +26,14 @@ Keccak_1600::Keccak_1600(size_t output_bits) : std::string Keccak_1600::name() const { return fmt("Keccak-1600({})", m_keccak.output_bits()); } -std::unique_ptr Keccak_1600::new_object() const { return std::make_unique(m_keccak.output_bits()); } - -void Keccak_1600::clear() { - m_keccak.clear(); +std::unique_ptr Keccak_1600::new_object() const { + return std::make_unique(m_keccak.output_bits()); } -void Keccak_1600::add_data(const uint8_t input[], size_t length) { - m_keccak.absorb(std::span(input, length)); -} +void Keccak_1600::clear() { m_keccak.clear(); } -void Keccak_1600::final_result(uint8_t output[]) { - m_keccak.finish(std::span(output, m_keccak.output_length())); -} +void Keccak_1600::add_data(const uint8_t input[], size_t length) { m_keccak.absorb(std::span(input, length)); } + +void Keccak_1600::final_result(uint8_t output[]) { m_keccak.finish(std::span(output, m_keccak.output_length())); } } // namespace Botan diff --git a/src/lib/hash/keccak/keccak.h b/src/lib/hash/keccak/keccak.h index 96f3069fb43..df86ed6478f 100644 --- a/src/lib/hash/keccak/keccak.h +++ b/src/lib/hash/keccak/keccak.h @@ -18,7 +18,9 @@ namespace Botan { /** * Keccak[1600], the SHA-3 submission without any final bit padding. Not an official NIST SHA-3-derived hash function. * -* In the terminology of the official SHA-3 specification [1], the instantiations of this hash function (with the output bit size in brackets) are given as +* In the terminology of the official SHA-3 specification [1], +* the instantiations of this hash function +* (with the output bit size in brackets) are given as * * Keccak1600[224](M) = KECCAK[448] (M, 224) * Keccak1600[256](M) = KECCAK[512] (M, 256) @@ -51,9 +53,6 @@ class Keccak_1600 final : public HashFunction { void add_data(const uint8_t input[], size_t length) override; void final_result(uint8_t out[]) override; - /*size_t m_output_bits, m_bitrate; - secure_vector m_S; - size_t m_S_pos;*/ Keccak_FIPS m_keccak; }; diff --git a/src/lib/hash/sha3/keccak_fips.h b/src/lib/hash/sha3/keccak_fips.h index bc32c4063d7..f0586c48804 100644 --- a/src/lib/hash/sha3/keccak_fips.h +++ b/src/lib/hash/sha3/keccak_fips.h @@ -35,7 +35,7 @@ namespace Botan { * [2] https://csrc.nist.gov/projects/hash-functions/sha-3-project */ -class Keccak_FIPS { +class Keccak_FIPS final { public: /** * @param output_bits the size of the hash output; must be one of diff --git a/src/lib/hash/sha3/sha3.cpp b/src/lib/hash/sha3/sha3.cpp index e8d810f3fd2..02d8c0efe75 100644 --- a/src/lib/hash/sha3/sha3.cpp +++ b/src/lib/hash/sha3/sha3.cpp @@ -10,14 +10,12 @@ #include #include #include -#include #include +#include namespace Botan { - -SHA_3::SHA_3(size_t output_bits) : m_keccak(output_bits, 2*output_bits, 2, 2) -{ +SHA_3::SHA_3(size_t output_bits) : m_keccak(output_bits, 2 * output_bits, 2, 2) { // We only support the parameters for SHA-3 in this constructor if(output_bits != 224 && output_bits != 256 && output_bits != 384 && output_bits != 512) { @@ -25,27 +23,20 @@ SHA_3::SHA_3(size_t output_bits) : m_keccak(output_bits, 2*output_bits, 2, 2) } } -std::string SHA_3::name() const { return fmt("SHA-3({})", m_keccak.output_length()*8 ); } +std::string SHA_3::name() const { return fmt("SHA-3({})", m_keccak.output_length() * 8); } -std::string SHA_3::provider() const { - return m_keccak.provider(); -} +std::string SHA_3::provider() const { return m_keccak.provider(); } std::unique_ptr SHA_3::copy_state() const { return std::make_unique(*this); } -std::unique_ptr SHA_3::new_object() const { return std::make_unique(m_keccak.output_length() * 8); } - -void SHA_3::clear() { - m_keccak.clear(); +std::unique_ptr SHA_3::new_object() const { + return std::make_unique(m_keccak.output_length() * 8); } -void SHA_3::add_data(const uint8_t input[], size_t length) { +void SHA_3::clear() { m_keccak.clear(); } - m_keccak.absorb(std::span(input, length)); -} +void SHA_3::add_data(const uint8_t input[], size_t length) { m_keccak.absorb(std::span(input, length)); } -void SHA_3::final_result(uint8_t output[]) { - m_keccak.finish(std::span(output, m_keccak.output_length())); -} +void SHA_3::final_result(uint8_t output[]) { m_keccak.finish(std::span(output, m_keccak.output_length())); } } // namespace Botan diff --git a/src/lib/hash/sha3/sha3.h b/src/lib/hash/sha3/sha3.h index 3eba65b136b..fc69ad0cac7 100644 --- a/src/lib/hash/sha3/sha3.h +++ b/src/lib/hash/sha3/sha3.h @@ -10,8 +10,8 @@ #include #include -#include #include +#include namespace Botan { @@ -36,10 +36,6 @@ class SHA_3 : public HashFunction { void clear() override; std::string provider() const override; - - - - private: /** * Add final padding and permute. The padding is assumed to be @@ -63,7 +59,6 @@ class SHA_3 : public HashFunction { static size_t absorb( size_t bitrate, secure_vector& S, size_t S_pos, const uint8_t input[], size_t length); - /** * Expand from provided state * @param bitrate sponge parameter @@ -73,17 +68,14 @@ class SHA_3 : public HashFunction { */ static void expand(size_t bitrate, secure_vector& S, uint8_t output[], size_t output_length); - /** * The bare Keccak-1600 permutation */ static void permute(uint64_t A[25]); - void add_data(const uint8_t input[], size_t length) override; void final_result(uint8_t out[]) override; - Keccak_FIPS m_keccak; }; diff --git a/src/lib/mac/kmac/kmac.cpp b/src/lib/mac/kmac/kmac.cpp index 48f1e7b2698..b40ad3f3eab 100644 --- a/src/lib/mac/kmac/kmac.cpp +++ b/src/lib/mac/kmac/kmac.cpp @@ -151,9 +151,7 @@ void KMAC256::start_msg(const uint8_t nonce[], size_t nonce_len) { } KMAC256::KMAC256(uint32_t output_bit_length) : - m_output_bit_length(output_bit_length), - m_hash(output_bit_length, 512, 00, 2), - m_pad_byte_length(136) { + m_output_bit_length(output_bit_length), m_hash(output_bit_length, 512, 00, 2), m_pad_byte_length(136) { // ensure valid output length byte_len_from_bit_len(m_output_bit_length); } @@ -169,8 +167,7 @@ void KMAC256::final_result(unsigned char* output) { right_encode(m_output_bit_length, tail); m_hash.absorb(std::span(tail)); - m_hash.finish(std::span(output, m_output_bit_length/8)); - + m_hash.finish(std::span(output, m_output_bit_length / 8)); } void KMAC256::key_schedule(const uint8_t key[], size_t key_length) {