Botan  1.11.34
Crypto and TLS for C++11
Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
Botan::SHA_512 Class Referencefinal

#include <sha2_64.h>

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

Public Member Functions

void clear () override
 
HashFunctionclone () const override
 
void final (byte out[])
 
secure_vector< bytefinal ()
 
template<typename Alloc >
void final (std::vector< byte, Alloc > &out)
 
std::vector< bytefinal_stdvec ()
 
size_t hash_block_size () const override
 
std::string name () const override
 
size_t output_length () const override
 
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)
 
virtual std::string provider () const
 
 SHA_512 ()
 
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)
 

Static Public Member Functions

static std::unique_ptr< HashFunctioncreate (const std::string &algo_spec, const std::string &provider="")
 
static std::unique_ptr< HashFunctioncreate_or_throw (const std::string &algo_spec, const std::string &provider="")
 
static std::vector< std::string > providers (const std::string &algo_spec)
 

Protected Member Functions

void add_data (const byte input[], size_t length) override
 
void final_result (byte output[]) override
 
virtual void write_count (byte out[])
 

Detailed Description

SHA-512

Definition at line 39 of file sha2_64.h.

Constructor & Destructor Documentation

§ SHA_512()

Botan::SHA_512::SHA_512 ( )
inline

Definition at line 48 of file sha2_64.h.

48  : MDx_HashFunction(128, true, true, 16), m_digest(8)
49  { clear(); }
void clear() override
Definition: sha2_64.cpp:230
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

§ add_data()

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

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  m_count += length;
44 
45  if(m_position)
46  {
47  buffer_insert(m_buffer, m_position, input, length);
48 
49  if(m_position + length >= m_buffer.size())
50  {
51  compress_n(m_buffer.data(), 1);
52  input += (m_buffer.size() - m_position);
53  length -= (m_buffer.size() - m_position);
54  m_position = 0;
55  }
56  }
57 
58  const size_t full_blocks = length / m_buffer.size();
59  const size_t remaining = length % m_buffer.size();
60 
61  if(full_blocks)
62  compress_n(input, full_blocks);
63 
64  buffer_insert(m_buffer, m_position, input + full_blocks * m_buffer.size(), remaining);
65  m_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:125

§ clear()

void Botan::SHA_512::clear ( )
overridevirtual

Reset the state.

Reimplemented from Botan::MDx_HashFunction.

Definition at line 230 of file sha2_64.cpp.

References Botan::MDx_HashFunction::clear().

231  {
233  m_digest[0] = 0x6A09E667F3BCC908;
234  m_digest[1] = 0xBB67AE8584CAA73B;
235  m_digest[2] = 0x3C6EF372FE94F82B;
236  m_digest[3] = 0xA54FF53A5F1D36F1;
237  m_digest[4] = 0x510E527FADE682D1;
238  m_digest[5] = 0x9B05688C2B3E6C1F;
239  m_digest[6] = 0x1F83D9ABFB41BD6B;
240  m_digest[7] = 0x5BE0CD19137E2179;
241  }
void clear() override
Definition: mdx_hash.cpp:32

§ clone()

HashFunction* Botan::SHA_512::clone ( ) const
inlineoverridevirtual
Returns
new object representing the same algorithm as *this

Implements Botan::HashFunction.

Definition at line 44 of file sha2_64.h.

44 { return new SHA_512; }

§ create()

std::unique_ptr< HashFunction > Botan::HashFunction::create ( const std::string &  algo_spec,
const std::string &  provider = "" 
)
staticinherited

Create an instance based on a name, or return null if the algo/provider combination cannot be found. If provider is empty then best available is chosen.

Definition at line 93 of file hash.cpp.

References hash, and Botan::make_openssl_hash().

Referenced by botan_hash_init(), botan_pubkey_fingerprint(), Botan::OCSP::CertID::CertID(), Botan::BlockCipher::create(), Botan::MessageAuthenticationCode::create(), Botan::PBKDF::create(), Botan::KDF::create(), Botan::HashFunction::create_or_throw(), Botan::Private_Key::fingerprint(), Botan::X509_Certificate::fingerprint(), Botan::get_eme(), Botan::get_emsa(), Botan::get_hash(), Botan::get_hash_function(), Botan::OCSP::CertID::is_id_for(), Botan::X942_PRF::kdf(), Botan::newhope_shareda(), Botan::newhope_sharedb(), and Botan::X509_Certificate::subject_public_key_bitstring_sha1().

