Botan  1.10.9
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
Botan::SHA_160 Class Reference

#include <sha160.h>

Inheritance diagram for Botan::SHA_160:
Botan::MDx_HashFunction Botan::HashFunction Botan::Buffered_Computation Botan::Algorithm Botan::SHA_160_SSE2 Botan::SHA_160_X86_32 Botan::SHA_160_X86_64

Public Member Functions

void clear ()
 
HashFunctionclone () const
 
void final (byte out[])
 
SecureVector< bytefinal ()
 
size_t hash_block_size () const
 
std::string name () const
 
size_t output_length () const
 
SecureVector< byteprocess (const byte in[], size_t length)
 
SecureVector< byteprocess (const MemoryRegion< byte > &in)
 
SecureVector< byteprocess (const std::string &in)
 
 SHA_160 ()
 
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 compress_n (const byte[], size_t blocks)
 
void copy_out (byte[])
 
void final_result (byte output[])
 
 SHA_160 (size_t W_size)
 
virtual void write_count (byte out[])
 

Protected Attributes

SecureVector< u32bitdigest
 
SecureVector< u32bitW
 

Detailed Description

NIST's SHA-160

Definition at line 18 of file sha160.h.

Constructor & Destructor Documentation

Botan::SHA_160::SHA_160 ( )
inline

Definition at line 27 of file sha160.h.

27  : MDx_HashFunction(64, true, true), digest(5), W(80)
28  {
29  clear();
30  }
SecureVector< u32bit > W
Definition: sha160.h:55
void clear()
Definition: sha160.cpp:150
SecureVector< u32bit > digest
Definition: sha160.h:50
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::SHA_160::SHA_160 ( size_t  W_size)
inlineprotected

Set a custom size for the W array. Normally 80, but some subclasses need slightly more for best performance/internal constraints

Parameters
W_sizehow big to make W

Definition at line 38 of file sha160.h.

38  :
39  MDx_HashFunction(64, true, true), digest(5), W(W_size)
40  {
41  clear();
42  }
SecureVector< u32bit > W
Definition: sha160.h:55
void clear()
Definition: sha160.cpp:150
SecureVector< u32bit > digest
Definition: sha160.h:50
MDx_HashFunction(size_t block_length, bool big_byte_endian, bool big_bit_endian, size_t counter_size=8)
Definition: mdx_hash.cpp:17

Member Function Documentation

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

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 Botan::MDx_HashFunction::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::SHA_160::clear ( )
virtual

Zeroize internal state

Reimplemented from Botan::MDx_HashFunction.

Definition at line 150 of file sha160.cpp.

References Botan::MDx_HashFunction::clear(), digest, W, and Botan::zeroise().

151  {
153  zeroise(W);
154  digest[0] = 0x67452301;
155  digest[1] = 0xEFCDAB89;
156  digest[2] = 0x98BADCFE;
157  digest[3] = 0x10325476;
158  digest[4] = 0xC3D2E1F0;
159  }
SecureVector< u32bit > W
Definition: sha160.h:55
SecureVector< u32bit > digest
Definition: sha160.h:50
void zeroise(MemoryRegion< T > &vec)
Definition: secmem.h:415
HashFunction* Botan::SHA_160::clone ( ) const
inlinevirtual

Get a new object representing the same algorithm as *this

Implements Botan::HashFunction.

Reimplemented in Botan::SHA_160_SSE2, Botan::SHA_160_X86_32, and Botan::SHA_160_X86_64.

Definition at line 23 of file sha160.h.

23 { return new SHA_160; }
void Botan::SHA_160::compress_n ( const byte  blocks[],
size_t  block_n 
)
protectedvirtual

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

Parameters
blocksthe input
block_nthe number of blocks

Implements Botan::MDx_HashFunction.

Definition at line 61 of file sha160.cpp.

References digest, Botan::HAS_160_F::F1(), Botan::HAS_160_F::F2(), Botan::HAS_160_F::F3(), Botan::HAS_160_F::F4(), Botan::MDx_HashFunction::hash_block_size(), Botan::load_be(), Botan::rotate_left(), and W.

