Coverage Report

Created: 2024-06-28 06:08

/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.30k
 #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.66k
 #define BOTAN_SET_GLOBAL_DS CF_NORET(util::SetGlobalDs(&ds));
99
6.66k
 #define BOTAN_UNSET_GLOBAL_DS CF_NORET(util::UnsetGlobalDs());
100
#else
101
 #define BOTAN_SET_GLOBAL_DS
102
 #define BOTAN_UNSET_GLOBAL_DS
103
#endif
104
105
namespace Botan_detail {
106
107
#if !defined(CRYPTOFUZZ_BOTAN_IS_ORACLE)
108
    class Fuzzer_RNG final : public ::Botan::RandomNumberGenerator {
109
        private:
110
            Datasource& ds;
111
        public:
112
            Fuzzer_RNG(Datasource& ds) :
113
                ds(ds)
114
5.30k
            { }
115
116
2.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
18.9k
                    std::span<const uint8_t> input) override {
125
18.9k
                (void)input;
126
127
18.9k
                if ( output.empty() ) {
128
0
                    return;
129
0
                }
130
131
18.9k
                const auto data = ds.GetData(0, output.size(), output.size());
132
133
18.9k
                std::copy(data.begin(), data.end(), output.begin());
134
18.9k
            }
135
136
0
            std::string name() const override { return "Fuzzer_RNG"; }
137
    };
138
#endif /* CRYPTOFUZZ_BOTAN_IS_ORACLE */
139
140
2.93k
    const std::string parenthesize(const std::string parent, const std::string child) {
141
2.93k
        static const std::string pOpen("(");
142
2.93k
        static const std::string pClose(")");
143
144
2.93k
        return parent + pOpen + child + pClose;
145
2.93k
    }
146
147
2.54k
    std::optional<std::string> DigestIDToString(const uint64_t digestType, const bool altShake = false, const bool isHmac = false) {
148
2.54k
#include "digest_string_lut.h"
149
2.54k
        std::optional<std::string> ret = std::nullopt;
150
151
2.54k
        CF_CHECK_NE(LUT.find(digestType), LUT.end());
152
153
2.44k
        if ( isHmac == false ) {
154
2.25k
            if (    digestType == CF_DIGEST("SIPHASH64") ||
155
2.25k
                    digestType == CF_DIGEST("SIPHASH128") ) {
156
0
                return std::nullopt;
157
0
            }
158
2.25k
        }
159
2.44k
        if ( altShake == true && digestType == CF_DIGEST("SHAKE128") ) {
160
29
            ret = "SHAKE-128(256)";
161
2.41k
        } else if ( altShake == true && digestType == CF_DIGEST("SHAKE256") ) {
162
51
            ret = "SHAKE-256(512)";
163
2.36k
        } else if ( altShake == true && digestType == CF_DIGEST("SHAKE256_114") ) {
164
0
            ret = "SHAKE-256(912)"; /* 114 bytes * 8 = 912 bits */
165
2.36k
        } else {
166
2.36k
            ret = LUT.at(digestType);
167
2.36k
        }
168
2.54k
end:
169
2.54k
        return ret;
170
2.44k
    }
171
172
} /* namespace Botan_detail */
173
174
544
std::optional<component::Digest> Botan::OpDigest(operation::Digest& op) {
175
544
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
176
544
    std::optional<component::Digest> ret = std::nullopt;
177
544
    std::unique_ptr<::Botan::HashFunction> hash = nullptr;
178
544
    util::Multipart parts;
179
544
    size_t numClears = 0;
180
181
    /* Initialize */
182
544
    {
183
544
        BOTAN_SET_GLOBAL_DS
184
185
544
        std::optional<std::string> algoString;
186
544
        CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.digestType.Get()), std::nullopt);
187
515
        CF_CHECK_NE(hash = ::Botan::HashFunction::create(*algoString), nullptr);
188
189
515
        parts = util::ToParts(ds, op.cleartext);
190
515
    }
191
192
581
again:
193
    /* Process */
194
39.8k
    for (const auto& part : parts) {
195
39.8k
        hash->update(part.first, part.second);
196
39.8k
        bool clear = false;
197
198
39.8k
        if ( numClears < 3 ) {
199
39.8k
            try {
200
39.8k
#if !defined(CRYPTOFUZZ_BOTAN_IS_ORACLE)
201
39.8k
                clear = ds.Get<bool>();
202
39.8k
#endif /* CRYPTOFUZZ_BOTAN_IS_ORACLE */
203
39.8k
            } catch ( ... ) { }
204
39.8k
        }
205
206
39.8k
        if ( clear == true ) {
207
66
            hash->clear();
208
66
            numClears++;
209
66
            goto again;
210
66
        }
211
39.8k
    }
212
213
    /* Finalize */
214
515
    {
215
515
        const auto res = hash->final();
216
515
        ret = component::Digest(res.data(), res.size());
217
515
    }
218
219
544
end:
220
544
    BOTAN_UNSET_GLOBAL_DS
221
222
544
    return ret;
223
515
}
224
225
195
std::optional<component::MAC> Botan::OpHMAC(operation::HMAC& op) {
226
195
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
227
195
    std::optional<component::MAC> ret = std::nullopt;
228
195
    std::unique_ptr<::Botan::MessageAuthenticationCode> hmac = nullptr;
229
195
    util::Multipart parts;
230
231
195
    try {
232
        /* Initialize */
233
195
        {
234
195
            BOTAN_SET_GLOBAL_DS
235
236
195
            std::optional<std::string> algoString;
237
195
            CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.digestType.Get(), true, true), std::nullopt);
238
239
186
            std::string hmacString;
240
186
            if (
241
186
                    op.digestType.Is(CF_DIGEST("SIPHASH64")) ||
242
186
                    op.digestType.Is(CF_DIGEST("BLAKE2B_MAC")) ) {
243
0
                hmacString = *algoString;
244
186
            } else {
245
186
                hmacString = Botan_detail::parenthesize("HMAC", *algoString);
246
186
            }
247
248
186
            CF_CHECK_NE(hmac = ::Botan::MessageAuthenticationCode::create(hmacString), nullptr);
249
250
186
            try {
251
186
                hmac->set_key(op.cipher.key.GetPtr(), op.cipher.key.GetSize());
252
186
            } catch ( ... ) {
253
8
                goto end;
254
8
            }
255
256
178
            parts = util::ToParts(ds, op.cleartext);
257
178
        }
258
259
        /* Process */
260
1.28k
        for (const auto& part : parts) {
261
1.28k
            hmac->update(part.first, part.second);
262
1.28k
        }
263
264
        /* Finalize */
265
178
        {
266
178
            const auto res = hmac->final();
267
178
            ret = component::MAC(res.data(), res.size());
268
178
        }
269
270
178
    } catch ( ... ) { }
271
272
195
end:
273
195
    BOTAN_UNSET_GLOBAL_DS
274
275
195
    return ret;
