Coverage Report

Created: 2026-07-22 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cryptofuzz-sp-math-all/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
12.1k
    ds(ds) {
24
12.1k
    if ( (key = wc_ecc_key_new(nullptr)) == nullptr ) {
25
1.05k
        throw std::exception();
26
1.05k
    }
27
12.1k
}
28
29
11.0k
ECCKey::~ECCKey() {
30
11.0k
    CF_NORET(wc_ecc_key_free(key));
31
11.0k
}
32
33
30.6k
ecc_key* ECCKey::GetPtr(void) {
34
30.6k
    uint8_t* x963 = nullptr;
35
30.6k
    ecc_key* newKey = nullptr;
36
37
30.6k
    bool exportToX963 = false;
38
30.6k
    try {
39
30.6k
        exportToX963 = ds.Get<bool>();
40
30.6k
    } catch ( ... ) { }
41
42
30.6k
    if ( exportToX963 == true ) {
43
2.23k
        CF_CHECK_NE(newKey = wc_ecc_key_new(nullptr), nullptr);
44
45
1.66k
        word32 outLen = 0;
46
47
1.66k
        bool compressed = false;
48
1.66k
        try { compressed  = ds.Get<bool>();} catch ( ... ) { }
49
50
3.08k
        WC_CHECK_EQ(wc_ecc_export_x963_ex(key, nullptr, &outLen, compressed), LENGTH_ONLY_E);
51
3.08k
        x963 = util::malloc(outLen);
52
3.08k
        WC_CHECK_EQ(wc_ecc_export_x963_ex(key, x963, &outLen, compressed), 0);;
53
54
        /* Get the curve id of the old key */
55
641
        int curveID;
56
641
        CF_CHECK_NE(curveID = wc_ecc_get_curve_id(key->idx), ECC_CURVE_INVALID);
57
58
641
        const bool valid = wc_ecc_check_key(key) == 0;
59
60
641
        haveAllocFailure = false;
61
641
        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
392
            CF_ASSERT((compressed && !valid) || haveAllocFailure, "Cannot import X963-exported ECC key");
68
392
            goto end;
69
392
        }
70
71
249
        if ( compressed ) {
72
167
            CF_CHECK_TRUE(valid);
73
31
        }
74
75
113
        CF_NORET(wc_ecc_key_free(key));
76
113
        key = newKey;
77
113
        newKey = nullptr;
78
113
    }
79
80
30.6k
end:
81
30.6k
    util::free(x963);
82
30.6k
    CF_NORET(wc_ecc_key_free(newKey));
83
84
30.6k
    return key;
85
30.6k
}
86
87
1.54k
bool ECCKey::SetCurve(const Type& curveType) {
88
1.54k
    bool ret = false;
89
90
1.54k
#if !defined(WOLFSSL_SP_MATH)
91
1.54k
    bool useCustomCurve = false;
92
93
1.54k
    try {
94
1.54k
        useCustomCurve = ds.Get<uint8_t>();
95
1.54k
    } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
96
1.54k
    useCustomCurve = false;
97
98
1.54k
    if ( useCustomCurve == false )
99
1.54k
#endif
100
1.54k
    {
101
#if defined(CRYPTOFUZZ_WOLFCRYPT_DEBUG)
102
        std::cout << "Using native curve" << std::endl;
103
#endif
104
1.54k
        std::optional<int> curveID;
105
106
1.54k
        CF_CHECK_NE(curveID = wolfCrypt_detail::toCurveID(curveType), std::nullopt);
107
1.10k
        this->curveID = *curveID;
108
109
1.10k
        WC_CHECK_EQ(wc_ecc_set_curve(GetPtr(), 0, *curveID), 0);
110
1.10k
    }
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
1.10k
#endif
122
123
1.10k
    ret = true;
124
125
1.54k
end:
126
1.54k
    return ret;
127
1.10k
}
128
129
3.81k
bool ECCKey::LoadPrivateKey(const component::Bignum& priv) {
130
3.81k
    bool ret = false;
131
3.81k
    std::optional<std::vector<uint8_t>> priv_bytes;
132
133
3.81k
    CF_CHECK_NE(priv_bytes = wolfCrypt_bignum::Bignum::ToBin(ds, priv), std::nullopt);
134
135
3.68k
    WC_CHECK_EQ(wc_ecc_import_private_key_ex(priv_bytes->data(), priv_bytes->size(), nullptr, 0, GetPtr(), *curveID), 0);
136
137
3.47k
    ret = true;
138
139
3.81k
end:
140
3.81k
    return ret;
141
3.47k
}
142
143
2.79k
std::optional<ECCPoint> ECCKey::MakePub(void) {
144
2.79k
    std::optional<ECCPoint> ret = std::nullopt;
145
146
2.79k
    ECCPoint pub(ds, *curveID);
147
2.79k
    WC_CHECK_EQ(wc_ecc_make_pub(GetPtr(), pub.GetPtr()), 0);
148
2.66k
    pub.SetInitialized();
149
150
2.66k
    return pub;
151
152
130
end:
153
130
    return ret;
154
2.79k
}
155
156
886
bool ECCKey::SetRNG(void) {
157
886
    bool ret = false;
158
159
886
    WC_CHECK_EQ(wc_ecc_set_rng(GetPtr(), wolfCrypt_detail::GetRNG()), 0);
160
161
886
    ret = true;
162
886
end:
163
886
    return ret;
164
886
}
165
166
ECCPoint::ECCPoint(Datasource& ds, const int curveID) :
167
16.3k
    ds(ds),
168
16.3k
    curveID(curveID) {
169
16.3k
    if ( (point = wc_ecc_new_point_h(nullptr)) == nullptr ) {
170
522
        throw std::exception();
171
522
    }
172
16.3k
}
173
174
/* Copy constructor */
175
ECCPoint::ECCPoint(const ECCPoint& other) :
176
2.64k
    ds(other.ds),
177
2.64k
    curveID(other.curveID),
178
2.64k
    locked(other.locked),
179
2.64k
    initialized(other.initialized)
