Coverage Report

Created: 2026-02-14 07:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cryptofuzz-openssl-api/modules/wolfcrypt-openssl/module_internal.h
Line
Count
Source
1
#pragma once
2
3
namespace cryptofuzz {
4
namespace module {
5
6
template <class T>
7
class CTX_Copier {
8
    private:
9
        T* ctx = nullptr;
10
        Datasource& ds;
11
12
        T* newCTX(void) const;
13
        int copyCTX(T* dest, T* src) const;
14
        void freeCTX(T* ctx) const;
15
16
106k
        T* copy(void) {
17
106k
            return ctx;
18
0
            bool doCopyCTX = true;
19
0
            try {
20
0
                doCopyCTX = ds.Get<bool>();
21
0
            } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
22
23
0
            if ( doCopyCTX == true ) {
24
0
                T* tmpCtx = newCTX();
25
0
                if ( tmpCtx != nullptr ) {
26
0
                    if ( copyCTX(tmpCtx, ctx) == 1 ) {
27
                        /* Copy succeeded, free the old ctx */
28
0
                        freeCTX(ctx);
29
30
                        /* Use the copied ctx */
31
0
                        ctx = tmpCtx;
32
0
                    } else {
33
0
                        freeCTX(tmpCtx);
34
0
                    }
35
0
                }
36
0
            }
37
38
0
            return ctx;
39
0
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_EVP_MD_CTX>::copy()
Line
Count
Source
16
41.9k
        T* copy(void) {
17
41.9k
            return ctx;
18
0
            bool doCopyCTX = true;
19
0
            try {
20
0
                doCopyCTX = ds.Get<bool>();
21
0
            } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
22
23
0
            if ( doCopyCTX == true ) {
24
0
                T* tmpCtx = newCTX();
25
0
                if ( tmpCtx != nullptr ) {
26
0
                    if ( copyCTX(tmpCtx, ctx) == 1 ) {
27
                        /* Copy succeeded, free the old ctx */
28
0
                        freeCTX(ctx);
29
30
                        /* Use the copied ctx */
31
0
                        ctx = tmpCtx;
32
0
                    } else {
33
0
                        freeCTX(tmpCtx);
34
0
                    }
35
0
                }
36
0
            }
37
38
0
            return ctx;
39
0
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_HMAC_CTX>::copy()
Line
Count
Source
16
2.38k
        T* copy(void) {
17
2.38k
            return ctx;
18
0
            bool doCopyCTX = true;
19
0
            try {
20
0
                doCopyCTX = ds.Get<bool>();
21
0
            } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
22
23
0
            if ( doCopyCTX == true ) {
24
0
                T* tmpCtx = newCTX();
25
0
                if ( tmpCtx != nullptr ) {
26
0
                    if ( copyCTX(tmpCtx, ctx) == 1 ) {
27
                        /* Copy succeeded, free the old ctx */
28
0
                        freeCTX(ctx);
29
30
                        /* Use the copied ctx */
31
0
                        ctx = tmpCtx;
32
0
                    } else {
33
0
                        freeCTX(tmpCtx);
34
0
                    }
35
0
                }
36
0
            }
37
38
0
            return ctx;
39
0
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_EVP_CIPHER_CTX>::copy()
Line
Count
Source
16
54.1k
        T* copy(void) {
17
54.1k
            return ctx;
18
0
            bool doCopyCTX = true;
19
0
            try {
20
0
                doCopyCTX = ds.Get<bool>();
21
0
            } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
22
23
0
            if ( doCopyCTX == true ) {
24
0
                T* tmpCtx = newCTX();
25
0
                if ( tmpCtx != nullptr ) {
26
0
                    if ( copyCTX(tmpCtx, ctx) == 1 ) {
27
                        /* Copy succeeded, free the old ctx */
28
0
                        freeCTX(ctx);
29
30
                        /* Use the copied ctx */
31
0
                        ctx = tmpCtx;
32
0
                    } else {
33
0
                        freeCTX(tmpCtx);
34
0
                    }
35
0
                }
36
0
            }
37
38
0
            return ctx;
39
0
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_CMAC_CTX>::copy()
Line
Count
Source
16
2.55k
        T* copy(void) {
17
2.55k
            return ctx;
18
0
            bool doCopyCTX = true;
19
0
            try {
20
0
                doCopyCTX = ds.Get<bool>();
21
0
            } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
22
23
0
            if ( doCopyCTX == true ) {
24
0
                T* tmpCtx = newCTX();
25
0
                if ( tmpCtx != nullptr ) {
26
0
                    if ( copyCTX(tmpCtx, ctx) == 1 ) {
27
                        /* Copy succeeded, free the old ctx */
28
0
                        freeCTX(ctx);
29
30
                        /* Use the copied ctx */
31
0
                        ctx = tmpCtx;
32
0
                    } else {
33
0
                        freeCTX(tmpCtx);
34
0
                    }
35
0
                }
36
0
            }
37
38
0
            return ctx;
39
0
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_EC_KEY>::copy()
Line
Count
Source
16
5.83k
        T* copy(void) {
17
5.83k
            return ctx;
18
0
            bool doCopyCTX = true;
19
0
            try {
20
0
                doCopyCTX = ds.Get<bool>();
21
0
            } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
22
23
0
            if ( doCopyCTX == true ) {
24
0
                T* tmpCtx = newCTX();
25
0
                if ( tmpCtx != nullptr ) {
26
0
                    if ( copyCTX(tmpCtx, ctx) == 1 ) {
27
                        /* Copy succeeded, free the old ctx */
28
0
                        freeCTX(ctx);
29
30
                        /* Use the copied ctx */
31
0
                        ctx = tmpCtx;
32
0
                    } else {
33
0
                        freeCTX(tmpCtx);
34
0
                    }
35
0
                }
36
0
            }
37
38
0
            return ctx;
39
0
        }
40
41
    public:
42
        CTX_Copier(Datasource& ds) :
43
12.6k
            ds(ds) {
44
12.6k
            ctx = newCTX();
45
12.6k
            if ( ctx == nullptr ) {
46
0
                abort();
47
0
            }
48
12.6k
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_EVP_MD_CTX>::CTX_Copier(fuzzing::datasource::Datasource&)
Line
Count
Source
43
1.69k
            ds(ds) {
44
1.69k
            ctx = newCTX();
45
1.69k
            if ( ctx == nullptr ) {
46
0
                abort();
47
0
            }
48
1.69k
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_HMAC_CTX>::CTX_Copier(fuzzing::datasource::Datasource&)
Line
Count
Source
43
137
            ds(ds) {
44
137
            ctx = newCTX();
45
137
            if ( ctx == nullptr ) {
46
0
                abort();
47
0
            }
48
137
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_EVP_CIPHER_CTX>::CTX_Copier(fuzzing::datasource::Datasource&)
Line
Count
Source
43
7.08k
            ds(ds) {
44
7.08k
            ctx = newCTX();
45
7.08k
            if ( ctx == nullptr ) {
46
0
                abort();
47
0
            }
48
7.08k
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_CMAC_CTX>::CTX_Copier(fuzzing::datasource::Datasource&)
Line
Count
Source
43
1.06k
            ds(ds) {
44
1.06k
            ctx = newCTX();
45
1.06k
            if ( ctx == nullptr ) {
46
0
                abort();
47
0
            }
48
1.06k
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_EC_KEY>::CTX_Copier(fuzzing::datasource::Datasource&)
Line
Count
Source
43
2.64k
            ds(ds) {
44
2.64k
            ctx = newCTX();
45
2.64k
            if ( ctx == nullptr ) {
46
0
                abort();
47
0
            }
48
2.64k
        }
49
50
106k
        T* GetPtr(void) {
51
106k
            return copy();
52
106k
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_EVP_MD_CTX>::GetPtr()
Line
Count
Source
50
41.9k
        T* GetPtr(void) {
51
41.9k
            return copy();
52
41.9k
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_HMAC_CTX>::GetPtr()
Line
Count
Source
50
2.38k
        T* GetPtr(void) {
51
2.38k
            return copy();
52
2.38k
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_EVP_CIPHER_CTX>::GetPtr()
Line
Count
Source
50
54.1k
        T* GetPtr(void) {
51
54.1k
            return copy();
52
54.1k
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_CMAC_CTX>::GetPtr()
Line
Count
Source
50
2.55k
        T* GetPtr(void) {
51
2.55k
            return copy();
52
2.55k
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_EC_KEY>::GetPtr()
Line
Count
Source
50
5.83k
        T* GetPtr(void) {
51
5.83k
            return copy();
52
5.83k
        }
53
54
12.6k
        ~CTX_Copier() {
55
12.6k
            freeCTX(ctx);
56
12.6k
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_EVP_MD_CTX>::~CTX_Copier()
Line
Count
Source
54
1.69k
        ~CTX_Copier() {
55
1.69k
            freeCTX(ctx);
56
1.69k
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_HMAC_CTX>::~CTX_Copier()
Line
Count
Source
54
137
        ~CTX_Copier() {
55
137
            freeCTX(ctx);
56
137
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_EVP_CIPHER_CTX>::~CTX_Copier()
Line
Count
Source
54
7.08k
        ~CTX_Copier() {
55
7.08k
            freeCTX(ctx);
56
7.08k
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_CMAC_CTX>::~CTX_Copier()
Line
Count
Source
54
1.06k
        ~CTX_Copier() {
55
1.06k
            freeCTX(ctx);
56
1.06k
        }
cryptofuzz::module::CTX_Copier<WOLFSSL_EC_KEY>::~CTX_Copier()
Line
Count
Source
54
2.64k
        ~CTX_Copier() {
55
2.64k
            freeCTX(ctx);
56
2.64k
        }
57
};
58
59
class EC_GROUP_Copier {
60
    private:
61
        bool locked = false;
62
        const int curveNID;
63
        EC_GROUP* group = nullptr;
64
        Datasource& ds;
65
66
3.02k
        EC_GROUP* newGroup(void) {
67
3.02k
            return EC_GROUP_new_by_curve_name(curveNID);
68
3.02k
        }
69
70
#if !defined(CRYPTOFUZZ_BORINGSSL) && !defined(CRYPTOFUZZ_WOLFCRYPT_OPENSSL)
71
        int copyGroup(EC_GROUP* dest, EC_GROUP* src) {
72
            return EC_GROUP_copy(dest, src);
73
        }
74
#endif
75
3.02k
        void freeGroup(EC_GROUP* group) {
76
3.02k
            /* noret */ EC_GROUP_free(group);
77
3.02k
        }
78
79
0
        EC_GROUP* copy(void) {
80
0
            bool doCopyGroup = true;
81
0
            try {
82
0
                doCopyGroup = ds.Get<bool>();
83
0
            } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
84
85
0
            if ( doCopyGroup == true ) {
86
#if !defined(CRYPTOFUZZ_BORINGSSL) && !defined(CRYPTOFUZZ_WOLFCRYPT_OPENSSL)
87
                EC_GROUP* tmpGroup = newGroup();
88
                if ( tmpGroup != nullptr ) {
89
                    if ( copyGroup(tmpGroup, group) == 1 ) {
90
                        /* Copy succeeded, free the old group */
91
                        freeGroup(group);
92
93
                        /* Use the copied group */
94
                        group = tmpGroup;
95
                    } else {
96
                        freeGroup(tmpGroup);
97
                    }
98
                }
99
#endif
100
0
            }
101
102
0
            return group;
103
0
        }
104
105
    public:
106
        EC_GROUP_Copier(Datasource& ds, const int curveNID) :
107
3.02k
            curveNID(curveNID), ds(ds) {
108
3.02k
            group = newGroup();
109
3.02k
        }
110
111
3.02k
        void Lock(void) {
112
3.02k
            locked = true;
113
3.02k
        }
114
115
19.0k
        EC_GROUP* GetPtr(void) {
116
19.0k
            if ( locked == true ) {
117
19.0k
                return group;
118
19.0k
            } else {
119
0
                return copy();
120
0
            }
121
19.0k
        }
122
123
3.02k
        ~EC_GROUP_Copier() {
124
3.02k
            freeGroup(group);
125
3.02k
        }
126
};
127
128
class EC_POINT_Copier {
129
    private:
130
        std::shared_ptr<EC_GROUP_Copier> group;
131
        EC_POINT* point = nullptr;
132
        Datasource& ds;
133
134
8.74k
        EC_POINT* newPoint(void) {
135
8.74k
            return EC_POINT_new(group->GetPtr());
136
8.74k
        }
137
138
5.46k
        int copyPoint(EC_POINT* dest, EC_POINT* src) {
139
5.46k
            return EC_POINT_copy(dest, src);
140
5.46k
        }
141
8.74k
        void freePoint(EC_POINT* point) {
142
8.74k
            /* noret */ EC_POINT_free(point);
143
8.74k
        }
144
145
6.06k
        EC_POINT* copy(void) {
146
6.06k
            bool doCopyPoint = true;
147
6.06k
            try {
148
6.06k
                doCopyPoint = ds.Get<bool>();
149
6.06k
            } catch ( fuzzing::datasource::Datasource::OutOfData ) { }
150
151
6.06k
            if ( doCopyPoint == true ) {
152
5.46k
                EC_POINT* tmpPoint = newPoint();
153
5.46k
                if ( tmpPoint != nullptr ) {
154
5.46k
                    if ( copyPoint(tmpPoint, point) == 1 ) {
155
                        /* Copy succeeded, free the old point */
156
5.46k
                        freePoint(point);
157
158
                        /* Use the copied point */
159
5.46k
                        point = tmpPoint;
160
5.46k
                    } else {
161
0
                        freePoint(tmpPoint);
162
0
                    }
163
5.46k
                }
164
5.46k
            }
165
166
6.06k
            return point;
167
6.06k
        }
168
169
1.72k
        bool set(OpenSSL_bignum::Bignum& pub_x, OpenSSL_bignum::Bignum& pub_y) {
170
1.72k
            bool ret = false;
171
172
1.72k
            CF_CHECK_NE(EC_POINT_set_affine_coordinates(group->GetPtr(), GetPtr(), pub_x.GetPtr(), pub_y.GetPtr(), nullptr), 0);
173
174
1.27k
            ret = true;
175
1.72k
end:
176
1.72k
            return ret;
177
1.27k
        }
178
179
    public:
180
        EC_POINT_Copier(Datasource& ds, std::shared_ptr<EC_GROUP_Copier> group) :
181
3.28k
            group(group), ds(ds) {
182
3.28k
            point = newPoint();
183
3.28k
            if ( point == nullptr ) {
184
0
                abort();
185
0
            }
186
3.28k
        }
187
188
1.72k
        bool Set(OpenSSL_bignum::Bignum& pub_x, OpenSSL_bignum::Bignum& pub_y) {
189
1.72k
            return set(pub_x, pub_y);
190
1.72k
        }
191
192
1.77k
        bool Set(const component::Bignum& pub_x, const component::Bignum& pub_y) {
193
1.77k
            bool ret = false;
194
195
1.77k
            OpenSSL_bignum::Bignum _pub_x(ds), _pub_y(ds);
196
197
1.77k
            CF_CHECK_EQ(_pub_x.Set(pub_x.ToString(ds)), true);
198
1.74k
            CF_CHECK_EQ(_pub_y.Set(pub_y.ToString(ds)), true);
199
200
1.72k
            CF_CHECK_TRUE(Set(_pub_x, _pub_y));
201
202
1.27k
            ret = true;
203
1.77k
end:
204
1.77k
            return ret;
205
1.27k
        }
206
207
131
        bool Get(OpenSSL_bignum::Bignum& pub_x, OpenSSL_bignum::Bignum& pub_y) {
208
131
            bool ret = false;
209
210
131
            CF_CHECK_NE(EC_POINT_get_affine_coordinates(group->GetPtr(), GetPtr(), pub_x.GetDestPtr(), pub_y.GetDestPtr(), nullptr), 0);
211
212
118
            ret = true;
213
131
end:
214
131
            return ret;
215
118
        }
216
217
131
        std::optional<component::ECC_Point> Get(void) {
218
131
            std::optional<component::ECC_Point> ret = std::nullopt;
219
220
131
            char* x_str = nullptr;
221
131
            char* y_str = nullptr;
222
223
131
            OpenSSL_bignum::Bignum x(ds);
224
131
            OpenSSL_bignum::Bignum y(ds);
225
226
131
            CF_CHECK_EQ(x.New(), true);
227
131
            CF_CHECK_EQ(y.New(), true);
228
229
131
            CF_CHECK_TRUE(Get(x, y));
230
231
118
            CF_CHECK_NE(x_str = BN_bn2dec(x.GetPtr()), nullptr);
232
118
            CF_CHECK_NE(y_str = BN_bn2dec(y.GetPtr()), nullptr);
233
234
118
            ret = { std::string(x_str), std::string(y_str) };
235
236
131
end:
237
131
            OPENSSL_free(x_str);
238
131
            OPENSSL_free(y_str);
239
240
131
            return ret;
241
118
        }
242
243
6.06k
        EC_POINT* GetPtr(void) {
244
6.06k
            return copy();
245
6.06k
        }
246
247
3.28k
        ~EC_POINT_Copier() {
248
3.28k
            freePoint(point);
249
3.28k
        }
250
};
251
252
#if !defined(CRYPTOFUZZ_OPENSSL_102)
253
1.69k
template<> EVP_MD_CTX* CTX_Copier<EVP_MD_CTX>::newCTX(void) const { return EVP_MD_CTX_new(); }
254
#else
255
template<> EVP_MD_CTX* CTX_Copier<EVP_MD_CTX>::newCTX(void) const {
256
    EVP_MD_CTX* ret = (EVP_MD_CTX*)malloc(sizeof(*ret));
257
    EVP_MD_CTX_init(ret);
258
    return ret;
259
}
260
#endif
261
262
0
template<> int CTX_Copier<EVP_MD_CTX>::copyCTX(EVP_MD_CTX* dest, EVP_MD_CTX* src) const { return EVP_MD_CTX_copy(dest, src); }
263
264
#if !defined(CRYPTOFUZZ_OPENSSL_102)
265
1.69k
template<> void CTX_Copier<EVP_MD_CTX>::freeCTX(EVP_MD_CTX* ctx) const { EVP_MD_CTX_free(ctx); }
266
#else
267
template<> void CTX_Copier<EVP_MD_CTX>::freeCTX(EVP_MD_CTX* ctx) const { EVP_MD_CTX_cleanup(ctx); free(ctx); }
268
#endif
269
270
7.08k
template<> EVP_CIPHER_CTX* CTX_Copier<EVP_CIPHER_CTX>::newCTX(void) const { return EVP_CIPHER_CTX_new(); }
271
0
template<> int CTX_Copier<EVP_CIPHER_CTX>::copyCTX(EVP_CIPHER_CTX* dest, EVP_CIPHER_CTX* src) const {
272
#if !defined(CRYPTOFUZZ_WOLFCRYPT_OPENSSL)
273
    return EVP_CIPHER_CTX_copy(dest, src);
274
#else
275
0
    (void)dest;
276
0
    (void)src;
277
278
    /* EVP_CIPHER_CTX_copy is not implemented in wolfCrypt; return failure */
279
0
    return 0;
280
0
#endif
281
0
}
282
7.08k
template<> void CTX_Copier<EVP_CIPHER_CTX>::freeCTX(EVP_CIPHER_CTX* ctx) const { return EVP_CIPHER_CTX_free(ctx); }
283
284
#if !defined(CRYPTOFUZZ_OPENSSL_102)
285
137
template<> HMAC_CTX* CTX_Copier<HMAC_CTX>::newCTX(void) const { return HMAC_CTX_new(); }
286
0
template<> int CTX_Copier<HMAC_CTX>::copyCTX(HMAC_CTX* dest, HMAC_CTX* src) const { return HMAC_CTX_copy(dest, src); }
287
137
template<> void CTX_Copier<HMAC_CTX>::freeCTX(HMAC_CTX* ctx) const { return HMAC_CTX_free(ctx); }
288
#endif
289
290
1.06k
template<> CMAC_CTX* CTX_Copier<CMAC_CTX>::newCTX(void) const { return CMAC_CTX_new(); }
291
0
template<> int CTX_Copier<CMAC_CTX>::copyCTX(CMAC_CTX* dest, CMAC_CTX* src) const {
292
0
    (void)dest;
293
0
    (void)src;
294
0
    return 0;
295
0
}
296
1.06k
template<> void CTX_Copier<CMAC_CTX>::freeCTX(CMAC_CTX* ctx) const { return CMAC_CTX_free(ctx); }
297
298
2.64k
template<> EC_KEY* CTX_Copier<EC_KEY>::newCTX(void) const { return EC_KEY_new(); }
299
0
template<> int CTX_Copier<EC_KEY>::copyCTX(EC_KEY* dest, EC_KEY* src) const {
300
#if !defined(CRYPTOFUZZ_BORINGSSL) && !defined(CRYPTOFUZZ_WOLFCRYPT_OPENSSL)
301
    return EC_KEY_copy(dest, src) == nullptr ? 0 : 1;
302
#else
303
0
    (void)dest;
304
0
    (void)src;
305
0
    return 0;
306
0
#endif
307
0
}
308
2.64k
template<> void CTX_Copier<EC_KEY>::freeCTX(EC_KEY* ctx) const { return EC_KEY_free(ctx); }
309
310
using CF_EVP_MD_CTX = CTX_Copier<EVP_MD_CTX>;
311
using CF_EVP_CIPHER_CTX = CTX_Copier<EVP_CIPHER_CTX>;
312
using CF_HMAC_CTX = CTX_Copier<HMAC_CTX>;
313
using CF_CMAC_CTX = CTX_Copier<CMAC_CTX>;
314
using CF_EC_KEY = CTX_Copier<EC_KEY>;
315
using CF_EC_POINT = EC_POINT_Copier;
316
using CF_EC_GROUP = EC_GROUP_Copier;
317
} /* namespace module */
318
} /* namespace cryptofuzz */