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

#include <sha2_32.h>

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

Public Member Functions

void clear () override
 
HashFunctionclone () const override
 
std::unique_ptr< HashFunctioncopy_state () const override
 
void final (uint8_t out[])
 
secure_vector< uint8_t > final ()
 
template<typename Alloc >
void final (std::vector< uint8_t, Alloc > &out)
 
std::vector< uint8_t > final_stdvec ()
 
size_t hash_block_size () const override final
 
std::string name () const override
 
size_t output_length () const override
 
secure_vector< uint8_t > process (const uint8_t in[], size_t length)
 
secure_vector< uint8_t > process (const secure_vector< uint8_t > &in)
 
secure_vector< uint8_t > process (const std::vector< uint8_t > &in)
 
secure_vector< uint8_t > process (const std::string &in)
 
virtual std::string provider () const
 
 SHA_256 ()
 
void update (const uint8_t in[], size_t length)
 
void update (const secure_vector< uint8_t > &in)
 
void update (const std::vector< uint8_t > &in)
 
void update (const std::string &str)
 
void update (uint8_t in)
 
template<typename T >
void update_be (const T in)
 

Static Public Member Functions

static void compress_digest (secure_vector< uint32_t > &digest, const uint8_t input[], size_t blocks)
 
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 uint8_t input[], size_t length) override final
 
void final_result (uint8_t output[]) override final
 
virtual void write_count (uint8_t out[])
 

Detailed Description

SHA-256

Definition at line 41 of file sha2_32.h.

Constructor & Destructor Documentation

◆ SHA_256()

Botan::SHA_256::SHA_256 ( )
inline

Definition at line 51 of file sha2_32.h.

Referenced by copy_state().

51  : MDx_HashFunction(64, true, true), m_digest(8)
52  { clear(); }
void clear() override
Definition: sha2_32.cpp:223
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 uint8_t  input[],
size_t  length 
)
finaloverrideprotectedvirtualinherited

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 42 of file mdx_hash.cpp.

References Botan::buffer_insert(), and Botan::MDx_HashFunction::compress_n().

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

◆ clear()

void Botan::SHA_256::clear ( )
overridevirtual

Reset the state.

Reimplemented from Botan::MDx_HashFunction.

Definition at line 223 of file sha2_32.cpp.

References Botan::MDx_HashFunction::clear().

224  {
226  m_digest[0] = 0x6A09E667;
227  m_digest[1] = 0xBB67AE85;
228  m_digest[2] = 0x3C6EF372;
229  m_digest[3] = 0xA54FF53A;
230  m_digest[4] = 0x510E527F;
231  m_digest[5] = 0x9B05688C;
232  m_digest[6] = 0x1F83D9AB;
233  m_digest[7] = 0x5BE0CD19;
234  }
void clear() override
Definition: mdx_hash.cpp:33

◆ clone()

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

Implements Botan::HashFunction.

Definition at line 46 of file sha2_32.h.

46 { return new SHA_256; }

◆ compress_digest()

void Botan::SHA_256::compress_digest ( secure_vector< uint32_t > &  digest,
const uint8_t  input[],
size_t  blocks 
)
static

Definition at line 44 of file sha2_32.cpp.

References Botan::load_be< uint32_t >(), and SHA2_32_F.

