Coverage Report

Created: 2026-02-14 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cryptofuzz-sp-math-all-8bit/modules/wolfcrypt/ecdsa_448.cpp
Line
Count
Source
1
#include "ecdsa_448.h"
2
#include "module_internal.h"
3
#include "shared.h"
4
#include "bn_ops.h"
5
#include <cryptofuzz/util.h>
6
7
extern "C" {
8
#include <wolfssl/options.h>
9
#include <wolfssl/wolfcrypt/curve448.h>
10
#include <wolfssl/wolfcrypt/ed448.h>
11
}
12
13
namespace cryptofuzz {
14
namespace module {
15
namespace wolfCrypt_detail {
16
17
#if defined(CRYPTOFUZZ_WOLFCRYPT_ALLOCATION_FAILURES)
18
    extern bool haveAllocFailure;
19
#endif
20
21
2.08k
static bool ed448LoadPrivateKey(ed448_key& key, component::Bignum priv, Datasource& ds) {
22
2.08k
    bool ret = false;
23
24
2.08k
    uint8_t priv_bytes[ED448_KEY_SIZE];
25
26
2.08k
    CF_CHECK_EQ(wolfCrypt_bignum::Bignum::ToBin(ds, priv, priv_bytes, sizeof(priv_bytes)), true);
27
2.01k
    CF_CHECK_EQ(wc_ed448_import_private_only(priv_bytes, sizeof(priv_bytes), &key), 0);
28
29
2.01k
    ret = true;
30
2.08k
end:
31
2.08k
    return ret;
32
2.01k
}
33
34
3.76k
static std::optional<std::vector<uint8_t>> ed448GetPublicKeyAsVector(ed448_key& key) {
35
3.76k
    std::optional<std::vector<uint8_t>> ret = std::nullopt;
36
3.76k
    uint8_t pub_bytes[ED448_PUB_KEY_SIZE];
37
38
3.76k
    WC_CHECK_EQ(wc_ed448_make_public(&key, pub_bytes, sizeof(pub_bytes)), MP_OKAY);
39
40
3.68k
    ret = std::vector<uint8_t>(pub_bytes, pub_bytes + sizeof(pub_bytes));
41
3.76k
end:
42
3.76k
    return ret;
43
3.68k
}
44
45
1.75k
static std::optional<component::ECC_PublicKey> ed448GetPublicKey(ed448_key& key, Datasource& ds) {
46
1.75k
    std::optional<component::ECC_PublicKey> ret = std::nullopt;
47
1.75k
    std::optional<std::vector<uint8_t>> pubv = std::nullopt;
48
1.75k
    std::optional<component::Bignum> pub = std::nullopt;
49
50
1.75k
    CF_CHECK_NE(pubv = ed448GetPublicKeyAsVector(key), std::nullopt);
51
1.72k
    CF_CHECK_NE(pub = wolfCrypt_bignum::Bignum::BinToBignum(ds, pubv->data(), pubv->size()), std::nullopt);
52
53
1.71k
    ret = {pub->ToString(), "0"};
54
55
1.75k
end:
56
1.75k
    return ret;
57
1.71k
}
58
59
2.01k
static bool ed448DerivePublicKey(ed448_key& key) {
60
2.01k
    std::optional<std::vector<uint8_t>> pubv = std::nullopt;
61
2.01k
    bool ret = false;
62
63
2.01k
    CF_CHECK_NE(pubv = ed448GetPublicKeyAsVector(key), std::nullopt);
64
1.95k
    memcpy(key.p, pubv->data(), ED448_PUB_KEY_SIZE);
65
1.95k
    key.pubKeySet = 1;
66
67
1.95k
    ret = true;
68
69
2.01k
end:
70
2.01k
    return ret;
71
1.95k
}
72
73
2.95k
static bool ed448LoadPublicKey(ed448_key& key, component::Bignum pub, Datasource& ds, const bool mustSucceed = false) {
74
2.95k
    bool ret = false;
75
76
2.95k
    uint8_t pub_bytes[ED448_PUB_KEY_SIZE];
77
2.95k
    CF_CHECK_EQ(wolfCrypt_bignum::Bignum::ToBin(ds, pub, pub_bytes, sizeof(pub_bytes), mustSucceed), true);
78
2.87k
    CF_CHECK_EQ(wc_ed448_import_public(pub_bytes, sizeof(pub_bytes), &key), 0);
79
80
2.73k
    ret = true;
81
2.95k
end:
82
2.95k
    return ret;
83
2.73k
}
84
85
1.03k
std::optional<component::ECC_PublicKey> OpECC_PrivateToPublic_Curve448(operation::ECC_PrivateToPublic& op) {
86
1.03k
    std::optional<component::ECC_PublicKey> ret = std::nullopt;
87
1.03k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
88
1.03k
    wolfCrypt_detail::SetGlobalDs(&ds);
89
90
1.03k
    static const uint8_t basepoint[CURVE448_KEY_SIZE] = {5};
91
92
1.03k
    std::optional<std::vector<uint8_t>> priv_bytes = std::nullopt;
93
1.03k
    uint8_t pub_bytes[CURVE448_PUB_KEY_SIZE];
94
95
    /* Load private key */
96
1.03k
    {
97
1.03k
        priv_bytes = util::DecToBin(op.priv.ToTrimmedString(), CURVE448_KEY_SIZE);
98
1.03k
        CF_CHECK_NE(priv_bytes, std::nullopt);
99
100
871
        priv_bytes->data()[0] &= 0xFC;
101
871
        priv_bytes->data()[55] |= 0x80; static_assert(55 < CURVE448_KEY_SIZE);
102
871
    }
103
104
    /* Convert to public key */
105
0
    {
106
871
        CF_CHECK_EQ(curve448(pub_bytes, priv_bytes->data(), basepoint), MP_OKAY);
107
871
    }
108
109
    /* Convert public key */
110
0
    {
111
871
        ret = { util::BinToDec(pub_bytes, sizeof(pub_bytes)), "0" };
112
871
    }
113
114
1.03k
end:
115
1.03k
    wolfCrypt_detail::UnsetGlobalDs();
116
1.03k
    return ret;
117
871
}
118
119
446
std::optional<component::ECC_PublicKey> OpECC_PrivateToPublic_Ed448(operation::ECC_PrivateToPublic& op) {
120
446
    std::optional<component::ECC_PublicKey> ret = std::nullopt;
121
446
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
122
446
    wolfCrypt_detail::SetGlobalDs(&ds);
123
124
446
    uint8_t pub_bytes[ED448_PUB_KEY_SIZE];
125
126
446
    ed448_key key;
127
446
    bool e448_key_inited = false;
128
129
446
    WC_CHECK_EQ(wc_ed448_init(&key), 0);
130
446
    e448_key_inited = true;
131
132
    /* Load private key */
133
446
    {
134
446
        const auto priv_bytes = util::DecToBin(op.priv.ToTrimmedString(), ED448_KEY_SIZE);
135
446
        CF_CHECK_NE(priv_bytes, std::nullopt);
136
137
375
        WC_CHECK_EQ(wc_ed448_import_private_only(priv_bytes->data(), priv_bytes->size(), &key), 0);
138
375
    }
139
140
    /* Convert to public key */
141
0
    {
142
375
        WC_CHECK_EQ(wc_ed448_make_public(&key, pub_bytes, sizeof(pub_bytes)), MP_OKAY);
143
273
    }
144
145
    /* Convert public key */
146
0
    {
147
273
        ret = { util::BinToDec(pub_bytes, sizeof(pub_bytes)), "0" };
148
273
    }
149
150
446
end:
151
446
    if ( e448_key_inited == true ) {
152
446
        wc_ed448_free(&key);
153
446
    }
154
155
446
    wolfCrypt_detail::UnsetGlobalDs();
156
446
    return ret;
157
273
}
158
159
0
std::optional<bool> OpECC_ValidatePubkey_Ed448(operation::ECC_ValidatePubkey& op) {
160
0
    std::optional<bool> ret = std::nullopt;
161
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
162
0
    wolfCrypt_detail::SetGlobalDs(&ds);
163
164
0
    ed448_key key;
165
0
    bool e448_key_inited = false;
166
167
0
    WC_CHECK_EQ(wc_ed448_init(&key), 0);
168
0
    e448_key_inited = true;
169
170
0
    CF_CHECK_EQ(ed448LoadPublicKey(key, op.pub.first, ds), true);
171
172
0
    haveAllocFailure = false;
173
0
    ret = wc_ed448_check_key(&key) == 0;
174
0
    if ( *ret == false && haveAllocFailure == true ) {
175
0
        ret = std::nullopt;
176
0
    }
177
178
0
end:
179
0
    if ( e448_key_inited == true ) {
180
0
        wc_ed448_free(&key);
181
0
    }
182
183
0
    wolfCrypt_detail::UnsetGlobalDs();
184
0
    return ret;
185
0
}
186
187
0
std::optional<bool> OpECC_ValidatePubkey_Curve448(operation::ECC_ValidatePubkey& op) {
188
0
    std::optional<bool> ret = std::nullopt;
189
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
190
0
    wolfCrypt_detail::SetGlobalDs(&ds);
191
192
0
    wolfCrypt_bignum::Bignum pub(ds);
193
0
    uint8_t pub_bytes[CURVE448_KEY_SIZE];
194
195
0
    CF_CHECK_EQ(pub.Set(op.pub.first.ToString(ds)), true);
196
0
    CF_CHECK_TRUE(pub.ToBin(pub_bytes, sizeof(pub_bytes)));
197
198
0
    haveAllocFailure = false;
199
0
    ret = wc_curve448_check_public(pub_bytes, sizeof(pub_bytes), EC448_BIG_ENDIAN) == 0;
200
0
    if ( *ret == false && haveAllocFailure == true ) {
201
0
        ret = std::nullopt;
202
0
    }
203
204
0
end:
205
206
0
    wolfCrypt_detail::UnsetGlobalDs();
207
0
    return ret;
208
0
}
209
210
2.08k
std::optional<component::ECDSA_Signature> OpECDSA_Sign_ed448(operation::ECDSA_Sign& op) {
211
2.08k
    std::optional<component::ECDSA_Signature> ret = std::nullopt;
212
2.08k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
213
2.08k
    wolfCrypt_detail::SetGlobalDs(&ds);
214
215
2.08k
    ed448_key key;
216
2.08k
    bool e448_key_inited = false;
217
2.08k
    uint8_t sig[ED448_SIG_SIZE];
218
2.08k
    word32 sigSz = sizeof(sig);
219
220
2.08k
    WC_CHECK_EQ(wc_ed448_init(&key), 0);
221
2.08k
    e448_key_inited = true;
222
223
2.08k
    CF_CHECK_EQ(wolfCrypt_detail::ed448LoadPrivateKey(key, op.priv, ds), true);
224
2.01k
    CF_CHECK_EQ(wolfCrypt_detail::ed448DerivePublicKey(key), true);
225
226
    /* Sign message */
227
1.95k
    WC_CHECK_EQ(wc_ed448_sign_msg(op.cleartext.GetPtr(), op.cleartext.GetSize(), sig, &sigSz, &key, nullptr, 0), MP_OKAY);
228
1.80k
    CF_CHECK_EQ(sigSz, ED448_SIG_SIZE);
229
1.80k
    static_assert(ED448_SIG_SIZE % 2 == 0);
230
231
1.80k
    {
232
1.80k
        std::optional<component::BignumPair> ret_sig;
233
1.80k
        std::optional<component::BignumPair> ret_pub;
234
235
1.80k
        CF_CHECK_NE(ret_sig = wolfCrypt_bignum::Bignum::BinToBignumPair(ds, sig, ED448_SIG_SIZE), std::nullopt);
236
1.75k
        CF_CHECK_NE(ret_pub = wolfCrypt_detail::ed448GetPublicKey(key, ds), std::nullopt);
237
238
1.71k
        ret = component::ECDSA_Signature(*ret_sig, *ret_pub);
239
1.71k
    }
240
241
2.08k
end:
242
2.08k
    if ( e448_key_inited == true ) {
243
2.08k
        wc_ed448_free(&key);
244
2.08k
    }
245
246
2.08k
    wolfCrypt_detail::UnsetGlobalDs();
247
2.08k
    return ret;
248
1.71k
}
249
250
2.95k
std::optional<bool> OpECDSA_Verify_ed448(operation::ECDSA_Verify& op) {
251
2.95k
    std::optional<bool> ret = std::nullopt;
252
2.95k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
253
2.95k
    wolfCrypt_detail::SetGlobalDs(&ds);
254
255
2.95k
    ed448_key key;
256
2.95k
    bool e448_key_inited = false;
257
2.95k
    uint8_t ed448sig[ED448_SIG_SIZE];
258
2.95k
    int verify;
259
2.95k
    bool oneShot = true;
260
2.95k
    bool haveEmptyPart = false;
261
262
2.95k
    haveAllocFailure = false;
263
2.95k
    ret = false;
264
265
2.95k
    WC_CHECK_EQ(wc_ed448_init(&key), 0);
266
2.95k
    e448_key_inited = true;
267
268
2.95k
    CF_CHECK_EQ(ed448LoadPublicKey(key, op.signature.pub.first, ds, true), true);
269
2.73k
    CF_CHECK_EQ(wolfCrypt_bignum::Bignum::ToBin(ds, op.signature.signature, ed448sig, sizeof(ed448sig), true), true);
270
271
2.61k
#if defined(WOLFSSL_ED25519_STREAMING_VERIFY)
272
2.61k
    try { oneShot = ds.Get<bool>(); } catch ( ... ) { }
273
2.61k
#endif
274
275
2.61k
    if ( oneShot == true ) {
276
1.85k
        WC_CHECK_EQ(wc_ed448_verify_msg(ed448sig, sizeof(ed448sig), op.cleartext.GetPtr(), op.cleartext.GetSize(), &verify, &key, nullptr, 0), 0);
277
1.16k
    } else {
278
#if !defined(WOLFSSL_ED25519_STREAMING_VERIFY)
279
        CF_UNREACHABLE();
280
#else
281
759
        const auto parts = util::ToParts(ds, op.cleartext);
282
283
759
        WC_CHECK_EQ(wc_ed448_verify_msg_init(ed448sig, sizeof(ed448sig), &key, (byte)Ed448, nullptr, 0), 0);
284
285
201k
        for (const auto& part : parts) {
286
201k
            if ( part.second == 0 ) {
287
199k
                haveEmptyPart = true;
288
199k
            }
289
201k
            WC_CHECK_EQ(wc_ed448_verify_msg_update(part.first, part.second, &key), 0);
290
201k
        }
291
292
1.17k
        WC_CHECK_EQ(wc_ed448_verify_msg_final(ed448sig, sizeof(ed448sig), &verify, &key), 0);
293
1.17k
#endif
294
1.17k
    }
295
296
1.59k
    ret = verify ? true : false;
297
298
2.95k
end:
299
2.95k
    if ( e448_key_inited == true ) {
300
2.95k
        wc_ed448_free(&key);
301
2.95k
    }
302
303
2.95k
    wolfCrypt_detail::UnsetGlobalDs();
304
305
2.95k
    if ( ret && *ret == false ) {
306
1.35k
        if ( haveAllocFailure ) {
307
306
            ret = std::nullopt;
308
1.04k
        } else if ( haveEmptyPart ) {
309
177
            ret = std::nullopt;
310
871
        } else if ( op.cleartext.IsZero() ) {
311
87
            ret = std::nullopt;
312
87
        }
313
314
1.35k
    }
315
2.95k
    return ret;
316
1.59k
}
317
318
} /* namespace wolfCrypt_detail */
319
} /* namespace module */
320
} /* namespace cryptofuzz */