Skip to content

Commit

Permalink
made keccak_fips final class; some code formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
falko-strenzke committed Jun 7, 2023
1 parent 84f8d18 commit a97a58d
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 49 deletions.
19 changes: 7 additions & 12 deletions src/lib/hash/keccak/keccak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ namespace Botan {

std::unique_ptr<HashFunction> Keccak_1600::copy_state() const { return std::make_unique<Keccak_1600>(*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) {
Expand All @@ -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<HashFunction> Keccak_1600::new_object() const { return std::make_unique<Keccak_1600>(m_keccak.output_bits()); }

void Keccak_1600::clear() {
m_keccak.clear();
std::unique_ptr<HashFunction> Keccak_1600::new_object() const {
return std::make_unique<Keccak_1600>(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
7 changes: 3 additions & 4 deletions src/lib/hash/keccak/keccak.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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<uint64_t> m_S;
size_t m_S_pos;*/
Keccak_FIPS m_keccak;
};

Expand Down
2 changes: 1 addition & 1 deletion src/lib/hash/sha3/keccak_fips.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 9 additions & 18 deletions src/lib/hash/sha3/sha3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,33 @@
#include <botan/exceptn.h>
#include <botan/internal/cpuid.h>
#include <botan/internal/fmt.h>
#include <botan/internal/loadstor.h>
#include <botan/internal/keccak_fips.h>
#include <botan/internal/loadstor.h>

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) {
throw Invalid_Argument(fmt("SHA_3: Invalid output length {}", output_bits));
}
}

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<HashFunction> SHA_3::copy_state() const { return std::make_unique<SHA_3>(*this); }

std::unique_ptr<HashFunction> SHA_3::new_object() const { return std::make_unique<SHA_3>(m_keccak.output_length() * 8); }

void SHA_3::clear() {
m_keccak.clear();
std::unique_ptr<HashFunction> SHA_3::new_object() const {
return std::make_unique<SHA_3>(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
10 changes: 1 addition & 9 deletions src/lib/hash/sha3/sha3.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@

#include <botan/hash.h>
#include <botan/secmem.h>
#include <string>
#include <botan/internal/keccak_fips.h>
#include <string>

namespace Botan {

Expand All @@ -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
Expand All @@ -63,7 +59,6 @@ class SHA_3 : public HashFunction {
static size_t absorb(
size_t bitrate, secure_vector<uint64_t>& S, size_t S_pos, const uint8_t input[], size_t length);


/**
* Expand from provided state
* @param bitrate sponge parameter
Expand All @@ -73,17 +68,14 @@ class SHA_3 : public HashFunction {
*/
static void expand(size_t bitrate, secure_vector<uint64_t>& 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;
};

Expand Down
7 changes: 2 additions & 5 deletions src/lib/mac/kmac/kmac.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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) {
Expand Down

0 comments on commit a97a58d

Please sign in to comment.