Coverage Report

Created: 2024-02-25 06:16

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