Botan 3.14.1
Crypto and TLS for C&
Botan::SHA_256 Class Referencefinal

#include <sha2_32.h>

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

Public Types

using digest_type = secure_vector<uint32_t>

Public Member Functions

void clear () override
HashFunction * clone () const
std::unique_ptr< HashFunction > copy_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
std::string name () const override
std::unique_ptr< HashFunction > new_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
virtual size_t security_level () const
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 (digest_type &digest, std::span< const uint8_t > input, size_t blocks)
static void compress_digest_armv8 (digest_type &digest, std::span< const uint8_t > input, size_t blocks)
static void compress_digest_x86 (digest_type &digest, std::span< const uint8_t > input, size_t blocks)
static void compress_n (digest_type &digest, std::span< const uint8_t > input, size_t blocks)
static std::unique_ptr< HashFunction > create (std::string_view algo_spec, std::string_view provider="")
static std::unique_ptr< HashFunction > create_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 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 = 32

Detailed Description

SHA-256

Definition at line 59 of file sha2_32.h.

Member Typedef Documentation

◆ digest_type

Definition at line 61 of file sha2_32.h.

Member Function Documentation

◆ clear()

void Botan::SHA_256::clear ( )
inlineoverridevirtual

Reset the state.

Implements Botan::HashFunction.

Definition at line 83 of file sha2_32.h.

83{ m_md.clear(); }

◆ clone()

HashFunction * Botan::HashFunction::clone ( ) const
inlineinherited

Create a new uninitialized object of the same type

Returns
new object representing the same algorithm as *this

Definition at line 100 of file hash.h.

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

References new_object().

◆ compress_digest()

void BOTAN_SCRUB_STACK_AFTER_RETURN Botan::SHA_256::compress_digest ( digest_type & digest,
std::span< const uint8_t > input,
size_t blocks )
static

Definition at line 57 of file sha2_32.cpp.

