monotone

monotone Mtn Source Tree

Root/botan/mdx_hash.cpp

1/*************************************************
2* MDx Hash Function Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/mdx_hash.h>
7#include <botan/bit_ops.h>
8
9namespace Botan {
10
11/*************************************************
12* MDx_HashFunction Constructor *
13*************************************************/
14MDx_HashFunction::MDx_HashFunction(u32bit hash_len, u32bit block_len,
15 bool byte_end, bool bit_end,
16 u32bit cnt_size) :
17 HashFunction(hash_len, block_len), buffer(block_len),
18 BIG_BYTE_ENDIAN(byte_end), BIG_BIT_ENDIAN(bit_end), COUNT_SIZE(cnt_size)
19 {
20 if(COUNT_SIZE >= OUTPUT_LENGTH || COUNT_SIZE >= HASH_BLOCK_SIZE)
21 throw Invalid_Argument("MDx_HashFunction: COUNT_SIZE is too big");
22 count = position = 0;
23 }
24
25/*************************************************
26* Clear memory of sensitive data *
27*************************************************/
28void MDx_HashFunction::clear() throw()
29 {
30 buffer.clear();
31 count = position = 0;
32 }
33
34/*************************************************
35* Update the hash *
36*************************************************/
37void MDx_HashFunction::add_data(const byte input[], u32bit length)
38 {
39 count += length;
40 buffer.copy(position, input, length);
41 if(position + length >= HASH_BLOCK_SIZE)
42 {
43 hash(buffer.begin());
44 input += (HASH_BLOCK_SIZE - position);
45 length -= (HASH_BLOCK_SIZE - position);
46 while(length >= HASH_BLOCK_SIZE)
47 {
48 hash(input);
49 input += HASH_BLOCK_SIZE;
50 length -= HASH_BLOCK_SIZE;
51 }
52 buffer.copy(input, length);
53 position = 0;
54 }
55 position += length;
56 }
57
58/*************************************************
59* Finalize a Hash *
60*************************************************/
61void MDx_HashFunction::final_result(byte output[])
62 {
63 buffer[position] = (BIG_BIT_ENDIAN ? 0x80 : 0x01);
64 for(u32bit j = position+1; j != HASH_BLOCK_SIZE; j++)
65 buffer[j] = 0;
66 if(position >= HASH_BLOCK_SIZE - COUNT_SIZE)
67 {
68 hash(buffer);
69 buffer.clear();
70 }
71 write_count(buffer + HASH_BLOCK_SIZE - COUNT_SIZE);
72
73 hash(buffer);
74 copy_out(output);
75 clear();
76 }
77
78/*************************************************
79* Write the count bits to the buffer *
80*************************************************/
81void MDx_HashFunction::write_count(byte out[])
82 {
83 if(COUNT_SIZE < 8)
84 throw Invalid_State("MDx_HashFunction::write_count: COUNT_SIZE < 8");
85 for(u32bit j = 0; j != 8; j++)
86 {
87 const u32bit choose = (BIG_BYTE_ENDIAN ? (j % 8) : (7 - (j % 8)));
88 out[j+COUNT_SIZE-8] = get_byte(choose, 8 * count);
89 }
90 }
91
92}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status