Botan 3.10.0
Crypto and TLS for C&
Botan::SHA_1 Class Referencefinal

#include <sha1.h>

Inheritance diagram for Botan::SHA_1:
Botan::HashFunction Botan::Buffered_Computation

Public Types

using digest_type = secure_vector<uint32_t>

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
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 void compress_n (digest_type &digest, std::span< const uint8_t > input, size_t blocks)
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 void init (digest_type &digest)
static std::vector< std::string > providers (std::string_view algo_spec)
static void sha1_armv8_compress_n (digest_type &digest, std::span< const uint8_t > blocks, size_t block_count)
static void sha1_compress_x86 (digest_type &digest, std::span< const uint8_t > blocks, size_t block_count)

Static Public Attributes

static constexpr MD_Endian bit_endianness = MD_Endian::Big
static constexpr size_t block_bytes = 64
static constexpr MD_Endian byte_endianness = MD_Endian::Big
static constexpr size_t ctr_bytes = 8
static constexpr size_t output_bytes = 20

Detailed Description

NIST's SHA-1

Definition at line 18 of file sha1.h.

Member Typedef Documentation

◆ digest_type

Definition at line 20 of file sha1.h.

Member Function Documentation

◆ clear()

void Botan::SHA_1::clear ( )
inlineoverridevirtual

Reset the state.

Implements Botan::HashFunction.

Definition at line 44 of file sha1.h.

44{ m_md.clear(); }

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

◆ compress_n()

void Botan::SHA_1::compress_n ( digest_type & digest,
std::span< const uint8_t > input,
size_t blocks )
static

Definition at line 24 of file sha1.cpp.

