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