276
195
}
277
278
namespace Botan_detail {
279
280
1.96k
    std::optional<std::string> CipherIDToString(const uint64_t digestType, const bool withMode = true) {
281
1.96k
#include "cipher_string_lut.h"
282
1.96k
        std::optional<std::string> ret = std::nullopt;
283
284
1.96k
        CF_CHECK_NE(LUT.find(digestType), LUT.end());
285
1.96k
        ret = withMode ? LUT.at(digestType).first : LUT.at(digestType).second;
286
1.96k
end:
287
1.96k
        return ret;
288
1.96k
    }
289
290
    template <class OperationType>
291
    const uint8_t* GetInPtr(const OperationType& op);
292
293
    template <>
294
1.09k
    const uint8_t* GetInPtr(const operation::SymmetricEncrypt& op) {
295
1.09k
        return op.cleartext.GetPtr();
296
1.09k
    }
297
298
    template <>
299
1.02k
    const uint8_t* GetInPtr(const operation::SymmetricDecrypt& op) {
300
1.02k
        return op.ciphertext.GetPtr();
301
1.02k
    }
302
303
    template <class OperationType>
304
    size_t GetInSize(const OperationType& op);
305
306
    template <>
307
1.09k
    size_t GetInSize(const operation::SymmetricEncrypt& op) {
308
1.09k
        return op.cleartext.GetSize();
309
1.09k
    }
310
311
    template <>
312
1.41k
    size_t GetInSize(const operation::SymmetricDecrypt& op) {
313
1.41k
        return op.ciphertext.GetSize();
314
1.41k
    }
315
316
    template <class OperationType>
317
    ::Botan::Cipher_Dir GetCryptType(void);
318
319
    template <>
320
1.99k
    ::Botan::Cipher_Dir GetCryptType<operation::SymmetricEncrypt>(void) {
321
1.99k
        return ::Botan::Cipher_Dir::Encryption;
322
1.99k
    }
323
324
    template <>
325
1.93k
    ::Botan::Cipher_Dir GetCryptType<operation::SymmetricDecrypt>(void) {
326
1.93k
        return ::Botan::Cipher_Dir::Decryption;
327
1.93k
    }
328
329
    template <class OperationType>
330
    std::optional<size_t> GetTagSize(const OperationType& op);
331
332
    template <>
333
1.60k
    std::optional<size_t> GetTagSize<>(const operation::SymmetricEncrypt& op) {
334
1.60k
        if ( op.tagSize == std::nullopt ) {
335
1.01k
            return std::nullopt;
336
1.01k
        }
337
338
582
        return *op.tagSize;
339
1.60k
    }
340
341
    template <>
342
2.60k
    std::optional<size_t> GetTagSize<>(const operation::SymmetricDecrypt& op) {
343
2.60k
        if ( op.tag == std::nullopt ) {
344
1.49k
            return std::nullopt;
345
1.49k
        }
346
347
1.11k
        return op.tag->GetSize();
348
2.60k
    }
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
384
    const uint8_t* GetTagPtr<>(const operation::SymmetricDecrypt& op) {
362
384
        if ( op.tag == std::nullopt ) {
363
0
            return nullptr;
364
0
        }
365
366
384
        return op.tag->GetPtr();
367
384
    }
368
369
    template <class CryptClass>
370
    void SetAAD(std::shared_ptr<CryptClass> crypt, const std::optional<component::AAD>& aad);
371
372
    template <>
373
376
    void SetAAD<>(std::shared_ptr<::Botan::AEAD_Mode> crypt, const std::optional<component::AAD>& aad) {
374
376
        if ( aad != std::nullopt ) {
375
152
            crypt->set_associated_data(aad->Get());
376
152
        }
377
376
    }
378
379
    template <>
380
439
    void SetAAD<>(std::shared_ptr<::Botan::Cipher_Mode> crypt, const std::optional<component::AAD>& aad) {
381
439
        (void)crypt;
382
439
        (void)aad;
383
439
    }
384
385
    template <class OperationType>
386
1.96k
    ::Botan::secure_vector<uint8_t> GetInData(const OperationType& op) {
387
1.96k
        const auto inPtr = GetInPtr(op);
388
1.96k
        ::Botan::secure_vector<uint8_t> ret(inPtr, inPtr + GetInSize(op));
389
390
1.96k
        if ( GetCryptType<OperationType>() == ::Botan::Cipher_Dir::Encryption ) {
391
996
            return ret;
392
996
        }
393
394
965
        const auto tagSize = GetTagSize(op);
395
396
965
        if ( tagSize == std::nullopt || *tagSize == 0 ) {
397
581
            return ret;
398
581
        }
399
400
        /* Append the tag */
401
402
384
        ret.resize(ret.size() + *tagSize);
403
404
384
        memcpy(ret.data() + GetInSize(op), GetTagPtr(op), *tagSize);
405
406
384
        return ret;
407
965
    }
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
996
    ::Botan::secure_vector<uint8_t> GetInData(const OperationType& op) {
387
996
        const auto inPtr = GetInPtr(op);
388
996
        ::Botan::secure_vector<uint8_t> ret(inPtr, inPtr + GetInSize(op));
389
390
996
        if ( GetCryptType<OperationType>() == ::Botan::Cipher_Dir::Encryption ) {
391
996
            return ret;
392
996
        }
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
965
    ::Botan::secure_vector<uint8_t> GetInData(const OperationType& op) {
387
965
        const auto inPtr = GetInPtr(op);
388
965
        ::Botan::secure_vector<uint8_t> ret(inPtr, inPtr + GetInSize(op));
389
390
965
        if ( GetCryptType<OperationType>() == ::Botan::Cipher_Dir::Encryption ) {
391
0
            return ret;
392
0
        }
393
394
965
        const auto tagSize = GetTagSize(op);
395
396
965
        if ( tagSize == std::nullopt || *tagSize == 0 ) {
397
581
            return ret;
398
581
        }
399
400
        /* Append the tag */
401
402
384
        ret.resize(ret.size() + *tagSize);
403
404
384
        memcpy(ret.data() + GetInSize(op), GetTagPtr(op), *tagSize);
405
406
384
        return ret;
407
965
    }
408
409
    template <class ReturnType>
410
    ReturnType ToReturnType(const ::Botan::secure_vector<uint8_t>& data, std::optional<size_t> tagSize);
411
412
    template <>
413
338
    component::Ciphertext ToReturnType(const ::Botan::secure_vector<uint8_t>& data, std::optional<size_t> tagSize) {
414
338
        if ( tagSize == std::nullopt ) {
415
171
            return component::Ciphertext(Buffer(data.data(), data.size()));
416
171
        }
417
418
167
        const size_t ciphertextSize = data.size() - *tagSize;
419
420
167
        return component::Ciphertext(Buffer(data.data(), ciphertextSize), Buffer(data.data() + ciphertextSize, *tagSize));
421
338
    }
422
423
    template <>
424
395
    component::Cleartext ToReturnType(const ::Botan::secure_vector<uint8_t>& data, std::optional<size_t> tagSize) {
425
395
        (void)tagSize;
426
427
395
        return component::Cleartext(Buffer(data.data(), data.size()));
428
395
    }
429
430
    template <class ReturnType, class OperationType, class CryptClass>
431
2.94k
        std::optional<ReturnType> Crypt(OperationType& op, Datasource& ds) {
432
2.94k
            std::optional<ReturnType> ret = std::nullopt;
433
434
2.94k
            if ( typeid(CryptClass) == typeid(::Botan::Cipher_Mode) ) {
435
1.67k
                if ( op.aad != std::nullopt ) {
436
397
                    return std::nullopt;
437
397
                }
438
1.28k
                if ( GetTagSize(op) != std::nullopt ) {
439
591
                    return std::nullopt;
440
591
                }
441
1.28k
            }
442
443
1.96k
            std::shared_ptr<CryptClass> crypt = nullptr;
444
1.96k
            const ::Botan::SymmetricKey key(op.cipher.key.GetPtr(), op.cipher.key.GetSize());
445
1.96k
            const ::Botan::InitializationVector iv(op.cipher.iv.GetPtr(), op.cipher.iv.GetSize());
446
1.96k
            ::Botan::secure_vector<uint8_t> in = GetInData(op);
447
1.96k
            ::Botan::secure_vector<uint8_t> out;
448
1.96k
            bool useOneShot = true;
449
1.96k
            util::Multipart parts;
450
451
1.96k
            const std::optional<size_t> tagSize = GetTagSize(op);
452
453
1.96k
            try {
454
                /* Initialize */
455
1.96k
                {
456
1.96k
                    std::optional<std::string> _algoString;
457
1.96k
                    CF_CHECK_NE(_algoString = Botan_detail::CipherIDToString(op.cipher.cipherType.Get()), std::nullopt);
458
1.96k
                    std::string algoString;
459
1.96k
                    if ( tagSize == std::nullopt ) {
460
1.25k
                        algoString = Botan_detail::parenthesize(*_algoString, std::to_string(0));
461
1.25k
                    } else {
462
706
                        algoString = Botan_detail::parenthesize(*_algoString, std::to_string(*tagSize));
463
706
                    }
464
465
1.96k
                    CF_CHECK_NE(crypt = CryptClass::create(algoString, GetCryptType<OperationType>()), nullptr);
466
1.57k
                    crypt->set_key(key);
467
468
1.57k
                    SetAAD(crypt, op.aad);
469
470
1.57k
                    crypt->start(iv.bits_of());
471
1.57k
                    if ( crypt->update_granularity() == 1 ) {
472
420
                        try {
473
420
                            useOneShot = ds.Get<bool>();
474
420
                        } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
475
420
                    }
476
1.57k
                    if ( useOneShot == false ) {
477
157
                        parts = util::ToParts(ds, GetInPtr(op), GetInSize(op));
478
157
                    }
479
1.57k
                }
480
481
                /* Process */
482
0
                {
483
1.57k
                    if ( useOneShot == true ) {
484
635
                        crypt->finish(in);
485
935
                    } else {
486
3.68k
                        for (const auto& part : parts) {
487
3.68k
                            std::vector<uint8_t> tmp(part.first, part.first + part.second);
488
3.68k
                            const auto num = crypt->process(tmp.data(), tmp.size());
489
3.68k
                            out.insert(out.end(), tmp.begin(), tmp.begin() + num);
490
3.68k
                        }
491
935
                        crypt->finish(out, out.size());
492
935
                    }
493
1.57k
                }
494
495
                /* Finalize */
496
1.57k
                {
497
                    /* TODO take max output size in consideration */
498
499
1.57k
                    if ( useOneShot == true ) {
500
576
                        ret = ToReturnType<ReturnType>(in, tagSize);
501
994
                    } else {
502
994
                        ret = ToReturnType<ReturnType>(::Botan::secure_vector<uint8_t>(out.data(), out.data() + out.size()), tagSize);
503
994
                    }
504
1.57k
                }
505
1.57k
            } catch ( ... ) { }
506
1.96k
end:
507
508
1.96k
            return ret;
509
1.96k
        }
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
669
        std::optional<ReturnType> Crypt(OperationType& op, Datasource& ds) {
432
669
            std::optional<ReturnType> ret = std::nullopt;
433
434
669
            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
669
            std::shared_ptr<CryptClass> crypt = nullptr;
444
669
            const ::Botan::SymmetricKey key(op.cipher.key.GetPtr(), op.cipher.key.GetSize());
445
669
            const ::Botan::InitializationVector iv(op.cipher.iv.GetPtr(), op.cipher.iv.GetSize());
446
669
            ::Botan::secure_vector<uint8_t> in = GetInData(op);
447
669
            ::Botan::secure_vector<uint8_t> out;
448
669
            bool useOneShot = true;
449
669
            util::Multipart parts;
450
451
669
            const std::optional<size_t> tagSize = GetTagSize(op);
452
453
669
            try {
454
                /* Initialize */
455
669
                {
456
669
                    std::optional<std::string> _algoString;
457
669
                    CF_CHECK_NE(_algoString = Botan_detail::CipherIDToString(op.cipher.cipherType.Get()), std::nullopt);
458
669
                    std::string algoString;
459
669
                    if ( tagSize == std::nullopt ) {
460
364
                        algoString = Botan_detail::parenthesize(*_algoString, std::to_string(0));
461
364
                    } else {
462
305
                        algoString = Botan_detail::parenthesize(*_algoString, std::to_string(*tagSize));
463
305
                    }
464
465
669
                    CF_CHECK_NE(crypt = CryptClass::create(algoString, GetCryptType<OperationType>()), nullptr);
466
492
                    crypt->set_key(key);
467
468
492
                    SetAAD(crypt, op.aad);
469
470
492
                    crypt->start(iv.bits_of());
471
492
                    if ( crypt->update_granularity() == 1 ) {
472
0
                        try {
473
0
                            useOneShot = ds.Get<bool>();
474
0
                        } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
475
0
                    }
476
492
                    if ( useOneShot == false ) {
477
0
                        parts = util::ToParts(ds, GetInPtr(op), GetInSize(op));
478
0
                    }
479
492
                }
480
481
                /* Process */
482
0
                {
483
492
                    if ( useOneShot == true ) {
484
167
                        crypt->finish(in);
485
325
                    } else {
486
325
                        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
325
                        crypt->finish(out, out.size());
492
325
                    }
493
492
                }
494
495
                /* Finalize */
496
492
                {
497
                    /* TODO take max output size in consideration */
498
499
492
                    if ( useOneShot == true ) {
500
167
                        ret = ToReturnType<ReturnType>(in, tagSize);
501
325
                    } else {
502
325
                        ret = ToReturnType<ReturnType>(::Botan::secure_vector<uint8_t>(out.data(), out.data() + out.size()), tagSize);
503
325
                    }
504
492
                }
505
492
            } catch ( ... ) { }
506
669
end:
507
508
669
            return ret;
509
669
        }
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
827
        std::optional<ReturnType> Crypt(OperationType& op, Datasource& ds) {
432
827
            std::optional<ReturnType> ret = std::nullopt;
433
434
827
            if ( typeid(CryptClass) == typeid(::Botan::Cipher_Mode) ) {
435
827
                if ( op.aad != std::nullopt ) {
436
223
                    return std::nullopt;
437
223
                }
438
604
                if ( GetTagSize(op) != std::nullopt ) {
439
277
                    return std::nullopt;
440
277
                }
441
604
            }
442
443
327
            std::shared_ptr<CryptClass> crypt = nullptr;
444
327
            const ::Botan::SymmetricKey key(op.cipher.key.GetPtr(), op.cipher.key.GetSize());
445
327
            const ::Botan::InitializationVector iv(op.cipher.iv.GetPtr(), op.cipher.iv.GetSize());
446
327
            ::Botan::secure_vector<uint8_t> in = GetInData(op);
447
327
            ::Botan::secure_vector<uint8_t> out;
448
327
            bool useOneShot = true;
449
327
            util::Multipart parts;
450
451
327
            const std::optional<size_t> tagSize = GetTagSize(op);
452
453
327
            try {
454
                /* Initialize */
455
327
                {
456
327
                    std::optional<std::string> _algoString;
457
327
                    CF_CHECK_NE(_algoString = Botan_detail::CipherIDToString(op.cipher.cipherType.Get()), std::nullopt);
458
327
                    std::string algoString;
459
327
                    if ( tagSize == std::nullopt ) {
460
327
                        algoString = Botan_detail::parenthesize(*_algoString, std::to_string(0));
461
327
                    } else {
462
0
                        algoString = Botan_detail::parenthesize(*_algoString, std::to_string(*tagSize));
463
0
                    }
464
465
327
                    CF_CHECK_NE(crypt = CryptClass::create(algoString, GetCryptType<OperationType>()), nullptr);
466
327
                    crypt->set_key(key);
467
468
327
                    SetAAD(crypt, op.aad);
469
470
327
                    crypt->start(iv.bits_of());
471
327
                    if ( crypt->update_granularity() == 1 ) {
472
171
                        try {
473
171
                            useOneShot = ds.Get<bool>();
474
171
                        } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
475
171
                    }
476
327
                    if ( useOneShot == false ) {
477
96
                        parts = util::ToParts(ds, GetInPtr(op), GetInSize(op));
478
96
                    }
479
327
                }
480
481
                /* Process */
482
0
                {
483
327
                    if ( useOneShot == true ) {
484
75
                        crypt->finish(in);
485
252
                    } else {
486
2.39k
                        for (const auto& part : parts) {
487
2.39k
                            std::vector<uint8_t> tmp(part.first, part.first + part.second);
488
2.39k
                            const auto num = crypt->process(tmp.data(), tmp.size());
489
2.39k
                            out.insert(out.end(), tmp.begin(), tmp.begin() + num);
490
2.39k
                        }
491
252
                        crypt->finish(out, out.size());
492
252
                    }
493
327
                }
494
495
                /* Finalize */
496
327
                {
497
                    /* TODO take max output size in consideration */
498
499
327
                    if ( useOneShot == true ) {
500
75
                        ret = ToReturnType<ReturnType>(in, tagSize);
501
252
                    } else {
502
252
                        ret = ToReturnType<ReturnType>(::Botan::secure_vector<uint8_t>(out.data(), out.data() + out.size()), tagSize);
503
252
                    }
504
327
                }
505
327
            } catch ( ... ) { }
506
327
end:
507
508
327
            return ret;
509
327
        }
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
603
        std::optional<ReturnType> Crypt(OperationType& op, Datasource& ds) {
432
603
            std::optional<ReturnType> ret = std::nullopt;
433
434
603
            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
603
            std::shared_ptr<CryptClass> crypt = nullptr;
444
603
            const ::Botan::SymmetricKey key(op.cipher.key.GetPtr(), op.cipher.key.GetSize());
445
603
            const ::Botan::InitializationVector iv(op.cipher.iv.GetPtr(), op.cipher.iv.GetSize());
446
603
            ::Botan::secure_vector<uint8_t> in = GetInData(op);
447
603
            ::Botan::secure_vector<uint8_t> out;
448
603
            bool useOneShot = true;
449
603
            util::Multipart parts;
450
451
603
            const std::optional<size_t> tagSize = GetTagSize(op);
452
453
603
            try {
454
                /* Initialize */
455
603
                {
456
603
                    std::optional<std::string> _algoString;
457
603
                    CF_CHECK_NE(_algoString = Botan_detail::CipherIDToString(op.cipher.cipherType.Get()), std::nullopt);
458
603
                    std::string algoString;
459
603
                    if ( tagSize == std::nullopt ) {
460
202
                        algoString = Botan_detail::parenthesize(*_algoString, std::to_string(0));
461
401
                    } else {
462
401
                        algoString = Botan_detail::parenthesize(*_algoString, std::to_string(*tagSize));
463
401
                    }
464
465
603
                    CF_CHECK_NE(crypt = CryptClass::create(algoString, GetCryptType<OperationType>()), nullptr);
466
389
                    crypt->set_key(key);
467
468
389
                    SetAAD(crypt, op.aad);
469
470
389
                    crypt->start(iv.bits_of());
471
389
                    if ( crypt->update_granularity() == 1 ) {
472
0
                        try {
473
0
                            useOneShot = ds.Get<bool>();
474
0
                        } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
475
0
                    }
476
389
                    if ( useOneShot == false ) {
477
0
                        parts = util::ToParts(ds, GetInPtr(op), GetInSize(op));
478
0
                    }
479
389
                }
480
481
                /* Process */
482
0
                {
483
389
                    if ( useOneShot == true ) {
484
205
                        crypt->finish(in);
485
205
                    } else {
486
184
                        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
184
                        crypt->finish(out, out.size());
492
184
                    }
493
389
                }
494
495
                /* Finalize */
496
389
                {
497
                    /* TODO take max output size in consideration */
498
499
389
                    if ( useOneShot == true ) {
500
146
                        ret = ToReturnType<ReturnType>(in, tagSize);
501
243
                    } else {
502
243
                        ret = ToReturnType<ReturnType>(::Botan::secure_vector<uint8_t>(out.data(), out.data() + out.size()), tagSize);
503
243
                    }
504
389
                }
505
389
            } catch ( ... ) { }
506
603
end:
507
508
603
            return ret;
509
603
        }
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
850
        std::optional<ReturnType> Crypt(OperationType& op, Datasource& ds) {
432
850
            std::optional<ReturnType> ret = std::nullopt;
433
434
850
            if ( typeid(CryptClass) == typeid(::Botan::Cipher_Mode) ) {
435
850
                if ( op.aad != std::nullopt ) {
436
174
                    return std::nullopt;
437
174
                }
438
676
                if ( GetTagSize(op) != std::nullopt ) {
439
314
                    return std::nullopt;
440
314
                }
441
676
            }
442
443
362
            std::shared_ptr<CryptClass> crypt = nullptr;
444
362
            const ::Botan::SymmetricKey key(op.cipher.key.GetPtr(), op.cipher.key.GetSize());
445
362
            const ::Botan::InitializationVector iv(op.cipher.iv.GetPtr(), op.cipher.iv.GetSize());
446
362
            ::Botan::secure_vector<uint8_t> in = GetInData(op);
447
362
            ::Botan::secure_vector<uint8_t> out;
448
362
            bool useOneShot = true;
449
362
            util::Multipart parts;
450
451
362
            const std::optional<size_t> tagSize = GetTagSize(op);
452
453
362
            try {
454
                /* Initialize */
455
362
                {
456
362
                    std::optional<std::string> _algoString;
457
362
                    CF_CHECK_NE(_algoString = Botan_detail::CipherIDToString(op.cipher.cipherType.Get()), std::nullopt);
458
362
                    std::string algoString;
459
362
                    if ( tagSize == std::nullopt ) {
460
362
                        algoString = Botan_detail::parenthesize(*_algoString, std::to_string(0));
461
362
                    } else {
462
0
                        algoString = Botan_detail::parenthesize(*_algoString, std::to_string(*tagSize));
463
0
                    }
464
465
362
                    CF_CHECK_NE(crypt = CryptClass::create(algoString, GetCryptType<OperationType>()), nullptr);
466
362
                    crypt->set_key(key);
467
468
362
                    SetAAD(crypt, op.aad);
469
470
362
                    crypt->start(iv.bits_of());
471
362
                    if ( crypt->update_granularity() == 1 ) {
472
249
                        try {
473
249
                            useOneShot = ds.Get<bool>();
474
249
                        } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
475
249
                    }
476
362
                    if ( useOneShot == false ) {
477
61
                        parts = util::ToParts(ds, GetInPtr(op), GetInSize(op));
478
61
                    }
479
362
                }
480
481
                /* Process */
482
0
                {
483
362
                    if ( useOneShot == true ) {
484
188
                        crypt->finish(in);
485
188
                    } else {
486
1.28k
                        for (const auto& part : parts) {
487
1.28k
                            std::vector<uint8_t> tmp(part.first, part.first + part.second);
488
1.28k
                            const auto num = crypt->process(tmp.data(), tmp.size());
489
1.28k
                            out.insert(out.end(), tmp.begin(), tmp.begin() + num);
490
1.28k
                        }
491
174
                        crypt->finish(out, out.size());
492
174
                    }
493
362
                }
494
495
                /* Finalize */
496
362
                {
497
                    /* TODO take max output size in consideration */
498
499
362
                    if ( useOneShot == true ) {
500
188
                        ret = ToReturnType<ReturnType>(in, tagSize);
501
188
                    } else {
502
174
                        ret = ToReturnType<ReturnType>(::Botan::secure_vector<uint8_t>(out.data(), out.data() + out.size()), tagSize);
503
174
                    }
504
362
                }
505
362
            } catch ( ... ) { }
506
362
end:
507
508
362
            return ret;
509
362
        }
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.49k
std::optional<component::Ciphertext> Botan::OpSymmetricEncrypt(operation::SymmetricEncrypt& op) {
563
1.49k
    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
3
        return std::nullopt;
566
3
    }
567
568
1.49k
    std::optional<component::Ciphertext> ret = std::nullopt;
569
570
1.49k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
571
1.49k
    BOTAN_SET_GLOBAL_DS
572
573
1.49k
    if ( cryptofuzz::repository::IsAEAD(op.cipher.cipherType.Get()) ) {
574
669
        ret = Botan_detail::Crypt<component::Ciphertext, operation::SymmetricEncrypt, ::Botan::AEAD_Mode>(op, ds);
575
827
    } else {
576
827
        ret = Botan_detail::Crypt<component::Ciphertext, operation::SymmetricEncrypt, ::Botan::Cipher_Mode>(op, ds);
577
827
    }
578
579
1.49k
    BOTAN_UNSET_GLOBAL_DS
580
581
1.49k
    return ret;
582
1.49k
}
583
584
1.45k
std::optional<component::Cleartext> Botan::OpSymmetricDecrypt(operation::SymmetricDecrypt& op) {
585
1.45k
    if ( op.cipher.cipherType.Is(CF_CIPHER("CHACHA20_POLY1305")) && op.cipher.iv.GetSize() == 24 ) {
586
1
        return std::nullopt;
587
1
    }
588
589
1.45k
    std::optional<component::Cleartext> ret = std::nullopt;
590
591
1.45k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
592
1.45k
    BOTAN_SET_GLOBAL_DS
593
594
1.45k
    if ( cryptofuzz::repository::IsAEAD(op.cipher.cipherType.Get()) ) {
595
603
        ret = Botan_detail::Crypt<component::Cleartext, operation::SymmetricDecrypt, ::Botan::AEAD_Mode>(op, ds);
596
850
    } else {
597
850
        ret = Botan_detail::Crypt<component::Cleartext, operation::SymmetricDecrypt, ::Botan::Cipher_Mode>(op, ds);
598
850
    }
599
600
1.45k
    BOTAN_UNSET_GLOBAL_DS
601
602
1.45k
    return ret;
603
1.45k
}
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
611
std::optional<component::Key> Botan::OpKDF_HKDF(operation::KDF_HKDF& op) {
648
611
    std::optional<component::Key> ret = std::nullopt;
649
611
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
650
611
    std::unique_ptr<::Botan::KDF> hkdf = nullptr;
651
652
611
    try {
653
611
        {
654
611
            BOTAN_SET_GLOBAL_DS
655
656
611
            std::optional<std::string> algoString;
657
611
            CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.digestType.Get(), true), std::nullopt);
658
659
570
            const std::string hkdfString = Botan_detail::parenthesize("HKDF", *algoString);
660
570
            hkdf = ::Botan::KDF::create(hkdfString);
661
570
        }
662
663
0
        {
664
570
            auto derived = hkdf->derive_key(op.keySize, op.password.Get(), op.salt.Get(), op.info.Get());
665
666
570
            ret = component::Key(derived.data(), derived.size());
667
570
        }
668
570
    } catch ( ... ) { }
