/src/cryptofuzz-sp-math/modules/wolfcrypt/bn_helper.cpp
Line | Count | Source |
1 | | #include "bn_ops.h" |
2 | | #include <iostream> |
3 | | |
4 | | namespace cryptofuzz { |
5 | | namespace module { |
6 | | |
7 | | namespace wolfCrypt_detail { |
8 | | #if defined(CRYPTOFUZZ_WOLFCRYPT_ALLOCATION_FAILURES) |
9 | | extern bool disableAllocationFailures; |
10 | | extern bool haveAllocFailure; |
11 | | #endif |
12 | | } /* namespace wolfCrypt_detail */ |
13 | | |
14 | | namespace wolfCrypt_bignum { |
15 | | |
16 | 231k | Bignum::read_radix_error_t Bignum::read_radix(mp_int* dest, const std::string& str, const size_t base) { |
17 | 231k | return read_radix(dest, str.c_str(), base); |
18 | 231k | } |
19 | | |
20 | 232k | Bignum::read_radix_error_t Bignum::read_radix(mp_int* dest, const char* str, const size_t base) { |
21 | 232k | Bignum::read_radix_error_t ret; |
22 | | |
23 | | /* Create a temporary variable for storing the result of mp_read_radix, |
24 | | * because if mp_read_radix fails (e.g. due to allocation failure), |
25 | | * it will set the value of the destination variable to 0. |
26 | | * |
27 | | * See OSS-Fuzz 31709 / ZD 11834 for discussion. */ |
28 | 232k | auto newMp = (mp_int*)util::malloc(sizeof(mp_int)); |
29 | 232k | if ( mp_init(newMp) != MP_OKAY ) { |
30 | 0 | util::free(newMp); |
31 | 0 | return READ_RADIX_FAIL_MEMORY; |
32 | 0 | } |
33 | | |
34 | 232k | wolfCrypt_detail::haveAllocFailure = false; |
35 | 232k | if ( mp_read_radix(newMp, str, base) != MP_OKAY ) { |
36 | 1.57k | ret = wolfCrypt_detail::haveAllocFailure ? READ_RADIX_FAIL_MEMORY : READ_RADIX_FAIL_OTHER; |
37 | 1.57k | goto end; |
38 | 1.57k | } |
39 | | |
40 | 230k | wolfCrypt_detail::haveAllocFailure = false; |
41 | 230k | if ( mp_copy(newMp, dest) != MP_OKAY ) { |
42 | 1.14k | ret = wolfCrypt_detail::haveAllocFailure ? READ_RADIX_FAIL_MEMORY : READ_RADIX_FAIL_OTHER; |
43 | 1.14k | goto end; |
44 | 1.14k | } |
45 | | |
46 | 229k | ret = READ_RADIX_OK; |
47 | | |
48 | 232k | end: |
49 | 232k | CF_NORET(mp_clear(newMp)); |
50 | 232k | util::free(newMp); |
51 | | |
52 | 232k | return ret; |
53 | 229k | } |
54 | | |
55 | 4.80k | void Bignum::baseConversion(void) const { |
56 | | #if !defined(WOLFSSL_SP_MATH) |
57 | | uint8_t base = 2; |
58 | | char* str = nullptr; |
59 | | |
60 | | try { base = ds.Get<uint8_t>(); } catch ( fuzzing::datasource::Datasource::OutOfData ) { } |
61 | | |
62 | | #if defined(CRYPTOFUZZ_WOLFCRYPT_DEBUG) |
63 | | std::cout << "Convert to base " << std::to_string(base) << " and back" << std::endl; |
64 | | #endif |
65 | | { |
66 | | int size; |
67 | | CF_CHECK_EQ(mp_radix_size(mp, base, &size), MP_OKAY); |
68 | | CF_ASSERT(size > 0, "Output of mp_radix_size is 0 or less"); |
69 | | |
70 | | str = (char*)util::malloc(size); |
71 | | |
72 | | #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH) |
73 | | CF_CHECK_EQ(mp_toradix(mp, str, base), MP_OKAY); |
74 | | #else |
75 | | wolfCrypt_detail::haveAllocFailure = false; |
76 | | CF_ASSERT( |
77 | | mp_toradix(mp, str, base) == MP_OKAY || |
78 | | wolfCrypt_detail::haveAllocFailure || |
79 | | base < 2 || |
80 | | base > 64, |
81 | | "wolfCrypt cannot convert mp to string"); |
82 | | |
83 | | /* If allocation failure occurred, then do not use 'str' */ |
84 | | CF_CHECK_FALSE(wolfCrypt_detail::haveAllocFailure); |
85 | | #endif |
86 | | |
87 | | { |
88 | | const auto ret = read_radix(mp, str, base); |
89 | | CF_ASSERT(ret == READ_RADIX_OK || ret == READ_RADIX_FAIL_MEMORY, "wolfCrypt cannot parse the output of mp_toradix"); |
90 | | } |
91 | | } |
92 | | |
93 | | end: |
94 | | util::free(str); |
95 | | #endif |
96 | 4.80k | } |
97 | | |
98 | 11.6k | void Bignum::binaryConversion(void) const { |
99 | 11.6k | uint8_t* data = nullptr; |
100 | 11.6k | CF_CHECK_EQ(mp_isneg(mp), 0); |
101 | | |
102 | | #if LIBWOLFSSL_VERSION_HEX == 0x05005001 |
103 | | { |
104 | | /* Old version of the binary conversion logic, which doesn't crash if |
105 | | * mp_to_unsigned_bin_len() succeeds with an undersized buffer. |
106 | | * |
107 | | * This logic is retained specifically to prevent the libecc |
108 | | * OSS-Fuzz fuzzer from crashing, because this uses an older version |
109 | | * of wolfCrypt (specifically 0x05005001), which still has the |
110 | | * mp_to_unsigned_bin_len() bug. |
111 | | */ |
112 | | |
113 | | const auto size = mp_unsigned_bin_size(mp); |
114 | | CF_ASSERT(size >= 0, "mp_unsigned_bin_size returned negative value"); |
115 | | |
116 | | data = util::malloc(size); |
117 | | CF_CHECK_EQ(mp_to_unsigned_bin_len(mp, data, size), MP_OKAY); |
118 | | |
119 | | /* Ensure no allocation failure occurs in mp_read_unsigned_bin |
120 | | * because this can leave the mp in a corrupted state |
121 | | */ |
122 | | const auto cached_disableAllocationFailures = wolfCrypt_detail::disableAllocationFailures; |
123 | | wolfCrypt_detail::disableAllocationFailures = true; |
124 | | |
125 | | CF_ASSERT(mp_read_unsigned_bin(mp, data, size) == MP_OKAY, "Cannot parse output of mp_to_unsigned_bin_len"); |
126 | | |
127 | | wolfCrypt_detail::disableAllocationFailures = cached_disableAllocationFailures; |
128 | | } |
129 | | #else |
130 | 11.6k | { |
131 | 11.6k | bool randomSize = false; |
132 | | |
133 | 11.6k | try { randomSize = ds.Get<bool>(); } catch ( fuzzing::datasource::Datasource::OutOfData ) { } |
134 | | |
135 | 11.6k | size_t size = 0; |
136 | 11.6k | int numBits = 0; |
137 | | |
138 | 11.6k | if ( randomSize == false ) { |
139 | 5.40k | const auto size2 = mp_unsigned_bin_size(mp); |
140 | 5.40k | CF_ASSERT(size2 >= 0, "mp_unsigned_bin_size returned negative value"); |
141 | 5.40k | size = size2; |
142 | 6.26k | } else { |
143 | 6.26k | try { |
144 | 6.26k | size = ds.Get<uint16_t>(); |
145 | 6.26k | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } |
146 | | |
147 | 6.26k | numBits = mp_count_bits(mp); |
148 | 6.26k | CF_ASSERT(numBits >= 0, "mp_count_bits result is negative"); |
149 | 6.26k | } |
150 | | |
151 | 11.6k | data = util::malloc(size); |
152 | 11.6k | CF_CHECK_EQ(mp_to_unsigned_bin_len(mp, data, size), MP_OKAY); |
153 | | |
154 | 9.76k | CF_ASSERT( |
155 | 9.76k | size * 8 >= static_cast<size_t>(numBits), |
156 | 9.76k | "mp_to_unsigned_bin_len succeeded with undersized buffer"); |
157 | | |
158 | | /* Ensure no allocation failure occurs in mp_read_unsigned_bin |
159 | | * because this can leave the mp in a corrupted state |
160 | | */ |
161 | 9.76k | const auto cached_disableAllocationFailures = wolfCrypt_detail::disableAllocationFailures; |
162 | 9.76k | wolfCrypt_detail::disableAllocationFailures = true; |
163 | | |
164 | 9.76k | if ( randomSize == false ) { |
165 | 3.92k | CF_ASSERT(mp_read_unsigned_bin(mp, data, size) == MP_OKAY, "Cannot parse output of mp_to_unsigned_bin_len"); |
166 | 5.84k | } else { |
167 | | /* mp_read_unsigned_bin can fail if the input buffer is too large. |
168 | | * |
169 | | * Read into a temp variable, and copy to mp if reading succeeds, otherwise |
170 | | * retain old value in mp. |
171 | | */ |
172 | 5.84k | Bignum tmp(ds); |
173 | 5.84k | if ( mp_read_unsigned_bin(tmp.GetPtrDirect(), data, size) == MP_OKAY ) { |
174 | 134 | /* ignore result */ mp_copy(tmp.GetPtrDirect(), mp); |
175 | 134 | } |
176 | 5.84k | } |
177 | | |
178 | 9.76k | wolfCrypt_detail::disableAllocationFailures = cached_disableAllocationFailures; |
179 | 9.76k | } |
180 | 0 | #endif |
181 | | |
182 | | |
183 | 11.6k | end: |
184 | 11.6k | util::free(data); |
185 | 11.6k | } |
186 | | |
187 | 1.13M | void Bignum::invariants(void) const { |
188 | 1.13M | #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) |
189 | 1.13M | CF_ASSERT(mp->used <= mp->size, "used is larger than size"); |
190 | | #elif !defined(USE_FAST_MATH) |
191 | | CF_ASSERT(mp->used <= mp->alloc, "used is larger than size"); |
192 | | #endif |
193 | 1.13M | } |
194 | | |
195 | | Bignum::Bignum(Datasource& ds, const bool mp_init_size_ok) : |
196 | 453k | ds(ds) { |
197 | 453k | mp = (mp_int*)util::malloc(sizeof(mp_int)); |
198 | | |
199 | 453k | if ( init_mp_int(mp, ds, mp_init_size_ok) != MP_OKAY ) { |
200 | 0 | util::free(mp); |
201 | 0 | throw std::exception(); |
202 | 0 | } |
203 | 453k | } |
204 | | |
205 | | Bignum::Bignum(mp_int* mp, Datasource& ds) : |
206 | 13.4k | mp(mp), |
207 | 13.4k | ds(ds), |
208 | 13.4k | noFree(true) |
209 | 13.4k | { } |
210 | | |
211 | | Bignum::Bignum(const Bignum& other) : |
212 | 215k | ds(other.ds) { |
213 | 215k | mp = (mp_int*)util::malloc(sizeof(mp_int)); |
214 | 215k | if ( init_mp_int(mp, ds) != MP_OKAY ) { |
215 | 0 | util::free(mp); |
216 | 0 | throw std::exception(); |
217 | 0 | } |
218 | 215k | if ( mp_copy(other.mp, mp) != MP_OKAY ) { |
219 | 32 | util::free(mp); |
220 | 32 | throw std::exception(); |
221 | 32 | } |
222 | 215k | } |
223 | | |
224 | | Bignum::Bignum(const Bignum&& other) : |
225 | 216k | ds(other.ds) { |
226 | 216k | mp = (mp_int*)util::malloc(sizeof(mp_int)); |
227 | 216k | if ( init_mp_int(mp, ds) != MP_OKAY ) { |
228 | 0 | util::free(mp); |
229 | 0 | throw std::exception(); |
230 | 0 | } |
231 | 216k | if ( mp_copy(other.mp, mp) != MP_OKAY ) { |
232 | 222 | util::free(mp); |
233 | 222 | throw std::exception(); |
234 | 222 | } |
235 | 216k | } |
236 | | |
237 | 902k | Bignum::~Bignum() { |
238 | 902k | invariants(); |
239 | 902k | if ( noFree == false ) { |
240 | 885k | CF_NORET(mp_clear(mp)); |
241 | 885k | util::free(mp); |
242 | 885k | } |
243 | 902k | } |
244 | | |
245 | 0 | void Bignum::SetNoFree(void) { |
246 | 0 | noFree = true; |
247 | 0 | } |
248 | | |
249 | 320k | bool Bignum::Set(const std::string s) { |
250 | 320k | bool ret = false; |
251 | | |
252 | 320k | bool hex = false; |
253 | 320k | try { |
254 | 320k | hex = ds.Get<bool>(); |
255 | 320k | } catch ( ... ) { } |
256 | | |
257 | 320k | #if defined(WOLFSSL_SP_MATH) |
258 | 320k | hex = true; |
259 | 320k | #endif |
260 | | |
261 | 320k | if ( hex == true ) { |
262 | 95.7k | CF_CHECK_EQ(read_radix(mp, util::DecToHex(s), 16), READ_RADIX_OK); |
263 | 224k | } else { |
264 | 224k | CF_CHECK_EQ(read_radix(mp, s, 10), READ_RADIX_OK); |
265 | 222k | } |
266 | | |
267 | 315k | ret = true; |
268 | 320k | end: |
269 | 320k | return ret; |
270 | 315k | } |
271 | | |
272 | 47.1k | bool Bignum::Set(const component::Bignum i) { |
273 | 47.1k | bool ret = false; |
274 | | |
275 | 47.1k | CF_CHECK_EQ(Set(i.ToString()), true); |
276 | | |
277 | 46.1k | ret = true; |
278 | 47.1k | end: |
279 | 47.1k | return ret; |
280 | 46.1k | } |
281 | | |
282 | 307k | mp_int* Bignum::GetPtr(void) const { |
283 | 307k | invariants(); |
284 | | |
285 | 307k | { |
286 | | /* Optionally clamp the bignum. This should not affect its value. */ |
287 | | |
288 | 307k | bool clamp = false; |
289 | | |
290 | 307k | try { clamp = ds.Get<bool>(); } catch ( fuzzing::datasource::Datasource::OutOfData ) { } |
291 | | |
292 | 307k | if ( clamp ) { |
293 | | /* Implemented as a macro so CF_NORET cannot be used here */ |
294 | 26.7k | /* noret */ mp_clamp(mp); |
295 | 26.7k | } |
296 | 307k | } |
297 | | |
298 | 307k | { |
299 | | /* Optionally convert to a random base and back */ |
300 | | |
301 | 307k | bool convert = false; |
302 | | |
303 | 307k | try { convert = ds.Get<bool>(); } catch ( fuzzing::datasource::Datasource::OutOfData ) { } |
304 | | |
305 | 307k | if ( convert ) { |
306 | 26.0k | baseConversion(); |
307 | 26.0k | } |
308 | 307k | } |
309 | | |
310 | 307k | { |
311 | | /* Optionally convert to bytes and back */ |
312 | | |
313 | 307k | bool convert = false; |
314 | | |
315 | 307k | try { convert = ds.Get<bool>(); } catch ( fuzzing::datasource::Datasource::OutOfData ) { } |
316 | | |
317 | 307k | if ( convert ) { |
318 | 24.6k | binaryConversion(); |
319 | 24.6k | } |
320 | 307k | } |
321 | | |
322 | 307k | return mp; |
323 | 307k | } |
324 | | |
325 | 274k | mp_int* Bignum::GetPtrDirect(void) const { |
326 | 274k | return mp; |
327 | 274k | } |
328 | | |
329 | 735 | std::optional<uint64_t> Bignum::AsUint64(void) const { |
330 | 735 | std::optional<uint64_t> ret = std::nullopt; |
331 | 735 | uint64_t v = 0; |
332 | | |
333 | | #if !defined(WOLFSSL_SP_MATH) |
334 | | CF_CHECK_EQ(mp_isneg(mp), 0); |
335 | | #endif |
336 | 735 | CF_CHECK_LTE(mp_count_bits(mp), (int)(sizeof(v) * 8)); |
337 | 697 | CF_CHECK_EQ(mp_to_unsigned_bin_len(mp, (uint8_t*)&v, sizeof(v)), MP_OKAY); |
338 | 697 | v = |
339 | 697 | ((v & 0xFF00000000000000) >> 56) | |
340 | 697 | ((v & 0x00FF000000000000) >> 40) | |
341 | 697 | ((v & 0x0000FF0000000000) >> 24) | |
342 | 697 | ((v & 0x000000FF00000000) >> 8) | |
343 | 697 | ((v & 0x00000000FF000000) << 8) | |
344 | 697 | ((v & 0x0000000000FF0000) << 24) | |
345 | 697 | ((v & 0x000000000000FF00) << 40) | |
346 | 697 | ((v & 0x00000000000000FF) << 56); |
347 | | |
348 | 697 | ret = v; |
349 | 735 | end: |
350 | 735 | return ret; |
351 | 697 | } |
352 | | |
353 | 17.3k | std::optional<std::string> Bignum::ToDecString(void) { |
354 | 17.3k | std::optional<std::string> ret = std::nullopt; |
355 | 17.3k | char* str = nullptr; |
356 | | |
357 | 17.3k | #if defined(WOLFSSL_SP_MATH) |
358 | 17.3k | str = (char*)util::malloc(8192); |
359 | | |
360 | 17.3k | CF_CHECK_EQ(mp_tohex(mp, str), MP_OKAY); |
361 | 17.3k | ret = { util::HexToDec(str) }; |
362 | | #else |
363 | | bool hex = false; |
364 | | int size; |
365 | | |
366 | | try { |
367 | | hex = ds.Get<bool>(); |
368 | | } catch ( ... ) { } |
369 | | |
370 | | |
371 | | if ( hex == true ) { |
372 | | CF_CHECK_EQ(mp_radix_size(mp, 16, &size), MP_OKAY); |
373 | | CF_ASSERT(size > 0, "Output of mp_radix_size is 0 or less"); |
374 | | |
375 | | str = (char*)util::malloc(size+1); |
376 | | |
377 | | CF_CHECK_EQ(mp_tohex(mp, str), MP_OKAY); |
378 | | ret = { util::HexToDec(str) }; |
379 | | } else { |
380 | | CF_CHECK_EQ(mp_radix_size(mp, 10, &size), MP_OKAY); |
381 | | CF_ASSERT(size > 0, "Output of mp_radix_size is 0 or less"); |
382 | | |
383 | | str = (char*)util::malloc(size); |
384 | | |
385 | | CF_CHECK_EQ(mp_toradix(mp, str, 10), MP_OKAY); |
386 | | ret = std::string(str); |
387 | | } |
388 | | #endif |
389 | | |
390 | 17.3k | end: |
391 | 17.3k | free(str); |
392 | | |
393 | 17.3k | return ret; |
394 | 17.3k | } |
395 | | |
396 | 32.3k | std::optional<component::Bignum> Bignum::ToComponentBignum(void) { |
397 | 32.3k | std::optional<component::Bignum> ret = std::nullopt; |
398 | | |
399 | 32.3k | auto str = ToDecString(); |
400 | 32.3k | CF_CHECK_NE(str, std::nullopt); |
401 | 31.6k | ret = { str }; |
402 | 32.3k | end: |
403 | 32.3k | return ret; |
404 | 31.6k | } |
405 | | |
406 | 27.0k | bool Bignum::ToBin(uint8_t* dest, const size_t size) { |
407 | 27.0k | bool ret = false; |
408 | | |
409 | 27.0k | const auto required = mp_unsigned_bin_size(GetPtr()); |
410 | 27.0k | CF_ASSERT(required >= 0, "mp_unsigned_bin_size returned negative value"); |
411 | | |
412 | 27.0k | CF_CHECK_GTE(size, static_cast<size_t>(required)); |
413 | 26.8k | CF_CHECK_EQ(mp_to_unsigned_bin_len(GetPtr(), dest, size), MP_OKAY); |
414 | | |
415 | 25.8k | ret = true; |
416 | 27.0k | end: |
417 | 27.0k | return ret; |
418 | 25.8k | } |
419 | | |
420 | | |
421 | 22.1k | std::optional<std::vector<uint8_t>> Bignum::ToBin(Datasource& ds, const component::Bignum b, std::optional<size_t> size) { |
422 | 22.1k | std::optional<std::vector<uint8_t>> ret = std::nullopt; |
423 | 22.1k | std::vector<uint8_t> v; |
424 | 22.1k | Bignum bn(ds); |
425 | 22.1k | uint16_t padding = 0; |
426 | | |
427 | 22.1k | CF_CHECK_EQ(bn.Set(b), true); |
428 | 21.5k | if ( size != std::nullopt ) { |
429 | 0 | v.resize(*size); |
430 | 21.5k | } else { |
431 | 21.5k | try { |
432 | 21.5k | padding = ds.Get<uint16_t>(); |
433 | 21.5k | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } |
434 | | |
435 | 21.5k | v.resize( mp_unsigned_bin_size(bn.GetPtr()) + padding ); |
436 | 21.5k | } |
437 | | |
438 | 21.5k | CF_CHECK_EQ(bn.ToBin(v.data(), v.size()), true); |
439 | | |
440 | 20.6k | ret = v; |
441 | 22.1k | end: |
442 | 22.1k | return ret; |
443 | 20.6k | } |
444 | | |
445 | | bool Bignum::ToBin( |
446 | | Datasource& ds, |
447 | | const component::Bignum b, |
448 | | uint8_t* dest, |
449 | | const size_t size, |
450 | 21.5k | const bool mustSucceed) { |
451 | 21.5k | bool ret = false; |
452 | 21.5k | if ( mustSucceed == true ) { |
453 | 17.3k | std::optional<std::vector<uint8_t>> bytes; |
454 | 17.3k | CF_CHECK_NE(bytes = util::DecToBin(b.ToTrimmedString(), size), std::nullopt); |
455 | 16.9k | memcpy(dest, bytes->data(), bytes->size()); |
456 | 16.9k | ret = true; |
457 | 16.9k | } else { |
458 | 4.23k | Bignum bn(ds); |
459 | | |
460 | 4.23k | CF_CHECK_EQ(bn.Set(b), true); |
461 | 4.09k | CF_CHECK_EQ(bn.ToBin(dest, size), true); |
462 | | |
463 | 3.97k | ret = true; |
464 | 3.97k | } |
465 | 21.5k | end: |
466 | 21.5k | return ret; |
467 | 21.5k | } |
468 | | |
469 | | bool Bignum::ToBin( |
470 | | Datasource& ds, |
471 | | const component::BignumPair b, |
472 | | uint8_t* dest, |
473 | | const size_t size, |
474 | 5.68k | const bool mustSucceed) { |
475 | 5.68k | CF_ASSERT((size % 2) == 0, "Input size is not multiple of 2 in Bignum::ToBin"); |
476 | | |
477 | 5.68k | bool ret = false; |
478 | 5.68k | const auto halfSize = size / 2; |
479 | | |
480 | 5.68k | CF_CHECK_EQ(ToBin(ds, b.first, dest, halfSize, mustSucceed), true); |
481 | 5.54k | CF_CHECK_EQ(ToBin(ds, b.second, dest + halfSize, halfSize, mustSucceed), true); |
482 | | |
483 | 5.40k | ret = true; |
484 | 5.68k | end: |
485 | 5.68k | return ret; |
486 | 5.40k | } |
487 | | |
488 | 4.90k | std::optional<component::Bignum> Bignum::BinToBignum(Datasource& ds, const uint8_t* src, const size_t size) { |
489 | 4.90k | std::optional<component::Bignum> ret = std::nullopt; |
490 | | |
491 | 4.90k | wolfCrypt_bignum::Bignum bn(ds); |
492 | 4.90k | CF_CHECK_EQ(mp_read_unsigned_bin(bn.GetPtr(), src, size), MP_OKAY); |
493 | | |
494 | 4.73k | ret = bn.ToComponentBignum(); |
495 | | |
496 | 4.90k | end: |
497 | 4.90k | return ret; |
498 | 4.73k | } |
499 | | |
500 | 3.23k | std::optional<component::BignumPair> Bignum::BinToBignumPair(Datasource& ds, const uint8_t* src, const size_t size) { |
501 | 3.23k | CF_ASSERT((size % 2) == 0, "Input size is not multiple of 2 in Bignum::BinToBignumPair"); |
502 | | |
503 | 3.23k | std::optional<component::BignumPair> ret = std::nullopt; |
504 | 3.23k | std::optional<component::Bignum> A, B; |
505 | 3.23k | const auto halfSize = size / 2; |
506 | | |
507 | 3.23k | { |
508 | 3.23k | wolfCrypt_bignum::Bignum bn(ds); |
509 | 3.23k | CF_CHECK_EQ(mp_read_unsigned_bin(bn.GetPtr(), src, halfSize), MP_OKAY); |
510 | 3.21k | CF_CHECK_NE(A = bn.ToComponentBignum(), std::nullopt); |
511 | 3.17k | } |
512 | | |
513 | 0 | { |
514 | 3.17k | wolfCrypt_bignum::Bignum bn(ds); |
515 | 3.17k | CF_CHECK_EQ(mp_read_unsigned_bin(bn.GetPtr(), src + halfSize, halfSize), MP_OKAY); |
516 | 3.16k | CF_CHECK_NE(B = bn.ToComponentBignum(), std::nullopt); |
517 | 3.14k | } |
518 | | |
519 | | |
520 | 0 | ret = {A->ToTrimmedString(), B->ToTrimmedString()}; |
521 | | |
522 | 3.23k | end: |
523 | 3.23k | return ret; |
524 | 3.14k | } |
525 | | |
526 | 53.9k | void Bignum::Randomize(void) { |
527 | 53.9k | std::vector<uint8_t> data; |
528 | 53.9k | try { |
529 | 53.9k | data = ds.GetData(0, 1, 1024); |
530 | 53.9k | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } |
531 | | |
532 | 53.9k | if ( !data.empty() ) { |
533 | 2.09k | /* ignore return value */ mp_read_unsigned_bin(GetPtrDirect(), data.data(), data.size()); |
534 | 2.09k | } |
535 | 53.9k | } |
536 | | |
537 | 8.79k | bool Bignum::operator==(const Bignum& rhs) const { |
538 | 8.79k | return mp_cmp(GetPtr(), rhs.GetPtr()) == MP_EQ; |
539 | 8.79k | } |
540 | | |
541 | | BignumCluster::BignumCluster(Datasource& ds, Bignum bn0, Bignum bn1, Bignum bn2, Bignum bn3) : |
542 | 53.9k | ds(ds), |
543 | 53.9k | bn({bn0, bn1, bn2, bn3}) |
544 | 53.9k | { } |
545 | | |
546 | 53.9k | BignumCluster::~BignumCluster() { |
547 | 269k | for (size_t i = 0; i < 4; i++) { |
548 | 215k | if ( cache.bn[i] == nullptr ) { |
549 | 3.62k | continue; |
550 | 3.62k | } |
551 | | |
552 | 212k | mp_clear(cache.bn[i]); |
553 | 212k | util::free(cache.bn[i]); |
554 | 212k | } |
555 | 53.9k | } |
556 | | |
557 | 83.8k | Bignum& BignumCluster::operator[](const size_t index) { |
558 | 83.8k | CF_ASSERT(index < bn.size(), "Invalid index requested in BignumCluster::operator[]"); |
559 | | |
560 | 83.8k | try { |
561 | | /* Rewire? */ |
562 | 83.8k | if ( ds.Get<bool>() == true ) { |
563 | | /* Pick a random bignum */ |
564 | 9.02k | const auto newIndex = ds.Get<uint8_t>() % 4; |
565 | | |
566 | | /* Same value? */ |
567 | 9.02k | if ( bn[newIndex] == bn[index] ) { |
568 | | /* Then return reference to other bignum */ |
569 | 2.75k | return bn[newIndex]; |
570 | 2.75k | } |
571 | | |
572 | | /* Fall through */ |
573 | 9.02k | } |
574 | 83.8k | } catch ( fuzzing::datasource::Datasource::OutOfData ) { } |
575 | | |
576 | 81.0k | return bn[index]; |
577 | 83.8k | } |
578 | | |
579 | 213k | bool BignumCluster::Set(const size_t index, const std::string s) { |
580 | 213k | CF_ASSERT(index < bn.size(), "Invalid index requested in BignumCluster::Set"); |
581 | | |
582 | 213k | return bn[index].Set(s); |
583 | 213k | } |
584 | | |
585 | 616 | mp_int* BignumCluster::GetDestPtr(const size_t index) { |
586 | | /* Because it is requested as a destination pointer, |
587 | | * this bignum will be altered, hence invalidate |
588 | | * the cache |
589 | | */ |
590 | 616 | InvalidateCache(); |
591 | | |
592 | 616 | return bn[index].GetPtr(); |
593 | 616 | } |
594 | | |
595 | 18.8k | mp_int* BignumCluster::GetResPtr(void) { |
596 | 18.8k | CF_ASSERT(res_index == std::nullopt, "Reusing result pointer"); |
597 | | |
598 | 18.8k | res_index = 0; |
599 | | |
600 | 18.8k | try { res_index = ds.Get<uint8_t>() % 4; } |
601 | 18.8k | catch ( fuzzing::datasource::Datasource::OutOfData ) { } |
602 | | |
603 | 18.8k | InvalidateCache(); |
604 | | |
605 | 18.8k | return bn[*res_index].GetPtr(); |
606 | 18.8k | } |
607 | | |
608 | 12.9k | bool BignumCluster::CopyResult(Bignum& res) const { |
609 | 12.9k | bool ret = false; |
610 | | |
611 | 12.9k | CF_ASSERT(res_index != std::nullopt, "Result index is undefined"); |
612 | 12.9k | wolfCrypt_detail::disableAllocationFailures = true; |
613 | | |
614 | 12.9k | const auto src = bn[*res_index].GetPtrDirect(); |
615 | 12.9k | auto dest = res.GetPtr(); |
616 | | |
617 | 12.9k | if ( mp_copy(src, dest) != MP_OKAY ) { |
618 | | #if defined(USE_FAST_MATH) |
619 | | goto end; |
620 | | #elif defined(USE_INTEGER_HEAP_MATH) |
621 | | CF_NORET(mp_clear(dest)); |
622 | | CF_ASSERT(mp_init_size(dest, src->alloc) == 0, "Cannot initialze result"); |
623 | | CF_ASSERT( |
624 | | mp_copy(src, dest) == MP_OKAY, |
625 | | "mp_copy failed unexpectedly"); |
626 | | #else |
627 | 187 | CF_NORET(mp_clear(dest)); |
628 | 187 | CF_ASSERT(mp_init_size(dest, src->size) == 0, "Cannot initialze result"); |
629 | 187 | CF_ASSERT( |
630 | 187 | mp_copy(src, dest) == MP_OKAY, |
631 | 187 | "mp_copy failed unexpectedly"); |
632 | 187 | #endif |
633 | 187 | } |
634 | | |
635 | 12.9k | ret = true; |
636 | | #if defined(USE_FAST_MATH) |
637 | | end: |
638 | | #endif |
639 | 12.9k | wolfCrypt_detail::disableAllocationFailures = false; |
640 | 12.9k | return ret; |
641 | 12.9k | } |
642 | | |
643 | 53.0k | void BignumCluster::Save(void) { |
644 | 265k | for (size_t i = 0; i < 4; i++) { |
645 | 212k | mp_int* cached_mp = (mp_int*)util::malloc(sizeof(mp_int)); |
646 | | |
647 | 212k | wolfCrypt_detail::disableAllocationFailures = true; |
648 | | |
649 | 212k | CF_ASSERT(mp_init(cached_mp) == MP_OKAY, "mp_init failed unexpectedly"); |
650 | 212k | CF_ASSERT(mp_copy(bn[i].GetPtrDirect(), cached_mp) == MP_OKAY, "mp_copy failed unexpectedly"); |
651 | | |
652 | 212k | wolfCrypt_detail::disableAllocationFailures = false; |
653 | | |
654 | 212k | cache.bn[i] = cached_mp; |
655 | 212k | } |
656 | 53.0k | } |
657 | | |
658 | 19.5k | void BignumCluster::InvalidateCache(void) { |
659 | 19.5k | cache.invalid = true; |
660 | 19.5k | } |
661 | | |
662 | 21.2k | bool BignumCluster::EqualsCache(void) const { |
663 | 21.2k | if ( cache.invalid == true ) { |
664 | 13.8k | return true; |
665 | 13.8k | } |
666 | | |
667 | 36.7k | for (size_t i = 0; i < 4; i++) { |
668 | 29.3k | if ( cache.bn[i] == nullptr ) { |
669 | 0 | continue; |
670 | 0 | } |
671 | | |
672 | 29.3k | wolfCrypt_detail::disableAllocationFailures = true; |
673 | | |
674 | 29.3k | if ( mp_cmp(bn[i].GetPtrDirect(), cache.bn[i]) != MP_EQ ) { |
675 | | #if defined(CRYPTOFUZZ_WOLFCRYPT_DEBUG) |
676 | | char str[8192]; |
677 | | |
678 | | std::cout << "Bignum with index " << std::to_string(i) << " was changed" << std::endl; |
679 | | |
680 | | wolfCrypt_detail::disableAllocationFailures = true; |
681 | | |
682 | | CF_ASSERT(mp_tohex(cache.bn[i], str) == MP_OKAY, "mp_tohex failed unexpectedly"); |
683 | | printf("it was: %s\n", str); |
684 | | |
685 | | CF_ASSERT(mp_tohex(bn[i].GetPtrDirect(), str) == MP_OKAY, "mp_tohex failed unexpectedly"); |
686 | | printf("it is now %s\n", str); |
687 | | |
688 | | #endif |
689 | 0 | wolfCrypt_detail::disableAllocationFailures = false; |
690 | |
|
691 | 0 | return false; |
692 | 0 | } |
693 | | |
694 | 29.3k | wolfCrypt_detail::disableAllocationFailures = false; |
695 | 29.3k | } |
696 | | |
697 | 7.34k | return true; |
698 | 7.34k | } |
699 | | |
700 | | } /* namespace wolfCrypt_bignum */ |
701 | | } /* namespace module */ |
702 | | } /* namespace cryptofuzz */ |