62  {
63  using namespace SHA1_F;
64 
65  u32bit A = digest[0], B = digest[1], C = digest[2],
66  D = digest[3], E = digest[4];
67 
68  for(size_t i = 0; i != blocks; ++i)
69  {
70  load_be(&W[0], input, 16);
71 
72  for(size_t j = 16; j != 80; j += 8)
73  {
74  W[j ] = rotate_left((W[j-3] ^ W[j-8] ^ W[j-14] ^ W[j-16]), 1);
75  W[j+1] = rotate_left((W[j-2] ^ W[j-7] ^ W[j-13] ^ W[j-15]), 1);
76  W[j+2] = rotate_left((W[j-1] ^ W[j-6] ^ W[j-12] ^ W[j-14]), 1);
77  W[j+3] = rotate_left((W[j ] ^ W[j-5] ^ W[j-11] ^ W[j-13]), 1);
78  W[j+4] = rotate_left((W[j+1] ^ W[j-4] ^ W[j-10] ^ W[j-12]), 1);
79  W[j+5] = rotate_left((W[j+2] ^ W[j-3] ^ W[j- 9] ^ W[j-11]), 1);
80  W[j+6] = rotate_left((W[j+3] ^ W[j-2] ^ W[j- 8] ^ W[j-10]), 1);
81  W[j+7] = rotate_left((W[j+4] ^ W[j-1] ^ W[j- 7] ^ W[j- 9]), 1);
82  }
83 
84  F1(A, B, C, D, E, W[ 0]); F1(E, A, B, C, D, W[ 1]);
85  F1(D, E, A, B, C, W[ 2]); F1(C, D, E, A, B, W[ 3]);
86  F1(B, C, D, E, A, W[ 4]); F1(A, B, C, D, E, W[ 5]);
87  F1(E, A, B, C, D, W[ 6]); F1(D, E, A, B, C, W[ 7]);
88  F1(C, D, E, A, B, W[ 8]); F1(B, C, D, E, A, W[ 9]);
89  F1(A, B, C, D, E, W[10]); F1(E, A, B, C, D, W[11]);
90  F1(D, E, A, B, C, W[12]); F1(C, D, E, A, B, W[13]);
91  F1(B, C, D, E, A, W[14]); F1(A, B, C, D, E, W[15]);
92  F1(E, A, B, C, D, W[16]); F1(D, E, A, B, C, W[17]);
93  F1(C, D, E, A, B, W[18]); F1(B, C, D, E, A, W[19]);
94 
95  F2(A, B, C, D, E, W[20]); F2(E, A, B, C, D, W[21]);
96  F2(D, E, A, B, C, W[22]); F2(C, D, E, A, B, W[23]);
97  F2(B, C, D, E, A, W[24]); F2(A, B, C, D, E, W[25]);
98  F2(E, A, B, C, D, W[26]); F2(D, E, A, B, C, W[27]);
99  F2(C, D, E, A, B, W[28]); F2(B, C, D, E, A, W[29]);
100  F2(A, B, C, D, E, W[30]); F2(E, A, B, C, D, W[31]);
101  F2(D, E, A, B, C, W[32]); F2(C, D, E, A, B, W[33]);
102  F2(B, C, D, E, A, W[34]); F2(A, B, C, D, E, W[35]);
103  F2(E, A, B, C, D, W[36]); F2(D, E, A, B, C, W[37]);
104  F2(C, D, E, A, B, W[38]); F2(B, C, D, E, A, W[39]);
105 
106  F3(A, B, C, D, E, W[40]); F3(E, A, B, C, D, W[41]);
107  F3(D, E, A, B, C, W[42]); F3(C, D, E, A, B, W[43]);
108  F3(B, C, D, E, A, W[44]); F3(A, B, C, D, E, W[45]);
109  F3(E, A, B, C, D, W[46]); F3(D, E, A, B, C, W[47]);
110  F3(C, D, E, A, B, W[48]); F3(B, C, D, E, A, W[49]);
111  F3(A, B, C, D, E, W[50]); F3(E, A, B, C, D, W[51]);
112  F3(D, E, A, B, C, W[52]); F3(C, D, E, A, B, W[53]);
113  F3(B, C, D, E, A, W[54]); F3(A, B, C, D, E, W[55]);
114  F3(E, A, B, C, D, W[56]); F3(D, E, A, B, C, W[57]);
115  F3(C, D, E, A, B, W[58]); F3(B, C, D, E, A, W[59]);
116 
117  F4(A, B, C, D, E, W[60]); F4(E, A, B, C, D, W[61]);
118  F4(D, E, A, B, C, W[62]); F4(C, D, E, A, B, W[63]);
119  F4(B, C, D, E, A, W[64]); F4(A, B, C, D, E, W[65]);
120  F4(E, A, B, C, D, W[66]); F4(D, E, A, B, C, W[67]);
121  F4(C, D, E, A, B, W[68]); F4(B, C, D, E, A, W[69]);
122  F4(A, B, C, D, E, W[70]); F4(E, A, B, C, D, W[71]);
123  F4(D, E, A, B, C, W[72]); F4(C, D, E, A, B, W[73]);
124  F4(B, C, D, E, A, W[74]); F4(A, B, C, D, E, W[75]);
125  F4(E, A, B, C, D, W[76]); F4(D, E, A, B, C, W[77]);
126  F4(C, D, E, A, B, W[78]); F4(B, C, D, E, A, W[79]);
127 
128  A = (digest[0] += A);
129  B = (digest[1] += B);
130  C = (digest[2] += C);
131  D = (digest[3] += D);
132  E = (digest[4] += E);
133 
134  input += hash_block_size();
135  }
136  }
T load_be(const byte in[], size_t off)
Definition: loadstor.h:100
size_t hash_block_size() const
Definition: mdx_hash.h:32
T rotate_left(T input, size_t rot)
Definition: rotate.h:21
SecureVector< u32bit > W
Definition: sha160.h:55
void F2(u32bit A, u32bit &B, u32bit C, u32bit D, u32bit &E, u32bit msg, u32bit rot)
Definition: has160.cpp:29
void F3(u32bit A, u32bit &B, u32bit C, u32bit D, u32bit &E, u32bit msg, u32bit rot)
Definition: has160.cpp:39
SecureVector< u32bit > digest
Definition: sha160.h:50
void F4(u32bit A, u32bit &B, u32bit C, u32bit D, u32bit &E, u32bit msg, u32bit rot)
Definition: has160.cpp:49
void F1(u32bit A, u32bit &B, u32bit C, u32bit D, u32bit &E, u32bit msg, u32bit rot)
Definition: has160.cpp:19
unsigned int u32bit
Definition: types.h:32
void Botan::SHA_160::copy_out ( byte  buffer[])
protectedvirtual