669
670
611
end:
671
611
    BOTAN_UNSET_GLOBAL_DS
672
673
611
    return ret;
674
611
}
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
114
std::optional<component::Key> Botan::OpKDF_TLS1_PRF(operation::KDF_TLS1_PRF& op) {
829
114
    std::optional<component::Key> ret = std::nullopt;
830
114
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
831
114
    std::unique_ptr<::Botan::KDF> tlsprf = nullptr;
832
833
114
    try {
834
114
        BOTAN_SET_GLOBAL_DS
835
836
114
        {
837
114
            CF_CHECK_EQ(op.digestType.Get(), CF_DIGEST("MD5_SHA1"));
838
93
            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
114
end:
849
114
    BOTAN_UNSET_GLOBAL_DS
850
851
114
    return ret;
852
114
}
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
5.35k
    std::optional<std::string> CurveIDToString(const uint64_t curveID) {
899
5.35k
#include "curve_string_lut.h"
900
5.35k
        std::optional<std::string> ret = std::nullopt;
901
902
5.35k
        CF_CHECK_NE(LUT.find(curveID), LUT.end());
903
2.67k
        ret = LUT.at(curveID);
904
5.35k
end:
905
5.35k
        return ret;
906
2.67k
    }
907
} /* namespace Botan_detail */
908
909
2.86k
std::optional<component::ECC_KeyPair> Botan::OpECC_GenerateKeyPair(operation::ECC_GenerateKeyPair& op) {
910
2.86k
    std::optional<component::ECC_KeyPair> ret = std::nullopt;
911
2.86k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
912
913
2.86k
    std::optional<std::string> curveString;
914
2.86k
    BOTAN_FUZZER_RNG;
915
916
2.86k
    CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt);
917
918
668
    try {
919
668
        ::Botan::EC_Group group(*curveString);
920
668
        auto priv = ::Botan::ECDSA_PrivateKey(rng, group);
921
922
668
        const auto pub_x = priv.public_point().get_affine_x();
923
668
        const auto pub_y = priv.public_point().get_affine_y();
924
925
668
        {
926
668
            const auto pub = std::make_unique<::Botan::ECDSA_PublicKey>(::Botan::ECDSA_PublicKey(group, priv.public_point()));
927
668
            CF_ASSERT(pub->check_key(rng, true) == true, "Generated pubkey fails validation");
928
668
        }
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
668
    } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
934
935
2.86k
end:
936
2.86k
    return ret;
937
668
}
938
939
166
std::optional<bool> Botan::OpECC_ValidatePubkey(operation::ECC_ValidatePubkey& op) {
940
166
    std::optional<bool> ret = std::nullopt;
941
166
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
942
943
166
    BOTAN_FUZZER_RNG;
944
166
    std::unique_ptr<::Botan::Public_Key> pub = nullptr;
945
946
166
    try {
947
166
        std::optional<std::string> curveString;
948
166
        CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt);
949
950
166
        ::Botan::EC_Group group(*curveString);
951
166
        const ::Botan::BigInt pub_x(op.pub.first.ToString(ds));
952
166
        const ::Botan::BigInt pub_y(op.pub.second.ToString(ds));
953
166
        const ::Botan::PointGFp public_point = group.point(pub_x, pub_y);
954
166
        pub = std::make_unique<::Botan::ECDSA_PublicKey>(::Botan::ECDSA_PublicKey(group, public_point));
955
956
166
        ret = pub->check_key(rng, true);
957
166
    } catch ( ... ) { }
