diff options
author | Jack Lloyd <[email protected]> | 2018-12-07 17:27:13 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-12-08 06:59:36 -0500 |
commit | 0c2da7d179bd029a6abaafb6f9b54bf5b079df52 (patch) | |
tree | 90603a3ee25155af64afc1c8bff5d7637774b1bb /src/lib/hash | |
parent | 1605c244695a9d2b871dc46c8dbe6bc3fced45a6 (diff) |
Cleanups in MDx_HashFunction
Diffstat (limited to 'src/lib/hash')
-rw-r--r-- | src/lib/hash/mdx_hash/mdx_hash.cpp | 57 | ||||
-rw-r--r-- | src/lib/hash/mdx_hash/mdx_hash.h | 12 |
2 files changed, 37 insertions, 32 deletions
diff --git a/src/lib/hash/mdx_hash/mdx_hash.cpp b/src/lib/hash/mdx_hash/mdx_hash.cpp index 7d163dbfb..75893d51a 100644 --- a/src/lib/hash/mdx_hash/mdx_hash.cpp +++ b/src/lib/hash/mdx_hash/mdx_hash.cpp @@ -1,6 +1,6 @@ /* * Merkle-Damgard Hash Function -* (C) 1999-2008 Jack Lloyd +* (C) 1999-2008,2018 Jack Lloyd * * Botan is released under the Simplified BSD License (see license.txt) */ @@ -15,16 +15,19 @@ namespace Botan { * MDx_HashFunction Constructor */ MDx_HashFunction::MDx_HashFunction(size_t block_len, - bool byte_end, - bool bit_end, - size_t cnt_size) : - m_buffer(block_len), + bool byte_big_endian, + bool bit_big_endian, + uint8_t cnt_size) : + m_pad_char(bit_big_endian == true ? 0x80 : 0x01), + m_counter_size(cnt_size), + m_count_big_endian(byte_big_endian), + m_block_len(block_len), m_count(0), - m_position(0), - BIG_BYTE_ENDIAN(byte_end), - BIG_BIT_ENDIAN(bit_end), - COUNT_SIZE(cnt_size) + m_buffer(m_block_len), + m_position(0) { + if(m_counter_size < 8 || m_counter_size > m_block_len) + throw Invalid_State("MDx_HashFunction invalid counter length"); } /* @@ -47,22 +50,24 @@ void MDx_HashFunction::add_data(const uint8_t input[], size_t length) { buffer_insert(m_buffer, m_position, input, length); - if(m_position + length >= m_buffer.size()) + if(m_position + length >= m_block_len) { compress_n(m_buffer.data(), 1); - input += (m_buffer.size() - m_position); - length -= (m_buffer.size() - m_position); + input += (m_block_len - m_position); + length -= (m_block_len - m_position); m_position = 0; } } - const size_t full_blocks = length / m_buffer.size(); - const size_t remaining = length % m_buffer.size(); + const size_t full_blocks = length / m_block_len; + const size_t remaining = length % m_block_len; - if(full_blocks) + if(full_blocks > 0) + { compress_n(input, full_blocks); + } - buffer_insert(m_buffer, m_position, input + full_blocks * m_buffer.size(), remaining); + buffer_insert(m_buffer, m_position, input + full_blocks * m_block_len, remaining); m_position += remaining; } @@ -71,16 +76,16 @@ void MDx_HashFunction::add_data(const uint8_t input[], size_t length) */ void MDx_HashFunction::final_result(uint8_t output[]) { - clear_mem(&m_buffer[m_position], m_buffer.size() - m_position); - m_buffer[m_position] = (BIG_BIT_ENDIAN ? 0x80 : 0x01); + clear_mem(&m_buffer[m_position], m_block_len - m_position); + m_buffer[m_position] = m_pad_char; - if(m_position >= m_buffer.size() - COUNT_SIZE) + if(m_position >= m_block_len - m_counter_size) { compress_n(m_buffer.data(), 1); zeroise(m_buffer); } - write_count(&m_buffer[m_buffer.size() - COUNT_SIZE]); + write_count(&m_buffer[m_block_len - m_counter_size]); compress_n(m_buffer.data(), 1); copy_out(output); @@ -92,17 +97,15 @@ void MDx_HashFunction::final_result(uint8_t output[]) */ void MDx_HashFunction::write_count(uint8_t out[]) { - if(COUNT_SIZE < 8) - throw Invalid_State("MDx_HashFunction::write_count: COUNT_SIZE < 8"); - if(COUNT_SIZE >= output_length() || COUNT_SIZE >= hash_block_size()) - throw Invalid_Argument("MDx_HashFunction: COUNT_SIZE is too big"); + BOTAN_ASSERT_NOMSG(m_counter_size <= output_length()); + BOTAN_ASSERT_NOMSG(m_counter_size >= 8); const uint64_t bit_count = m_count * 8; - if(BIG_BYTE_ENDIAN) - store_be(bit_count, out + COUNT_SIZE - 8); + if(m_count_big_endian) + store_be(bit_count, out + m_counter_size - 8); else - store_le(bit_count, out + COUNT_SIZE - 8); + store_le(bit_count, out + m_counter_size - 8); } } diff --git a/src/lib/hash/mdx_hash/mdx_hash.h b/src/lib/hash/mdx_hash/mdx_hash.h index f958e9fb7..bd2752bef 100644 --- a/src/lib/hash/mdx_hash/mdx_hash.h +++ b/src/lib/hash/mdx_hash/mdx_hash.h @@ -27,7 +27,7 @@ class BOTAN_PUBLIC_API(2,0) MDx_HashFunction : public HashFunction MDx_HashFunction(size_t block_length, bool big_byte_endian, bool big_bit_endian, - size_t counter_size = 8); + uint8_t counter_size = 8); size_t hash_block_size() const override final { return m_buffer.size(); } protected: @@ -55,12 +55,14 @@ class BOTAN_PUBLIC_API(2,0) MDx_HashFunction : public HashFunction */ virtual void write_count(uint8_t out[]); private: - secure_vector<uint8_t> m_buffer; + const uint8_t m_pad_char; + const uint8_t m_counter_size; + const bool m_count_big_endian; + const size_t m_block_len; + uint64_t m_count; + secure_vector<uint8_t> m_buffer; size_t m_position; - - const bool BIG_BYTE_ENDIAN, BIG_BIT_ENDIAN; - const size_t COUNT_SIZE; }; } |