59 {
60#if defined(BOTAN_HAS_SHA2_32_X86)
62 return SHA_256::compress_digest_x86(digest, input, blocks);
63 }
64#endif
65
66#if defined(BOTAN_HAS_SHA2_32_ARMV8)
68 return SHA_256::compress_digest_armv8(digest, input, blocks);
69 }
70#endif
71
72#if defined(BOTAN_HAS_SHA2_32_X86_AVX2)
74 return SHA_256::compress_digest_x86_avx2(digest, input, blocks);
75 }
76#endif
77
78#if defined(BOTAN_HAS_SHA2_32_SIMD)
80 return SHA_256::compress_digest_x86_simd(digest, input, blocks);
81 }
82#endif
83
84 uint32_t A = digest[0];
85 uint32_t B = digest[1];
86 uint32_t C = digest[2];
87 uint32_t D = digest[3];
88 uint32_t E = digest[4];
89 uint32_t F = digest[5];
90 uint32_t G = digest[6];
91 uint32_t H = digest[7];
92
93 std::array<uint32_t, 16> W{};
94
95 BufferSlicer in(input);
96
97 for(size_t i = 0; i != blocks; ++i) {
98 load_be(W, in.take<block_bytes>());
99
100 // clang-format off
101
102 SHA2_32_F(A, B, C, D, E, F, G, H, W[ 0], W[14], W[ 9], W[ 1], SHA256_K[ 0]);
103 SHA2_32_F(H, A, B, C, D, E, F, G, W[ 1], W[15], W[10], W[ 2], SHA256_K[ 1]);
104 SHA2_32_F(G, H, A, B, C, D, E, F, W[ 2], W[ 0], W[11], W[ 3], SHA256_K[ 2]);
105 SHA2_32_F(F, G, H, A, B, C, D, E, W[ 3], W[ 1], W[12], W[ 4], SHA256_K[ 3]);
106 SHA2_32_F(E, F, G, H, A, B, C, D, W[ 4], W[ 2], W[13], W[ 5], SHA256_K[ 4]);
107 SHA2_32_F(D, E, F, G, H, A, B, C, W[ 5], W[ 3], W[14], W[ 6], SHA256_K[ 5]);
108 SHA2_32_F(C, D, E, F, G, H, A, B, W[ 6], W[ 4], W[15], W[ 7], SHA256_K[ 6]);
109 SHA2_32_F(B, C, D, E, F, G, H, A, W[ 7], W[ 5], W[ 0], W[ 8], SHA256_K[ 7]);
110 SHA2_32_F(A, B, C, D, E, F, G, H, W[ 8], W[ 6], W[ 1], W[ 9], SHA256_K[ 8]);
111 SHA2_32_F(H, A, B, C, D, E, F, G, W[ 9], W[ 7], W[ 2], W[10], SHA256_K[ 9]);
112 SHA2_32_F(G, H, A, B, C, D, E, F, W[10], W[ 8], W[ 3], W[11], SHA256_K[10]);
113 SHA2_32_F(F, G, H, A, B, C, D, E, W[11], W[ 9], W[ 4], W[12], SHA256_K[11]);
114 SHA2_32_F(E, F, G, H, A, B, C, D, W[12], W[10], W[ 5], W[13], SHA256_K[12]);
115 SHA2_32_F(D, E, F, G, H, A, B, C, W[13], W[11], W[ 6], W[14], SHA256_K[13]);
116 SHA2_32_F(C, D, E, F, G, H, A, B, W[14], W[12], W[ 7], W[15], SHA256_K[14]);
117 SHA2_32_F(B, C, D, E, F, G, H, A, W[15], W[13], W[ 8], W[ 0], SHA256_K[15]);
118
119 SHA2_32_F(A, B, C, D, E, F, G, H, W[ 0], W[14], W[ 9], W[ 1], SHA256_K[16]);
120 SHA2_32_F(H, A, B, C, D, E, F, G, W[ 1], W[15], W[10], W[ 2], SHA256_K[17]);
121 SHA2_32_F(G, H, A, B, C, D, E, F, W[ 2], W[ 0], W[11], W[ 3], SHA256_K[18]);
122 SHA2_32_F(F, G, H, A, B, C, D, E, W[ 3], W[ 1], W[12], W[ 4], SHA256_K[19]);
123 SHA2_32_F(E, F, G, H, A, B, C, D, W[ 4], W[ 2], W[13], W[ 5], SHA256_K[20]);
124 SHA2_32_F(D, E, F, G, H, A, B, C, W[ 5], W[ 3], W[14], W[ 6], SHA256_K[21]);
125 SHA2_32_F(C, D, E, F, G, H, A, B, W[ 6], W[ 4], W[15], W[ 7], SHA256_K[22]);
126 SHA2_32_F(B, C, D, E, F, G, H, A, W[ 7], W[ 5], W[ 0], W[ 8], SHA256_K[23]);
127 SHA2_32_F(A, B, C, D, E, F, G, H, W[ 8], W[ 6], W[ 1], W[ 9], SHA256_K[24]);
128 SHA2_32_F(H, A, B, C, D, E, F, G, W[ 9], W[ 7], W[ 2], W[10], SHA256_K[25]);
129 SHA2_32_F(G, H, A, B, C, D, E, F, W[10], W[ 8], W[ 3], W[11], SHA256_K[26]);
130 SHA2_32_F(F, G, H, A, B, C, D, E, W[11], W[ 9], W[ 4], W[12], SHA256_K[27]);
131 SHA2_32_F(E, F, G, H, A, B, C, D, W[12], W[10], W[ 5], W[13], SHA256_K[28]);
132 SHA2_32_F(D, E, F, G, H, A, B, C, W[13], W[11], W[ 6], W[14], SHA256_K[29]);
133 SHA2_32_F(C, D, E, F, G, H, A, B, W[14], W[12], W[ 7], W[15], SHA256_K[30]);
134 SHA2_32_F(B, C, D, E, F, G, H, A, W[15], W[13], W[ 8], W[ 0], SHA256_K[31]);
135
136 SHA2_32_F(A, B, C, D, E, F, G, H, W[ 0], W[14], W[ 9], W[ 1], SHA256_K[32]);
137 SHA2_32_F(H, A, B, C, D, E, F, G, W[ 1], W[15], W[10], W[ 2], SHA256_K[33]);
138 SHA2_32_F(G, H, A, B, C, D, E, F, W[ 2], W[ 0], W[11], W[ 3], SHA256_K[34]);
139 SHA2_32_F(F, G, H, A, B, C, D, E, W[ 3], W[ 1], W[12], W[ 4], SHA256_K[35]);
140 SHA2_32_F(E, F, G, H, A, B, C, D, W[ 4], W[ 2], W[13], W[ 5], SHA256_K[36]);
141 SHA2_32_F(D, E, F, G, H, A, B, C, W[ 5], W[ 3], W[14], W[ 6], SHA256_K[37]);
142 SHA2_32_F(C, D, E, F, G, H, A, B, W[ 6], W[ 4], W[15], W[ 7], SHA256_K[38]);
143 SHA2_32_F(B, C, D, E, F, G, H, A, W[ 7], W[ 5], W[ 0], W[ 8], SHA256_K[39]);
144 SHA2_32_F(A, B, C, D, E, F, G, H, W[ 8], W[ 6], W[ 1], W[ 9], SHA256_K[40]);
145 SHA2_32_F(H, A, B, C, D, E, F, G, W[ 9], W[ 7], W[ 2], W[10], SHA256_K[41]);
146 SHA2_32_F(G, H, A, B, C, D, E, F, W[10], W[ 8], W[ 3], W[11], SHA256_K[42]);
147 SHA2_32_F(F, G, H, A, B, C, D, E, W[11], W[ 9], W[ 4], W[12], SHA256_K[43]);
148 SHA2_32_F(E, F, G, H, A, B, C, D, W[12], W[10], W[ 5], W[13], SHA256_K[44]);
149 SHA2_32_F(D, E, F, G, H, A, B, C, W[13], W[11], W[ 6], W[14], SHA256_K[45]);
150 SHA2_32_F(C, D, E, F, G, H, A, B, W[14], W[12], W[ 7], W[15], SHA256_K[46]);
151 SHA2_32_F(B, C, D, E, F, G, H, A, W[15], W[13], W[ 8], W[ 0], SHA256_K[47]);
152
153 SHA2_32_F(A, B, C, D, E, F, G, H, W[ 0], W[14], W[ 9], W[ 1], SHA256_K[48]);
154 SHA2_32_F(H, A, B, C, D, E, F, G, W[ 1], W[15], W[10], W[ 2], SHA256_K[49]);
155 SHA2_32_F(G, H, A, B, C, D, E, F, W[ 2], W[ 0], W[11], W[ 3], SHA256_K[50]);
156 SHA2_32_F(F, G, H, A, B, C, D, E, W[ 3], W[ 1], W[12], W[ 4], SHA256_K[51]);
157 SHA2_32_F(E, F, G, H, A, B, C, D, W[ 4], W[ 2], W[13], W[ 5], SHA256_K[52]);
158 SHA2_32_F(D, E, F, G, H, A, B, C, W[ 5], W[ 3], W[14], W[ 6], SHA256_K[53]);
159 SHA2_32_F(C, D, E, F, G, H, A, B, W[ 6], W[ 4], W[15], W[ 7], SHA256_K[54]);
160 SHA2_32_F(B, C, D, E, F, G, H, A, W[ 7], W[ 5], W[ 0], W[ 8], SHA256_K[55]);
161 SHA2_32_F(A, B, C, D, E, F, G, H, W[ 8], W[ 6], W[ 1], W[ 9], SHA256_K[56]);
162 SHA2_32_F(H, A, B, C, D, E, F, G, W[ 9], W[ 7], W[ 2], W[10], SHA256_K[57]);
163 SHA2_32_F(G, H, A, B, C, D, E, F, W[10], W[ 8], W[ 3], W[11], SHA256_K[58]);
164 SHA2_32_F(F, G, H, A, B, C, D, E, W[11], W[ 9], W[ 4], W[12], SHA256_K[59]);
165 SHA2_32_F(E, F, G, H, A, B, C, D, W[12], W[10], W[ 5], W[13], SHA256_K[60]);
166 SHA2_32_F(D, E, F, G, H, A, B, C, W[13], W[11], W[ 6], W[14], SHA256_K[61]);
167 SHA2_32_F(C, D, E, F, G, H, A, B, W[14], W[12], W[ 7], W[15], SHA256_K[62]);
168 SHA2_32_F(B, C, D, E, F, G, H, A, W[15], W[13], W[ 8], W[ 0], SHA256_K[63]);
169
170 // clang-format on
171
172 A = (digest[0] += A);
173 B = (digest[1] += B);
174 C = (digest[2] += C);
175 D = (digest[3] += D);
176 E = (digest[4] += E);
177 F = (digest[5] += F);
178 G = (digest[6] += G);
179 H = (digest[7] += H);
180 }
181}
static bool has(CPUID::Feature feat)
Definition cpuid.h:93
static void compress_digest_x86(digest_type &digest, std::span< const uint8_t > input, size_t blocks)
static void compress_digest_armv8(digest_type &digest, std::span< const uint8_t > input, size_t blocks)
static constexpr size_t block_bytes
Definition sha2_32.h:65
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:33
constexpr uint32_t SHA256_K[64]
Definition sha2_32_f.h:19
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:504