958
959
166
end:
960
166
    return ret;
961
166
}
962
963
1.23k
std::optional<component::ECC_PublicKey> Botan::OpECC_PrivateToPublic(operation::ECC_PrivateToPublic& op) {
964
1.23k
    std::optional<component::ECC_PublicKey> ret = std::nullopt;
965
1.23k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
966
967
1.23k
    BOTAN_FUZZER_RNG;
968
969
1.23k
    try {
970
1.23k
        std::optional<std::string> curveString;
971
972
1.23k
        if ( op.curveType.Get() == CF_ECC_CURVE("x25519") ) {
973
491
            uint8_t priv_bytes[32];
974
975
491
            const ::Botan::BigInt priv_bigint(op.priv.ToString(ds));
976
491
            CF_CHECK_GT(priv_bigint, 0);
977
978
478
            priv_bigint.binary_encode(priv_bytes, sizeof(priv_bytes));
979
478
            priv_bytes[0] &= 248;
980
478
            priv_bytes[31] &= 127;
981
478
            priv_bytes[31] |= 64;
982
478
            const ::Botan::secure_vector<uint8_t> priv_vec(priv_bytes, priv_bytes + sizeof(priv_bytes));
983
984
478
            auto priv = ::Botan::X25519_PrivateKey(priv_vec);
985
986
478
            ::Botan::BigInt pub;
987
478
            pub.binary_decode(priv.public_value());
988
989
478
            ret = { pub.to_dec_string(), "0" };
990
743
        } else {
991
743
            CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt);
992
558
            ::Botan::EC_Group group(*curveString);
993
994
558
            const ::Botan::BigInt priv_bn(op.priv.ToString(ds));
995
558
            CF_CHECK_GT(priv_bn, 0);
996
997
546
            auto priv = std::make_unique<::Botan::ECDSA_PrivateKey>(::Botan::ECDSA_PrivateKey(rng, group, priv_bn));
998
999
546
            const auto pub_x = priv->public_point().get_affine_x();
1000
546
            const auto pub_y = priv->public_point().get_affine_y();
1001
1002
546
            ret = { pub_x.to_dec_string(), pub_y.to_dec_string() };
1003
546
        }
1004
1.23k
    } catch ( ... ) { }
1005
1006
1.23k
end:
1007
1.23k
    return ret;
1008
1.23k
}
1009
1010
namespace Botan_detail {
1011
    template <class PrivkeyType, class Operation, bool RFC6979 = true>
1012
967
        std::optional<component::ECDSA_Signature> ECxDSA_Sign(Operation& op) {
1013
967
            std::optional<component::ECDSA_Signature> ret = std::nullopt;
1014
967
            Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1015
1016
967
            std::unique_ptr<PrivkeyType> priv = nullptr;
1017
967
            std::unique_ptr<::Botan::Public_Key> pub = nullptr;
1018
967
            std::unique_ptr<::Botan::PK_Signer> signer;
1019
1020
967
            BOTAN_FUZZER_RNG;
1021
1022
967
            BOTAN_SET_GLOBAL_DS
1023
1024
967
            if ( RFC6979 == true ) {
1025
967
                CF_CHECK_EQ(op.UseRFC6979Nonce(), true);
1026
660
            } else {
1027
0
                CF_CHECK_EQ(op.UseRandomNonce(), true);
1028
0
            }
1029
1030
660
            CF_CHECK_EQ(op.digestType.Get(), CF_DIGEST("SHA256"));
1031
1032
292
            try {
1033
                /* Initialize */
1034
292
                {
1035
1036
292
                    std::optional<std::string> curveString, algoString;
1037
1038
292
                    CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt);
1039
263
                    ::Botan::EC_Group group(*curveString);
1040
1041
                    /* Private key */
1042
263
                    {
1043
263
                        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
263
                        CF_CHECK_NE(priv_bn, 0);
1048
1049
261
                        priv = std::make_unique<PrivkeyType>(PrivkeyType(rng, group, priv_bn));
1050
261
                    }
1051
1052
                    /* Prepare signer */
1053
261
                    CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.digestType.Get()), std::nullopt);
1054
1055
261
                    const std::string emsa1String = Botan_detail::parenthesize("EMSA1", *algoString);
1056
261
                    signer.reset(new ::Botan::PK_Signer(*priv, rng, emsa1String, ::Botan::Signature_Format::DerSequence));
1057
261
                }
1058
1059
                /* Process */
1060
0
                {
1061
261
                    const auto signature = signer->sign_message(op.cleartext.Get(), rng);
1062
1063
                    /* Retrieve R and S */
1064
261
                    {
1065
261
                        ::Botan::BER_Decoder decoder(signature);
1066
261
                        ::Botan::BER_Decoder ber_sig = decoder.start_sequence();
1067
1068
261
                        size_t count = 0;
1069
1070
261
                        ::Botan::BigInt R;
1071
261
                        ::Botan::BigInt S;
1072
625
                        while(ber_sig.more_items())
1073
364
                        {
1074
364
                            switch ( count ) {
1075
182
                                case    0:
1076
182
                                    ber_sig.decode(R);
1077
182
                                    break;
1078
182
                                case    1:
1079
182
                                    ber_sig.decode(S);
1080
182
                                    break;
1081
0
                                default:
1082
0
                                    printf("Error: Too many parts in signature BER\n");
1083
0
                                    abort();
1084
364
                            }
1085
1086
364
                            ++count;
1087
364
                        }
1088
1089
261
                        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
11
                            if (S > ::Botan::BigInt("57896044618658097711785492504343953926418782139537452191302581570759080747168")) {
1094
3
                                S = ::Botan::BigInt("115792089237316195423570985008687907852837564279074904382605163141518161494337") - S;
1095
3
                            }
1096
250
                        } else if ( op.curveType.Get() == CF_ECC_CURVE("secp256r1") ) {
1097
                            /* Similar ECDSA signature malleability adjustment for compatibility with trezor-firmware */
1098
30
                            if (S > ::Botan::BigInt("57896044605178124381348723474703786764998477612067880171211129530534256022184")) {
1099
19
                                S = ::Botan::BigInt("115792089210356248762697446949407573529996955224135760342422259061068512044369") - S;
1100
19
                            }
1101
30
                        }
1102
1103
261
                        const auto pub_x = priv->public_point().get_affine_x().to_dec_string();
1104
261
                        const auto pub_y = priv->public_point().get_affine_y().to_dec_string();
1105
1106
261
                        const auto R_str = R.to_dec_string();
1107
261
                        const auto S_str = S.to_dec_string();
1108
1109
261
                        ret = component::ECDSA_Signature({ R_str, S_str }, { pub_x, pub_y });
1110
261
                    }
1111
261
                }
1112
261
            } catch ( ... ) { }
1113
1114
967
end:
1115
967
            BOTAN_UNSET_GLOBAL_DS
1116
1117
967
            return ret;
1118
292
        }
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
967
        std::optional<component::ECDSA_Signature> ECxDSA_Sign(Operation& op) {
1013
967
            std::optional<component::ECDSA_Signature> ret = std::nullopt;
1014
967
            Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1015
1016
967
            std::unique_ptr<PrivkeyType> priv = nullptr;
1017
967
            std::unique_ptr<::Botan::Public_Key> pub = nullptr;
1018
967
            std::unique_ptr<::Botan::PK_Signer> signer;
1019
1020
967
            BOTAN_FUZZER_RNG;
1021
1022
967
            BOTAN_SET_GLOBAL_DS
1023
1024
967
            if ( RFC6979 == true ) {
1025
967
                CF_CHECK_EQ(op.UseRFC6979Nonce(), true);
1026
660
            } else {
1027
0
                CF_CHECK_EQ(op.UseRandomNonce(), true);
1028
0
            }
1029
1030
660
            CF_CHECK_EQ(op.digestType.Get(), CF_DIGEST("SHA256"));
1031
1032
292
            try {
1033
                /* Initialize */
1034
292
                {
1035
1036
292
                    std::optional<std::string> curveString, algoString;
1037
1038
292
                    CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt);
1039
263
                    ::Botan::EC_Group group(*curveString);
1040
1041
                    /* Private key */
1042
263
                    {
1043
263
                        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
263
                        CF_CHECK_NE(priv_bn, 0);
1048
1049
261
                        priv = std::make_unique<PrivkeyType>(PrivkeyType(rng, group, priv_bn));
1050
261
                    }
1051
1052
                    /* Prepare signer */
1053
261
                    CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.digestType.Get()), std::nullopt);
1054
1055
261
                    const std::string emsa1String = Botan_detail::parenthesize("EMSA1", *algoString);
1056
261
                    signer.reset(new ::Botan::PK_Signer(*priv, rng, emsa1String, ::Botan::Signature_Format::DerSequence));
1057
261
                }
1058
1059
                /* Process */