180
2.64k
{
181
2.64k
    if ( (point = wc_ecc_new_point_h(nullptr)) == nullptr ) {
182
7
        throw std::exception();
183
7
    }
184
185
2.63k
    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
2.63k
}
190
191
18.4k
ECCPoint::~ECCPoint() {
192
18.4k
    CF_NORET(wc_ecc_del_point(point));
193
18.4k
}
194
195
26.2k
ecc_point* ECCPoint::GetPtr() {
196
26.2k
    uint8_t* out = nullptr;
197
26.2k
    ecc_point* newPoint = nullptr;
198
199
26.2k
    if ( locked == false && initialized == true ) {
200
4.96k
        bool exportToDER = false;
201
202
4.96k
        try {
203
4.96k
            exportToDER = ds.Get<bool>();
204
4.96k
        } catch ( ... ) { }
205
206
4.96k
        if ( exportToDER == true ) {
207
228
            bool compressed = false;
208
228
            try {
209
228
                compressed = ds.Get<bool>();
210
228
            } catch ( ... ) { }
211
212
228
            const int curveIdx = wc_ecc_get_curve_idx(curveID);
213
228
            CF_CHECK_NE(newPoint = wc_ecc_new_point_h(nullptr), nullptr);
214
215
176
            word32 outSz = 0xFFFF;
216
176
            try { outSz = ds.Get<word32>() & 0xFFFF; } catch ( ... ) { }
217
218
176
            out = util::malloc(outSz);
219
220
176
            if ( compressed == false ) {
221
87
                WC_CHECK_EQ(wc_ecc_export_point_der(curveIdx, point, out, &outSz), 0);
222
89
            } else {
223
89
                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
39
            }
226
227
76
            {
228
76
                haveAllocFailure = false;
229
76
                const bool success = wc_ecc_import_point_der(out, outSz, curveIdx, newPoint) == 0;
230
231
76
                if ( success ) {
232
                    /* Point imported. Replace old point with new point. */
233
234
76
                    CF_NORET(wc_ecc_del_point(point));
235
76
                    point = newPoint;
236
76
                    newPoint = nullptr;
237
76
                } else {
238
                    /* Failure */
239
240
0
                    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
0
                }
245
76
            }
246
76
        }
247
4.96k
    }
248
249
26.2k
end:
250
26.2k
    util::free(out);
251
26.2k
    CF_NORET(wc_ecc_del_point(newPoint));
252
253
26.2k
    return point;
254
26.2k
}
255
256
bool ECCPoint::Set(
257
        const component::BignumPair& xy,
258
        const uint64_t curveType,
259
        const bool pointCheck,
260
7.39k
        const bool mp_init_size_ok) {
261
7.39k
    bool ret = false;
262
263
7.39k
    bool projective = false;
264
7.39k
    try {
265
7.39k
        projective = ds.Get<bool>();
266
7.39k
    } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
267
268
7.39k
    wolfCrypt_bignum::Bignum x(ds, mp_init_size_ok);
269
7.39k
    wolfCrypt_bignum::Bignum y(ds, mp_init_size_ok);
270
7.39k
    wolfCrypt_bignum::Bignum z(ds, mp_init_size_ok);
271
272
7.39k
    if ( projective == false ) {
273
5.22k
        CF_CHECK_TRUE(x.Set(xy.first));
274
5.17k
        CF_CHECK_TRUE(y.Set(xy.second));
275
5.13k
        CF_CHECK_TRUE(z.Set("1"));
276
5.13k
    } else {
277
2.16k
        const auto proj = util::ToRandomProjective(
278
2.16k
                ds,
279
2.16k
                xy.first.ToTrimmedString(),
280
2.16k
                xy.second.ToTrimmedString(),
281
2.16k
                curveType);
282
2.16k
        CF_CHECK_TRUE(x.Set(proj[0]));
283
1.87k
        CF_CHECK_TRUE(y.Set(proj[1]));
284
1.64k
        CF_CHECK_TRUE(z.Set(proj[2]));
285
1.59k
    }
286
287
13.4k
    WC_CHECK_EQ(mp_copy(x.GetPtr(), point->x), MP_OKAY);
288
13.4k
    WC_CHECK_EQ(mp_copy(y.GetPtr(), point->y), MP_OKAY);
289
6.73k
    WC_CHECK_EQ(mp_copy(z.GetPtr(), point->z), MP_OKAY);
290
291
6.73k
    if ( pointCheck ) {
292
0
        CF_CHECK_TRUE(CurveCheck());
293
0
    }
294
295
6.73k
    SetInitialized();
296
297
6.73k
    ret = true;
298
299
7.25k
end:
300
7.25k
    return ret;
301
6.73k
}
302
303
5.25k
bool ECCPoint::ToProjective(wolfCrypt_bignum::Bignum& prime) {
304
5.25k
    bool ret = false;
305
306
5.25k
    wolfCrypt_bignum::Bignum mu(ds);
307
308
5.25k
    WC_CHECK_EQ(mp_montgomery_calc_normalization(mu.GetPtr(), prime.GetPtr()), MP_OKAY);
309
310
5.21k
    if ( mp_cmp_d(mu.GetPtr(), 1) != MP_EQ ) {
311
3.72k
        WC_CHECK_EQ(mp_mulmod(point->x, mu.GetPtr(), prime.GetPtr(), point->x), MP_OKAY);
312
3.65k
        WC_CHECK_EQ(mp_mulmod(point->y, mu.GetPtr(), prime.GetPtr(), point->y), MP_OKAY);
313
3.62k
        WC_CHECK_EQ(mp_mulmod(point->z, mu.GetPtr(), prime.GetPtr(), point->z), MP_OKAY);
314
3.61k
    }
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
5.11k
    Lock();
320
321
5.11k
    ret = true;
322
323
5.25k
end:
324
5.25k
    return ret;
325
5.11k
}
326
327
4.87k
bool ECCPoint::CurveCheck(void) const {
328
4.87k
    const int curveIdx = wc_ecc_get_curve_idx(curveID);
329
4.87k
    return wc_ecc_point_is_on_curve(point, curveIdx) == 0;
330
4.87k
}
331
332
10.3k
void ECCPoint::Lock(void) {
333
10.3k
    locked = true;
334
10.3k
}
335
336
9.23k
void ECCPoint::SetInitialized(void) {
337
9.23k
    initialized = true;
338
9.23k
}
339
340
2.26k
std::optional<component::BignumPair> ECCPoint::ToBignumPair(void) {
341
2.26k
    std::optional<component::BignumPair> ret = std::nullopt;
342
343
2.26k
    wolfCrypt_bignum::Bignum pub_x(GetPtr()->x, ds);
344
345
    /* Pointer is stored in pub_x; lock to prevent UAF */
346
2.26k
    Lock();
347
348
2.26k
    wolfCrypt_bignum::Bignum pub_y(GetPtr()->y, ds);
349
350
2.26k
    std::optional<std::string> pub_x_str, pub_y_str;
351
2.26k
    CF_CHECK_NE(pub_x_str = pub_x.ToDecString(), std::nullopt);
352
2.25k
    CF_CHECK_NE(pub_y_str = pub_y.ToDecString(), std::nullopt);
353
354
2.25k
    ret = { *pub_x_str, *pub_y_str };
355
2.26k
end:
356
357
2.26k
    return ret;
358
2.25k
}
359
360
1.84k
int ECCPoint::Compare(ECCPoint& other) {
361
1.84k
    return wc_ecc_cmp_point(GetPtr(), other.GetPtr());
362
1.84k
}
363
364
1.89k
std::optional<bool> ECCPoint::IsNeg(ECCPoint& other, wolfCrypt_bignum::Bignum& prime) {
365
1.89k
    std::optional<bool> ret = std::nullopt;
366
367
1.89k
    wolfCrypt_bignum::Bignum neg(ds);
368
1.89k
    if ( mp_sub(prime.GetPtr(), other.GetPtr()->y, neg.GetPtr()) == MP_OKAY) {
369
1.86k
        ret = static_cast<bool>(mp_cmp(point->y, neg.GetPtr()) == MP_EQ);
370
1.86k
    }
371
372
1.89k
    return ret;
373
1.89k
}
374
375
1.43k
std::optional<component::ECC_PublicKey> OpECC_PrivateToPublic_Generic(operation::ECC_PrivateToPublic& op) {
376
1.43k
    std::optional<component::ECC_PublicKey> ret = std::nullopt;
377
1.43k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
378
1.43k
    wolfCrypt_detail::SetGlobalDs(&ds);
379
380
1.43k
    try {
381
1.43k
        ECCKey key(ds);
382
383
        /* Initialize */
384
1.43k
        {
385
1.43k
            CF_CHECK_EQ(key.SetCurve(op.curveType), true);
386
901
            CF_CHECK_EQ(key.LoadPrivateKey(op.priv), true);
387
675
        }
388
389
        /* Process/Finalize */
390
0
        {
391
675
            auto pub = key.MakePub();
392
675
            CF_CHECK_NE(pub, std::nullopt);
393
394
593
            ret = pub->ToBignumPair();
395
593
        }
396
593
    } catch ( ... ) { }
397
398
1.43k
end:
399
400
1.43k
    wolfCrypt_detail::UnsetGlobalDs();
401
1.43k
    return ret;
402
1.43k
}
403
404
3.06k
std::optional<bool> OpECC_ValidatePubkey_Generic(operation::ECC_ValidatePubkey& op) {
405
3.06k
    std::optional<bool> ret = std::nullopt;
406
3.06k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
407
3.06k
    wolfCrypt_detail::SetGlobalDs(&ds);
408
409
3.06k
    std::optional<int> curveID;
410
411
3.06k
    try {
412
3.06k
        ECCKey key(ds);
413
3.06k
        {
414
3.06k
            const char* name = nullptr;
415
416
3.06k
            CF_CHECK_NE(curveID = wolfCrypt_detail::toCurveID(op.curveType), std::nullopt);
417
418
2.66k
            CF_CHECK_NE(name = wc_ecc_get_name(*curveID), nullptr);
419
420
2.64k
            WC_CHECK_EQ(wc_ecc_import_raw(
421
2.46k
                        key.GetPtr(),
422
2.46k
                        util::DecToHex(op.pub.first.ToTrimmedString()).c_str(),
423
2.46k
                        util::DecToHex(op.pub.second.ToTrimmedString()).c_str(),
424
2.46k
                        nullptr,
425
2.46k
                        name), 0);
426
2.46k
            haveAllocFailure = false;
427
2.46k
            ret = wc_ecc_check_key(key.GetPtr()) == 0;
428
2.46k
            if ( *ret == false && haveAllocFailure == true ) {
429
462
                ret = std::nullopt;
430
462
            }
431
2.46k
        }
432
2.46k
    } catch ( ... ) { }
433
434
3.06k
end:
435
436
3.06k
    wolfCrypt_detail::UnsetGlobalDs();
437
3.06k
    return ret;
438
3.06k
}
439
440
2.65k
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
2.65k
    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
