Coverage Report

Created: 2024-06-28 06:39

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