1060
0
                {
1061
261
                    const auto signature = signer->sign_message(op.cleartext.Get(), rng);
1062
1063
                    /* Retrieve R and S */
1064
261
                    {
1065
261
                        ::Botan::BER_Decoder decoder(signature);
1066
261
                        ::Botan::BER_Decoder ber_sig = decoder.start_sequence();
1067
1068
261
                        size_t count = 0;
1069
1070
261
                        ::Botan::BigInt R;
1071
261
                        ::Botan::BigInt S;
1072
625
                        while(ber_sig.more_items())
1073
364
                        {
1074
364
                            switch ( count ) {
1075
182
                                case    0:
1076
182
                                    ber_sig.decode(R);
1077
182
                                    break;
1078
182
                                case    1:
1079
182
                                    ber_sig.decode(S);
1080
182
                                    break;
1081
0
                                default:
1082
0
                                    printf("Error: Too many parts in signature BER\n");
1083
0
                                    abort();
1084
364
                            }
1085
1086
364
                            ++count;
1087
364
                        }
1088
1089
261
                        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
11
                            if (S > ::Botan::BigInt("57896044618658097711785492504343953926418782139537452191302581570759080747168")) {
1094
3
                                S = ::Botan::BigInt("115792089237316195423570985008687907852837564279074904382605163141518161494337") - S;
1095
3
                            }
1096
250
                        } else if ( op.curveType.Get() == CF_ECC_CURVE("secp256r1") ) {
1097
                            /* Similar ECDSA signature malleability adjustment for compatibility with trezor-firmware */
1098
30
                            if (S > ::Botan::BigInt("57896044605178124381348723474703786764998477612067880171211129530534256022184")) {
1099
19
                                S = ::Botan::BigInt("115792089210356248762697446949407573529996955224135760342422259061068512044369") - S;
1100
19
                            }
1101
30
                        }
1102
1103
261
                        const auto pub_x = priv->public_point().get_affine_x().to_dec_string();
1104
261
                        const auto pub_y = priv->public_point().get_affine_y().to_dec_string();
1105
1106
261
                        const auto R_str = R.to_dec_string();
1107
261
                        const auto S_str = S.to_dec_string();
1108
1109
261
                        ret = component::ECDSA_Signature({ R_str, S_str }, { pub_x, pub_y });
1110
261
                    }
1111
261
                }
1112
261
            } catch ( ... ) { }
1113
1114
967
end:
1115
967
            BOTAN_UNSET_GLOBAL_DS
1116
1117
967
            return ret;
1118
292
        }
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.03k
std::optional<component::ECDSA_Signature> Botan::OpECDSA_Sign(operation::ECDSA_Sign& op) {
1122
1.03k
    if ( op.curveType.Is(CF_ECC_CURVE("ed25519")) ) {
1123
68
        const auto _priv_bytes = util::DecToBin(op.priv.ToTrimmedString(), 32);
1124
68
        if ( _priv_bytes == std::nullopt ) {
1125
1
            return std::nullopt;
1126
1
        }
1127
1128
67
        const ::Botan::secure_vector<uint8_t> priv_bytes(_priv_bytes->data(), _priv_bytes->data() + _priv_bytes->size());
1129
1130
67
        const auto priv = std::make_unique<::Botan::Ed25519_PrivateKey>(priv_bytes);
1131
1132
67
        std::unique_ptr<::Botan::PK_Signer> signer;
1133
1134
67
        Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1135
67
        BOTAN_FUZZER_RNG;
1136
1137
67
        signer.reset(new ::Botan::PK_Signer(*priv, rng, "Pure", ::Botan::Signature_Format::Standard));
1138
1139
67
        const auto signature = signer->sign_message(op.cleartext.Get(), rng);
1140
67
        CF_ASSERT(signature.size() == 64, "ed25519 signature is not 64 bytes");
1141
1142
67
        const auto pub = priv->get_public_key();
1143
67
        CF_ASSERT(pub.size() == 32, "ed25519 pubkey is not 32 bytes");
1144
1145
67
        const auto ret = component::ECDSA_Signature(
1146
67
                { util::BinToDec(signature.data(), 32), util::BinToDec(signature.data() + 32, 32) },
1147
67
                { util::BinToDec(pub.data(), 32), "0"}
1148
67
        );
1149
1150
67
        return ret;
1151
67
    }
1152
1153
967
    return Botan_detail::ECxDSA_Sign<::Botan::ECDSA_PrivateKey, operation::ECDSA_Sign>(op);
1154
1.03k
}
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.28k
        std::optional<bool> ECxDSA_Verify(Operation& op) {
1163
1.28k
            std::optional<bool> ret = std::nullopt;
1164
1.28k
            Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1165
1166
1.28k
            ::Botan::secure_vector<uint8_t> sig;
1167
1.28k
            std::unique_ptr<::Botan::Public_Key> pub = nullptr;
1168
1.28k
            std::unique_ptr<::Botan::EC_Group> group = nullptr;
1169
1.28k
            Buffer CT;
1170
1171
1.28k
            {
1172
1.28k
                BOTAN_SET_GLOBAL_DS
1173
1174
1.28k
                std::optional<std::string> curveString;
1175
1.28k
                CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt);
1176
1.02k
                group = std::make_unique<::Botan::EC_Group>(*curveString);
1177
1.02k
            }
1178
1179
            /* Construct signature */
1180
0
            {
1181
1.02k
                const ::Botan::BigInt R(op.signature.signature.first.ToString(ds));
1182
1.02k
                const ::Botan::BigInt S(op.signature.signature.second.ToString(ds));
1183
1.02k
                try {
1184
1.02k
                    sig = ::Botan::BigInt::encode_fixed_length_int_pair(R, S, group->get_order_bytes());
1185
1.02k
                } catch ( ::Botan::Encoding_Error ) {
1186
                    /* Invalid signature */
1187
23
                    BOTAN_UNSET_GLOBAL_DS
1188
23
                    return false;
1189
23
                }
1190
1.02k
            }
1191
1192
            /* Construct pubkey */
1193
1.00k
            try {
1194
1.00k
                const ::Botan::BigInt pub_x(op.signature.pub.first.ToString(ds));
1195
1.00k
                const ::Botan::BigInt pub_y(op.signature.pub.second.ToString(ds));
1196
1.00k
                const ::Botan::PointGFp public_point = group->point(pub_x, pub_y);
1197
1.00k
                pub = std::make_unique<PubkeyType>(PubkeyType(*group, public_point));
1198
1.00k
            } catch ( ::Botan::Invalid_Argument ) {
1199
                /* Invalid point */
1200
18
                BOTAN_UNSET_GLOBAL_DS
1201
18
                return false;
1202
18
            }
1203
1204
            /* Construct input */
1205
983
            {
1206
983
                if ( op.digestType.Get() == CF_DIGEST("NULL") ) {
1207
0
                    CT = op.cleartext.ECDSA_RandomPad(ds, op.curveType);
1208
983
                } else {
1209
983
                    std::optional<std::string> algoString;
1210
983
                    CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.digestType.Get()), std::nullopt);
1211
1212
956
                    auto hash = ::Botan::HashFunction::create(*algoString);
1213
956
                    hash->update(op.cleartext.GetPtr(), op.cleartext.GetSize());
1214
956
                    const auto _CT = hash->final();
1215
956
                    CT = Buffer(_CT.data(), _CT.size()).ECDSA_RandomPad(ds, op.curveType);
1216
956
                }
1217
983
            }
1218
1219
956
            ret = ::Botan::PK_Verifier(*pub, "Raw").verify_message(CT.Get(), sig);
1220
1221
1.24k
end:
1222
1.24k
            BOTAN_UNSET_GLOBAL_DS
1223
1224
1.24k
            return ret;
1225
956
        }
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.28k
        std::optional<bool> ECxDSA_Verify(Operation& op) {
1163
1.28k
            std::optional<bool> ret = std::nullopt;
1164
1.28k
            Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1165
1166
1.28k
            ::Botan::secure_vector<uint8_t> sig;
1167
1.28k
            std::unique_ptr<::Botan::Public_Key> pub = nullptr;
1168
1.28k
            std::unique_ptr<::Botan::EC_Group> group = nullptr;
1169
1.28k
            Buffer CT;
1170
1171
1.28k
            {
1172
1.28k
                BOTAN_SET_GLOBAL_DS
1173
1174
1.28k
                std::optional<std::string> curveString;
1175
1.28k
                CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt);
1176
1.02k
                group = std::make_unique<::Botan::EC_Group>(*curveString);
1177
1.02k
            }
1178
1179
            /* Construct signature */
1180
0
            {
1181
1.02k
                const ::Botan::BigInt R(op.signature.signature.first.ToString(ds));
1182
1.02k
                const ::Botan::BigInt S(op.signature.signature.second.ToString(ds));
1183
1.02k
                try {
1184
1.02k
                    sig = ::Botan::BigInt::encode_fixed_length_int_pair(R, S, group->get_order_bytes());
1185
1.02k
                } catch ( ::Botan::Encoding_Error ) {
1186
                    /* Invalid signature */
1187
23
                    BOTAN_UNSET_GLOBAL_DS
1188
23
                    return false;
1189
23
                }
1190
1.02k
            }
1191
1192
            /* Construct pubkey */
1193
1.00k
            try {
1194
1.00k
                const ::Botan::BigInt pub_x(op.signature.pub.first.ToString(ds));
1195
1.00k
                const ::Botan::BigInt pub_y(op.signature.pub.second.ToString(ds));
1196
1.00k
                const ::Botan::PointGFp public_point = group->point(pub_x, pub_y);
1197
1.00k
                pub = std::make_unique<PubkeyType>(PubkeyType(*group, public_point));
1198
1.00k
            } catch ( ::Botan::Invalid_Argument ) {
1199
                /* Invalid point */
1200
18
                BOTAN_UNSET_GLOBAL_DS
1201
18
                return false;
1202
18
            }
1203
1204
            /* Construct input */
1205
983
            {
1206
983
                if ( op.digestType.Get() == CF_DIGEST("NULL") ) {
1207
0
                    CT = op.cleartext.ECDSA_RandomPad(ds, op.curveType);
1208
983
                } else {
1209
983
                    std::optional<std::string> algoString;
1210
983
                    CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.digestType.Get()), std::nullopt);
1211
1212
956
                    auto hash = ::Botan::HashFunction::create(*algoString);
1213
956
                    hash->update(op.cleartext.GetPtr(), op.cleartext.GetSize());
1214
956
                    const auto _CT = hash->final();
1215
956
                    CT = Buffer(_CT.data(), _CT.size()).ECDSA_RandomPad(ds, op.curveType);
1216
956
                }
1217
983
            }
1218
1219
956
            ret = ::Botan::PK_Verifier(*pub, "Raw").verify_message(CT.Get(), sig);
1220
1221
1.24k
end:
1222
1.24k
            BOTAN_UNSET_GLOBAL_DS
1223
1224
1.24k
            return ret;
1225
956
        }