2.65k
    }
455
456
2.65k
    std::optional<bool> ret = std::nullopt;
457
2.65k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
458
2.65k
    wolfCrypt_detail::SetGlobalDs(&ds);
459
460
2.65k
    std::optional<int> curveID;
461
2.65k
    uint8_t* sig = nullptr;
462
2.65k
    uint8_t* hash = nullptr;
463
2.65k
    word32 sigSz = ECC_MAX_SIG_SIZE;
464
2.65k
    int verify;
465
466
2.65k
    bool ctIsEmpty = false;
467
2.65k
    bool randomSigSz = false;
468
2.65k
    bool hashNotFound = false;
469
470
2.65k
    {
471
2.65k
        try {
472
2.65k
            randomSigSz = ds.Get<bool>();
473
2.65k
            if ( randomSigSz ) {
474
679
                sigSz = ds.Get<uint8_t>();
475
679
            }
476
2.65k
        } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
477
478
2.65k
        sig = util::malloc(sigSz);
479
2.65k
    }
480
481
2.65k
    try {
482
2.65k
        ECCKey key(ds);
483
2.65k
        {
484
2.65k
            const char* name = nullptr;
485
486
2.65k
            CF_CHECK_NE(curveID = wolfCrypt_detail::toCurveID(op.curveType), std::nullopt);
487
488
2.44k
            CF_CHECK_NE(name = wc_ecc_get_name(*curveID), nullptr);
489
490
2.42k
            haveAllocFailure = false;
491
2.42k
            ret = false;
492
493
2.42k
            WC_CHECK_EQ(wc_ecc_import_raw(
494
2.38k
                        key.GetPtr(),
495
2.38k
                        util::DecToHex(op.signature.pub.first.ToTrimmedString()).c_str(),
496
2.38k
                        util::DecToHex(op.signature.pub.second.ToTrimmedString()).c_str(),
497
2.38k
                        nullptr,
498
2.38k
                        name), 0);
499
2.38k
            WC_CHECK_EQ(wc_ecc_check_key(key.GetPtr()), 0);
500
2.19k
        }
501
502
2.19k
        WC_CHECK_EQ(wc_ecc_rs_to_sig(
503
2.08k
                    util::DecToHex(op.signature.signature.first.ToTrimmedString()).c_str(),
504
2.08k
                    util::DecToHex(op.signature.signature.second.ToTrimmedString()).c_str(),
505
2.08k
                    sig, &sigSz), 0);
506
507
2.08k
        if ( op.digestType.Get() == CF_DIGEST("NULL") ) {
508
1.78k
            const auto CT = op.cleartext.ECDSA_RandomPad(ds, op.curveType);
509
1.78k
            ctIsEmpty = CT.GetSize() == 0;
510
1.78k
            WC_CHECK_EQ(wc_ecc_verify_hash(sig, sigSz, CT.GetPtr(), CT.GetSize(), &verify, key.GetPtr()), 0);
511
1.58k
        } else {
512
297
            std::optional<wc_HashType> hashType;
513
297
            hashNotFound = true;
514
297
            CF_CHECK_NE(hashType = wolfCrypt_detail::toHashType(op.digestType), std::nullopt);
515
297
            hashNotFound = false;
516
517
297
            const auto hashSize = wc_HashGetDigestSize(*hashType);
518
297
            hash = util::malloc(hashSize);
519
520
297
            WC_CHECK_EQ(wc_Hash(*hashType, op.cleartext.GetPtr(), op.cleartext.GetSize(), hash, hashSize), 0);
521
522
297
            const auto CT = Buffer(hash, hashSize).ECDSA_RandomPad(ds, op.curveType);
523
297
            ctIsEmpty = CT.GetSize() == 0;
524
297
            WC_CHECK_EQ(wc_ecc_verify_hash(sig, sigSz, CT.GetPtr(), CT.GetSize(), &verify, key.GetPtr()), 0);
525
297
        }
526
527
1.88k
        ret = verify ? true : false;
528
1.88k
    } catch ( ... ) { }