24 {
25 using namespace SHA1_F;
26
27#if defined(BOTAN_HAS_SHA1_X86_SHA_NI)
29 return sha1_compress_x86(digest, input, blocks);
30 }
31#endif
32
33#if defined(BOTAN_HAS_SHA1_ARMV8)
35 return sha1_armv8_compress_n(digest, input, blocks);
36 }
37#endif
38
39#if defined(BOTAN_HAS_SHA1_AVX2)
41 return avx2_compress_n(digest, input, blocks);
42 }
43#endif
44
45#if defined(BOTAN_HAS_SHA1_SIMD_4X32)
47 return simd_compress_n(digest, input, blocks);
48 }
49#endif
50
51 uint32_t A = digest[0];
52 uint32_t B = digest[1];
53 uint32_t C = digest[2];
54 uint32_t D = digest[3];
55 uint32_t E = digest[4];
56 std::array<uint32_t, 80> W{};
57 auto W_in = std::span{W}.first<block_bytes / sizeof(uint32_t)>();
58
59 BufferSlicer in(input);
60
61 for(size_t i = 0; i != blocks; ++i) {
62 load_be(W_in, in.take<block_bytes>());
63
64 // clang-format off
65
66 for(size_t j = 16; j != 80; j += 8) {
67 W[j + 0] = rotl<1>(W[j - 3] ^ W[j - 8] ^ W[j - 14] ^ W[j - 16]);
68 W[j + 1] = rotl<1>(W[j - 2] ^ W[j - 7] ^ W[j - 13] ^ W[j - 15]);
69 W[j + 2] = rotl<1>(W[j - 1] ^ W[j - 6] ^ W[j - 12] ^ W[j - 14]);
70 W[j + 3] = rotl<1>(W[j ] ^ W[j - 5] ^ W[j - 11] ^ W[j - 13]);
71 W[j + 4] = rotl<1>(W[j + 1] ^ W[j - 4] ^ W[j - 10] ^ W[j - 12]);
72 W[j + 5] = rotl<1>(W[j + 2] ^ W[j - 3] ^ W[j - 9] ^ W[j - 11]);
73 W[j + 6] = rotl<1>(W[j + 3] ^ W[j - 2] ^ W[j - 8] ^ W[j - 10]);
74 W[j + 7] = rotl<1>(W[j + 4] ^ W[j - 1] ^ W[j - 7] ^ W[j - 9]);
75 }
76
77 // clang-format on
78
79 F1(A, B, C, D, E, W[0] + K1);
80 F1(E, A, B, C, D, W[1] + K1);
81 F1(D, E, A, B, C, W[2] + K1);
82 F1(C, D, E, A, B, W[3] + K1);
83 F1(B, C, D, E, A, W[4] + K1);
84 F1(A, B, C, D, E, W[5] + K1);
85 F1(E, A, B, C, D, W[6] + K1);
86 F1(D, E, A, B, C, W[7] + K1);
87 F1(C, D, E, A, B, W[8] + K1);
88 F1(B, C, D, E, A, W[9] + K1);
89 F1(A, B, C, D, E, W[10] + K1);
90 F1(E, A, B, C, D, W[11] + K1);
91 F1(D, E, A, B, C, W[12] + K1);
92 F1(C, D, E, A, B, W[13] + K1);
93 F1(B, C, D, E, A, W[14] + K1);
94 F1(A, B, C, D, E, W[15] + K1);
95 F1(E, A, B, C, D, W[16] + K1);
96 F1(D, E, A, B, C, W[17] + K1);
97 F1(C, D, E, A, B, W[18] + K1);
98 F1(B, C, D, E, A, W[19] + K1);
99
100 F2(A, B, C, D, E, W[20] + K2);
101 F2(E, A, B, C, D, W[21] + K2);
102 F2(D, E, A, B, C, W[22] + K2);
103 F2(C, D, E, A, B, W[23] + K2);
104 F2(B, C, D, E, A, W[24] + K2);
105 F2(A, B, C, D, E, W[25] + K2);
106 F2(E, A, B, C, D, W[26] + K2);
107 F2(D, E, A, B, C, W[27] + K2);
108 F2(C, D, E, A, B, W[28] + K2);
109 F2(B, C, D, E, A, W[29] + K2);
110 F2(A, B, C, D, E, W[30] + K2);
111 F2(E, A, B, C, D, W[31] + K2);
112 F2(D, E, A, B, C, W[32] + K2);
113 F2(C, D, E, A, B, W[33] + K2);
114 F2(B, C, D, E, A, W[34] + K2);
115 F2(A, B, C, D, E, W[35] + K2);
116 F2(E, A, B, C, D, W[36] + K2);
117 F2(D, E, A, B, C, W[37] + K2);
118 F2(C, D, E, A, B, W[38] + K2);
119 F2(B, C, D, E, A, W[39] + K2);
120
121 F3(A, B, C, D, E, W[40] + K3);
122 F3(E, A, B, C, D, W[41] + K3);
123 F3(D, E, A, B, C, W[42] + K3);
124 F3(C, D, E, A, B, W[43] + K3);
125 F3(B, C, D, E, A, W[44] + K3);
126 F3(A, B, C, D, E, W[45] + K3);
127 F3(E, A, B, C, D, W[46] + K3);
128 F3(D, E, A, B, C, W[47] + K3);
129 F3(C, D, E, A, B, W[48] + K3);
130 F3(B, C, D, E, A, W[49] + K3);
131 F3(A, B, C, D, E, W[50] + K3);
132 F3(E, A, B, C, D, W[51] + K3);
133 F3(D, E, A, B, C, W[52] + K3);
134 F3(C, D, E, A, B, W[53] + K3);
135 F3(B, C, D, E, A, W[54] + K3);
136 F3(A, B, C, D, E, W[55] + K3);
137 F3(E, A, B, C, D, W[56] + K3);
138 F3(D, E, A, B, C, W[57] + K3);
139 F3(C, D, E, A, B, W[58] + K3);
140 F3(B, C, D, E, A, W[59] + K3);
141
142 F4(A, B, C, D, E, W[60] + K4);
143 F4(E, A, B, C, D, W[61] + K4);
144 F4(D, E, A, B, C, W[62] + K4);
145 F4(C, D, E, A, B, W[63] + K4);
146 F4(B, C, D, E, A, W[64] + K4);
147 F4(A, B, C, D, E, W[65] + K4);
148 F4(E, A, B, C, D, W[66] + K4);
149 F4(D, E, A, B, C, W[67] + K4);
150 F4(C, D, E, A, B, W[68] + K4);
151 F4(B, C, D, E, A, W[69] + K4);
152 F4(A, B, C, D, E, W[70] + K4);
153 F4(E, A, B, C, D, W[71] + K4);
154 F4(D, E, A, B, C, W[72] + K4);
155 F4(C, D, E, A, B, W[73] + K4);
156 F4(B, C, D, E, A, W[74] + K4);
157 F4(A, B, C, D, E, W[75] + K4);
158 F4(E, A, B, C, D, W[76] + K4);
159 F4(D, E, A, B, C, W[77] + K4);
160 F4(C, D, E, A, B, W[78] + K4);
161 F4(B, C, D, E, A, W[79] + K4);
162
163 A = (digest[0] += A);
164 B = (digest[1] += B);
165 C = (digest[2] += C);
166 D = (digest[3] += D);
167 E = (digest[4] += E);
168 }
169}
static bool has(CPUID::Feature feat)
Definition cpuid.h:94
static void sha1_compress_x86(digest_type &digest, std::span< const uint8_t > blocks, size_t block_count)
Definition sha1_x86.cpp:74
static void sha1_armv8_compress_n(digest_type &digest, std::span< const uint8_t > blocks, size_t block_count)
static constexpr size_t block_bytes
Definition sha1.h:24
void F2(uint32_t A, uint32_t &B, uint32_t C, uint32_t D, uint32_t &E, uint32_t M)
Definition sha1_f.h:26
void F4(uint32_t A, uint32_t &B, uint32_t C, uint32_t D, uint32_t &E, uint32_t M)
Definition sha1_f.h:37
void F3(uint32_t A, uint32_t &B, uint32_t C, uint32_t D, uint32_t &E, uint32_t M)
Definition sha1_f.h:31
void F1(uint32_t A, uint32_t &B, uint32_t C, uint32_t D, uint32_t &E, uint32_t M)
Definition sha1_f.h:21
BOTAN_FORCE_INLINE constexpr T rotl(T input)
Definition rotate.h:23
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504