Unexecuted instantiation: std::__1::optional<bool> cryptofuzz::module::Botan_detail::ECxDSA_Verify<Botan::ECGDSA_PublicKey, cryptofuzz::operation::ECGDSA_Verify>(cryptofuzz::operation::ECGDSA_Verify&)
1226
} /* namespace Botan_detail */
1227
1228
1.43k
std::optional<bool> Botan::OpECDSA_Verify(operation::ECDSA_Verify& op) {
1229
1.43k
    if ( op.curveType.Is(CF_ECC_CURVE("ed25519")) ) {
1230
149
        const auto pub_bytes = util::DecToBin(op.signature.pub.first.ToTrimmedString(), 32);
1231
149
        if ( pub_bytes == std::nullopt ) {
1232
2
            return std::nullopt;
1233
2
        }
1234
147
        const auto pub = std::make_unique<::Botan::Ed25519_PublicKey>(*pub_bytes);
1235
1236
147
        const auto sig_r = util::DecToBin(op.signature.signature.first.ToTrimmedString(), 32);
1237
147
        if ( sig_r == std::nullopt ) {
1238
2
            return std::nullopt;
1239
2
        }
1240
1241
145
        const auto sig_s = util::DecToBin(op.signature.signature.second.ToTrimmedString(), 32);
1242
145
        if ( sig_s == std::nullopt ) {
1243
2
            return std::nullopt;
1244
2
        }
1245
1246
143
        std::vector<uint8_t> sig_bytes(64);
1247
143
        memcpy(sig_bytes.data(), sig_r->data(), 32);
1248
143
        memcpy(sig_bytes.data() + 32, sig_s->data(), 32);
1249
1250
143
        const bool ret = ::Botan::PK_Verifier(*pub, "Pure").verify_message(op.cleartext.Get(), sig_bytes);
1251
143
        return ret;
1252
1253
1.28k
    } else {
1254
1.28k
        return Botan_detail::ECxDSA_Verify<::Botan::ECDSA_PublicKey, operation::ECDSA_Verify>(op);
1255
1.28k
    }
1256
1.43k
}
1257
1258
0
std::optional<bool> Botan::OpECGDSA_Verify(operation::ECGDSA_Verify& op) {
1259
0
    return Botan_detail::ECxDSA_Verify<::Botan::ECGDSA_PublicKey, operation::ECGDSA_Verify>(op);
1260
0
}
1261
1262
0
std::optional<component::ECC_PublicKey> Botan::OpECDSA_Recover(operation::ECDSA_Recover& op) {
1263
0
    std::optional<component::ECC_PublicKey> ret = std::nullopt;
1264
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1265
1266
0
    std::unique_ptr<::Botan::EC_Group> group = nullptr;
1267
0
    Buffer CT;
1268
1269
0
    {
1270
0
        std::optional<std::string> curveString;
1271
0
        CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt);
1272
0
        group = std::make_unique<::Botan::EC_Group>(*curveString);
1273
0
    }
1274
1275
    /* Construct input */
1276
0
    {
1277
0
        if ( op.digestType.Get() == CF_DIGEST("NULL") ) {
1278
0
            CT = op.cleartext.ECDSA_RandomPad(ds, op.curveType);
1279
0
        } else {
1280
0
            std::optional<std::string> algoString;
1281
0
            CF_CHECK_NE(algoString = Botan_detail::DigestIDToString(op.digestType.Get()), std::nullopt);
1282
1283
0
            auto hash = ::Botan::HashFunction::create(*algoString);
1284
0
            hash->update(op.cleartext.GetPtr(), op.cleartext.GetSize());
1285
0
            const auto _CT = hash->final();
1286
0
            CT = Buffer(_CT.data(), _CT.size()).ECDSA_RandomPad(ds, op.curveType);
1287
0
        }
1288
0
    }
1289
1290
0
    {
1291
0
        const ::Botan::BigInt R(op.signature.first.ToString(ds));
1292
0
        const ::Botan::BigInt S(op.signature.second.ToString(ds));
1293
1294
0
        std::unique_ptr<::Botan::ECDSA_PublicKey> pub = nullptr;
1295
0
        try {
1296
0
            pub = std::make_unique<::Botan::ECDSA_PublicKey>(*group, CT.Get(), R, S, op.id);
1297
1298
0
            ret = {
1299
0
                pub->public_point().get_affine_x().to_dec_string(),
1300
0
                pub->public_point().get_affine_y().to_dec_string()
1301
0
            };
1302
0
        } catch ( ::Botan::Invalid_State& e ) {
1303
0
        } catch ( ::Botan::Decoding_Error& ) {
1304
0
        } catch ( ::Botan::Invalid_Argument& ) {
1305
            //ret = {"0", "0"};
1306
0
        }
1307
1308
0
    }
1309
1310
0
end:
1311
0
    return ret;
1312
0
}
1313
1314
0
std::optional<component::Bignum> Botan::OpDH_Derive(operation::DH_Derive& op) {
1315
0
    std::optional<component::Bignum> ret = std::nullopt;
1316
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1317
1318
0
    BOTAN_FUZZER_RNG;
1319
1320
0
    try {
1321
0
        CF_CHECK_NE(op.priv.ToTrimmedString(), "0");
1322
1323
0
        const ::Botan::BigInt g(op.base.ToString(ds));
1324
0
        const ::Botan::BigInt p(op.prime.ToString(ds));
1325
0
        const ::Botan::DL_Group grp(p, g);
1326
1327
0
        const ::Botan::BigInt _priv(op.priv.ToString(ds));
1328
1329
        /* Prevent time-out */
1330
0
        CF_CHECK_LT(g.bytes(), 80);
1331
0
        CF_CHECK_LT(p.bytes(), 80);
1332
0
        CF_CHECK_LT(_priv.bytes(), 80);
1333
1334
0
        std::unique_ptr<::Botan::Private_Key> priv(new ::Botan::DH_PrivateKey(grp, _priv));
1335
1336
0
        const ::Botan::BigInt _pub(op.pub.ToString(ds));
1337
0
        ::Botan::DH_PublicKey pub(grp, _pub);
1338
1339
0
        std::unique_ptr<::Botan::PK_Key_Agreement> kas(new ::Botan::PK_Key_Agreement(*priv, rng, "Raw"));
1340
0
        const auto derived_key = kas->derive_key(0, pub.public_value());
1341
1342
0
        const auto derived_str = ::Botan::BigInt(derived_key.bits_of()).to_dec_string();
1343
0
        if ( derived_str != "0" ) {
1344
0
            ret = derived_str;
1345
0
        }
1346
0
    } catch ( ... ) { }
1347
1348
0
end:
1349
0
    return ret;
1350
0
}
1351
1352
0
std::optional<component::ECC_Point> Botan::OpECC_Point_Add(operation::ECC_Point_Add& op) {
1353
0
    std::optional<component::ECC_Point> ret = std::nullopt;
1354
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1355
1356
0
    BOTAN_FUZZER_RNG;
1357
1358
0
    std::unique_ptr<::Botan::EC_Group> group = nullptr;
1359
0
    std::unique_ptr<::Botan::PointGFp> a, b;
1360
1361
0
    {
1362
0
        std::optional<std::string> curveString;
1363
0
        CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt);
1364
0
        group = std::make_unique<::Botan::EC_Group>(*curveString);
1365
0
    }
1366
1367
0
    {
1368
        /* A */
1369
0
        {
1370
0
            const auto a_x = ::Botan::BigInt(op.a.first.ToString(ds));
1371
0
            CF_CHECK_GTE(a_x, 0);
1372
1373
0
            const auto a_y = ::Botan::BigInt(op.a.second.ToString(ds));
1374
0
            CF_CHECK_GTE(a_y, 0);
1375
1376
0
            try {
1377
0
                a = std::make_unique<::Botan::PointGFp>(group->point(a_x, a_y));
1378
0
            } catch ( ::Botan::Invalid_Argument ) {
1379
0
                goto end;
1380
0
            }
1381
0
            CF_CHECK_TRUE(a->on_the_curve());
1382
0
        }
1383
1384
        /* B */
1385
0
        {
1386
0
            const auto b_x = ::Botan::BigInt(op.b.first.ToString(ds));
1387
0
            CF_CHECK_GTE(b_x, 0);
1388
1389
0
            const auto b_y = ::Botan::BigInt(op.b.second.ToString(ds));
1390
0
            CF_CHECK_GTE(b_y, 0);
1391
1392
0
            try {
1393
0
                b = std::make_unique<::Botan::PointGFp>(group->point(b_x, b_y));
1394
0
            } catch ( ::Botan::Invalid_Argument ) {
1395
0
                goto end;
1396
0
            }
1397
1398
0
            CF_CHECK_TRUE(b->on_the_curve());
1399
0
        }
1400
1401
0
        const bool is_negation = *a == -(*b);
1402
1403
0
        ::Botan::PointGFp _res = *a + *b;
1404
1405
0
        const bool is_zero = _res.is_zero();
1406
1407
        /* If A is a negation of B, then addition of both should result in point at infinity */
1408
        /* Otherwise, it should result in non-infinity. */
1409
0
        CF_ASSERT(is_zero == is_negation, "Unexpected point addition result");
1410
0
        CF_CHECK_FALSE(is_zero);
1411
1412
0
        const auto x = _res.get_affine_x();
1413
0
        const auto y = _res.get_affine_y();
1414
1415
0
        ret = {
1416
0
            util::HexToDec(x.to_hex_string()),
1417
0
            util::HexToDec(y.to_hex_string()),
1418
0
        };
1419
1420
0
    }
1421
1422
0
end:
1423
0
    return ret;
1424
0
}
1425
1426
0
std::optional<component::ECC_Point> Botan::OpECC_Point_Sub(operation::ECC_Point_Sub& op) {
1427
0
    std::optional<component::ECC_Point> ret = std::nullopt;
1428
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1429
1430
0
    BOTAN_FUZZER_RNG;
1431
1432
0
    std::unique_ptr<::Botan::EC_Group> group = nullptr;
1433
0
    std::unique_ptr<::Botan::PointGFp> a, b;
1434
1435
0
    {
1436
0
        std::optional<std::string> curveString;
1437
0
        CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt);
1438
0
        group = std::make_unique<::Botan::EC_Group>(*curveString);
1439
0
    }
1440
1441
0
    {
1442
        /* A */
1443
0
        {
1444
0
            const auto a_x = ::Botan::BigInt(op.a.first.ToString(ds));
1445
0
            CF_CHECK_GTE(a_x, 0);
1446
1447
0
            const auto a_y = ::Botan::BigInt(op.a.second.ToString(ds));
1448
0
            CF_CHECK_GTE(a_y, 0);
1449
1450
0
            try {
1451
0
                a = std::make_unique<::Botan::PointGFp>(group->point(a_x, a_y));
1452
0
            } catch ( ::Botan::Invalid_Argument ) {
1453
0
                goto end;
1454
0
            }
1455
0
            CF_CHECK_TRUE(a->on_the_curve());
1456
0
        }
1457
1458
        /* B */
1459
0
        {
1460
0
            const auto b_x = ::Botan::BigInt(op.b.first.ToString(ds));
1461
0
            CF_CHECK_GTE(b_x, 0);
1462
1463
0
            const auto b_y = ::Botan::BigInt(op.b.second.ToString(ds));
1464
0
            CF_CHECK_GTE(b_y, 0);
1465
1466
0
            try {
1467
0
                b = std::make_unique<::Botan::PointGFp>(group->point(b_x, b_y));
1468
0
            } catch ( ::Botan::Invalid_Argument ) {
1469
0
                goto end;
1470
0
            }
1471
1472
0
            CF_CHECK_TRUE(b->on_the_curve());
1473
0
        }
1474
1475
0
        const bool is_eq = *a == *b;
1476
1477
0
        ::Botan::PointGFp _res = *a - *b;
1478
1479
0
        const bool is_zero = _res.is_zero();
1480
1481
        /* If A equals B, then subtraction of both should result in point at infinity */
1482
        /* Otherwise, it should result in non-infinity. */
1483
0
        CF_ASSERT(is_zero == is_eq, "Unexpected point subtraction result");
1484
0
        CF_CHECK_FALSE(is_zero);
1485
1486
0
        const auto x = _res.get_affine_x();
1487
0
        const auto y = _res.get_affine_y();
1488
1489
0
        ret = {
1490
0
            util::HexToDec(x.to_hex_string()),
1491
0
            util::HexToDec(y.to_hex_string()),
1492
0
        };
1493
1494
0
    }
1495
1496
0
end:
1497
0
    return ret;