References Botan::CPUFeature::AVX2, block_bytes, Botan::CPUFeature::BMI, BOTAN_SCRUB_STACK_AFTER_RETURN, compress_digest_armv8(), compress_digest_x86(), Botan::CPUID::has(), Botan::load_be(), Botan::CPUFeature::SHA, Botan::CPUFeature::SHA2, Botan::SHA256_K, Botan::SHA2_32_F(), Botan::CPUFeature::SIMD_4X32, and Botan::BufferSlicer::take().

Referenced by Botan::SHA_224::compress_n(), compress_n(), and Botan::create_sha2_32_mb_engine().

◆ compress_digest_armv8()

void BOTAN_FN_ISA_SHA2 BOTAN_SCRUB_STACK_AFTER_RETURN Botan::SHA_256::compress_digest_armv8 ( digest_type & digest,
std::span< const uint8_t > input,
size_t blocks )
static

Definition at line 47 of file sha2_32_armv8.cpp.

49 {
50 // Load initial values
51 SIMD_4x32 s0 = SIMD_4x32::load_le(&digest[0]); // NOLINT(*-container-data-pointer)
52 SIMD_4x32 s1 = SIMD_4x32::load_le(&digest[4]);
53
54 const uint32_t* input32 = reinterpret_cast<const uint32_t*>(input8.data());
55
56 while(blocks > 0) {
57 const auto s0_save = s0;
58 const auto s1_save = s1;
59
60 auto w0 = SIMD_4x32::load_be(input32);
61 auto w1 = SIMD_4x32::load_be(input32 + 4);
62 auto w2 = SIMD_4x32::load_be(input32 + 8);
63 auto w3 = SIMD_4x32::load_be(input32 + 12);
64
65 for(size_t r = 0; r != 48; r += 16) {
66 aarch64_sha256_update(s0, s1, w0, &SHA256_K[r]);
67 w0 = aarch64_sha256_expand_w(w0, w1, w2, w3);
68
69 aarch64_sha256_update(s0, s1, w1, &SHA256_K[r + 4 * 1]);
70 w1 = aarch64_sha256_expand_w(w1, w2, w3, w0);
71
72 aarch64_sha256_update(s0, s1, w2, &SHA256_K[r + 4 * 2]);
73 w2 = aarch64_sha256_expand_w(w2, w3, w0, w1);
74
75 aarch64_sha256_update(s0, s1, w3, &SHA256_K[r + 4 * 3]);
76 w3 = aarch64_sha256_expand_w(w3, w0, w1, w2);
77 }
78
79 aarch64_sha256_update(s0, s1, w0, &SHA256_K[4 * 12]);
80 aarch64_sha256_update(s0, s1, w1, &SHA256_K[4 * 13]);
81 aarch64_sha256_update(s0, s1, w2, &SHA256_K[4 * 14]);
82 aarch64_sha256_update(s0, s1, w3, &SHA256_K[4 * 15]);
83
84 s0 += s0_save;
85 s1 += s1_save;
86
87 input32 += 64 / 4;
88 blocks--;
89 }
90
91 s0.store_le(&digest[0]); // NOLINT(*-container-data-pointer)
92 s1.store_le(&digest[4]);
93}
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 load_be(const void *in) noexcept
Definition simd_4x32.h:194
static SIMD_4x32 BOTAN_FN_ISA_SIMD_4X32 load_le(const void *in) noexcept
Definition simd_4x32.h:183

