/src/cryptofuzz/modules/openssl/module_internal.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include "bn_ops.h" |
4 | | |
5 | | namespace cryptofuzz { |
6 | | namespace module { |
7 | | |
8 | | template <class T> |
9 | | class CTX_Copier { |
10 | | private: |
11 | | T* ctx = nullptr; |
12 | | Datasource& ds; |
13 | | |
14 | | T* newCTX(void) const; |
15 | | int copyCTX(T* dest, T* src) const; |
16 | | void freeCTX(T* ctx) const; |
17 | | |
18 | 267k | T* copy(void) { |
19 | 267k | bool doCopyCTX = true; |
20 | 267k | try { |
21 | 267k | doCopyCTX = ds.Get<bool>(); |
22 | 267k | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } |
23 | | |
24 | 267k | if ( doCopyCTX == true ) { |
25 | 265k | T* tmpCtx = newCTX(); |
26 | 265k | if ( tmpCtx != nullptr ) { |
27 | 265k | if ( copyCTX(tmpCtx, ctx) == 1 ) { |
28 | | /* Copy succeeded, free the old ctx */ |
29 | 255k | freeCTX(ctx); |
30 | | |
31 | | /* Use the copied ctx */ |
32 | 255k | ctx = tmpCtx; |
33 | 255k | } else { |
34 | 10.0k | freeCTX(tmpCtx); |
35 | 10.0k | } |
36 | 265k | } |
37 | 265k | } |
38 | | |
39 | 267k | return ctx; |
40 | 267k | } cryptofuzz::module::CTX_Copier<env_md_ctx_st>::copy() Line | Count | Source | 18 | 132k | T* copy(void) { | 19 | 132k | bool doCopyCTX = true; | 20 | 132k | try { | 21 | 132k | doCopyCTX = ds.Get<bool>(); | 22 | 132k | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } | 23 | | | 24 | 132k | if ( doCopyCTX == true ) { | 25 | 131k | T* tmpCtx = newCTX(); | 26 | 131k | if ( tmpCtx != nullptr ) { | 27 | 131k | if ( copyCTX(tmpCtx, ctx) == 1 ) { | 28 | | /* Copy succeeded, free the old ctx */ | 29 | 129k | freeCTX(ctx); | 30 | | | 31 | | /* Use the copied ctx */ | 32 | 129k | ctx = tmpCtx; | 33 | 129k | } else { | 34 | 2.61k | freeCTX(tmpCtx); | 35 | 2.61k | } | 36 | 131k | } | 37 | 131k | } | 38 | | | 39 | 132k | return ctx; | 40 | 132k | } |
cryptofuzz::module::CTX_Copier<hmac_ctx_st>::copy() Line | Count | Source | 18 | 726 | T* copy(void) { | 19 | 726 | bool doCopyCTX = true; | 20 | 726 | try { | 21 | 726 | doCopyCTX = ds.Get<bool>(); | 22 | 726 | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } | 23 | | | 24 | 726 | if ( doCopyCTX == true ) { | 25 | 542 | T* tmpCtx = newCTX(); | 26 | 542 | if ( tmpCtx != nullptr ) { | 27 | 542 | if ( copyCTX(tmpCtx, ctx) == 1 ) { | 28 | | /* Copy succeeded, free the old ctx */ | 29 | 382 | freeCTX(ctx); | 30 | | | 31 | | /* Use the copied ctx */ | 32 | 382 | ctx = tmpCtx; | 33 | 382 | } else { | 34 | 160 | freeCTX(tmpCtx); | 35 | 160 | } | 36 | 542 | } | 37 | 542 | } | 38 | | | 39 | 726 | return ctx; | 40 | 726 | } |
cryptofuzz::module::CTX_Copier<evp_cipher_ctx_st>::copy() Line | Count | Source | 18 | 54.5k | T* copy(void) { | 19 | 54.5k | bool doCopyCTX = true; | 20 | 54.5k | try { | 21 | 54.5k | doCopyCTX = ds.Get<bool>(); | 22 | 54.5k | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } | 23 | | | 24 | 54.5k | if ( doCopyCTX == true ) { | 25 | 54.3k | T* tmpCtx = newCTX(); | 26 | 54.3k | if ( tmpCtx != nullptr ) { | 27 | 54.3k | if ( copyCTX(tmpCtx, ctx) == 1 ) { | 28 | | /* Copy succeeded, free the old ctx */ | 29 | 51.0k | freeCTX(ctx); | 30 | | | 31 | | /* Use the copied ctx */ | 32 | 51.0k | ctx = tmpCtx; | 33 | 51.0k | } else { | 34 | 3.33k | freeCTX(tmpCtx); | 35 | 3.33k | } | 36 | 54.3k | } | 37 | 54.3k | } | 38 | | | 39 | 54.5k | return ctx; | 40 | 54.5k | } |
cryptofuzz::module::CTX_Copier<CMAC_CTX_st>::copy() Line | Count | Source | 18 | 75.9k | T* copy(void) { | 19 | 75.9k | bool doCopyCTX = true; | 20 | 75.9k | try { | 21 | 75.9k | doCopyCTX = ds.Get<bool>(); | 22 | 75.9k | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } | 23 | | | 24 | 75.9k | if ( doCopyCTX == true ) { | 25 | 75.6k | T* tmpCtx = newCTX(); | 26 | 75.6k | if ( tmpCtx != nullptr ) { | 27 | 75.6k | if ( copyCTX(tmpCtx, ctx) == 1 ) { | 28 | | /* Copy succeeded, free the old ctx */ | 29 | 71.7k | freeCTX(ctx); | 30 | | | 31 | | /* Use the copied ctx */ | 32 | 71.7k | ctx = tmpCtx; | 33 | 71.7k | } else { | 34 | 3.90k | freeCTX(tmpCtx); | 35 | 3.90k | } | 36 | 75.6k | } | 37 | 75.6k | } | 38 | | | 39 | 75.9k | return ctx; | 40 | 75.9k | } |
cryptofuzz::module::CTX_Copier<ec_key_st>::copy() Line | Count | Source | 18 | 3.51k | T* copy(void) { | 19 | 3.51k | bool doCopyCTX = true; | 20 | 3.51k | try { | 21 | 3.51k | doCopyCTX = ds.Get<bool>(); | 22 | 3.51k | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } | 23 | | | 24 | 3.51k | if ( doCopyCTX == true ) { | 25 | 3.23k | T* tmpCtx = newCTX(); | 26 | 3.23k | if ( tmpCtx != nullptr ) { | 27 | 3.23k | if ( copyCTX(tmpCtx, ctx) == 1 ) { | 28 | | /* Copy succeeded, free the old ctx */ | 29 | 3.23k | freeCTX(ctx); | 30 | | | 31 | | /* Use the copied ctx */ | 32 | 3.23k | ctx = tmpCtx; | 33 | 3.23k | } else { | 34 | 0 | freeCTX(tmpCtx); | 35 | 0 | } | 36 | 3.23k | } | 37 | 3.23k | } | 38 | | | 39 | 3.51k | return ctx; | 40 | 3.51k | } |
|
41 | | |
42 | | public: |
43 | | CTX_Copier(Datasource& ds) : |
44 | 13.0k | ds(ds) { |
45 | 13.0k | ctx = newCTX(); |
46 | | |
47 | 13.0k | CF_ASSERT(ctx != nullptr, "Cannot create ctx"); |
48 | 13.0k | } cryptofuzz::module::CTX_Copier<env_md_ctx_st>::CTX_Copier(fuzzing::datasource::Datasource&) Line | Count | Source | 44 | 3.00k | ds(ds) { | 45 | 3.00k | ctx = newCTX(); | 46 | | | 47 | 3.00k | CF_ASSERT(ctx != nullptr, "Cannot create ctx"); | 48 | 3.00k | } |
cryptofuzz::module::CTX_Copier<hmac_ctx_st>::CTX_Copier(fuzzing::datasource::Datasource&) Line | Count | Source | 44 | 167 | ds(ds) { | 45 | 167 | ctx = newCTX(); | 46 | | | 47 | 167 | CF_ASSERT(ctx != nullptr, "Cannot create ctx"); | 48 | 167 | } |
cryptofuzz::module::CTX_Copier<evp_cipher_ctx_st>::CTX_Copier(fuzzing::datasource::Datasource&) Line | Count | Source | 44 | 4.20k | ds(ds) { | 45 | 4.20k | ctx = newCTX(); | 46 | | | 47 | 4.20k | CF_ASSERT(ctx != nullptr, "Cannot create ctx"); | 48 | 4.20k | } |
cryptofuzz::module::CTX_Copier<CMAC_CTX_st>::CTX_Copier(fuzzing::datasource::Datasource&) Line | Count | Source | 44 | 4.10k | ds(ds) { | 45 | 4.10k | ctx = newCTX(); | 46 | | | 47 | 4.10k | CF_ASSERT(ctx != nullptr, "Cannot create ctx"); | 48 | 4.10k | } |
cryptofuzz::module::CTX_Copier<ec_key_st>::CTX_Copier(fuzzing::datasource::Datasource&) Line | Count | Source | 44 | 1.56k | ds(ds) { | 45 | 1.56k | ctx = newCTX(); | 46 | | | 47 | 1.56k | CF_ASSERT(ctx != nullptr, "Cannot create ctx"); | 48 | 1.56k | } |
|
49 | | |
50 | 267k | T* GetPtr(void) { |
51 | 267k | return copy(); |
52 | 267k | } cryptofuzz::module::CTX_Copier<env_md_ctx_st>::GetPtr() Line | Count | Source | 50 | 132k | T* GetPtr(void) { | 51 | 132k | return copy(); | 52 | 132k | } |
cryptofuzz::module::CTX_Copier<hmac_ctx_st>::GetPtr() Line | Count | Source | 50 | 726 | T* GetPtr(void) { | 51 | 726 | return copy(); | 52 | 726 | } |
cryptofuzz::module::CTX_Copier<evp_cipher_ctx_st>::GetPtr() Line | Count | Source | 50 | 54.5k | T* GetPtr(void) { | 51 | 54.5k | return copy(); | 52 | 54.5k | } |
cryptofuzz::module::CTX_Copier<CMAC_CTX_st>::GetPtr() Line | Count | Source | 50 | 75.9k | T* GetPtr(void) { | 51 | 75.9k | return copy(); | 52 | 75.9k | } |
cryptofuzz::module::CTX_Copier<ec_key_st>::GetPtr() Line | Count | Source | 50 | 3.51k | T* GetPtr(void) { | 51 | 3.51k | return copy(); | 52 | 3.51k | } |
|
53 | | |
54 | 13.0k | ~CTX_Copier() { |
55 | 13.0k | freeCTX(ctx); |
56 | 13.0k | } cryptofuzz::module::CTX_Copier<env_md_ctx_st>::~CTX_Copier() Line | Count | Source | 54 | 3.00k | ~CTX_Copier() { | 55 | 3.00k | freeCTX(ctx); | 56 | 3.00k | } |
cryptofuzz::module::CTX_Copier<hmac_ctx_st>::~CTX_Copier() Line | Count | Source | 54 | 167 | ~CTX_Copier() { | 55 | 167 | freeCTX(ctx); | 56 | 167 | } |
cryptofuzz::module::CTX_Copier<evp_cipher_ctx_st>::~CTX_Copier() Line | Count | Source | 54 | 4.20k | ~CTX_Copier() { | 55 | 4.20k | freeCTX(ctx); | 56 | 4.20k | } |
cryptofuzz::module::CTX_Copier<CMAC_CTX_st>::~CTX_Copier() Line | Count | Source | 54 | 4.10k | ~CTX_Copier() { | 55 | 4.10k | freeCTX(ctx); | 56 | 4.10k | } |
cryptofuzz::module::CTX_Copier<ec_key_st>::~CTX_Copier() Line | Count | Source | 54 | 1.56k | ~CTX_Copier() { | 55 | 1.56k | freeCTX(ctx); | 56 | 1.56k | } |
|
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 | 2.40k | EC_GROUP* newGroup(void) { |
67 | 2.40k | return EC_GROUP_new_by_curve_name(curveNID); |
68 | 2.40k | } |
69 | | |
70 | | #if !defined(CRYPTOFUZZ_BORINGSSL) |
71 | 0 | int copyGroup(EC_GROUP* dest, EC_GROUP* src) { |
72 | 0 | return EC_GROUP_copy(dest, src); |
73 | 0 | } |
74 | | #endif |
75 | 2.40k | void freeGroup(EC_GROUP* group) { |
76 | 2.40k | /* noret */ EC_GROUP_free(group); |
77 | 2.40k | } |
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 | 0 | #if !defined(CRYPTOFUZZ_BORINGSSL) |
87 | 0 | EC_GROUP* tmpGroup = newGroup(); |
88 | 0 | if ( tmpGroup != nullptr ) { |
89 | 0 | if ( copyGroup(tmpGroup, group) == 1 ) { |
90 | | /* Copy succeeded, free the old group */ |
91 | 0 | freeGroup(group); |
92 | | |
93 | | /* Use the copied group */ |
94 | 0 | group = tmpGroup; |
95 | 0 | } else { |
96 | 0 | freeGroup(tmpGroup); |
97 | 0 | } |
98 | 0 | } |
99 | 0 | #endif |
100 | 0 | } |
101 | |
|
102 | 0 | return group; |
103 | 0 | } |
104 | | |
105 | | public: |
106 | | EC_GROUP_Copier(Datasource& ds, const int curveNID) : |
107 | 2.40k | curveNID(curveNID), ds(ds) { |
108 | 2.40k | group = newGroup(); |
109 | 2.40k | } |
110 | | |
111 | 2.40k | void Lock(void) { |
112 | 2.40k | locked = true; |
113 | 2.40k | } |
114 | | |
115 | 14.9k | EC_GROUP* GetPtr(void) { |
116 | 14.9k | if ( locked == true ) { |
117 | 14.9k | return group; |
118 | 14.9k | } else { |
119 | 0 | return copy(); |
120 | 0 | } |
121 | 14.9k | } |
122 | | |
123 | 2.40k | ~EC_GROUP_Copier() { |
124 | 2.40k | freeGroup(group); |
125 | 2.40k | } |
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 | | const uint64_t curveType; |
134 | | |
135 | 6.94k | EC_POINT* newPoint(void) { |
136 | 6.94k | return EC_POINT_new(group->GetPtr()); |
137 | 6.94k | } |
138 | | |
139 | 4.46k | int copyPoint(EC_POINT* dest, EC_POINT* src) { |
140 | 4.46k | return EC_POINT_copy(dest, src); |
141 | 4.46k | } |
142 | 6.94k | void freePoint(EC_POINT* point) { |
143 | 6.94k | /* noret */ EC_POINT_free(point); |
144 | 6.94k | } |
145 | | |
146 | | |
147 | 0 | point_conversion_form_t GetForm(void) { |
148 | 0 | uint8_t form = 0; |
149 | 0 | try { |
150 | 0 | form = ds.Get<uint8_t>() % 3; |
151 | 0 | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } |
152 | 0 |
|
153 | 0 | if ( form == 0 ) { |
154 | 0 | return POINT_CONVERSION_COMPRESSED; |
155 | 0 | } else if ( form == 1 ) { |
156 | 0 | return POINT_CONVERSION_UNCOMPRESSED; |
157 | 0 | } else if ( form == 2 ) { |
158 | 0 | return POINT_CONVERSION_HYBRID; |
159 | 0 | } else { |
160 | 0 | CF_UNREACHABLE(); |
161 | 0 | } |
162 | 0 | } |
163 | | |
164 | | |
165 | 4.67k | EC_POINT* copy(void) { |
166 | 4.67k | { |
167 | 4.67k | bool doCopyPoint = true; |
168 | 4.67k | try { |
169 | 4.67k | doCopyPoint = ds.Get<bool>(); |
170 | 4.67k | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } |
171 | | |
172 | 4.67k | if ( doCopyPoint == true ) { |
173 | 4.46k | EC_POINT* tmpPoint = newPoint(); |
174 | 4.46k | if ( tmpPoint != nullptr ) { |
175 | 4.46k | if ( copyPoint(tmpPoint, point) == 1 ) { |
176 | | /* Copy succeeded, free the old point */ |
177 | 4.46k | freePoint(point); |
178 | | |
179 | | /* Use the copied point */ |
180 | 4.46k | point = tmpPoint; |
181 | 4.46k | } else { |
182 | 0 | freePoint(tmpPoint); |
183 | 0 | } |
184 | 4.46k | } |
185 | 4.46k | } |
186 | 4.67k | } |
187 | | |
188 | 4.67k | return point; |
189 | 4.67k | } |
190 | | |
191 | 926 | bool set(OpenSSL_bignum::Bignum& pub_x, OpenSSL_bignum::Bignum& pub_y) { |
192 | 926 | bool ret = false; |
193 | | |
194 | | #if !defined(CRYPTOFUZZ_BORINGSSL) && !defined(CRYPTOFUZZ_LIBRESSL) && !defined(CRYPTOFUZZ_OPENSSL_102) && !defined(CRYPTOFUZZ_OPENSSL_110) && !defined(CRYPTOFUZZ_OPENSSL_098) |
195 | | CF_CHECK_NE(EC_POINT_set_affine_coordinates(group->GetPtr(), GetPtr(), pub_x.GetPtr(), pub_y.GetPtr(), nullptr), 0); |
196 | | #else |
197 | 926 | CF_CHECK_NE(EC_POINT_set_affine_coordinates_GFp(group->GetPtr(), GetPtr(), pub_x.GetPtr(), pub_y.GetPtr(), nullptr), 0); |
198 | 620 | #endif |
199 | | |
200 | 620 | ret = true; |
201 | 926 | end: |
202 | 926 | return ret; |
203 | 620 | } |
204 | | |
205 | 0 | bool set_compressed(OpenSSL_bignum::Bignum& pub_x, OpenSSL_bignum::Bignum& pub_y) { |
206 | 0 | bool ret = false; |
207 | |
|
208 | 0 | const bool is_prime_curve = |
209 | 0 | #if defined(CRYPTOFUZZ_LIBRESSL) || defined(CRYPTOFUZZ_BORINGSSL) || defined(CRYPTOFUZZ_OPENSSL_102) || defined(CRYPTOFUZZ_OPENSSL_098) |
210 | 0 | EC_METHOD_get_field_type(EC_GROUP_method_of(group->GetPtr())) |
211 | | #else |
212 | | EC_GROUP_get_field_type(group->GetPtr()) |
213 | | #endif |
214 | 0 | == NID_X9_62_prime_field; |
215 | |
|
216 | | #if defined(CRYPTOFUZZ_BORINGSSL) |
217 | | if ( is_prime_curve ) { |
218 | | return set(pub_x, pub_y); |
219 | | } |
220 | | #endif |
221 | |
|
222 | 0 | OpenSSL_bignum::Bignum field(ds); |
223 | 0 | CF_CHECK_TRUE(field.New()); |
224 | 0 | int y_bit; |
225 | | |
226 | | /* Reduction of Y is necessary in order to correctly determine y_bit */ |
227 | 0 | { |
228 | 0 | OpenSSL_bignum::BN_CTX ctx(ds); |
229 | 0 | BIGNUM* y = pub_y.GetDestPtr(); |
230 | |
|
231 | 0 | #if defined(CRYPTOFUZZ_LIBRESSL) || defined(CRYPTOFUZZ_BORINGSSL) || defined(CRYPTOFUZZ_OPENSSL_102) || defined(CRYPTOFUZZ_OPENSSL_098) |
232 | | /* LibreSSL and BoringSSL don't have EC_GROUP_get0_field(), |
233 | | * so try to retrieve the prime from the repository |
234 | | */ |
235 | 0 | const auto prime = cryptofuzz::repository::ECC_CurveToPrime(curveType); |
236 | 0 | CF_CHECK_NE(prime, std::nullopt); |
237 | 0 | CF_CHECK_EQ(field.Set(*prime), true); |
238 | | #else |
239 | | (void)curveType; |
240 | | |
241 | | const BIGNUM* _field; |
242 | | CF_ASSERT( |
243 | | (_field = EC_GROUP_get0_field(group->GetPtr())) != nullptr, |
244 | | "EC_GROUP_get0_field returned NULL"); |
245 | | CF_CHECK_NE(BN_copy(field.GetDestPtr(), _field), nullptr); |
246 | | #endif |
247 | |
|
248 | 0 | CF_CHECK_EQ(BN_mod(y, y, field.GetPtr(), ctx.GetPtr()), 1); |
249 | 0 | } |
250 | | |
251 | 0 | if ( is_prime_curve == true ) { |
252 | 0 | y_bit = BN_is_bit_set(pub_y.GetPtr(), 0); |
253 | 0 | } else { |
254 | | /* Binary curve */ |
255 | | #if defined(CRYPTOFUZZ_BORINGSSL) |
256 | | CF_UNREACHABLE(); |
257 | | #else |
258 | 0 | if ( BN_is_zero(pub_x.GetPtr()) ) { |
259 | 0 | y_bit = 0; |
260 | 0 | } else { |
261 | 0 | OpenSSL_bignum::BN_CTX ctx(ds); |
262 | 0 | OpenSSL_bignum::Bignum div(ds); |
263 | |
|
264 | 0 | CF_CHECK_TRUE(div.New()); |
265 | 0 | CF_CHECK_EQ(BN_GF2m_mod_div(div.GetDestPtr(), pub_y.GetPtr(), pub_x.GetPtr(), field.GetPtr(), ctx.GetPtr()), 1); |
266 | |
|
267 | 0 | y_bit = BN_is_bit_set(div.GetPtr(), 0); |
268 | 0 | } |
269 | 0 | #endif |
270 | 0 | } |
271 | | |
272 | | #if !defined(CRYPTOFUZZ_BORINGSSL) && !defined(CRYPTOFUZZ_LIBRESSL) && !defined(CRYPTOFUZZ_OPENSSL_102) && !defined(CRYPTOFUZZ_OPENSSL_110) && !defined(CRYPTOFUZZ_OPENSSL_098) |
273 | | CF_CHECK_NE(EC_POINT_set_compressed_coordinates(group->GetPtr(), GetPtr(), pub_x.GetPtr(), y_bit, nullptr), 0); |
274 | | #else |
275 | 0 | CF_CHECK_NE(EC_POINT_set_compressed_coordinates_GFp(group->GetPtr(), GetPtr(), pub_x.GetPtr(), y_bit, nullptr), 0); |
276 | 0 | #endif |
277 | |
|
278 | 0 | CF_ASSERT( |
279 | 0 | EC_POINT_is_on_curve(group->GetPtr(), GetPtr(), nullptr) == 1, |
280 | 0 | "Decompressed point not on curve") |
281 | | |
282 | 0 | ret = true; |
283 | 0 | end: |
284 | 0 | return ret; |
285 | 0 | } |
286 | | |
287 | | public: |
288 | | EC_POINT_Copier(Datasource& ds, std::shared_ptr<EC_GROUP_Copier> group, const uint64_t curveType) : |
289 | 2.48k | group(group), ds(ds), curveType(curveType) { |
290 | 2.48k | point = newPoint(); |
291 | | |
292 | 2.48k | CF_ASSERT(point != nullptr, "Cannot create EC_POINT"); |
293 | 2.48k | } |
294 | | |
295 | 4.67k | EC_POINT* GetPtr(void) { |
296 | 4.67k | return copy(); |
297 | 4.67k | } |
298 | | |
299 | 2.48k | ~EC_POINT_Copier() { |
300 | 2.48k | freePoint(point); |
301 | 2.48k | } |
302 | | |
303 | 926 | bool Set(OpenSSL_bignum::Bignum& pub_x, OpenSSL_bignum::Bignum& pub_y, const bool allowCompressed = true) { |
304 | 926 | bool compressed = false; |
305 | 926 | try { |
306 | 926 | compressed = ds.Get<bool>(); |
307 | 926 | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } |
308 | | |
309 | 926 | if ( allowCompressed == false ) { |
310 | 784 | compressed = false; |
311 | 784 | } |
312 | | |
313 | | /* Currently disabled because it leads to spurious discrepancies */ |
314 | 926 | compressed = false; |
315 | | |
316 | 926 | return compressed ? |
317 | 0 | set_compressed(pub_x, pub_y) : |
318 | 926 | set(pub_x, pub_y); |
319 | 926 | } |
320 | | |
321 | 926 | bool Set(const component::Bignum& pub_x, const component::Bignum& pub_y, const bool allowCompressed = true) { |
322 | 926 | bool ret = false; |
323 | | |
324 | 926 | OpenSSL_bignum::Bignum _pub_x(ds), _pub_y(ds); |
325 | | |
326 | 926 | CF_CHECK_EQ(_pub_x.Set(pub_x.ToString(ds)), true); |
327 | 926 | CF_CHECK_EQ(_pub_y.Set(pub_y.ToString(ds)), true); |
328 | | |
329 | 926 | CF_CHECK_TRUE(Set(_pub_x, _pub_y, allowCompressed)); |
330 | | |
331 | 620 | ret = true; |
332 | 926 | end: |
333 | 926 | return ret; |
334 | 620 | } |
335 | | |
336 | 27 | bool Get(OpenSSL_bignum::Bignum& pub_x, OpenSSL_bignum::Bignum& pub_y) { |
337 | 27 | bool ret = false; |
338 | | |
339 | | #if !defined(CRYPTOFUZZ_BORINGSSL) && !defined(CRYPTOFUZZ_LIBRESSL) && !defined(CRYPTOFUZZ_OPENSSL_102) && !defined(CRYPTOFUZZ_OPENSSL_110) && !defined(CRYPTOFUZZ_OPENSSL_098) |
340 | | CF_CHECK_NE(EC_POINT_get_affine_coordinates(group->GetPtr(), GetPtr(), pub_x.GetDestPtr(), pub_y.GetDestPtr(), nullptr), 0); |
341 | | #else |
342 | 27 | CF_CHECK_NE(EC_POINT_get_affine_coordinates_GFp(group->GetPtr(), GetPtr(), pub_x.GetDestPtr(), pub_y.GetDestPtr(), nullptr), 0); |
343 | 22 | #endif |
344 | | |
345 | 22 | ret = true; |
346 | 27 | end: |
347 | 27 | return ret; |
348 | 22 | } |
349 | | |
350 | 27 | std::optional<component::ECC_Point> Get(void) { |
351 | 27 | std::optional<component::ECC_Point> ret = std::nullopt; |
352 | | |
353 | 27 | char* x_str = nullptr; |
354 | 27 | char* y_str = nullptr; |
355 | | |
356 | 27 | OpenSSL_bignum::Bignum x(ds); |
357 | 27 | OpenSSL_bignum::Bignum y(ds); |
358 | | |
359 | 27 | CF_CHECK_EQ(x.New(), true); |
360 | 27 | CF_CHECK_EQ(y.New(), true); |
361 | | |
362 | 27 | CF_CHECK_TRUE(Get(x, y)); |
363 | | |
364 | 22 | CF_CHECK_NE(x_str = BN_bn2dec(x.GetPtr()), nullptr); |
365 | 22 | CF_CHECK_NE(y_str = BN_bn2dec(y.GetPtr()), nullptr); |
366 | | |
367 | 22 | ret = { std::string(x_str), std::string(y_str) }; |
368 | | |
369 | 27 | end: |
370 | 27 | OPENSSL_free(x_str); |
371 | 27 | OPENSSL_free(y_str); |
372 | | |
373 | 27 | return ret; |
374 | 22 | } |
375 | | }; |
376 | | |
377 | | #if !defined(CRYPTOFUZZ_OPENSSL_102) && !defined(CRYPTOFUZZ_OPENSSL_098) |
378 | 134k | template<> EVP_MD_CTX* CTX_Copier<EVP_MD_CTX>::newCTX(void) const { return EVP_MD_CTX_new(); } |
379 | | #else |
380 | | template<> EVP_MD_CTX* CTX_Copier<EVP_MD_CTX>::newCTX(void) const { |
381 | | EVP_MD_CTX* ret = (EVP_MD_CTX*)malloc(sizeof(*ret)); |
382 | | EVP_MD_CTX_init(ret); |
383 | | return ret; |
384 | | } |
385 | | #endif |
386 | | |
387 | 131k | template<> int CTX_Copier<EVP_MD_CTX>::copyCTX(EVP_MD_CTX* dest, EVP_MD_CTX* src) const { return EVP_MD_CTX_copy(dest, src); } |
388 | | |
389 | | #if !defined(CRYPTOFUZZ_OPENSSL_102) && !defined(CRYPTOFUZZ_OPENSSL_098) |
390 | 134k | template<> void CTX_Copier<EVP_MD_CTX>::freeCTX(EVP_MD_CTX* ctx) const { EVP_MD_CTX_free(ctx); } |
391 | | #else |
392 | | template<> void CTX_Copier<EVP_MD_CTX>::freeCTX(EVP_MD_CTX* ctx) const { EVP_MD_CTX_cleanup(ctx); free(ctx); } |
393 | | #endif |
394 | | |
395 | 58.5k | template<> EVP_CIPHER_CTX* CTX_Copier<EVP_CIPHER_CTX>::newCTX(void) const { return EVP_CIPHER_CTX_new(); } |
396 | | #if !defined(CRYPTOFUZZ_OPENSSL_098) |
397 | 54.3k | template<> int CTX_Copier<EVP_CIPHER_CTX>::copyCTX(EVP_CIPHER_CTX* dest, EVP_CIPHER_CTX* src) const { return EVP_CIPHER_CTX_copy(dest, src); } |
398 | | #else |
399 | | template<> int CTX_Copier<EVP_CIPHER_CTX>::copyCTX(EVP_CIPHER_CTX* dest, EVP_CIPHER_CTX* src) const { (void)dest; (void)src; return 0; } |
400 | | #endif |
401 | 58.5k | template<> void CTX_Copier<EVP_CIPHER_CTX>::freeCTX(EVP_CIPHER_CTX* ctx) const { return EVP_CIPHER_CTX_free(ctx); } |
402 | | |
403 | | #if !defined(CRYPTOFUZZ_OPENSSL_102) && !defined(CRYPTOFUZZ_OPENSSL_098) |
404 | 709 | template<> HMAC_CTX* CTX_Copier<HMAC_CTX>::newCTX(void) const { return HMAC_CTX_new(); } |
405 | 542 | template<> int CTX_Copier<HMAC_CTX>::copyCTX(HMAC_CTX* dest, HMAC_CTX* src) const { return HMAC_CTX_copy(dest, src); } |
406 | 709 | template<> void CTX_Copier<HMAC_CTX>::freeCTX(HMAC_CTX* ctx) const { return HMAC_CTX_free(ctx); } |
407 | | #endif |
408 | | |
409 | | #if !defined(CRYPTOFUZZ_OPENSSL_098) |
410 | 79.7k | template<> CMAC_CTX* CTX_Copier<CMAC_CTX>::newCTX(void) const { return CMAC_CTX_new(); } |
411 | 75.6k | template<> int CTX_Copier<CMAC_CTX>::copyCTX(CMAC_CTX* dest, CMAC_CTX* src) const { return CMAC_CTX_copy(dest, src); } |
412 | 79.7k | template<> void CTX_Copier<CMAC_CTX>::freeCTX(CMAC_CTX* ctx) const { return CMAC_CTX_free(ctx); } |
413 | | #endif |
414 | | |
415 | 4.79k | template<> EC_KEY* CTX_Copier<EC_KEY>::newCTX(void) const { return EC_KEY_new(); } |
416 | 3.23k | template<> int CTX_Copier<EC_KEY>::copyCTX(EC_KEY* dest, EC_KEY* src) const { |
417 | 3.23k | #if !defined(CRYPTOFUZZ_BORINGSSL) |
418 | 3.23k | return EC_KEY_copy(dest, src) == nullptr ? 0 : 1; |
419 | | #else |
420 | | (void)dest; |
421 | | (void)src; |
422 | | return 0; |
423 | | #endif |
424 | 3.23k | } |
425 | 4.79k | template<> void CTX_Copier<EC_KEY>::freeCTX(EC_KEY* ctx) const { return EC_KEY_free(ctx); } |
426 | | |
427 | | using CF_EVP_MD_CTX = CTX_Copier<EVP_MD_CTX>; |
428 | | using CF_EVP_CIPHER_CTX = CTX_Copier<EVP_CIPHER_CTX>; |
429 | | using CF_HMAC_CTX = CTX_Copier<HMAC_CTX>; |
430 | | using CF_CMAC_CTX = CTX_Copier<CMAC_CTX>; |
431 | | using CF_EC_KEY = CTX_Copier<EC_KEY>; |
432 | | using CF_EC_POINT = EC_POINT_Copier; |
433 | | using CF_EC_GROUP = EC_GROUP_Copier; |
434 | | } /* namespace module */ |
435 | | } /* namespace cryptofuzz */ |