Botan  1.10.9
Public Member Functions | Protected Member Functions | List of all members
Botan::MDx_HashFunction Class Referenceabstract

#include <mdx_hash.h>

Inheritance diagram for Botan::MDx_HashFunction:
Botan::HashFunction Botan::Buffered_Computation Botan::Algorithm Botan::BMW_512 Botan::HAS_160 Botan::MD4 Botan::MD5 Botan::RIPEMD_128 Botan::RIPEMD_160 Botan::SHA_160 Botan::SHA_224 Botan::SHA_256 Botan::SHA_384 Botan::SHA_512 Botan::Tiger Botan::Whirlpool

Public Member Functions

virtual HashFunctionclone () const =0
 
void final (byte out[])
 
SecureVector< bytefinal ()
 
size_t hash_block_size () const
 
 MDx_HashFunction (size_t block_length, bool big_byte_endian, bool big_bit_endian, size_t counter_size=8)
 
virtual std::string name () const =0
 
virtual size_t output_length () const =0
 
SecureVector< byteprocess (const byte in[], size_t length)
 
SecureVector< byteprocess (const MemoryRegion< byte > &in)
 
SecureVector< byteprocess (const std::string &in)
 
void update (const byte in[], size_t length)
 
void update (const MemoryRegion< byte > &in)
 
void update (const std::string &str)
 
void update (byte in)
 
template<typename T >
void update_be (const T in)
 

Protected Member Functions

void add_data (const byte input[], size_t length)
 
void clear ()
 
virtual void compress_n (const byte blocks[], size_t block_n)=0
 
virtual void copy_out (byte buffer[])=0
 
void final_result (byte output[])
 
virtual void write_count (byte out[])
 

Detailed Description

MDx Hash Function Base Class

Definition at line 18 of file mdx_hash.h.

Constructor & Destructor Documentation

Botan::MDx_HashFunction::MDx_HashFunction ( size_t  block_length,
bool  big_byte_endian,
bool  big_bit_endian,
size_t  counter_size = 8 
)
Parameters
block_lengthis the number of bytes per block
big_byte_endianspecifies if the hash uses big-endian bytes
big_bit_endianspecifies if the hash uses big-endian bits
counter_sizespecifies the size of the counter var in bytes

Definition at line 17 of file mdx_hash.cpp.

20  :
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  }

Member Function Documentation

void Botan::MDx_HashFunction::add_data ( const byte  input[],
size_t  length 
)
protectedvirtual

Add more data to the computation

Parameters
inputis an input buffer
lengthis the length of input in bytes

Implements Botan::Buffered_Computation.

Definition at line 41 of file mdx_hash.cpp.

References compress_n(), Botan::MemoryRegion< T >::copy(), and Botan::MemoryRegion< T >::size().

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  }
virtual void compress_n(const byte blocks[], size_t block_n)=0
void copy(const T in[], size_t n)
Definition: secmem.h:120
size_t size() const
Definition: secmem.h:29
void Botan::MDx_HashFunction::clear ( )
protectedvirtual
virtual HashFunction* Botan::HashFunction::clone ( ) const
pure virtualinherited
virtual void Botan::MDx_HashFunction::compress_n ( const byte  blocks[],
size_t  block_n 
)
protectedpure virtual

Run the hash's compression function over a set of blocks

Parameters
blocksthe input
block_nthe number of blocks

Implemented in Botan::SHA_160, Botan::MD4, and Botan::MD5.

Referenced by add_data(), and final_result().

virtual void Botan::MDx_HashFunction::copy_out ( byte  buffer[])
protectedpure virtual

Copy the output to the buffer

Parameters
bufferto put the output into

Implemented in Botan::SHA_160, Botan::MD4, and Botan::MD5.

Referenced by final_result().

void Botan::Buffered_Computation::final ( byte  out[])
inlineinherited
SecureVector<byte> Botan::Buffered_Computation::final ( )
inlineinherited

Complete the computation and retrieve the final result.

Returns
SecureVector holding the result

Definition at line 87 of file buf_comp.h.

88  {
89  SecureVector<byte> output(output_length());
90  final_result(&output[0]);
91  return output;
92  }
virtual size_t output_length() const =0
void Botan::MDx_HashFunction::final_result ( byte  out[])
protectedvirtual

Write the final output to out

Parameters
outis an output buffer of output_length()

Implements Botan::Buffered_Computation.

