Botan: src/hash/mdx_hash/mdx_hash.cpp Source File

Botan 1.10.17

mdx_hash.cpp
Go to the documentation of this file.
1 /*
2 * Merkle-Damgard Hash Function
3 * (C) 1999-2008 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/mdx_hash.h>
9 #include <botan/exceptn.h>
10 #include <botan/loadstor.h>
11
12 namespace Botan {
13
14 /*
15 * MDx_HashFunction Constructor
16 */
17 MDx_HashFunction::MDx_HashFunction(size_t block_len,
18 bool byte_end,
19 bool bit_end,
20 size_t cnt_size) :
21 buffer(block_len),
22 BIG_BYTE_ENDIAN(byte_end),
23 BIG_BIT_ENDIAN(bit_end),
24 COUNT_SIZE(cnt_size)
25 {
26 count = position = 0;
27 }
28
29 /*
30 * Clear memory of sensitive data
31 */
32 void MDx_HashFunction::clear()
33 {
34 zeroise(buffer);
35 count = position = 0;
36 }
37
38 /*
39 * Update the hash
40 */
41 void MDx_HashFunction::add_data(const byte input[], size_t length)
42 {
43 count += length;
44
45 if(position)
46 {
47 buffer.copy(position, input, length);
48
49 if(position + length >= buffer.size())
50 {
51 compress_n(&buffer[0], 1);
52 input += (buffer.size() - position);
53 length -= (buffer.size() - position);
54 position = 0;
55 }
56 }
57
58 const size_t full_blocks = length / buffer.size();
59 const size_t remaining = length % buffer.size();
60
61 if(full_blocks)
62 compress_n(input, full_blocks);
63
64 buffer.copy(position, input + full_blocks * buffer.size(), remaining);
65 position += remaining;
66 }
67
68 /*
69 * Finalize a hash
70 */
71 void MDx_HashFunction::final_result(byte output[])
72 {
73 buffer[position] = (BIG_BIT_ENDIAN ? 0x80 : 0x01);
74 for(size_t i = position+1; i != buffer.size(); ++i)
75 buffer[i] = 0;
76
77 if(position >= buffer.size() - COUNT_SIZE)
78 {
79 compress_n(&buffer[0], 1);
80 zeroise(buffer);
81 }
82
83 write_count(&buffer[buffer.size() - COUNT_SIZE]);
84
85 compress_n(&buffer[0], 1);
86 copy_out(output);
87 clear();
88 }
89
90 /*
91 * Write the count bits to the buffer
92 */
93 void MDx_HashFunction::write_count(byte out[])
94 {
95 if(COUNT_SIZE < 8)
96 throw Invalid_State("MDx_HashFunction::write_count: COUNT_SIZE < 8");
97 if(COUNT_SIZE >= output_length() || COUNT_SIZE >= hash_block_size())
98 throw Invalid_Argument("MDx_HashFunction: COUNT_SIZE is too big");
99
100 const u64bit bit_count = count * 8;
101
102 if(BIG_BYTE_ENDIAN)
103 store_be(bit_count, out + COUNT_SIZE - 8);
104 else
105 store_le(bit_count, out + COUNT_SIZE - 8);
106 }
107
108 }
Botan::MDx_HashFunction::add_data
void add_data(const byte input[], size_t length)
Definition: mdx_hash.cpp:41
Botan::MDx_HashFunction::compress_n
virtual void compress_n(const byte blocks[], size_t block_n)=0
Botan::store_le
void store_le(u16bit in, byte out[2])
Definition: loadstor.h:427
Botan::MDx_HashFunction::final_result
void final_result(byte output[])
Definition: mdx_hash.cpp:71
Botan::Invalid_State
Definition: exceptn.h:25
Botan::Invalid_Argument
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
Botan::MDx_HashFunction::write_count
virtual void write_count(byte out[])
Definition: mdx_hash.cpp:93
Botan::MemoryRegion::copy
void copy(const T in[], size_t n)
Definition: secmem.h:126
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::u64bit
unsigned long long u64bit
Definition: types.h:49
Botan
Definition: algo_base.h:14
Botan::MemoryRegion::size
size_t size() const
Definition: secmem.h:29
Botan::store_be
void store_be(u16bit in, byte out[2])
Definition: loadstor.h:412
Botan::MDx_HashFunction::clear
void clear()
Definition: mdx_hash.cpp:32
Botan::MDx_HashFunction::hash_block_size
size_t hash_block_size() const
Definition: mdx_hash.h:32
Botan::zeroise
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:428
Botan::Buffered_Computation::output_length
virtual size_t output_length() const =0
Botan::MDx_HashFunction::MDx_HashFunction
MDx_HashFunction(size_t block_length, bool big_byte_endian, bool big_bit_endian, size_t counter_size=8)
Definition: mdx_hash.cpp:17
Botan::MDx_HashFunction::copy_out
virtual void copy_out(byte buffer[])=0