529
530
2.65k
end:
531
532
2.65k
    util::free(sig);
533
2.65k
    util::free(hash);
534
535
2.65k
    wolfCrypt_detail::UnsetGlobalDs();
536
537
2.65k
    if ( ret && *ret == false ) {
538
722
        if ( haveAllocFailure ) {
539
168
            ret = std::nullopt;
540
554
        } else if ( randomSigSz ) {
541
145
            ret = std::nullopt;
542
409
        } else if ( ctIsEmpty ) {
543
            /* wolfCrypt ECDSA verification will fail if the input msg is empty */
544
23
            ret = std::nullopt;
545
386
        } else if ( hashNotFound ) {
546
0
            ret = std::nullopt;
547
386
        } else if ( op.digestType.Is(CF_DIGEST("NULL")) && op.cleartext.IsZero() ) {
548
59
            ret = std::nullopt;
549
59
        }
550
722
    }
551
552
2.65k
    return ret;
553
2.65k
}
554
555
201
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
201
    std::optional<component::ECCSI_Signature> ret = std::nullopt;
561
562
201
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
563
201
    wolfCrypt_detail::SetGlobalDs(&ds);
564
565
201
    uint8_t hashSz = WC_MAX_DIGEST_SIZE;
566
201
    static_assert(WC_MAX_DIGEST_SIZE < 256);
567
568
201
    uint8_t* hash = util::malloc(hashSz);
569
570
201
    uint8_t* encoded = nullptr;
571
201
    EccsiKey eccsi;
572
201
    bool eccsi_initialized = false;
573
574
201
    try {
575
201
        int size;
576
201
        std::optional<component::BignumPair> pubbn = std::nullopt;
577
578
201
        {
579
            /* Private to public */
580
201
            ECCKey key(ds);
581
201
            CF_CHECK_EQ(key.SetCurve(op.curveType), true);
582
155
            CF_CHECK_GT(size = key.GetPtr()->dp->size, 0);
583
155
            CF_CHECK_EQ(key.LoadPrivateKey(op.priv), true);
584
130
            auto pub = key.MakePub();
585
130
            CF_CHECK_NE(pub, std::nullopt);
586
108
            CF_CHECK_NE(pubbn = pub->ToBignumPair(), std::nullopt);
587
588
            /* Encode private and public */
589
107
            encoded = util::malloc(size * 3);
590
591
107
            WC_CHECK_EQ(mp_to_unsigned_bin_len(key.GetPtr()->k, encoded, size), 0);
592
101
            WC_CHECK_EQ(mp_to_unsigned_bin_len(pub->GetPtr()->x, encoded + size, size), 0);
593
101
            WC_CHECK_EQ(mp_to_unsigned_bin_len(pub->GetPtr()->y, encoded + (size * 2), size), 0);
594
101
        }
595
596
0
        {
597
101
            std::optional<int> curveId;
598
101
            CF_CHECK_NE(curveId = toCurveID(op.curveType), std::nullopt);
599
600
101
            WC_CHECK_EQ(wc_InitEccsiKey_ex(&eccsi, size, *curveId, nullptr, -1), 0);
601
97
            eccsi_initialized = true;
602
603
97
            WC_CHECK_EQ(wc_ImportEccsiKey(&eccsi, encoded, size * 3), 0);
604
97
            {
605
97
                uint8_t sig[257]; /* XXX random size */
606
97
                word32 sigSz = sizeof(sig);
607
97
                ECCPoint pvt(ds, *curveId);
608
97
                wolfCrypt_bignum::Bignum ssk(ds);
609
97
                std::optional<wc_HashType> hashType;
610
97
                CF_CHECK_NE(hashType = toHashType(op.digestType), std::nullopt);
611
58
                WC_CHECK_EQ(wc_MakeEccsiPair(
612
58
                            &eccsi,
613
58
                            &rng,
614
58
                            *hashType,
615
58
                            op.id.GetPtr(),
616
58
                            op.id.GetSize(),
617
58
                            ssk.GetPtr(),
618
58
                            pvt.GetPtr()), 0);
619
58
                WC_CHECK_EQ(wc_HashEccsiId(
620
58
                            &eccsi,
621
58
                            *hashType,
622
58
                            op.id.GetPtr(),
623
58
                            op.id.GetSize(),
624
58
                            pvt.GetPtr(),
625
58
                            hash,
626
58
                            &hashSz), 0);
627
58
                WC_CHECK_EQ(wc_SetEccsiHash(&eccsi, hash, hashSz), 0);
628
58
                WC_CHECK_EQ(wc_SetEccsiPair(&eccsi, ssk.GetPtr(), pvt.GetPtr()), 0);
629
58
                WC_CHECK_EQ(wc_SignEccsiHash(
630
58
                            &eccsi,
631
58
                            &rng,
632
58
                            *hashType,
633
58
                            op.cleartext.GetPtr(),
634
58
                            op.cleartext.GetSize(),
635
58
                            sig, &sigSz), 0);
636
58
                CF_ASSERT(sigSz == static_cast<size_t>(size) * 4 + 1, "Unexpected signature size");
637
58
                {
638
58
                    wolfCrypt_bignum::Bignum r(ds), s(ds);
639
58
                    std::optional<std::string> r_str, s_str;
640
58
                    std::optional<component::BignumPair> pvtbn = std::nullopt;
641
642
58
                    WC_CHECK_EQ(mp_read_unsigned_bin(r.GetPtr(), sig, size), 0);
643
58
                    CF_CHECK_NE(r_str = r.ToDecString(), std::nullopt);
644
645
58
                    WC_CHECK_EQ(mp_read_unsigned_bin(s.GetPtr(), sig + size, size), 0);
646
58
                    CF_CHECK_NE(s_str = s.ToDecString(), std::nullopt);
647
648
58
                    CF_CHECK_NE(pvtbn = pvt.ToBignumPair(), std::nullopt);
649
650
58
                    ret = component::ECCSI_Signature{
651
58
                        component::BignumPair{*r_str, *s_str},
652
58
                        *pubbn,
653
58
                        *pvtbn};
654
58
                }
655
58
            }
656
58
        }
657
58
    } catch ( ... ) { }
658
659
201
end:
660
201
    if ( eccsi_initialized ) {
661
45
        CF_NORET(wc_FreeEccsiKey(&eccsi));
662
45
    }