Definition at line 71 of file mdx_hash.cpp.

References clear(), compress_n(), copy_out(), Botan::MemoryRegion< T >::size(), write_count(), and Botan::zeroise().

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  }
virtual void compress_n(const byte blocks[], size_t block_n)=0
virtual void write_count(byte out[])
Definition: mdx_hash.cpp:93
size_t size() const
Definition: secmem.h:29
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:415
virtual void copy_out(byte buffer[])=0
size_t Botan::MDx_HashFunction::hash_block_size ( ) const
inlinevirtual

The hash block size as defined for this algorithm

Reimplemented from Botan::HashFunction.

Definition at line 32 of file mdx_hash.h.

Referenced by Botan::MD4::compress_n(), Botan::MD5::compress_n(), Botan::SHA_160::compress_n(), and write_count().

32 { return buffer.size(); }
size_t size() const
Definition: secmem.h:29
virtual std::string Botan::Algorithm::name ( ) const
pure virtualinherited
Returns
name of this algorithm

Implemented in Botan::AES_256, Botan::AES_256_NI, Botan::AES_256_SSSE3, Botan::Camellia_256, Botan::GOST_28147_89, Botan::AES_192, Botan::AES_192_NI, Botan::TLS_12_PRF, Botan::AES_192_SSSE3, Botan::TripleDES, Botan::Camellia_192, Botan::SHA_256, Botan::Comb4P, Botan::SHA_512, Botan::MessageAuthenticationCode, Botan::Lion, Botan::PKCS5_PBKDF1, Botan::WiderWake_41_BE, Botan::Skein_512, Botan::Keccak_1600, Botan::CTR_BE, Botan::OFB, Botan::Salsa20, Botan::Cascade_Cipher, Botan::LubyRackoff, Botan::Turing, Botan::RC2, Botan::Blowfish, Botan::OpenPGP_S2K, Botan::AES_128, Botan::AES_128_NI, Botan::TLS_PRF, Botan::KDF1, Botan::AES_128_SSSE3, Botan::Camellia_128, Botan::CAST_128, Botan::CAST_256, Botan::DES, Botan::DESX, Botan::IDEA, Botan::KASUMI, Botan::MARS, Botan::MISTY1, Botan::Noekeon, Botan::RC5, Botan::RC6, Botan::SAFER_SK, Botan::SEED, Botan::Serpent, Botan::Skipjack, Botan::Square, Botan::TEA, Botan::Twofish, Botan::XTEA, Botan::KDF2, Botan::ARC4, Botan::SSL3_PRF, Botan::X942_PRF, Botan::Parallel, Botan::HMAC, Botan::ANSI_X919_MAC, Botan::GOST_34_11, Botan::HAS_160, Botan::SHA_224, Botan::CBC_MAC, Botan::CMAC, Botan::SSL3_MAC, Botan::PKCS5_PBKDF2, Botan::Adler32, Botan::CRC24, Botan::CRC32, Botan::BMW_512, Botan::MD2, Botan::MD4, Botan::MD5, Botan::RIPEMD_128, Botan::RIPEMD_160, Botan::SHA_160, Botan::SHA_384, Botan::Tiger, and Botan::Whirlpool.

Referenced by Botan::Algorithm_Factory::add_block_cipher(), Botan::Algorithm_Factory::add_hash_function(), Botan::Algorithm_Factory::add_pbkdf(), Botan::Algorithm_Factory::add_stream_cipher(), Botan::ANSI_X919_MAC::ANSI_X919_MAC(), Botan::choose_sig_format(), Botan::CMAC::CMAC(), Botan::Comb4P::Comb4P(), Botan::EMSA2::EMSA2(), Botan::EMSA3::EMSA3(), Botan::HMAC::HMAC(), Botan::SSL3_MAC::name(), Botan::CBC_MAC::name(), Botan::CMAC::name(), Botan::HMAC::name(), Botan::PBE_PKCS5v15::name(), Botan::CBC_Encryption::name(), Botan::ECB_Encryption::name(), Botan::ANSI_X931_RNG::name(), Botan::Randpool::name(), Botan::PBE_PKCS5v20::name(), Botan::Cascade_Cipher::name(), Botan::LubyRackoff::name(), Botan::CTR_BE::name(), Botan::OFB::name(), Botan::Lion::name(), Botan::ECB_Decryption::name(), Botan::CBC_Decryption::name(), Botan::PBE_PKCS5v15::PBE_PKCS5v15(), Botan::PBE_PKCS5v20::PBE_PKCS5v20(), Botan::StreamCipher::set_iv(), and Botan::SSL3_MAC::SSL3_MAC().