95  {
96 #if defined(BOTAN_HAS_OPENSSL)
97  if(provider.empty() || provider == "openssl")
98  {
99  if(auto hash = make_openssl_hash(algo_spec))
100  return hash;
101 
102  if(!provider.empty())
103  return nullptr;
104  }
105 #endif
106 
107  // TODO: CommonCrypto hashes
108 
109  if(provider.empty() == false && provider != "base")
110  return nullptr; // unknown provider
111 
112 #if defined(BOTAN_HAS_SHA1)
113  if(algo_spec == "SHA-160" ||
114  algo_spec == "SHA-1" ||
115  algo_spec == "SHA1")
116  {
117  return std::unique_ptr<HashFunction>(new SHA_160);
118  }
119 #endif
120 
121 #if defined(BOTAN_HAS_SHA2_32)
122  if(algo_spec == "SHA-224")
123  {
124  return std::unique_ptr<HashFunction>(new SHA_224);
125  }
126 
127  if(algo_spec == "SHA-256")
128  {
129  return std::unique_ptr<HashFunction>(new SHA_256);
130  }
131 #endif
132 
133 #if defined(BOTAN_HAS_SHA2_64)
134  if(algo_spec == "SHA-384")
135  {
136  return std::unique_ptr<HashFunction>(new SHA_384);
137  }
138 
139  if(algo_spec == "SHA-512")
140  {
141  return std::unique_ptr<HashFunction>(new SHA_512);
142  }
143 
144  if(algo_spec == "SHA-512-256")
145  {
146  return std::unique_ptr<HashFunction>(new SHA_512_256);
147  }
148 #endif
149 
150 #if defined(BOTAN_HAS_RIPEMD_160)
151  if(algo_spec == "RIPEMD-160")
152  {
153  return std::unique_ptr<HashFunction>(new RIPEMD_160);
154  }
155 #endif
156 
157 #if defined(BOTAN_HAS_WHIRLPOOL)
158  if(algo_spec == "Whirlpool")
159  {
160  return std::unique_ptr<HashFunction>(new Whirlpool);
161  }
162 #endif
163 
164 #if defined(BOTAN_HAS_MD5)
165  if(algo_spec == "MD5")
166  {
167  return std::unique_ptr<HashFunction>(new MD5);
168  }
169 #endif
170 
171 #if defined(BOTAN_HAS_MD4)
172  if(algo_spec == "MD4")
173  {
174  return std::unique_ptr<HashFunction>(new MD4);
175  }
176 #endif
177 
178 #if defined(BOTAN_HAS_GOST_34_11)
179  if(algo_spec == "GOST-R-34.11-94" || algo_spec == "GOST-34.11")
180  {
181  return std::unique_ptr<HashFunction>(new GOST_34_11);
182  }
183 #endif
184 
185 #if defined(BOTAN_HAS_ADLER32)
186  if(algo_spec == "Adler32")
187  {
188  return std::unique_ptr<HashFunction>(new Adler32);
189  }
190 #endif
191 
192 #if defined(BOTAN_HAS_CRC24)
193  if(algo_spec == "CRC24")
194  {
195  return std::unique_ptr<HashFunction>(new CRC24);
196  }
197 #endif
198 
199 #if defined(BOTAN_HAS_CRC32)
200  if(algo_spec == "CRC32")
201  {
202  return std::unique_ptr<HashFunction>(new CRC32);
203  }
204 #endif
205 
206  const SCAN_Name req(algo_spec);
207 
208 #if defined(BOTAN_HAS_TIGER)
209  if(req.algo_name() == "Tiger")
210  {
211  return std::unique_ptr<HashFunction>(
212  new Tiger(req.arg_as_integer(0, 24),
213  req.arg_as_integer(1, 3)));
214  }
215 #endif
216 
217 #if defined(BOTAN_HAS_SKEIN_512)
218  if(req.algo_name() == "Skein-512")
219  {
220  return std::unique_ptr<HashFunction>(
221  new Skein_512(req.arg_as_integer(0, 512), req.arg(1, "")));
222  }
223 #endif
224 
225 #if defined(BOTAN_HAS_BLAKE2B)
226  if(req.algo_name() == "Blake2b")
227  {
228  return std::unique_ptr<HashFunction>(
229  new Blake2b(req.arg_as_integer(0, 512)));
230  }
231 #endif
232 
233 #if defined(BOTAN_HAS_KECCAK)
234  if(req.algo_name() == "Keccak-1600")
235  {
236  return std::unique_ptr<HashFunction>(
237  new Keccak_1600(req.arg_as_integer(0, 512)));
238  }
239 #endif
240 
241 #if defined(BOTAN_HAS_SHA3)
242  if(req.algo_name() == "SHA-3")
243  {
244  return std::unique_ptr<HashFunction>(
245  new SHA_3(req.arg_as_integer(0, 512)));
246  }
247 #endif
248 
249 #if defined(BOTAN_HAS_SHAKE)
250  if(req.algo_name() == "SHAKE-128")
251  {
252  return std::unique_ptr<HashFunction>(new SHAKE_128(req.arg_as_integer(0, 128)));
253  }
254  if(req.algo_name() == "SHAKE-256")
255  {
256  return std::unique_ptr<HashFunction>(new SHAKE_256(req.arg_as_integer(0, 256)));
257  }
258 #endif
259 
260 #if defined(BOTAN_HAS_WHIRLPOOL)
261  if(req.algo_name() == "Whirlpool")
262  {
263  return std::unique_ptr<HashFunction>(new Whirlpool);
264  }
265 #endif
266 
267 #if defined(BOTAN_HAS_PARALLEL_HASH)
268  if(req.algo_name() == "Parallel")
269  {
270  std::vector<std::unique_ptr<HashFunction>> hashes;
271 
272  for(size_t i = 0; i != req.arg_count(); ++i)
273  {
274  auto h = HashFunction::create(req.arg(i));
275  if(!h)
276  {
277  return nullptr;
278  }
279  hashes.push_back(std::move(h));
280  }
281 
282  return std::unique_ptr<HashFunction>(new Parallel(hashes));
283  }
284 #endif
285 
286 #if defined(BOTAN_HAS_COMB4P)
287  if(req.algo_name() == "Comb4P" && req.arg_count() == 2)
288  {
289  std::unique_ptr<HashFunction> h1(HashFunction::create(req.arg(0)));
290  std::unique_ptr<HashFunction> h2(HashFunction::create(req.arg(1)));
291 
292  if(h1 && h2)
293  return std::unique_ptr<HashFunction>(new Comb4P(h1.release(), h2.release()));
294  }
295 #endif
296 
297 
298  return nullptr;
299  }
virtual std::string provider() const
Definition: hash.h:57
std::unique_ptr< HashFunction > make_openssl_hash(const std::string &name)
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:93
MechanismType hash