1498
0
}
1499
1500
0
std::optional<component::ECC_Point> Botan::OpECC_Point_Mul(operation::ECC_Point_Mul& op) {
1501
0
    std::optional<component::ECC_Point> ret = std::nullopt;
1502
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1503
1504
0
    BOTAN_FUZZER_RNG;
1505
1506
0
    std::unique_ptr<::Botan::EC_Group> group = nullptr;
1507
1508
0
    {
1509
0
        std::optional<std::string> curveString;
1510
0
        CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt);
1511
0
        group = std::make_unique<::Botan::EC_Group>(*curveString);
1512
0
    }
1513
1514
0
    try {
1515
0
        const auto a_x = ::Botan::BigInt(op.a.first.ToString(ds));
1516
0
        CF_CHECK_GTE(a_x, 0);
1517
1518
0
        const auto a_y = ::Botan::BigInt(op.a.second.ToString(ds));
1519
0
        CF_CHECK_GTE(a_y, 0);
1520
1521
0
        const auto a = group->point(a_x, a_y);
1522
0
        CF_CHECK_TRUE(a.on_the_curve());
1523
1524
0
        const auto b = ::Botan::BigInt(op.b.ToString(ds));
1525
1526
0
        CF_CHECK_GTE(b, 0);
1527
1528
0
        std::vector<::Botan::BigInt> ws(::Botan::PointGFp::WORKSPACE_SIZE);
1529
1530
0
        bool useBlinding = false;
1531
#if defined(CRYPTOFUZZ_BOTAN_IS_ORACLE)
1532
        try {
1533
            useBlinding = ds.Get<bool>();
1534
        } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
1535
#endif
1536
1537
0
        ::Botan::PointGFp _res;
1538
1539
0
        if ( useBlinding == false ) {
1540
0
            _res = a * b;
1541
0
        } else {
1542
0
            _res = group->blinded_var_point_multiply(a, b, rng, ws);
1543
0
        }
1544
1545
0
        const auto x = _res.get_affine_x();
1546
0
        const auto y = _res.get_affine_y();
1547
1548
0
        ret = {
1549
0
            util::HexToDec(x.to_hex_string()),
1550
0
            util::HexToDec(y.to_hex_string()),
1551
0
        };
1552
1553
0
    } catch ( ... ) { }
1554
1555
0
end:
1556
0
    return ret;
1557
0
}
1558
1559
0
std::optional<component::ECC_Point> Botan::OpECC_Point_Neg(operation::ECC_Point_Neg& op) {
1560
0
    std::optional<component::ECC_Point> ret = std::nullopt;
1561
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1562
1563
0
    std::unique_ptr<::Botan::EC_Group> group = nullptr;
1564
1565
0
    {
1566
0
        std::optional<std::string> curveString;
1567
0
        CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt);
1568
0
        group = std::make_unique<::Botan::EC_Group>(*curveString);
1569
0
    }
1570
1571
0
    try {
1572
0
        const auto a_x = ::Botan::BigInt(op.a.first.ToString(ds));
1573
0
        CF_CHECK_GTE(a_x, 0);
1574
1575
0
        const auto a_y = ::Botan::BigInt(op.a.second.ToString(ds));
1576
0
        CF_CHECK_GTE(a_y, 0);
1577
1578
0
        const auto a = group->point(a_x, a_y);
1579
0
        CF_CHECK_TRUE(a.on_the_curve());
1580
1581
0
        const ::Botan::PointGFp _res = -a;
1582
1583
0
        const auto x = _res.get_affine_x();
1584
0
        const auto y = _res.get_affine_y();
1585
1586
0
        ret = {
1587
0
            util::HexToDec(x.to_hex_string()),
1588
0
            util::HexToDec(y.to_hex_string()),
1589
0
        };
1590
1591
0
    } catch ( ... ) { }
1592
1593
0
end:
1594
0
    return ret;
1595
0
}
1596
1597
0
std::optional<component::ECC_Point> Botan::OpECC_Point_Dbl(operation::ECC_Point_Dbl& op) {
1598
0
    std::optional<component::ECC_Point> ret = std::nullopt;
1599
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1600
1601
0
    std::unique_ptr<::Botan::EC_Group> group = nullptr;
1602
1603
0
    {
1604
0
        std::optional<std::string> curveString;
1605
0
        CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt);
1606
0
        group = std::make_unique<::Botan::EC_Group>(*curveString);
1607
0
    }
1608
1609
0
    try {
1610
0
        const auto a_x = ::Botan::BigInt(op.a.first.ToString(ds));
1611
0
        CF_CHECK_GTE(a_x, 0);
1612
1613
0
        const auto a_y = ::Botan::BigInt(op.a.second.ToString(ds));
1614
0
        CF_CHECK_GTE(a_y, 0);
1615
1616
0
        const auto a = group->point(a_x, a_y);
1617
0
        CF_CHECK_TRUE(a.on_the_curve());
1618
1619
0
        const ::Botan::PointGFp _res = a + a;
1620
1621
0
        const auto x = _res.get_affine_x();
1622
0
        const auto y = _res.get_affine_y();
1623
1624
0
        ret = {
1625
0
            util::HexToDec(x.to_hex_string()),
1626
0
            util::HexToDec(y.to_hex_string()),
1627
0
        };
1628
1629
0
    } catch ( ... ) { }
1630
1631
0
end:
1632
0
    return ret;