46  {
47 #if defined(BOTAN_HAS_SHA2_32_X86)
48  if(CPUID::has_intel_sha())
49  {
50  return SHA_256::compress_digest_x86(digest, input, blocks);
51  }
52 #endif
53 
54 #if defined(BOTAN_HAS_SHA2_32_X86_BMI2)
55  if(CPUID::has_bmi2())
56  {
57  return SHA_256::compress_digest_x86_bmi2(digest, input, blocks);
58  }
59 #endif
60 
61 #if defined(BOTAN_HAS_SHA2_32_ARMV8)
62  if(CPUID::has_arm_sha2())
63  {
64  return SHA_256::compress_digest_armv8(digest, input, blocks);
65  }
66 #endif
67 
68  uint32_t A = digest[0], B = digest[1], C = digest[2],
69  D = digest[3], E = digest[4], F = digest[5],
70  G = digest[6], H = digest[7];
71 
72  for(size_t i = 0; i != blocks; ++i)
73  {
74  uint32_t W00 = load_be<uint32_t>(input, 0);
75  uint32_t W01 = load_be<uint32_t>(input, 1);
76  uint32_t W02 = load_be<uint32_t>(input, 2);
77  uint32_t W03 = load_be<uint32_t>(input, 3);
78  uint32_t W04 = load_be<uint32_t>(input, 4);
79  uint32_t W05 = load_be<uint32_t>(input, 5);
80  uint32_t W06 = load_be<uint32_t>(input, 6);
81  uint32_t W07 = load_be<uint32_t>(input, 7);
82  uint32_t W08 = load_be<uint32_t>(input, 8);
83  uint32_t W09 = load_be<uint32_t>(input, 9);
84  uint32_t W10 = load_be<uint32_t>(input, 10);
85  uint32_t W11 = load_be<uint32_t>(input, 11);
86  uint32_t W12 = load_be<uint32_t>(input, 12);
87  uint32_t W13 = load_be<uint32_t>(input, 13);
88  uint32_t W14 = load_be<uint32_t>(input, 14);
89  uint32_t W15 = load_be<uint32_t>(input, 15);
90 
91  SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x428A2F98);
92  SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x71374491);
93  SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0xB5C0FBCF);
94  SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0xE9B5DBA5);
95  SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x3956C25B);
96  SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x59F111F1);
97  SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x923F82A4);
98  SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0xAB1C5ED5);
99  SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xD807AA98);
100  SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x12835B01);
101  SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x243185BE);
102  SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x550C7DC3);
103  SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x72BE5D74);
104  SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0x80DEB1FE);
105  SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x9BDC06A7);
106  SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC19BF174);
107 
108  SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0xE49B69C1);
109  SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0xEFBE4786);
110  SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x0FC19DC6);
111  SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x240CA1CC);
112  SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x2DE92C6F);
113  SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4A7484AA);
114  SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5CB0A9DC);
115  SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x76F988DA);
116  SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x983E5152);
117  SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA831C66D);
118  SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xB00327C8);
119  SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xBF597FC7);
120  SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xC6E00BF3);
121  SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD5A79147);
122  SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0x06CA6351);
123  SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x14292967);
124 
125  SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x27B70A85);
126  SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x2E1B2138);
127  SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x4D2C6DFC);
128  SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x53380D13);
129  SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x650A7354);
130  SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x766A0ABB);
131  SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x81C2C92E);
132  SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x92722C85);
133  SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0xA2BFE8A1);
134  SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0xA81A664B);
135  SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0xC24B8B70);
136  SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0xC76C51A3);
137  SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0xD192E819);
138  SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xD6990624);
139  SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xF40E3585);
140  SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0x106AA070);
141 
142  SHA2_32_F(A, B, C, D, E, F, G, H, W00, W14, W09, W01, 0x19A4C116);
143  SHA2_32_F(H, A, B, C, D, E, F, G, W01, W15, W10, W02, 0x1E376C08);
144  SHA2_32_F(G, H, A, B, C, D, E, F, W02, W00, W11, W03, 0x2748774C);
145  SHA2_32_F(F, G, H, A, B, C, D, E, W03, W01, W12, W04, 0x34B0BCB5);
146  SHA2_32_F(E, F, G, H, A, B, C, D, W04, W02, W13, W05, 0x391C0CB3);
147  SHA2_32_F(D, E, F, G, H, A, B, C, W05, W03, W14, W06, 0x4ED8AA4A);
148  SHA2_32_F(C, D, E, F, G, H, A, B, W06, W04, W15, W07, 0x5B9CCA4F);
149  SHA2_32_F(B, C, D, E, F, G, H, A, W07, W05, W00, W08, 0x682E6FF3);
150  SHA2_32_F(A, B, C, D, E, F, G, H, W08, W06, W01, W09, 0x748F82EE);
151  SHA2_32_F(H, A, B, C, D, E, F, G, W09, W07, W02, W10, 0x78A5636F);
152  SHA2_32_F(G, H, A, B, C, D, E, F, W10, W08, W03, W11, 0x84C87814);
153  SHA2_32_F(F, G, H, A, B, C, D, E, W11, W09, W04, W12, 0x8CC70208);
154  SHA2_32_F(E, F, G, H, A, B, C, D, W12, W10, W05, W13, 0x90BEFFFA);
155  SHA2_32_F(D, E, F, G, H, A, B, C, W13, W11, W06, W14, 0xA4506CEB);
156  SHA2_32_F(C, D, E, F, G, H, A, B, W14, W12, W07, W15, 0xBEF9A3F7);
157  SHA2_32_F(B, C, D, E, F, G, H, A, W15, W13, W08, W00, 0xC67178F2);
158 
159  A = (digest[0] += A);
160  B = (digest[1] += B);
161  C = (digest[2] += C);
162  D = (digest[3] += D);
163  E = (digest[4] += E);
164  F = (digest[5] += F);
165  G = (digest[6] += G);
166  H = (digest[7] += H);
167 
168  input += 64;
169  }
170  }
uint32_t load_be< uint32_t >(const uint8_t in[], size_t off)
Definition: loadstor.h:177
#define SHA2_32_F(A, B, C, D, E, F, G, H, M1, M2, M3, M4, magic)
Definition: sha2_32.cpp:30