References BOTAN_SCRUB_STACK_AFTER_RETURN, Botan::SIMD_4x32::load_be(), Botan::SIMD_4x32::load_le(), Botan::SHA256_K, and Botan::SIMD_4x32::store_le().

Referenced by compress_digest().

◆ compress_digest_x86()

void BOTAN_FN_ISA_SHANI BOTAN_SCRUB_STACK_AFTER_RETURN Botan::SHA_256::compress_digest_x86 ( digest_type & digest,
std::span< const uint8_t > input,
size_t blocks )
static

Definition at line 57 of file sha2_32_x86.cpp.

59 {
60 const uint8_t* input = input_span.data();
61
62 SIMD_4x32 S0 = SIMD_4x32::load_le(&digest[0]); // NOLINT(*container-data-pointer)
63 SIMD_4x32 S1 = SIMD_4x32::load_le(&digest[4]);
64
65 sha256_permute_state(S0, S1);
66
67 while(blocks > 0) {
68 const auto S0_SAVE = S0;
69 const auto S1_SAVE = S1;
70
71 auto W0 = SIMD_4x32::load_be(input);
72 auto W1 = SIMD_4x32::load_be(input + 16);
73 auto W2 = SIMD_4x32::load_be(input + 32);
74 auto W3 = SIMD_4x32::load_be(input + 48);
75
76 sha256_rnds4(S0, S1, W0, SIMD_4x32::load_le(&SHA256_K[0]));
77 sha256_rnds4(S0, S1, W1, SIMD_4x32::load_le(&SHA256_K[4]));
78 sha256_rnds4(S0, S1, W2, SIMD_4x32::load_le(&SHA256_K[8]));
79 sha256_rnds4(S0, S1, W3, SIMD_4x32::load_le(&SHA256_K[12]));
80
81 W0 = SIMD_4x32(_mm_sha256msg1_epu32(W0.raw(), W1.raw()));
82 W1 = SIMD_4x32(_mm_sha256msg1_epu32(W1.raw(), W2.raw()));
83
84 sha256_msg_exp(W2, W3, W0, W1);
85
86 sha256_rnds4(S0, S1, W0, SIMD_4x32::load_le(&SHA256_K[4 * 4]));
87 sha256_rnds4(S0, S1, W1, SIMD_4x32::load_le(&SHA256_K[4 * 5]));
88
89 sha256_msg_exp(W0, W1, W2, W3);
90
91 sha256_rnds4(S0, S1, W2, SIMD_4x32::load_le(&SHA256_K[4 * 6]));
92 sha256_rnds4(S0, S1, W3, SIMD_4x32::load_le(&SHA256_K[4 * 7]));
93
94 sha256_msg_exp(W2, W3, W0, W1);
95
96 sha256_rnds4(S0, S1, W0, SIMD_4x32::load_le(&SHA256_K[4 * 8]));
97 sha256_rnds4(S0, S1, W1, SIMD_4x32::load_le(&SHA256_K[4 * 9]));
98
99 sha256_msg_exp(W0, W1, W2, W3);
100
101 sha256_rnds4(S0, S1, W2, SIMD_4x32::load_le(&SHA256_K[4 * 10]));
102 sha256_rnds4(S0, S1, W3, SIMD_4x32::load_le(&SHA256_K[4 * 11]));
103
104 sha256_msg_exp(W2, W3, W0, W1);
105
106 sha256_rnds4(S0, S1, W0, SIMD_4x32::load_le(&SHA256_K[4 * 12]));
107 sha256_rnds4(S0, S1, W1, SIMD_4x32::load_le(&SHA256_K[4 * 13]));
108
109 sha256_msg_exp(W0, W1, W2, W3);
110
111 sha256_rnds4(S0, S1, W2, SIMD_4x32::load_le(&SHA256_K[4 * 14]));
112 sha256_rnds4(S0, S1, W3, SIMD_4x32::load_le(&SHA256_K[4 * 15]));
113
114 // Add values back to state
115 S0 += S0_SAVE;
116 S1 += S1_SAVE;
117
118 input += 64;
119 blocks--;
120 }
121
122 sha256_permute_state(S1, S0);
123
124 S0.store_le(&digest[0]); // NOLINT(*container-data-pointer)
125 S1.store_le(&digest[4]);
126}