1633
0
}
1634
1635
0
std::optional<bool> Botan::OpECC_Point_Cmp(operation::ECC_Point_Cmp& op) {
1636
0
    std::optional<bool> ret = std::nullopt;
1637
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1638
1639
0
    BOTAN_FUZZER_RNG;
1640
1641
0
    std::unique_ptr<::Botan::EC_Group> group = nullptr;
1642
0
    std::unique_ptr<::Botan::PointGFp> a, b;
1643
1644
0
    {
1645
0
        std::optional<std::string> curveString;
1646
0
        CF_CHECK_NE(curveString = Botan_detail::CurveIDToString(op.curveType.Get()), std::nullopt);
1647
0
        group = std::make_unique<::Botan::EC_Group>(*curveString);
1648
0
    }
1649
1650
0
    {
1651
        /* A */
1652
0
        {
1653
0
            const auto a_x = ::Botan::BigInt(op.a.first.ToString(ds));
1654
0
            CF_CHECK_GTE(a_x, 0);
1655
1656
0
            const auto a_y = ::Botan::BigInt(op.a.second.ToString(ds));
1657
0
            CF_CHECK_GTE(a_y, 0);
1658
1659
0
            try {
1660
0
                a = std::make_unique<::Botan::PointGFp>(group->point(a_x, a_y));
1661
0
            } catch ( ::Botan::Invalid_Argument ) {
1662
0
                goto end;
1663
0
            }
1664
0
            CF_CHECK_TRUE(a->on_the_curve());
1665
0
        }
1666
1667
        /* B */
1668
0
        {
1669
0
            const auto b_x = ::Botan::BigInt(op.b.first.ToString(ds));
1670
0
            CF_CHECK_GTE(b_x, 0);
1671
1672
0
            const auto b_y = ::Botan::BigInt(op.b.second.ToString(ds));
1673
0
            CF_CHECK_GTE(b_y, 0);
1674
1675
0
            try {
1676
0
                b = std::make_unique<::Botan::PointGFp>(group->point(b_x, b_y));
1677
0
            } catch ( ::Botan::Invalid_Argument ) {
1678
0
                goto end;
1679
0
            }
1680
1681
0
            CF_CHECK_TRUE(b->on_the_curve());
1682
0
        }
1683
1684
0
        ret = *a == *b;
1685
0
    }
1686
1687
0
end:
1688
0
    return ret;
1689
0
}
1690
1691
0
std::optional<bool> Botan::OpDSA_Verify(operation::DSA_Verify& op) {
1692
0
    std::optional<bool> ret = std::nullopt;
1693
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1694
1695
0
    BOTAN_FUZZER_RNG;
1696
1697
0
    try {
1698
0
        const auto p = ::Botan::BigInt(op.parameters.p.ToString(ds));
1699
        /* Avoid time-outs */
1700
0
        CF_CHECK_LTE(p.bytes(), 300);
1701
0
        const auto q = ::Botan::BigInt(op.parameters.q.ToString(ds));
1702
0
        const auto g = ::Botan::BigInt(op.parameters.g.ToString(ds));
1703
1704
        /* Botan can verify signatures with g = 0.
1705
         * Avoid discrepancies with OpenSSL
1706
         */
1707
0
        CF_CHECK_NE(g, 0);
1708
1709
0
        const ::Botan::DL_Group group(p, q, g);
1710
0
        CF_CHECK_TRUE(group.verify_group(rng));
1711
1712
0
        const auto y = ::Botan::BigInt(op.pub.ToString(ds));
1713
0
        const auto pub = std::make_unique<::Botan::DSA_PublicKey>(group, y);
1714
1715
0
        const auto r = ::Botan::BigInt(op.signature.first.ToString(ds));
1716
0
        const auto s = ::Botan::BigInt(op.signature.second.ToString(ds));
1717
1718
0
        const auto sig = ::Botan::BigInt::encode_fixed_length_int_pair(
1719
0
                r, s, q.bytes());
1720
0
        auto verifier = ::Botan::PK_Verifier(*pub, "Raw");
1721
0
        verifier.update(op.cleartext.Get());
1722
0
        ret = verifier.check_signature(sig);
1723
0
    } catch ( ... ) {
1724
0
    }
1725
1726
0
end:
1727
0
    return ret;
1728
0
}
1729
1730
0
std::optional<component::Bignum> Botan::OpBignumCalc(operation::BignumCalc& op) {
1731
0
    std::optional<component::Bignum> ret = std::nullopt;
1732
1733
0
    if ( op.modulo ) {
1734
0
        switch ( op.calcOp.Get() ) {
1735
0
            case    CF_CALCOP("Add(A,B)"):
1736
0
            case    CF_CALCOP("Bit(A,B)"):
1737
0
            case    CF_CALCOP("CondSet(A,B)"):
1738
0
            case    CF_CALCOP("Exp(A,B)"):
1739
0
            case    CF_CALCOP("InvMod(A,B)"):
1740
0
            case    CF_CALCOP("IsEq(A,B)"):
1741
0
            case    CF_CALCOP("IsEven(A)"):
1742
0
            case    CF_CALCOP("IsOdd(A)"):
1743
0
            case    CF_CALCOP("IsOne(A)"):
1744
0
            case    CF_CALCOP("IsZero(A)"):
1745
0
            case    CF_CALCOP("LShift1(A)"):
1746
0
            case    CF_CALCOP("Mul(A,B)"):
1747
0
            case    CF_CALCOP("Not(A)"):
1748
0
            case    CF_CALCOP("NumBits(A)"):
1749
0
            case    CF_CALCOP("RShift(A,B)"):
1750
0
            case    CF_CALCOP("Set(A)"):
1751
0
            case    CF_CALCOP("Sqr(A)"):
1752
0
            case    CF_CALCOP("Sqrt(A)"):
1753
0
            case    CF_CALCOP("Sub(A,B)"):
1754
0
                break;
1755
0
            default:
1756
0
                return ret;
1757
0
        }
1758
0
    }
1759
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1760
1761
0
    Botan_bignum::Bignum res(&ds, "0");
1762
0
    std::vector<Botan_bignum::Bignum> bn{
1763
0
        Botan_bignum::Bignum(&ds, op.bn0.ToString(ds)),
1764
0
        Botan_bignum::Bignum(&ds, op.bn1.ToString(ds)),
1765
0
        Botan_bignum::Bignum(&ds, op.bn2.ToString(ds)),
1766
0
        Botan_bignum::Bignum(&ds, op.bn3.ToString(ds))
1767
0
    };
1768
0
    std::unique_ptr<Botan_bignum::Operation> opRunner = nullptr;
1769
1770
0
    switch ( op.calcOp.Get() ) {
1771
0
        case    CF_CALCOP("Add(A,B)"):
1772
0
            opRunner = std::make_unique<Botan_bignum::Add>();
1773
0
            break;
1774
0
        case    CF_CALCOP("Sub(A,B)"):
1775
0
            opRunner = std::make_unique<Botan_bignum::Sub>();
1776
0
            break;
1777
0
        case    CF_CALCOP("Mul(A,B)"):
1778
0
            opRunner = std::make_unique<Botan_bignum::Mul>();
1779
0
            break;
1780
0
        case    CF_CALCOP("Div(A,B)"):
1781
0
            opRunner = std::make_unique<Botan_bignum::Div>();
1782
0
            break;
1783
0
        case    CF_CALCOP("Mod(A,B)"):
1784
0
            opRunner = std::make_unique<Botan_bignum::Mod>();
1785
0
            break;
1786
0
        case    CF_CALCOP("ExpMod(A,B,C)"):
1787
            /* Too slow with larger values */
1788
0
            CF_CHECK_LT(op.bn0.GetSize(), 1000);
1789
0
            CF_CHECK_LT(op.bn1.GetSize(), 1000);
1790
0
            CF_CHECK_LT(op.bn2.GetSize(), 1000);
1791
1792
0
            opRunner = std::make_unique<Botan_bignum::ExpMod>();
1793
0
            break;
1794
0
        case    CF_CALCOP("Exp(A,B)"):
1795
0
            opRunner = std::make_unique<Botan_bignum::Exp>();
1796
0
            break;
1797
0
        case    CF_CALCOP("Sqr(A)"):
1798
0
            opRunner = std::make_unique<Botan_bignum::Sqr>();
1799
0
            break;
1800
0
        case    CF_CALCOP("GCD(A,B)"):
1801
0
            opRunner = std::make_unique<Botan_bignum::GCD>();
1802
0
            break;
1803
0
        case    CF_CALCOP("SqrMod(A,B)"):
1804
0
            opRunner = std::make_unique<Botan_bignum::SqrMod>();
1805
0
            break;
1806
0
        case    CF_CALCOP("InvMod(A,B)"):
1807
0
            opRunner = std::make_unique<Botan_bignum::InvMod>();
1808
0
            break;
1809
0
        case    CF_CALCOP("Cmp(A,B)"):
1810
0
            opRunner = std::make_unique<Botan_bignum::Cmp>();
1811
0
            break;
1812
0
        case    CF_CALCOP("LCM(A,B)"):
1813
0
            opRunner = std::make_unique<Botan_bignum::LCM>();
1814
0
            break;
1815
0
        case    CF_CALCOP("Abs(A)"):
1816
0
            opRunner = std::make_unique<Botan_bignum::Abs>();
1817
0
            break;
1818
0
        case    CF_CALCOP("Jacobi(A,B)"):
1819
0
            opRunner = std::make_unique<Botan_bignum::Jacobi>();
1820
0
            break;
1821
0
        case    CF_CALCOP("Neg(A)"):
1822
0
            opRunner = std::make_unique<Botan_bignum::Neg>();
1823
0
            break;
1824
0
        case    CF_CALCOP("IsPrime(A)"):
1825
0
            opRunner = std::make_unique<Botan_bignum::IsPrime>();
1826
0
            break;
1827
0
        case    CF_CALCOP("RShift(A,B)"):
1828
0
            opRunner = std::make_unique<Botan_bignum::RShift>();
1829
0
            break;
1830
0
        case    CF_CALCOP("LShift1(A)"):
1831
0
            opRunner = std::make_unique<Botan_bignum::LShift1>();
1832
0
            break;
1833
0
        case    CF_CALCOP("IsNeg(A)"):
1834
0
            opRunner = std::make_unique<Botan_bignum::IsNeg>();
1835
0
            break;
1836
0
        case    CF_CALCOP("IsEq(A,B)"):
1837
0
            opRunner = std::make_unique<Botan_bignum::IsEq>();
1838
0
            break;
1839
0
        case    CF_CALCOP("IsGt(A,B)"):
1840
0
            opRunner = std::make_unique<Botan_bignum::IsGt>();
1841
0
            break;
1842
0
        case    CF_CALCOP("IsGte(A,B)"):
1843
0
            opRunner = std::make_unique<Botan_bignum::IsGte>();
1844
0
            break;
1845
0
        case    CF_CALCOP("IsLt(A,B)"):
1846
0
            opRunner = std::make_unique<Botan_bignum::IsLt>();
1847
0
            break;
1848
0
        case    CF_CALCOP("IsLte(A,B)"):
1849
0
            opRunner = std::make_unique<Botan_bignum::IsLte>();
1850
0
            break;
1851
0
        case    CF_CALCOP("IsEven(A)"):
1852
0
            opRunner = std::make_unique<Botan_bignum::IsEven>();
1853
0
            break;
1854
0
        case    CF_CALCOP("IsOdd(A)"):
1855
0
            opRunner = std::make_unique<Botan_bignum::IsOdd>();
1856
0
            break;
1857
0
        case    CF_CALCOP("IsZero(A)"):
1858
0
            opRunner = std::make_unique<Botan_bignum::IsZero>();
1859
0
            break;
1860
0
        case    CF_CALCOP("IsNotZero(A)"):
1861
0
            opRunner = std::make_unique<Botan_bignum::IsNotZero>();
1862
0
            break;
1863
0
        case    CF_CALCOP("IsOne(A)"):
1864
0
            opRunner = std::make_unique<Botan_bignum::IsOne>();
1865
0
            break;
1866
0
        case    CF_CALCOP("MulMod(A,B,C)"):
1867
0
            opRunner = std::make_unique<Botan_bignum::MulMod>();
1868
0
            break;
1869
0
        case    CF_CALCOP("Bit(A,B)"):
1870
0
            opRunner = std::make_unique<Botan_bignum::Bit>();
1871
0
            break;
1872
0
        case    CF_CALCOP("CmpAbs(A,B)"):
1873
0
            opRunner = std::make_unique<Botan_bignum::CmpAbs>();
1874
0
            break;
1875
0
        case    CF_CALCOP("SetBit(A,B)"):
1876
0
            opRunner = std::make_unique<Botan_bignum::SetBit>();
1877
0
            break;
1878
0
        case    CF_CALCOP("Mod_NIST_192(A)"):
1879
0
            opRunner = std::make_unique<Botan_bignum::Mod_NIST_192>();
1880
0
            break;
1881
0
        case    CF_CALCOP("Mod_NIST_224(A)"):
1882
0
            opRunner = std::make_unique<Botan_bignum::Mod_NIST_224>();
1883
0
            break;
1884
0
        case    CF_CALCOP("Mod_NIST_256(A)"):
1885
0
            opRunner = std::make_unique<Botan_bignum::Mod_NIST_256>();
1886
0
            break;
1887
0
        case    CF_CALCOP("Mod_NIST_384(A)"):
1888
0
            opRunner = std::make_unique<Botan_bignum::Mod_NIST_384>();
1889
0
            break;
1890
0
        case    CF_CALCOP("Mod_NIST_521(A)"):
1891
0
            opRunner = std::make_unique<Botan_bignum::Mod_NIST_521>();
1892
0
            break;
1893
0
        case    CF_CALCOP("ClearBit(A,B)"):
1894
0
            opRunner = std::make_unique<Botan_bignum::ClearBit>();
1895
0
            break;
1896
0
        case    CF_CALCOP("MulAdd(A,B,C)"):
1897
0
            opRunner = std::make_unique<Botan_bignum::MulAdd>();
1898
0
            break;
1899
0
        case    CF_CALCOP("MulDiv(A,B,C)"):
1900
0
            opRunner = std::make_unique<Botan_bignum::MulDiv>();
1901
0
            break;
1902
0
        case    CF_CALCOP("MulDivCeil(A,B,C)"):
1903
0
            opRunner = std::make_unique<Botan_bignum::MulDivCeil>();
1904
0
            break;
1905
0
        case    CF_CALCOP("Exp2(A)"):
1906
0
            opRunner = std::make_unique<Botan_bignum::Exp2>();
1907
0
            break;
1908
0
        case    CF_CALCOP("NumLSZeroBits(A)"):
1909
0
            opRunner = std::make_unique<Botan_bignum::NumLSZeroBits>();
1910
0
            break;
1911
0
        case    CF_CALCOP("Sqrt(A)"):
1912
0
            if ( op.modulo == std::nullopt ) {
1913
0
                opRunner = std::make_unique<Botan_bignum::Sqrt>();
1914
0
            } else {
1915
0
                opRunner = std::make_unique<Botan_bignum::Ressol>();
1916
0
            }
1917
0
            break;
1918
0
        case    CF_CALCOP("AddMod(A,B,C)"):
1919
0
            opRunner = std::make_unique<Botan_bignum::AddMod>();
1920
0
            break;
1921
0
        case    CF_CALCOP("SubMod(A,B,C)"):
1922
0
            opRunner = std::make_unique<Botan_bignum::SubMod>();
1923
0
            break;
1924
0
        case    CF_CALCOP("NumBits(A)"):
1925
0
            opRunner = std::make_unique<Botan_bignum::NumBits>();
1926
0
            break;
1927
0
        case    CF_CALCOP("Set(A)"):
1928
0
            opRunner = std::make_unique<Botan_bignum::Set>();
1929
0
            break;
1930
0
        case    CF_CALCOP("CondSet(A,B)"):
1931
0
            opRunner = std::make_unique<Botan_bignum::CondSet>();
1932
0
            break;
1933
        /*
1934
        case    CF_CALCOP("Ressol(A,B)"):
1935
            opRunner = std::make_unique<Botan_bignum::Ressol>();
1936
            break;
1937
        */
1938
0
        case    CF_CALCOP("Not(A)"):
1939
0
            opRunner = std::make_unique<Botan_bignum::Not>();
1940
0
            break;
1941
0
        case    CF_CALCOP("Prime()"):
1942
0
            opRunner = std::make_unique<Botan_bignum::Prime>();
1943
0
            break;
1944
0
        case    CF_CALCOP("RandRange(A,B)"):
1945
0
            opRunner = std::make_unique<Botan_bignum::RandRange>();
1946
0
            break;
1947
0
        case    CF_CALCOP("IsSquare(A)"):
1948
0
            opRunner = std::make_unique<Botan_bignum::IsSquare>();
1949
0
            break;
1950
0
    }
1951
1952
0
    CF_CHECK_NE(opRunner, nullptr);
1953
1954
#if defined(CRYPTOFUZZ_BOTAN_IS_ORACLE)
1955
    try {
1956
#endif
1957
0
        CF_CHECK_EQ(opRunner->Run(
1958
0
                    ds,
1959
0
                    res,
1960
0
                    bn,
1961
0
                    op.modulo ?
1962
0
                        std::optional<Botan_bignum::Bignum>(Botan_bignum::Bignum(op.modulo->ToTrimmedString())) :
1963
0
                        std::nullopt), true);
1964
#if defined(CRYPTOFUZZ_BOTAN_IS_ORACLE)
1965
    } catch ( ... ) {
1966
        goto end;
1967
    }
1968
#endif
1969
1970
0
    ret = { util::HexToDec(res.Ref().to_hex_string()) };
1971
1972
0
end:
1973
0
    return ret;
1974
0
}
1975
1976
0
bool Botan::SupportsModularBignumCalc(void) const {
1977
0
    return true;
1978
0
}
1979
1980
} /* namespace module */
1981
} /* namespace cryptofuzz */