663
201
    util::free(hash);
664
201
    util::free(encoded);
665
201
    wolfCrypt_detail::UnsetGlobalDs();
666
201
    return ret;
667
201
#endif
668
201
}
669
670
44
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
44
    std::optional<bool> ret = std::nullopt;
676
44
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
677
44
    wolfCrypt_detail::SetGlobalDs(&ds);
678
679
44
    uint8_t hashSz = WC_MAX_DIGEST_SIZE;
680
44
    static_assert(WC_MAX_DIGEST_SIZE < 256);
681
682
44
    EccsiKey eccsi;
683
684
44
    uint8_t* hash = util::malloc(hashSz);
685
44
    std::optional<wc_HashType> hashType;
686
44
    std::optional<int> curveId;
687
44
    std::vector<uint8_t> pub, sig;
688
44
    int verified;
689
44
    int size;
690
44
    bool eccsi_initialized = false;
691
692
44
    CF_CHECK_NE(curveId = toCurveID(op.curveType), std::nullopt);
693
16
    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
44
end:
765
44
    if ( eccsi_initialized ) {
766
0
        CF_NORET(wc_FreeEccsiKey(&eccsi));
767
0
    }
768
44
    util::free(hash);
769
44
    wolfCrypt_detail::UnsetGlobalDs();
770
44
    if ( ret && *ret == false ) {
771
0
        if ( haveAllocFailure ) {
772
0
            ret = std::nullopt;
773
0
        }
774
0
    }
775
44
    return ret;