References Botan::CPUFeature::AVX2, block_bytes, Botan::CPUFeature::BMI, Botan::CPUID::has(), Botan::load_be(), Botan::rotl(), Botan::CPUFeature::SHA, Botan::CPUFeature::SHA1, sha1_armv8_compress_n(), sha1_compress_x86(), Botan::CPUFeature::SIMD_4X32, and Botan::BufferSlicer::take().

◆ copy_state()

std::unique_ptr< HashFunction > Botan::SHA_1::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 210 of file sha1.cpp.

210 {
211 return std::make_unique<SHA_1>(*this);
212}

◆ 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_1::hash_block_size ( ) const
inlineoverridevirtual
Returns
hash block size as defined for this algorithm

Reimplemented from Botan::HashFunction.

Definition at line 36 of file sha1.h.

36{ return block_bytes; }

References block_bytes.

◆ init()

void Botan::SHA_1::init ( digest_type & digest)
static

Definition at line 174 of file sha1.cpp.

174 {
175 digest.assign({0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0});
176}

◆ name()

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

Implements Botan::HashFunction.

Definition at line 32 of file sha1.h.

32{ return "SHA-1"; }

◆ new_object()

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

Implements Botan::HashFunction.

Definition at line 206 of file sha1.cpp.

206 {
207 return std::make_unique<SHA_1>();
208}

◆ output_length()

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

Implements Botan::Buffered_Computation.

Definition at line 34 of file sha1.h.

34{ return 20; }

◆ 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_1::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 178 of file sha1.cpp.

178 {
179#if defined(BOTAN_HAS_SHA1_X86_SHA_NI)
180 if(auto feat = CPUID::check(CPUID::Feature::SHA)) {
181 return *feat;
182 }
183#endif
184
185#if defined(BOTAN_HAS_SHA1_ARMV8)
186 if(auto feat = CPUID::check(CPUID::Feature::SHA1)) {
187 return *feat;
188 }
189#endif
190
191#if defined(BOTAN_HAS_SHA1_AVX2)
193 return *feat;
194 }
195#endif
196
197#if defined(BOTAN_HAS_SHA1_SIMD_4X32)
198 if(auto feat = CPUID::check(CPUID::Feature::SIMD_4X32)) {
199 return *feat;
200 }
201#endif
202
203 return "base";
204}
static std::optional< std::string > check(CPUID::Feature feat)
Definition cpuid.h:67

References Botan::CPUFeature::AVX2, Botan::CPUFeature::BMI, Botan::CPUID::check(), Botan::CPUFeature::SHA, Botan::CPUFeature::SHA1, and Botan::CPUFeature::SIMD_4X32.

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

◆ sha1_armv8_compress_n()

void BOTAN_FN_ISA_SHA2 Botan::SHA_1::sha1_armv8_compress_n ( digest_type & digest,
std::span< const uint8_t > blocks,
size_t block_count )
static

Definition at line 21 of file sha1_armv8.cpp.

