Coverage Report

Created: 2025-08-29 06:23

/src/Botan-3.4.0/src/lib/hash/hash.cpp
Line
Count
Source (jump to first uncovered line)
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/scan_name.h>
12
13
#if defined(BOTAN_HAS_ADLER32)
14
   #include <botan/internal/adler32.h>
15
#endif
16
17
#if defined(BOTAN_HAS_CRC24)
18
   #include <botan/internal/crc24.h>
19
#endif
20
21
#if defined(BOTAN_HAS_CRC32)
22
   #include <botan/internal/crc32.h>
23
#endif
24
25
#if defined(BOTAN_HAS_GOST_34_11)
26
   #include <botan/internal/gost_3411.h>
27
#endif
28
29
#if defined(BOTAN_HAS_KECCAK)
30
   #include <botan/internal/keccak.h>
31
#endif
32
33
#if defined(BOTAN_HAS_MD4)
34
   #include <botan/internal/md4.h>
35
#endif
36
37
#if defined(BOTAN_HAS_MD5)
38
   #include <botan/internal/md5.h>
39
#endif
40
41
#if defined(BOTAN_HAS_RIPEMD_160)
42
   #include <botan/internal/rmd160.h>
43
#endif
44
45
#if defined(BOTAN_HAS_SHA1)
46
   #include <botan/internal/sha1.h>
47
#endif
48
49
#if defined(BOTAN_HAS_SHA2_32)
50
   #include <botan/internal/sha2_32.h>
51
#endif
52
53
#if defined(BOTAN_HAS_SHA2_64)
54
   #include <botan/internal/sha2_64.h>
55
#endif
56
57
#if defined(BOTAN_HAS_SHA3)
58
   #include <botan/internal/sha3.h>
59
#endif
60
61
#if defined(BOTAN_HAS_SHAKE)
62
   #include <botan/internal/shake.h>
63
#endif
64
65
#if defined(BOTAN_HAS_SKEIN_512)
66
   #include <botan/internal/skein_512.h>
67
#endif
68
69
#if defined(BOTAN_HAS_STREEBOG)
70
   #include <botan/internal/streebog.h>
71
#endif
72
73
#if defined(BOTAN_HAS_SM3)
74
   #include <botan/internal/sm3.h>
75
#endif
76
77
#if defined(BOTAN_HAS_WHIRLPOOL)
78
   #include <botan/internal/whirlpool.h>
79
#endif
80
81
#if defined(BOTAN_HAS_PARALLEL_HASH)
82
   #include <botan/internal/par_hash.h>
83
#endif
84
85
#if defined(BOTAN_HAS_TRUNCATED_HASH)
86
   #include <botan/internal/trunc_hash.h>
87
#endif
88
89
#if defined(BOTAN_HAS_COMB4P)
90
   #include <botan/internal/comb4p.h>
91
#endif
92
93
#if defined(BOTAN_HAS_BLAKE2B)
94
   #include <botan/internal/blake2b.h>
95
#endif
96
97
#if defined(BOTAN_HAS_BLAKE2S)
98
   #include <botan/internal/blake2s.h>
99
#endif
100
101
#if defined(BOTAN_HAS_COMMONCRYPTO)
102
   #include <botan/internal/commoncrypto.h>