Copy the output to the buffer

Parameters
bufferto put the output into

Implements Botan::MDx_HashFunction.

Definition at line 141 of file sha160.cpp.

References digest, output_length(), and Botan::store_be().

142  {
143  for(size_t i = 0; i != output_length(); i += 4)
144  store_be(digest[i/4], output + i);
145  }
SecureVector< u32bit > digest
Definition: sha160.h:50
void store_be(u16bit in, byte out[2])
Definition: loadstor.h:412
size_t output_length() const
Definition: sha160.h:22
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[])
protectedvirtualinherited

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 Botan::MDx_HashFunction::clear(), Botan::MDx_HashFunction::compress_n(), Botan::MDx_HashFunction::copy_out(), Botan::MemoryRegion< T >::size(), Botan::MDx_HashFunction::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
inlinevirtualinherited

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(), compress_n(), and Botan::MDx_HashFunction::write_count().

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

Implements Botan::Algorithm.

Definition at line 21 of file sha160.h.

21 { return "SHA-160"; }
size_t Botan::SHA_160::output_length ( ) const
inlinevirtual
Returns
length of the output of this function in bytes

Implements Botan::Buffered_Computation.

Definition at line 22 of file sha160.h.

Referenced by copy_out().

22 { return 20; }
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[])
protectedvirtualinherited

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 Botan::MDx_HashFunction::hash_block_size(), Botan::Buffered_Computation::output_length(), Botan::store_be(), and Botan::store_le().

Referenced by Botan::MDx_HashFunction::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

Member Data Documentation

SecureVector<u32bit> Botan::SHA_160::digest
protected

The digest value, exposed for use by subclasses (asm, SSE2)

Definition at line 50 of file sha160.h.

Referenced by clear(), compress_n(), and copy_out().

SecureVector<u32bit> Botan::SHA_160::W
protected

The message buffer, exposed for use by subclasses (asm, SSE2)

Definition at line 55 of file sha160.h.

Referenced by clear(), and compress_n().


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