/src/cryptofuzz/modules/botan/module.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #include "module.h" |
2 | | #include <cryptofuzz/util.h> |
3 | | #include <cryptofuzz/repository.h> |
4 | | #include <botan/aead.h> |
5 | | #include <botan/ber_dec.h> |
6 | | #include <botan/bigint.h> |
7 | | #include <botan/cipher_mode.h> |
8 | | #include <botan/curve25519.h> |
9 | | #include <botan/dh.h> |
10 | | #include <botan/dl_group.h> |
11 | | #include <botan/dsa.h> |
12 | | #include <botan/ecdsa.h> |
13 | | #include <botan/ecgdsa.h> |
14 | | #include <botan/ed25519.h> |
15 | | #include <botan/hash.h> |
16 | | #include <botan/kdf.h> |
17 | | #include <botan/mac.h> |
18 | | #include <botan/pubkey.h> |
19 | | #include <botan/pwdhash.h> |
20 | | #include <botan/system_rng.h> |
21 | | #include "bn_ops.h" |
22 | | |
23 | | namespace cryptofuzz { |
24 | | namespace module { |
25 | | |
26 | | Botan::Botan(void) : |
27 | 2 | Module("Botan") { |
28 | 2 | if ( setenv("BOTAN_MLOCK_POOL_SIZE", "0", 1) != 0 ) { |
29 | 0 | abort(); |
30 | 0 | } |
31 | | |
32 | | /* Add a few curves */ |
33 | | |
34 | 2 | { |
35 | 2 | const ::Botan::OID secp112r1_oid("1.3.132.0.6"); |
36 | 2 | const ::Botan::EC_Group secp112r1( |
37 | 2 | ::Botan::BigInt("4451685225093714772084598273548427"), |
38 | 2 | ::Botan::BigInt("4451685225093714772084598273548424"), |
39 | 2 | ::Botan::BigInt("2061118396808653202902996166388514"), |
40 | 2 | ::Botan::BigInt("188281465057972534892223778713752"), |
41 | 2 | ::Botan::BigInt("3419875491033170827167861896082688"), |
42 | 2 | ::Botan::BigInt("4451685225093714776491891542548933"), |
43 | 2 | 1, |
44 | 2 | secp112r1_oid); |
45 | 2 | ::Botan::OID::register_oid(secp112r1_oid, "secp112r1"); |
46 | 2 | } |
47 | | |
48 | 2 | { |
49 | 2 | const ::Botan::OID secp112r2_oid("1.3.132.0.7"); |
50 | 2 | const ::Botan::EC_Group secp112r2( |
51 | 2 | ::Botan::BigInt("4451685225093714772084598273548427"), |
52 | 2 | ::Botan::BigInt("1970543761890640310119143205433388"), |
53 | 2 | ::Botan::BigInt("1660538572255285715897238774208265"), |
54 | 2 | ::Botan::BigInt("1534098225527667214992304222930499"), |
55 | 2 | ::Botan::BigInt("3525120595527770847583704454622871"), |
56 | 2 | ::Botan::BigInt("1112921306273428674967732714786891"), |
57 | 2 | 4, |
58 | 2 | secp112r2_oid); |
59 | 2 | ::Botan::OID::register_oid(secp112r2_oid, "secp112r2"); |
60 | 2 | } |
61 | | |
62 | 2 | { |
63 | 2 | const ::Botan::OID secp128r1_oid("1.3.132.0.28"); |
64 | 2 | const ::Botan::EC_Group secp128r1( |
65 | 2 | ::Botan::BigInt("340282366762482138434845932244680310783"), |
66 | 2 | ::Botan::BigInt("340282366762482138434845932244680310780"), |
67 | 2 | ::Botan::BigInt("308990863222245658030922601041482374867"), |
68 | 2 | ::Botan::BigInt("29408993404948928992877151431649155974"), |
69 | 2 | ::Botan::BigInt("275621562871047521857442314737465260675"), |
70 | 2 | ::Botan::BigInt("340282366762482138443322565580356624661"), |
71 | 2 | 1, |
72 | 2 | secp128r1_oid); |
73 | 2 | ::Botan::OID::register_oid(secp128r1_oid, "secp128r1"); |
74 | 2 | } |
75 | | |
76 | 2 | { |
77 | 2 | const ::Botan::OID secp128r2_oid("1.3.132.0.29"); |
78 | 2 | const ::Botan::EC_Group secp128r2( |
79 | 2 | ::Botan::BigInt("340282366762482138434845932244680310783"), |
80 | 2 | ::Botan::BigInt("284470887156368047300405921324061011681"), |
81 | 2 | ::Botan::BigInt("126188322377389722996253562430093625949"), |
82 | 2 | ::Botan::BigInt("164048790688614013222215505581242564928"), |
83 | 2 | ::Botan::BigInt("52787839253935625605232456597451787076"), |
84 | 2 | ::Botan::BigInt("85070591690620534603955721926813660579"), |
85 | 2 | 4, |
86 | 2 | secp128r2_oid); |
87 | 2 | ::Botan::OID::register_oid(secp128r2_oid, "secp128r2"); |
88 | 2 | } |
89 | 2 | } |
90 | | |
91 | | #if !defined(CRYPTOFUZZ_BOTAN_IS_ORACLE) |
92 | 4.40k | #define BOTAN_FUZZER_RNG Botan_detail::Fuzzer_RNG rng(ds); |
93 | | #else |
94 | | #define BOTAN_FUZZER_RNG ::Botan::System_RNG rng; |
95 | | #endif /* CRYPTOFUZZ_BOTAN_IS_ORACLE */ |
96 | | |
97 | | #if !defined(CRYPTOFUZZ_BOTAN_IS_ORACLE) |
98 | 23.0k | #define BOTAN_SET_GLOBAL_DS CF_NORET(util::SetGlobalDs(&ds)); |
99 | 23.0k | #define BOTAN_UNSET_GLOBAL_DS CF_NORET(util::UnsetGlobalDs()); |
100 | | #else |
101 | | #define BOTAN_SET_GLOBAL_DS |
102 | | #define BOTAN_UNSET_GLOBAL_DS |
103 | | #endif |
104 | | |
105 | | namespace Botan_detail { |
106 | | |
107 | | #if !defined(CRYPTOFUZZ_BOTAN_IS_ORACLE) |
108 | | class Fuzzer_RNG final : public ::Botan::RandomNumberGenerator { |
109 | | private: |
110 | | Datasource& ds; |
111 | | public: |
112 | | Fuzzer_RNG(Datasource& ds) : |
113 | | ds(ds) |
114 | 4.40k | { } |
115 | | |
116 | 1.51k | bool is_seeded() const override { return true; } |
117 | | |
118 | 0 | bool accepts_input() const override { return false; } |
119 | | |
120 | 0 | void clear() override {} |
121 | | |
122 | | virtual void fill_bytes_with_input( |
123 | | std::span<uint8_t> output, |
124 | 4.36k | std::span<const uint8_t> input) override { |
125 | 4.36k | (void)input; |
126 | | |
127 | 4.36k | if ( output.empty() ) { |
128 | 0 | return; |
129 | 0 | } |
130 | | |
131 | 4.36k | const auto data = ds.GetData(0, output.size(), output.size()); |
132 | | |
133 | 4.36k | std::copy(data.begin(), data.end(), output.begin()); |
134 | 4.36k | } |
135 | | |
136 | 0 | std::string name() const override { return "Fuzzer_RNG"; } |
137 | | }; |
138 | | #endif /* CRYPTOFUZZ_BOTAN_IS_ORACLE */ |
139 | | |
140 | 13.3k | const std::string parenthesize(const std::string parent, const std::string child) { |
141 | 13.3k | static const std::string pOpen("("); |
142 | 13.3k | static const std::string pClose(")"); |
143 | | |
144 | 13.3k | return parent + pOpen + child + pClose; |
145 | 13.3k | } |
146 | | |
147 | 9.21k | std::optional<std::string> DigestIDToString(const uint64_t digestType, const bool altShake = false, const bool isHmac = false) { |
148 | 9.21k | #include "digest_string_lut.h" |
149 | 9.21k | std::optional<std::string> ret = std::nullopt; |
150 | | |
151 | 9.21k | CF_CHECK_NE(LUT.find(digestType), LUT.end()); |
152 | | |
153 | 6.47k | if ( isHmac == false ) { |
154 | 5.81k | if ( digestType == CF_DIGEST("SIPHASH64") || |
155 | 5.81k | digestType == CF_DIGEST("SIPHASH128") ) { |
156 | 62 | return std::nullopt; |
157 | 62 | } |
158 | 5.81k | } |
159 | 6.41k | if ( altShake == true && digestType == CF_DIGEST("SHAKE128") ) { |
160 | 94 | ret = "SHAKE-128(256)"; |
161 | 6.32k | } else if ( altShake == true && digestType == CF_DIGEST("SHAKE256") ) { |
162 | 93 | ret = "SHAKE-256(512)"; |
163 | 6.22k | } else if ( altShake == true && digestType == CF_DIGEST("SHAKE256_114") ) { |
164 | 0 | ret = "SHAKE-256(912)"; /* 114 bytes * 8 = 912 bits */ |
165 | 6.22k | } else { |
166 | 6.22k | ret = LUT.at(digestType); |
167 | 6.22k | } |
168 | 9.14k | end: |
169 | 9.14k | return ret; |
170 | 6.41k | } |
171 | | |
172 | | } /* namespace Botan_detail */ |
173 | | |
174 | 2.24k | std::optional<component::Digest> Botan::OpDigest(operation::Digest& op) { |
175 | 2.24k | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
176 | 2.24k | std::optional<component::Digest> ret = std::nullopt; |
177 | 2.24k | std::unique_ptr<::Botan::HashFunction> hash = nullptr; |
178 | 2.24k | util::Multipart parts; |
179 | 2.24k | size_t numClears = 0; |
180 | | |
181 | | /* Initialize */ |
182 | 2.24k | { |
183 | 2.24k | BOTAN_SET_GLOBAL_DS |
184 | | |
185 | 2.24k | std::optional<std::string> algoString; |
186 | 2.24k | CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.digestType.Get()), std::nullopt); |
187 | 919 | CF_CHECK_NE(hash = ::Botan::HashFunction::create(*algoString), nullptr); |
188 | | |
189 | 919 | parts = util::ToParts(ds, op.cleartext); |
190 | 919 | } |
191 | | |
192 | 1.47k | again: |
193 | | /* Process */ |
194 | 58.2k | for (const auto& part : parts) { |
195 | 58.2k | hash->update(part.first, part.second); |
196 | 58.2k | bool clear = false; |
197 | | |
198 | 58.2k | if ( numClears < 3 ) { |
199 | 58.0k | try { |
200 | 58.0k | #if !defined(CRYPTOFUZZ_BOTAN_IS_ORACLE) |
201 | 58.0k | clear = ds.Get<bool>(); |
202 | 58.0k | #endif /* CRYPTOFUZZ_BOTAN_IS_ORACLE */ |
203 | 58.0k | } catch ( ... ) { } |
204 | 58.0k | } |
205 | | |
206 | 58.2k | if ( clear == true ) { |
207 | 551 | hash->clear(); |
208 | 551 | numClears++; |
209 | 551 | goto again; |
210 | 551 | } |
211 | 58.2k | } |
212 | | |
213 | | /* Finalize */ |
214 | 919 | { |
215 | 919 | const auto res = hash->final(); |
216 | 919 | ret = component::Digest(res.data(), res.size()); |
217 | 919 | } |
218 | | |
219 | 2.24k | end: |
220 | 2.24k | BOTAN_UNSET_GLOBAL_DS |
221 | | |
222 | 2.24k | return ret; |
223 | 919 | } |
224 | | |
225 | 784 | std::optional<component::MAC> Botan::OpHMAC(operation::HMAC& op) { |
226 | 784 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
227 | 784 | std::optional<component::MAC> ret = std::nullopt; |
228 | 784 | std::unique_ptr<::Botan::MessageAuthenticationCode> hmac = nullptr; |
229 | 784 | util::Multipart parts; |
230 | | |
231 | 784 | try { |
232 | | /* Initialize */ |
233 | 784 | { |
234 | 784 | BOTAN_SET_GLOBAL_DS |
235 | | |
236 | 784 | std::optional<std::string> algoString; |
237 | 784 | CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.digestType.Get(), true, true), std::nullopt); |
238 | | |
239 | 663 | std::string hmacString; |
240 | 663 | if ( |
241 | 663 | op.digestType.Is(CF_DIGEST("SIPHASH64")) || |
242 | 663 | op.digestType.Is(CF_DIGEST("BLAKE2B_MAC")) ) { |
243 | 289 | hmacString = *algoString; |
244 | 374 | } else { |
245 | 374 | hmacString = Botan_detail::parenthesize("HMAC", *algoString); |
246 | 374 | } |
247 | | |
248 | 663 | CF_CHECK_NE(hmac = ::Botan::MessageAuthenticationCode::create(hmacString), nullptr); |
249 | | |
250 | 663 | try { |
251 | 663 | hmac->set_key(op.cipher.key.GetPtr(), op.cipher.key.GetSize()); |
252 | 663 | } catch ( ... ) { |
253 | 98 | goto end; |
254 | 98 | } |
255 | | |
256 | 516 | parts = util::ToParts(ds, op.cleartext); |
257 | 516 | } |
258 | | |
259 | | /* Process */ |
260 | 15.5k | for (const auto& part : parts) { |
261 | 15.5k | hmac->update(part.first, part.second); |
262 | 15.5k | } |
263 | | |
264 | | /* Finalize */ |
265 | 516 | { |
266 | 516 | const auto res = hmac->final(); |
267 | 516 | ret = component::MAC(res.data(), res.size()); |
268 | 516 | } |
269 | | |
270 | 516 | } catch ( ... ) { } |
271 | | |
272 | 784 | end: |
273 | 784 | BOTAN_UNSET_GLOBAL_DS |
274 | | |
275 | 784 | return ret; |
276 | 784 | } |
277 | | |
278 | | namespace Botan_detail { |
279 | | |
280 | 10.6k | std::optional<std::string> CipherIDToString(const uint64_t digestType, const bool withMode = true) { |
281 | 10.6k | #include "cipher_string_lut.h" |
282 | 10.6k | std::optional<std::string> ret = std::nullopt; |
283 | | |
284 | 10.6k | CF_CHECK_NE(LUT.find(digestType), LUT.end()); |
285 | 7.80k | ret = withMode ? LUT.at(digestType).first : LUT.at(digestType).second; |
286 | 10.6k | end: |
287 | 10.6k | return ret; |
288 | 7.80k | } |
289 | | |
290 | | template <class OperationType> |
291 | | const uint8_t* GetInPtr(const OperationType& op); |
292 | | |
293 | | template <> |
294 | 5.40k | const uint8_t* GetInPtr(const operation::SymmetricEncrypt& op) { |
295 | 5.40k | return op.cleartext.GetPtr(); |
296 | 5.40k | } |
297 | | |
298 | | template <> |
299 | 4.46k | const uint8_t* GetInPtr(const operation::SymmetricDecrypt& op) { |
300 | 4.46k | return op.ciphertext.GetPtr(); |
301 | 4.46k | } |
302 | | |
303 | | template <class OperationType> |
304 | | size_t GetInSize(const OperationType& op); |
305 | | |
306 | | template <> |
307 | 5.40k | size_t GetInSize(const operation::SymmetricEncrypt& op) { |
308 | 5.40k | return op.cleartext.GetSize(); |
309 | 5.40k | } |
310 | | |
311 | | template <> |
312 | 6.12k | size_t GetInSize(const operation::SymmetricDecrypt& op) { |
313 | 6.12k | return op.ciphertext.GetSize(); |
314 | 6.12k | } |
315 | | |
316 | | template <class OperationType> |
317 | | ::Botan::Cipher_Dir GetCryptType(void); |
318 | | |
319 | | template <> |
320 | 8.28k | ::Botan::Cipher_Dir GetCryptType<operation::SymmetricEncrypt>(void) { |
321 | 8.28k | return ::Botan::Cipher_Dir::Encryption; |
322 | 8.28k | } |
323 | | |
324 | | template <> |
325 | 7.68k | ::Botan::Cipher_Dir GetCryptType<operation::SymmetricDecrypt>(void) { |
326 | 7.68k | return ::Botan::Cipher_Dir::Decryption; |
327 | 7.68k | } |
328 | | |
329 | | template <class OperationType> |
330 | | std::optional<size_t> GetTagSize(const OperationType& op); |
331 | | |
332 | | template <> |
333 | 8.86k | std::optional<size_t> GetTagSize<>(const operation::SymmetricEncrypt& op) { |
334 | 8.86k | if ( op.tagSize == std::nullopt ) { |
335 | 7.00k | return std::nullopt; |
336 | 7.00k | } |
337 | | |
338 | 1.85k | return *op.tagSize; |
339 | 8.86k | } |
340 | | |
341 | | template <> |
342 | 11.3k | std::optional<size_t> GetTagSize<>(const operation::SymmetricDecrypt& op) { |
343 | 11.3k | if ( op.tag == std::nullopt ) { |
344 | 7.46k | return std::nullopt; |
345 | 7.46k | } |
346 | | |
347 | 3.88k | return op.tag->GetSize(); |
348 | 11.3k | } |
349 | | |
350 | | template <class OperationType> |
351 | | const uint8_t* GetTagPtr(const OperationType& op); |
352 | | |
353 | | template <> |
354 | 0 | const uint8_t* GetTagPtr<>(const operation::SymmetricEncrypt& op) { |
355 | 0 | (void)op; |
356 | |
|
357 | 0 | return nullptr; |
358 | 0 | } |
359 | | |
360 | | template <> |
361 | 1.65k | const uint8_t* GetTagPtr<>(const operation::SymmetricDecrypt& op) { |
362 | 1.65k | if ( op.tag == std::nullopt ) { |
363 | 0 | return nullptr; |
364 | 0 | } |
365 | | |
366 | 1.65k | return op.tag->GetPtr(); |
367 | 1.65k | } |
368 | | |
369 | | template <class CryptClass> |
370 | | void SetAAD(std::shared_ptr<CryptClass> crypt, const std::optional<component::AAD>& aad); |
371 | | |
372 | | template <> |
373 | 2.12k | void SetAAD<>(std::shared_ptr<::Botan::AEAD_Mode> crypt, const std::optional<component::AAD>& aad) { |
374 | 2.12k | if ( aad != std::nullopt ) { |
375 | 1.42k | crypt->set_associated_data(aad->Get()); |
376 | 1.42k | } |
377 | 2.12k | } |
378 | | |
379 | | template <> |
380 | 2.22k | void SetAAD<>(std::shared_ptr<::Botan::Cipher_Mode> crypt, const std::optional<component::AAD>& aad) { |
381 | 2.22k | (void)crypt; |
382 | 2.22k | (void)aad; |
383 | 2.22k | } |
384 | | |
385 | | template <class OperationType> |
386 | 9.37k | ::Botan::secure_vector<uint8_t> GetInData(const OperationType& op) { |
387 | 9.37k | const auto inPtr = GetInPtr(op); |
388 | 9.37k | ::Botan::secure_vector<uint8_t> ret(inPtr, inPtr + GetInSize(op)); |
389 | | |
390 | 9.37k | if ( GetCryptType<OperationType>() == ::Botan::Cipher_Dir::Encryption ) { |
391 | 5.05k | return ret; |
392 | 5.05k | } |
393 | | |
394 | 4.31k | const auto tagSize = GetTagSize(op); |
395 | | |
396 | 4.31k | if ( tagSize == std::nullopt || *tagSize == 0 ) { |
397 | 2.66k | return ret; |
398 | 2.66k | } |
399 | | |
400 | | /* Append the tag */ |
401 | | |
402 | 1.65k | ret.resize(ret.size() + *tagSize); |
403 | | |
404 | 1.65k | memcpy(ret.data() + GetInSize(op), GetTagPtr(op), *tagSize); |
405 | | |
406 | 1.65k | return ret; |
407 | 4.31k | } std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > cryptofuzz::module::Botan_detail::GetInData<cryptofuzz::operation::SymmetricEncrypt>(cryptofuzz::operation::SymmetricEncrypt const&) Line | Count | Source | 386 | 5.05k | ::Botan::secure_vector<uint8_t> GetInData(const OperationType& op) { | 387 | 5.05k | const auto inPtr = GetInPtr(op); | 388 | 5.05k | ::Botan::secure_vector<uint8_t> ret(inPtr, inPtr + GetInSize(op)); | 389 | | | 390 | 5.05k | if ( GetCryptType<OperationType>() == ::Botan::Cipher_Dir::Encryption ) { | 391 | 5.05k | return ret; | 392 | 5.05k | } | 393 | | | 394 | 0 | const auto tagSize = GetTagSize(op); | 395 | |
| 396 | 0 | if ( tagSize == std::nullopt || *tagSize == 0 ) { | 397 | 0 | return ret; | 398 | 0 | } | 399 | | | 400 | | /* Append the tag */ | 401 | | | 402 | 0 | ret.resize(ret.size() + *tagSize); | 403 | |
| 404 | 0 | memcpy(ret.data() + GetInSize(op), GetTagPtr(op), *tagSize); | 405 | |
| 406 | 0 | return ret; | 407 | 0 | } |
std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> > cryptofuzz::module::Botan_detail::GetInData<cryptofuzz::operation::SymmetricDecrypt>(cryptofuzz::operation::SymmetricDecrypt const&) Line | Count | Source | 386 | 4.31k | ::Botan::secure_vector<uint8_t> GetInData(const OperationType& op) { | 387 | 4.31k | const auto inPtr = GetInPtr(op); | 388 | 4.31k | ::Botan::secure_vector<uint8_t> ret(inPtr, inPtr + GetInSize(op)); | 389 | | | 390 | 4.31k | if ( GetCryptType<OperationType>() == ::Botan::Cipher_Dir::Encryption ) { | 391 | 0 | return ret; | 392 | 0 | } | 393 | | | 394 | 4.31k | const auto tagSize = GetTagSize(op); | 395 | | | 396 | 4.31k | if ( tagSize == std::nullopt || *tagSize == 0 ) { | 397 | 2.66k | return ret; | 398 | 2.66k | } | 399 | | | 400 | | /* Append the tag */ | 401 | | | 402 | 1.65k | ret.resize(ret.size() + *tagSize); | 403 | | | 404 | 1.65k | memcpy(ret.data() + GetInSize(op), GetTagPtr(op), *tagSize); | 405 | | | 406 | 1.65k | return ret; | 407 | 4.31k | } |
|
408 | | |
409 | | template <class ReturnType> |
410 | | ReturnType ToReturnType(const ::Botan::secure_vector<uint8_t>& data, std::optional<size_t> tagSize); |
411 | | |
412 | | template <> |
413 | 1.70k | component::Ciphertext ToReturnType(const ::Botan::secure_vector<uint8_t>& data, std::optional<size_t> tagSize) { |
414 | 1.70k | if ( tagSize == std::nullopt ) { |
415 | 921 | return component::Ciphertext(Buffer(data.data(), data.size())); |
416 | 921 | } |
417 | | |
418 | 780 | const size_t ciphertextSize = data.size() - *tagSize; |
419 | | |
420 | 780 | return component::Ciphertext(Buffer(data.data(), ciphertextSize), Buffer(data.data() + ciphertextSize, *tagSize)); |
421 | 1.70k | } |
422 | | |
423 | | template <> |
424 | 1.72k | component::Cleartext ToReturnType(const ::Botan::secure_vector<uint8_t>& data, std::optional<size_t> tagSize) { |
425 | 1.72k | (void)tagSize; |
426 | | |
427 | 1.72k | return component::Cleartext(Buffer(data.data(), data.size())); |
428 | 1.72k | } |
429 | | |
430 | | template <class ReturnType, class OperationType, class CryptClass> |
431 | 11.0k | std::optional<ReturnType> Crypt(OperationType& op, Datasource& ds) { |
432 | 11.0k | std::optional<ReturnType> ret = std::nullopt; |
433 | | |
434 | 11.0k | if ( typeid(CryptClass) == typeid(::Botan::Cipher_Mode) ) { |
435 | 7.36k | if ( op.aad != std::nullopt ) { |
436 | 842 | return std::nullopt; |
437 | 842 | } |
438 | 6.52k | if ( GetTagSize(op) != std::nullopt ) { |
439 | 856 | return std::nullopt; |
440 | 856 | } |
441 | 6.52k | } |
442 | | |
443 | 9.37k | std::shared_ptr<CryptClass> crypt = nullptr; |
444 | 9.37k | const ::Botan::SymmetricKey key(op.cipher.key.GetPtr(), op.cipher.key.GetSize()); |
445 | 9.37k | const ::Botan::InitializationVector iv(op.cipher.iv.GetPtr(), op.cipher.iv.GetSize()); |
446 | 9.37k | ::Botan::secure_vector<uint8_t> in = GetInData(op); |
447 | 9.37k | ::Botan::secure_vector<uint8_t> out; |
448 | 9.37k | bool useOneShot = true; |
449 | 9.37k | util::Multipart parts; |
450 | | |
451 | 9.37k | const std::optional<size_t> tagSize = GetTagSize(op); |
452 | | |
453 | 9.37k | try { |
454 | | /* Initialize */ |
455 | 9.37k | { |
456 | 9.37k | std::optional<std::string> _algoString; |
457 | 9.37k | CF_CHECK_NE(_algoString = Botan_detail::CipherIDToString(op.cipher.cipherType.Get()), std::nullopt); |
458 | 6.59k | std::string algoString; |
459 | 6.59k | if ( tagSize == std::nullopt ) { |
460 | 3.60k | algoString = Botan_detail::parenthesize(*_algoString, std::to_string(0)); |
461 | 3.60k | } else { |
462 | 2.99k | algoString = Botan_detail::parenthesize(*_algoString, std::to_string(*tagSize)); |
463 | 2.99k | } |
464 | | |
465 | 6.59k | CF_CHECK_NE(crypt = CryptClass::create(algoString, GetCryptType<OperationType>()), nullptr); |
466 | 6.00k | crypt->set_key(key); |
467 | | |
468 | 6.00k | SetAAD(crypt, op.aad); |
469 | | |
470 | 6.00k | crypt->start(iv.bits_of()); |
471 | 6.00k | if ( crypt->update_granularity() == 1 ) { |
472 | 2.03k | try { |
473 | 2.03k | useOneShot = ds.Get<bool>(); |
474 | 2.03k | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } |
475 | 2.03k | } |
476 | 6.00k | if ( useOneShot == false ) { |
477 | 491 | parts = util::ToParts(ds, GetInPtr(op), GetInSize(op)); |
478 | 491 | } |
479 | 6.00k | } |
480 | | |
481 | | /* Process */ |
482 | 0 | { |
483 | 6.00k | if ( useOneShot == true ) { |
484 | 3.57k | crypt->finish(in); |
485 | 3.57k | } else { |
486 | 4.97k | for (const auto& part : parts) { |
487 | 4.97k | std::vector<uint8_t> tmp(part.first, part.first + part.second); |
488 | 4.97k | const auto num = crypt->process(tmp.data(), tmp.size()); |
489 | 4.97k | out.insert(out.end(), tmp.begin(), tmp.begin() + num); |
490 | 4.97k | } |
491 | 2.42k | crypt->finish(out, out.size()); |
492 | 2.42k | } |
493 | 6.00k | } |
494 | | |
495 | | /* Finalize */ |
496 | 6.00k | { |
497 | | /* TODO take max output size in consideration */ |
498 | | |
499 | 6.00k | if ( useOneShot == true ) { |
500 | 3.04k | ret = ToReturnType<ReturnType>(in, tagSize); |
501 | 3.04k | } else { |
502 | 2.95k | ret = ToReturnType<ReturnType>(::Botan::secure_vector<uint8_t>(out.data(), out.data() + out.size()), tagSize); |
503 | 2.95k | } |
504 | 6.00k | } |
505 | 6.00k | } catch ( ... ) { } |
506 | 9.37k | end: |
507 | | |
508 | 9.37k | return ret; |
509 | 9.37k | } std::__1::optional<cryptofuzz::component::Ciphertext> cryptofuzz::module::Botan_detail::Crypt<cryptofuzz::component::Ciphertext, cryptofuzz::operation::SymmetricEncrypt, Botan::AEAD_Mode>(cryptofuzz::operation::SymmetricEncrypt&, fuzzing::datasource::Datasource&) Line | Count | Source | 431 | 1.73k | std::optional<ReturnType> Crypt(OperationType& op, Datasource& ds) { | 432 | 1.73k | std::optional<ReturnType> ret = std::nullopt; | 433 | | | 434 | 1.73k | if ( typeid(CryptClass) == typeid(::Botan::Cipher_Mode) ) { | 435 | 0 | if ( op.aad != std::nullopt ) { | 436 | 0 | return std::nullopt; | 437 | 0 | } | 438 | 0 | if ( GetTagSize(op) != std::nullopt ) { | 439 | 0 | return std::nullopt; | 440 | 0 | } | 441 | 0 | } | 442 | | | 443 | 1.73k | std::shared_ptr<CryptClass> crypt = nullptr; | 444 | 1.73k | const ::Botan::SymmetricKey key(op.cipher.key.GetPtr(), op.cipher.key.GetSize()); | 445 | 1.73k | const ::Botan::InitializationVector iv(op.cipher.iv.GetPtr(), op.cipher.iv.GetSize()); | 446 | 1.73k | ::Botan::secure_vector<uint8_t> in = GetInData(op); | 447 | 1.73k | ::Botan::secure_vector<uint8_t> out; | 448 | 1.73k | bool useOneShot = true; | 449 | 1.73k | util::Multipart parts; | 450 | | | 451 | 1.73k | const std::optional<size_t> tagSize = GetTagSize(op); | 452 | | | 453 | 1.73k | try { | 454 | | /* Initialize */ | 455 | 1.73k | { | 456 | 1.73k | std::optional<std::string> _algoString; | 457 | 1.73k | CF_CHECK_NE(_algoString = Botan_detail::CipherIDToString(op.cipher.cipherType.Get()), std::nullopt); | 458 | 1.56k | std::string algoString; | 459 | 1.56k | if ( tagSize == std::nullopt ) { | 460 | 245 | algoString = Botan_detail::parenthesize(*_algoString, std::to_string(0)); | 461 | 1.32k | } else { | 462 | 1.32k | algoString = Botan_detail::parenthesize(*_algoString, std::to_string(*tagSize)); | 463 | 1.32k | } | 464 | | | 465 | 1.56k | CF_CHECK_NE(crypt = CryptClass::create(algoString, GetCryptType<OperationType>()), nullptr); | 466 | 1.32k | crypt->set_key(key); | 467 | | | 468 | 1.32k | SetAAD(crypt, op.aad); | 469 | | | 470 | 1.32k | crypt->start(iv.bits_of()); | 471 | 1.32k | if ( crypt->update_granularity() == 1 ) { | 472 | 576 | try { | 473 | 576 | useOneShot = ds.Get<bool>(); | 474 | 576 | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } | 475 | 576 | } | 476 | 1.32k | if ( useOneShot == false ) { | 477 | 213 | parts = util::ToParts(ds, GetInPtr(op), GetInSize(op)); | 478 | 213 | } | 479 | 1.32k | } | 480 | | | 481 | | /* Process */ | 482 | 0 | { | 483 | 1.32k | if ( useOneShot == true ) { | 484 | 567 | crypt->finish(in); | 485 | 757 | } else { | 486 | 3.49k | for (const auto& part : parts) { | 487 | 3.49k | std::vector<uint8_t> tmp(part.first, part.first + part.second); | 488 | 3.49k | const auto num = crypt->process(tmp.data(), tmp.size()); | 489 | 3.49k | out.insert(out.end(), tmp.begin(), tmp.begin() + num); | 490 | 3.49k | } | 491 | 757 | crypt->finish(out, out.size()); | 492 | 757 | } | 493 | 1.32k | } | 494 | | | 495 | | /* Finalize */ | 496 | 1.32k | { | 497 | | /* TODO take max output size in consideration */ | 498 | | | 499 | 1.32k | if ( useOneShot == true ) { | 500 | 567 | ret = ToReturnType<ReturnType>(in, tagSize); | 501 | 757 | } else { | 502 | 757 | ret = ToReturnType<ReturnType>(::Botan::secure_vector<uint8_t>(out.data(), out.data() + out.size()), tagSize); | 503 | 757 | } | 504 | 1.32k | } | 505 | 1.32k | } catch ( ... ) { } | 506 | 1.73k | end: | 507 | | | 508 | 1.73k | return ret; | 509 | 1.73k | } |
std::__1::optional<cryptofuzz::component::Ciphertext> cryptofuzz::module::Botan_detail::Crypt<cryptofuzz::component::Ciphertext, cryptofuzz::operation::SymmetricEncrypt, Botan::Cipher_Mode>(cryptofuzz::operation::SymmetricEncrypt&, fuzzing::datasource::Datasource&) Line | Count | Source | 431 | 4.33k | std::optional<ReturnType> Crypt(OperationType& op, Datasource& ds) { | 432 | 4.33k | std::optional<ReturnType> ret = std::nullopt; | 433 | | | 434 | 4.33k | if ( typeid(CryptClass) == typeid(::Botan::Cipher_Mode) ) { | 435 | 4.33k | if ( op.aad != std::nullopt ) { | 436 | 526 | return std::nullopt; | 437 | 526 | } | 438 | 3.80k | if ( GetTagSize(op) != std::nullopt ) { | 439 | 479 | return std::nullopt; | 440 | 479 | } | 441 | 3.80k | } | 442 | | | 443 | 3.32k | std::shared_ptr<CryptClass> crypt = nullptr; | 444 | 3.32k | const ::Botan::SymmetricKey key(op.cipher.key.GetPtr(), op.cipher.key.GetSize()); | 445 | 3.32k | const ::Botan::InitializationVector iv(op.cipher.iv.GetPtr(), op.cipher.iv.GetSize()); | 446 | 3.32k | ::Botan::secure_vector<uint8_t> in = GetInData(op); | 447 | 3.32k | ::Botan::secure_vector<uint8_t> out; | 448 | 3.32k | bool useOneShot = true; | 449 | 3.32k | util::Multipart parts; | 450 | | | 451 | 3.32k | const std::optional<size_t> tagSize = GetTagSize(op); | 452 | | | 453 | 3.32k | try { | 454 | | /* Initialize */ | 455 | 3.32k | { | 456 | 3.32k | std::optional<std::string> _algoString; | 457 | 3.32k | CF_CHECK_NE(_algoString = Botan_detail::CipherIDToString(op.cipher.cipherType.Get()), std::nullopt); | 458 | 1.66k | std::string algoString; | 459 | 1.66k | if ( tagSize == std::nullopt ) { | 460 | 1.66k | algoString = Botan_detail::parenthesize(*_algoString, std::to_string(0)); | 461 | 1.66k | } else { | 462 | 0 | algoString = Botan_detail::parenthesize(*_algoString, std::to_string(*tagSize)); | 463 | 0 | } | 464 | | | 465 | 1.66k | CF_CHECK_NE(crypt = CryptClass::create(algoString, GetCryptType<OperationType>()), nullptr); | 466 | 1.47k | crypt->set_key(key); | 467 | | | 468 | 1.47k | SetAAD(crypt, op.aad); | 469 | | | 470 | 1.47k | crypt->start(iv.bits_of()); | 471 | 1.47k | if ( crypt->update_granularity() == 1 ) { | 472 | 259 | try { | 473 | 259 | useOneShot = ds.Get<bool>(); | 474 | 259 | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } | 475 | 259 | } | 476 | 1.47k | if ( useOneShot == false ) { | 477 | 135 | parts = util::ToParts(ds, GetInPtr(op), GetInSize(op)); | 478 | 135 | } | 479 | 1.47k | } | 480 | | | 481 | | /* Process */ | 482 | 0 | { | 483 | 1.47k | if ( useOneShot == true ) { | 484 | 836 | crypt->finish(in); | 485 | 836 | } else { | 486 | 635 | for (const auto& part : parts) { | 487 | 459 | std::vector<uint8_t> tmp(part.first, part.first + part.second); | 488 | 459 | const auto num = crypt->process(tmp.data(), tmp.size()); | 489 | 459 | out.insert(out.end(), tmp.begin(), tmp.begin() + num); | 490 | 459 | } | 491 | 635 | crypt->finish(out, out.size()); | 492 | 635 | } | 493 | 1.47k | } | 494 | | | 495 | | /* Finalize */ | 496 | 1.47k | { | 497 | | /* TODO take max output size in consideration */ | 498 | | | 499 | 1.47k | if ( useOneShot == true ) { | 500 | 786 | ret = ToReturnType<ReturnType>(in, tagSize); | 501 | 786 | } else { | 502 | 685 | ret = ToReturnType<ReturnType>(::Botan::secure_vector<uint8_t>(out.data(), out.data() + out.size()), tagSize); | 503 | 685 | } | 504 | 1.47k | } | 505 | 1.47k | } catch ( ... ) { } | 506 | 3.32k | end: | 507 | | | 508 | 3.32k | return ret; | 509 | 3.32k | } |
std::__1::optional<cryptofuzz::Buffer> cryptofuzz::module::Botan_detail::Crypt<cryptofuzz::Buffer, cryptofuzz::operation::SymmetricDecrypt, Botan::AEAD_Mode>(cryptofuzz::operation::SymmetricDecrypt&, fuzzing::datasource::Datasource&) Line | Count | Source | 431 | 1.98k | std::optional<ReturnType> Crypt(OperationType& op, Datasource& ds) { | 432 | 1.98k | std::optional<ReturnType> ret = std::nullopt; | 433 | | | 434 | 1.98k | if ( typeid(CryptClass) == typeid(::Botan::Cipher_Mode) ) { | 435 | 0 | if ( op.aad != std::nullopt ) { | 436 | 0 | return std::nullopt; | 437 | 0 | } | 438 | 0 | if ( GetTagSize(op) != std::nullopt ) { | 439 | 0 | return std::nullopt; | 440 | 0 | } | 441 | 0 | } | 442 | | | 443 | 1.98k | std::shared_ptr<CryptClass> crypt = nullptr; | 444 | 1.98k | const ::Botan::SymmetricKey key(op.cipher.key.GetPtr(), op.cipher.key.GetSize()); | 445 | 1.98k | const ::Botan::InitializationVector iv(op.cipher.iv.GetPtr(), op.cipher.iv.GetSize()); | 446 | 1.98k | ::Botan::secure_vector<uint8_t> in = GetInData(op); | 447 | 1.98k | ::Botan::secure_vector<uint8_t> out; | 448 | 1.98k | bool useOneShot = true; | 449 | 1.98k | util::Multipart parts; | 450 | | | 451 | 1.98k | const std::optional<size_t> tagSize = GetTagSize(op); | 452 | | | 453 | 1.98k | try { | 454 | | /* Initialize */ | 455 | 1.98k | { | 456 | 1.98k | std::optional<std::string> _algoString; | 457 | 1.98k | CF_CHECK_NE(_algoString = Botan_detail::CipherIDToString(op.cipher.cipherType.Get()), std::nullopt); | 458 | 1.81k | std::string algoString; | 459 | 1.81k | if ( tagSize == std::nullopt ) { | 460 | 139 | algoString = Botan_detail::parenthesize(*_algoString, std::to_string(0)); | 461 | 1.67k | } else { | 462 | 1.67k | algoString = Botan_detail::parenthesize(*_algoString, std::to_string(*tagSize)); | 463 | 1.67k | } | 464 | | | 465 | 1.81k | CF_CHECK_NE(crypt = CryptClass::create(algoString, GetCryptType<OperationType>()), nullptr); | 466 | 1.74k | crypt->set_key(key); | 467 | | | 468 | 1.74k | SetAAD(crypt, op.aad); | 469 | | | 470 | 1.74k | crypt->start(iv.bits_of()); | 471 | 1.74k | if ( crypt->update_granularity() == 1 ) { | 472 | 916 | try { | 473 | 916 | useOneShot = ds.Get<bool>(); | 474 | 916 | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } | 475 | 916 | } | 476 | 1.74k | if ( useOneShot == false ) { | 477 | 108 | parts = util::ToParts(ds, GetInPtr(op), GetInSize(op)); | 478 | 108 | } | 479 | 1.74k | } | 480 | | | 481 | | /* Process */ | 482 | 0 | { | 483 | 1.74k | if ( useOneShot == true ) { | 484 | 1.17k | crypt->finish(in); | 485 | 1.17k | } else { | 486 | 709 | for (const auto& part : parts) { | 487 | 709 | std::vector<uint8_t> tmp(part.first, part.first + part.second); | 488 | 709 | const auto num = crypt->process(tmp.data(), tmp.size()); | 489 | 709 | out.insert(out.end(), tmp.begin(), tmp.begin() + num); | 490 | 709 | } | 491 | 577 | crypt->finish(out, out.size()); | 492 | 577 | } | 493 | 1.74k | } | 494 | | | 495 | | /* Finalize */ | 496 | 1.74k | { | 497 | | /* TODO take max output size in consideration */ | 498 | | | 499 | 1.74k | if ( useOneShot == true ) { | 500 | 712 | ret = ToReturnType<ReturnType>(in, tagSize); | 501 | 1.03k | } else { | 502 | 1.03k | ret = ToReturnType<ReturnType>(::Botan::secure_vector<uint8_t>(out.data(), out.data() + out.size()), tagSize); | 503 | 1.03k | } | 504 | 1.74k | } | 505 | 1.74k | } catch ( ... ) { } | 506 | 1.98k | end: | 507 | | | 508 | 1.98k | return ret; | 509 | 1.98k | } |
std::__1::optional<cryptofuzz::Buffer> cryptofuzz::module::Botan_detail::Crypt<cryptofuzz::Buffer, cryptofuzz::operation::SymmetricDecrypt, Botan::Cipher_Mode>(cryptofuzz::operation::SymmetricDecrypt&, fuzzing::datasource::Datasource&) Line | Count | Source | 431 | 3.03k | std::optional<ReturnType> Crypt(OperationType& op, Datasource& ds) { | 432 | 3.03k | std::optional<ReturnType> ret = std::nullopt; | 433 | | | 434 | 3.03k | if ( typeid(CryptClass) == typeid(::Botan::Cipher_Mode) ) { | 435 | 3.03k | if ( op.aad != std::nullopt ) { | 436 | 316 | return std::nullopt; | 437 | 316 | } | 438 | 2.71k | if ( GetTagSize(op) != std::nullopt ) { | 439 | 377 | return std::nullopt; | 440 | 377 | } | 441 | 2.71k | } | 442 | | | 443 | 2.33k | std::shared_ptr<CryptClass> crypt = nullptr; | 444 | 2.33k | const ::Botan::SymmetricKey key(op.cipher.key.GetPtr(), op.cipher.key.GetSize()); | 445 | 2.33k | const ::Botan::InitializationVector iv(op.cipher.iv.GetPtr(), op.cipher.iv.GetSize()); | 446 | 2.33k | ::Botan::secure_vector<uint8_t> in = GetInData(op); | 447 | 2.33k | ::Botan::secure_vector<uint8_t> out; | 448 | 2.33k | bool useOneShot = true; | 449 | 2.33k | util::Multipart parts; | 450 | | | 451 | 2.33k | const std::optional<size_t> tagSize = GetTagSize(op); | 452 | | | 453 | 2.33k | try { | 454 | | /* Initialize */ | 455 | 2.33k | { | 456 | 2.33k | std::optional<std::string> _algoString; | 457 | 2.33k | CF_CHECK_NE(_algoString = Botan_detail::CipherIDToString(op.cipher.cipherType.Get()), std::nullopt); | 458 | 1.55k | std::string algoString; | 459 | 1.55k | if ( tagSize == std::nullopt ) { | 460 | 1.55k | algoString = Botan_detail::parenthesize(*_algoString, std::to_string(0)); | 461 | 1.55k | } else { | 462 | 0 | algoString = Botan_detail::parenthesize(*_algoString, std::to_string(*tagSize)); | 463 | 0 | } | 464 | | | 465 | 1.55k | CF_CHECK_NE(crypt = CryptClass::create(algoString, GetCryptType<OperationType>()), nullptr); | 466 | 1.45k | crypt->set_key(key); | 467 | | | 468 | 1.45k | SetAAD(crypt, op.aad); | 469 | | | 470 | 1.45k | crypt->start(iv.bits_of()); | 471 | 1.45k | if ( crypt->update_granularity() == 1 ) { | 472 | 282 | try { | 473 | 282 | useOneShot = ds.Get<bool>(); | 474 | 282 | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } | 475 | 282 | } | 476 | 1.45k | if ( useOneShot == false ) { | 477 | 35 | parts = util::ToParts(ds, GetInPtr(op), GetInSize(op)); | 478 | 35 | } | 479 | 1.45k | } | 480 | | | 481 | | /* Process */ | 482 | 0 | { | 483 | 1.45k | if ( useOneShot == true ) { | 484 | 1.00k | crypt->finish(in); | 485 | 1.00k | } else { | 486 | 453 | for (const auto& part : parts) { | 487 | 308 | std::vector<uint8_t> tmp(part.first, part.first + part.second); | 488 | 308 | const auto num = crypt->process(tmp.data(), tmp.size()); | 489 | 308 | out.insert(out.end(), tmp.begin(), tmp.begin() + num); | 490 | 308 | } | 491 | 453 | crypt->finish(out, out.size()); | 492 | 453 | } | 493 | 1.45k | } | 494 | | | 495 | | /* Finalize */ | 496 | 1.45k | { | 497 | | /* TODO take max output size in consideration */ | 498 | | | 499 | 1.45k | if ( useOneShot == true ) { | 500 | 977 | ret = ToReturnType<ReturnType>(in, tagSize); | 501 | 977 | } else { | 502 | 480 | ret = ToReturnType<ReturnType>(::Botan::secure_vector<uint8_t>(out.data(), out.data() + out.size()), tagSize); | 503 | 480 | } | 504 | 1.45k | } | 505 | 1.45k | } catch ( ... ) { } | 506 | 2.33k | end: | 507 | | | 508 | 2.33k | return ret; | 509 | 2.33k | } |
|
510 | | |
511 | | } /* namespace Botan_detail */ |
512 | | |
513 | 1.83k | std::optional<component::MAC> Botan::OpCMAC(operation::CMAC& op) { |
514 | 1.83k | if ( !repository::IsCBC(op.cipher.cipherType.Get()) ) { |
515 | 525 | return std::nullopt; |
516 | 525 | } |
517 | 1.31k | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
518 | 1.31k | std::optional<component::MAC> ret = std::nullopt; |
519 | 1.31k | std::unique_ptr<::Botan::MessageAuthenticationCode> cmac = nullptr; |
520 | 1.31k | util::Multipart parts; |
521 | | |
522 | 1.31k | try { |
523 | | /* Initialize */ |
524 | 1.31k | { |
525 | 1.31k | BOTAN_SET_GLOBAL_DS |
526 | | |
527 | 1.31k | std::optional<std::string> algoString; |
528 | 1.31k | CF_CHECK_NE(algoString = Botan_detail::CipherIDToString(op.cipher.cipherType.Get(), false), std::nullopt); |
529 | | |
530 | 1.21k | const std::string cmacString = Botan_detail::parenthesize("CMAC", *algoString); |
531 | | |
532 | 1.21k | CF_CHECK_NE(cmac = ::Botan::MessageAuthenticationCode::create(cmacString), nullptr); |
533 | | |
534 | 1.21k | try { |
535 | 1.21k | cmac->set_key(op.cipher.key.GetPtr(), op.cipher.key.GetSize()); |
536 | 1.21k | } catch ( ... ) { |
537 | 228 | goto end; |
538 | 228 | } |
539 | | |
540 | 982 | parts = util::ToParts(ds, op.cleartext); |
541 | 982 | } |
542 | | |
543 | | /* Process */ |
544 | 20.5k | for (const auto& part : parts) { |
545 | 20.5k | cmac->update(part.first, part.second); |
546 | 20.5k | } |
547 | | |
548 | | /* Finalize */ |
549 | 982 | { |
550 | 982 | const auto res = cmac->final(); |
551 | 982 | ret = component::MAC(res.data(), res.size()); |
552 | 982 | } |
553 | | |
554 | 982 | } catch ( ... ) { } |
555 | | |
556 | 1.31k | end: |
557 | 1.31k | BOTAN_UNSET_GLOBAL_DS |
558 | | |
559 | 1.31k | return ret; |
560 | 1.31k | } |
561 | | |
562 | 6.07k | std::optional<component::Ciphertext> Botan::OpSymmetricEncrypt(operation::SymmetricEncrypt& op) { |
563 | 6.07k | if ( op.cipher.cipherType.Is(CF_CIPHER("CHACHA20_POLY1305")) && op.cipher.iv.GetSize() == 24 ) { |
564 | | /* Botan interpretes CHACHA20_POLY1305 + 192 bits IV as XCHACHA20_POLY1305 */ |
565 | 10 | return std::nullopt; |
566 | 10 | } |
567 | | |
568 | 6.06k | std::optional<component::Ciphertext> ret = std::nullopt; |
569 | | |
570 | 6.06k | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
571 | 6.06k | BOTAN_SET_GLOBAL_DS |
572 | | |
573 | 6.06k | if ( cryptofuzz::repository::IsAEAD(op.cipher.cipherType.Get()) ) { |
574 | 1.73k | ret = Botan_detail::Crypt<component::Ciphertext, operation::SymmetricEncrypt, ::Botan::AEAD_Mode>(op, ds); |
575 | 4.33k | } else { |
576 | 4.33k | ret = Botan_detail::Crypt<component::Ciphertext, operation::SymmetricEncrypt, ::Botan::Cipher_Mode>(op, ds); |
577 | 4.33k | } |
578 | | |
579 | 6.06k | BOTAN_UNSET_GLOBAL_DS |
580 | | |
581 | 6.06k | return ret; |
582 | 6.07k | } |
583 | | |
584 | 5.04k | std::optional<component::Cleartext> Botan::OpSymmetricDecrypt(operation::SymmetricDecrypt& op) { |
585 | 5.04k | if ( op.cipher.cipherType.Is(CF_CIPHER("CHACHA20_POLY1305")) && op.cipher.iv.GetSize() == 24 ) { |
586 | 35 | return std::nullopt; |
587 | 35 | } |
588 | | |
589 | 5.01k | std::optional<component::Cleartext> ret = std::nullopt; |
590 | | |
591 | 5.01k | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
592 | 5.01k | BOTAN_SET_GLOBAL_DS |
593 | | |
594 | 5.01k | if ( cryptofuzz::repository::IsAEAD(op.cipher.cipherType.Get()) ) { |
595 | 1.98k | ret = Botan_detail::Crypt<component::Cleartext, operation::SymmetricDecrypt, ::Botan::AEAD_Mode>(op, ds); |
596 | 3.03k | } else { |
597 | 3.03k | ret = Botan_detail::Crypt<component::Cleartext, operation::SymmetricDecrypt, ::Botan::Cipher_Mode>(op, ds); |
598 | 3.03k | } |
599 | | |
600 | 5.01k | BOTAN_UNSET_GLOBAL_DS |
601 | | |
602 | 5.01k | return ret; |
603 | 5.04k | } |
604 | | |
605 | 547 | std::optional<component::Key> Botan::OpKDF_SCRYPT(operation::KDF_SCRYPT& op) { |
606 | 547 | std::optional<component::Key> ret = std::nullopt; |
607 | 547 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
608 | 547 | std::unique_ptr<::Botan::PasswordHashFamily> pwdhash_fam = nullptr; |
609 | 547 | std::unique_ptr<::Botan::PasswordHash> pwdhash = nullptr; |
610 | 547 | uint8_t* out = util::malloc(op.keySize); |
611 | | |
612 | 547 | try { |
613 | | /* Initialize */ |
614 | 547 | { |
615 | 547 | BOTAN_SET_GLOBAL_DS |
616 | | |
617 | 547 | CF_CHECK_NE(pwdhash_fam = ::Botan::PasswordHashFamily::create("Scrypt"), nullptr); |
618 | 547 | CF_CHECK_NE(pwdhash = pwdhash_fam->from_params(op.N, op.r, op.p), nullptr); |
619 | | |
620 | 547 | } |
621 | | |
622 | | /* Process */ |
623 | 0 | { |
624 | 547 | pwdhash->derive_key( |
625 | 547 | out, |
626 | 547 | op.keySize, |
627 | 547 | (const char*)op.password.GetPtr(), |
628 | 547 | op.password.GetSize(), |
629 | 547 | op.salt.GetPtr(), |
630 | 547 | op.salt.GetSize()); |
631 | 547 | } |
632 | | |
633 | | /* Finalize */ |
634 | 547 | { |
635 | 547 | ret = component::Key(out, op.keySize); |
636 | 547 | } |
637 | 547 | } catch ( ... ) { } |
638 | | |
639 | 547 | end: |
640 | 547 | util::free(out); |
641 | | |
642 | 547 | BOTAN_UNSET_GLOBAL_DS |
643 | | |
644 | 547 | return ret; |
645 | 547 | } |
646 | | |
647 | 2.90k | std::optional<component::Key> Botan::OpKDF_HKDF(operation::KDF_HKDF& op) { |
648 | 2.90k | std::optional<component::Key> ret = std::nullopt; |
649 | 2.90k | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
650 | 2.90k | std::unique_ptr<::Botan::KDF> hkdf = nullptr; |
651 | | |
652 | 2.90k | try { |
653 | 2.90k | { |
654 | 2.90k | BOTAN_SET_GLOBAL_DS |
655 | | |
656 | 2.90k | std::optional<std::string> algoString; |
657 | 2.90k | CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.digestType.Get(), true), std::nullopt); |
658 | | |
659 | 2.16k | const std::string hkdfString = Botan_detail::parenthesize("HKDF", *algoString); |
660 | 2.16k | hkdf = ::Botan::KDF::create(hkdfString); |
661 | 2.16k | } |
662 | | |
663 | 0 | { |
664 | 2.16k | auto derived = hkdf->derive_key(op.keySize, op.password.Get(), op.salt.Get(), op.info.Get()); |
665 | | |
666 | 2.16k | ret = component::Key(derived.data(), derived.size()); |
667 | 2.16k | } |
668 | 2.16k | } catch ( ... ) { } |
669 | | |
670 | 2.90k | end: |
671 | 2.90k | BOTAN_UNSET_GLOBAL_DS |
672 | | |
673 | 2.90k | return ret; |
674 | 2.90k | } |
675 | | |
676 | 985 | std::optional<component::Key> Botan::OpKDF_PBKDF2(operation::KDF_PBKDF2& op) { |
677 | 985 | std::optional<component::Key> ret = std::nullopt; |
678 | 985 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
679 | 985 | std::unique_ptr<::Botan::PasswordHashFamily> pwdhash_fam = nullptr; |
680 | 985 | std::unique_ptr<::Botan::PasswordHash> pwdhash = nullptr; |
681 | 985 | uint8_t* out = util::malloc(op.keySize); |
682 | | |
683 | 985 | try { |
684 | | /* Initialize */ |
685 | 985 | { |
686 | 985 | BOTAN_SET_GLOBAL_DS |
687 | | |
688 | 985 | std::optional<std::string> algoString; |
689 | 985 | CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.digestType.Get(), true), std::nullopt); |
690 | | |
691 | 733 | const std::string pbkdf2String = Botan_detail::parenthesize("PBKDF2", *algoString); |
692 | 733 | CF_CHECK_NE(pwdhash_fam = ::Botan::PasswordHashFamily::create(pbkdf2String), nullptr); |
693 | | |
694 | 733 | CF_CHECK_NE(pwdhash = pwdhash_fam->from_params(op.iterations), nullptr); |
695 | | |
696 | 733 | } |
697 | | |
698 | | /* Process */ |
699 | 0 | { |
700 | 733 | pwdhash->derive_key( |
701 | 733 | out, |
702 | 733 | op.keySize, |
703 | 733 | (const char*)op.password.GetPtr(), |
704 | 733 | op.password.GetSize(), |
705 | 733 | op.salt.GetPtr(), |
706 | 733 | op.salt.GetSize()); |
707 | 733 | } |
708 | | |
709 | | /* Finalize */ |
710 | 733 | { |
711 | 733 | ret = component::Key(out, op.keySize); |
712 | 733 | } |
713 | 733 | } catch ( ... ) { } |
714 | | |
715 | 985 | end: |
716 | 985 | util::free(out); |
717 | | |
718 | 985 | BOTAN_UNSET_GLOBAL_DS |
719 | | |
720 | 985 | return ret; |
721 | 985 | } |
722 | | |
723 | 338 | std::optional<component::Key> Botan::OpKDF_ARGON2(operation::KDF_ARGON2& op) { |
724 | 338 | std::optional<component::Key> ret = std::nullopt; |
725 | 338 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
726 | 338 | std::unique_ptr<::Botan::PasswordHashFamily> pwdhash_fam = nullptr; |
727 | 338 | std::unique_ptr<::Botan::PasswordHash> pwdhash = nullptr; |
728 | 338 | uint8_t* out = util::malloc(op.keySize); |
729 | | |
730 | 338 | try { |
731 | | /* Initialize */ |
732 | 338 | { |
733 | 338 | BOTAN_SET_GLOBAL_DS |
734 | | |
735 | 338 | std::string argon2String; |
736 | | |
737 | 338 | switch ( op.type ) { |
738 | 161 | case 0: |
739 | 161 | argon2String = "Argon2d"; |
740 | 161 | break; |
741 | 60 | case 1: |
742 | 60 | argon2String = "Argon2i"; |
743 | 60 | break; |
744 | 94 | case 2: |
745 | 94 | argon2String = "Argon2id"; |
746 | 94 | break; |
747 | 23 | default: |
748 | 23 | goto end; |
749 | 338 | } |
750 | 315 | CF_CHECK_NE(pwdhash_fam = ::Botan::PasswordHashFamily::create(argon2String), nullptr); |
751 | | |
752 | 315 | CF_CHECK_NE(pwdhash = pwdhash_fam->from_params( |
753 | 315 | op.memory, |
754 | 315 | op.iterations, |
755 | 315 | op.threads), nullptr); |
756 | 315 | } |
757 | | |
758 | | /* Process */ |
759 | 0 | { |
760 | 315 | pwdhash->derive_key( |
761 | 315 | out, |
762 | 315 | op.keySize, |
763 | 315 | (const char*)op.password.GetPtr(), |
764 | 315 | op.password.GetSize(), |
765 | 315 | op.salt.GetPtr(), |
766 | 315 | op.salt.GetSize()); |
767 | 315 | } |
768 | | |
769 | | /* Finalize */ |
770 | 315 | { |
771 | 315 | ret = component::Key(out, op.keySize); |
772 | 315 | } |
773 | 315 | } catch ( ... ) { } |
774 | | |
775 | 338 | end: |
776 | 338 | util::free(out); |
777 | | |
778 | 338 | BOTAN_UNSET_GLOBAL_DS |
779 | | |
780 | 338 | return ret; |
781 | 338 | } |
782 | | |
783 | 1.19k | std::optional<component::Key> Botan::OpKDF_SP_800_108(operation::KDF_SP_800_108& op) { |
784 | 1.19k | std::optional<component::Key> ret = std::nullopt; |
785 | 1.19k | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
786 | 1.19k | uint8_t* out = util::malloc(op.keySize); |
787 | 1.19k | std::unique_ptr<::Botan::KDF> sp_800_108 = nullptr; |
788 | | |
789 | 1.19k | try { |
790 | 1.19k | BOTAN_SET_GLOBAL_DS |
791 | | |
792 | 1.19k | std::optional<std::string> algoString; |
793 | 1.19k | CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.mech.type.Get(), true), std::nullopt); |
794 | | |
795 | 1.02k | const std::string hmacString = Botan_detail::parenthesize("HMAC", *algoString); |
796 | 1.02k | std::string sp_800_108_string; |
797 | 1.02k | switch ( op.mode ) { |
798 | 300 | case 0: |
799 | 300 | sp_800_108_string = Botan_detail::parenthesize("SP800-108-Counter", hmacString); |
800 | 300 | break; |
801 | 325 | case 1: |
802 | 325 | sp_800_108_string = Botan_detail::parenthesize("SP800-108-Feedback", hmacString); |
803 | 325 | break; |
804 | 366 | case 2: |
805 | 366 | sp_800_108_string = Botan_detail::parenthesize("SP800-108-Pipeline", hmacString); |
806 | 366 | break; |
807 | 36 | default: |
808 | 36 | goto end; |
809 | 1.02k | } |
810 | | |
811 | 991 | sp_800_108 = ::Botan::KDF::create(sp_800_108_string); |
812 | | |
813 | 991 | { |
814 | 991 | auto derived = sp_800_108->derive_key(op.keySize, op.secret.Get(), op.salt.Get(), op.label.Get()); |
815 | | |
816 | 991 | ret = component::Key(derived.data(), derived.size()); |
817 | 991 | } |
818 | 991 | } catch ( ... ) { } |
819 | | |
820 | 1.19k | end: |
821 | 1.19k | util::free(out); |
822 | | |
823 | 1.19k | BOTAN_UNSET_GLOBAL_DS |
824 | | |
825 | 1.19k | return ret; |
826 | 1.19k | } |
827 | | |
828 | 248 | std::optional<component::Key> Botan::OpKDF_TLS1_PRF(operation::KDF_TLS1_PRF& op) { |
829 | 248 | std::optional<component::Key> ret = std::nullopt; |
830 | 248 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
831 | 248 | std::unique_ptr<::Botan::KDF> tlsprf = nullptr; |
832 | | |
833 | 248 | try { |
834 | 248 | BOTAN_SET_GLOBAL_DS |
835 | | |
836 | 248 | { |
837 | 248 | CF_CHECK_EQ(op.digestType.Get(), CF_DIGEST("MD5_SHA1")); |
838 | 37 | CF_CHECK_NE(tlsprf = ::Botan::KDF::create("TLS-PRF()"), nullptr); |
839 | 0 | } |
840 | | |
841 | 0 | { |
842 | 0 | const auto derived = tlsprf->derive_key(op.keySize, op.secret.Get(), op.seed.Get(), std::vector<uint8_t>{}); |
843 | |
|
844 | 0 | ret = component::Key(derived.data(), derived.size()); |
845 | 0 | } |
846 | 0 | } catch ( ... ) { } |
847 | | |
848 | 248 | end: |
849 | 248 | BOTAN_UNSET_GLOBAL_DS |
850 | | |
851 | 248 | return ret; |
852 | 248 | } |
853 | | |
854 | 60 | std::optional<component::Key> Botan::OpKDF_BCRYPT(operation::KDF_BCRYPT& op) { |
855 | 60 | std::optional<component::Key> ret = std::nullopt; |
856 | 60 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
857 | 60 | std::unique_ptr<::Botan::PasswordHashFamily> pwdhash_fam = nullptr; |
858 | 60 | std::unique_ptr<::Botan::PasswordHash> pwdhash = nullptr; |
859 | 60 | uint8_t* out = util::malloc(op.keySize); |
860 | | |
861 | 60 | try { |
862 | 60 | BOTAN_SET_GLOBAL_DS |
863 | | |
864 | | /* Initialize */ |
865 | 60 | { |
866 | 60 | CF_CHECK_EQ(op.digestType.Get(), CF_DIGEST("SHA512")); |
867 | 43 | CF_CHECK_NE(pwdhash_fam = ::Botan::PasswordHashFamily::create("Bcrypt-PBKDF"), nullptr); |
868 | 43 | CF_CHECK_NE(pwdhash = pwdhash_fam->from_params(op.iterations), nullptr); |
869 | | |
870 | 43 | } |
871 | | |
872 | | /* Process */ |
873 | 0 | { |
874 | 43 | pwdhash->derive_key( |
875 | 43 | out, |
876 | 43 | op.keySize, |
877 | 43 | (const char*)op.secret.GetPtr(), |
878 | 43 | op.secret.GetSize(), |
879 | 43 | op.salt.GetPtr(), |
880 | 43 | op.salt.GetSize()); |
881 | 43 | } |
882 | | |
883 | | /* Finalize */ |
884 | 43 | { |
885 | 43 | ret = component::Key(out, op.keySize); |
886 | 43 | } |
887 | 43 | } catch ( ... ) { } |
888 | | |
889 | 60 | end: |
890 | 60 | util::free(out); |
891 | | |
892 | 60 | BOTAN_UNSET_GLOBAL_DS |
893 | | |
894 | 60 | return ret; |
895 | 60 | } |
896 | | |
897 | | namespace Botan_detail { |
898 | 4.55k | std::optional<std::string> CurveIDToString(const uint64_t curveID) { |
899 | 4.55k | #include "curve_string_lut.h" |
900 | 4.55k | std::optional<std::string> ret = std::nullopt; |
901 | | |
902 | 4.55k | CF_CHECK_NE(LUT.find(curveID), LUT.end()); |
903 | 3.58k | ret = LUT.at(curveID); |
904 | 4.55k | end: |
905 | 4.55k | return ret; |
906 | 3.58k | } |
907 | | } /* namespace Botan_detail */ |
908 | | |
909 | 599 | std::optional<component::ECC_KeyPair> Botan::OpECC_GenerateKeyPair(operation::ECC_GenerateKeyPair& op) { |
910 | 599 | std::optional<component::ECC_KeyPair> ret = std::nullopt; |
911 | 599 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
912 | | |
913 | 599 | std::optional<std::string> curveString; |
914 | 599 | BOTAN_FUZZER_RNG; |
915 | | |
916 | 599 | CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt); |
917 | | |
918 | 314 | try { |
919 | 314 | ::Botan::EC_Group group(*curveString); |
920 | 314 | auto priv = ::Botan::ECDSA_PrivateKey(rng, group); |
921 | | |
922 | 314 | const auto pub_x = priv.public_point().get_affine_x(); |
923 | 314 | const auto pub_y = priv.public_point().get_affine_y(); |
924 | | |
925 | 314 | { |
926 | 314 | const auto pub = std::make_unique<::Botan::ECDSA_PublicKey>(::Botan::ECDSA_PublicKey(group, priv.public_point())); |
927 | 314 | CF_ASSERT(pub->check_key(rng, true) == true, "Generated pubkey fails validation"); |
928 | 314 | } |
929 | | |
930 | 0 | ret = { priv.private_value().to_dec_string(), { pub_x.to_dec_string(), pub_y.to_dec_string() } }; |
931 | | |
932 | | /* Catch exception thrown from Botan_detail::Fuzzer_RNG::randomize */ |
933 | 314 | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } |
934 | | |
935 | 599 | end: |
936 | 599 | return ret; |
937 | 314 | } |
938 | | |
939 | 463 | std::optional<bool> Botan::OpECC_ValidatePubkey(operation::ECC_ValidatePubkey& op) { |
940 | 463 | std::optional<bool> ret = std::nullopt; |
941 | 463 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
942 | | |
943 | 463 | BOTAN_FUZZER_RNG; |
944 | 463 | std::unique_ptr<::Botan::Public_Key> pub = nullptr; |
945 | | |
946 | 463 | try { |
947 | 463 | std::optional<std::string> curveString; |
948 | 463 | CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt); |
949 | | |
950 | 372 | ::Botan::EC_Group group(*curveString); |
951 | 372 | const ::Botan::BigInt pub_x(op.pub.first.ToString(ds)); |
952 | 372 | const ::Botan::BigInt pub_y(op.pub.second.ToString(ds)); |
953 | 372 | const ::Botan::PointGFp public_point = group.point(pub_x, pub_y); |
954 | 372 | pub = std::make_unique<::Botan::ECDSA_PublicKey>(::Botan::ECDSA_PublicKey(group, public_point)); |
955 | | |
956 | 372 | ret = pub->check_key(rng, true); |
957 | 372 | } catch ( ... ) { } |
958 | | |
959 | 463 | end: |
960 | 463 | return ret; |
961 | 463 | } |
962 | | |
963 | 654 | std::optional<component::ECC_PublicKey> Botan::OpECC_PrivateToPublic(operation::ECC_PrivateToPublic& op) { |
964 | 654 | std::optional<component::ECC_PublicKey> ret = std::nullopt; |
965 | 654 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
966 | | |
967 | 654 | BOTAN_FUZZER_RNG; |
968 | | |
969 | 654 | try { |
970 | 654 | std::optional<std::string> curveString; |
971 | | |
972 | 654 | if ( op.curveType.Get() == CF_ECC_CURVE("x25519") ) { |
973 | 132 | uint8_t priv_bytes[32]; |
974 | | |
975 | 132 | const ::Botan::BigInt priv_bigint(op.priv.ToString(ds)); |
976 | 132 | CF_CHECK_GT(priv_bigint, 0); |
977 | | |
978 | 112 | priv_bigint.binary_encode(priv_bytes, sizeof(priv_bytes)); |
979 | 112 | priv_bytes[0] &= 248; |
980 | 112 | priv_bytes[31] &= 127; |
981 | 112 | priv_bytes[31] |= 64; |
982 | 112 | const ::Botan::secure_vector<uint8_t> priv_vec(priv_bytes, priv_bytes + sizeof(priv_bytes)); |
983 | | |
984 | 112 | auto priv = ::Botan::X25519_PrivateKey(priv_vec); |
985 | | |
986 | 112 | ::Botan::BigInt pub; |
987 | 112 | pub.binary_decode(priv.public_value()); |
988 | | |
989 | 112 | ret = { pub.to_dec_string(), "0" }; |
990 | 522 | } else { |
991 | 522 | CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt); |
992 | 321 | ::Botan::EC_Group group(*curveString); |
993 | | |
994 | 321 | const ::Botan::BigInt priv_bn(op.priv.ToString(ds)); |
995 | 321 | CF_CHECK_GT(priv_bn, 0); |
996 | | |
997 | 304 | auto priv = std::make_unique<::Botan::ECDSA_PrivateKey>(::Botan::ECDSA_PrivateKey(rng, group, priv_bn)); |
998 | | |
999 | 304 | const auto pub_x = priv->public_point().get_affine_x(); |
1000 | 304 | const auto pub_y = priv->public_point().get_affine_y(); |
1001 | | |
1002 | 304 | ret = { pub_x.to_dec_string(), pub_y.to_dec_string() }; |
1003 | 304 | } |
1004 | 654 | } catch ( ... ) { } |
1005 | | |
1006 | 654 | end: |
1007 | 654 | return ret; |
1008 | 654 | } |
1009 | | |
1010 | | namespace Botan_detail { |
1011 | | template <class PrivkeyType, class Operation, bool RFC6979 = true> |
1012 | 652 | std::optional<component::ECDSA_Signature> ECxDSA_Sign(Operation& op) { |
1013 | 652 | std::optional<component::ECDSA_Signature> ret = std::nullopt; |
1014 | 652 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
1015 | | |
1016 | 652 | std::unique_ptr<PrivkeyType> priv = nullptr; |
1017 | 652 | std::unique_ptr<::Botan::Public_Key> pub = nullptr; |
1018 | 652 | std::unique_ptr<::Botan::PK_Signer> signer; |
1019 | | |
1020 | 652 | BOTAN_FUZZER_RNG; |
1021 | | |
1022 | 652 | BOTAN_SET_GLOBAL_DS |
1023 | | |
1024 | 652 | if ( RFC6979 == true ) { |
1025 | 523 | CF_CHECK_EQ(op.UseRFC6979Nonce(), true); |
1026 | 274 | } else { |
1027 | 129 | CF_CHECK_EQ(op.UseRandomNonce(), true); |
1028 | 88 | } |
1029 | | |
1030 | 362 | CF_CHECK_EQ(op.digestType.Get(), CF_DIGEST("SHA256")); |
1031 | | |
1032 | 301 | try { |
1033 | | /* Initialize */ |
1034 | 301 | { |
1035 | | |
1036 | 301 | std::optional<std::string> curveString, algoString; |
1037 | | |
1038 | 301 | CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt); |
1039 | 276 | ::Botan::EC_Group group(*curveString); |
1040 | | |
1041 | | /* Private key */ |
1042 | 276 | { |
1043 | 276 | const ::Botan::BigInt priv_bn(op.priv.ToString(ds)); |
1044 | | |
1045 | | /* Botan appears to generate a new key if the input key is 0, |
1046 | | * so don't do this */ |
1047 | 276 | CF_CHECK_NE(priv_bn, 0); |
1048 | | |
1049 | 245 | priv = std::make_unique<PrivkeyType>(PrivkeyType(rng, group, priv_bn)); |
1050 | 245 | } |
1051 | | |
1052 | | /* Prepare signer */ |
1053 | 245 | CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.digestType.Get()), std::nullopt); |
1054 | | |
1055 | 245 | const std::string emsa1String = Botan_detail::parenthesize("EMSA1", *algoString); |
1056 | 245 | signer.reset(new ::Botan::PK_Signer(*priv, rng, emsa1String, ::Botan::Signature_Format::DerSequence)); |
1057 | 245 | } |
1058 | | |
1059 | | /* Process */ |
1060 | 0 | { |
1061 | 245 | const auto signature = signer->sign_message(op.cleartext.Get(), rng); |
1062 | | |
1063 | | /* Retrieve R and S */ |
1064 | 245 | { |
1065 | 245 | ::Botan::BER_Decoder decoder(signature); |
1066 | 245 | ::Botan::BER_Decoder ber_sig = decoder.start_sequence(); |
1067 | | |
1068 | 245 | size_t count = 0; |
1069 | | |
1070 | 245 | ::Botan::BigInt R; |
1071 | 245 | ::Botan::BigInt S; |
1072 | 555 | while(ber_sig.more_items()) |
1073 | 310 | { |
1074 | 310 | switch ( count ) { |
1075 | 155 | case 0: |
1076 | 155 | ber_sig.decode(R); |
1077 | 155 | break; |
1078 | 155 | case 1: |
1079 | 155 | ber_sig.decode(S); |
1080 | 155 | break; |
1081 | 0 | default: |
1082 | 0 | printf("Error: Too many parts in signature BER\n"); |
1083 | 0 | abort(); |
1084 | 310 | } |
1085 | | |
1086 | 310 | ++count; |
1087 | 310 | } |
1088 | | |
1089 | 245 | if ( op.curveType.Get() == CF_ECC_CURVE("secp256k1") ) { |
1090 | | /* For compatibility with the secp256k1 library. |
1091 | | * See: https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki#low-s-values-in-signatures |
1092 | | */ |
1093 | 33 | if (S > ::Botan::BigInt("57896044618658097711785492504343953926418782139537452191302581570759080747168")) { |
1094 | 21 | S = ::Botan::BigInt("115792089237316195423570985008687907852837564279074904382605163141518161494337") - S; |
1095 | 21 | } |
1096 | 212 | } else if ( op.curveType.Get() == CF_ECC_CURVE("secp256r1") ) { |
1097 | | /* Similar ECDSA signature malleability adjustment for compatibility with trezor-firmware */ |
1098 | 28 | if (S > ::Botan::BigInt("57896044605178124381348723474703786764998477612067880171211129530534256022184")) { |
1099 | 16 | S = ::Botan::BigInt("115792089210356248762697446949407573529996955224135760342422259061068512044369") - S; |
1100 | 16 | } |
1101 | 28 | } |
1102 | | |
1103 | 245 | const auto pub_x = priv->public_point().get_affine_x().to_dec_string(); |
1104 | 245 | const auto pub_y = priv->public_point().get_affine_y().to_dec_string(); |
1105 | | |
1106 | 245 | const auto R_str = R.to_dec_string(); |
1107 | 245 | const auto S_str = S.to_dec_string(); |
1108 | | |
1109 | 245 | ret = component::ECDSA_Signature({ R_str, S_str }, { pub_x, pub_y }); |
1110 | 245 | } |
1111 | 245 | } |
1112 | 245 | } catch ( ... ) { } |
1113 | | |
1114 | 652 | end: |
1115 | 652 | BOTAN_UNSET_GLOBAL_DS |
1116 | | |
1117 | 652 | return ret; |
1118 | 301 | } std::__1::optional<cryptofuzz::component::ECDSA_Signature> cryptofuzz::module::Botan_detail::ECxDSA_Sign<Botan::ECDSA_PrivateKey, cryptofuzz::operation::ECDSA_Sign, true>(cryptofuzz::operation::ECDSA_Sign&) Line | Count | Source | 1012 | 523 | std::optional<component::ECDSA_Signature> ECxDSA_Sign(Operation& op) { | 1013 | 523 | std::optional<component::ECDSA_Signature> ret = std::nullopt; | 1014 | 523 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); | 1015 | | | 1016 | 523 | std::unique_ptr<PrivkeyType> priv = nullptr; | 1017 | 523 | std::unique_ptr<::Botan::Public_Key> pub = nullptr; | 1018 | 523 | std::unique_ptr<::Botan::PK_Signer> signer; | 1019 | | | 1020 | 523 | BOTAN_FUZZER_RNG; | 1021 | | | 1022 | 523 | BOTAN_SET_GLOBAL_DS | 1023 | | | 1024 | 523 | if ( RFC6979 == true ) { | 1025 | 523 | CF_CHECK_EQ(op.UseRFC6979Nonce(), true); | 1026 | 274 | } else { | 1027 | 0 | CF_CHECK_EQ(op.UseRandomNonce(), true); | 1028 | 0 | } | 1029 | | | 1030 | 274 | CF_CHECK_EQ(op.digestType.Get(), CF_DIGEST("SHA256")); | 1031 | | | 1032 | 233 | try { | 1033 | | /* Initialize */ | 1034 | 233 | { | 1035 | | | 1036 | 233 | std::optional<std::string> curveString, algoString; | 1037 | | | 1038 | 233 | CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt); | 1039 | 219 | ::Botan::EC_Group group(*curveString); | 1040 | | | 1041 | | /* Private key */ | 1042 | 219 | { | 1043 | 219 | const ::Botan::BigInt priv_bn(op.priv.ToString(ds)); | 1044 | | | 1045 | | /* Botan appears to generate a new key if the input key is 0, | 1046 | | * so don't do this */ | 1047 | 219 | CF_CHECK_NE(priv_bn, 0); | 1048 | | | 1049 | 209 | priv = std::make_unique<PrivkeyType>(PrivkeyType(rng, group, priv_bn)); | 1050 | 209 | } | 1051 | | | 1052 | | /* Prepare signer */ | 1053 | 209 | CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.digestType.Get()), std::nullopt); | 1054 | | | 1055 | 209 | const std::string emsa1String = Botan_detail::parenthesize("EMSA1", *algoString); | 1056 | 209 | signer.reset(new ::Botan::PK_Signer(*priv, rng, emsa1String, ::Botan::Signature_Format::DerSequence)); | 1057 | 209 | } | 1058 | | | 1059 | | /* Process */ | 1060 | 0 | { | 1061 | 209 | const auto signature = signer->sign_message(op.cleartext.Get(), rng); | 1062 | | | 1063 | | /* Retrieve R and S */ | 1064 | 209 | { | 1065 | 209 | ::Botan::BER_Decoder decoder(signature); | 1066 | 209 | ::Botan::BER_Decoder ber_sig = decoder.start_sequence(); | 1067 | | | 1068 | 209 | size_t count = 0; | 1069 | | | 1070 | 209 | ::Botan::BigInt R; | 1071 | 209 | ::Botan::BigInt S; | 1072 | 491 | while(ber_sig.more_items()) | 1073 | 282 | { | 1074 | 282 | switch ( count ) { | 1075 | 141 | case 0: | 1076 | 141 | ber_sig.decode(R); | 1077 | 141 | break; | 1078 | 141 | case 1: | 1079 | 141 | ber_sig.decode(S); | 1080 | 141 | break; | 1081 | 0 | default: | 1082 | 0 | printf("Error: Too many parts in signature BER\n"); | 1083 | 0 | abort(); | 1084 | 282 | } | 1085 | | | 1086 | 282 | ++count; | 1087 | 282 | } | 1088 | | | 1089 | 209 | if ( op.curveType.Get() == CF_ECC_CURVE("secp256k1") ) { | 1090 | | /* For compatibility with the secp256k1 library. | 1091 | | * See: https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki#low-s-values-in-signatures | 1092 | | */ | 1093 | 31 | if (S > ::Botan::BigInt("57896044618658097711785492504343953926418782139537452191302581570759080747168")) { | 1094 | 20 | S = ::Botan::BigInt("115792089237316195423570985008687907852837564279074904382605163141518161494337") - S; | 1095 | 20 | } | 1096 | 178 | } else if ( op.curveType.Get() == CF_ECC_CURVE("secp256r1") ) { | 1097 | | /* Similar ECDSA signature malleability adjustment for compatibility with trezor-firmware */ | 1098 | 26 | if (S > ::Botan::BigInt("57896044605178124381348723474703786764998477612067880171211129530534256022184")) { | 1099 | 15 | S = ::Botan::BigInt("115792089210356248762697446949407573529996955224135760342422259061068512044369") - S; | 1100 | 15 | } | 1101 | 26 | } | 1102 | | | 1103 | 209 | const auto pub_x = priv->public_point().get_affine_x().to_dec_string(); | 1104 | 209 | const auto pub_y = priv->public_point().get_affine_y().to_dec_string(); | 1105 | | | 1106 | 209 | const auto R_str = R.to_dec_string(); | 1107 | 209 | const auto S_str = S.to_dec_string(); | 1108 | | | 1109 | 209 | ret = component::ECDSA_Signature({ R_str, S_str }, { pub_x, pub_y }); | 1110 | 209 | } | 1111 | 209 | } | 1112 | 209 | } catch ( ... ) { } | 1113 | | | 1114 | 523 | end: | 1115 | 523 | BOTAN_UNSET_GLOBAL_DS | 1116 | | | 1117 | 523 | return ret; | 1118 | 233 | } |
std::__1::optional<cryptofuzz::component::ECDSA_Signature> cryptofuzz::module::Botan_detail::ECxDSA_Sign<Botan::ECGDSA_PrivateKey, cryptofuzz::operation::ECGDSA_Sign, false>(cryptofuzz::operation::ECGDSA_Sign&) Line | Count | Source | 1012 | 129 | std::optional<component::ECDSA_Signature> ECxDSA_Sign(Operation& op) { | 1013 | 129 | std::optional<component::ECDSA_Signature> ret = std::nullopt; | 1014 | 129 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); | 1015 | | | 1016 | 129 | std::unique_ptr<PrivkeyType> priv = nullptr; | 1017 | 129 | std::unique_ptr<::Botan::Public_Key> pub = nullptr; | 1018 | 129 | std::unique_ptr<::Botan::PK_Signer> signer; | 1019 | | | 1020 | 129 | BOTAN_FUZZER_RNG; | 1021 | | | 1022 | 129 | BOTAN_SET_GLOBAL_DS | 1023 | | | 1024 | 129 | if ( RFC6979 == true ) { | 1025 | 0 | CF_CHECK_EQ(op.UseRFC6979Nonce(), true); | 1026 | 129 | } else { | 1027 | 129 | CF_CHECK_EQ(op.UseRandomNonce(), true); | 1028 | 88 | } | 1029 | | | 1030 | 88 | CF_CHECK_EQ(op.digestType.Get(), CF_DIGEST("SHA256")); | 1031 | | | 1032 | 68 | try { | 1033 | | /* Initialize */ | 1034 | 68 | { | 1035 | | | 1036 | 68 | std::optional<std::string> curveString, algoString; | 1037 | | | 1038 | 68 | CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt); | 1039 | 57 | ::Botan::EC_Group group(*curveString); | 1040 | | | 1041 | | /* Private key */ | 1042 | 57 | { | 1043 | 57 | const ::Botan::BigInt priv_bn(op.priv.ToString(ds)); | 1044 | | | 1045 | | /* Botan appears to generate a new key if the input key is 0, | 1046 | | * so don't do this */ | 1047 | 57 | CF_CHECK_NE(priv_bn, 0); | 1048 | | | 1049 | 36 | priv = std::make_unique<PrivkeyType>(PrivkeyType(rng, group, priv_bn)); | 1050 | 36 | } | 1051 | | | 1052 | | /* Prepare signer */ | 1053 | 36 | CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.digestType.Get()), std::nullopt); | 1054 | | | 1055 | 36 | const std::string emsa1String = Botan_detail::parenthesize("EMSA1", *algoString); | 1056 | 36 | signer.reset(new ::Botan::PK_Signer(*priv, rng, emsa1String, ::Botan::Signature_Format::DerSequence)); | 1057 | 36 | } | 1058 | | | 1059 | | /* Process */ | 1060 | 0 | { | 1061 | 36 | const auto signature = signer->sign_message(op.cleartext.Get(), rng); | 1062 | | | 1063 | | /* Retrieve R and S */ | 1064 | 36 | { | 1065 | 36 | ::Botan::BER_Decoder decoder(signature); | 1066 | 36 | ::Botan::BER_Decoder ber_sig = decoder.start_sequence(); | 1067 | | | 1068 | 36 | size_t count = 0; | 1069 | | | 1070 | 36 | ::Botan::BigInt R; | 1071 | 36 | ::Botan::BigInt S; | 1072 | 64 | while(ber_sig.more_items()) | 1073 | 28 | { | 1074 | 28 | switch ( count ) { | 1075 | 14 | case 0: | 1076 | 14 | ber_sig.decode(R); | 1077 | 14 | break; | 1078 | 14 | case 1: | 1079 | 14 | ber_sig.decode(S); | 1080 | 14 | break; | 1081 | 0 | default: | 1082 | 0 | printf("Error: Too many parts in signature BER\n"); | 1083 | 0 | abort(); | 1084 | 28 | } | 1085 | | | 1086 | 28 | ++count; | 1087 | 28 | } | 1088 | | | 1089 | 36 | if ( op.curveType.Get() == CF_ECC_CURVE("secp256k1") ) { | 1090 | | /* For compatibility with the secp256k1 library. | 1091 | | * See: https://github.com/bitcoin/bips/blob/master/bip-0062.mediawiki#low-s-values-in-signatures | 1092 | | */ | 1093 | 2 | if (S > ::Botan::BigInt("57896044618658097711785492504343953926418782139537452191302581570759080747168")) { | 1094 | 1 | S = ::Botan::BigInt("115792089237316195423570985008687907852837564279074904382605163141518161494337") - S; | 1095 | 1 | } | 1096 | 34 | } else if ( op.curveType.Get() == CF_ECC_CURVE("secp256r1") ) { | 1097 | | /* Similar ECDSA signature malleability adjustment for compatibility with trezor-firmware */ | 1098 | 2 | if (S > ::Botan::BigInt("57896044605178124381348723474703786764998477612067880171211129530534256022184")) { | 1099 | 1 | S = ::Botan::BigInt("115792089210356248762697446949407573529996955224135760342422259061068512044369") - S; | 1100 | 1 | } | 1101 | 2 | } | 1102 | | | 1103 | 36 | const auto pub_x = priv->public_point().get_affine_x().to_dec_string(); | 1104 | 36 | const auto pub_y = priv->public_point().get_affine_y().to_dec_string(); | 1105 | | | 1106 | 36 | const auto R_str = R.to_dec_string(); | 1107 | 36 | const auto S_str = S.to_dec_string(); | 1108 | | | 1109 | 36 | ret = component::ECDSA_Signature({ R_str, S_str }, { pub_x, pub_y }); | 1110 | 36 | } | 1111 | 36 | } | 1112 | 36 | } catch ( ... ) { } | 1113 | | | 1114 | 129 | end: | 1115 | 129 | BOTAN_UNSET_GLOBAL_DS | 1116 | | | 1117 | 129 | return ret; | 1118 | 68 | } |
|
1119 | | } /* namespace Botan_detail */ |
1120 | | |
1121 | 793 | std::optional<component::ECDSA_Signature> Botan::OpECDSA_Sign(operation::ECDSA_Sign& op) { |
1122 | 793 | if ( op.curveType.Is(CF_ECC_CURVE("ed25519")) ) { |
1123 | 270 | const auto _priv_bytes = util::DecToBin(op.priv.ToTrimmedString(), 32); |
1124 | 270 | if ( _priv_bytes == std::nullopt ) { |
1125 | 11 | return std::nullopt; |
1126 | 11 | } |
1127 | | |
1128 | 259 | const ::Botan::secure_vector<uint8_t> priv_bytes(_priv_bytes->data(), _priv_bytes->data() + _priv_bytes->size()); |
1129 | | |
1130 | 259 | const auto priv = std::make_unique<::Botan::Ed25519_PrivateKey>(priv_bytes); |
1131 | | |
1132 | 259 | std::unique_ptr<::Botan::PK_Signer> signer; |
1133 | | |
1134 | 259 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
1135 | 259 | BOTAN_FUZZER_RNG; |
1136 | | |
1137 | 259 | signer.reset(new ::Botan::PK_Signer(*priv, rng, "Pure", ::Botan::Signature_Format::Standard)); |
1138 | | |
1139 | 259 | const auto signature = signer->sign_message(op.cleartext.Get(), rng); |
1140 | 259 | CF_ASSERT(signature.size() == 64, "ed25519 signature is not 64 bytes"); |
1141 | | |
1142 | 259 | const auto pub = priv->get_public_key(); |
1143 | 259 | CF_ASSERT(pub.size() == 32, "ed25519 pubkey is not 32 bytes"); |
1144 | | |
1145 | 259 | const auto ret = component::ECDSA_Signature( |
1146 | 259 | { util::BinToDec(signature.data(), 32), util::BinToDec(signature.data() + 32, 32) }, |
1147 | 259 | { util::BinToDec(pub.data(), 32), "0"} |
1148 | 259 | ); |
1149 | | |
1150 | 259 | return ret; |
1151 | 259 | } |
1152 | | |
1153 | 523 | return Botan_detail::ECxDSA_Sign<::Botan::ECDSA_PrivateKey, operation::ECDSA_Sign>(op); |
1154 | 793 | } |
1155 | | |
1156 | 129 | std::optional<component::ECGDSA_Signature> Botan::OpECGDSA_Sign(operation::ECGDSA_Sign& op) { |
1157 | 129 | return Botan_detail::ECxDSA_Sign<::Botan::ECGDSA_PrivateKey, operation::ECGDSA_Sign, false>(op); |
1158 | 129 | } |
1159 | | |
1160 | | namespace Botan_detail { |
1161 | | template <class PubkeyType, class Operation> |
1162 | 661 | std::optional<bool> ECxDSA_Verify(Operation& op) { |
1163 | 661 | std::optional<bool> ret = std::nullopt; |
1164 | 661 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
1165 | | |
1166 | 661 | ::Botan::secure_vector<uint8_t> sig; |
1167 | 661 | std::unique_ptr<::Botan::Public_Key> pub = nullptr; |
1168 | 661 | std::unique_ptr<::Botan::EC_Group> group = nullptr; |
1169 | 661 | Buffer CT; |
1170 | | |
1171 | 661 | { |
1172 | 661 | BOTAN_SET_GLOBAL_DS |
1173 | | |
1174 | 661 | std::optional<std::string> curveString; |
1175 | 661 | CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt); |
1176 | 597 | group = std::make_unique<::Botan::EC_Group>(*curveString); |
1177 | 597 | } |
1178 | | |
1179 | | /* Construct signature */ |
1180 | 0 | { |
1181 | 597 | const ::Botan::BigInt R(op.signature.signature.first.ToString(ds)); |
1182 | 597 | const ::Botan::BigInt S(op.signature.signature.second.ToString(ds)); |
1183 | 597 | try { |
1184 | 597 | sig = ::Botan::BigInt::encode_fixed_length_int_pair(R, S, group->get_order_bytes()); |
1185 | 597 | } catch ( ::Botan::Encoding_Error ) { |
1186 | | /* Invalid signature */ |
1187 | 42 | BOTAN_UNSET_GLOBAL_DS |
1188 | 42 | return false; |
1189 | 42 | } |
1190 | 597 | } |
1191 | | |
1192 | | /* Construct pubkey */ |
1193 | 555 | try { |
1194 | 555 | const ::Botan::BigInt pub_x(op.signature.pub.first.ToString(ds)); |
1195 | 555 | const ::Botan::BigInt pub_y(op.signature.pub.second.ToString(ds)); |
1196 | 555 | const ::Botan::PointGFp public_point = group->point(pub_x, pub_y); |
1197 | 555 | pub = std::make_unique<PubkeyType>(PubkeyType(*group, public_point)); |
1198 | 555 | } catch ( ::Botan::Invalid_Argument ) { |
1199 | | /* Invalid point */ |
1200 | 29 | BOTAN_UNSET_GLOBAL_DS |
1201 | 29 | return false; |
1202 | 29 | } |
1203 | | |
1204 | | /* Construct input */ |
1205 | 526 | { |
1206 | 526 | if ( op.digestType.Get() == CF_DIGEST("NULL") ) { |
1207 | 134 | CT = op.cleartext.ECDSA_RandomPad(ds, op.curveType); |
1208 | 392 | } else { |
1209 | 392 | std::optional<std::string> algoString; |
1210 | 392 | CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.digestType.Get()), std::nullopt); |
1211 | | |
1212 | 273 | auto hash = ::Botan::HashFunction::create(*algoString); |
1213 | 273 | hash->update(op.cleartext.GetPtr(), op.cleartext.GetSize()); |
1214 | 273 | const auto _CT = hash->final(); |
1215 | 273 | CT = Buffer(_CT.data(), _CT.size()).ECDSA_RandomPad(ds, op.curveType); |
1216 | 273 | } |
1217 | 526 | } |
1218 | | |
1219 | 407 | ret = ::Botan::PK_Verifier(*pub, "Raw").verify_message(CT.Get(), sig); |
1220 | | |
1221 | 590 | end: |
1222 | 590 | BOTAN_UNSET_GLOBAL_DS |
1223 | | |
1224 | 590 | return ret; |
1225 | 407 | } std::__1::optional<bool> cryptofuzz::module::Botan_detail::ECxDSA_Verify<Botan::ECDSA_PublicKey, cryptofuzz::operation::ECDSA_Verify>(cryptofuzz::operation::ECDSA_Verify&) Line | Count | Source | 1162 | 416 | std::optional<bool> ECxDSA_Verify(Operation& op) { | 1163 | 416 | std::optional<bool> ret = std::nullopt; | 1164 | 416 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); | 1165 | | | 1166 | 416 | ::Botan::secure_vector<uint8_t> sig; | 1167 | 416 | std::unique_ptr<::Botan::Public_Key> pub = nullptr; | 1168 | 416 | std::unique_ptr<::Botan::EC_Group> group = nullptr; | 1169 | 416 | Buffer CT; | 1170 | | | 1171 | 416 | { | 1172 | 416 | BOTAN_SET_GLOBAL_DS | 1173 | | | 1174 | 416 | std::optional<std::string> curveString; | 1175 | 416 | CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt); | 1176 | 387 | group = std::make_unique<::Botan::EC_Group>(*curveString); | 1177 | 387 | } | 1178 | | | 1179 | | /* Construct signature */ | 1180 | 0 | { | 1181 | 387 | const ::Botan::BigInt R(op.signature.signature.first.ToString(ds)); | 1182 | 387 | const ::Botan::BigInt S(op.signature.signature.second.ToString(ds)); | 1183 | 387 | try { | 1184 | 387 | sig = ::Botan::BigInt::encode_fixed_length_int_pair(R, S, group->get_order_bytes()); | 1185 | 387 | } catch ( ::Botan::Encoding_Error ) { | 1186 | | /* Invalid signature */ | 1187 | 25 | BOTAN_UNSET_GLOBAL_DS | 1188 | 25 | return false; | 1189 | 25 | } | 1190 | 387 | } | 1191 | | | 1192 | | /* Construct pubkey */ | 1193 | 362 | try { | 1194 | 362 | const ::Botan::BigInt pub_x(op.signature.pub.first.ToString(ds)); | 1195 | 362 | const ::Botan::BigInt pub_y(op.signature.pub.second.ToString(ds)); | 1196 | 362 | const ::Botan::PointGFp public_point = group->point(pub_x, pub_y); | 1197 | 362 | pub = std::make_unique<PubkeyType>(PubkeyType(*group, public_point)); | 1198 | 362 | } catch ( ::Botan::Invalid_Argument ) { | 1199 | | /* Invalid point */ | 1200 | 19 | BOTAN_UNSET_GLOBAL_DS | 1201 | 19 | return false; | 1202 | 19 | } | 1203 | | | 1204 | | /* Construct input */ | 1205 | 343 | { | 1206 | 343 | if ( op.digestType.Get() == CF_DIGEST("NULL") ) { | 1207 | 108 | CT = op.cleartext.ECDSA_RandomPad(ds, op.curveType); | 1208 | 235 | } else { | 1209 | 235 | std::optional<std::string> algoString; | 1210 | 235 | CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.digestType.Get()), std::nullopt); | 1211 | | | 1212 | 178 | auto hash = ::Botan::HashFunction::create(*algoString); | 1213 | 178 | hash->update(op.cleartext.GetPtr(), op.cleartext.GetSize()); | 1214 | 178 | const auto _CT = hash->final(); | 1215 | 178 | CT = Buffer(_CT.data(), _CT.size()).ECDSA_RandomPad(ds, op.curveType); | 1216 | 178 | } | 1217 | 343 | } | 1218 | | | 1219 | 286 | ret = ::Botan::PK_Verifier(*pub, "Raw").verify_message(CT.Get(), sig); | 1220 | | | 1221 | 372 | end: | 1222 | 372 | BOTAN_UNSET_GLOBAL_DS | 1223 | | | 1224 | 372 | return ret; | 1225 | 286 | } |
std::__1::optional<bool> cryptofuzz::module::Botan_detail::ECxDSA_Verify<Botan::ECGDSA_PublicKey, cryptofuzz::operation::ECGDSA_Verify>(cryptofuzz::operation::ECGDSA_Verify&) Line | Count | Source | 1162 | 245 | std::optional<bool> ECxDSA_Verify(Operation& op) { | 1163 | 245 | std::optional<bool> ret = std::nullopt; | 1164 | 245 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); | 1165 | | | 1166 | 245 | ::Botan::secure_vector<uint8_t> sig; | 1167 | 245 | std::unique_ptr<::Botan::Public_Key> pub = nullptr; | 1168 | 245 | std::unique_ptr<::Botan::EC_Group> group = nullptr; | 1169 | 245 | Buffer CT; | 1170 | | | 1171 | 245 | { | 1172 | 245 | BOTAN_SET_GLOBAL_DS | 1173 | | | 1174 | 245 | std::optional<std::string> curveString; | 1175 | 245 | CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt); | 1176 | 210 | group = std::make_unique<::Botan::EC_Group>(*curveString); | 1177 | 210 | } | 1178 | | | 1179 | | /* Construct signature */ | 1180 | 0 | { | 1181 | 210 | const ::Botan::BigInt R(op.signature.signature.first.ToString(ds)); | 1182 | 210 | const ::Botan::BigInt S(op.signature.signature.second.ToString(ds)); | 1183 | 210 | try { | 1184 | 210 | sig = ::Botan::BigInt::encode_fixed_length_int_pair(R, S, group->get_order_bytes()); | 1185 | 210 | } catch ( ::Botan::Encoding_Error ) { | 1186 | | /* Invalid signature */ | 1187 | 17 | BOTAN_UNSET_GLOBAL_DS | 1188 | 17 | return false; | 1189 | 17 | } | 1190 | 210 | } | 1191 | | | 1192 | | /* Construct pubkey */ | 1193 | 193 | try { | 1194 | 193 | const ::Botan::BigInt pub_x(op.signature.pub.first.ToString(ds)); | 1195 | 193 | const ::Botan::BigInt pub_y(op.signature.pub.second.ToString(ds)); | 1196 | 193 | const ::Botan::PointGFp public_point = group->point(pub_x, pub_y); | 1197 | 193 | pub = std::make_unique<PubkeyType>(PubkeyType(*group, public_point)); | 1198 | 193 | } catch ( ::Botan::Invalid_Argument ) { | 1199 | | /* Invalid point */ | 1200 | 10 | BOTAN_UNSET_GLOBAL_DS | 1201 | 10 | return false; | 1202 | 10 | } | 1203 | | | 1204 | | /* Construct input */ | 1205 | 183 | { | 1206 | 183 | if ( op.digestType.Get() == CF_DIGEST("NULL") ) { | 1207 | 26 | CT = op.cleartext.ECDSA_RandomPad(ds, op.curveType); | 1208 | 157 | } else { | 1209 | 157 | std::optional<std::string> algoString; | 1210 | 157 | CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.digestType.Get()), std::nullopt); | 1211 | | | 1212 | 95 | auto hash = ::Botan::HashFunction::create(*algoString); | 1213 | 95 | hash->update(op.cleartext.GetPtr(), op.cleartext.GetSize()); | 1214 | 95 | const auto _CT = hash->final(); | 1215 | 95 | CT = Buffer(_CT.data(), _CT.size()).ECDSA_RandomPad(ds, op.curveType); | 1216 | 95 | } | 1217 | 183 | } | 1218 | | | 1219 | 121 | ret = ::Botan::PK_Verifier(*pub, "Raw").verify_message(CT.Get(), sig); | 1220 | | | 1221 | 218 | end: | 1222 | 218 | BOTAN_UNSET_GLOBAL_DS | 1223 | | | 1224 | 218 | return ret; | 1225 | 121 | } |
|
1226 | | } /* namespace Botan_detail */ |
1227 | | |
1228 | 856 | std::optional<bool> Botan::OpECDSA_Verify(operation::ECDSA_Verify& op) { |
1229 | 856 | if ( op.curveType.Is(CF_ECC_CURVE("ed25519")) ) { |
1230 | 440 | const auto pub_bytes = util::DecToBin(op.signature.pub.first.ToTrimmedString(), 32); |
1231 | 440 | if ( pub_bytes == std::nullopt ) { |
1232 | 10 | return std::nullopt; |
1233 | 10 | } |
1234 | 430 | const auto pub = std::make_unique<::Botan::Ed25519_PublicKey>(*pub_bytes); |
1235 | | |
1236 | 430 | const auto sig_r = util::DecToBin(op.signature.signature.first.ToTrimmedString(), 32); |
1237 | 430 | if ( sig_r == std::nullopt ) { |
1238 | 14 | return std::nullopt; |
1239 | 14 | } |
1240 | | |
1241 | 416 | const auto sig_s = util::DecToBin(op.signature.signature.second.ToTrimmedString(), 32); |
1242 | 416 | if ( sig_s == std::nullopt ) { |
1243 | 15 | return std::nullopt; |
1244 | 15 | } |
1245 | | |
1246 | 401 | std::vector<uint8_t> sig_bytes(64); |
1247 | 401 | memcpy(sig_bytes.data(), sig_r->data(), 32); |
1248 | 401 | memcpy(sig_bytes.data() + 32, sig_s->data(), 32); |
1249 | | |
1250 | 401 | const bool ret = ::Botan::PK_Verifier(*pub, "Pure").verify_message(op.cleartext.Get(), sig_bytes); |
1251 | 401 | return ret; |
1252 | | |
1253 | 416 | } else { |
1254 | 416 | return Botan_detail::ECxDSA_Verify<::Botan::ECDSA_PublicKey, operation::ECDSA_Verify>(op); |
1255 | 416 | } |
1256 | 856 | } |
1257 | | |
1258 | 245 | std::optional<bool> Botan::OpECGDSA_Verify(operation::ECGDSA_Verify& op) { |
1259 | 245 | return Botan_detail::ECxDSA_Verify<::Botan::ECGDSA_PublicKey, operation::ECGDSA_Verify>(op); |
1260 | 245 | } |
1261 | | |
1262 | 718 | std::optional<component::ECC_PublicKey> Botan::OpECDSA_Recover(operation::ECDSA_Recover& op) { |
1263 | 718 | std::optional<component::ECC_PublicKey> ret = std::nullopt; |
1264 | 718 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
1265 | | |
1266 | 718 | std::unique_ptr<::Botan::EC_Group> group = nullptr; |
1267 | 718 | Buffer CT; |
1268 | | |
1269 | 718 | { |
1270 | 718 | std::optional<std::string> curveString; |
1271 | 718 | CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt); |
1272 | 686 | group = std::make_unique<::Botan::EC_Group>(*curveString); |
1273 | 686 | } |
1274 | | |
1275 | | /* Construct input */ |
1276 | 0 | { |
1277 | 686 | if ( op.digestType.Get() == CF_DIGEST("NULL") ) { |
1278 | 187 | CT = op.cleartext.ECDSA_RandomPad(ds, op.curveType); |
1279 | 499 | } else { |
1280 | 499 | std::optional<std::string> algoString; |
1281 | 499 | CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.digestType.Get()), std::nullopt); |
1282 | | |
1283 | 426 | auto hash = ::Botan::HashFunction::create(*algoString); |
1284 | 426 | hash->update(op.cleartext.GetPtr(), op.cleartext.GetSize()); |
1285 | 426 | const auto _CT = hash->final(); |
1286 | 426 | CT = Buffer(_CT.data(), _CT.size()).ECDSA_RandomPad(ds, op.curveType); |
1287 | 426 | } |
1288 | 686 | } |
1289 | | |
1290 | 613 | { |
1291 | 613 | const ::Botan::BigInt R(op.signature.first.ToString(ds)); |
1292 | 613 | const ::Botan::BigInt S(op.signature.second.ToString(ds)); |
1293 | | |
1294 | 613 | std::unique_ptr<::Botan::ECDSA_PublicKey> pub = nullptr; |
1295 | 613 | try { |
1296 | 613 | pub = std::make_unique<::Botan::ECDSA_PublicKey>(*group, CT.Get(), R, S, op.id); |
1297 | | |
1298 | 613 | ret = { |
1299 | 613 | pub->public_point().get_affine_x().to_dec_string(), |
1300 | 613 | pub->public_point().get_affine_y().to_dec_string() |
1301 | 613 | }; |
1302 | 613 | } catch ( ::Botan::Invalid_State& e ) { |
1303 | 89 | } catch ( ::Botan::Decoding_Error& ) { |
1304 | 89 | } catch ( ::Botan::Invalid_Argument& ) { |
1305 | | //ret = {"0", "0"}; |
1306 | 66 | } |
1307 | | |
1308 | 613 | } |
1309 | | |
1310 | 718 | end: |
1311 | 718 | return ret; |
1312 | 613 | } |
1313 | | |
1314 | 354 | std::optional<component::Bignum> Botan::OpDH_Derive(operation::DH_Derive& op) { |
1315 | 354 | std::optional<component::Bignum> ret = std::nullopt; |
1316 | 354 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
1317 | | |
1318 | 354 | BOTAN_FUZZER_RNG; |
1319 | | |
1320 | 354 | try { |
1321 | 354 | CF_CHECK_NE(op.priv.ToTrimmedString(), "0"); |
1322 | | |
1323 | 300 | const ::Botan::BigInt g(op.base.ToString(ds)); |
1324 | 300 | const ::Botan::BigInt p(op.prime.ToString(ds)); |
1325 | 300 | const ::Botan::DL_Group grp(p, g); |
1326 | | |
1327 | 300 | const ::Botan::BigInt _priv(op.priv.ToString(ds)); |
1328 | | |
1329 | | /* Prevent time-out */ |
1330 | 300 | CF_CHECK_LT(g.bytes(), 80); |
1331 | 281 | CF_CHECK_LT(p.bytes(), 80); |
1332 | 214 | CF_CHECK_LT(_priv.bytes(), 80); |
1333 | | |
1334 | 203 | std::unique_ptr<::Botan::Private_Key> priv(new ::Botan::DH_PrivateKey(grp, _priv)); |
1335 | | |
1336 | 203 | const ::Botan::BigInt _pub(op.pub.ToString(ds)); |
1337 | 203 | ::Botan::DH_PublicKey pub(grp, _pub); |
1338 | | |
1339 | 203 | std::unique_ptr<::Botan::PK_Key_Agreement> kas(new ::Botan::PK_Key_Agreement(*priv, rng, "Raw")); |
1340 | 203 | const auto derived_key = kas->derive_key(0, pub.public_value()); |
1341 | | |
1342 | 203 | const auto derived_str = ::Botan::BigInt(derived_key.bits_of()).to_dec_string(); |
1343 | 203 | if ( derived_str != "0" ) { |
1344 | 48 | ret = derived_str; |
1345 | 48 | } |
1346 | 203 | } catch ( ... ) { } |
1347 | | |
1348 | 354 | end: |
1349 | 354 | return ret; |
1350 | 354 | } |
1351 | | |
1352 | 157 | std::optional<component::ECC_Point> Botan::OpECC_Point_Add(operation::ECC_Point_Add& op) { |
1353 | 157 | std::optional<component::ECC_Point> ret = std::nullopt; |
1354 | 157 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
1355 | | |
1356 | 157 | BOTAN_FUZZER_RNG; |
1357 | | |
1358 | 157 | std::unique_ptr<::Botan::EC_Group> group = nullptr; |
1359 | 157 | std::unique_ptr<::Botan::PointGFp> a, b; |
1360 | | |
1361 | 157 | { |
1362 | 157 | std::optional<std::string> curveString; |
1363 | 157 | CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt); |
1364 | 115 | group = std::make_unique<::Botan::EC_Group>(*curveString); |
1365 | 115 | } |
1366 | | |
1367 | 0 | { |
1368 | | /* A */ |
1369 | 115 | { |
1370 | 115 | const auto a_x = ::Botan::BigInt(op.a.first.ToString(ds)); |
1371 | 115 | CF_CHECK_GTE(a_x, 0); |
1372 | | |
1373 | 115 | const auto a_y = ::Botan::BigInt(op.a.second.ToString(ds)); |
1374 | 115 | CF_CHECK_GTE(a_y, 0); |
1375 | | |
1376 | 115 | try { |
1377 | 115 | a = std::make_unique<::Botan::PointGFp>(group->point(a_x, a_y)); |
1378 | 115 | } catch ( ::Botan::Invalid_Argument ) { |
1379 | 12 | goto end; |
1380 | 12 | } |
1381 | 103 | CF_CHECK_TRUE(a->on_the_curve()); |
1382 | 69 | } |
1383 | | |
1384 | | /* B */ |
1385 | 0 | { |
1386 | 69 | const auto b_x = ::Botan::BigInt(op.b.first.ToString(ds)); |
1387 | 69 | CF_CHECK_GTE(b_x, 0); |
1388 | | |
1389 | 69 | const auto b_y = ::Botan::BigInt(op.b.second.ToString(ds)); |
1390 | 69 | CF_CHECK_GTE(b_y, 0); |
1391 | | |
1392 | 69 | try { |
1393 | 69 | b = std::make_unique<::Botan::PointGFp>(group->point(b_x, b_y)); |
1394 | 69 | } catch ( ::Botan::Invalid_Argument ) { |
1395 | 10 | goto end; |
1396 | 10 | } |
1397 | | |
1398 | 59 | CF_CHECK_TRUE(b->on_the_curve()); |
1399 | 38 | } |
1400 | | |
1401 | 0 | const bool is_negation = *a == -(*b); |
1402 | | |
1403 | 38 | ::Botan::PointGFp _res = *a + *b; |
1404 | | |
1405 | 38 | const bool is_zero = _res.is_zero(); |
1406 | | |
1407 | | /* If A is a negation of B, then addition of both should result in point at infinity */ |
1408 | | /* Otherwise, it should result in non-infinity. */ |
1409 | 38 | CF_ASSERT(is_zero == is_negation, "Unexpected point addition result"); |
1410 | 38 | CF_CHECK_FALSE(is_zero); |
1411 | | |
1412 | 28 | const auto x = _res.get_affine_x(); |
1413 | 28 | const auto y = _res.get_affine_y(); |
1414 | | |
1415 | 28 | ret = { |
1416 | 28 | util::HexToDec(x.to_hex_string()), |
1417 | 28 | util::HexToDec(y.to_hex_string()), |
1418 | 28 | }; |
1419 | | |
1420 | 28 | } |
1421 | | |
1422 | 157 | end: |
1423 | 157 | return ret; |
1424 | 28 | } |
1425 | | |
1426 | 126 | std::optional<component::ECC_Point> Botan::OpECC_Point_Sub(operation::ECC_Point_Sub& op) { |
1427 | 126 | std::optional<component::ECC_Point> ret = std::nullopt; |
1428 | 126 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
1429 | | |
1430 | 126 | BOTAN_FUZZER_RNG; |
1431 | | |
1432 | 126 | std::unique_ptr<::Botan::EC_Group> group = nullptr; |
1433 | 126 | std::unique_ptr<::Botan::PointGFp> a, b; |
1434 | | |
1435 | 126 | { |
1436 | 126 | std::optional<std::string> curveString; |
1437 | 126 | CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt); |
1438 | 92 | group = std::make_unique<::Botan::EC_Group>(*curveString); |
1439 | 92 | } |
1440 | | |
1441 | 0 | { |
1442 | | /* A */ |
1443 | 92 | { |
1444 | 92 | const auto a_x = ::Botan::BigInt(op.a.first.ToString(ds)); |
1445 | 92 | CF_CHECK_GTE(a_x, 0); |
1446 | | |
1447 | 92 | const auto a_y = ::Botan::BigInt(op.a.second.ToString(ds)); |
1448 | 92 | CF_CHECK_GTE(a_y, 0); |
1449 | | |
1450 | 92 | try { |
1451 | 92 | a = std::make_unique<::Botan::PointGFp>(group->point(a_x, a_y)); |
1452 | 92 | } catch ( ::Botan::Invalid_Argument ) { |
1453 | 14 | goto end; |
1454 | 14 | } |
1455 | 78 | CF_CHECK_TRUE(a->on_the_curve()); |
1456 | 54 | } |
1457 | | |
1458 | | /* B */ |
1459 | 0 | { |
1460 | 54 | const auto b_x = ::Botan::BigInt(op.b.first.ToString(ds)); |
1461 | 54 | CF_CHECK_GTE(b_x, 0); |
1462 | | |
1463 | 54 | const auto b_y = ::Botan::BigInt(op.b.second.ToString(ds)); |
1464 | 54 | CF_CHECK_GTE(b_y, 0); |
1465 | | |
1466 | 54 | try { |
1467 | 54 | b = std::make_unique<::Botan::PointGFp>(group->point(b_x, b_y)); |
1468 | 54 | } catch ( ::Botan::Invalid_Argument ) { |
1469 | 10 | goto end; |
1470 | 10 | } |
1471 | | |
1472 | 44 | CF_CHECK_TRUE(b->on_the_curve()); |
1473 | 33 | } |
1474 | | |
1475 | 0 | const bool is_eq = *a == *b; |
1476 | | |
1477 | 33 | ::Botan::PointGFp _res = *a - *b; |
1478 | | |
1479 | 33 | const bool is_zero = _res.is_zero(); |
1480 | | |
1481 | | /* If A equals B, then subtraction of both should result in point at infinity */ |
1482 | | /* Otherwise, it should result in non-infinity. */ |
1483 | 33 | CF_ASSERT(is_zero == is_eq, "Unexpected point subtraction result"); |
1484 | 33 | CF_CHECK_FALSE(is_zero); |
1485 | | |
1486 | 19 | const auto x = _res.get_affine_x(); |
1487 | 19 | const auto y = _res.get_affine_y(); |
1488 | | |
1489 | 19 | ret = { |
1490 | 19 | util::HexToDec(x.to_hex_string()), |
1491 | 19 | util::HexToDec(y.to_hex_string()), |
1492 | 19 | }; |
1493 | | |
1494 | 19 | } |
1495 | | |
1496 | 126 | end: |
1497 | 126 | return ret; |
1498 | 19 | } |
1499 | | |
1500 | 599 | std::optional<component::ECC_Point> Botan::OpECC_Point_Mul(operation::ECC_Point_Mul& op) { |
1501 | 599 | std::optional<component::ECC_Point> ret = std::nullopt; |
1502 | 599 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
1503 | | |
1504 | 599 | BOTAN_FUZZER_RNG; |
1505 | | |
1506 | 599 | std::unique_ptr<::Botan::EC_Group> group = nullptr; |
1507 | | |
1508 | 599 | { |
1509 | 599 | std::optional<std::string> curveString; |
1510 | 599 | CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt); |
1511 | 549 | group = std::make_unique<::Botan::EC_Group>(*curveString); |
1512 | 549 | } |
1513 | | |
1514 | 549 | try { |
1515 | 549 | const auto a_x = ::Botan::BigInt(op.a.first.ToString(ds)); |
1516 | 549 | CF_CHECK_GTE(a_x, 0); |
1517 | | |
1518 | 549 | const auto a_y = ::Botan::BigInt(op.a.second.ToString(ds)); |
1519 | 549 | CF_CHECK_GTE(a_y, 0); |
1520 | | |
1521 | 549 | const auto a = group->point(a_x, a_y); |
1522 | 549 | CF_CHECK_TRUE(a.on_the_curve()); |
1523 | | |
1524 | 443 | const auto b = ::Botan::BigInt(op.b.ToString(ds)); |
1525 | | |
1526 | 443 | CF_CHECK_GTE(b, 0); |
1527 | | |
1528 | 443 | std::vector<::Botan::BigInt> ws(::Botan::PointGFp::WORKSPACE_SIZE); |
1529 | | |
1530 | 443 | bool useBlinding = false; |
1531 | | #if defined(CRYPTOFUZZ_BOTAN_IS_ORACLE) |
1532 | | try { |
1533 | | useBlinding = ds.Get<bool>(); |
1534 | | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } |
1535 | | #endif |
1536 | | |
1537 | 443 | ::Botan::PointGFp _res; |
1538 | | |
1539 | 443 | if ( useBlinding == false ) { |
1540 | 112 | _res = a * b; |
1541 | 331 | } else { |
1542 | 331 | _res = group->blinded_var_point_multiply(a, b, rng, ws); |
1543 | 331 | } |
1544 | | |
1545 | 443 | const auto x = _res.get_affine_x(); |
1546 | 443 | const auto y = _res.get_affine_y(); |
1547 | | |
1548 | 443 | ret = { |
1549 | 443 | util::HexToDec(x.to_hex_string()), |
1550 | 443 | util::HexToDec(y.to_hex_string()), |
1551 | 443 | }; |
1552 | | |
1553 | 443 | } catch ( ... ) { } |
1554 | | |
1555 | 599 | end: |
1556 | 599 | return ret; |
1557 | 549 | } |
1558 | | |
1559 | 129 | std::optional<component::ECC_Point> Botan::OpECC_Point_Neg(operation::ECC_Point_Neg& op) { |
1560 | 129 | std::optional<component::ECC_Point> ret = std::nullopt; |
1561 | 129 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
1562 | | |
1563 | 129 | std::unique_ptr<::Botan::EC_Group> group = nullptr; |
1564 | | |
1565 | 129 | { |
1566 | 129 | std::optional<std::string> curveString; |
1567 | 129 | CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt); |
1568 | 90 | group = std::make_unique<::Botan::EC_Group>(*curveString); |
1569 | 90 | } |
1570 | | |
1571 | 90 | try { |
1572 | 90 | const auto a_x = ::Botan::BigInt(op.a.first.ToString(ds)); |
1573 | 90 | CF_CHECK_GTE(a_x, 0); |
1574 | | |
1575 | 90 | const auto a_y = ::Botan::BigInt(op.a.second.ToString(ds)); |
1576 | 90 | CF_CHECK_GTE(a_y, 0); |
1577 | | |
1578 | 90 | const auto a = group->point(a_x, a_y); |
1579 | 90 | CF_CHECK_TRUE(a.on_the_curve()); |
1580 | | |
1581 | 66 | const ::Botan::PointGFp _res = -a; |
1582 | | |
1583 | 66 | const auto x = _res.get_affine_x(); |
1584 | 66 | const auto y = _res.get_affine_y(); |
1585 | | |
1586 | 66 | ret = { |
1587 | 66 | util::HexToDec(x.to_hex_string()), |
1588 | 66 | util::HexToDec(y.to_hex_string()), |
1589 | 66 | }; |
1590 | | |
1591 | 66 | } catch ( ... ) { } |
1592 | | |
1593 | 129 | end: |
1594 | 129 | return ret; |
1595 | 90 | } |
1596 | | |
1597 | 123 | std::optional<component::ECC_Point> Botan::OpECC_Point_Dbl(operation::ECC_Point_Dbl& op) { |
1598 | 123 | std::optional<component::ECC_Point> ret = std::nullopt; |
1599 | 123 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
1600 | | |
1601 | 123 | std::unique_ptr<::Botan::EC_Group> group = nullptr; |
1602 | | |
1603 | 123 | { |
1604 | 123 | std::optional<std::string> curveString; |
1605 | 123 | CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt); |
1606 | 70 | group = std::make_unique<::Botan::EC_Group>(*curveString); |
1607 | 70 | } |
1608 | | |
1609 | 70 | try { |
1610 | 70 | const auto a_x = ::Botan::BigInt(op.a.first.ToString(ds)); |
1611 | 70 | CF_CHECK_GTE(a_x, 0); |
1612 | | |
1613 | 70 | const auto a_y = ::Botan::BigInt(op.a.second.ToString(ds)); |
1614 | 70 | CF_CHECK_GTE(a_y, 0); |
1615 | | |
1616 | 70 | const auto a = group->point(a_x, a_y); |
1617 | 70 | CF_CHECK_TRUE(a.on_the_curve()); |
1618 | | |
1619 | 41 | const ::Botan::PointGFp _res = a + a; |
1620 | | |
1621 | 41 | const auto x = _res.get_affine_x(); |
1622 | 41 | const auto y = _res.get_affine_y(); |
1623 | | |
1624 | 41 | ret = { |
1625 | 41 | util::HexToDec(x.to_hex_string()), |
1626 | 41 | util::HexToDec(y.to_hex_string()), |
1627 | 41 | }; |
1628 | | |
1629 | 41 | } catch ( ... ) { } |
1630 | | |
1631 | 123 | end: |
1632 | 123 | return ret; |
1633 | 70 | } |
1634 | | |
1635 | 155 | std::optional<bool> Botan::OpECC_Point_Cmp(operation::ECC_Point_Cmp& op) { |
1636 | 155 | std::optional<bool> ret = std::nullopt; |
1637 | 155 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
1638 | | |
1639 | 155 | BOTAN_FUZZER_RNG; |
1640 | | |
1641 | 155 | std::unique_ptr<::Botan::EC_Group> group = nullptr; |
1642 | 155 | std::unique_ptr<::Botan::PointGFp> a, b; |
1643 | | |
1644 | 155 | { |
1645 | 155 | std::optional<std::string> curveString; |
1646 | 155 | CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt); |
1647 | 104 | group = std::make_unique<::Botan::EC_Group>(*curveString); |
1648 | 104 | } |
1649 | | |
1650 | 0 | { |
1651 | | /* A */ |
1652 | 104 | { |
1653 | 104 | const auto a_x = ::Botan::BigInt(op.a.first.ToString(ds)); |
1654 | 104 | CF_CHECK_GTE(a_x, 0); |
1655 | | |
1656 | 104 | const auto a_y = ::Botan::BigInt(op.a.second.ToString(ds)); |
1657 | 104 | CF_CHECK_GTE(a_y, 0); |
1658 | | |
1659 | 104 | try { |
1660 | 104 | a = std::make_unique<::Botan::PointGFp>(group->point(a_x, a_y)); |
1661 | 104 | } catch ( ::Botan::Invalid_Argument ) { |
1662 | 10 | goto end; |
1663 | 10 | } |
1664 | 94 | CF_CHECK_TRUE(a->on_the_curve()); |
1665 | 61 | } |
1666 | | |
1667 | | /* B */ |
1668 | 0 | { |
1669 | 61 | const auto b_x = ::Botan::BigInt(op.b.first.ToString(ds)); |
1670 | 61 | CF_CHECK_GTE(b_x, 0); |
1671 | | |
1672 | 61 | const auto b_y = ::Botan::BigInt(op.b.second.ToString(ds)); |
1673 | 61 | CF_CHECK_GTE(b_y, 0); |
1674 | | |
1675 | 61 | try { |
1676 | 61 | b = std::make_unique<::Botan::PointGFp>(group->point(b_x, b_y)); |
1677 | 61 | } catch ( ::Botan::Invalid_Argument ) { |
1678 | 10 | goto end; |
1679 | 10 | } |
1680 | | |
1681 | 51 | CF_CHECK_TRUE(b->on_the_curve()); |
1682 | 23 | } |
1683 | | |
1684 | 0 | ret = *a == *b; |
1685 | 23 | } |
1686 | | |
1687 | 155 | end: |
1688 | 155 | return ret; |
1689 | 23 | } |
1690 | | |
1691 | 388 | std::optional<bool> Botan::OpDSA_Verify(operation::DSA_Verify& op) { |
1692 | 388 | std::optional<bool> ret = std::nullopt; |
1693 | 388 | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
1694 | | |
1695 | 388 | BOTAN_FUZZER_RNG; |
1696 | | |
1697 | 388 | try { |
1698 | 388 | const auto p = ::Botan::BigInt(op.parameters.p.ToString(ds)); |
1699 | | /* Avoid time-outs */ |
1700 | 388 | CF_CHECK_LTE(p.bytes(), 300); |
1701 | 371 | const auto q = ::Botan::BigInt(op.parameters.q.ToString(ds)); |
1702 | 371 | const auto g = ::Botan::BigInt(op.parameters.g.ToString(ds)); |
1703 | | |
1704 | | /* Botan can verify signatures with g = 0. |
1705 | | * Avoid discrepancies with OpenSSL |
1706 | | */ |
1707 | 371 | CF_CHECK_NE(g, 0); |
1708 | | |
1709 | 340 | const ::Botan::DL_Group group(p, q, g); |
1710 | 340 | CF_CHECK_TRUE(group.verify_group(rng)); |
1711 | | |
1712 | 223 | const auto y = ::Botan::BigInt(op.pub.ToString(ds)); |
1713 | 223 | const auto pub = std::make_unique<::Botan::DSA_PublicKey>(group, y); |
1714 | | |
1715 | 223 | const auto r = ::Botan::BigInt(op.signature.first.ToString(ds)); |
1716 | 223 | const auto s = ::Botan::BigInt(op.signature.second.ToString(ds)); |
1717 | | |
1718 | 223 | const auto sig = ::Botan::BigInt::encode_fixed_length_int_pair( |
1719 | 223 | r, s, q.bytes()); |
1720 | 223 | auto verifier = ::Botan::PK_Verifier(*pub, "Raw"); |
1721 | 223 | verifier.update(op.cleartext.Get()); |
1722 | 223 | ret = verifier.check_signature(sig); |
1723 | 223 | } catch ( ... ) { |
1724 | 83 | } |
1725 | | |
1726 | 388 | end: |
1727 | 388 | return ret; |
1728 | 388 | } |
1729 | | |
1730 | 8.02k | std::optional<component::Bignum> Botan::OpBignumCalc(operation::BignumCalc& op) { |
1731 | 8.02k | std::optional<component::Bignum> ret = std::nullopt; |
1732 | | |
1733 | 8.02k | if ( op.modulo ) { |
1734 | 1.89k | switch ( op.calcOp.Get() ) { |
1735 | 20 | case CF_CALCOP("Add(A,B)"): |
1736 | 37 | case CF_CALCOP("Bit(A,B)"): |
1737 | 139 | case CF_CALCOP("CondSet(A,B)"): |
1738 | 175 | case CF_CALCOP("Exp(A,B)"): |
1739 | 205 | case CF_CALCOP("InvMod(A,B)"): |
1740 | 241 | case CF_CALCOP("IsEq(A,B)"): |
1741 | 252 | case CF_CALCOP("IsEven(A)"): |
1742 | 267 | case CF_CALCOP("IsOdd(A)"): |
1743 | 317 | case CF_CALCOP("IsOne(A)"): |
1744 | 344 | case CF_CALCOP("IsZero(A)"): |
1745 | 366 | case CF_CALCOP("LShift1(A)"): |
1746 | 391 | case CF_CALCOP("Mul(A,B)"): |
1747 | 434 | case CF_CALCOP("Not(A)"): |
1748 | 458 | case CF_CALCOP("NumBits(A)"): |
1749 | 508 | case CF_CALCOP("RShift(A,B)"): |
1750 | 545 | case CF_CALCOP("Set(A)"): |
1751 | 597 | case CF_CALCOP("Sqr(A)"): |
1752 | 727 | case CF_CALCOP("Sqrt(A)"): |
1753 | 753 | case CF_CALCOP("Sub(A,B)"): |
1754 | 753 | break; |
1755 | 1.14k | default: |
1756 | 1.14k | return ret; |
1757 | 1.89k | } |
1758 | 1.89k | } |
1759 | 6.88k | Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize()); |
1760 | | |
1761 | 6.88k | Botan_bignum::Bignum res(&ds, "0"); |
1762 | 6.88k | std::vector<Botan_bignum::Bignum> bn{ |
1763 | 6.88k | Botan_bignum::Bignum(&ds, op.bn0.ToString(ds)), |
1764 | 6.88k | Botan_bignum::Bignum(&ds, op.bn1.ToString(ds)), |
1765 | 6.88k | Botan_bignum::Bignum(&ds, op.bn2.ToString(ds)), |
1766 | 6.88k | Botan_bignum::Bignum(&ds, op.bn3.ToString(ds)) |
1767 | 6.88k | }; |
1768 | 6.88k | std::unique_ptr<Botan_bignum::Operation> opRunner = nullptr; |
1769 | | |
1770 | 6.88k | switch ( op.calcOp.Get() ) { |
1771 | 49 | case CF_CALCOP("Add(A,B)"): |
1772 | 49 | opRunner = std::make_unique<Botan_bignum::Add>(); |
1773 | 49 | break; |
1774 | 45 | case CF_CALCOP("Sub(A,B)"): |
1775 | 45 | opRunner = std::make_unique<Botan_bignum::Sub>(); |
1776 | 45 | break; |
1777 | 62 | case CF_CALCOP("Mul(A,B)"): |
1778 | 62 | opRunner = std::make_unique<Botan_bignum::Mul>(); |
1779 | 62 | break; |
1780 | 155 | case CF_CALCOP("Div(A,B)"): |
1781 | 155 | opRunner = std::make_unique<Botan_bignum::Div>(); |
1782 | 155 | break; |
1783 | 187 | case CF_CALCOP("Mod(A,B)"): |
1784 | 187 | opRunner = std::make_unique<Botan_bignum::Mod>(); |
1785 | 187 | break; |
1786 | 387 | case CF_CALCOP("ExpMod(A,B,C)"): |
1787 | | /* Too slow with larger values */ |
1788 | 387 | CF_CHECK_LT(op.bn0.GetSize(), 1000); |
1789 | 384 | CF_CHECK_LT(op.bn1.GetSize(), 1000); |
1790 | 383 | CF_CHECK_LT(op.bn2.GetSize(), 1000); |
1791 | | |
1792 | 382 | opRunner = std::make_unique<Botan_bignum::ExpMod>(); |
1793 | 382 | break; |
1794 | 46 | case CF_CALCOP("Exp(A,B)"): |
1795 | 46 | opRunner = std::make_unique<Botan_bignum::Exp>(); |
1796 | 46 | break; |
1797 | 105 | case CF_CALCOP("Sqr(A)"): |
1798 | 105 | opRunner = std::make_unique<Botan_bignum::Sqr>(); |
1799 | 105 | break; |
1800 | 74 | case CF_CALCOP("GCD(A,B)"): |
1801 | 74 | opRunner = std::make_unique<Botan_bignum::GCD>(); |
1802 | 74 | break; |
1803 | 75 | case CF_CALCOP("SqrMod(A,B)"): |
1804 | 75 | opRunner = std::make_unique<Botan_bignum::SqrMod>(); |
1805 | 75 | break; |
1806 | 223 | case CF_CALCOP("InvMod(A,B)"): |
1807 | 223 | opRunner = std::make_unique<Botan_bignum::InvMod>(); |
1808 | 223 | break; |
1809 | 13 | case CF_CALCOP("Cmp(A,B)"): |
1810 | 13 | opRunner = std::make_unique<Botan_bignum::Cmp>(); |
1811 | 13 | break; |
1812 | 104 | case CF_CALCOP("LCM(A,B)"): |
1813 | 104 | opRunner = std::make_unique<Botan_bignum::LCM>(); |
1814 | 104 | break; |
1815 | 16 | case CF_CALCOP("Abs(A)"): |
1816 | 16 | opRunner = std::make_unique<Botan_bignum::Abs>(); |
1817 | 16 | break; |
1818 | 77 | case CF_CALCOP("Jacobi(A,B)"): |
1819 | 77 | opRunner = std::make_unique<Botan_bignum::Jacobi>(); |
1820 | 77 | break; |
1821 | 25 | case CF_CALCOP("Neg(A)"): |
1822 | 25 | opRunner = std::make_unique<Botan_bignum::Neg>(); |
1823 | 25 | break; |
1824 | 150 | case CF_CALCOP("IsPrime(A)"): |
1825 | 150 | opRunner = std::make_unique<Botan_bignum::IsPrime>(); |
1826 | 150 | break; |
1827 | 143 | case CF_CALCOP("RShift(A,B)"): |
1828 | 143 | opRunner = std::make_unique<Botan_bignum::RShift>(); |
1829 | 143 | break; |
1830 | 50 | case CF_CALCOP("LShift1(A)"): |
1831 | 50 | opRunner = std::make_unique<Botan_bignum::LShift1>(); |
1832 | 50 | break; |
1833 | 10 | case CF_CALCOP("IsNeg(A)"): |
1834 | 10 | opRunner = std::make_unique<Botan_bignum::IsNeg>(); |
1835 | 10 | break; |
1836 | 50 | case CF_CALCOP("IsEq(A,B)"): |
1837 | 50 | opRunner = std::make_unique<Botan_bignum::IsEq>(); |
1838 | 50 | break; |
1839 | 10 | case CF_CALCOP("IsGt(A,B)"): |
1840 | 10 | opRunner = std::make_unique<Botan_bignum::IsGt>(); |
1841 | 10 | break; |
1842 | 10 | case CF_CALCOP("IsGte(A,B)"): |
1843 | 10 | opRunner = std::make_unique<Botan_bignum::IsGte>(); |
1844 | 10 | break; |
1845 | 10 | case CF_CALCOP("IsLt(A,B)"): |
1846 | 10 | opRunner = std::make_unique<Botan_bignum::IsLt>(); |
1847 | 10 | break; |
1848 | 10 | case CF_CALCOP("IsLte(A,B)"): |
1849 | 10 | opRunner = std::make_unique<Botan_bignum::IsLte>(); |
1850 | 10 | break; |
1851 | 28 | case CF_CALCOP("IsEven(A)"): |
1852 | 28 | opRunner = std::make_unique<Botan_bignum::IsEven>(); |
1853 | 28 | break; |
1854 | 26 | case CF_CALCOP("IsOdd(A)"): |
1855 | 26 | opRunner = std::make_unique<Botan_bignum::IsOdd>(); |
1856 | 26 | break; |
1857 | 43 | case CF_CALCOP("IsZero(A)"): |
1858 | 43 | opRunner = std::make_unique<Botan_bignum::IsZero>(); |
1859 | 43 | break; |
1860 | 10 | case CF_CALCOP("IsNotZero(A)"): |
1861 | 10 | opRunner = std::make_unique<Botan_bignum::IsNotZero>(); |
1862 | 10 | break; |
1863 | 60 | case CF_CALCOP("IsOne(A)"): |
1864 | 60 | opRunner = std::make_unique<Botan_bignum::IsOne>(); |
1865 | 60 | break; |
1866 | 86 | case CF_CALCOP("MulMod(A,B,C)"): |
1867 | 86 | opRunner = std::make_unique<Botan_bignum::MulMod>(); |
1868 | 86 | break; |
1869 | 44 | case CF_CALCOP("Bit(A,B)"): |
1870 | 44 | opRunner = std::make_unique<Botan_bignum::Bit>(); |
1871 | 44 | break; |
1872 | 61 | case CF_CALCOP("CmpAbs(A,B)"): |
1873 | 61 | opRunner = std::make_unique<Botan_bignum::CmpAbs>(); |
1874 | 61 | break; |
1875 | 23 | case CF_CALCOP("SetBit(A,B)"): |
1876 | 23 | opRunner = std::make_unique<Botan_bignum::SetBit>(); |
1877 | 23 | break; |
1878 | 72 | case CF_CALCOP("Mod_NIST_192(A)"): |
1879 | 72 | opRunner = std::make_unique<Botan_bignum::Mod_NIST_192>(); |
1880 | 72 | break; |
1881 | 73 | case CF_CALCOP("Mod_NIST_224(A)"): |
1882 | 73 | opRunner = std::make_unique<Botan_bignum::Mod_NIST_224>(); |
1883 | 73 | break; |
1884 | 35 | case CF_CALCOP("Mod_NIST_256(A)"): |
1885 | 35 | opRunner = std::make_unique<Botan_bignum::Mod_NIST_256>(); |
1886 | 35 | break; |
1887 | 46 | case CF_CALCOP("Mod_NIST_384(A)"): |
1888 | 46 | opRunner = std::make_unique<Botan_bignum::Mod_NIST_384>(); |
1889 | 46 | break; |
1890 | 57 | case CF_CALCOP("Mod_NIST_521(A)"): |
1891 | 57 | opRunner = std::make_unique<Botan_bignum::Mod_NIST_521>(); |
1892 | 57 | break; |
1893 | 35 | case CF_CALCOP("ClearBit(A,B)"): |
1894 | 35 | opRunner = std::make_unique<Botan_bignum::ClearBit>(); |
1895 | 35 | break; |
1896 | 38 | case CF_CALCOP("MulAdd(A,B,C)"): |
1897 | 38 | opRunner = std::make_unique<Botan_bignum::MulAdd>(); |
1898 | 38 | break; |
1899 | 24 | case CF_CALCOP("MulDiv(A,B,C)"): |
1900 | 24 | opRunner = std::make_unique<Botan_bignum::MulDiv>(); |
1901 | 24 | break; |
1902 | 81 | case CF_CALCOP("MulDivCeil(A,B,C)"): |
1903 | 81 | opRunner = std::make_unique<Botan_bignum::MulDivCeil>(); |
1904 | 81 | break; |
1905 | 35 | case CF_CALCOP("Exp2(A)"): |
1906 | 35 | opRunner = std::make_unique<Botan_bignum::Exp2>(); |
1907 | 35 | break; |
1908 | 11 | case CF_CALCOP("NumLSZeroBits(A)"): |
1909 | 11 | opRunner = std::make_unique<Botan_bignum::NumLSZeroBits>(); |
1910 | 11 | break; |
1911 | 212 | case CF_CALCOP("Sqrt(A)"): |
1912 | 212 | if ( op.modulo == std::nullopt ) { |
1913 | 82 | opRunner = std::make_unique<Botan_bignum::Sqrt>(); |
1914 | 130 | } else { |
1915 | 130 | opRunner = std::make_unique<Botan_bignum::Ressol>(); |
1916 | 130 | } |
1917 | 212 | break; |
1918 | 85 | case CF_CALCOP("AddMod(A,B,C)"): |
1919 | 85 | opRunner = std::make_unique<Botan_bignum::AddMod>(); |
1920 | 85 | break; |
1921 | 102 | case CF_CALCOP("SubMod(A,B,C)"): |
1922 | 102 | opRunner = std::make_unique<Botan_bignum::SubMod>(); |
1923 | 102 | break; |
1924 | 34 | case CF_CALCOP("NumBits(A)"): |
1925 | 34 | opRunner = std::make_unique<Botan_bignum::NumBits>(); |
1926 | 34 | break; |
1927 | 48 | case CF_CALCOP("Set(A)"): |
1928 | 48 | opRunner = std::make_unique<Botan_bignum::Set>(); |
1929 | 48 | break; |
1930 | 104 | case CF_CALCOP("CondSet(A,B)"): |
1931 | 104 | opRunner = std::make_unique<Botan_bignum::CondSet>(); |
1932 | 104 | break; |
1933 | | /* |
1934 | | case CF_CALCOP("Ressol(A,B)"): |
1935 | | opRunner = std::make_unique<Botan_bignum::Ressol>(); |
1936 | | break; |
1937 | | */ |
1938 | 64 | case CF_CALCOP("Not(A)"): |
1939 | 64 | opRunner = std::make_unique<Botan_bignum::Not>(); |
1940 | 64 | break; |
1941 | 550 | case CF_CALCOP("Prime()"): |
1942 | 550 | opRunner = std::make_unique<Botan_bignum::Prime>(); |
1943 | 550 | break; |
1944 | 31 | case CF_CALCOP("RandRange(A,B)"): |
1945 | 31 | opRunner = std::make_unique<Botan_bignum::RandRange>(); |
1946 | 31 | break; |
1947 | 34 | case CF_CALCOP("IsSquare(A)"): |
1948 | 34 | opRunner = std::make_unique<Botan_bignum::IsSquare>(); |
1949 | 34 | break; |
1950 | 6.88k | } |
1951 | | |
1952 | 6.87k | CF_CHECK_NE(opRunner, nullptr); |
1953 | | |
1954 | | #if defined(CRYPTOFUZZ_BOTAN_IS_ORACLE) |
1955 | | try { |
1956 | | #endif |
1957 | 4.23k | CF_CHECK_EQ(opRunner->Run( |
1958 | 4.23k | ds, |
1959 | 4.23k | res, |
1960 | 4.23k | bn, |
1961 | 4.23k | op.modulo ? |
1962 | 4.23k | std::optional<Botan_bignum::Bignum>(Botan_bignum::Bignum(op.modulo->ToTrimmedString())) : |
1963 | 4.23k | std::nullopt), true); |
1964 | | #if defined(CRYPTOFUZZ_BOTAN_IS_ORACLE) |
1965 | | } catch ( ... ) { |
1966 | | goto end; |
1967 | | } |
1968 | | #endif |
1969 | | |
1970 | 3.51k | ret = { util::HexToDec(res.Ref().to_hex_string()) }; |
1971 | | |
1972 | 6.88k | end: |
1973 | 6.88k | return ret; |
1974 | 3.51k | } |
1975 | | |
1976 | 1.90k | bool Botan::SupportsModularBignumCalc(void) const { |
1977 | 1.90k | return true; |
1978 | 1.90k | } |
1979 | | |
1980 | | } /* namespace module */ |
1981 | | } /* namespace cryptofuzz */ |