23 {
24 // Load magic constants
25 const uint32x4_t C0 = vdupq_n_u32(0x5A827999);
26 const uint32x4_t C1 = vdupq_n_u32(0x6ED9EBA1);
27 const uint32x4_t C2 = vdupq_n_u32(0x8F1BBCDC);
28 const uint32x4_t C3 = vdupq_n_u32(0xCA62C1D6);
29
30 uint32x4_t ABCD = vld1q_u32(&digest[0]); // NOLINT(*-container-data-pointer)
31 uint32_t E0 = digest[4];
32
33 const uint32_t* input32 = reinterpret_cast<const uint32_t*>(input8.data());
34
35 while(blocks > 0) {
36 // Save current hash
37 const uint32x4_t ABCD_SAVED = ABCD;
38 const uint32_t E0_SAVED = E0;
39
40 uint32x4_t MSG0 = vld1q_u32(input32 + 0);
41 uint32x4_t MSG1 = vld1q_u32(input32 + 4);
42 uint32x4_t MSG2 = vld1q_u32(input32 + 8);
43 uint32x4_t MSG3 = vld1q_u32(input32 + 12);
44
45 MSG0 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(MSG0)));
46 MSG1 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(MSG1)));
47 MSG2 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(MSG2)));
48 MSG3 = vreinterpretq_u32_u8(vrev32q_u8(vreinterpretq_u8_u32(MSG3)));
49
50 uint32x4_t TMP0 = vaddq_u32(MSG0, C0);
51 uint32x4_t TMP1 = vaddq_u32(MSG1, C0);
52
53 // Rounds 0-3
54 uint32_t E1 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
55 ABCD = vsha1cq_u32(ABCD, E0, TMP0);
56 TMP0 = vaddq_u32(MSG2, C0);
57 MSG0 = vsha1su0q_u32(MSG0, MSG1, MSG2);
58
59 // Rounds 4-7
60 E0 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
61 ABCD = vsha1cq_u32(ABCD, E1, TMP1);
62 TMP1 = vaddq_u32(MSG3, C0);
63 MSG0 = vsha1su1q_u32(MSG0, MSG3);
64 MSG1 = vsha1su0q_u32(MSG1, MSG2, MSG3);
65
66 // Rounds 8-11
67 E1 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
68 ABCD = vsha1cq_u32(ABCD, E0, TMP0);
69 TMP0 = vaddq_u32(MSG0, C0);
70 MSG1 = vsha1su1q_u32(MSG1, MSG0);
71 MSG2 = vsha1su0q_u32(MSG2, MSG3, MSG0);
72
73 // Rounds 12-15
74 E0 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
75 ABCD = vsha1cq_u32(ABCD, E1, TMP1);
76 TMP1 = vaddq_u32(MSG1, C1);
77 MSG2 = vsha1su1q_u32(MSG2, MSG1);
78 MSG3 = vsha1su0q_u32(MSG3, MSG0, MSG1);
79
80 // Rounds 16-19
81 E1 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
82 ABCD = vsha1cq_u32(ABCD, E0, TMP0);
83 TMP0 = vaddq_u32(MSG2, C1);
84 MSG3 = vsha1su1q_u32(MSG3, MSG2);
85 MSG0 = vsha1su0q_u32(MSG0, MSG1, MSG2);
86
87 // Rounds 20-23
88 E0 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
89 ABCD = vsha1pq_u32(ABCD, E1, TMP1);
90 TMP1 = vaddq_u32(MSG3, C1);
91 MSG0 = vsha1su1q_u32(MSG0, MSG3);
92 MSG1 = vsha1su0q_u32(MSG1, MSG2, MSG3);
93
94 // Rounds 24-27
95 E1 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
96 ABCD = vsha1pq_u32(ABCD, E0, TMP0);
97 TMP0 = vaddq_u32(MSG0, C1);
98 MSG1 = vsha1su1q_u32(MSG1, MSG0);
99 MSG2 = vsha1su0q_u32(MSG2, MSG3, MSG0);
100
101 // Rounds 28-31
102 E0 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
103 ABCD = vsha1pq_u32(ABCD, E1, TMP1);
104 TMP1 = vaddq_u32(MSG1, C1);
105 MSG2 = vsha1su1q_u32(MSG2, MSG1);
106 MSG3 = vsha1su0q_u32(MSG3, MSG0, MSG1);
107
108 // Rounds 32-35
109 E1 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
110 ABCD = vsha1pq_u32(ABCD, E0, TMP0);
111 TMP0 = vaddq_u32(MSG2, C2);
112 MSG3 = vsha1su1q_u32(MSG3, MSG2);
113 MSG0 = vsha1su0q_u32(MSG0, MSG1, MSG2);
114
115 // Rounds 36-39
116 E0 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
117 ABCD = vsha1pq_u32(ABCD, E1, TMP1);
118 TMP1 = vaddq_u32(MSG3, C2);
119 MSG0 = vsha1su1q_u32(MSG0, MSG3);
120 MSG1 = vsha1su0q_u32(MSG1, MSG2, MSG3);
121
122 // Rounds 40-43
123 E1 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
124 ABCD = vsha1mq_u32(ABCD, E0, TMP0);
125 TMP0 = vaddq_u32(MSG0, C2);
126 MSG1 = vsha1su1q_u32(MSG1, MSG0);
127 MSG2 = vsha1su0q_u32(MSG2, MSG3, MSG0);
128
129 // Rounds 44-47
130 E0 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
131 ABCD = vsha1mq_u32(ABCD, E1, TMP1);
132 TMP1 = vaddq_u32(MSG1, C2);
133 MSG2 = vsha1su1q_u32(MSG2, MSG1);
134 MSG3 = vsha1su0q_u32(MSG3, MSG0, MSG1);
135
136 // Rounds 48-51
137 E1 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
138 ABCD = vsha1mq_u32(ABCD, E0, TMP0);
139 TMP0 = vaddq_u32(MSG2, C2);
140 MSG3 = vsha1su1q_u32(MSG3, MSG2);
141 MSG0 = vsha1su0q_u32(MSG0, MSG1, MSG2);
142
143 // Rounds 52-55
144 E0 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
145 ABCD = vsha1mq_u32(ABCD, E1, TMP1);
146 TMP1 = vaddq_u32(MSG3, C3);
147 MSG0 = vsha1su1q_u32(MSG0, MSG3);
148 MSG1 = vsha1su0q_u32(MSG1, MSG2, MSG3);
149
150 // Rounds 56-59
151 E1 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
152 ABCD = vsha1mq_u32(ABCD, E0, TMP0);
153 TMP0 = vaddq_u32(MSG0, C3);
154 MSG1 = vsha1su1q_u32(MSG1, MSG0);
155 MSG2 = vsha1su0q_u32(MSG2, MSG3, MSG0);
156
157 // Rounds 60-63
158 E0 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
159 ABCD = vsha1pq_u32(ABCD, E1, TMP1);
160 TMP1 = vaddq_u32(MSG1, C3);
161 MSG2 = vsha1su1q_u32(MSG2, MSG1);
162 MSG3 = vsha1su0q_u32(MSG3, MSG0, MSG1);
163
164 // Rounds 64-67
165 E1 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
166 ABCD = vsha1pq_u32(ABCD, E0, TMP0);
167 TMP0 = vaddq_u32(MSG2, C3);
168 MSG3 = vsha1su1q_u32(MSG3, MSG2);
169 MSG0 = vsha1su0q_u32(MSG0, MSG1, MSG2);
170
171 // Rounds 68-71
172 E0 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
173 ABCD = vsha1pq_u32(ABCD, E1, TMP1);
174 TMP1 = vaddq_u32(MSG3, C3);
175
176 // Rounds 72-75
177 E1 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
178 ABCD = vsha1pq_u32(ABCD, E0, TMP0);
179
180 // Rounds 76-79
181 E0 = vsha1h_u32(vgetq_lane_u32(ABCD, 0));
182 ABCD = vsha1pq_u32(ABCD, E1, TMP1);
183
184 // Add state back
185 E0 += E0_SAVED;
186 ABCD = vaddq_u32(ABCD_SAVED, ABCD);
187
188 input32 += 64 / 4;
189 blocks--;
190 }
191
192 // Save digest
193 vst1q_u32(&digest[0], ABCD); // NOLINT(*-container-data-pointer)
194 digest[4] = E0;
195}