103
#endif
104
105
namespace Botan {
106
107
743k
std::unique_ptr<HashFunction> HashFunction::create(std::string_view algo_spec, std::string_view provider) {
108
#if defined(BOTAN_HAS_COMMONCRYPTO)
109
   if(provider.empty() || provider == "commoncrypto") {
110
      if(auto hash = make_commoncrypto_hash(algo_spec))
111
         return hash;
112
113
      if(!provider.empty())
114
         return nullptr;
115
   }
116
#endif
117
118
743k
   if(provider.empty() == false && provider != "base") {
119
0
      return nullptr;  // unknown provider
120
0
   }
121
122
743k
#if defined(BOTAN_HAS_SHA1)
123
743k
   if(algo_spec == "SHA-1") {
124
9.98k
      return std::make_unique<SHA_1>();
125
9.98k
   }
126
733k
#endif
127
128
733k
#if defined(BOTAN_HAS_SHA2_32)
129
733k
   if(algo_spec == "SHA-224") {
130
17.5k
      return std::make_unique<SHA_224>();
131
17.5k
   }
132
133
716k
   if(algo_spec == "SHA-256") {
134
93.9k
      return std::make_unique<SHA_256>();
135
93.9k
   }
136
622k
#endif
137
138
622k
#if defined(BOTAN_HAS_SHA2_64)
139
622k
   if(algo_spec == "SHA-384") {
140
43.6k
      return std::make_unique<SHA_384>();
141
43.6k
   }
142
143
578k
   if(algo_spec == "SHA-512") {
144
288k
      return std::make_unique<SHA_512>();
145
288k
   }
146
147
289k
   if(algo_spec == "SHA-512-256") {
148
0
      return std::make_unique<SHA_512_256>();
149
0
   }
150
289k
#endif
151
152
289k
#if defined(BOTAN_HAS_RIPEMD_160)
153
289k
   if(algo_spec == "RIPEMD-160") {
154
12.7k
      return std::make_unique<RIPEMD_160>();
155
12.7k
   }
156
277k
#endif
157
158
#if defined(BOTAN_HAS_WHIRLPOOL)
159
   if(algo_spec == "Whirlpool") {
160
      return std::make_unique<Whirlpool>();
161
   }
162
#endif
163
164
277k
#if defined(BOTAN_HAS_MD5)
165
277k
   if(algo_spec == "MD5") {
166
54.8k
      return std::make_unique<MD5>();
167
54.8k
   }
168
222k
#endif
169
170
#if defined(BOTAN_HAS_MD4)
171
   if(algo_spec == "MD4") {
172
      return std::make_unique<MD4>();
173
   }
174
#endif
175
176
#if defined(BOTAN_HAS_GOST_34_11)
177
   if(algo_spec == "GOST-R-34.11-94" || algo_spec == "GOST-34.11") {
178
      return std::make_unique<GOST_34_11>();
179
   }
180
#endif
181
182
#if defined(BOTAN_HAS_ADLER32)
183
   if(algo_spec == "Adler32") {
184
      return std::make_unique<Adler32>();
185
   }
186
#endif
187
188
222k
#if defined(BOTAN_HAS_CRC24)
189
222k
   if(algo_spec == "CRC24") {
190
99.2k
      return std::make_unique<CRC24>();
191
99.2k
   }
192
122k
#endif
193
194
#if defined(BOTAN_HAS_CRC32)
195
   if(algo_spec == "CRC32") {
196
      return std::make_unique<CRC32>();
197
   }
198
#endif
199
200
#if defined(BOTAN_HAS_STREEBOG)
201
   if(algo_spec == "Streebog-256") {
202
      return std::make_unique<Streebog>(256);
203
   }
204
   if(algo_spec == "Streebog-512") {
205
      return std::make_unique<Streebog>(512);
206
   }
207
#endif
208
209
122k
#if defined(BOTAN_HAS_SM3)
210
122k
   if(algo_spec == "SM3") {
211
10.4k
      return std::make_unique<SM3>();
212
10.4k
   }
213
112k
#endif
214
215
112k
   const SCAN_Name req(algo_spec);
216
217
#if defined(BOTAN_HAS_SKEIN_512)
218
   if(req.algo_name() == "Skein-512") {
219
      return std::make_unique<Skein_512>(req.arg_as_integer(0, 512), req.arg(1, ""));
220
   }
221
#endif
222
223
#if defined(BOTAN_HAS_BLAKE2B)
224
   if(req.algo_name() == "Blake2b" || req.algo_name() == "BLAKE2b") {
225
      return std::make_unique<BLAKE2b>(req.arg_as_integer(0, 512));
226
   }
227
#endif
228
229
#if defined(BOTAN_HAS_BLAKE2S)
230
   if(req.algo_name() == "Blake2s" || req.algo_name() == "BLAKE2s") {
231
      return std::make_unique<BLAKE2s>(req.arg_as_integer(0, 256));
232
   }
233
#endif
234
235
#if defined(BOTAN_HAS_KECCAK)
236
   if(req.algo_name() == "Keccak-1600") {
237
      return std::make_unique<Keccak_1600>(req.arg_as_integer(0, 512));
238
   }
239
#endif
240
241
112k
#if defined(BOTAN_HAS_SHA3)
242
112k
   if(req.algo_name() == "SHA-3") {
243
39.3k
      return std::make_unique<SHA_3>(req.arg_as_integer(0, 512));
244
39.3k
   }
245
73.1k
#endif
246
247
73.1k
#if defined(BOTAN_HAS_SHAKE)
248
73.1k
   if(req.algo_name() == "SHAKE-128" && req.arg_count() == 1) {
249
0
      return std::make_unique<SHAKE_128>(req.arg_as_integer(0));
250
0
   }
251
73.1k
   if(req.algo_name() == "SHAKE-256" && req.arg_count() == 1) {
252
0
      return std::make_unique<SHAKE_256>(req.arg_as_integer(0));
253
0
   }
254
73.1k
#endif
255
256
#if defined(BOTAN_HAS_PARALLEL_HASH)
257
   if(req.algo_name() == "Parallel") {
258
      std::vector<std::unique_ptr<HashFunction>> hashes;
259
260
      for(size_t i = 0; i != req.arg_count(); ++i) {
261
         auto h = HashFunction::create(req.arg(i));
262
         if(!h) {
263
            return nullptr;
264
         }
265
         hashes.push_back(std::move(h));
266
      }
267
268
      return std::make_unique<Parallel>(hashes);
269
   }
270
#endif
271
272
73.1k
#if defined(BOTAN_HAS_TRUNCATED_HASH)
273
73.1k
   if(req.algo_name() == "Truncated" && req.arg_count() == 2) {
274
0
      auto hash = HashFunction::create(req.arg(0));
275
0
      if(!hash) {
276
0
         return nullptr;
277
0
      }
278
279
0
      return std::make_unique<Truncated_Hash>(std::move(hash), req.arg_as_integer(1));
280
0
   }
281
73.1k
#endif
282
283
#if defined(BOTAN_HAS_COMB4P)
284
   if(req.algo_name() == "Comb4P" && req.arg_count() == 2) {
285
      auto h1 = HashFunction::create(req.arg(0));
286
      auto h2 = HashFunction::create(req.arg(1));
287
288
      if(h1 && h2) {
289
         return std::make_unique<Comb4P>(std::move(h1), std::move(h2));
290
      }
291
   }
292
#endif
293
294
73.1k
   return nullptr;
295
73.1k
}
296
297
//static
298
57.4k
std::unique_ptr<HashFunction> HashFunction::create_or_throw(std::string_view algo, std::string_view provider) {
299
57.4k
   if(auto hash = HashFunction::create(algo, provider)) {
300
57.4k
      return hash;
301
57.4k
   }
302
0
   throw Lookup_Error("Hash", algo, provider);
303
57.4k
}
304
305
0
std::vector<std::string> HashFunction::providers(std::string_view algo_spec) {
306
0
   return probe_providers_of<HashFunction>(algo_spec, {"base", "commoncrypto"});
307
0
}
308
309
}  // namespace Botan