Coverage Report

Created: 2023-09-25 06:34

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