Coverage Report

Created: 2026-11-12 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cryptofuzz-normal-math/modules/wolfcrypt/ecdsa_generic.cpp
Line
Count
Source
1
#include "ecdsa_generic.h"
2
#include "module_internal.h"
3
#include "shared.h"
4
#include "bn_ops.h"
5
#include <cryptofuzz/util.h>
6
#include <iostream>
7
8
namespace cryptofuzz {
9
namespace module {
10
namespace wolfCrypt_detail {
11
12
#if !defined(WOLFSSL_SP_MATH)
13
#include "custom_curves.h"
14
#endif
15
16
#if defined(CRYPTOFUZZ_WOLFCRYPT_ALLOCATION_FAILURES)
17
    extern bool haveAllocFailure;
18
#endif
19
20
WC_RNG* GetRNG(void);
21
22
ECCKey::ECCKey(Datasource& ds) :
23
28.3k
    ds(ds) {
24
28.3k
    if ( (key = wc_ecc_key_new(nullptr)) == nullptr ) {
25
2.17k
        throw std::exception();
26
2.17k
    }
27
28.3k
}
28
29
26.2k
ECCKey::~ECCKey() {
30
26.2k
    CF_NORET(wc_ecc_key_free(key));
31
26.2k
}
32
33
65.3k
ecc_key* ECCKey::GetPtr(void) {
34
65.3k
    uint8_t* x963 = nullptr;
35
65.3k
    ecc_key* newKey = nullptr;
36
37
65.3k
    bool exportToX963 = false;
38
65.3k
    try {
39
65.3k
        exportToX963 = ds.Get<bool>();
40
65.3k
    } catch ( ... ) { }
41
42
65.3k
    if ( exportToX963 == true ) {
43
5.51k
        CF_CHECK_NE(newKey = wc_ecc_key_new(nullptr), nullptr);
44
45
4.24k
        word32 outLen = 0;
46
47
4.24k
        bool compressed = false;
48
4.24k
        try { compressed  = ds.Get<bool>();} catch ( ... ) { }
49
50
7.99k
        WC_CHECK_EQ(wc_ecc_export_x963_ex(key, nullptr, &outLen, compressed), LENGTH_ONLY_E);
51
7.99k
        x963 = util::malloc(outLen);
52
7.99k
        WC_CHECK_EQ(wc_ecc_export_x963_ex(key, x963, &outLen, compressed), 0);;
53
54
        /* Get the curve id of the old key */
55
2.18k
        int curveID;
56
2.18k
        CF_CHECK_NE(curveID = wc_ecc_get_curve_id(key->idx), ECC_CURVE_INVALID);
57
58
2.18k
        const bool valid = wc_ecc_check_key(key) == 0;
59
60
2.18k
        haveAllocFailure = false;
61
2.18k
        if ( wc_ecc_import_x963_ex(x963, outLen, newKey, curveID) != 0 ) {
62
            /* Allowed to fail if either:
63
             *
64
             * - Compression is used and the input key is invalid
65
             * - An allocation failure occured during wc_ecc_import_x963_ex
66
             */
67
1.44k
            CF_ASSERT((compressed && !valid) || haveAllocFailure, "Cannot import X963-exported ECC key");
68
1.44k
            goto end;
69
1.44k
        }
70
71
745
        if ( compressed ) {
72
601
            CF_CHECK_TRUE(valid);
73
59
        }
74
75
203
        CF_NORET(wc_ecc_key_free(key));
76
203
        key = newKey;
77
203
        newKey = nullptr;
78
203
    }
79
80
65.3k
end:
81
65.3k
    util::free(x963);
82
65.3k
    CF_NORET(wc_ecc_key_free(newKey));
83
84
65.3k
    return key;
85
65.3k
}
86
87
8.50k
bool ECCKey::SetCurve(const Type& curveType) {
88
8.50k
    bool ret = false;
89
90
8.50k
#if !defined(WOLFSSL_SP_MATH)
91
8.50k
    bool useCustomCurve = false;
92
93
8.50k
    try {
94
8.50k
        useCustomCurve = ds.Get<uint8_t>();
95
8.50k
    } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
96
8.50k
    useCustomCurve = false;
97
98
8.50k
    if ( useCustomCurve == false )
99
8.50k
#endif
100
8.50k
    {
101
#if defined(CRYPTOFUZZ_WOLFCRYPT_DEBUG)
102
        std::cout << "Using native curve" << std::endl;
103
#endif
104
8.50k
        std::optional<int> curveID;
105
106
8.50k
        CF_CHECK_NE(curveID = wolfCrypt_detail::toCurveID(curveType), std::nullopt);
107
6.38k
        this->curveID = *curveID;
108
109
6.38k
        WC_CHECK_EQ(wc_ecc_set_curve(GetPtr(), 0, *curveID), 0);
110
6.38k
    }
111
0
#if !defined(WOLFSSL_SP_MATH)
112
0
    else {
113
 #if defined(CRYPTOFUZZ_WOLFCRYPT_DEBUG)
114
        std::cout << "Using custom curve" << std::endl;
115
 #endif
116
0
        const ecc_set_type* curveSpec;
117
0
        CF_CHECK_NE(curveSpec = GetCustomCurve(curveType.Get()), nullptr);
118
0
        WC_CHECK_EQ(wc_ecc_set_custom_curve(GetPtr(), curveSpec), 0);
119
0
        this->curveID = ECC_CURVE_CUSTOM;
120
0
    }
121
6.38k
#endif
122
123
6.38k
    ret = true;
124
125
8.50k
end:
126
8.50k
    return ret;
127
6.38k
}
128
129
8.89k
bool ECCKey::LoadPrivateKey(const component::Bignum& priv) {
130
8.89k
    bool ret = false;
131
8.89k
    std::optional<std::vector<uint8_t>> priv_bytes;
132
133
8.89k
    CF_CHECK_NE(priv_bytes = wolfCrypt_bignum::Bignum::ToBin(ds, priv), std::nullopt);
134
135
8.56k
    WC_CHECK_EQ(wc_ecc_import_private_key_ex(priv_bytes->data(), priv_bytes->size(), nullptr, 0, GetPtr(), *curveID), 0);
136
137
7.36k
    ret = true;
138
139
8.89k
end:
140
8.89k
    return ret;
141
7.36k
}
142
143
5.73k
std::optional<ECCPoint> ECCKey::MakePub(void) {
144
5.73k
    std::optional<ECCPoint> ret = std::nullopt;
145
146
5.73k
    ECCPoint pub(ds, *curveID);
147
5.73k
    WC_CHECK_EQ(wc_ecc_make_pub(GetPtr(), pub.GetPtr()), 0);
148
5.52k
    pub.SetInitialized();
149
150
5.52k
    return pub;
151
152
209
end:
153
209
    return ret;
154
5.73k
}
155
156
2.21k
bool ECCKey::SetRNG(void) {
157
2.21k
    bool ret = false;
158
159
2.21k
    WC_CHECK_EQ(wc_ecc_set_rng(GetPtr(), wolfCrypt_detail::GetRNG()), 0);
160
161
2.21k
    ret = true;
162
2.21k
end:
163
2.21k
    return ret;
164
2.21k
}
165
166
ECCPoint::ECCPoint(Datasource& ds, const int curveID) :
167
35.9k
    ds(ds),
168
35.9k
    curveID(curveID) {
169
35.9k
    if ( (point = wc_ecc_new_point_h(nullptr)) == nullptr ) {
170
1.08k
        throw std::exception();
171
1.08k
    }
172
35.9k
}
173
174
/* Copy constructor */
175
ECCPoint::ECCPoint(const ECCPoint& other) :
176
5.44k
    ds(other.ds),
177
5.44k
    curveID(other.curveID),
178
5.44k
    locked(other.locked),
179
5.44k
    initialized(other.initialized)
180
5.44k
{
181
5.44k
    if ( (point = wc_ecc_new_point_h(nullptr)) == nullptr ) {
182
11
        throw std::exception();
183
11
    }
184
185
5.43k
    if ( wc_ecc_copy_point(other.point, point) != 0 ) {
186
0
        CF_NORET(wc_ecc_del_point(point));
187
0
        throw std::exception();
188
0
    }
189
5.43k
}
190
191
40.3k
ECCPoint::~ECCPoint() {
192
40.3k
    CF_NORET(wc_ecc_del_point(point));
193
40.3k
}
194
195
50.5k
ecc_point* ECCPoint::GetPtr() {
196
50.5k
    uint8_t* out = nullptr;
197
50.5k
    ecc_point* newPoint = nullptr;
198
199
50.5k
    if ( locked == false && initialized == true ) {
200
9.82k
        bool exportToDER = false;
201
202
9.82k
        try {
203
9.82k
            exportToDER = ds.Get<bool>();
204
9.82k
        } catch ( ... ) { }
205
206
9.82k
        if ( exportToDER == true ) {
207
600
            bool compressed = false;
208
600
            try {
209
600
                compressed = ds.Get<bool>();
210
600
            } catch ( ... ) { }
211
212
600
            const int curveIdx = wc_ecc_get_curve_idx(curveID);
213
600
            CF_CHECK_NE(newPoint = wc_ecc_new_point_h(nullptr), nullptr);
214
215
460
            word32 outSz = 0xFFFF;
216
460
            try { outSz = ds.Get<word32>() & 0xFFFF; } catch ( ... ) { }
217
218
460
            out = util::malloc(outSz);
219
220
460
            if ( compressed == false ) {
221
270
                WC_CHECK_EQ(wc_ecc_export_point_der(curveIdx, point, out, &outSz), 0);
222
190
            } else {
223
190
                WC_CHECK_EQ(wc_ecc_export_point_der(curveIdx, point, out, &outSz), 0);
224
                //WC_CHECK_EQ(wc_ecc_export_point_der_compressed(curveIdx, point, out, &outSz), 0);
225
73
            }
226
227
196
            {
228
196
                haveAllocFailure = false;
229
196
                const bool success = wc_ecc_import_point_der(out, outSz, curveIdx, newPoint) == 0;
230
231
196
                if ( success ) {
232
                    /* Point imported. Replace old point with new point. */
233
234
186
                    CF_NORET(wc_ecc_del_point(point));
235
186
                    point = newPoint;
236
186
                    newPoint = nullptr;
237
186
                } else {
238
                    /* Failure */
239
240
10
                    if ( haveAllocFailure == false ) {
241
                        /* Failure is only acceptable if an allocation failure occured, crash otherwise */
242
0
                        CF_ASSERT(0, "Cannot import DER-exported ECC point");
243
0
                    }
244
10
                }
245
196
            }
246
196
        }
247
9.82k
    }
248
249
50.5k
end:
250
50.5k
    util::free(out);
251
50.5k
    CF_NORET(wc_ecc_del_point(newPoint));
252
253
50.5k
    return point;
254
50.5k
}
255
256
bool ECCPoint::Set(
257
        const component::BignumPair& xy,
258
        const uint64_t curveType,
259
        const bool pointCheck,
260
15.6k
        const bool mp_init_size_ok) {
261
15.6k
    bool ret = false;
262
263
15.6k
    bool projective = false;
264
15.6k
    try {
265
15.6k
        projective = ds.Get<bool>();
266
15.6k
    } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
267
268
15.6k
    wolfCrypt_bignum::Bignum x(ds, mp_init_size_ok);
269
15.6k
    wolfCrypt_bignum::Bignum y(ds, mp_init_size_ok);
270
15.6k
    wolfCrypt_bignum::Bignum z(ds, mp_init_size_ok);
271
272
15.6k
    if ( projective == false ) {
273
9.36k
        CF_CHECK_TRUE(x.Set(xy.first));
274
9.20k
        CF_CHECK_TRUE(y.Set(xy.second));
275
9.12k
        CF_CHECK_TRUE(z.Set("1"));
276
9.10k
    } else {
277
6.33k
        const auto proj = util::ToRandomProjective(
278
6.33k
                ds,
279
6.33k
                xy.first.ToTrimmedString(),
280
6.33k
                xy.second.ToTrimmedString(),
281
6.33k
                curveType);
282
6.33k
        CF_CHECK_TRUE(x.Set(proj[0]));
283
5.32k
        CF_CHECK_TRUE(y.Set(proj[1]));
284
4.78k
        CF_CHECK_TRUE(z.Set(proj[2]));
285
4.65k
    }
286
287
27.5k
    WC_CHECK_EQ(mp_copy(x.GetPtr(), point->x), MP_OKAY);
288
27.5k
    WC_CHECK_EQ(mp_copy(y.GetPtr(), point->y), MP_OKAY);
289
13.7k
    WC_CHECK_EQ(mp_copy(z.GetPtr(), point->z), MP_OKAY);
290
291
13.7k
    if ( pointCheck ) {
292
0
        CF_CHECK_TRUE(CurveCheck());
293
0
    }
294
295
13.7k
    SetInitialized();
296
297
13.7k
    ret = true;
298
299
15.2k
end:
300
15.2k
    return ret;
301
13.7k
}
302
303
9.44k
bool ECCPoint::ToProjective(wolfCrypt_bignum::Bignum& prime) {
304
9.44k
    bool ret = false;
305
306
9.44k
    wolfCrypt_bignum::Bignum mu(ds);
307
308
9.44k
    WC_CHECK_EQ(mp_montgomery_calc_normalization(mu.GetPtr(), prime.GetPtr()), MP_OKAY);
309
310
9.37k
    if ( mp_cmp_d(mu.GetPtr(), 1) != MP_EQ ) {
311
7.72k
        WC_CHECK_EQ(mp_mulmod(point->x, mu.GetPtr(), prime.GetPtr(), point->x), MP_OKAY);
312
7.56k
        WC_CHECK_EQ(mp_mulmod(point->y, mu.GetPtr(), prime.GetPtr(), point->y), MP_OKAY);
313
7.47k
        WC_CHECK_EQ(mp_mulmod(point->z, mu.GetPtr(), prime.GetPtr(), point->z), MP_OKAY);
314
7.42k
    }
315
316
    /* Lock so it isn't attempted to export/import the projective point in GetPtr(),
317
     * which will lead to incorrect results
318
     */
319
9.08k
    Lock();
320
321
9.08k
    ret = true;
322
323
9.44k
end:
324
9.44k
    return ret;
325
9.08k
}
326
327
11.2k
bool ECCPoint::CurveCheck(void) const {
328
11.2k
    const int curveIdx = wc_ecc_get_curve_idx(curveID);
329
11.2k
    return wc_ecc_point_is_on_curve(point, curveIdx) == 0;
330
11.2k
}
331
332
19.9k
void ECCPoint::Lock(void) {
333
19.9k
    locked = true;
334
19.9k
}
335
336
18.7k
void ECCPoint::SetInitialized(void) {
337
18.7k
    initialized = true;
338
18.7k
}
339
340
5.00k
std::optional<component::BignumPair> ECCPoint::ToBignumPair(void) {
341
5.00k
    std::optional<component::BignumPair> ret = std::nullopt;
342
343
5.00k
    wolfCrypt_bignum::Bignum pub_x(GetPtr()->x, ds);
344
345
    /* Pointer is stored in pub_x; lock to prevent UAF */
346
5.00k
    Lock();
347
348
5.00k
    wolfCrypt_bignum::Bignum pub_y(GetPtr()->y, ds);
349
350
5.00k
    std::optional<std::string> pub_x_str, pub_y_str;
351
5.00k
    CF_CHECK_NE(pub_x_str = pub_x.ToDecString(), std::nullopt);
352
4.99k
    CF_CHECK_NE(pub_y_str = pub_y.ToDecString(), std::nullopt);
353
354
4.98k
    ret = { *pub_x_str, *pub_y_str };
355
5.00k
end:
356
357
5.00k
    return ret;
358
4.98k
}
359
360
2.39k
int ECCPoint::Compare(ECCPoint& other) {
361
2.39k
    return wc_ecc_cmp_point(GetPtr(), other.GetPtr());
362
2.39k
}
363
364
2.52k
std::optional<bool> ECCPoint::IsNeg(ECCPoint& other, wolfCrypt_bignum::Bignum& prime) {
365
2.52k
    std::optional<bool> ret = std::nullopt;
366
367
2.52k
    wolfCrypt_bignum::Bignum neg(ds);
368
2.52k
    if ( mp_sub(prime.GetPtr(), other.GetPtr()->y, neg.GetPtr()) == MP_OKAY) {
369
2.46k
        ret = static_cast<bool>(mp_cmp(point->y, neg.GetPtr()) == MP_EQ);
370
2.46k
    }
371
372
2.52k
    return ret;
373
2.52k
}
374
375
4.89k
std::optional<component::ECC_PublicKey> OpECC_PrivateToPublic_Generic(operation::ECC_PrivateToPublic& op) {
376
4.89k
    std::optional<component::ECC_PublicKey> ret = std::nullopt;
377
4.89k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
378
4.89k
    wolfCrypt_detail::SetGlobalDs(&ds);
379
380
4.89k
    try {
381
4.89k
        ECCKey key(ds);
382
383
        /* Initialize */
384
4.89k
        {
385
4.89k
            CF_CHECK_EQ(key.SetCurve(op.curveType), true);
386
3.19k
            CF_CHECK_EQ(key.LoadPrivateKey(op.priv), true);
387
2.15k
        }
388
389
        /* Process/Finalize */
390
0
        {
391
2.15k
            auto pub = key.MakePub();
392
2.15k
            CF_CHECK_NE(pub, std::nullopt);
393
394
2.00k
            ret = pub->ToBignumPair();
395
2.00k
        }
396
2.00k
    } catch ( ... ) { }
397
398
4.89k
end:
399
400
4.89k
    wolfCrypt_detail::UnsetGlobalDs();
401
4.89k
    return ret;
402
4.89k
}
403
404
7.75k
std::optional<bool> OpECC_ValidatePubkey_Generic(operation::ECC_ValidatePubkey& op) {
405
7.75k
    std::optional<bool> ret = std::nullopt;
406
7.75k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
407
7.75k
    wolfCrypt_detail::SetGlobalDs(&ds);
408
409
7.75k
    std::optional<int> curveID;
410
411
7.75k
    try {
412
7.75k
        ECCKey key(ds);
413
7.75k
        {
414
7.75k
            const char* name = nullptr;
415
416
7.75k
            CF_CHECK_NE(curveID = wolfCrypt_detail::toCurveID(op.curveType), std::nullopt);
417
418
6.63k
            CF_CHECK_NE(name = wc_ecc_get_name(*curveID), nullptr);
419
420
6.61k
            WC_CHECK_EQ(wc_ecc_import_raw(
421
6.01k
                        key.GetPtr(),
422
6.01k
                        util::DecToHex(op.pub.first.ToTrimmedString()).c_str(),
423
6.01k
                        util::DecToHex(op.pub.second.ToTrimmedString()).c_str(),
424
6.01k
                        nullptr,
425
6.01k
                        name), 0);
426
6.01k
            haveAllocFailure = false;
427
6.01k
            ret = wc_ecc_check_key(key.GetPtr()) == 0;
428
6.01k
            if ( *ret == false && haveAllocFailure == true ) {
429
1.46k
                ret = std::nullopt;
430
1.46k
            }
431
6.01k
        }
432
6.01k
    } catch ( ... ) { }
433
434
7.75k
end:
435
436
7.75k
    wolfCrypt_detail::UnsetGlobalDs();
437
7.75k
    return ret;
438
7.75k
}
439
440
5.00k
std::optional<bool> OpECDSA_Verify_Generic(operation::ECDSA_Verify& op) {
441
    /* These are currently not supported by wc_Hash
442
     * See also ZD 16119
443
     */
444
5.00k
    switch ( op.digestType.Get() ) {
445
0
        case CF_DIGEST("MD2"):
446
0
        case CF_DIGEST("MD4"):
447
0
        case CF_DIGEST("BLAKE2B512"):
448
0
        case CF_DIGEST("BLAKE2S256"):
449
0
        case CF_DIGEST("SHA3-224"):
450
0
        case CF_DIGEST("SHA3-256"):
451
0
        case CF_DIGEST("SHA3-384"):
452
0
        case CF_DIGEST("SHA3-512"):
453
0
            return std::nullopt;
454
5.00k
    }
455
456
5.00k
    std::optional<bool> ret = std::nullopt;
457
5.00k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
458
5.00k
    wolfCrypt_detail::SetGlobalDs(&ds);
459
460
5.00k
    std::optional<int> curveID;
461
5.00k
    uint8_t* sig = nullptr;
462
5.00k
    uint8_t* hash = nullptr;
463
5.00k
    word32 sigSz = ECC_MAX_SIG_SIZE;
464
5.00k
    int verify;
465
466
5.00k
    bool ctIsEmpty = false;
467
5.00k
    bool randomSigSz = false;
468
5.00k
    bool hashNotFound = false;
469
470
5.00k
    {
471
5.00k
        try {
472
5.00k
            randomSigSz = ds.Get<bool>();
473
5.00k
            if ( randomSigSz ) {
474
1.07k
                sigSz = ds.Get<uint8_t>();
475
1.07k
            }
476
5.00k
        } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
477
478
5.00k
        sig = util::malloc(sigSz);
479
5.00k
    }
480
481
5.00k
    try {
482
5.00k
        ECCKey key(ds);
483
5.00k
        {
484
5.00k
            const char* name = nullptr;
485
486
5.00k
            CF_CHECK_NE(curveID = wolfCrypt_detail::toCurveID(op.curveType), std::nullopt);
487
488
4.63k
            CF_CHECK_NE(name = wc_ecc_get_name(*curveID), nullptr);
489
490
4.61k
            haveAllocFailure = false;
491
4.61k
            ret = false;
492
493
4.61k
            WC_CHECK_EQ(wc_ecc_import_raw(
494
4.46k
                        key.GetPtr(),
495
4.46k
                        util::DecToHex(op.signature.pub.first.ToTrimmedString()).c_str(),
496
4.46k
                        util::DecToHex(op.signature.pub.second.ToTrimmedString()).c_str(),
497
4.46k
                        nullptr,
498
4.46k
                        name), 0);
499
4.46k
            WC_CHECK_EQ(wc_ecc_check_key(key.GetPtr()), 0);
500
4.01k
        }
501
502
4.01k
        WC_CHECK_EQ(wc_ecc_rs_to_sig(
503
3.64k
                    util::DecToHex(op.signature.signature.first.ToTrimmedString()).c_str(),
504
3.64k
                    util::DecToHex(op.signature.signature.second.ToTrimmedString()).c_str(),
505
3.64k
                    sig, &sigSz), 0);
506
507
3.64k
        if ( op.digestType.Get() == CF_DIGEST("NULL") ) {
508
3.23k
            const auto CT = op.cleartext.ECDSA_RandomPad(ds, op.curveType);
509
3.23k
            ctIsEmpty = CT.GetSize() == 0;
510
3.23k
            WC_CHECK_EQ(wc_ecc_verify_hash(sig, sigSz, CT.GetPtr(), CT.GetSize(), &verify, key.GetPtr()), 0);
511
2.64k
        } else {
512
413
            std::optional<wc_HashType> hashType;
513
413
            hashNotFound = true;
514
413
            CF_CHECK_NE(hashType = wolfCrypt_detail::toHashType(op.digestType), std::nullopt);
515
413
            hashNotFound = false;
516
517
413
            const auto hashSize = wc_HashGetDigestSize(*hashType);
518
413
            hash = util::malloc(hashSize);
519
520
413
            WC_CHECK_EQ(wc_Hash(*hashType, op.cleartext.GetPtr(), op.cleartext.GetSize(), hash, hashSize), 0);
521
522
413
            const auto CT = Buffer(hash, hashSize).ECDSA_RandomPad(ds, op.curveType);
523
413
            ctIsEmpty = CT.GetSize() == 0;
524
413
            WC_CHECK_EQ(wc_ecc_verify_hash(sig, sigSz, CT.GetPtr(), CT.GetSize(), &verify, key.GetPtr()), 0);
525
413
        }
526
527
3.06k
        ret = verify ? true : false;
528
3.06k
    } catch ( ... ) { }
529
530
5.00k
end:
531
532
5.00k
    util::free(sig);
533
5.00k
    util::free(hash);
534
535
5.00k
    wolfCrypt_detail::UnsetGlobalDs();
536
537
5.00k
    if ( ret && *ret == false ) {
538
1.77k
        if ( haveAllocFailure ) {
539
439
            ret = std::nullopt;
540
1.33k
        } else if ( randomSigSz ) {
541
297
            ret = std::nullopt;
542
1.03k
        } else if ( ctIsEmpty ) {
543
            /* wolfCrypt ECDSA verification will fail if the input msg is empty */
544
62
            ret = std::nullopt;
545
972
        } else if ( hashNotFound ) {
546
0
            ret = std::nullopt;
547
972
        } else if ( op.digestType.Is(CF_DIGEST("NULL")) && op.cleartext.IsZero() ) {
548
180
            ret = std::nullopt;
549
180
        }
550
1.77k
    }
551
552
5.00k
    return ret;
553
5.00k
}
554
555
568
std::optional<component::ECCSI_Signature> OpECCSI_Sign(operation::ECCSI_Sign& op) {
556
#if !defined(WOLFCRYPT_HAVE_ECCSI)
557
    (void)op;
558
    return std::nullopt;
559
#else
560
568
    std::optional<component::ECCSI_Signature> ret = std::nullopt;
561
562
568
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
563
568
    wolfCrypt_detail::SetGlobalDs(&ds);
564
565
568
    uint8_t hashSz = WC_MAX_DIGEST_SIZE;
566
568
    static_assert(WC_MAX_DIGEST_SIZE < 256);
567
568
568
    uint8_t* hash = util::malloc(hashSz);
569
570
568
    uint8_t* encoded = nullptr;
571
568
    EccsiKey eccsi;
572
568
    bool eccsi_initialized = false;
573
574
568
    try {
575
568
        int size;
576
568
        std::optional<component::BignumPair> pubbn = std::nullopt;
577
578
568
        {
579
            /* Private to public */
580
568
            ECCKey key(ds);
581
568
            CF_CHECK_EQ(key.SetCurve(op.curveType), true);
582
414
            CF_CHECK_GT(size = key.GetPtr()->dp->size, 0);
583
414
            CF_CHECK_EQ(key.LoadPrivateKey(op.priv), true);
584
321
            auto pub = key.MakePub();
585
321
            CF_CHECK_NE(pub, std::nullopt);
586
289
            CF_CHECK_NE(pubbn = pub->ToBignumPair(), std::nullopt);
587
588
            /* Encode private and public */
589
288
            encoded = util::malloc(size * 3);
590
591
288
            WC_CHECK_EQ(mp_to_unsigned_bin_len(key.GetPtr()->k, encoded, size), 0);
592
243
            WC_CHECK_EQ(mp_to_unsigned_bin_len(pub->GetPtr()->x, encoded + size, size), 0);
593
243
            WC_CHECK_EQ(mp_to_unsigned_bin_len(pub->GetPtr()->y, encoded + (size * 2), size), 0);
594
243
        }
595
596
0
        {
597
243
            std::optional<int> curveId;
598
243
            CF_CHECK_NE(curveId = toCurveID(op.curveType), std::nullopt);
599
600
243
            WC_CHECK_EQ(wc_InitEccsiKey_ex(&eccsi, size, *curveId, nullptr, -1), 0);
601
237
            eccsi_initialized = true;
602
603
237
            WC_CHECK_EQ(wc_ImportEccsiKey(&eccsi, encoded, size * 3), 0);
604
237
            {
605
237
                uint8_t sig[257]; /* XXX random size */
606
237
                word32 sigSz = sizeof(sig);
607
237
                ECCPoint pvt(ds, *curveId);
608
237
                wolfCrypt_bignum::Bignum ssk(ds);
609
237
                std::optional<wc_HashType> hashType;
610
237
                CF_CHECK_NE(hashType = toHashType(op.digestType), std::nullopt);
611
121
                WC_CHECK_EQ(wc_MakeEccsiPair(
612
121
                            &eccsi,
613
121
                            &rng,
614
121
                            *hashType,
615
121
                            op.id.GetPtr(),
616
121
                            op.id.GetSize(),
617
121
                            ssk.GetPtr(),
618
121
                            pvt.GetPtr()), 0);
619
121
                WC_CHECK_EQ(wc_HashEccsiId(
620
121
                            &eccsi,
621
121
                            *hashType,
622
121
                            op.id.GetPtr(),
623
121
                            op.id.GetSize(),
624
121
                            pvt.GetPtr(),
625
121
                            hash,
626
121
                            &hashSz), 0);
627
121
                WC_CHECK_EQ(wc_SetEccsiHash(&eccsi, hash, hashSz), 0);
628
121
                WC_CHECK_EQ(wc_SetEccsiPair(&eccsi, ssk.GetPtr(), pvt.GetPtr()), 0);
629
121
                WC_CHECK_EQ(wc_SignEccsiHash(
630
121
                            &eccsi,
631
121
                            &rng,
632
121
                            *hashType,
633
121
                            op.cleartext.GetPtr(),
634
121
                            op.cleartext.GetSize(),
635
121
                            sig, &sigSz), 0);
636
121
                CF_ASSERT(sigSz == static_cast<size_t>(size) * 4 + 1, "Unexpected signature size");
637
121
                {
638
121
                    wolfCrypt_bignum::Bignum r(ds), s(ds);
639
121
                    std::optional<std::string> r_str, s_str;
640
121
                    std::optional<component::BignumPair> pvtbn = std::nullopt;
641
642
121
                    WC_CHECK_EQ(mp_read_unsigned_bin(r.GetPtr(), sig, size), 0);
643
121
                    CF_CHECK_NE(r_str = r.ToDecString(), std::nullopt);
644
645
121
                    WC_CHECK_EQ(mp_read_unsigned_bin(s.GetPtr(), sig + size, size), 0);
646
121
                    CF_CHECK_NE(s_str = s.ToDecString(), std::nullopt);
647
648
121
                    CF_CHECK_NE(pvtbn = pvt.ToBignumPair(), std::nullopt);
649
650
121
                    ret = component::ECCSI_Signature{
651
121
                        component::BignumPair{*r_str, *s_str},
652
121
                        *pubbn,
653
121
                        *pvtbn};
654
121
                }
655
121
            }
656
121
        }
657
121
    } catch ( ... ) { }
658
659
568
end:
660
568
    if ( eccsi_initialized ) {
661
119
        CF_NORET(wc_FreeEccsiKey(&eccsi));
662
119
    }
663
568
    util::free(hash);
664
568
    util::free(encoded);
665
568
    wolfCrypt_detail::UnsetGlobalDs();
666
568
    return ret;
667
568
#endif
668
568
}
669
670
166
std::optional<bool> OpECCSI_Verify(operation::ECCSI_Verify& op) {
671
#if !defined(WOLFCRYPT_HAVE_ECCSI)
672
    (void)op;
673
    return std::nullopt;
674
#else
675
166
    std::optional<bool> ret = std::nullopt;
676
166
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
677
166
    wolfCrypt_detail::SetGlobalDs(&ds);
678
679
166
    uint8_t hashSz = WC_MAX_DIGEST_SIZE;
680
166
    static_assert(WC_MAX_DIGEST_SIZE < 256);
681
682
166
    EccsiKey eccsi;
683
684
166
    uint8_t* hash = util::malloc(hashSz);
685
166
    std::optional<wc_HashType> hashType;
686
166
    std::optional<int> curveId;
687
166
    std::vector<uint8_t> pub, sig;
688
166
    int verified;
689
166
    int size;
690
166
    bool eccsi_initialized = false;
691
692
166
    CF_CHECK_NE(curveId = toCurveID(op.curveType), std::nullopt);
693
68
    CF_CHECK_NE(hashType = toHashType(op.digestType), std::nullopt);
694
695
0
    haveAllocFailure = false;
696
0
    ret = false;
697
698
0
    try {
699
0
    {
700
0
        ECCKey key(ds);
701
0
        CF_CHECK_EQ(key.SetCurve(op.curveType), true);
702
0
        CF_CHECK_GT(size = key.GetPtr()->dp->size, 0);
703
0
    }
704
705
    /* Pub to binary */
706
0
    {
707
0
        const auto x = op.signature.pub.first.ToBin(size);
708
0
        CF_CHECK_NE(x, std::nullopt);
709
0
        pub.insert(pub.end(), x->begin(), x->end());
710
711
0
        const auto y = op.signature.pub.second.ToBin(size);
712
0
        CF_CHECK_NE(y, std::nullopt);
713
0
        pub.insert(pub.end(), y->begin(), y->end());
714
0
    }
715
716
    /* Sig to binary */
717
0
    {
718
0
        const auto r = op.signature.signature.first.ToBin(size);
719
0
        CF_CHECK_NE(r, std::nullopt);
720
0
        sig.insert(sig.end(), r->begin(), r->end());
721
722
0
        const auto s = op.signature.signature.second.ToBin(size);
723
0
        CF_CHECK_NE(s, std::nullopt);
724
0
        sig.insert(sig.end(), s->begin(), s->end());
725
726
0
        sig.push_back(0x04);
727
728
0
        const auto pvt_x = op.signature.pvt.first.ToBin(size);
729
0
        CF_CHECK_NE(pvt_x, std::nullopt);
730
0
        sig.insert(sig.end(), pvt_x->begin(), pvt_x->end());
731
732
0
        const auto pvt_y = op.signature.pvt.second.ToBin(size);
733
0
        CF_CHECK_NE(pvt_y, std::nullopt);
734
0
        sig.insert(sig.end(), pvt_y->begin(), pvt_y->end());
735
0
    }
736
737
0
    {
738
0
        ECCPoint pvt(ds, *curveId);
739
740
0
        WC_CHECK_EQ(wc_InitEccsiKey_ex(&eccsi, size, *curveId, nullptr, -1), 0);
741
0
        eccsi_initialized = true;
742
743
0
        WC_CHECK_EQ(wc_ImportEccsiPublicKey(&eccsi,
744
0
                    pub.data(), pub.size(), 0), 0);
745
0
        WC_CHECK_EQ(wc_DecodeEccsiPvtFromSig(&eccsi, sig.data(), sig.size(), pvt.GetPtr()), 0);
746
0
        WC_CHECK_EQ(wc_HashEccsiId(
747
0
                    &eccsi,
748
0
                    *hashType,
749
0
                    op.id.GetPtr(&ds), op.id.GetSize(),
750
0
                    pvt.GetPtr(),
751
0
                    hash,
752
0
                    &hashSz), 0);
753
0
        WC_CHECK_EQ(wc_SetEccsiHash(&eccsi, hash, hashSz), 0);
754
0
        WC_CHECK_EQ(wc_VerifyEccsiHash(
755
0
                    &eccsi,
756
0
                    *hashType,
757
0
                    op.cleartext.GetPtr(&ds), op.cleartext.GetSize(),
758
0
                    sig.data(), sig.size(),
759
0
                    &verified), 0);
760
0
        ret = verified == 1;
761
0
    }
762
0
    } catch ( ... ) { }
763
764
166
end:
765
166
    if ( eccsi_initialized ) {
766
0
        CF_NORET(wc_FreeEccsiKey(&eccsi));
767
0
    }
768
166
    util::free(hash);
769
166
    wolfCrypt_detail::UnsetGlobalDs();
770
166
    if ( ret && *ret == false ) {
771
0
        if ( haveAllocFailure ) {
772
0
            ret = std::nullopt;
773
0
        }
774
0
    }
775
166
    return ret;
776
0
#endif
777
0
}
778
779
5.13k
std::optional<component::ECDSA_Signature> OpECDSA_Sign_Generic(operation::ECDSA_Sign& op) {
780
5.13k
    std::optional<component::ECDSA_Signature> ret = std::nullopt;
781
5.13k
    if ( op.UseRandomNonce() == false && op.UseSpecifiedNonce() == false ) {
782
244
        return ret;
783
244
    }
784
785
4.89k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
786
4.89k
    wolfCrypt_detail::SetGlobalDs(&ds);
787
788
4.89k
    uint8_t* sig = nullptr;
789
4.89k
    word32 sigSz = ECC_MAX_SIG_SIZE;
790
4.89k
    Buffer CT;
791
4.89k
    uint8_t* hash = nullptr;
792
4.89k
    size_t hashSize = 0;
793
4.89k
    uint8_t* nonce_bytes = nullptr;
794
4.89k
    wolfCrypt_bignum::Bignum nonce(ds);
795
796
    /* Initialize r, s using mp_init(), not mp_init_size(),
797
     * as the latter is not compatible with DecodeECC_DSA_Sig().
798
     *
799
     * See ZD 15728.
800
     */
801
4.89k
    wolfCrypt_bignum::Bignum r(ds, false), s(ds, false);
802
803
4.89k
    CF_CHECK_NE(op.priv.ToTrimmedString(), "0");
804
805
4.74k
    {
806
4.74k
        try {
807
4.74k
            sigSz = ds.Get<uint8_t>();
808
4.74k
        } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
809
810
4.74k
        sig = util::malloc(sigSz);
811
4.74k
    }
812
813
4.74k
    try {
814
4.74k
        ECCKey key(ds);
815
4.74k
        CF_CHECK_EQ(key.SetCurve(op.curveType), true);
816
4.33k
        CF_CHECK_EQ(key.LoadPrivateKey(op.priv), true);
817
4.16k
        key.GetPtr()->type = ECC_PRIVATEKEY_ONLY;
818
819
4.16k
        if ( op.UseSpecifiedNonce() == true ) {
820
1.55k
            CF_CHECK_EQ(nonce.Set(op.nonce.ToString(ds)), true);
821
822
1.51k
            const size_t nonce_bytes_size = mp_unsigned_bin_size(nonce.GetPtr());
823
824
            /* Convert nonce to byte array */
825
1.51k
            nonce_bytes = util::malloc(nonce_bytes_size);
826
1.51k
            CF_CHECK_EQ(mp_to_unsigned_bin(nonce.GetPtr(), nonce_bytes), 0);
827
828
            /* Set nonce */
829
1.47k
            WC_CHECK_EQ(wc_ecc_sign_set_k(nonce_bytes, nonce_bytes_size, key.GetPtr()), 0);
830
1.33k
        }
831
832
3.94k
        auto pub = key.MakePub();
833
3.94k
        CF_CHECK_NE(pub, std::nullopt);
834
835
3.90k
        if ( op.digestType.Get() == CF_DIGEST("NULL") ) {
836
3.66k
            CT = op.cleartext.ECDSA_RandomPad(ds, op.curveType);
837
3.66k
        } else {
838
240
            std::optional<wc_HashType> hashType;
839
240
            CF_CHECK_NE(hashType = wolfCrypt_detail::toHashType(op.digestType), std::nullopt);
840
841
240
            hashSize = wc_HashGetDigestSize(*hashType);
842
240
            hash = util::malloc(hashSize);
843
844
240
            WC_CHECK_EQ(wc_Hash(*hashType, op.cleartext.GetPtr(), op.cleartext.GetSize(), hash, hashSize), 0);
845
846
240
            CT = Buffer(hash, hashSize).ECDSA_RandomPad(ds, op.curveType);
847
240
        }
848
849
        /* Sign */
850
6.81k
        WC_CHECK_EQ(wc_ecc_sign_hash(CT.GetPtr(), CT.GetSize(), sig, &sigSz, wolfCrypt_detail::GetRNG(), key.GetPtr()), 0);
851
852
        /* Verify */
853
6.81k
        {
854
6.81k
            int verify;
855
6.81k
            haveAllocFailure = false;
856
6.81k
            if ( wc_ecc_verify_hash(sig, sigSz, CT.GetPtr(), CT.GetSize(), &verify, key.GetPtr()) == 0 && haveAllocFailure == false ) {
857
2.64k
                CF_ASSERT(verify, "Cannot verify generated signature");
858
2.64k
            }
859
6.81k
        }
860
861
2.90k
        CF_CHECK_EQ(DecodeECC_DSA_Sig(sig, sigSz, r.GetPtr(), s.GetPtr()), 0);
862
2.90k
        {
863
2.90k
            std::optional<std::string> r_str, s_str;
864
865
2.90k
            if ( op.curveType.Get() == CF_ECC_CURVE("secp256k1") ) {
866
118
                wolfCrypt_bignum::Bignum SMax(ds);
867
118
                CF_CHECK_EQ(SMax.Set("57896044618658097711785492504343953926418782139537452191302581570759080747168"), true);
868
117
                if ( mp_cmp(s.GetPtr(), SMax.GetPtr()) == 1 ) {
869
56
                    wolfCrypt_bignum::Bignum SSub(ds);
870
56
                    CF_CHECK_EQ(SSub.Set("115792089237316195423570985008687907852837564279074904382605163141518161494337"), true);
871
56
                    CF_CHECK_EQ(mp_sub(SSub.GetPtr(), s.GetPtr(), s.GetPtr()), 0);
872
56
                }
873
2.78k
            } else if ( op.curveType.Get() == CF_ECC_CURVE("secp256r1") ) {
874
500
                wolfCrypt_bignum::Bignum SMax(ds);
875
500
                CF_CHECK_EQ(SMax.Set("57896044605178124381348723474703786764998477612067880171211129530534256022184"), true);
876
499
                if ( mp_cmp(s.GetPtr(), SMax.GetPtr()) == 1 ) {
877
244
                    wolfCrypt_bignum::Bignum SSub(ds);
878
244
                    CF_CHECK_EQ(SSub.Set("115792089210356248762697446949407573529996955224135760342422259061068512044369"), true);
879
243
                    CF_CHECK_EQ(mp_sub(SSub.GetPtr(), s.GetPtr(), s.GetPtr()), 0);
880
243
                }
881
499
            }
882
883
2.90k
            CF_CHECK_NE(r_str = r.ToDecString(), std::nullopt);
884
2.89k
            CF_CHECK_NE(s_str = s.ToDecString(), std::nullopt);
885
886
2.89k
            const auto pub2 = pub->ToBignumPair();
887
2.89k
            CF_CHECK_NE(pub2, std::nullopt);
888
889
2.89k
            ret = component::ECDSA_Signature({*r_str, *s_str}, *pub2);
890
2.89k
        }
891
2.89k
    } catch ( ... ) { }
892
4.89k
end:
893
894
4.89k
    util::free(sig);
895
4.89k
    util::free(hash);
896
4.89k
    util::free(nonce_bytes);
897
898
4.89k
    wolfCrypt_detail::UnsetGlobalDs();
899
900
4.89k
    return ret;
901
4.74k
}
902
903
1.29k
std::optional<component::Ciphertext> OpECIES_Encrypt_Generic(operation::ECIES_Encrypt& op) {
904
1.29k
    std::optional<component::Ciphertext> ret = std::nullopt;
905
#if !defined(HAVE_ECC_ENCRYPT)
906
    (void)op;
907
#else
908
1.29k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
909
1.29k
    wolfCrypt_detail::SetGlobalDs(&ds);
910
1.29k
    uint8_t* out = nullptr;
911
912
1.29k
    CF_CHECK_TRUE(op.cipherType.Is(CF_CIPHER("AES_128_CBC")));
913
953
    CF_CHECK_EQ(op.iv, std::nullopt);
914
915
908
    try {
916
908
        ECCKey priv(ds), pub(ds);
917
908
        word32 outSz = ds.Get<uint32_t>() % 0xFFFFFF;
918
919
        /* Initialize private key */
920
908
        {
921
908
            CF_CHECK_TRUE(priv.SetCurve(op.curveType));
922
830
            CF_CHECK_TRUE(priv.LoadPrivateKey(op.priv));
923
762
            CF_CHECK_TRUE(priv.SetRNG());
924
762
        }
925
926
        /* Initialize public key */
927
0
        {
928
762
            std::optional<int> curveID;
929
762
            const char* name = nullptr;
930
931
762
            CF_CHECK_NE(curveID = wolfCrypt_detail::toCurveID(op.curveType), std::nullopt);
932
933
762
            CF_CHECK_NE(name = wc_ecc_get_name(*curveID), nullptr);
934
935
762
            WC_CHECK_EQ(wc_ecc_import_raw(
936
731
                        pub.GetPtr(),
937
731
                        util::DecToHex(op.pub.first.ToTrimmedString()).c_str(),
938
731
                        util::DecToHex(op.pub.second.ToTrimmedString()).c_str(),
939
731
                        nullptr,
940
731
                        name), 0);
941
942
731
            CF_CHECK_TRUE(pub.SetRNG());
943
731
        }
944
945
0
        out = util::malloc(outSz);
946
947
731
        WC_CHECK_EQ(wc_ecc_encrypt(priv.GetPtr(), pub.GetPtr(), op.cleartext.GetPtr(), op.cleartext.GetSize(), out, &outSz, nullptr), 0);
948
949
584
        ret = component::Ciphertext(Buffer(out, outSz));
950
584
    } catch ( ... ) { }
951
952
1.29k
end:
953
1.29k
    util::free(out);
954
955
1.29k
    wolfCrypt_detail::UnsetGlobalDs();
956
1.29k
#endif
957
1.29k
    return ret;
958
908
}
959
960
1.33k
std::optional<component::Cleartext> OpECIES_Decrypt_Generic(operation::ECIES_Decrypt& op) {
961
1.33k
    std::optional<component::Cleartext> ret = std::nullopt;
962
#if !defined(HAVE_ECC_ENCRYPT)
963
    (void)op;
964
#else
965
1.33k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
966
1.33k
    wolfCrypt_detail::SetGlobalDs(&ds);
967
1.33k
    uint8_t* out = nullptr;
968
969
1.33k
    CF_CHECK_TRUE(op.cipherType.Is(CF_CIPHER("AES_128_CBC")));
970
996
    CF_CHECK_EQ(op.iv, std::nullopt);
971
972
977
    try {
973
977
        ECCKey priv(ds), pub(ds);
974
977
        word32 outSz = ds.Get<uint32_t>() % 0xFFFFFF;
975
976
        /* Initialize private key */
977
977
        {
978
977
            CF_CHECK_TRUE(priv.SetCurve(op.curveType));
979
902
            CF_CHECK_TRUE(priv.LoadPrivateKey(op.priv));
980
839
            CF_CHECK_TRUE(priv.SetRNG());
981
839
        }
982
983
        /* Initialize public key */
984
0
        {
985
839
            std::optional<int> curveID;
986
839
            const char* name = nullptr;
987
988
839
            CF_CHECK_NE(curveID = wolfCrypt_detail::toCurveID(op.curveType), std::nullopt);
989
990
839
            CF_CHECK_NE(name = wc_ecc_get_name(*curveID), nullptr);
991
992
839
            WC_CHECK_EQ(wc_ecc_import_raw(
993
784
                        pub.GetPtr(),
994
784
                        util::DecToHex(op.pub.first.ToTrimmedString()).c_str(),
995
784
                        util::DecToHex(op.pub.second.ToTrimmedString()).c_str(),
996
784
                        nullptr,
997
784
                        name), 0);
998
999
784
            CF_CHECK_TRUE(pub.SetRNG());
1000
784
        }
1001
1002
0
        out = util::malloc(outSz);
1003
1004
784
        WC_CHECK_EQ(wc_ecc_decrypt(priv.GetPtr(), pub.GetPtr(), op.ciphertext.GetPtr(), op.ciphertext.GetSize(), out, &outSz, nullptr), 0);
1005
1006
388
        ret = component::Cleartext(Buffer(out, outSz));
1007
388
    } catch ( ... ) { }
1008
1009
1.33k
end:
1010
1.33k
    util::free(out);
1011
1012
1.33k
    wolfCrypt_detail::UnsetGlobalDs();
1013
1.33k
#endif
1014
1.33k
    return ret;
1015
977
}
1016
1017
1.07k
std::optional<component::Secret> OpECDH_Derive(operation::ECDH_Derive& op) {
1018
1.07k
    std::optional<component::Secret> ret = std::nullopt;
1019
1.07k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1020
1.07k
    wolfCrypt_detail::SetGlobalDs(&ds);
1021
1022
    /* try/catch because ECCKey constructor may throw if allocation fails */
1023
1.07k
    try {
1024
        /* TODO dynamic size */
1025
1.07k
        uint8_t out[1024];
1026
1.07k
        word32 outlen = sizeof(out);
1027
1.07k
        ECCKey priv(ds), pub(ds);
1028
1029
        /* Initialize private key */
1030
1.07k
        {
1031
1.07k
            CF_CHECK_TRUE(priv.SetCurve(op.curveType));
1032
845
            CF_CHECK_TRUE(priv.LoadPrivateKey(op.priv));
1033
751
            CF_CHECK_TRUE(priv.SetRNG());
1034
751
            WC_CHECK_EQ(
1035
751
                    wc_ecc_set_flags(priv.GetPtr(), WC_ECC_FLAG_COFACTOR), 0);
1036
751
        }
1037
1038
        /* Initialize public key */
1039
0
        {
1040
751
            std::optional<int> curveID;
1041
751
            const char* name = nullptr;
1042
1043
751
            CF_CHECK_NE(curveID = wolfCrypt_detail::toCurveID(op.curveType), std::nullopt);
1044
1045
751
            CF_CHECK_NE(name = wc_ecc_get_name(*curveID), nullptr);
1046
1047
751
            WC_CHECK_EQ(wc_ecc_import_raw(
1048
593
                        pub.GetPtr(),
1049
593
                        util::DecToHex(op.pub.first.ToTrimmedString()).c_str(),
1050
593
                        util::DecToHex(op.pub.second.ToTrimmedString()).c_str(),
1051
593
                        nullptr,
1052
593
                        name), 0);
1053
593
            WC_CHECK_EQ(wc_ecc_check_key(pub.GetPtr()), 0);
1054
431
        }
1055
1056
431
        WC_CHECK_EQ(wc_ecc_shared_secret(priv.GetPtr(), pub.GetPtr(), out, &outlen), 0);
1057
1058
415
        ret = component::Secret(Buffer(out, outlen));
1059
415
    } catch ( ... ) { }
1060
1061
1.07k
end:
1062
1.07k
    wolfCrypt_detail::UnsetGlobalDs();
1063
1064
1.07k
    return ret;
1065
1.07k
}
1066
1067
2.51k
std::optional<component::ECC_Point> OpECC_Point_Add(operation::ECC_Point_Add& op) {
1068
2.51k
    std::optional<component::ECC_Point> ret = std::nullopt;
1069
2.51k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1070
2.51k
    wolfCrypt_detail::SetGlobalDs(&ds);
1071
1072
2.51k
    std::optional<int> curveID;
1073
2.51k
    int curveIdx;
1074
2.51k
    const ecc_set_type* curve = nullptr;
1075
2.51k
    bool failed = false;
1076
2.51k
    bool valid = false;
1077
2.51k
    std::optional<bool> is_neg = std::nullopt;
1078
1079
2.51k
    CF_CHECK_NE(curveID = wolfCrypt_detail::toCurveID(op.curveType), std::nullopt);
1080
1081
2.25k
    CF_CHECK_NE(curveIdx = wc_ecc_get_curve_idx(*curveID), ECC_CURVE_INVALID);
1082
2.25k
    CF_CHECK_NE(curve = wc_ecc_get_curve_params(curveIdx), nullptr);
1083
1084
    /* try/catch because ECCPoint constructor may throw if allocation fails */
1085
2.25k
    try {
1086
2.25k
        ECCPoint res(ds, *curveID), a(ds, *curveID), b(ds, *curveID);
1087
2.25k
        wolfCrypt_bignum::Bignum Af(ds), prime(ds), mu(ds);
1088
2.25k
        mp_digit mp;
1089
1090
        /* Set points */
1091
2.25k
        CF_CHECK_TRUE(a.Set(op.a, op.curveType.Get()));
1092
2.10k
        CF_CHECK_TRUE(b.Set(op.b, op.curveType.Get()));
1093
1094
1.92k
        valid = a.CurveCheck() && b.CurveCheck();
1095
1096
        /* Retrieve curve parameter */
1097
1.92k
        CF_CHECK_EQ(Af.Set(util::HexToDec(curve->Af)), true);
1098
1.86k
        CF_CHECK_EQ(prime.Set(util::HexToDec(curve->prime)), true);
1099
1100
1.81k
        is_neg = a.IsNeg(b, prime);
1101
1102
1.81k
        CF_CHECK_TRUE(a.ToProjective(prime));
1103
1.73k
        CF_CHECK_TRUE(b.ToProjective(prime));
1104
1105
1.70k
        WC_CHECK_EQ(mp_montgomery_setup(prime.GetPtr(), &mp), MP_OKAY);
1106
1107
#if defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_PUBLIC_ECC_ADD_DBL)
1108
        goto end;
1109
#else
1110
1.70k
        {
1111
1.70k
            bool dbl = false;
1112
1.70k
            bool safe = false;
1113
1114
1.70k
            if ( a.Compare(b) == MP_EQ ) {
1115
292
                try { dbl = ds.Get<bool>(); } catch ( ... ) { }
1116
292
            }
1117
1118
1.70k
#if !(defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_ECC_ADD_DBL))
1119
1.70k
            try { safe = ds.Get<bool>(); } catch ( ... ) { }
1120
1.70k
#endif
1121
1122
1.70k
            if ( safe ) {
1123
#if defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_ECC_ADD_DBL)
1124
                CF_UNREACHABLE();
1125
#else
1126
59
                int infinity;
1127
1128
59
                if ( dbl == true ) {
1129
15
                    failed = true;
1130
15
                    haveAllocFailure = false;
1131
15
                    WC_CHECK_EQ(ecc_projective_dbl_point_safe(a.GetPtr(), res.GetPtr(), Af.GetPtr(), prime.GetPtr(), mp), 0);
1132
5
                    failed = false;
1133
44
                } else {
1134
44
                    failed = true;
1135
44
                    haveAllocFailure = false;
1136
44
                    WC_CHECK_EQ(ecc_projective_add_point_safe(a.GetPtr(), b.GetPtr(), res.GetPtr(), Af.GetPtr(), prime.GetPtr(), mp, &infinity), 0);
1137
14
                    failed = false;
1138
14
                }
1139
59
#endif
1140
1.29k
            } else {
1141
1.29k
                if ( dbl == true ) {
1142
11
                    failed = true;
1143
11
                    haveAllocFailure = false;
1144
11
                    WC_CHECK_EQ(ecc_projective_dbl_point(a.GetPtr(), res.GetPtr(), Af.GetPtr(), prime.GetPtr(), mp), 0);
1145
5
                    failed = false;
1146
1.28k
                } else {
1147
1.28k
                    failed = true;
1148
1.28k
                    haveAllocFailure = false;
1149
1.28k
                    WC_CHECK_EQ(ecc_projective_add_point(a.GetPtr(), b.GetPtr(), res.GetPtr(), Af.GetPtr(), prime.GetPtr(), mp), 0);
1150
1.09k
                    failed = false;
1151
1152
                    /* Do not return result if inputs are negations of the same point */
1153
1.09k
                    CF_CHECK_NE(is_neg, std::nullopt);
1154
1.06k
                    CF_CHECK_FALSE(*is_neg);
1155
1.00k
                }
1156
1.29k
            }
1157
1158
            /* Lock to prevent exporting the projective point */
1159
1.02k
            res.Lock();
1160
1.02k
        }
1161
0
#endif
1162
1163
        /* To affine */
1164
1.02k
        WC_CHECK_EQ(ecc_map(res.GetPtr(), prime.GetPtr(), mp), MP_OKAY);
1165
1166
        /* Only return the result if the input points are valid */
1167
1.01k
        CF_CHECK_TRUE(valid);
1168
1169
        /* Only return the result if the output point is valid */
1170
185
        CF_CHECK_TRUE(res.CurveCheck());
1171
1172
172
        ret = res.ToBignumPair();
1173
348
    } catch ( ... ) { }
1174
1175
2.51k
end:
1176
2.51k
    wolfCrypt_detail::UnsetGlobalDs();
1177
1178
2.51k
    if ( valid ) {
1179
242
        if ( !haveAllocFailure ) {
1180
229
            if ( is_neg && !*is_neg ) {
1181
179
                CF_ASSERT(!failed, "Point adding failed");
1182
179
            }
1183
229
        }
1184
242
    }
1185
1186
2.51k
    return ret;
1187
2.51k
}
1188
1189
4.04k
std::optional<component::ECC_Point> OpECC_Point_Mul(operation::ECC_Point_Mul& op) {
1190
4.04k
    std::optional<component::ECC_Point> ret = std::nullopt;
1191
4.04k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1192
4.04k
    wolfCrypt_detail::SetGlobalDs(&ds);
1193
1194
4.04k
    std::optional<int> curveID;
1195
4.04k
    int curveIdx;
1196
4.04k
    const ecc_set_type* curve = nullptr;
1197
1198
4.04k
    CF_CHECK_NE(curveID = wolfCrypt_detail::toCurveID(op.curveType), std::nullopt);
1199
1200
3.80k
    CF_CHECK_NE(curveIdx = wc_ecc_get_curve_idx(*curveID), ECC_CURVE_INVALID);
1201
3.78k
    CF_CHECK_NE(curve = wc_ecc_get_curve_params(curveIdx), nullptr);
1202
1203
    /* try/catch because ECCPoint constructor may throw if allocation fails */
1204
3.78k
    try {
1205
3.78k
        ECCPoint res(ds, *curveID), a(ds, *curveID);
1206
3.78k
        wolfCrypt_bignum::Bignum b(ds), Af(ds), prime(ds);
1207
3.78k
        bool valid = false;
1208
1209
        /* Set point */
1210
3.78k
        CF_CHECK_TRUE(a.Set(op.a, op.curveType.Get()));
1211
3.43k
        valid = a.CurveCheck();
1212
1213
        /* Set multiplier */
1214
3.43k
        CF_CHECK_EQ(b.Set(op.b.ToString(ds)), true);
1215
1216
        /* Retrieve curve parameters */
1217
3.35k
        CF_CHECK_EQ(Af.Set(util::HexToDec(curve->Af)), true);
1218
3.29k
        CF_CHECK_EQ(prime.Set(util::HexToDec(curve->prime)), true);
1219
1220
        /* Multiply */
1221
3.22k
        WC_CHECK_EQ(wc_ecc_mulmod_ex(b.GetPtr(), a.GetPtr(), res.GetPtr(), Af.GetPtr(), prime.GetPtr(), 1, nullptr), 0);
1222
1223
        /* Only return the result if the input point is valid */
1224
2.70k
        CF_CHECK_TRUE(valid);
1225
1226
573
        ret = res.ToBignumPair();
1227
573
    } catch ( ... ) { }
1228
1229
4.04k
end:
1230
4.04k
    wolfCrypt_detail::UnsetGlobalDs();
1231
1232
4.04k
    return ret;
1233
3.78k
}
1234
1235
5.55k
std::optional<component::ECC_Point> OpECC_Point_Dbl(operation::ECC_Point_Dbl& op) {
1236
5.55k
    std::optional<component::ECC_Point> ret = std::nullopt;
1237
5.55k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1238
5.55k
    wolfCrypt_detail::SetGlobalDs(&ds);
1239
1240
5.55k
    std::optional<int> curveID;
1241
5.55k
    int curveIdx;
1242
5.55k
    const ecc_set_type* curve = nullptr;
1243
5.55k
    bool failed = false;
1244
5.55k
    bool valid = false;
1245
1246
5.55k
    CF_CHECK_NE(curveID = wolfCrypt_detail::toCurveID(op.curveType), std::nullopt);
1247
1248
5.29k
    CF_CHECK_NE(curveIdx = wc_ecc_get_curve_idx(*curveID), ECC_CURVE_INVALID);
1249
5.29k
    CF_CHECK_NE(curve = wc_ecc_get_curve_params(curveIdx), nullptr);
1250
1251
    /* try/catch because ECCPoint constructor may throw if allocation fails */
1252
5.29k
    try {
1253
5.29k
        ECCPoint res(ds, *curveID), a(ds, *curveID);
1254
5.29k
        wolfCrypt_bignum::Bignum Af(ds), prime(ds), mu(ds);
1255
5.29k
        mp_digit mp;
1256
1257
        /* Set points */
1258
5.29k
        CF_CHECK_TRUE(a.Set(op.a, op.curveType.Get()));
1259
1260
4.26k
        valid = a.CurveCheck();
1261
1262
        /* Retrieve curve parameter */
1263
4.26k
        CF_CHECK_EQ(Af.Set(util::HexToDec(curve->Af)), true);
1264
4.20k
        CF_CHECK_EQ(prime.Set(util::HexToDec(curve->prime)), true);
1265
1266
4.12k
        CF_CHECK_TRUE(a.ToProjective(prime));
1267
1268
3.93k
        WC_CHECK_EQ(mp_montgomery_setup(prime.GetPtr(), &mp), MP_OKAY);
1269
1270
#if defined(WOLFSSL_SP_MATH) && !defined(WOLFSSL_PUBLIC_ECC_ADD_DBL)
1271
        goto end;
1272
#else
1273
3.93k
        {
1274
3.93k
            bool safe = false;
1275
1276
3.93k
#if !(defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_ECC_ADD_DBL))
1277
3.93k
            try { safe = ds.Get<bool>(); } catch ( ... ) { }
1278
3.93k
#endif
1279
1280
3.93k
            if ( safe ) {
1281
#if defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_ECC_ADD_DBL)
1282
                CF_UNREACHABLE();
1283
#else
1284
67
                failed = true;
1285
67
                haveAllocFailure = false;
1286
67
                WC_CHECK_EQ(ecc_projective_dbl_point_safe(a.GetPtr(), res.GetPtr(), Af.GetPtr(), prime.GetPtr(), mp), 0);
1287
29
                failed = false;
1288
29
#endif
1289
3.31k
            } else {
1290
3.31k
                failed = true;
1291
3.31k
                haveAllocFailure = false;
1292
3.31k
                WC_CHECK_EQ(ecc_projective_dbl_point(a.GetPtr(), res.GetPtr(), Af.GetPtr(), prime.GetPtr(), mp), 0);
1293
3.09k
                failed = false;
1294
3.09k
            }
1295
1296
            /* Lock to prevent exporting the projective point */
1297
3.12k
            res.Lock();
1298
3.12k
        }
1299
0
#endif
1300
1301
        /* To affine */
1302
3.12k
        WC_CHECK_EQ(ecc_map(res.GetPtr(), prime.GetPtr(), mp), MP_OKAY);
1303
1304
        /* Only return the result if the input points are valid */
1305
3.04k
        CF_CHECK_TRUE(valid);
1306
1307
        /* Only return the result if the output point is valid */
1308
218
        CF_CHECK_TRUE(res.CurveCheck());
1309
1310
169
        ret = res.ToBignumPair();
1311
554
    } catch ( ... ) { }
1312
1313
5.55k
end:
1314
5.55k
    if ( valid ) {
1315
262
        if ( !haveAllocFailure ) {
1316
197
            CF_ASSERT(!failed, "Point doubling failed");
1317
197
        }
1318
262
    }
1319
1320
5.55k
    wolfCrypt_detail::UnsetGlobalDs();
1321
1322
5.55k
    return ret;
1323
5.55k
}
1324
1325
0
std::optional<bool> OpECC_Point_Cmp(operation::ECC_Point_Cmp& op) {
1326
#if 0
1327
    std::optional<bool> ret = std::nullopt;
1328
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1329
    wolfCrypt_detail::SetGlobalDs(&ds);
1330
1331
    std::optional<int> curveID;
1332
    int curveIdx;
1333
    const ecc_set_type* curve = nullptr;
1334
1335
    CF_CHECK_NE(curveID = wolfCrypt_detail::toCurveID(op.curveType), std::nullopt);
1336
1337
    CF_CHECK_NE(curveIdx = wc_ecc_get_curve_idx(*curveID), ECC_CURVE_INVALID);
1338
    CF_CHECK_NE(curve = wc_ecc_get_curve_params(curveIdx), nullptr);
1339
1340
    /* try/catch because ECCPoint constructor may throw if allocation fails */
1341
    try {
1342
        ECCPoint res(ds, *curveID), a(ds, *curveID), b(ds, *curveID);
1343
        wolfCrypt_bignum::Bignum Af(ds), prime(ds), mu(ds);
1344
        bool valid = false;
1345
1346
        /* Set points */
1347
        CF_CHECK_TRUE(a.Set(op.a, op.curveType.Get()));
1348
        CF_CHECK_TRUE(b.Set(op.b, op.curveType.Get()));
1349
1350
        valid = a.CurveCheck() && b.CurveCheck();
1351
        (void)valid;
1352
1353
        /* Retrieve curve parameter */
1354
        CF_CHECK_EQ(Af.Set(util::HexToDec(curve->Af)), true);
1355
        CF_CHECK_EQ(prime.Set(util::HexToDec(curve->prime)), true);
1356
1357
        CF_CHECK_TRUE(a.ToProjective(prime));
1358
        CF_CHECK_TRUE(b.ToProjective(prime));
1359
1360
        ret = wc_ecc_cmp_point(a.GetPtr(), b.GetPtr()) == MP_EQ;
1361
    } catch ( ... ) { }
1362
1363
end:
1364
    wolfCrypt_detail::UnsetGlobalDs();
1365
1366
    return ret;
1367
#else
1368
    /* ZD 15599 */
1369
0
    (void)op;
1370
0
    return std::nullopt;
1371
0
#endif
1372
0
}
1373
1374
} /* namespace wolfCrypt_detail */
1375
} /* namespace module */
1376
} /* namespace cryptofuzz */