References BOTAN_SCRUB_STACK_AFTER_RETURN, Botan::SIMD_4x32::load_be(), Botan::SIMD_4x32::load_le(), Botan::SHA256_K, and Botan::SIMD_4x32::store_le().

Referenced by compress_digest().

◆ compress_n()

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

Definition at line 215 of file sha2_32.cpp.

215 {
216 SHA_256::compress_digest(digest, input, blocks);
217}
static void compress_digest(digest_type &digest, std::span< const uint8_t > input, size_t blocks)
Definition sha2_32.cpp:57

References compress_digest().

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

227 {
228 return std::make_unique<SHA_256>(*this);
229}

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

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

References BOTAN_UNUSED, create(), Botan::make_commoncrypto_hash(), Botan::AlgorithmSpec::match(), and provider().

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

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

311 {
312 if(auto hash = HashFunction::create(algo, provider)) {
313 return hash;
314 }
315 throw Lookup_Error("Hash", algo, provider);
316}

References create(), and provider().

Referenced by botan_pubkey_fingerprint(), botan_pubkey_sm2_compute_za(), Botan::OCSP::CertID::CertID(), Botan::TLS::Client_Hello_Internal::Client_Hello_Internal(), Botan::SPAKE2p::SystemParameters::confirmation_size(), Botan::Kyber_90s_Symmetric_Primitives::create_G(), Botan::Kyber_Modern_Symmetric_Primitives::create_G(), Botan::ML_KEM_Symmetric_Primitives::create_G(), Botan::Kyber_90s_Symmetric_Primitives::create_H(), Botan::Kyber_Modern_Symmetric_Primitives::create_H(), Botan::ML_KEM_Symmetric_Primitives::create_H(), Botan::create_hex_fingerprint(), Botan::ML_KEM_Symmetric_Primitives::create_J(), Botan::Kyber_90s_Symmetric_Primitives::create_KDF(), Botan::Kyber_Modern_Symmetric_Primitives::create_KDF(), Botan::Sodium::crypto_hash_sha256(), Botan::Sodium::crypto_hash_sha512(), Botan::TLS::PSKImporter::derive_imported_psk(), Botan::Bcrypt_PBKDF::derive_key(), Botan::PKCS12::export_to(), Botan::TLS::Handshake_Hash::final(), Botan::X509_Certificate_Cache::find_or_insert(), Botan::generate_dsa_primes(), Botan::h2c_expand_message(), Botan::LMOTS_Params::hash(), Botan::LMS_Params::hash(), Botan::Classic_McEliece_Parameters::hash_func(), Botan::Stateful_Key_Index_Registry::KeyId::KeyId(), Botan::TLS::make_hello_random(), Botan::Roughtime::nonce_from_blind(), Botan::OAEP::OAEP(), 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(), 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.

