Botan 3.1.1
Crypto and TLS for C&
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
 
std::unique_ptr< HashFunctioncopy_state () const override
 
template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
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 final
 
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>>
T process (const uint8_t in[], size_t length)
 
template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
T process (std::span< const uint8_t > in)
 
template<concepts::resizable_byte_buffer T = secure_vector<uint8_t>>
T process (std::string_view in)
 
std::string provider () const override
 
 SHA_256 ()
 
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_digest (secure_vector< uint32_t > &digest, 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 std::vector< std::string > providers (std::string_view 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
 

Detailed Description

SHA-256

Definition at line 45 of file sha2_32.h.

Constructor & Destructor Documentation

◆ SHA_256()

Botan::SHA_256::SHA_256 ( )
inline

Definition at line 59 of file sha2_32.h.

59: MDx_HashFunction(64, true, true), m_digest(8) { clear(); }
MDx_HashFunction(size_t block_length, bool big_byte_endian, bool big_bit_endian, uint8_t counter_size=8)
Definition: mdx_hash.cpp:19
void clear() override
Definition: sha2_32.cpp:231

References clear().

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

49 {
50 const size_t block_len = static_cast<size_t>(1) << m_block_bits;
51
52 m_count += length;
53
54 if(m_position) {
55 buffer_insert(m_buffer, m_position, input, length);
56
57 if(m_position + length >= block_len) {
58 compress_n(m_buffer.data(), 1);
59 input += (block_len - m_position);
60 length -= (block_len - m_position);
61 m_position = 0;
62 }
63 }
64
65 // Just in case the compiler can't figure out block_len is a power of 2
66 const size_t full_blocks = length >> m_block_bits;
67 const size_t remaining = length & (block_len - 1);
68
69 if(full_blocks > 0) {
70 compress_n(input, full_blocks);
71 }
72
73 buffer_insert(m_buffer, m_position, input + full_blocks * block_len, remaining);
74 m_position += remaining;
75}
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: mem_ops.h:211

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

◆ clear()

void Botan::SHA_256::clear ( )
overridevirtual

Reset the state.

Reimplemented from Botan::MDx_HashFunction.

Definition at line 231 of file sha2_32.cpp.

231 {
233 m_digest[0] = 0x6A09E667;
234 m_digest[1] = 0xBB67AE85;
235 m_digest[2] = 0x3C6EF372;
236 m_digest[3] = 0xA54FF53A;
237 m_digest[4] = 0x510E527F;
238 m_digest[5] = 0x9B05688C;
239 m_digest[6] = 0x1F83D9AB;
240 m_digest[7] = 0x5BE0CD19;
241}
void clear() override
Definition: mdx_hash.cpp:41

References Botan::MDx_HashFunction::clear().

Referenced by SHA_256().

◆ clone()

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

Definition at line 87 of file hash.h.

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

◆ compress_digest()

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

Definition at line 56 of file sha2_32.cpp.

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

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

◆ 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 49 of file sha2_32.cpp.

49 {
50 return std::make_unique<SHA_256>(*this);
51}

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

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

Referenced by botan_hash_init(), Botan::EME::create(), Botan::EMSA::create(), Botan::BlockCipher::create(), Botan::HashFunction::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(), and Botan::X942_PRF::kdf().

◆ create_or_throw()

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

◆ 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.data());
80 return output;
81 }
virtual size_t output_length() const =0
FE_25519 T
Definition: ge.cpp:34

References T.

◆ final() [2/4]

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

Definition at line 85 of file buf_comp.h.

85 {
86 BOTAN_ASSERT_NOMSG(out.size() >= output_length());
87 final_result(out.data());
88 }
#define BOTAN_ASSERT_NOMSG(expr)
Definition: assert.h:59

References BOTAN_ASSERT_NOMSG.

◆ final() [3/4]

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

Definition at line 91 of file buf_comp.h.

91 {
92 out.resize(output_length());
93 final_result(out.data());
94 }

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

Referenced by Botan::ed25519_gen_keypair(), Botan::ed25519_sign(), Botan::ed25519_verify(), Botan::mgf1_mask(), Botan::pbkdf2(), Botan::Dilithium::Polynomial::poly_challenge(), Botan::Kyber_Modern_Symmetric_Primitives::PRF(), Botan::Sphincs_Hash_Functions_Sha2::PRF_msg(), Botan::Sphincs_Hash_Functions_Shake::PRF_msg(), and Botan::sm2_compute_za().

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