◆ copy_state()

std::unique_ptr< HashFunction > Botan::SHA_256::copy_state ( ) const
overridevirtual

Return a new hash object with the same state as *this. This allows computing the hash of several messages with a common prefix more efficiently than would otherwise be possible.

This function should be called clone but that was already used for the case of returning an uninitialized object.

Returns
new hash object

Implements Botan::HashFunction.

Definition at line 19 of file sha2_32.cpp.

References SHA_256().

20  {
21  return std::unique_ptr<HashFunction>(new SHA_256(*this));
22  }

◆ 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 110 of file hash.cpp.

References Botan::SCAN_Name::algo_name(), Botan::SCAN_Name::arg(), Botan::SCAN_Name::arg_as_integer(), Botan::SCAN_Name::arg_count(), hash, Botan::make_bearssl_hash(), Botan::make_commoncrypto_hash(), Botan::make_openssl_hash(), and Botan::HashFunction::provider().

Referenced by botan_hash_init(), botan_pubkey_fingerprint(), Botan::BlockCipher::create(), Botan::KDF::create(), Botan::MessageAuthenticationCode::create(), Botan::PBKDF::create(), Botan::PasswordHashFamily::create(), Botan::HashFunction::create_or_throw(), Botan::Certificate_Store_In_Memory::find_cert_by_pubkey_sha1(), Botan::Certificate_Store_In_Memory::find_cert_by_raw_subject_dn_sha256(), Botan::get_eme(), Botan::get_emsa(), Botan::OCSP::CertID::is_id_for(), Botan::X942_PRF::kdf(), and Botan::X509_Certificate::raw_subject_dn_sha256().