Returns
a contiguous container holding the result

Definition at line 104 of file buf_comp.h.

104 {
105 T output(output_length());
106 final_result(output);
107 return output;
108 }
virtual size_t output_length() const =0

References output_length().

◆ final() [2/4]

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

Complete the computation and retrieve the final result

Parameters
outthe buffer to write the result to, must be output_length() bytes

Definition at line 54 of file buf_comp.cpp.

54 {
55 BOTAN_ARG_CHECK(out.size() >= output_length(), "Output buffer has insufficient capacity");
56 // Pass exactly output_length() bytes so that an oversized buffer has the
57 // result written to its leading bytes with the remainder left untouched.
58 final_result(out.first(output_length()));
59}
#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

Complete the computation and retrieve the final result

Parameters
outa container which is resized to hold the result

Definition at line 127 of file buf_comp.h.

127 {
128 out.resize(output_length());
129 final_result(out);
130 }

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, which must be of length output_length()

Definition at line 97 of file buf_comp.h.

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

References output_length().

Referenced by Botan::expand_message_xmd(), 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(), and Botan::TPM2::Signature_Operation::sign().

◆ final_stdvec()

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

Complete the computation and retrieve the final result

Returns
a std::vector holding the result

Definition at line 114 of file buf_comp.h.

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

References final().

Referenced by Botan::expand_message_xmd().

◆ hash_block_size()

size_t Botan::SHA_256::hash_block_size ( ) const
inlineoverridevirtual

Return the internal block size of this hash function

Returns
hash block size as defined for this algorithm

Reimplemented from Botan::HashFunction.

Definition at line 77 of file sha2_32.h.

77{ return block_bytes; }

References block_bytes.

◆ init()

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

Definition at line 219 of file sha2_32.cpp.

219 {
220 digest.assign({0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19});
221}

Referenced by Botan::create_sha2_32_mb_engine().

◆ name()

std::string Botan::SHA_256::name ( ) const
inlineoverridevirtual

Return the name of this hash function

Returns
the hash function name

Implements Botan::HashFunction.

Definition at line 73 of file sha2_32.h.

73{ return "SHA-256"; }

◆ new_object()

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

Create a new uninitialized object of the same type

