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
12namespace Botan {
13
14/*
15* MDx_HashFunction Constructor
16*/
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*/
33 {
34 zeroise(buffer);
35 count = position = 0;
36 }
37
38/*
39* Update the hash
40*/
41void 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*/
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*/
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}
virtual size_t output_length() const =0
void add_data(const byte input[], size_t length)
Definition mdx_hash.cpp:41
MDx_HashFunction(size_t block_length, bool big_byte_endian, bool big_bit_endian, size_t counter_size=8)
Definition mdx_hash.cpp:17
virtual void compress_n(const byte blocks[], size_t block_n)=0
size_t hash_block_size() const
Definition mdx_hash.h:32
virtual void copy_out(byte buffer[])=0
virtual void write_count(byte out[])
Definition mdx_hash.cpp:93
void final_result(byte output[])
Definition mdx_hash.cpp:71
void zeroise(MemoryRegion< T > &vec)
Definition secmem.h:428
unsigned long long u64bit
Definition types.h:49
std::invalid_argument Invalid_Argument
Definition exceptn.h:20
void store_be(u16bit in, byte out[2])
Definition loadstor.h:412
void store_le(u16bit in, byte out[2])
Definition loadstor.h:427
Invalid_State(const std::string &err)
Definition exceptn.h:27