Botan 3.14.1
Crypto and TLS for C&
hash.cpp
Go to the documentation of this file.
1/*
2* Hash Functions
3* (C) 2015 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/hash.h>
9
10#include <botan/exceptn.h>
11#include <botan/internal/algorithm_spec.h>
12#include <botan/internal/probe_providers.h>
13
14#if defined(BOTAN_HAS_ADLER32)
15 #include <botan/internal/adler32.h>
16#endif
17
18#if defined(BOTAN_HAS_ASCON_HASH256)
19 #include <botan/internal/ascon_hash256.h>
20#endif
21
22#if defined(BOTAN_HAS_CRC24)
23 #include <botan/internal/crc24.h>
24#endif
25
26#if defined(BOTAN_HAS_CRC32)
27 #include <botan/internal/crc32.h>
28#endif
29
30#if defined(BOTAN_HAS_GOST_34_11)
31 #include <botan/internal/gost_3411.h>
32#endif
33
34#if defined(BOTAN_HAS_KECCAK)
35 #include <botan/internal/keccak.h>
36#endif
37
38#if defined(BOTAN_HAS_MD4)
39 #include <botan/internal/md4.h>
40#endif
41
42#if defined(BOTAN_HAS_MD5)
43 #include <botan/internal/md5.h>
44#endif
45
46#if defined(BOTAN_HAS_RIPEMD_160)
47 #include <botan/internal/rmd160.h>
48#endif
49
50#if defined(BOTAN_HAS_SHA1)
51 #include <botan/internal/sha1.h>
52#endif
53
54#if defined(BOTAN_HAS_SHA2_32)
55 #include <botan/internal/sha2_32.h>
56#endif
57
58#if defined(BOTAN_HAS_SHA2_64)
59 #include <botan/internal/sha2_64.h>
60#endif
61
62#if defined(BOTAN_HAS_SHA3)
63 #include <botan/internal/sha3.h>
64#endif
65
66#if defined(BOTAN_HAS_SHAKE)
67 #include <botan/internal/shake.h>
68#endif
69
70#if defined(BOTAN_HAS_SKEIN_512)
71 #include <botan/internal/skein_512.h>
72#endif
73
74#if defined(BOTAN_HAS_STREEBOG)
75 #include <botan/internal/streebog.h>
76#endif
77
78#if defined(BOTAN_HAS_SM3)
79 #include <botan/internal/sm3.h>
80#endif
81
82#if defined(BOTAN_HAS_WHIRLPOOL)
83 #include <botan/internal/whirlpool.h>
84#endif
85
86#if defined(BOTAN_HAS_PARALLEL_HASH)
87 #include <botan/internal/par_hash.h>
88#endif
89
90#if defined(BOTAN_HAS_TRUNCATED_HASH)
91 #include <botan/internal/trunc_hash.h>
92#endif
93
94#if defined(BOTAN_HAS_COMB4P)
95 #include <botan/internal/comb4p.h>
96#endif
97
98#if defined(BOTAN_HAS_BLAKE2B)
99 #include <botan/internal/blake2b.h>
100#endif
101
102#if defined(BOTAN_HAS_BLAKE2S)
103 #include <botan/internal/blake2s.h>
104#endif
105
106#if defined(BOTAN_HAS_COMMONCRYPTO)
107 #include <botan/internal/commoncrypto.h>
108#endif
109
110namespace Botan {
111
112std::unique_ptr<HashFunction> HashFunction::create(std::string_view algo_spec, std::string_view provider) {
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}
309
310//static
311std::unique_ptr<HashFunction> HashFunction::create_or_throw(std::string_view algo, std::string_view provider) {
312 if(auto hash = HashFunction::create(algo, provider)) {
313 return hash;
314 }
315 throw Lookup_Error("Hash", algo, provider);
316}
317
318std::vector<std::string> HashFunction::providers(std::string_view algo_spec) {
319 return probe_providers_of<HashFunction>(algo_spec, {"base", "commoncrypto"});
320}
321
322} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:151
std::optional< AlgorithmMatch > match(const AlgorithmPattern &pattern) const &
static std::vector< std::string > providers(std::string_view algo_spec)
Definition hash.cpp:318
virtual std::string provider() const
Definition hash.h:51
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:311
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)
std::vector< std::string > probe_providers_of(std::string_view algo_spec, const std::vector< std::string > &possible={"base"})