Coverage Report

Created: 2025-11-16 07:15

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cryptofuzz-normal-math/modules/wolfcrypt/ecdsa_25519.cpp
Line
Count
Source
1
#include "ecdsa_25519.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/curve25519.h>
10
#include <wolfssl/wolfcrypt/ed25519.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
1.99k
static bool ed25519LoadPrivateKey(ed25519_key& key, component::Bignum priv, Datasource& ds) {
22
1.99k
    bool ret = false;
23
24
1.99k
    uint8_t priv_bytes[ED25519_KEY_SIZE];
25
26
1.99k
    CF_CHECK_EQ(wolfCrypt_bignum::Bignum::ToBin(ds, priv, priv_bytes, sizeof(priv_bytes)), true);
27
1.77k
    CF_CHECK_EQ(wc_ed25519_import_private_only(priv_bytes, sizeof(priv_bytes), &key), 0);
28
29
1.77k
    ret = true;
30
1.99k
end:
31
1.99k
    return ret;
32
1.77k
}
33
34
2.87k
static bool ed25519LoadPublicKey(ed25519_key& key, component::Bignum pub, Datasource& ds, const bool mustSucceed = false) {
35
2.87k
    bool ret = false;
36
37
2.87k
    uint8_t pub_bytes[ED25519_PUB_KEY_SIZE];
38
2.87k
    CF_CHECK_EQ(wolfCrypt_bignum::Bignum::ToBin(ds, pub, pub_bytes, sizeof(pub_bytes), mustSucceed), true);
39
2.80k
    CF_CHECK_EQ(wc_ed25519_import_public(pub_bytes, sizeof(pub_bytes), &key), 0);
40
41
2.68k
    ret = true;
42
2.87k
end:
43
2.87k
    return ret;
44
2.68k
}
45
46
2.98k
static std::optional<std::vector<uint8_t>> ed25519GetPublicKeyAsVector(ed25519_key& key) {
47
2.98k
    std::optional<std::vector<uint8_t>> ret = std::nullopt;
48
2.98k
    uint8_t pub_bytes[ED25519_PUB_KEY_SIZE];
49
50
2.98k
    WC_CHECK_EQ(wc_ed25519_make_public(&key, pub_bytes, sizeof(pub_bytes)), MP_OKAY);
51
52
2.85k
    ret = std::vector<uint8_t>(pub_bytes, pub_bytes + sizeof(pub_bytes));
53
2.98k
end:
54
2.98k
    return ret;
55
2.85k
}
56
57
1.43k
static std::optional<component::ECC_PublicKey> ed25519GetPublicKey(ed25519_key& key, Datasource& ds) {
58
1.43k
    std::optional<component::ECC_PublicKey> ret = std::nullopt;
59
1.43k
    std::optional<std::vector<uint8_t>> pubv = std::nullopt;
60
1.43k
    std::optional<component::Bignum> pub = std::nullopt;
61
62
1.43k
    CF_CHECK_NE(pubv = ed25519GetPublicKeyAsVector(key), std::nullopt);
63
1.37k
    CF_CHECK_NE(pub = wolfCrypt_bignum::Bignum::BinToBignum(ds, pubv->data(), pubv->size()), std::nullopt);
64
65
1.33k
    ret = {pub->ToString(), "0"};
66
67
1.43k
end:
68
1.43k
    return ret;
69
1.33k
}
70
71
1.54k
static bool ed25519DerivePublicKey(ed25519_key& key) {
72
1.54k
    std::optional<std::vector<uint8_t>> pubv = std::nullopt;
73
1.54k
    bool ret = false;
74
75
1.54k
    CF_CHECK_NE(pubv = ed25519GetPublicKeyAsVector(key), std::nullopt);
76
1.48k
    memcpy(key.p, pubv->data(), ED25519_PUB_KEY_SIZE);
77
1.48k
    key.pubKeySet = 1;
78
79
1.48k
    ret = true;
80
81
1.54k
end:
82
1.54k
    return ret;
83
1.48k
}
84
85
972
std::optional<component::ECC_PublicKey> OpECC_PrivateToPublic_Curve25519(operation::ECC_PrivateToPublic& op) {
86
972
    std::optional<component::ECC_PublicKey> ret = std::nullopt;
87
972
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
88
972
    wolfCrypt_detail::SetGlobalDs(&ds);
89
90
972
    wolfCrypt_bignum::Bignum priv(ds), pub(ds);
91
972
    uint8_t pub_bytes[CURVE25519_KEYSIZE];
92
972
    uint8_t priv_bytes[CURVE25519_KEYSIZE];
93
94
    /* Load private key */
95
972
    {
96
972
        CF_CHECK_EQ(priv.Set(op.priv.ToString(ds)), true);
97
872
        CF_CHECK_TRUE(priv.ToBin(priv_bytes, sizeof(priv_bytes)));
98
766
        priv_bytes[0] &= 248;
99
766
        priv_bytes[31] &= 127;
100
766
        priv_bytes[31] |= 64;
101
766
    }
102
103
    /* Convert to public key */
104
0
    {
105
766
        WC_CHECK_EQ(wc_curve25519_make_pub(sizeof(pub_bytes), pub_bytes, sizeof(priv_bytes), priv_bytes), MP_OKAY);
106
531
    }
107
108
    /* Convert public key */
109
0
    {
110
531
        std::optional<std::string> pub_x_str;
111
531
        CF_CHECK_EQ(mp_read_unsigned_bin(pub.GetPtr(), pub_bytes, sizeof(pub_bytes)), MP_OKAY);
112
479
        CF_CHECK_NE(pub_x_str = pub.ToDecString(), std::nullopt);
113
475
        ret = { *pub_x_str, "0" };
114
475
    }
115
116
972
end:
117
972
    wolfCrypt_detail::UnsetGlobalDs();
118
972
    return ret;
119
475
}
120
121
348
std::optional<component::ECC_PublicKey> OpECC_PrivateToPublic_Ed25519(operation::ECC_PrivateToPublic& op) {
122
348
    std::optional<component::ECC_PublicKey> ret = std::nullopt;
123
348
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
124
348
    wolfCrypt_detail::SetGlobalDs(&ds);
125
126
348
    wolfCrypt_bignum::Bignum priv(ds), pub(ds);
127
128
348
    ed25519_key key;
129
348
    bool e25519_key_inited = false;
130
131
348
    WC_CHECK_EQ(wc_ed25519_init(&key), 0);
132
348
    e25519_key_inited = true;
133
134
348
    CF_CHECK_EQ(ed25519LoadPrivateKey(key, op.priv, ds), true);
135
224
    ret = ed25519GetPublicKey(key, ds);
136
137
348
end:
138
348
    if ( e25519_key_inited == true ) {
139
348
        wc_ed25519_free(&key);
140
348
    }
141
142
348
    wolfCrypt_detail::UnsetGlobalDs();
143
348
    return ret;
144
224
}
145
146
0
std::optional<bool> OpECC_ValidatePubkey_Curve25519(operation::ECC_ValidatePubkey& op) {
147
0
    std::optional<bool> ret = std::nullopt;
148
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
149
0
    wolfCrypt_detail::SetGlobalDs(&ds);
150
151
0
    wolfCrypt_bignum::Bignum pub(ds);
152
0
    uint8_t pub_bytes[CURVE25519_KEYSIZE];
153
154
0
    CF_CHECK_EQ(pub.Set(op.pub.first.ToString(ds)), true);
155
0
    CF_CHECK_TRUE(pub.ToBin(pub_bytes, sizeof(pub_bytes)));
156
157
0
    haveAllocFailure = false;
158
0
    ret = wc_curve25519_check_public(pub_bytes, sizeof(pub_bytes), EC25519_BIG_ENDIAN) == 0;
159
0
    if ( *ret == false && haveAllocFailure == true ) {
160
0
        ret = std::nullopt;
161
0
    }
162
163
0
end:
164
165
0
    wolfCrypt_detail::UnsetGlobalDs();
166
0
    return ret;
167
0
}
168
169
0
std::optional<bool> OpECC_ValidatePubkey_Ed25519(operation::ECC_ValidatePubkey& op) {
170
0
    std::optional<bool> ret = std::nullopt;
171
0
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
172
0
    wolfCrypt_detail::SetGlobalDs(&ds);
173
174
0
    ed25519_key key;
175
0
    bool e25519_key_inited = false;
176
177
0
    WC_CHECK_EQ(wc_ed25519_init(&key), 0);
178
0
    e25519_key_inited = true;
179
180
0
    CF_CHECK_EQ(ed25519LoadPublicKey(key, op.pub.first, ds), true);
181
182
0
    haveAllocFailure = false;
183
0
    ret = wc_ed25519_check_key(&key) == 0;
184
0
    if ( *ret == false && haveAllocFailure == true ) {
185
0
        ret = std::nullopt;
186
0
    }
187
188
0
end:
189
0
    if ( e25519_key_inited == true ) {
190
0
        wc_ed25519_free(&key);
191
0
    }
192
193
0
    wolfCrypt_detail::UnsetGlobalDs();
194
0
    return ret;
195
0
}
196
197
1.64k
std::optional<component::ECDSA_Signature> OpECDSA_Sign_ed25519(operation::ECDSA_Sign& op) {
198
1.64k
    std::optional<component::ECDSA_Signature> ret = std::nullopt;
199
1.64k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
200
1.64k
    wolfCrypt_detail::SetGlobalDs(&ds);
201
202
1.64k
    ed25519_key key;
203
1.64k
    bool e25519_key_inited = false;
204
1.64k
    uint8_t sig[ED25519_SIG_SIZE];
205
1.64k
    word32 sigSz = sizeof(sig);
206
207
1.64k
    WC_CHECK_EQ(wc_ed25519_init(&key), 0);
208
1.64k
    e25519_key_inited = true;
209
210
1.64k
    CF_CHECK_EQ(wolfCrypt_detail::ed25519LoadPrivateKey(key, op.priv, ds), true);
211
1.54k
    CF_CHECK_EQ(wolfCrypt_detail::ed25519DerivePublicKey(key), true);
212
213
    /* Sign message */
214
1.48k
    WC_CHECK_EQ(wc_ed25519_sign_msg(op.cleartext.GetPtr(), op.cleartext.GetSize(), sig, &sigSz, &key), MP_OKAY);
215
1.30k
    CF_CHECK_EQ(sigSz, ED25519_SIG_SIZE);
216
1.30k
    static_assert(ED25519_SIG_SIZE % 2 == 0);
217
218
1.30k
    {
219
1.30k
        std::optional<component::BignumPair> ret_sig;
220
1.30k
        std::optional<component::BignumPair> ret_pub;
221
222
1.30k
        CF_CHECK_NE(ret_sig = wolfCrypt_bignum::Bignum::BinToBignumPair(ds, sig, ED25519_SIG_SIZE), std::nullopt);
223
1.20k
        CF_CHECK_NE(ret_pub = wolfCrypt_detail::ed25519GetPublicKey(key, ds), std::nullopt);
224
225
1.19k
        ret = component::ECDSA_Signature(*ret_sig, *ret_pub);
226
1.19k
    }
227
228
1.64k
end:
229
1.64k
    if ( e25519_key_inited == true ) {
230
1.64k
        wc_ed25519_free(&key);
231
1.64k
    }
232
233
1.64k
    wolfCrypt_detail::UnsetGlobalDs();
234
1.64k
    return ret;
235
1.19k
}
236
237
2.87k
std::optional<bool> OpECDSA_Verify_ed25519(operation::ECDSA_Verify& op) {
238
2.87k
    std::optional<bool> ret = std::nullopt;
239
2.87k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
240
2.87k
    wolfCrypt_detail::SetGlobalDs(&ds);
241
242
2.87k
    ed25519_key key;
243
2.87k
    bool e25519_key_inited = false;
244
2.87k
    uint8_t ed25519sig[ED25519_SIG_SIZE];
245
2.87k
    int verify;
246
2.87k
    bool oneShot = true;
247
2.87k
    bool haveEmptyPart = false;
248
249
2.87k
    haveAllocFailure = false;
250
2.87k
    ret = false;
251
252
2.87k
    WC_CHECK_EQ(wc_ed25519_init(&key), 0);
253
2.87k
    e25519_key_inited = true;
254
255
2.87k
    CF_CHECK_EQ(ed25519LoadPublicKey(key, op.signature.pub.first, ds, true), true);
256
2.68k
    CF_CHECK_EQ(wolfCrypt_bignum::Bignum::ToBin(ds, op.signature.signature, ed25519sig, sizeof(ed25519sig), true), true);
257
258
2.54k
#if defined(WOLFSSL_ED25519_STREAMING_VERIFY)
259
2.54k
    try { oneShot = ds.Get<bool>(); } catch ( ... ) { }
260
2.54k
#endif
261
262
2.54k
    if ( oneShot == true ) {
263
1.74k
        WC_CHECK_EQ(wc_ed25519_verify_msg(ed25519sig, sizeof(ed25519sig), op.cleartext.GetPtr(), op.cleartext.GetSize(), &verify, &key), 0);
264
796
    } else {
265
#if !defined(WOLFSSL_ED25519_STREAMING_VERIFY)
266
        CF_UNREACHABLE();
267
#else
268
796
        const auto parts = util::ToParts(ds, op.cleartext);
269
270
796
        WC_CHECK_EQ(wc_ed25519_verify_msg_init(ed25519sig, sizeof(ed25519sig), &key, (byte)Ed25519, nullptr, 0), 0);
271
272
119k
        for (const auto& part : parts) {
273
119k
            if ( part.second == 0 ) {
274
117k
                haveEmptyPart = true;
275
117k
            }
276
119k
            WC_CHECK_EQ(wc_ed25519_verify_msg_update(part.first, part.second, &key), 0);
277
119k
        }
278
279
1.02k
        WC_CHECK_EQ(wc_ed25519_verify_msg_final(ed25519sig, sizeof(ed25519sig), &verify, &key), 0);
280
1.02k
#endif
281
1.02k
    }
282
283
1.15k
    ret = verify ? true : false;
284
285
2.87k
end:
286
2.87k
    if ( e25519_key_inited == true ) {
287
2.87k
        wc_ed25519_free(&key);
288
2.87k
    }
289
290
2.87k
    wolfCrypt_detail::UnsetGlobalDs();
291
292
2.87k
    if ( ret && *ret == false ) {
293
1.72k
        if ( haveAllocFailure ) {
294
466
            ret = std::nullopt;
295
1.25k
        } else if ( haveEmptyPart ) {
296
217
            ret = std::nullopt;
297
1.04k
        } else if ( op.cleartext.IsZero() ) {
298
107
            ret = std::nullopt;
299
107
        }
300
301
1.72k
    }
302
2.87k
    return ret;
303
1.15k
}
304
305
} /* namespace wolfCrypt_detail */
306
} /* namespace module */
307
} /* namespace cryptofuzz */