Returns
new object representing the same algorithm as *this

Implements Botan::HashFunction.

Definition at line 223 of file sha2_32.cpp.

223 {
224 return std::make_unique<SHA_256>();
225}

◆ output_length()

size_t Botan::SHA_256::output_length ( ) const
inlineoverridevirtual

Return the output length of this function

Returns
length of the output of this function in bytes

Implements Botan::Buffered_Computation.

Definition at line 75 of file sha2_32.h.

75{ return output_bytes; }
static constexpr size_t output_bytes
Definition sha2_32.h:66

References output_bytes.

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

140 {
141 update(in, length);
142 return final<T>();
143 }
void update(const uint8_t in[], size_t length)
Definition buf_comp.h:35

References final(), and update().

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

164 {
165 update(in);
166 return final<T>();
167 }

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

152 {
153 update(in);
154 return final<T>();
155 }

References final(), and update().

◆ provider()

std::string Botan::SHA_256::provider ( ) const
overridevirtual

Return the name of the provider implementing this object

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

211 {
212 return sha256_provider();
213}

◆ providers()

std::vector< std::string > Botan::HashFunction::providers ( std::string_view algo_spec)
staticinherited

List the providers available for a given hash

Returns
list of available providers for this algorithm, empty if not available
Parameters
algo_specalgorithm name

Definition at line 318 of file hash.cpp.

318 {
319 return probe_providers_of<HashFunction>(algo_spec, {"base", "commoncrypto"});
320}
std::vector< std::string > probe_providers_of(std::string_view algo_spec, const std::vector< std::string > &possible={"base"})

References Botan::probe_providers_of().

◆ security_level()

virtual size_t Botan::HashFunction::security_level ( ) const
inlinevirtualinherited

Return an estimate, in bits, of the security level of this hash function, with respect to collision resistance. For most hashes this is simply half the output length, matching the generic birthday attack. It is lower for hash functions with a known collision attack, and zero for checksums and any hash where finding collisions is trivial.

Reimplemented in Botan::Adler32, Botan::Comb4P, Botan::CRC24, Botan::CRC32, Botan::GOST_34_11, Botan::MD4, Botan::MD5, Botan::Parallel, Botan::SHA_1, Botan::SHAKE_128, Botan::SHAKE_256, Botan::TPM2::HashFunction, and Botan::Truncated_Hash.

Definition at line 77 of file hash.h.

77{ return 4 * output_length(); }

References Botan::Buffered_Computation::output_length().

Referenced by Botan::TPM2::HashFunction::security_level().

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

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

Referenced by Botan::expand_message_xmd(), Botan::PseudorandomKeyGeneration::gen(), Botan::mgf1_mask(), Botan::pbkdf2(), Botan::Sphincs_Hash_Functions_Sha2::PRF_msg(), process(), process(), and process().

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

41{ 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:59

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

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

◆ update_be() [1/3]

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

Add new input to process, encoded as a big-endian integer

Parameters
valthe value to process

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

◆ update_be() [2/3]

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

Add new input to process, encoded as a big-endian integer

Parameters
valthe value to process

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

Add new input to process, encoded as a big-endian integer

Parameters
valthe value to process

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

Add new input to process, encoded as a little-endian integer

Parameters
valthe value to process

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

Add new input to process, encoded as a little-endian integer

Parameters
valthe value to process

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

Add new input to process, encoded as a little-endian integer

Parameters
valthe value to process

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_256::bit_endianness = MD_Endian::Big
staticconstexpr

Definition at line 64 of file sha2_32.h.

◆ block_bytes

size_t Botan::SHA_256::block_bytes = 64
staticconstexpr

Definition at line 65 of file sha2_32.h.

Referenced by compress_digest(), Botan::create_sha2_32_mb_engine(), and hash_block_size().

◆ byte_endianness

MD_Endian Botan::SHA_256::byte_endianness = MD_Endian::Big
staticconstexpr

Definition at line 63 of file sha2_32.h.

◆ ctr_bytes

size_t Botan::SHA_256::ctr_bytes = 8
staticconstexpr

Definition at line 67 of file sha2_32.h.

Referenced by Botan::create_sha2_32_mb_engine().

◆ output_bytes

size_t Botan::SHA_256::output_bytes = 32
staticconstexpr

Definition at line 66 of file sha2_32.h.

Referenced by output_length().


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