diff options
author | Jack Lloyd <[email protected]> | 2016-11-10 13:25:10 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2016-11-10 13:25:10 -0500 |
commit | b7ae8043e963467eb222a44f48d66a1df36d9371 (patch) | |
tree | f5ec4059e21d874404c60595568a4a3d4d7305c9 /src/lib | |
parent | e6bf87b6b0d4d9c3877b79de53de58dc34a3acca (diff) |
Add SHAKE-128 and SHAKE-256 as hash functions
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/hash/hash.cpp | 15 | ||||
-rw-r--r-- | src/lib/hash/keccak/keccak.cpp | 42 | ||||
-rw-r--r-- | src/lib/hash/sha3/sha3.cpp | 77 | ||||
-rw-r--r-- | src/lib/hash/sha3/sha3.h | 33 | ||||
-rw-r--r-- | src/lib/hash/shake/info.txt | 5 | ||||
-rw-r--r-- | src/lib/hash/shake/shake.cpp | 101 | ||||
-rw-r--r-- | src/lib/hash/shake/shake.h | 81 | ||||
-rw-r--r-- | src/lib/stream/shake_cipher/shake_cipher.cpp | 14 | ||||
-rw-r--r-- | src/lib/stream/shake_cipher/shake_cipher.h | 6 | ||||
-rw-r--r-- | src/lib/stream/stream_cipher.cpp | 2 |
10 files changed, 290 insertions, 86 deletions
diff --git a/src/lib/hash/hash.cpp b/src/lib/hash/hash.cpp index ede2f8c99..112554127 100644 --- a/src/lib/hash/hash.cpp +++ b/src/lib/hash/hash.cpp @@ -56,6 +56,10 @@ #include <botan/sha3.h> #endif +#if defined(BOTAN_HAS_SHAKE) + #include <botan/shake.h> +#endif + #if defined(BOTAN_HAS_SKEIN_512) #include <botan/skein_512.h> #endif @@ -242,6 +246,17 @@ std::unique_ptr<HashFunction> HashFunction::create(const std::string& algo_spec, } #endif +#if defined(BOTAN_HAS_SHAKE) + if(req.algo_name() == "SHAKE-128") + { + return std::unique_ptr<HashFunction>(new SHAKE_128(req.arg_as_integer(0, 128))); + } + if(req.algo_name() == "SHAKE-256") + { + return std::unique_ptr<HashFunction>(new SHAKE_256(req.arg_as_integer(0, 256))); + } +#endif + #if defined(BOTAN_HAS_WHIRLPOOL) if(req.algo_name() == "Whirlpool") { diff --git a/src/lib/hash/keccak/keccak.cpp b/src/lib/hash/keccak/keccak.cpp index 60cb20696..e0c67131b 100644 --- a/src/lib/hash/keccak/keccak.cpp +++ b/src/lib/hash/keccak/keccak.cpp @@ -44,47 +44,7 @@ void Keccak_1600::clear() void Keccak_1600::add_data(const byte input[], size_t length) { - if(length == 0) - return; - - while(length) - { - size_t to_take = std::min(length, m_bitrate / 8 - m_S_pos); - - length -= to_take; - - while(to_take && m_S_pos % 8) - { - m_S[m_S_pos / 8] ^= static_cast<u64bit>(input[0]) << (8 * (m_S_pos % 8)); - - ++m_S_pos; - ++input; - --to_take; - } - - while(to_take && to_take % 8 == 0) - { - m_S[m_S_pos / 8] ^= load_le<u64bit>(input, 0); - m_S_pos += 8; - input += 8; - to_take -= 8; - } - - while(to_take) - { - m_S[m_S_pos / 8] ^= static_cast<u64bit>(input[0]) << (8 * (m_S_pos % 8)); - - ++m_S_pos; - ++input; - --to_take; - } - - if(m_S_pos == m_bitrate / 8) - { - SHA_3::permute(m_S.data()); - m_S_pos = 0; - } - } + m_S_pos = SHA_3::absorb(m_bitrate, m_S, m_S_pos, input, length); } void Keccak_1600::final_result(byte output[]) diff --git a/src/lib/hash/sha3/sha3.cpp b/src/lib/hash/sha3/sha3.cpp index 3897f0588..2361d7d5b 100644 --- a/src/lib/hash/sha3/sha3.cpp +++ b/src/lib/hash/sha3/sha3.cpp @@ -109,16 +109,6 @@ SHA_3::SHA_3(size_t output_bits) : std::to_string(output_bits)); } -SHA_3::SHA_3(size_t output_bits, size_t capacity) : - m_output_bits(output_bits), - m_bitrate(1600 - capacity), - m_S(25), - m_S_pos(0) - { - if(capacity == 0 || capacity >= 1600) - throw Invalid_Argument("Impossible SHA-3 capacity specified"); - } - std::string SHA_3::name() const { return "SHA-3(" + std::to_string(m_output_bits) + ")"; @@ -135,49 +125,84 @@ void SHA_3::clear() m_S_pos = 0; } -void SHA_3::add_data(const byte input[], size_t length) +//static +size_t SHA_3::absorb(size_t bitrate, + secure_vector<uint64_t>& S, size_t S_pos, + const byte input[], size_t length) { - if(length == 0) - return; - - while(length) + while(length > 0) { - size_t to_take = std::min(length, m_bitrate / 8 - m_S_pos); + size_t to_take = std::min(length, bitrate / 8 - S_pos); length -= to_take; - while(to_take && m_S_pos % 8) + while(to_take && S_pos % 8) { - m_S[m_S_pos / 8] ^= static_cast<u64bit>(input[0]) << (8 * (m_S_pos % 8)); + S[S_pos / 8] ^= static_cast<u64bit>(input[0]) << (8 * (S_pos % 8)); - ++m_S_pos; + ++S_pos; ++input; --to_take; } while(to_take && to_take % 8 == 0) { - m_S[m_S_pos / 8] ^= load_le<u64bit>(input, 0); - m_S_pos += 8; + S[S_pos / 8] ^= load_le<u64bit>(input, 0); + S_pos += 8; input += 8; to_take -= 8; } while(to_take) { - m_S[m_S_pos / 8] ^= static_cast<u64bit>(input[0]) << (8 * (m_S_pos % 8)); + S[S_pos / 8] ^= static_cast<u64bit>(input[0]) << (8 * (S_pos % 8)); - ++m_S_pos; + ++S_pos; ++input; --to_take; } - if(m_S_pos == m_bitrate / 8) + if(S_pos == bitrate / 8) { - SHA_3::permute(m_S.data()); - m_S_pos = 0; + SHA_3::permute(S.data()); + S_pos = 0; } } + + return S_pos; + } + +//static +void SHA_3::expand(size_t bitrate, + secure_vector<uint64_t>& S, + byte output[], size_t output_length) + { + BOTAN_ARG_CHECK(bitrate % 8 == 0); + + size_t Si = 0; + + for(size_t i = 0; i != output_length; ++i) + { + if(i > 0) + { + if(i % (bitrate / 8) == 0) + { + SHA_3::permute(S.data()); + Si = 0; + } + else if(i % 8 == 0) + { + Si += 1; + } + } + + output[i] = get_byte(7 - (i % 8), S[Si]); + } + } + +void SHA_3::add_data(const byte input[], size_t length) + { + m_S_pos = SHA_3::absorb(m_bitrate, m_S, m_S_pos, input, length); } void SHA_3::final_result(byte output[]) diff --git a/src/lib/hash/sha3/sha3.h b/src/lib/hash/sha3/sha3.h index c877bd938..649aa12fd 100644 --- a/src/lib/hash/sha3/sha3.h +++ b/src/lib/hash/sha3/sha3.h @@ -27,14 +27,6 @@ class BOTAN_DLL SHA_3 : public HashFunction */ SHA_3(size_t output_bits); - /** - * @param output_bits the size of the hash output; must be a - * multiple of 8 (ie, byte-wide outputs) - * @param capacity the capacity of the spong, normally always - * 2*output_bits with SHA-3. - */ - SHA_3(size_t output_bits, size_t capacity); - size_t hash_block_size() const override { return m_bitrate / 8; } size_t output_length() const override { return m_output_bits / 8; } @@ -42,6 +34,31 @@ class BOTAN_DLL SHA_3 : public HashFunction std::string name() const override; void clear() override; + // Static functions for internal usage + + /** + * Absorb data into the provided state + * @param bitrate the bitrate to absorb into the sponge + * @param S the sponge state + * @param S_pos where to begin absorbing into S + * @param input the input data + * @param length size of input in bytes + */ + static size_t absorb(size_t bitrate, + secure_vector<uint64_t>& S, size_t S_pos, + const byte input[], size_t length); + + /** + * Expand from provided state + * @param bitrate sponge parameter + * @param S the state + * @param output the output buffer + * @param output_length the size of output in bytes + */ + static void expand(size_t bitrate, + secure_vector<uint64_t>& S, + byte output[], size_t output_length); + /** * The bare Keccak-1600 permutation */ diff --git a/src/lib/hash/shake/info.txt b/src/lib/hash/shake/info.txt new file mode 100644 index 000000000..f579383eb --- /dev/null +++ b/src/lib/hash/shake/info.txt @@ -0,0 +1,5 @@ +define SHAKE 20161009 + +<requires> +sha3 +</requires> diff --git a/src/lib/hash/shake/shake.cpp b/src/lib/hash/shake/shake.cpp new file mode 100644 index 000000000..1ff6f1fd3 --- /dev/null +++ b/src/lib/hash/shake/shake.cpp @@ -0,0 +1,101 @@ +/* +* 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/parsing.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); + } + +void SHAKE_128::clear() + { + zeroise(m_S); + m_S_pos = 0; + } + +void SHAKE_128::add_data(const byte 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(byte output[]) + { + std::vector<byte> padding(SHAKE_128_BITRATE / 8 - m_S_pos); + + padding[0] = 0x1F; + padding[padding.size()-1] |= 0x80; + + add_data(padding.data(), padding.size()); + + 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); + } + +void SHAKE_256::clear() + { + zeroise(m_S); + m_S_pos = 0; + } + +void SHAKE_256::add_data(const byte 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(byte output[]) + { + std::vector<byte> padding(SHAKE_256_BITRATE / 8 - m_S_pos); + + padding[0] = 0x1F; + padding[padding.size()-1] |= 0x80; + + add_data(padding.data(), padding.size()); + + SHA_3::expand(SHAKE_256_BITRATE, m_S, output, output_length()); + + clear(); + } + +} diff --git a/src/lib/hash/shake/shake.h b/src/lib/hash/shake/shake.h new file mode 100644 index 000000000..96c171323 --- /dev/null +++ b/src/lib/hash/shake/shake.h @@ -0,0 +1,81 @@ +/* +* 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> + +namespace Botan { + +/** +* SHAKE-128 +*/ +class BOTAN_DLL SHAKE_128 : public HashFunction + { + public: + + /** + * @param output_bits the desired output size in bits + * must be a multiple of 8 + */ + 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::string name() const override; + void clear() override; + + private: + void add_data(const byte input[], size_t length) override; + void final_result(byte out[]) override; + + static const size_t SHAKE_128_BITRATE = 1600 - 256; + + size_t m_output_bits; + secure_vector<u64bit> m_S; + size_t m_S_pos; + }; + +/** +* SHAKE-256 +*/ +class BOTAN_DLL SHAKE_256 : public HashFunction + { + public: + + /** + * @param output_bits the desired output size in bits + * must be a multiple of 8 + */ + 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::string name() const override; + void clear() override; + + private: + void add_data(const byte input[], size_t length) override; + void final_result(byte out[]) override; + + static const size_t SHAKE_256_BITRATE = 1600 - 512; + + size_t m_output_bits; + secure_vector<u64bit> m_S; + size_t m_S_pos; + }; + +} + +#endif diff --git a/src/lib/stream/shake_cipher/shake_cipher.cpp b/src/lib/stream/shake_cipher/shake_cipher.cpp index 5701e7802..dc3c73299 100644 --- a/src/lib/stream/shake_cipher/shake_cipher.cpp +++ b/src/lib/stream/shake_cipher/shake_cipher.cpp @@ -11,13 +11,13 @@ namespace Botan { -SHAKE_128::SHAKE_128() : +SHAKE_128_Cipher::SHAKE_128_Cipher() : m_state(25), m_buffer((1600 - 256) / 8), m_buf_pos(0) {} -void SHAKE_128::cipher(const byte in[], byte out[], size_t length) +void SHAKE_128_Cipher::cipher(const byte in[], byte out[], size_t length) { while(length >= m_buffer.size() - m_buf_pos) { @@ -35,7 +35,7 @@ void SHAKE_128::cipher(const byte in[], byte out[], size_t length) m_buf_pos += length; } -void SHAKE_128::key_schedule(const byte key[], size_t length) +void SHAKE_128_Cipher::key_schedule(const byte key[], size_t length) { zeroise(m_state); @@ -51,14 +51,14 @@ void SHAKE_128::key_schedule(const byte key[], size_t length) copy_out_le(m_buffer.data(), m_buffer.size(), m_state.data()); } -void SHAKE_128::clear() +void SHAKE_128_Cipher::clear() { zeroise(m_state); zeroise(m_buffer); m_buf_pos = 0; } -void SHAKE_128::set_iv(const byte[], size_t length) +void SHAKE_128_Cipher::set_iv(const byte[], size_t length) { /* * This could be supported in some way (say, by treating iv as @@ -68,8 +68,8 @@ void SHAKE_128::set_iv(const byte[], size_t length) throw Invalid_IV_Length(name(), length); } -void SHAKE_128::seek(u64bit) +void SHAKE_128_Cipher::seek(u64bit) { - throw Not_Implemented("SHAKE_128::seek"); + throw Not_Implemented("SHAKE_128_Cipher::seek"); } } diff --git a/src/lib/stream/shake_cipher/shake_cipher.h b/src/lib/stream/shake_cipher/shake_cipher.h index 57eda58a4..40915ecea 100644 --- a/src/lib/stream/shake_cipher/shake_cipher.h +++ b/src/lib/stream/shake_cipher/shake_cipher.h @@ -16,10 +16,10 @@ namespace Botan { /** * SHAKE-128 XOF presented as a stream cipher */ -class BOTAN_DLL SHAKE_128 final : public StreamCipher +class BOTAN_DLL SHAKE_128_Cipher final : public StreamCipher { public: - SHAKE_128(); + SHAKE_128_Cipher(); /** * Produce more XOF output @@ -49,7 +49,7 @@ class BOTAN_DLL SHAKE_128 final : public StreamCipher void clear() override; std::string name() const override { return "SHAKE-128"; } - StreamCipher* clone() const override { return new SHAKE_128; } + StreamCipher* clone() const override { return new SHAKE_128_Cipher; } private: void key_schedule(const byte key[], size_t key_len) override; diff --git a/src/lib/stream/stream_cipher.cpp b/src/lib/stream/stream_cipher.cpp index 4b27caafe..dfe1fa69b 100644 --- a/src/lib/stream/stream_cipher.cpp +++ b/src/lib/stream/stream_cipher.cpp @@ -74,7 +74,7 @@ std::unique_ptr<StreamCipher> StreamCipher::create(const std::string& algo_spec, if(req.algo_name() == "SHAKE-128") { if(provider.empty() || provider == "base") - return std::unique_ptr<StreamCipher>(new SHAKE_128); + return std::unique_ptr<StreamCipher>(new SHAKE_128_Cipher); } #endif |