776
0
#endif
777
0
}
778
779
2.84k
std::optional<component::ECDSA_Signature> OpECDSA_Sign_Generic(operation::ECDSA_Sign& op) {
780
2.84k
    std::optional<component::ECDSA_Signature> ret = std::nullopt;
781
2.84k
    if ( op.UseRandomNonce() == false && op.UseSpecifiedNonce() == false ) {
782
127
        return ret;
783
127
    }
784
785
2.72k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
786
2.72k
    wolfCrypt_detail::SetGlobalDs(&ds);
787
788
2.72k
    uint8_t* sig = nullptr;
789
2.72k
    word32 sigSz = ECC_MAX_SIG_SIZE;
790
2.72k
    Buffer CT;
791
2.72k
    uint8_t* hash = nullptr;
792
2.72k
    size_t hashSize = 0;
793
2.72k
    uint8_t* nonce_bytes = nullptr;
794
2.72k
    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
2.72k
    wolfCrypt_bignum::Bignum r(ds, false), s(ds, false);
802
803
2.72k
    CF_CHECK_NE(op.priv.ToTrimmedString(), "0");
804
805
2.65k
    {
806
2.65k
        try {
807
2.65k
            sigSz = ds.Get<uint8_t>();
808
2.65k
        } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
809
810
2.65k
        sig = util::malloc(sigSz);
811
2.65k
    }
812
813
2.65k
    try {
814
2.65k
        ECCKey key(ds);
815
2.65k
        CF_CHECK_EQ(key.SetCurve(op.curveType), true);
816
2.41k
        CF_CHECK_EQ(key.LoadPrivateKey(op.priv), true);
817
2.37k
        key.GetPtr()->type = ECC_PRIVATEKEY_ONLY;
818
819
2.37k
        if ( op.UseSpecifiedNonce() == true ) {
820
674
            CF_CHECK_EQ(nonce.Set(op.nonce.ToString(ds)), true);
821
822
655
            const size_t nonce_bytes_size = mp_unsigned_bin_size(nonce.GetPtr());
823
824
            /* Convert nonce to byte array */
825
655
            nonce_bytes = util::malloc(nonce_bytes_size);
826
655
            CF_CHECK_EQ(mp_to_unsigned_bin(nonce.GetPtr(), nonce_bytes), 0);
827
828
            /* Set nonce */
829
634
            WC_CHECK_EQ(wc_ecc_sign_set_k(nonce_bytes, nonce_bytes_size, key.GetPtr()), 0);
830
589
        }
831
832
2.28k
        auto pub = key.MakePub();
833
2.28k
        CF_CHECK_NE(pub, std::nullopt);
834
835
2.25k
        if ( op.digestType.Get() == CF_DIGEST("NULL") ) {
836
2.14k
            CT = op.cleartext.ECDSA_RandomPad(ds, op.curveType);
837
2.14k
        } else {
838
119
            std::optional<wc_HashType> hashType;
839
119
            CF_CHECK_NE(hashType = wolfCrypt_detail::toHashType(op.digestType), std::nullopt);
840
841
119
            hashSize = wc_HashGetDigestSize(*hashType);
842
119
            hash = util::malloc(hashSize);
843
844
119
            WC_CHECK_EQ(wc_Hash(*hashType, op.cleartext.GetPtr(), op.cleartext.GetSize(), hash, hashSize), 0);
845
846
119
            CT = Buffer(hash, hashSize).ECDSA_RandomPad(ds, op.curveType);
847
119
        }
848
849
        /* Sign */
850
3.99k
        WC_CHECK_EQ(wc_ecc_sign_hash(CT.GetPtr(), CT.GetSize(), sig, &sigSz, wolfCrypt_detail::GetRNG(), key.GetPtr()), 0);
851
852
        /* Verify */
853
3.99k
        {
854
3.99k
            int verify;
855
3.99k
            haveAllocFailure = false;
856
3.99k
            if ( wc_ecc_verify_hash(sig, sigSz, CT.GetPtr(), CT.GetSize(), &verify, key.GetPtr()) == 0 && haveAllocFailure == false ) {
857
1.58k
                CF_ASSERT(verify, "Cannot verify generated signature");
858
1.58k
            }
859
3.99k
        }
860
861
1.73k
        CF_CHECK_EQ(DecodeECC_DSA_Sig(sig, sigSz, r.GetPtr(), s.GetPtr()), 0);
862
1.73k
        {
863
1.73k
            std::optional<std::string> r_str, s_str;
864
865
1.73k
            if ( op.curveType.Get() == CF_ECC_CURVE("secp256k1") ) {
866
10
                wolfCrypt_bignum::Bignum SMax(ds);
867
10
                CF_CHECK_EQ(SMax.Set("57896044618658097711785492504343953926418782139537452191302581570759080747168"), true);
868
10
                if ( mp_cmp(s.GetPtr(), SMax.GetPtr()) == 1 ) {
869
4
                    wolfCrypt_bignum::Bignum SSub(ds);
870
4
                    CF_CHECK_EQ(SSub.Set("115792089237316195423570985008687907852837564279074904382605163141518161494337"), true);
871
4
                    CF_CHECK_EQ(mp_sub(SSub.GetPtr(), s.GetPtr(), s.GetPtr()), 0);
872
4
                }
873
1.72k
            } else if ( op.curveType.Get() == CF_ECC_CURVE("secp256r1") ) {
874
484
                wolfCrypt_bignum::Bignum SMax(ds);
875
484
                CF_CHECK_EQ(SMax.Set("57896044605178124381348723474703786764998477612067880171211129530534256022184"), true);
876
483
                if ( mp_cmp(s.GetPtr(), SMax.GetPtr()) == 1 ) {
877
246
                    wolfCrypt_bignum::Bignum SSub(ds);
878
246
                    CF_CHECK_EQ(SSub.Set("115792089210356248762697446949407573529996955224135760342422259061068512044369"), true);
879
246
                    CF_CHECK_EQ(mp_sub(SSub.GetPtr(), s.GetPtr(), s.GetPtr()), 0);
880
246
                }
881
483
            }
882
883
1.73k
            CF_CHECK_NE(r_str = r.ToDecString(), std::nullopt);
884
1.73k
            CF_CHECK_NE(s_str = s.ToDecString(), std::nullopt);
885
886
1.73k
            const auto pub2 = pub->ToBignumPair();
887
1.73k
            CF_CHECK_NE(pub2, std::nullopt);
888
889
1.73k
            ret = component::ECDSA_Signature({*r_str, *s_str}, *pub2);
890
1.73k
        }
891
1.73k
    } catch ( ... ) { }
892
2.72k
end:
893
894
2.72k
    util::free(sig);
895
2.72k
    util::free(hash);
896
2.72k
    util::free(nonce_bytes);
897
898
2.72k
    wolfCrypt_detail::UnsetGlobalDs();
899
900
2.72k
    return ret;
901
2.65k
}
902
903
507
std::optional<component::Ciphertext> OpECIES_Encrypt_Generic(operation::ECIES_Encrypt& op) {
904
507
    std::optional<component::Ciphertext> ret = std::nullopt;
905
#if !defined(HAVE_ECC_ENCRYPT)
906
    (void)op;
907
#else
908
507
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
909
507
    wolfCrypt_detail::SetGlobalDs(&ds);
910
507
    uint8_t* out = nullptr;
911
912
507
    CF_CHECK_TRUE(op.cipherType.Is(CF_CIPHER("AES_128_CBC")));
913
358
    CF_CHECK_EQ(op.iv, std::nullopt);
914
915
342
    try {
916
342
        ECCKey priv(ds), pub(ds);
917
342
        word32 outSz = ds.Get<uint32_t>() % 0xFFFFFF;
918
919
        /* Initialize private key */
920
342
        {
921
342
            CF_CHECK_TRUE(priv.SetCurve(op.curveType));
922
314
            CF_CHECK_TRUE(priv.LoadPrivateKey(op.priv));
923
303
            CF_CHECK_TRUE(priv.SetRNG());
924
303
        }
925
926
        /* Initialize public key */
927
0
        {
928
303
            std::optional<int> curveID;
929
303
            const char* name = nullptr;
930
931
303
            CF_CHECK_NE(curveID = wolfCrypt_detail::toCurveID(op.curveType), std::nullopt);
932
933
303
            CF_CHECK_NE(name = wc_ecc_get_name(*curveID), nullptr);
934
935
303
            WC_CHECK_EQ(wc_ecc_import_raw(
936
292
                        pub.GetPtr(),
937
292
                        util::DecToHex(op.pub.first.ToTrimmedString()).c_str(),
938
292
                        util::DecToHex(op.pub.second.ToTrimmedString()).c_str(),
939
292
                        nullptr,
940
292
                        name), 0);
941
942
292
            CF_CHECK_TRUE(pub.SetRNG());
943
292
        }
944
945
0
        out = util::malloc(outSz);
946
947
292
        WC_CHECK_EQ(wc_ecc_encrypt(priv.GetPtr(), pub.GetPtr(), op.cleartext.GetPtr(), op.cleartext.GetSize(), out, &outSz, nullptr), 0);
948
949
218
        ret = component::Ciphertext(Buffer(out, outSz));
950
218
    } catch ( ... ) { }
951
952
507
end:
953
507
    util::free(out);
954
955
507
    wolfCrypt_detail::UnsetGlobalDs();
956
507
#endif
957
507
    return ret;
958
342
}
959
960
509
std::optional<component::Cleartext> OpECIES_Decrypt_Generic(operation::ECIES_Decrypt& op) {
961
509
    std::optional<component::Cleartext> ret = std::nullopt;
962
#if !defined(HAVE_ECC_ENCRYPT)
963
    (void)op;
964
#else
965
509
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
966
509
    wolfCrypt_detail::SetGlobalDs(&ds);
967
509
    uint8_t* out = nullptr;
968
969
509
    CF_CHECK_TRUE(op.cipherType.Is(CF_CIPHER("AES_128_CBC")));
970
347
    CF_CHECK_EQ(op.iv, std::nullopt);
971
972
347
    try {
973
347
        ECCKey priv(ds), pub(ds);
974
347
        word32 outSz = ds.Get<uint32_t>() % 0xFFFFFF;
975
976
        /* Initialize private key */
977
347
        {
978
347
            CF_CHECK_TRUE(priv.SetCurve(op.curveType));
979
319
            CF_CHECK_TRUE(priv.LoadPrivateKey(op.priv));
980
305
            CF_CHECK_TRUE(priv.SetRNG());
981
305
        }
982
983
        /* Initialize public key */
984
0
        {
985
305
            std::optional<int> curveID;
986
305
            const char* name = nullptr;
987
988
305
            CF_CHECK_NE(curveID = wolfCrypt_detail::toCurveID(op.curveType), std::nullopt);
989
990
305
            CF_CHECK_NE(name = wc_ecc_get_name(*curveID), nullptr);
991
992
305
            WC_CHECK_EQ(wc_ecc_import_raw(
993
289
                        pub.GetPtr(),
994
289
                        util::DecToHex(op.pub.first.ToTrimmedString()).c_str(),
995
289
                        util::DecToHex(op.pub.second.ToTrimmedString()).c_str(),
996
289
                        nullptr,
997
289
                        name), 0);
998
999
289
            CF_CHECK_TRUE(pub.SetRNG());
1000
289
        }
1001
1002
0
        out = util::malloc(outSz);
1003
1004
289
        WC_CHECK_EQ(wc_ecc_decrypt(priv.GetPtr(), pub.GetPtr(), op.ciphertext.GetPtr(), op.ciphertext.GetSize(), out, &outSz, nullptr), 0);
1005
1006
159
        ret = component::Cleartext(Buffer(out, outSz));
1007
159
    } catch ( ... ) { }
1008
1009
509
end:
1010
509
    util::free(out);
1011
1012
509
    wolfCrypt_detail::UnsetGlobalDs();
1013
509
#endif
1014
509
    return ret;
1015
347
}
1016
1017
464
std::optional<component::Secret> OpECDH_Derive(operation::ECDH_Derive& op) {
1018
464
    std::optional<component::Secret> ret = std::nullopt;
1019
464
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1020
464
    wolfCrypt_detail::SetGlobalDs(&ds);
1021
1022
    /* try/catch because ECCKey constructor may throw if allocation fails */
1023
464
    try {
1024
        /* TODO dynamic size */
1025
464
        uint8_t out[1024];
1026
464
        word32 outlen = sizeof(out);
1027
464
        ECCKey priv(ds), pub(ds);
1028
1029
        /* Initialize private key */
1030
464
        {
1031
464
            CF_CHECK_TRUE(priv.SetCurve(op.curveType));
1032
386
            CF_CHECK_TRUE(priv.LoadPrivateKey(op.priv));
1033
361
            CF_CHECK_TRUE(priv.SetRNG());
1034
361
            WC_CHECK_EQ(
1035
361
                    wc_ecc_set_flags(priv.GetPtr(), WC_ECC_FLAG_COFACTOR), 0);
1036
361
        }
1037
1038
        /* Initialize public key */
1039
0
        {
1040
361
            std::optional<int> curveID;
1041
361
            const char* name = nullptr;
1042
1043
361
            CF_CHECK_NE(curveID = wolfCrypt_detail::toCurveID(op.curveType), std::nullopt);
1044
1045
361
            CF_CHECK_NE(name = wc_ecc_get_name(*curveID), nullptr);
1046
1047
361
            WC_CHECK_EQ(wc_ecc_import_raw(
1048
302
                        pub.GetPtr(),
1049
302
                        util::DecToHex(op.pub.first.ToTrimmedString()).c_str(),
1050
302
                        util::DecToHex(op.pub.second.ToTrimmedString()).c_str(),
1051
302
                        nullptr,
1052
302
                        name), 0);
1053
302
            WC_CHECK_EQ(wc_ecc_check_key(pub.GetPtr()), 0);
1054
183
        }
1055
1056
183
        WC_CHECK_EQ(wc_ecc_shared_secret(priv.GetPtr(), pub.GetPtr(), out, &outlen), 0);
1057
1058
167
        ret = component::Secret(Buffer(out, outlen));
1059
167
    } catch ( ... ) { }
1060
1061
464
end:
1062
464
    wolfCrypt_detail::UnsetGlobalDs();
1063
1064
464
    return ret;
1065
464
}
1066
1067
554
std::optional<component::ECC_Point> OpECC_Point_Add(operation::ECC_Point_Add& op) {
1068
554
    std::optional<component::ECC_Point> ret = std::nullopt;
1069
554
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1070
554
    wolfCrypt_detail::SetGlobalDs(&ds);
1071
1072
554
    std::optional<int> curveID;
1073
554
    int curveIdx;
1074
554
    const ecc_set_type* curve = nullptr;
1075
554
    bool failed = false;
1076
554
    bool valid = false;
1077
554
    std::optional<bool> is_neg = std::nullopt;
1078
1079
554
    CF_CHECK_NE(curveID = wolfCrypt_detail::toCurveID(op.curveType), std::nullopt);
1080
1081
499
    CF_CHECK_NE(curveIdx = wc_ecc_get_curve_idx(*curveID), ECC_CURVE_INVALID);
1082
499
    CF_CHECK_NE(curve = wc_ecc_get_curve_params(curveIdx), nullptr);
1083
1084
    /* try/catch because ECCPoint constructor may throw if allocation fails */
1085
499
    try {
1086
499
        ECCPoint res(ds, *curveID), a(ds, *curveID), b(ds, *curveID);
1087
499
        wolfCrypt_bignum::Bignum Af(ds), prime(ds), mu(ds);
1088
499
        mp_digit mp;
1089
1090
        /* Set points */
1091
499
        CF_CHECK_TRUE(a.Set(op.a, op.curveType.Get()));
1092
469
        CF_CHECK_TRUE(b.Set(op.b, op.curveType.Get()));
1093
1094
437
        valid = a.CurveCheck() && b.CurveCheck();
1095
1096
        /* Retrieve curve parameter */
1097
437
        CF_CHECK_EQ(Af.Set(util::HexToDec(curve->Af)), true);
1098
422
        CF_CHECK_EQ(prime.Set(util::HexToDec(curve->prime)), true);
1099
1100
415
        is_neg = a.IsNeg(b, prime);
1101
1102
415
        CF_CHECK_TRUE(a.ToProjective(prime));
1103
392
        CF_CHECK_TRUE(b.ToProjective(prime));
1104
1105
387
        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
387
        {
1111
387
            bool dbl = false;
1112
387
            bool safe = false;
1113
1114
387
            if ( a.Compare(b) == MP_EQ ) {
1115
65
                try { dbl = ds.Get<bool>(); } catch ( ... ) { }
1116
65
            }
1117
1118
387
#if !(defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_ECC_ADD_DBL))
1119
387
            try { safe = ds.Get<bool>(); } catch ( ... ) { }
1120
387
#endif
1121
1122
387
            if ( safe ) {
1123
#if defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_ECC_ADD_DBL)
1124
                CF_UNREACHABLE();
1125
#else
1126
15
                int infinity;
1127
1128
15
                if ( dbl == true ) {
1129
2
                    failed = true;
1130
2
                    haveAllocFailure = false;
1131
2
                    WC_CHECK_EQ(ecc_projective_dbl_point_safe(a.GetPtr(), res.GetPtr(), Af.GetPtr(), prime.GetPtr(), mp), 0);
1132
1
                    failed = false;
1133
13
                } else {
1134
13
                    failed = true;
1135
13
                    haveAllocFailure = false;
1136
13
                    WC_CHECK_EQ(ecc_projective_add_point_safe(a.GetPtr(), b.GetPtr(), res.GetPtr(), Af.GetPtr(), prime.GetPtr(), mp, &infinity), 0);
1137
5
                    failed = false;
1138
5
                }
1139
15
#endif
1140
294
            } else {
1141
294
                if ( dbl == true ) {
1142
3
                    failed = true;
1143
3
                    haveAllocFailure = false;
1144
3
                    WC_CHECK_EQ(ecc_projective_dbl_point(a.GetPtr(), res.GetPtr(), Af.GetPtr(), prime.GetPtr(), mp), 0);
1145
1
                    failed = false;
1146
291
                } else {
1147
291
                    failed = true;
1148
291
                    haveAllocFailure = false;
1149
291
                    WC_CHECK_EQ(ecc_projective_add_point(a.GetPtr(), b.GetPtr(), res.GetPtr(), Af.GetPtr(), prime.GetPtr(), mp), 0);
1150
244
                    failed = false;
1151
1152
                    /* Do not return result if inputs are negations of the same point */
1153
244
                    CF_CHECK_NE(is_neg, std::nullopt);
1154
237
                    CF_CHECK_FALSE(*is_neg);
1155
222
                }
1156
294
            }
1157
1158
            /* Lock to prevent exporting the projective point */
1159
229
            res.Lock();
1160
229
        }
1161
0
#endif
1162
1163
        /* To affine */
1164
229
        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
225
        CF_CHECK_TRUE(valid);
1168
1169
        /* Only return the result if the output point is valid */
1170
33
        CF_CHECK_TRUE(res.CurveCheck());
1171
1172
29
        ret = res.ToBignumPair();
1173
78
    } catch ( ... ) { }
1174
1175
554
end:
1176
554
    wolfCrypt_detail::UnsetGlobalDs();
1177
1178
554
    if ( valid ) {
1179
47
        if ( !haveAllocFailure ) {
1180
45
            if ( is_neg && !*is_neg ) {
1181
32
                CF_ASSERT(!failed, "Point adding failed");
1182
32
            }
1183
45
        }
1184
47
    }
1185
1186
554
    return ret;
1187
554
}
1188
1189
1.41k
std::optional<component::ECC_Point> OpECC_Point_Mul(operation::ECC_Point_Mul& op) {
1190
1.41k
    std::optional<component::ECC_Point> ret = std::nullopt;
1191
1.41k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1192
1.41k
    wolfCrypt_detail::SetGlobalDs(&ds);
1193
1194
1.41k
    std::optional<int> curveID;
1195
1.41k
    int curveIdx;
1196
1.41k
    const ecc_set_type* curve = nullptr;
1197
1198
1.41k
    CF_CHECK_NE(curveID = wolfCrypt_detail::toCurveID(op.curveType), std::nullopt);
1199
1200
1.33k
    CF_CHECK_NE(curveIdx = wc_ecc_get_curve_idx(*curveID), ECC_CURVE_INVALID);
1201
1.32k
    CF_CHECK_NE(curve = wc_ecc_get_curve_params(curveIdx), nullptr);
1202
1203
    /* try/catch because ECCPoint constructor may throw if allocation fails */
1204
1.32k
    try {
1205
1.32k
        ECCPoint res(ds, *curveID), a(ds, *curveID);
1206
1.32k
        wolfCrypt_bignum::Bignum b(ds), Af(ds), prime(ds);
1207
1.32k
        bool valid = false;
1208
1209
        /* Set point */
1210
1.32k
        CF_CHECK_TRUE(a.Set(op.a, op.curveType.Get()));
1211
1.22k
        valid = a.CurveCheck();
1212
1213
        /* Set multiplier */
1214
1.22k
        CF_CHECK_EQ(b.Set(op.b.ToString(ds)), true);
1215
1216
        /* Retrieve curve parameters */
1217
1.16k
        CF_CHECK_EQ(Af.Set(util::HexToDec(curve->Af)), true);
1218
1.13k
        CF_CHECK_EQ(prime.Set(util::HexToDec(curve->prime)), true);
1219
1220
        /* Multiply */
1221
1.10k
        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
881
        CF_CHECK_TRUE(valid);
1225
1226
209
        ret = res.ToBignumPair();
1227
209
    } catch ( ... ) { }
1228
1229
1.41k
end:
1230
1.41k
    wolfCrypt_detail::UnsetGlobalDs();
1231
1232
1.41k
    return ret;
1233
1.32k
}
1234
1235
901
std::optional<component::ECC_Point> OpECC_Point_Dbl(operation::ECC_Point_Dbl& op) {
1236
901
    std::optional<component::ECC_Point> ret = std::nullopt;
1237
901
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
1238
901
    wolfCrypt_detail::SetGlobalDs(&ds);
1239
1240
901
    std::optional<int> curveID;
1241
901
    int curveIdx;
1242
901
    const ecc_set_type* curve = nullptr;
1243
901
    bool failed = false;
1244
901
    bool valid = false;
1245
1246
901
    CF_CHECK_NE(curveID = wolfCrypt_detail::toCurveID(op.curveType), std::nullopt);
1247
1248
838
    CF_CHECK_NE(curveIdx = wc_ecc_get_curve_idx(*curveID), ECC_CURVE_INVALID);
1249
838
    CF_CHECK_NE(curve = wc_ecc_get_curve_params(curveIdx), nullptr);
1250
1251
    /* try/catch because ECCPoint constructor may throw if allocation fails */
1252
838
    try {
1253
838
        ECCPoint res(ds, *curveID), a(ds, *curveID);
1254
838
        wolfCrypt_bignum::Bignum Af(ds), prime(ds), mu(ds);
1255
838
        mp_digit mp;
1256
1257
        /* Set points */
1258
838
        CF_CHECK_TRUE(a.Set(op.a, op.curveType.Get()));
1259
1260
650
        valid = a.CurveCheck();
1261
1262
        /* Retrieve curve parameter */
1263
650
        CF_CHECK_EQ(Af.Set(util::HexToDec(curve->Af)), true);
1264
634
        CF_CHECK_EQ(prime.Set(util::HexToDec(curve->prime)), true);
1265
1266
608
        CF_CHECK_TRUE(a.ToProjective(prime));
1267
1268
557
        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
557
        {
1274
557
            bool safe = false;
1275
1276
557
#if !(defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_ECC_ADD_DBL))
1277
557
            try { safe = ds.Get<bool>(); } catch ( ... ) { }
1278
557
#endif
1279
1280
557
            if ( safe ) {
1281
#if defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_ECC_ADD_DBL)
1282
                CF_UNREACHABLE();
1283
#else
1284
11
                failed = true;
1285
11
                haveAllocFailure = false;
1286
11
                WC_CHECK_EQ(ecc_projective_dbl_point_safe(a.GetPtr(), res.GetPtr(), Af.GetPtr(), prime.GetPtr(), mp), 0);
1287
8
                failed = false;
1288
8
#endif
1289
447
            } else {
1290
447
                failed = true;
1291
447
                haveAllocFailure = false;
1292
447
                WC_CHECK_EQ(ecc_projective_dbl_point(a.GetPtr(), res.GetPtr(), Af.GetPtr(), prime.GetPtr(), mp), 0);
1293
410
                failed = false;
1294
410
            }
1295
1296
            /* Lock to prevent exporting the projective point */
1297
418
            res.Lock();
1298
418
        }
1299
0
#endif
1300
1301
        /* To affine */
1302
418
        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
408
        CF_CHECK_TRUE(valid);
1306
1307
        /* Only return the result if the output point is valid */
1308
37
        CF_CHECK_TRUE(res.CurveCheck());
1309
1310
27
        ret = res.ToBignumPair();
1311
99
    } catch ( ... ) { }
1312
1313
901
end:
1314
901
    if ( valid ) {
1315
50
        if ( !haveAllocFailure ) {
1316
35
            CF_ASSERT(!failed, "Point doubling failed");
1317
35
        }
1318
50
    }
1319
1320
901
    wolfCrypt_detail::UnsetGlobalDs();
1321
1322
901
    return ret;
1323
901
}
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 */