Referenced by compress_n().

◆ sha1_compress_x86()

void BOTAN_FN_ISA_SHANI Botan::SHA_1::sha1_compress_x86 ( digest_type & digest,
std::span< const uint8_t > blocks,
size_t block_count )
static

Definition at line 74 of file sha1_x86.cpp.

76 {
77 const uint8_t* input = input_span.data();
78
79 SIMD_4x32 ABCD = rev_words(SIMD_4x32::load_le(&digest[0])); // NOLINT(*-container-data-pointer)
80 SIMD_4x32 E0 = SIMD_4x32(0, 0, 0, digest[4]);
81
82 while(blocks > 0) {
83 // Save current hash
84 const auto ABCD_SAVE = ABCD;
85 const auto E0_SAVE = E0;
86
87 auto W0 = rev_words(SIMD_4x32::load_be(input));
88 auto W1 = rev_words(SIMD_4x32::load_be(input + 16));
89 auto W2 = rev_words(SIMD_4x32::load_be(input + 32));
90 auto W3 = rev_words(SIMD_4x32::load_be(input + 48));
91
92 sha1_x86_first8<0>(ABCD, E0, W0, W1);
93 sha1_x86_rnds8<0>(ABCD, E0, W2, W3);
94
95 W0 = sha1_x86_msg1(W0, W1);
96 W1 = sha1_x86_msg1(W1, W2);
97 W0 ^= W2;
98
99 sha1_x86_next_msg(W3, W0, W1, W2);
100 sha1_x86_next_msg(W0, W1, W2, W3);
101 sha1_x86_rnds8<0, 1>(ABCD, E0, W0, W1);
102
103 sha1_x86_next_msg(W1, W2, W3, W0);
104 sha1_x86_next_msg(W2, W3, W0, W1);
105 sha1_x86_rnds8<1>(ABCD, E0, W2, W3);
106
107 sha1_x86_next_msg(W3, W0, W1, W2);
108 sha1_x86_next_msg(W0, W1, W2, W3);
109 sha1_x86_rnds8<1>(ABCD, E0, W0, W1);
110
111 sha1_x86_next_msg(W1, W2, W3, W0);
112 sha1_x86_next_msg(W2, W3, W0, W1);
113 sha1_x86_rnds8<2>(ABCD, E0, W2, W3);
114
115 sha1_x86_next_msg(W3, W0, W1, W2);
116 sha1_x86_next_msg(W0, W1, W2, W3);
117 sha1_x86_rnds8<2>(ABCD, E0, W0, W1);
118
119 sha1_x86_next_msg(W1, W2, W3, W0);
120 sha1_x86_next_msg(W2, W3, W0, W1);
121 sha1_x86_rnds8<2, 3>(ABCD, E0, W2, W3);
122
123 sha1_x86_next_msg(W3, W0, W1, W2);
124 sha1_x86_next_msg(W0, W1, W2, W3);
125 sha1_x86_rnds8<3>(ABCD, E0, W0, W1);
126
127 sha1_x86_next_msg(W1, W2, W3, W0);
128 sha1_x86_next_msg(W2, W3, W0, W1);
129 sha1_x86_rnds8<3>(ABCD, E0, W2, W3);
130
131 ABCD += ABCD_SAVE;
132 E0 = sha1_x86_nexte(E0, E0_SAVE);
133
134 input += 64;
135 blocks--;
136 }
137
138 rev_words(ABCD).store_le(&digest[0]); // NOLINT(*-container-data-pointer)
139 digest[4] = _mm_extract_epi32(E0.raw(), 3);
140}
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 load_be(const void *in) noexcept
Definition simd_4x32.h:174
static SIMD_4x32 load_le(const void *in) noexcept
Definition simd_4x32.h:149

References Botan::SIMD_4x32::load_be(), Botan::SIMD_4x32::load_le(), and Botan::SIMD_4x32::raw().

Referenced by compress_n().

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

Member Data Documentation

◆ bit_endianness

MD_Endian Botan::SHA_1::bit_endianness = MD_Endian::Big
staticconstexpr

Definition at line 23 of file sha1.h.

◆ block_bytes

size_t Botan::SHA_1::block_bytes = 64
staticconstexpr

Definition at line 24 of file sha1.h.

Referenced by compress_n(), and hash_block_size().

◆ byte_endianness

MD_Endian Botan::SHA_1::byte_endianness = MD_Endian::Big
staticconstexpr

Definition at line 22 of file sha1.h.

◆ ctr_bytes

size_t Botan::SHA_1::ctr_bytes = 8
staticconstexpr

Definition at line 26 of file sha1.h.

◆ output_bytes

size_t Botan::SHA_1::output_bytes = 20
staticconstexpr

Definition at line 25 of file sha1.h.


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