§ create_or_throw()

std::unique_ptr< HashFunction > Botan::HashFunction::create_or_throw ( const std::string &  algo_spec,
const std::string &  provider = "" 
)
staticinherited

Create an instance based on a name If provider is empty then best available is chosen.

Parameters
algo_specalgorithm name
providerprovider implementation to use Throws Lookup_Error if not not found.

Definition at line 303 of file hash.cpp.

References Botan::HashFunction::create(), and hash.

Referenced by Botan::choose_sig_format(), Botan::TLS::Handshake_Hash::final(), Botan::generate_dsa_primes(), and Botan::make_hash_function().

305  {
306  if(auto hash = HashFunction::create(algo, provider))
307  {
308  return hash;
309  }
310  throw Lookup_Error("Hash", algo, provider);
311  }
virtual std::string provider() const
Definition: hash.h:57
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:93
MechanismType hash

§ final() [1/3]

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_hash_final(), botan_mac_final(), Botan::TLS::TLS_CBC_HMAC_AEAD_Encryption::finish(), Botan::TLS::TLS_CBC_HMAC_AEAD_Decryption::finish(), Botan::mgf1_mask(), Botan::pbkdf2(), and Botan::TLS_PRF::TLS_PRF().

90 { final_result(out); }

§ final() [2/3]

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.data());
101  return output;
102  }
virtual size_t output_length() const =0

§ final() [3/3]

template<typename Alloc >
void Botan::Buffered_Computation::final ( std::vector< byte, Alloc > &  out)
inlineinherited

Definition at line 112 of file buf_comp.h.

113  {
114  out.resize(output_length());
115  final_result(out.data());
116  }
virtual size_t output_length() const =0