112  {
113 
114 #if defined(BOTAN_HAS_COMMONCRYPTO)
115  if(provider.empty() || provider == "commoncrypto")
116  {
117  if(auto hash = make_commoncrypto_hash(algo_spec))
118  return hash;
119 
120  if(!provider.empty())
121  return nullptr;
122  }
123 #endif
124 
125 #if defined(BOTAN_HAS_OPENSSL)
126  if(provider.empty() || provider == "openssl")
127  {
128  if(auto hash = make_openssl_hash(algo_spec))
129  return hash;
130 
131  if(!provider.empty())
132  return nullptr;
133  }
134 #endif
135 
136 #if defined(BOTAN_HAS_BEARSSL)
137  if(provider.empty() || provider == "bearssl")
138  {
139  if(auto hash = make_bearssl_hash(algo_spec))
140  return hash;
141 
142  if(!provider.empty())
143  return nullptr;
144  }
145 #endif
146 
147  if(provider.empty() == false && provider != "base")
148  return nullptr; // unknown provider
149 
150 #if defined(BOTAN_HAS_SHA1)
151  if(algo_spec == "SHA-160" ||
152  algo_spec == "SHA-1" ||
153  algo_spec == "SHA1")
154  {
155  return std::unique_ptr<HashFunction>(new SHA_160);
156  }
157 #endif
158 
159 #if defined(BOTAN_HAS_SHA2_32)
160  if(algo_spec == "SHA-224")
161  {
162  return std::unique_ptr<HashFunction>(new SHA_224);
163  }
164 
165  if(algo_spec == "SHA-256")
166  {
167  return std::unique_ptr<HashFunction>(new SHA_256);
168  }
169 #endif
170 
171 #if defined(BOTAN_HAS_SHA2_64)
172  if(algo_spec == "SHA-384")
173  {
174  return std::unique_ptr<HashFunction>(new SHA_384);
175  }
176 
177  if(algo_spec == "SHA-512")
178  {
179  return std::unique_ptr<HashFunction>(new SHA_512);
180  }
181 
182  if(algo_spec == "SHA-512-256")
183  {
184  return std::unique_ptr<HashFunction>(new SHA_512_256);
185  }
186 #endif
187 
188 #if defined(BOTAN_HAS_RIPEMD_160)
189  if(algo_spec == "RIPEMD-160")
190  {
191  return std::unique_ptr<HashFunction>(new RIPEMD_160);
192  }
193 #endif
194 
195 #if defined(BOTAN_HAS_WHIRLPOOL)
196  if(algo_spec == "Whirlpool")
197  {
198  return std::unique_ptr<HashFunction>(new Whirlpool);
199  }
200 #endif
201 
202 #if defined(BOTAN_HAS_MD5)
203  if(algo_spec == "MD5")
204  {
205  return std::unique_ptr<HashFunction>(new MD5);
206  }
207 #endif
208 
209 #if defined(BOTAN_HAS_MD4)
210  if(algo_spec == "MD4")
211  {
212  return std::unique_ptr<HashFunction>(new MD4);
213  }
214 #endif
215 
216 #if defined(BOTAN_HAS_GOST_34_11)
217  if(algo_spec == "GOST-R-34.11-94" || algo_spec == "GOST-34.11")
218  {
219  return std::unique_ptr<HashFunction>(new GOST_34_11);
220  }
221 #endif
222 
223 #if defined(BOTAN_HAS_ADLER32)
224  if(algo_spec == "Adler32")
225  {
226  return std::unique_ptr<HashFunction>(new Adler32);
227  }
228 #endif
229 
230 #if defined(BOTAN_HAS_CRC24)
231  if(algo_spec == "CRC24")
232  {
233  return std::unique_ptr<HashFunction>(new CRC24);
234  }
235 #endif
236 
237 #if defined(BOTAN_HAS_CRC32)
238  if(algo_spec == "CRC32")
239  {
240  return std::unique_ptr<HashFunction>(new CRC32);
241  }
242 #endif
243 
244  const SCAN_Name req(algo_spec);
245 
246 #if defined(BOTAN_HAS_TIGER)
247  if(req.algo_name() == "Tiger")
248  {
249  return std::unique_ptr<HashFunction>(
250  new Tiger(req.arg_as_integer(0, 24),
251  req.arg_as_integer(1, 3)));
252  }
253 #endif
254 
255 #if defined(BOTAN_HAS_SKEIN_512)
256  if(req.algo_name() == "Skein-512")
257  {
258  return std::unique_ptr<HashFunction>(
259  new Skein_512(req.arg_as_integer(0, 512), req.arg(1, "")));
260  }
261 #endif
262 
263 #if defined(BOTAN_HAS_BLAKE2B)
264  if(req.algo_name() == "Blake2b")
265  {
266  return std::unique_ptr<HashFunction>(
267  new Blake2b(req.arg_as_integer(0, 512)));
268  }
269 #endif
270 
271 #if defined(BOTAN_HAS_KECCAK)
272  if(req.algo_name() == "Keccak-1600")
273  {
274  return std::unique_ptr<HashFunction>(
275  new Keccak_1600(req.arg_as_integer(0, 512)));
276  }
277 #endif
278 
279 #if defined(BOTAN_HAS_SHA3)
280  if(req.algo_name() == "SHA-3")
281  {
282  return std::unique_ptr<HashFunction>(
283  new SHA_3(req.arg_as_integer(0, 512)));
284  }
285 #endif
286 
287 #if defined(BOTAN_HAS_SHAKE)
288  if(req.algo_name() == "SHAKE-128")
289  {
290  return std::unique_ptr<HashFunction>(new SHAKE_128(req.arg_as_integer(0, 128)));
291  }
292  if(req.algo_name() == "SHAKE-256")
293  {
294  return std::unique_ptr<HashFunction>(new SHAKE_256(req.arg_as_integer(0, 256)));
295  }
296 #endif
297 
298 #if defined(BOTAN_HAS_STREEBOG)
299  if(algo_spec == "Streebog-256")
300  {
301  return std::unique_ptr<HashFunction>(new Streebog_256);
302  }
303  if(algo_spec == "Streebog-512")
304  {
305  return std::unique_ptr<HashFunction>(new Streebog_512);
306  }
307 #endif
308 
309 #if defined(BOTAN_HAS_SM3)
310  if(algo_spec == "SM3")
311  {
312  return std::unique_ptr<HashFunction>(new SM3);
313  }
314 #endif
315 
316 #if defined(BOTAN_HAS_WHIRLPOOL)
317  if(req.algo_name() == "Whirlpool")
318  {
319  return std::unique_ptr<HashFunction>(new Whirlpool);
320  }
321 #endif
322 
323 #if defined(BOTAN_HAS_PARALLEL_HASH)
324  if(req.algo_name() == "Parallel")
325  {
326  std::vector<std::unique_ptr<HashFunction>> hashes;
327 
328  for(size_t i = 0; i != req.arg_count(); ++i)
329  {
330  auto h = HashFunction::create(req.arg(i));
331  if(!h)
332  {
333  return nullptr;
334  }
335  hashes.push_back(std::move(h));
336  }
337 
338  return std::unique_ptr<HashFunction>(new Parallel(hashes));
339  }
340 #endif
341 
342 #if defined(BOTAN_HAS_COMB4P)
343  if(req.algo_name() == "Comb4P" && req.arg_count() == 2)
344  {
345  std::unique_ptr<HashFunction> h1(HashFunction::create(req.arg(0)));
346  std::unique_ptr<HashFunction> h2(HashFunction::create(req.arg(1)));
347 
348  if(h1 && h2)
349  return std::unique_ptr<HashFunction>(new Comb4P(h1.release(), h2.release()));
350  }
351 #endif
352 
353 
354  return nullptr;
355  }
std::unique_ptr< HashFunction > make_bearssl_hash(const std::string &name)
virtual std::string provider() const
Definition: hash.h:58
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:110
std::unique_ptr< HashFunction > make_commoncrypto_hash(const std::string &name)
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 found.

