Coverage Report

Created: 2024-06-28 06:39

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