virtual size_t Botan::Buffered_Computation::output_length ( ) const
pure virtualinherited
SecureVector<byte> Botan::Buffered_Computation::process ( const byte  in[],
size_t  length 
)
inlineinherited

Update and finalize computation. Does the same as calling update() and final() consecutively.

Parameters
inthe input to process as a byte array
lengththe length of the byte array
Returns
the result of the call to final()

Definition at line 101 of file buf_comp.h.

Referenced by Botan::Randpool::add_entropy(), Botan::EME1::EME1(), Botan::RTSS_Share::split(), and Botan::Cert_Extension::Subject_Key_ID::Subject_Key_ID().

102  {
103  add_data(in, length);
104  return final();
105  }
SecureVector<byte> Botan::Buffered_Computation::process ( const MemoryRegion< byte > &  in)
inlineinherited

Update and finalize computation. Does the same as calling update() and final() consecutively.

Parameters
inthe input to process
Returns
the result of the call to final()

Definition at line 113 of file buf_comp.h.

References Botan::MemoryRegion< T >::size().

114  {
115  add_data(&in[0], in.size());
116  return final();
117  }
size_t size() const
Definition: secmem.h:29
SecureVector<byte> Botan::Buffered_Computation::process ( const std::string &  in)
inlineinherited

Update and finalize computation. Does the same as calling update() and final() consecutively.

Parameters
inthe input to process as a string
Returns
the result of the call to final()

Definition at line 125 of file buf_comp.h.

126  {
127  update(in);
128  return final();
129  }
void update(const byte in[], size_t length)
Definition: buf_comp.h:33
void Botan::Buffered_Computation::update ( const byte  in[],
size_t  length 
)
inlineinherited
void Botan::Buffered_Computation::update ( const MemoryRegion< byte > &  in)
inlineinherited

Add new input to process.

Parameters
inthe input to process as a MemoryRegion

Definition at line 39 of file buf_comp.h.

References Botan::MemoryRegion< T >::size().

40  {
41  add_data(&in[0], in.size());
42  }
size_t size() const
Definition: secmem.h:29
void Botan::Buffered_Computation::update ( const std::string &  str)
inlineinherited

Add new input to process.

Parameters
strthe input to process as a std::string. Will be interpreted as a byte array based on the strings encoding.

Definition at line 63 of file buf_comp.h.

64  {
65  add_data(reinterpret_cast<const byte*>(str.data()), str.size());
66  }
void Botan::Buffered_Computation::update ( byte  in)
inlineinherited

Process a single byte.

Parameters
inthe byte to process

Definition at line 72 of file buf_comp.h.

72 { add_data(&in, 1); }
template<typename T >
void Botan::Buffered_Computation::update_be ( const T  in)
inlineinherited

Add an integer in big-endian order

Parameters
inthe value

Definition at line 48 of file buf_comp.h.

References Botan::get_byte().

Referenced by Botan::KDF2::derive(), Botan::PKCS5_PBKDF2::derive_key(), Botan::Record_Reader::get_record(), and Botan::MGF1::mask().

49  {
50  for(size_t i = 0; i != sizeof(T); ++i)
51  {
52  byte b = get_byte(i, in);
53  add_data(&b, 1);
54  }
55  }
byte get_byte(size_t byte_num, T input)
Definition: get_byte.h:21
unsigned char byte
Definition: types.h:22
void Botan::MDx_HashFunction::write_count ( byte  out[])
protectedvirtual

Write the count, if used, to this spot

Parameters
outwhere to write the counter to

Definition at line 93 of file mdx_hash.cpp.

References hash_block_size(), Botan::Buffered_Computation::output_length(), Botan::store_be(), and Botan::store_le().

Referenced by final_result().

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  }
size_t hash_block_size() const
Definition: mdx_hash.h:32
void store_le(u16bit in, byte out[2])
Definition: loadstor.h:427
std::invalid_argument Invalid_Argument
Definition: exceptn.h:20
unsigned long long u64bit
Definition: types.h:49
void store_be(u16bit in, byte out[2])
Definition: loadstor.h:412
virtual size_t output_length() const =0

The documentation for this class was generated from the following files: