Botan 3.10.0
Crypto and TLS for C&
Botan::SHA_3 Class Reference

#include <sha3.h>

Inheritance diagram for Botan::SHA_3:
Botan::HashFunction Botan::Buffered_Computation Botan::SHA_3_224 Botan::SHA_3_256 Botan::SHA_3_384 Botan::SHA_3_512

Public Member Functions

void clear () override
HashFunctionclone () const
std::unique_ptr< HashFunctioncopy_state () const override
template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
final ()
void final (std::span< uint8_t > out)
template<concepts::resizable_byte_buffer T>
void final (T &out)
void final (uint8_t out[])
std::vector< uint8_t > final_stdvec ()
size_t hash_block_size () const override
std::string name () const override
std::unique_ptr< HashFunctionnew_object () const override
size_t output_length () const override
template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
process (const uint8_t in[], size_t length)
template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
process (std::span< const uint8_t > in)
template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
process (std::string_view in)
std::string provider () const override
 SHA_3 (size_t output_bits)
void update (const uint8_t in[], size_t length)
void update (std::span< const uint8_t > in)
void update (std::string_view str)
void update (uint8_t in)
void update_be (uint16_t val)
void update_be (uint32_t val)
void update_be (uint64_t val)
void update_le (uint16_t val)
void update_le (uint32_t val)
void update_le (uint64_t val)

Static Public Member Functions

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

Detailed Description

SHA-3

Definition at line 21 of file sha3.h.

Constructor & Destructor Documentation

◆ SHA_3()

Botan::SHA_3::SHA_3 ( size_t output_bits)
explicit
Parameters
output_bitsthe size of the hash output; must be one of 224, 256, 384, or 512

Definition at line 16 of file sha3.cpp.

16 :
17 m_keccak({.capacity_bits = output_bits * 2, .padding = KeccakPadding::sha3()}), m_output_length(output_bits / 8) {
18 // We only support the parameters for SHA-3 in this constructor
19
20 if(output_bits != 224 && output_bits != 256 && output_bits != 384 && output_bits != 512) {
21 throw Invalid_Argument(fmt("SHA_3: Invalid output length {}", output_bits));
22 }
23}
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
static constexpr KeccakPadding sha3()
The number of relevant bits in 'padding'.
Definition keccak_perm.h:24

References Botan::KeccakPadding::sha3().

Referenced by Botan::SHA_3_224::SHA_3_224(), Botan::SHA_3_256::SHA_3_256(), Botan::SHA_3_384::SHA_3_384(), and Botan::SHA_3_512::SHA_3_512().

Member Function Documentation

◆ clear()

void Botan::SHA_3::clear ( )
overridevirtual

Reset the state.

Implements Botan::HashFunction.

Definition at line 41 of file sha3.cpp.

41 {
42 m_keccak.clear();
43}

◆ clone()

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

Definition at line 85 of file hash.h.

85{ return this->new_object().release(); }
virtual std::unique_ptr< HashFunction > new_object() const =0

References new_object().

◆ copy_state()

std::unique_ptr< HashFunction > Botan::SHA_3::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 33 of file sha3.cpp.

33 {
34 return std::make_unique<SHA_3>(*this);
35}

◆ create()