Definition at line 359 of file hash.cpp.

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

Referenced by botan_pubkey_sm2_compute_za(), Botan::OCSP::CertID::CertID(), Botan::create_hex_fingerprint(), Botan::EMSA_PKCS1v15_Raw::EMSA_PKCS1v15_Raw(), Botan::TLS::Handshake_Hash::final(), Botan::generate_dsa_primes(), Botan::TLS::make_hello_random(), Botan::newhope_shareda(), Botan::newhope_sharedb(), Botan::X509_Certificate::raw_issuer_dn_sha256(), Botan::RTSS_Share::split(), and Botan::Cert_Extension::Subject_Key_ID::Subject_Key_ID().

361  {
362  if(auto hash = HashFunction::create(algo, provider))
363  {
364  return hash;
365  }
366  throw Lookup_Error("Hash", algo, provider);
367  }
virtual std::string provider() const
Definition: hash.h:58
static std::unique_ptr< HashFunction > create(const std::string &algo_spec, const std::string &provider="")
Definition: hash.cpp:110
MechanismType hash

◆ final() [1/3]

void Botan::Buffered_Computation::final ( uint8_t  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 89 of file buf_comp.h.

Referenced by botan_mac_final(), Botan::ed25519_gen_keypair(), Botan::ed25519_sign(), Botan::ed25519_verify(), Botan::TLS::TLS_CBC_HMAC_AEAD_Encryption::finish(), Botan::TLS::TLS_CBC_HMAC_AEAD_Decryption::finish(), and Botan::pbkdf2().

89 { final_result(out); }

◆ final() [2/3]

secure_vector<uint8_t> Botan::Buffered_Computation::final ( )
inlineinherited

Complete the computation and retrieve the final result.

Returns
secure_vector holding the result

Definition at line 96 of file buf_comp.h.

97  {
98  secure_vector<uint8_t> output(output_length());
99  final_result(output.data());
100  return output;
101  }
virtual size_t output_length() const =0

◆ final() [3/3]

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

Definition at line 111 of file buf_comp.h.

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

◆ final_result()

void Botan::MDx_HashFunction::final_result ( uint8_t  out[])
finaloverrideprotectedvirtualinherited

Write the final output to out

Parameters
outis an output buffer of output_length()

Implements Botan::Buffered_Computation.

Definition at line 72 of file mdx_hash.cpp.

References Botan::MDx_HashFunction::clear(), Botan::clear_mem(), Botan::MDx_HashFunction::compress_n(), Botan::MDx_HashFunction::copy_out(), Botan::MDx_HashFunction::write_count(), and Botan::zeroise().

73  {
74  clear_mem(&m_buffer[m_position], m_buffer.size() - m_position);
75  m_buffer[m_position] = (BIG_BIT_ENDIAN ? 0x80 : 0x01);
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:33
void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:97
virtual void compress_n(const uint8_t blocks[], size_t block_n)=0
virtual void copy_out(uint8_t buffer[])=0
virtual void write_count(uint8_t out[])
Definition: mdx_hash.cpp:93
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:183

◆ final_stdvec()

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

Definition at line 103 of file buf_comp.h.

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

◆ hash_block_size()

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

32 { return m_buffer.size(); }

◆ name()

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

Implements Botan::HashFunction.

Definition at line 44 of file sha2_32.h.

44 { return "SHA-256"; }

◆ output_length()

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

Implements Botan::Buffered_Computation.

Definition at line 45 of file sha2_32.h.

45 { return 32; }

◆ process() [1/4]

secure_vector<uint8_t> Botan::Buffered_Computation::process ( const uint8_t  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 124 of file buf_comp.h.

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

◆ process() [2/4]

secure_vector<uint8_t> Botan::Buffered_Computation::process ( const secure_vector< uint8_t > &  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 136 of file buf_comp.h.

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

◆ process() [3/4]

secure_vector<uint8_t> Botan::Buffered_Computation::process ( const std::vector< uint8_t > &  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 148 of file buf_comp.h.

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

◆ process() [4/4]

secure_vector<uint8_t> 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 160 of file buf_comp.h.

References update.

161  {
162  update(in);
163  return final();
164  }
void update(const uint8_t 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 58 of file hash.h.

Referenced by Botan::HashFunction::create(), and Botan::HashFunction::create_or_throw().

58 { 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 369 of file hash.cpp.

370  {
371  return probe_providers_of<HashFunction>(algo_spec, {"base", "bearssl", "openssl", "commoncrypto"});
372  }

◆ update() [1/5]

void Botan::Buffered_Computation::update ( const uint8_t  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_mac_update(), Botan::ed25519_gen_keypair(), Botan::ed25519_sign(), Botan::ed25519_verify(), Botan::TLS::TLS_CBC_HMAC_AEAD_Encryption::finish(), Botan::TLS::TLS_CBC_HMAC_AEAD_Decryption::finish(), and Botan::pbkdf2().

34 { add_data(in, length); }

◆ update() [2/5]

void Botan::Buffered_Computation::update ( const secure_vector< uint8_t > &  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< uint8_t > &  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 72 of file buf_comp.h.

References Botan::cast_char_ptr_to_uint8().

73  {
74  add_data(cast_char_ptr_to_uint8(str.data()), str.size());
75  }
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:131

◆ update() [5/5]

void Botan::Buffered_Computation::update ( uint8_t  in)
inlineinherited

Process a single byte.

Parameters
inthe byte to process

Definition at line 81 of file buf_comp.h.

81 { 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(), and T.

Referenced by Botan::pbkdf2().

59  {
60  for(size_t i = 0; i != sizeof(T); ++i)
61  {
62  uint8_t b = get_byte(i, in);
63  add_data(&b, 1);
64  }
65  }
fe T
Definition: ge.cpp:37
uint8_t get_byte(size_t byte_num, T input)
Definition: loadstor.h:39

◆ write_count()

void Botan::MDx_HashFunction::write_count ( uint8_t  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 uint64_t 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_be(uint16_t in, uint8_t out[2])
Definition: loadstor.h:434
size_t hash_block_size() const override final
Definition: mdx_hash.h:32
virtual size_t output_length() const =0
void store_le(uint16_t in, uint8_t out[2])
Definition: loadstor.h:450

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