Coverage Report

Created: 2025-07-23 06:59

/src/cryptofuzz-sp-math/modules/wolfcrypt/ecdsa_25519.cpp
Line
Count
Source (jump to first uncovered line)
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.08k
static bool ed25519LoadPrivateKey(ed25519_key& key, component::Bignum priv, Datasource& ds) {
22
1.08k
    bool ret = false;
23
24
1.08k
    uint8_t priv_bytes[ED25519_KEY_SIZE];
25
26
1.08k
    CF_CHECK_EQ(wolfCrypt_bignum::Bignum::ToBin(ds, priv, priv_bytes, sizeof(priv_bytes)), true);
27
945
    CF_CHECK_EQ(wc_ed25519_import_private_only(priv_bytes, sizeof(priv_bytes), &key), 0);
28
29
945
    ret = true;
30
1.08k
end:
31
1.08k
    return ret;
32
945
}
33
34
1.58k
static bool ed25519LoadPublicKey(ed25519_key& key, component::Bignum pub, Datasource& ds, const bool mustSucceed = false) {
35
1.58k
    bool ret = false;
36
37
1.58k
    uint8_t pub_bytes[ED25519_PUB_KEY_SIZE];
38
1.58k
    CF_CHECK_EQ(wolfCrypt_bignum::Bignum::ToBin(ds, pub, pub_bytes, sizeof(pub_bytes), mustSucceed), true);
39
1.54k
    CF_CHECK_EQ(wc_ed25519_import_public(pub_bytes, sizeof(pub_bytes), &key), 0);
40
41
1.46k
    ret = true;
42
1.58k
end:
43
1.58k
    return ret;
44
1.46k
}
45
46
1.58k
static std::optional<std::vector<uint8_t>> ed25519GetPublicKeyAsVector(ed25519_key& key) {
47
1.58k
    std::optional<std::vector<uint8_t>> ret = std::nullopt;
48
1.58k
    uint8_t pub_bytes[ED25519_PUB_KEY_SIZE];
49
50
1.58k
    WC_CHECK_EQ(wc_ed25519_make_public(&key, pub_bytes, sizeof(pub_bytes)), MP_OKAY);
51
52
1.52k
    ret = std::vector<uint8_t>(pub_bytes, pub_bytes + sizeof(pub_bytes));
53
1.58k
end:
54
1.58k
    return ret;
55
1.52k
}
56
57
737
static std::optional<component::ECC_PublicKey> ed25519GetPublicKey(ed25519_key& key, Datasource& ds) {
58
737
    std::optional<component::ECC_PublicKey> ret = std::nullopt;
59
737
    std::optional<std::vector<uint8_t>> pubv = std::nullopt;
60
737
    std::optional<component::Bignum> pub = std::nullopt;
61
62
737
    CF_CHECK_NE(pubv = ed25519GetPublicKeyAsVector(key), std::nullopt);
63
710
    CF_CHECK_NE(pub = wolfCrypt_bignum::Bignum::BinToBignum(ds, pubv->data(), pubv->size()), std::nullopt);
64
65
687
    ret = {pub->ToString(), "0"};
66
67
737
end:
68
737
    return ret;
69
687
}
70
71
847
static bool ed25519DerivePublicKey(ed25519_key& key) {
72
847
    std::optional<std::vector<uint8_t>> pubv = std::nullopt;
73
847
    bool ret = false;
74
75
847
    CF_CHECK_NE(pubv = ed25519GetPublicKeyAsVector(key), std::nullopt);
76
812
    memcpy(key.p, pubv->data(), ED25519_PUB_KEY_SIZE);
77
812
    key.pubKeySet = 1;
78
79
812
    ret = true;
80
81
847
end:
82
847
    return ret;
83
812
}
84
85
477
std::optional<component::ECC_PublicKey> OpECC_PrivateToPublic_Curve25519(operation::ECC_PrivateToPublic& op) {
86
477
    std::optional<component::ECC_PublicKey> ret = std::nullopt;
87
477
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
88
477
    wolfCrypt_detail::SetGlobalDs(&ds);
89
90
477
    wolfCrypt_bignum::Bignum priv(ds), pub(ds);
91
477
    uint8_t pub_bytes[CURVE25519_KEYSIZE];
92
477
    uint8_t priv_bytes[CURVE25519_KEYSIZE];
93
94
    /* Load private key */
95
477
    {
96
477
        CF_CHECK_EQ(priv.Set(op.priv.ToString(ds)), true);
97
412
        CF_CHECK_TRUE(priv.ToBin(priv_bytes, sizeof(priv_bytes)));
98
352
        priv_bytes[0] &= 248;
99
352
        priv_bytes[31] &= 127;
100
352
        priv_bytes[31] |= 64;
101
352
    }
102
103
    /* Convert to public key */
104
0
    {
105
352
        WC_CHECK_EQ(wc_curve25519_make_pub(sizeof(pub_bytes), pub_bytes, sizeof(priv_bytes), priv_bytes), MP_OKAY);
106
279
    }
107
108
    /* Convert public key */
109
0
    {
110
279
        std::optional<std::string> pub_x_str;
111
279
        CF_CHECK_EQ(mp_read_unsigned_bin(pub.GetPtr(), pub_bytes, sizeof(pub_bytes)), MP_OKAY);
112
251
        CF_CHECK_NE(pub_x_str = pub.ToDecString(), std::nullopt);
113
251
        ret = { *pub_x_str, "0" };
114
251
    }
115
116
477
end:
117
477
    wolfCrypt_detail::UnsetGlobalDs();
118
477
    return ret;
119
251
}
120
121
169
std::optional<component::ECC_PublicKey> OpECC_PrivateToPublic_Ed25519(operation::ECC_PrivateToPublic& op) {
122
169
    std::optional<component::ECC_PublicKey> ret = std::nullopt;
123
169
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
124
169
    wolfCrypt_detail::SetGlobalDs(&ds);
125
126
169
    wolfCrypt_bignum::Bignum priv(ds), pub(ds);
127
128
169
    ed25519_key key;
129
169
    bool e25519_key_inited = false;
130
131
169
    WC_CHECK_EQ(wc_ed25519_init(&key), 0);
132
169
    e25519_key_inited = true;
133
134
169
    CF_CHECK_EQ(ed25519LoadPrivateKey(key, op.priv, ds), true);
135
98
    ret = ed25519GetPublicKey(key, ds);
136
137
169
end:
138
169
    if ( e25519_key_inited == true ) {
139
169
        wc_ed25519_free(&key);
140
169
    }
141
142
169
    wolfCrypt_detail::UnsetGlobalDs();
143
169
    return ret;
144
98
}
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
919
std::optional<component::ECDSA_Signature> OpECDSA_Sign_ed25519(operation::ECDSA_Sign& op) {
198
919
    std::optional<component::ECDSA_Signature> ret = std::nullopt;
199
919
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
200
919
    wolfCrypt_detail::SetGlobalDs(&ds);
201
202
919
    ed25519_key key;
203
919
    bool e25519_key_inited = false;
204
919
    uint8_t sig[ED25519_SIG_SIZE];
205
919
    word32 sigSz = sizeof(sig);
206
207
919
    WC_CHECK_EQ(wc_ed25519_init(&key), 0);
208
919
    e25519_key_inited = true;
209
210
919
    CF_CHECK_EQ(wolfCrypt_detail::ed25519LoadPrivateKey(key, op.priv, ds), true);
211
847
    CF_CHECK_EQ(wolfCrypt_detail::ed25519DerivePublicKey(key), true);
212
213
    /* Sign message */
214
812
    WC_CHECK_EQ(wc_ed25519_sign_msg(op.cleartext.GetPtr(), op.cleartext.GetSize(), sig, &sigSz, &key), MP_OKAY);
215
703
    CF_CHECK_EQ(sigSz, ED25519_SIG_SIZE);
216
703
    static_assert(ED25519_SIG_SIZE % 2 == 0);
217
218
703
    {
219
703
        std::optional<component::BignumPair> ret_sig;
220
703
        std::optional<component::BignumPair> ret_pub;
221
222
703
        CF_CHECK_NE(ret_sig = wolfCrypt_bignum::Bignum::BinToBignumPair(ds, sig, ED25519_SIG_SIZE), std::nullopt);
223
639
        CF_CHECK_NE(ret_pub = wolfCrypt_detail::ed25519GetPublicKey(key, ds), std::nullopt);
224
225
631
        ret = component::ECDSA_Signature(*ret_sig, *ret_pub);
226
631
    }
227
228
919
end:
229
919
    if ( e25519_key_inited == true ) {
230
919
        wc_ed25519_free(&key);
231
919
    }
232
233
919
    wolfCrypt_detail::UnsetGlobalDs();
234
919
    return ret;
235
631
}
236
237
1.58k
std::optional<bool> OpECDSA_Verify_ed25519(operation::ECDSA_Verify& op) {
238
1.58k
    std::optional<bool> ret = std::nullopt;
239
1.58k
    Datasource ds(op.modifier.GetPtr(), op.modifier.GetSize());
240
1.58k
    wolfCrypt_detail::SetGlobalDs(&ds);
241
242
1.58k
    ed25519_key key;
243
1.58k
    bool e25519_key_inited = false;
244
1.58k
    uint8_t ed25519sig[ED25519_SIG_SIZE];
245
1.58k
    int verify;
246
1.58k
    bool oneShot = true;
247
1.58k
    bool haveEmptyPart = false;
248
249
1.58k
    haveAllocFailure = false;
250
1.58k
    ret = false;
251
252
1.58k
    WC_CHECK_EQ(wc_ed25519_init(&key), 0);
253
1.58k
    e25519_key_inited = true;
254
255
1.58k
    CF_CHECK_EQ(ed25519LoadPublicKey(key, op.signature.pub.first, ds, true), true);
256
1.46k
    CF_CHECK_EQ(wolfCrypt_bignum::Bignum::ToBin(ds, op.signature.signature, ed25519sig, sizeof(ed25519sig), true), true);
257
258
1.39k
#if defined(WOLFSSL_ED25519_STREAMING_VERIFY)
259
1.39k
    try { oneShot = ds.Get<bool>(); } catch ( ... ) { }
260
1.39k
#endif
261
262
1.39k
    if ( oneShot == true ) {
263
886
        WC_CHECK_EQ(wc_ed25519_verify_msg(ed25519sig, sizeof(ed25519sig), op.cleartext.GetPtr(), op.cleartext.GetSize(), &verify, &key), 0);
264
508
    } else {
265
#if !defined(WOLFSSL_ED25519_STREAMING_VERIFY)
266
        CF_UNREACHABLE();
267
#else
268
508
        const auto parts = util::ToParts(ds, op.cleartext);
269
270
508
        WC_CHECK_EQ(wc_ed25519_verify_msg_init(ed25519sig, sizeof(ed25519sig), &key, (byte)Ed25519, nullptr, 0), 0);
271
272
28.5k
        for (const auto& part : parts) {
273
28.5k
            if ( part.second == 0 ) {
274
27.1k
                haveEmptyPart = true;
275
27.1k
            }
276
28.5k
            WC_CHECK_EQ(wc_ed25519_verify_msg_update(part.first, part.second, &key), 0);
277
28.5k
        }
278
279
662
        WC_CHECK_EQ(wc_ed25519_verify_msg_final(ed25519sig, sizeof(ed25519sig), &verify, &key), 0);
280
662
#endif
281
662
    }
282
283
627
    ret = verify ? true : false;
284
285
1.58k
end:
286
1.58k
    if ( e25519_key_inited == true ) {
287
1.58k
        wc_ed25519_free(&key);
288
1.58k
    }
289
290
1.58k
    wolfCrypt_detail::UnsetGlobalDs();
291
292
1.58k
    if ( ret && *ret == false ) {
293
957
        if ( haveAllocFailure ) {
294
263
            ret = std::nullopt;
295
694
        } else if ( haveEmptyPart ) {
296
136
            ret = std::nullopt;
297
558
        } else if ( op.cleartext.IsZero() ) {
298
56
            ret = std::nullopt;
299
56
        }
300
301
957
    }
302
1.58k
    return ret;
303
627
}
304
305
} /* namespace wolfCrypt_detail */
306
} /* namespace module */
307
} /* namespace cryptofuzz */