Botan  1.11.15
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes
Botan::SHA_160 Class Reference

#include <sha160.h>

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

List of all members.

Public Types

typedef SCAN_Name Spec

Public Member Functions

void clear ()
HashFunctionclone () const
void final (byte out[])
secure_vector< bytefinal ()
template<typename Alloc >
void final (std::vector< byte, Alloc > &out)
size_t hash_block_size () const
std::string name () const
size_t output_length () const
secure_vector< byteprocess (const byte in[], size_t length)
secure_vector< byteprocess (const secure_vector< byte > &in)
secure_vector< byteprocess (const std::vector< byte > &in)
secure_vector< byteprocess (const std::string &in)
 SHA_160 ()
void update (const byte in[], size_t length)
void update (const secure_vector< byte > &in)
void update (const std::vector< 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

secure_vector< u32bitdigest
secure_vector< u32bitW

Detailed Description

NIST's SHA-160

Definition at line 18 of file sha160.h.


Member Typedef Documentation

typedef SCAN_Name Botan::HashFunction::Spec [inherited]

Definition at line 37 of file hash.h.


Constructor & Destructor Documentation

Definition at line 27 of file sha160.h.

                : MDx_HashFunction(64, true, true), digest(5), W(80)
         {
         clear();
         }
Botan::SHA_160::SHA_160 ( size_t  W_size) [inline, protected]

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.

                             :
         MDx_HashFunction(64, true, true), digest(5), W(W_size)
         {
         clear();
         }

Member Function Documentation

void Botan::MDx_HashFunction::add_data ( const byte  input[],
size_t  length 
) [protected, virtual, inherited]

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

   {
   count += length;

   if(position)
      {
      buffer_insert(buffer, position, input, length);

      if(position + length >= buffer.size())
         {
         compress_n(&buffer[0], 1);
         input += (buffer.size() - position);
         length -= (buffer.size() - position);
         position = 0;
         }
      }

   const size_t full_blocks = length / buffer.size();
   const size_t remaining   = length % buffer.size();

   if(full_blocks)
      compress_n(input, full_blocks);

   buffer_insert(buffer, position, input + full_blocks * buffer.size(), remaining);
   position += remaining;
   }
void Botan::SHA_160::clear ( ) [virtual]

Reimplemented from Botan::MDx_HashFunction.

Definition at line 150 of file sha160.cpp.

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

   {
   MDx_HashFunction::clear();
   zeroise(W);
   digest[0] = 0x67452301;
   digest[1] = 0xEFCDAB89;
   digest[2] = 0x98BADCFE;
   digest[3] = 0x10325476;
   digest[4] = 0xC3D2E1F0;
   }
HashFunction* Botan::SHA_160::clone ( ) const [inline, virtual]
Returns:
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.

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

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 62 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.

   {
   using namespace SHA1_F;

   u32bit A = digest[0], B = digest[1], C = digest[2],
          D = digest[3], E = digest[4];

   for(size_t i = 0; i != blocks; ++i)
      {
      load_be(&W[0], input, 16);

      for(size_t j = 16; j != 80; j += 8)
         {
         W[j  ] = rotate_left((W[j-3] ^ W[j-8] ^ W[j-14] ^ W[j-16]), 1);
         W[j+1] = rotate_left((W[j-2] ^ W[j-7] ^ W[j-13] ^ W[j-15]), 1);
         W[j+2] = rotate_left((W[j-1] ^ W[j-6] ^ W[j-12] ^ W[j-14]), 1);
         W[j+3] = rotate_left((W[j  ] ^ W[j-5] ^ W[j-11] ^ W[j-13]), 1);
         W[j+4] = rotate_left((W[j+1] ^ W[j-4] ^ W[j-10] ^ W[j-12]), 1);
         W[j+5] = rotate_left((W[j+2] ^ W[j-3] ^ W[j- 9] ^ W[j-11]), 1);
         W[j+6] = rotate_left((W[j+3] ^ W[j-2] ^ W[j- 8] ^ W[j-10]), 1);
         W[j+7] = rotate_left((W[j+4] ^ W[j-1] ^ W[j- 7] ^ W[j- 9]), 1);
         }

      F1(A, B, C, D, E, W[ 0]);   F1(E, A, B, C, D, W[ 1]);
      F1(D, E, A, B, C, W[ 2]);   F1(C, D, E, A, B, W[ 3]);
      F1(B, C, D, E, A, W[ 4]);   F1(A, B, C, D, E, W[ 5]);
      F1(E, A, B, C, D, W[ 6]);   F1(D, E, A, B, C, W[ 7]);
      F1(C, D, E, A, B, W[ 8]);   F1(B, C, D, E, A, W[ 9]);
      F1(A, B, C, D, E, W[10]);   F1(E, A, B, C, D, W[11]);
      F1(D, E, A, B, C, W[12]);   F1(C, D, E, A, B, W[13]);
      F1(B, C, D, E, A, W[14]);   F1(A, B, C, D, E, W[15]);
      F1(E, A, B, C, D, W[16]);   F1(D, E, A, B, C, W[17]);
      F1(C, D, E, A, B, W[18]);   F1(B, C, D, E, A, W[19]);

      F2(A, B, C, D, E, W[20]);   F2(E, A, B, C, D, W[21]);
      F2(D, E, A, B, C, W[22]);   F2(C, D, E, A, B, W[23]);
      F2(B, C, D, E, A, W[24]);   F2(A, B, C, D, E, W[25]);
      F2(E, A, B, C, D, W[26]);   F2(D, E, A, B, C, W[27]);
      F2(C, D, E, A, B, W[28]);   F2(B, C, D, E, A, W[29]);
      F2(A, B, C, D, E, W[30]);   F2(E, A, B, C, D, W[31]);
      F2(D, E, A, B, C, W[32]);   F2(C, D, E, A, B, W[33]);
      F2(B, C, D, E, A, W[34]);   F2(A, B, C, D, E, W[35]);
      F2(E, A, B, C, D, W[36]);   F2(D, E, A, B, C, W[37]);
      F2(C, D, E, A, B, W[38]);   F2(B, C, D, E, A, W[39]);

      F3(A, B, C, D, E, W[40]);   F3(E, A, B, C, D, W[41]);
      F3(D, E, A, B, C, W[42]);   F3(C, D, E, A, B, W[43]);
      F3(B, C, D, E, A, W[44]);   F3(A, B, C, D, E, W[45]);
      F3(E, A, B, C, D, W[46]);   F3(D, E, A, B, C, W[47]);
      F3(C, D, E, A, B, W[48]);   F3(B, C, D, E, A, W[49]);
      F3(A, B, C, D, E, W[50]);   F3(E, A, B, C, D, W[51]);
      F3(D, E, A, B, C, W[52]);   F3(C, D, E, A, B, W[53]);
      F3(B, C, D, E, A, W[54]);   F3(A, B, C, D, E, W[55]);
      F3(E, A, B, C, D, W[56]);   F3(D, E, A, B, C, W[57]);
      F3(C, D, E, A, B, W[58]);   F3(B, C, D, E, A, W[59]);

      F4(A, B, C, D, E, W[60]);   F4(E, A, B, C, D, W[61]);
      F4(D, E, A, B, C, W[62]);   F4(C, D, E, A, B, W[63]);
      F4(B, C, D, E, A, W[64]);   F4(A, B, C, D, E, W[65]);
      F4(E, A, B, C, D, W[66]);   F4(D, E, A, B, C, W[67]);
      F4(C, D, E, A, B, W[68]);   F4(B, C, D, E, A, W[69]);
      F4(A, B, C, D, E, W[70]);   F4(E, A, B, C, D, W[71]);
      F4(D, E, A, B, C, W[72]);   F4(C, D, E, A, B, W[73]);
      F4(B, C, D, E, A, W[74]);   F4(A, B, C, D, E, W[75]);
      F4(E, A, B, C, D, W[76]);   F4(D, E, A, B, C, W[77]);
      F4(C, D, E, A, B, W[78]);   F4(B, C, D, E, A, W[79]);

      A = (digest[0] += A);
      B = (digest[1] += B);
      C = (digest[2] += C);
      D = (digest[3] += D);
      E = (digest[4] += E);

      input += hash_block_size();
      }
   }
void Botan::SHA_160::copy_out ( byte  buffer[]) [protected, virtual]

Copy the output to the buffer

Parameters:
bufferto put the output into

Implements Botan::MDx_HashFunction.

Definition at line 142 of file sha160.cpp.

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

void Botan::Buffered_Computation::final ( byte  out[]) [inline, inherited]

Complete the computation and retrieve the final result.

Parameters:
outThe byte array to be filled with the result. Must be of length output_length()

Definition at line 90 of file buf_comp.h.

Referenced by botan_hash_final(), botan_mac_final(), Botan::McEliece_KEM_Decryptor::decrypt(), Botan::TLS::Session::decrypt(), Botan::McEliece_KEM_Encryptor::encrypt(), Botan::TLS::Session::encrypt(), Botan::mgf1_mask(), Botan::pbkdf2(), and Botan::TLS::write_record().

{ final_result(out); }
secure_vector<byte> Botan::Buffered_Computation::final ( ) [inline, inherited]

Complete the computation and retrieve the final result.

Returns:
secure_vector holding the result

Definition at line 97 of file buf_comp.h.

         {
         secure_vector<byte> output(output_length());
         final_result(&output[0]);
         return output;
         }
template<typename Alloc >
void Botan::Buffered_Computation::final ( std::vector< byte, Alloc > &  out) [inline, inherited]

Definition at line 105 of file buf_comp.h.

         {
         out.resize(output_length());
         final_result(&out[0]);
         }
void Botan::MDx_HashFunction::final_result ( byte  out[]) [protected, virtual, inherited]

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

   {
   buffer[position] = (BIG_BIT_ENDIAN ? 0x80 : 0x01);
   for(size_t i = position+1; i != buffer.size(); ++i)
      buffer[i] = 0;

   if(position >= buffer.size() - COUNT_SIZE)
      {
      compress_n(&buffer[0], 1);
      zeroise(buffer);
      }

   write_count(&buffer[buffer.size() - COUNT_SIZE]);

   compress_n(&buffer[0], 1);
   copy_out(output);
   clear();
   }
size_t Botan::MDx_HashFunction::hash_block_size ( ) const [inline, virtual, inherited]
Returns:
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().

{ return buffer.size(); }
std::string Botan::SHA_160::name ( ) const [inline, virtual]

Implements Botan::HashFunction.

Definition at line 21 of file sha160.h.

{ return "SHA-160"; }
size_t Botan::SHA_160::output_length ( ) const [inline, virtual]
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().

{ return 20; }
secure_vector<byte> Botan::Buffered_Computation::process ( const byte  in[],
size_t  length 
) [inline, inherited]

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 118 of file buf_comp.h.

Referenced by Botan::HMAC_RNG::HMAC_RNG(), Botan::RTSS_Share::split(), and Botan::Cert_Extension::Subject_Key_ID::Subject_Key_ID().

         {
         add_data(in, length);
         return final();
         }
secure_vector<byte> Botan::Buffered_Computation::process ( const secure_vector< byte > &  in) [inline, inherited]

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 130 of file buf_comp.h.

         {
         add_data(&in[0], in.size());
         return final();
         }
secure_vector<byte> Botan::Buffered_Computation::process ( const std::vector< byte > &  in) [inline, inherited]

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 142 of file buf_comp.h.

         {
         add_data(&in[0], in.size());
         return final();
         }
secure_vector<byte> Botan::Buffered_Computation::process ( const std::string &  in) [inline, inherited]

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 154 of file buf_comp.h.

         {
         update(in);
         return final();
         }
void Botan::Buffered_Computation::update ( const byte  in[],
size_t  length 
) [inline, inherited]

Add new input to process.

Parameters:
inthe input to process as a byte array
lengthof param in in bytes

Definition at line 34 of file buf_comp.h.

Referenced by botan_hash_update(), botan_mac_update(), Botan::McEliece_KEM_Decryptor::decrypt(), Botan::TLS::Session::decrypt(), Botan::McEliece_KEM_Encryptor::encrypt(), Botan::TLS::Session::encrypt(), Botan::mgf1_mask(), Botan::pbkdf2(), and Botan::TLS::write_record().

{ add_data(in, length); }
void Botan::Buffered_Computation::update ( const secure_vector< byte > &  in) [inline, inherited]

Add new input to process.

Parameters:
inthe input to process as a secure_vector

Definition at line 40 of file buf_comp.h.

         {
         add_data(&in[0], in.size());
         }
void Botan::Buffered_Computation::update ( const std::vector< byte > &  in) [inline, inherited]

Add new input to process.

Parameters:
inthe input to process as a std::vector

Definition at line 49 of file buf_comp.h.

         {
         add_data(&in[0], in.size());
         }
void Botan::Buffered_Computation::update ( const std::string &  str) [inline, inherited]

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 73 of file buf_comp.h.

         {
         add_data(reinterpret_cast<const byte*>(str.data()), str.size());
         }
void Botan::Buffered_Computation::update ( byte  in) [inline, inherited]

Process a single byte.

Parameters:
inthe byte to process

Definition at line 82 of file buf_comp.h.

{ add_data(&in, 1); }
template<typename T >
void Botan::Buffered_Computation::update_be ( const T  in) [inline, inherited]

Add an integer in big-endian order

Parameters:
inthe value

Definition at line 58 of file buf_comp.h.

References Botan::get_byte().

Referenced by Botan::mgf1_mask(), and Botan::pbkdf2().

         {
         for(size_t i = 0; i != sizeof(T); ++i)
            {
            byte b = get_byte(i, in);
            add_data(&b, 1);
            }
         }
void Botan::MDx_HashFunction::write_count ( byte  out[]) [protected, virtual, inherited]

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().

   {
   if(COUNT_SIZE < 8)
      throw Invalid_State("MDx_HashFunction::write_count: COUNT_SIZE < 8");
   if(COUNT_SIZE >= output_length() || COUNT_SIZE >= hash_block_size())
      throw Invalid_Argument("MDx_HashFunction: COUNT_SIZE is too big");

   const u64bit bit_count = count * 8;

   if(BIG_BYTE_ENDIAN)
      store_be(bit_count, out + COUNT_SIZE - 8);
   else
      store_le(bit_count, out + COUNT_SIZE - 8);
   }

Member Data Documentation

secure_vector<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().

secure_vector<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: