Coverage Report

Created: 2024-11-21 06:38

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