diff options
Diffstat (limited to 'comm/third_party/botan/src/lib/hash/shake')
-rw-r--r-- | comm/third_party/botan/src/lib/hash/shake/info.txt | 7 | ||||
-rw-r--r-- | comm/third_party/botan/src/lib/hash/shake/shake.cpp | 97 | ||||
-rw-r--r-- | comm/third_party/botan/src/lib/hash/shake/shake.h | 85 |
3 files changed, 189 insertions, 0 deletions
diff --git a/comm/third_party/botan/src/lib/hash/shake/info.txt b/comm/third_party/botan/src/lib/hash/shake/info.txt new file mode 100644 index 0000000000..59a1a852a9 --- /dev/null +++ b/comm/third_party/botan/src/lib/hash/shake/info.txt @@ -0,0 +1,7 @@ +<defines> +SHAKE -> 20161009 +</defines> + +<requires> +sha3 +</requires> diff --git a/comm/third_party/botan/src/lib/hash/shake/shake.cpp b/comm/third_party/botan/src/lib/hash/shake/shake.cpp new file mode 100644 index 0000000000..76ed79a274 --- /dev/null +++ b/comm/third_party/botan/src/lib/hash/shake/shake.cpp @@ -0,0 +1,97 @@ +/* +* SHAKE-128/256 as a hash +* (C) 2016 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#include <botan/shake.h> +#include <botan/sha3.h> +#include <botan/exceptn.h> + +namespace Botan { + +SHAKE_128::SHAKE_128(size_t output_bits) : + m_output_bits(output_bits), m_S(25), m_S_pos(0) + { + if(output_bits % 8 != 0) + throw Invalid_Argument("SHAKE_128: Invalid output length " + + std::to_string(output_bits)); + } + +std::string SHAKE_128::name() const + { + return "SHAKE-128(" + std::to_string(m_output_bits) + ")"; + } + +HashFunction* SHAKE_128::clone() const + { + return new SHAKE_128(m_output_bits); + } + +std::unique_ptr<HashFunction> SHAKE_128::copy_state() const + { + return std::unique_ptr<HashFunction>(new SHAKE_128(*this)); + } + +void SHAKE_128::clear() + { + zeroise(m_S); + m_S_pos = 0; + } + +void SHAKE_128::add_data(const uint8_t input[], size_t length) + { + m_S_pos = SHA_3::absorb(SHAKE_128_BITRATE, m_S, m_S_pos, input, length); + } + +void SHAKE_128::final_result(uint8_t output[]) + { + SHA_3::finish(SHAKE_128_BITRATE, m_S, m_S_pos, 0x1F, 0x80); + SHA_3::expand(SHAKE_128_BITRATE, m_S, output, output_length()); + clear(); + } + +SHAKE_256::SHAKE_256(size_t output_bits) : + m_output_bits(output_bits), m_S(25), m_S_pos(0) + { + if(output_bits % 8 != 0) + throw Invalid_Argument("SHAKE_256: Invalid output length " + + std::to_string(output_bits)); + } + +std::string SHAKE_256::name() const + { + return "SHAKE-256(" + std::to_string(m_output_bits) + ")"; + } + +HashFunction* SHAKE_256::clone() const + { + return new SHAKE_256(m_output_bits); + } + +std::unique_ptr<HashFunction> SHAKE_256::copy_state() const + { + return std::unique_ptr<HashFunction>(new SHAKE_256(*this)); + } + +void SHAKE_256::clear() + { + zeroise(m_S); + m_S_pos = 0; + } + +void SHAKE_256::add_data(const uint8_t input[], size_t length) + { + m_S_pos = SHA_3::absorb(SHAKE_256_BITRATE, m_S, m_S_pos, input, length); + } + +void SHAKE_256::final_result(uint8_t output[]) + { + SHA_3::finish(SHAKE_256_BITRATE, m_S, m_S_pos, 0x1F, 0x80); + SHA_3::expand(SHAKE_256_BITRATE, m_S, output, output_length()); + + clear(); + } + +} diff --git a/comm/third_party/botan/src/lib/hash/shake/shake.h b/comm/third_party/botan/src/lib/hash/shake/shake.h new file mode 100644 index 0000000000..c52df136b7 --- /dev/null +++ b/comm/third_party/botan/src/lib/hash/shake/shake.h @@ -0,0 +1,85 @@ +/* +* SHAKE hash functions +* (C) 2010,2016 Jack Lloyd +* +* Botan is released under the Simplified BSD License (see license.txt) +*/ + +#ifndef BOTAN_SHAKE_HASH_H_ +#define BOTAN_SHAKE_HASH_H_ + +#include <botan/hash.h> +#include <botan/secmem.h> +#include <string> + +BOTAN_FUTURE_INTERNAL_HEADER(shake.h) + +namespace Botan { + +/** +* SHAKE-128 +*/ +class BOTAN_PUBLIC_API(2,0) SHAKE_128 final : public HashFunction + { + public: + + /** + * @param output_bits the desired output size in bits + * must be a multiple of 8 + */ + explicit SHAKE_128(size_t output_bits); + + size_t hash_block_size() const override { return SHAKE_128_BITRATE / 8; } + size_t output_length() const override { return m_output_bits / 8; } + + HashFunction* clone() const override; + std::unique_ptr<HashFunction> copy_state() const override; + std::string name() const override; + void clear() override; + + private: + void add_data(const uint8_t input[], size_t length) override; + void final_result(uint8_t out[]) override; + + static const size_t SHAKE_128_BITRATE = 1600 - 256; + + size_t m_output_bits; + secure_vector<uint64_t> m_S; + size_t m_S_pos; + }; + +/** +* SHAKE-256 +*/ +class BOTAN_PUBLIC_API(2,0) SHAKE_256 final : public HashFunction + { + public: + + /** + * @param output_bits the desired output size in bits + * must be a multiple of 8 + */ + explicit SHAKE_256(size_t output_bits); + + size_t hash_block_size() const override { return SHAKE_256_BITRATE / 8; } + size_t output_length() const override { return m_output_bits / 8; } + + HashFunction* clone() const override; + std::unique_ptr<HashFunction> copy_state() const override; + std::string name() const override; + void clear() override; + + private: + void add_data(const uint8_t input[], size_t length) override; + void final_result(uint8_t out[]) override; + + static const size_t SHAKE_256_BITRATE = 1600 - 512; + + size_t m_output_bits; + secure_vector<uint64_t> m_S; + size_t m_S_pos; + }; + +} + +#endif |