std::unique_ptr< HashFunction > Botan::HashFunction::create ( std::string_view algo_spec,
std::string_view 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 111 of file hash.cpp.

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

References Botan::SCAN_Name::algo_name(), Botan::SCAN_Name::arg(), Botan::SCAN_Name::arg_as_integer(), Botan::SCAN_Name::arg_count(), create(), Botan::make_commoncrypto_hash(), and provider().

Referenced by botan_hash_init(), Botan::BlockCipher::create(), Botan::EncryptionPaddingScheme::create(), create(), Botan::KDF::create(), Botan::MessageAuthenticationCode::create(), Botan::PasswordHashFamily::create(), Botan::PBKDF::create(), Botan::SignaturePaddingScheme::create(), and create_or_throw().

◆ create_or_throw()

std::unique_ptr< HashFunction > Botan::HashFunction::create_or_throw ( std::string_view algo_spec,
std::string_view 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 308 of file hash.cpp.

308 {
309 if(auto hash = HashFunction::create(algo, provider)) {
310 return hash;
311 }
312 throw Lookup_Error("Hash", algo, provider);
313}

References create(), and provider().

Referenced by botan_pubkey_fingerprint(), botan_pubkey_sm2_compute_za(), Botan::OCSP::CertID::CertID(), Botan::create_hex_fingerprint(), Botan::Sodium::crypto_hash_sha256(), Botan::Sodium::crypto_hash_sha512(), Botan::Bcrypt_PBKDF::derive_key(), Botan::ed25519_gen_keypair(), Botan::ed25519_sign(), Botan::ed25519_verify(), Botan::expand_message_xmd(), Botan::TLS::Handshake_Hash::final(), Botan::Certificate_Store_In_Memory::find_cert_by_pubkey_sha1(), Botan::Certificate_Store_In_Memory::find_cert_by_raw_subject_dn_sha256(), Botan::generate_dsa_primes(), Botan::LMOTS_Params::hash(), Botan::LMS_Params::hash(), Botan::Classic_McEliece_Parameters::hash_func(), Botan::OCSP::CertID::is_id_for(), Botan::TLS::make_hello_random(), Botan::Roughtime::nonce_from_blind(), Botan::PKCS1v15_Raw_SignaturePaddingScheme::PKCS1v15_Raw_SignaturePaddingScheme(), Botan::TLS::Transcript_Hash_State::set_algorithm(), Botan::RTSS_Share::split(), Botan::srp6_client_agree(), Botan::srp6_generate_verifier(), Botan::SRP6_Server_Session::step1(), Botan::SRP6_Server_Session::step2(), Botan::Cert_Extension::Subject_Key_ID::Subject_Key_ID(), and Botan::PK_Ops::Verification_with_Hash::Verification_with_Hash().

◆ final() [1/4]

template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
T Botan::Buffered_Computation::final ( )
inlineinherited

Complete the computation and retrieve the final result as a container of your choice.

Returns
a contiguous container holding the result

Definition at line 77 of file buf_comp.h.

77 {
78 T output(output_length());
79 final_result(output);
80 return output;
81 }
virtual size_t output_length() const =0

References output_length().

◆ final() [2/4]

void Botan::Buffered_Computation::final ( std::span< uint8_t > out)
inherited

Definition at line 54 of file buf_comp.cpp.

54 {
55 BOTAN_ARG_CHECK(out.size() >= output_length(), "provided output buffer has insufficient capacity");
56 final_result(out);
57}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33

References BOTAN_ARG_CHECK, and output_length().

◆ final() [3/4]

template<concepts::resizable_byte_buffer T>
void Botan::Buffered_Computation::final ( T & out)
inlineinherited

Definition at line 88 of file buf_comp.h.

88 {
89 out.resize(output_length());
90 final_result(out);
91 }

References output_length().

◆ final() [4/4]

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

69{ final_result({out, output_length()}); }

References output_length().

Referenced by final_stdvec(), Botan::PseudorandomKeyGeneration::gen(), Botan::TPM2::Verification_Operation::is_valid_signature(), Botan::mgf1_mask(), Botan::KMAC::operator=(), Botan::pbkdf2(), Botan::Sphincs_Hash_Functions_Sha2::PRF_msg(), process(), process(), process(), Botan::TPM2::Signature_Operation::sign(), and Botan::sm2_compute_za().

◆ final_stdvec()

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

Definition at line 83 of file buf_comp.h.

83{ return final<std::vector<uint8_t>>(); }
void final(uint8_t out[])
Definition buf_comp.h:69

References final().

◆ hash_block_size()

size_t Botan::SHA_3::hash_block_size ( ) const
inlineoverridevirtual
Returns
hash block size as defined for this algorithm

Reimplemented from Botan::HashFunction.

Definition at line 29 of file sha3.h.

29{ return m_keccak.byte_rate(); }

◆ name()

std::string Botan::SHA_3::name ( ) const
overridevirtual
Returns
the hash function name

Implements Botan::HashFunction.

Definition at line 25 of file sha3.cpp.

25 {
26 return fmt("SHA-3({})", m_output_length * 8);
27}

References Botan::fmt().

◆ new_object()

std::unique_ptr< HashFunction > Botan::SHA_3::new_object ( ) const
overridevirtual
Returns
new object representing the same algorithm as *this

Implements Botan::HashFunction.

Definition at line 37 of file sha3.cpp.

37 {
38 return std::make_unique<SHA_3>(m_output_length * 8);
39}

◆ output_length()

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

Implements Botan::Buffered_Computation.

Definition at line 31 of file sha3.h.

31{ return m_output_length; }

◆ process() [1/3]

template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
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 101 of file buf_comp.h.

101 {
102 update(in, length);
103 return final<T>();
104 }
void update(const uint8_t in[], size_t length)
Definition buf_comp.h:34

References final(), and update().

Referenced by Botan::Kyber_Symmetric_Primitives::H(), Botan::Kyber_Symmetric_Primitives::H(), and Botan::Kyber_Symmetric_Primitives::H().

◆ process() [2/3]

template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
T Botan::Buffered_Computation::process ( std::span< const uint8_t > in)
inlineinherited

Update and finalize computation. Does the same as calling update() and final() consecutively.

Parameters
inthe input to process as a contiguous container
Returns
the result of the call to final()

Definition at line 125 of file buf_comp.h.

125 {
126 update(in);
127 return final<T>();
128 }

References final(), and update().

◆ process() [3/3]

template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
T Botan::Buffered_Computation::process ( std::string_view 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 113 of file buf_comp.h.

113 {
114 update(in);
115 return final<T>();
116 }

References final(), and update().

◆ provider()

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

Reimplemented from Botan::HashFunction.

Definition at line 29 of file sha3.cpp.

29 {
30 return m_keccak.provider();
31}

◆ providers()

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

Definition at line 315 of file hash.cpp.

315 {
316 return probe_providers_of<HashFunction>(algo_spec, {"base", "commoncrypto"});
317}
std::vector< std::string > probe_providers_of(std::string_view algo_spec, const std::vector< std::string > &possible={"base"})
Definition scan_name.h:105

References Botan::probe_providers_of().

◆ update() [1/4]

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.

34{ add_data({in, length}); }

Referenced by Botan::PseudorandomKeyGeneration::gen(), Botan::LMOTS_Public_Key::LMOTS_Public_Key(), Botan::mgf1_mask(), Botan::pbkdf2(), Botan::Sphincs_Hash_Functions_Sha2::PRF_msg(), process(), process(), process(), Botan::TLS::TLS_NULL_HMAC_AEAD_Mode::set_associated_data_n(), and Botan::sm2_compute_za().

◆ update() [2/4]

void Botan::Buffered_Computation::update ( std::span< const uint8_t > in)
inlineinherited

Add new input to process.

Parameters
inthe input to process as a contiguous data range

Definition at line 40 of file buf_comp.h.

40{ add_data(in); }

◆ update() [3/4]

void Botan::Buffered_Computation::update ( std::string_view str)
inherited

Add new input to process.

Parameters
strthe input to process as a std::string_view. Will be interpreted as a byte array based on the strings encoding.

Definition at line 14 of file buf_comp.cpp.

14 {
15 add_data(as_span_of_bytes(str));
16}
std::span< const uint8_t > as_span_of_bytes(const char *s, size_t len)
Definition mem_utils.h:28

References Botan::as_span_of_bytes().

◆ update() [4/4]

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

Process a single byte.

Parameters
inthe byte to process

Definition at line 61 of file buf_comp.h.

61{ add_data({&in, 1}); }

◆ update_be() [1/3]

void Botan::Buffered_Computation::update_be ( uint16_t val)
inherited

Definition at line 18 of file buf_comp.cpp.

18 {
19 uint8_t inb[sizeof(val)];
20 store_be(val, inb);
21 add_data({inb, sizeof(inb)});
22}
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745

References Botan::store_be().

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

◆ update_be() [2/3]

void Botan::Buffered_Computation::update_be ( uint32_t val)
inherited

Definition at line 24 of file buf_comp.cpp.

24 {
25 uint8_t inb[sizeof(val)];
26 store_be(val, inb);
27 add_data({inb, sizeof(inb)});
28}

References Botan::store_be().

◆ update_be() [3/3]

void Botan::Buffered_Computation::update_be ( uint64_t val)
inherited

Definition at line 30 of file buf_comp.cpp.

30 {
31 uint8_t inb[sizeof(val)];
32 store_be(val, inb);
33 add_data({inb, sizeof(inb)});
34}

References Botan::store_be().

◆ update_le() [1/3]

void Botan::Buffered_Computation::update_le ( uint16_t val)
inherited

Definition at line 36 of file buf_comp.cpp.

36 {
37 uint8_t inb[sizeof(val)];
38 store_le(val, inb);
39 add_data({inb, sizeof(inb)});
40}
constexpr auto store_le(ParamTs &&... params)
Definition loadstor.h:736

References Botan::store_le().

◆ update_le() [2/3]

void Botan::Buffered_Computation::update_le ( uint32_t val)
inherited

Definition at line 42 of file buf_comp.cpp.

42 {
43 uint8_t inb[sizeof(val)];
44 store_le(val, inb);
45 add_data({inb, sizeof(inb)});
46}

References Botan::store_le().

◆ update_le() [3/3]

void Botan::Buffered_Computation::update_le ( uint64_t val)
inherited

Definition at line 48 of file buf_comp.cpp.

48 {
49 uint8_t inb[sizeof(val)];
50 store_le(val, inb);
51 add_data({inb, sizeof(inb)});
52}

References Botan::store_le().


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