§ final_result()

void Botan::MDx_HashFunction::final_result ( byte  out[])
overrideprotectedvirtualinherited

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  m_buffer[m_position] = (BIG_BIT_ENDIAN ? 0x80 : 0x01);
74  for(size_t i = m_position+1; i != m_buffer.size(); ++i)
75  m_buffer[i] = 0;
76 
77  if(m_position >= m_buffer.size() - COUNT_SIZE)
78  {
79  compress_n(m_buffer.data(), 1);
80  zeroise(m_buffer);
81  }
82 
83  write_count(&m_buffer[m_buffer.size() - COUNT_SIZE]);
84 
85  compress_n(m_buffer.data(), 1);
86  copy_out(output);
87  clear();
88  }
void clear() override
Definition: mdx_hash.cpp:32
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:203

§ final_stdvec()

std::vector<byte> Botan::Buffered_Computation::final_stdvec ( )
inlineinherited

Definition at line 104 of file buf_comp.h.

105  {
106  std::vector<byte> output(output_length());
107  final_result(output.data());
108  return output;
109  }
virtual size_t output_length() const =0

§ hash_block_size()

size_t Botan::MDx_HashFunction::hash_block_size ( ) const
inlineoverridevirtualinherited
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(), and Botan::MDx_HashFunction::write_count().

32 { return m_buffer.size(); }

§ name()

std::string Botan::SHA_512::name ( ) const
inlineoverridevirtual
Returns
the hash function name

Implements Botan::HashFunction.

Definition at line 42 of file sha2_64.h.

42 { return "SHA-512"; }

§ output_length()

size_t Botan::SHA_512::output_length ( ) const
inlineoverridevirtual
Returns
length of the output of this function in bytes

Implements Botan::Buffered_Computation.

Definition at line 43 of file sha2_64.h.

43 { return 64; }

§ process() [1/4]

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

Referenced by Botan::RTSS_Share::split(), and Botan::TLS_PRF::TLS_PRF().

126  {
127  add_data(in, length);
128  return final();
129  }

§ process() [2/4]

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

138  {
139  add_data(in.data(), in.size());
140  return final();
141  }

§ process() [3/4]

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

150  {
151  add_data(in.data(), in.size());
152  return final();
153  }

§ process() [4/4]

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

162  {
163  update(in);
164  return final();
165  }
void update(const byte in[], size_t length)
Definition: buf_comp.h:34

§ provider()

virtual std::string Botan::HashFunction::provider ( ) const
inlinevirtualinherited
Returns
provider information about this implementation. Default is "base", might also return "sse2", "avx2", "openssl", or some other arbitrary string.

Definition at line 57 of file hash.h.

57 { return "base"; }

§ providers()

std::vector< std::string > Botan::HashFunction::providers ( const std::string &  algo_spec)
staticinherited
Returns
list of available providers for this algorithm, empty if not available
Parameters
algo_specalgorithm name

Definition at line 313 of file hash.cpp.

Referenced by Botan::TLS::Ciphersuite::by_id(), and Botan::get_hash_function_providers().

314  {
315  return probe_providers_of<HashFunction>(algo_spec, {"base", "openssl"});
316  }

§ update() [1/5]

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_hash_update(), botan_mac_update(), Botan::TLS::TLS_CBC_HMAC_AEAD_Encryption::finish(), Botan::TLS::TLS_CBC_HMAC_AEAD_Decryption::finish(), Botan::mgf1_mask(), Botan::TLS::TLS_CBC_HMAC_AEAD_Decryption::output_length(), Botan::pbkdf2(), and Botan::TLS_PRF::TLS_PRF().

34 { add_data(in, length); }

§ update() [2/5]

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.data(), in.size());
43  }

§ update() [3/5]

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.data(), in.size());
52  }

§ update() [4/5]

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  }

§ update() [5/5]

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); }

§ update_be()

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(), and Botan::pbkdf2().

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: loadstor.h:47
std::uint8_t byte
Definition: types.h:31

§ write_count()

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 = m_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  }
void store_le(u16bit in, byte out[2])
Definition: loadstor.h:457
size_t hash_block_size() const override
Definition: mdx_hash.h:32
std::uint64_t u64bit
Definition: types.h:34
void store_be(u16bit in, byte out[2])
Definition: loadstor.h:441
virtual size_t output_length() const =0

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