Botan  1.11.10
Public Member Functions | Protected Member Functions | List of all members
Botan::SHA_256 Class Reference

#include <sha2_32.h>

Inheritance diagram for Botan::SHA_256:
Botan::MDx_HashFunction Botan::HashFunction Botan::Buffered_Computation

Public Member Functions

void clear ()
 
HashFunctionclone () const
 
void final (byte out[])
 
secure_vector< bytefinal ()
 
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_256 ()
 
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 final_result (byte output[])
 
virtual void write_count (byte out[])
 

Detailed Description

SHA-256

Definition at line 40 of file sha2_32.h.

Constructor & Destructor Documentation

Botan::SHA_256::SHA_256 ( )
inline

Definition at line 49 of file sha2_32.h.

49  : MDx_HashFunction(64, true, true), digest(8)
50  { clear(); }
void clear()
Definition: sha2_32.cpp:214
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::buffer_insert(), and Botan::MDx_HashFunction::compress_n().

42  {
43  count += length;
44 
45  if(position)
46  {
47  buffer_insert(buffer, 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_insert(buffer, position, input + full_blocks * buffer.size(), remaining);
65  position += remaining;
66  }
virtual void compress_n(const byte blocks[], size_t block_n)=0
size_t buffer_insert(std::vector< T, Alloc > &buf, size_t buf_offset, const T input[], size_t input_length)
Definition: secmem.h:103
void Botan::SHA_256::clear ( )
virtual

Reimplemented from Botan::MDx_HashFunction.

Definition at line 214 of file sha2_32.cpp.

References Botan::MDx_HashFunction::clear().

215  {
217  digest[0] = 0x6A09E667;
218  digest[1] = 0xBB67AE85;
219  digest[2] = 0x3C6EF372;
220  digest[3] = 0xA54FF53A;
221  digest[4] = 0x510E527F;
222  digest[5] = 0x9B05688C;
223  digest[6] = 0x1F83D9AB;
224  digest[7] = 0x5BE0CD19;
225  }
HashFunction* Botan::SHA_256::clone ( ) const
inlinevirtual
Returns
new object representing the same algorithm as *this

Implements Botan::HashFunction.

Definition at line 45 of file sha2_32.h.

45 { return new SHA_256; }
void Botan::Buffered_Computation::final ( byte  out[])
inlineinherited

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::X942_PRF::derive(), Botan::Hash_Filter::end_msg(), Botan::MAC_Filter::end_msg(), Botan::mgf1_mask(), Botan::SIV_Mode::S2V(), and Botan::TLS::write_record().

90 { final_result(out); }
secure_vector<byte> Botan::Buffered_Computation::final ( )
inlineinherited

Complete the computation and retrieve the final result.

Returns
secure_vector holding the result

Definition at line 97 of file buf_comp.h.

98  {
99  secure_vector<byte> output(output_length());
100  final_result(&output[0]);
101  return output;
102  }
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::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
virtual void copy_out(byte buffer[])=0
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:166
size_t Botan::MDx_HashFunction::hash_block_size ( ) const
inlinevirtualinherited
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(), Botan::SHA_160::compress_n(), and Botan::MDx_HashFunction::write_count().

32 { return buffer.size(); }
std::string Botan::SHA_256::name ( ) const
inlinevirtual

Implements Botan::HashFunction.

Definition at line 43 of file sha2_32.h.

Referenced by Botan::RTSS_Share::split().

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

Implements Botan::Buffered_Computation.

Definition at line 44 of file sha2_32.h.

44 { return 32; }
secure_vector<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 111 of file buf_comp.h.

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

112  {
113  add_data(in, length);
114  return final();
115  }
secure_vector<byte> Botan::Buffered_Computation::process ( const secure_vector< 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 123 of file buf_comp.h.

124  {
125  add_data(&in[0], in.size());
126  return final();
127  }
secure_vector<byte> Botan::Buffered_Computation::process ( const std::vector< 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 135 of file buf_comp.h.

136  {
137  add_data(&in[0], in.size());
138  return final();
139  }
secure_vector<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 147 of file buf_comp.h.

References Botan::TLS::update().

148  {
149  update(in);
150  return final();
151  }
void update(const byte in[], size_t length)
Definition: buf_comp.h:34
void Botan::Buffered_Computation::update ( const byte  in[],
size_t  length 
)
inlineinherited

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::X942_PRF::derive(), Botan::mgf1_mask(), Botan::SIV_Mode::S2V(), and Botan::TLS::write_record().

34 { add_data(in, length); }
void Botan::Buffered_Computation::update ( const secure_vector< byte > &  in)
inlineinherited

Add new input to process.

Parameters
inthe input to process as a secure_vector

Definition at line 40 of file buf_comp.h.

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

Add new input to process.

Parameters
inthe input to process as a std::vector

Definition at line 49 of file buf_comp.h.

50  {
51  add_data(&in[0], in.size());
52  }
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 73 of file buf_comp.h.

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

Process a single byte.

Parameters
inthe byte to process

Definition at line 82 of file buf_comp.h.

82 { 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 58 of file buf_comp.h.

References Botan::get_byte().

Referenced by Botan::mgf1_mask().

59  {
60  for(size_t i = 0; i != sizeof(T); ++i)
61  {
62  byte b = get_byte(i, in);
63  add_data(&b, 1);
64  }
65  }
byte get_byte(size_t byte_num, T input)
Definition: get_byte.h:21
uint8_t byte
Definition: types.h:30
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
void store_be(u16bit in, byte out[2])
Definition: loadstor.h:412
uint64_t u64bit
Definition: types.h:33
virtual size_t output_length() const =0

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