80 {
81 const size_t block_len = static_cast<size_t>(1) << m_block_bits;
82
83 clear_mem(&m_buffer[m_position], block_len - m_position);
84 m_buffer[m_position] = m_pad_char;
85
86 if(m_position >= block_len - m_counter_size) {
87 compress_n(m_buffer.data(), 1);
88 zeroise(m_buffer);
89 }
90
91 BOTAN_ASSERT_NOMSG(m_counter_size <= output_length());
92 BOTAN_ASSERT_NOMSG(m_counter_size >= 8);
93
94 const uint64_t bit_count = m_count * 8;
95
96 if(m_count_big_endian) {
97 store_be(bit_count, &m_buffer[block_len - 8]);
98 } else {
99 store_le(bit_count, &m_buffer[block_len - 8]);
100 }
101
102 compress_n(m_buffer.data(), 1);
103 copy_out(output);
104 clear();
105}
virtual void copy_out(uint8_t buffer[])=0
constexpr void store_le(uint16_t in, uint8_t out[2])
Definition: loadstor.h:422
void zeroise(std::vector< T, Alloc > &vec)
Definition: secmem.h:108
constexpr void store_be(uint16_t in, uint8_t out[2])
Definition: loadstor.h:407
constexpr void clear_mem(T *ptr, size_t n)
Definition: mem_ops.h:109

References BOTAN_ASSERT_NOMSG, Botan::MDx_HashFunction::clear(), Botan::clear_mem(), Botan::MDx_HashFunction::compress_n(), Botan::MDx_HashFunction::copy_out(), Botan::Buffered_Computation::output_length(), Botan::store_be(), Botan::store_le(), and Botan::zeroise().

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

◆ 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 29 of file mdx_hash.h.

29{ return m_buffer.size(); }

◆ name()

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

Implements Botan::HashFunction.

Definition at line 47 of file sha2_32.h.

47{ return "SHA-256"; }

◆ new_object()

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

Implements Botan::HashFunction.

Definition at line 51 of file sha2_32.h.

51{ return std::make_unique<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 49 of file sha2_32.h.

49{ return 32; }

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

104 {
105 update(in, length);
106 return final<T>();
107 }
int(* update)(CTX *, const void *, CC_LONG len)

References update.

Referenced by Botan::Dilithium_Symmetric_Primitives::CRH(), and Botan::Dilithium_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 128 of file buf_comp.h.

128 {
129 update(in);
130 return final<T>();
131 }

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

116 {
117 update(in);
118 return final<T>();
119 }

References update.

◆ provider()

std::string Botan::SHA_256::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 181 of file sha2_32.cpp.

181 {
182 return sha256_provider();
183}

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

295 {
296 return probe_providers_of<HashFunction>(algo_spec, {"base", "commoncrypto"});
297}

◆ update() [1/4]

void Botan::Buffered_Computation::update ( const uint8_t  in[],
size_t  length 
)
inlineinherited

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

◆ update() [3/4]

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

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

55{ add_data(cast_char_ptr_to_uint8(str.data()), str.size()); }
const uint8_t * cast_char_ptr_to_uint8(const char *s)
Definition: mem_ops.h:177

References Botan::cast_char_ptr_to_uint8().

◆ 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 13 of file buf_comp.cpp.

13 {
14 uint8_t inb[sizeof(val)];
15 store_be(val, inb);
16 add_data(inb, sizeof(inb));
17}

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 19 of file buf_comp.cpp.

19 {
20 uint8_t inb[sizeof(val)];
21 store_be(val, inb);
22 add_data(inb, sizeof(inb));
23}

References Botan::store_be().

◆ update_be() [3/3]

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

Definition at line 25 of file buf_comp.cpp.

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

References Botan::store_be().

◆ update_le() [1/3]

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

Definition at line 31 of file buf_comp.cpp.

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

References Botan::store_le().

◆ update_le() [2/3]

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

Definition at line 37 of file buf_comp.cpp.

37 {
38 uint8_t inb[sizeof(val)];
39 store_le(val, inb);
40 add_data(inb, sizeof(inb));
41}

References Botan::store_le().

◆ update_le() [3/3]

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

Definition at line 43 of file buf_comp.cpp.

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

References Botan::store_le().


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