/src/cryptofuzz/modules/wolfcrypt/bn_helper.cpp
Line  | Count  | Source (jump to first uncovered line)  | 
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  | 19.3k  | Bignum::read_radix_error_t Bignum::read_radix(mp_int* dest, const std::string& str, const size_t base) { | 
17  | 19.3k  |     return read_radix(dest, str.c_str(), base);  | 
18  | 19.3k  | }  | 
19  |  |  | 
20  | 19.4k  | Bignum::read_radix_error_t Bignum::read_radix(mp_int* dest, const char* str, const size_t base) { | 
21  | 19.4k  |     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  | 19.4k  |     auto newMp = (mp_int*)util::malloc(sizeof(mp_int));  | 
29  | 19.4k  |     if ( mp_init(newMp) != MP_OKAY ) { | 
30  | 0  |         util::free(newMp);  | 
31  | 0  |         return READ_RADIX_FAIL_MEMORY;  | 
32  | 0  |     }  | 
33  |  |  | 
34  | 19.4k  |     wolfCrypt_detail::haveAllocFailure = false;  | 
35  | 19.4k  |     if ( mp_read_radix(newMp, str, base) != MP_OKAY ) { | 
36  | 692  |         ret = wolfCrypt_detail::haveAllocFailure ? READ_RADIX_FAIL_MEMORY : READ_RADIX_FAIL_OTHER;  | 
37  | 692  |         goto end;  | 
38  | 692  |     }  | 
39  |  |  | 
40  | 18.7k  |     wolfCrypt_detail::haveAllocFailure = false;  | 
41  | 18.7k  |     if ( mp_copy(newMp, dest) != MP_OKAY ) { | 
42  | 224  |         ret = wolfCrypt_detail::haveAllocFailure ? READ_RADIX_FAIL_MEMORY : READ_RADIX_FAIL_OTHER;  | 
43  | 224  |         goto end;  | 
44  | 224  |     }  | 
45  |  |  | 
46  | 18.4k  |     ret = READ_RADIX_OK;  | 
47  |  |  | 
48  | 19.4k  | end:  | 
49  | 19.4k  |     CF_NORET(mp_clear(newMp));  | 
50  | 19.4k  |     util::free(newMp);  | 
51  |  |  | 
52  | 19.4k  |     return ret;  | 
53  | 18.4k  | }  | 
54  |  |  | 
55  | 2.51k  | void Bignum::baseConversion(void) const { | 
56  | 2.51k  | #if !defined(WOLFSSL_SP_MATH)  | 
57  | 2.51k  |     uint8_t base = 2;  | 
58  | 2.51k  |     char* str = nullptr;  | 
59  |  |  | 
60  | 2.51k  |     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  | 2.51k  |     { | 
66  | 2.51k  |         int size;  | 
67  | 2.51k  |         CF_CHECK_EQ(mp_radix_size(mp, base, &size), MP_OKAY);  | 
68  | 65  |         CF_ASSERT(size > 0, "Output of mp_radix_size is 0 or less");  | 
69  |  |  | 
70  | 65  |         str = (char*)util::malloc(size);  | 
71  |  |  | 
72  | 65  | #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL) || !defined(USE_FAST_MATH)  | 
73  | 65  |         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  | 54  |         { | 
88  | 54  |             const auto ret = read_radix(mp, str, base);  | 
89  | 54  |             CF_ASSERT(ret == READ_RADIX_OK || ret == READ_RADIX_FAIL_MEMORY, "wolfCrypt cannot parse the output of mp_toradix");  | 
90  | 54  |         }  | 
91  | 54  |     }  | 
92  |  |  | 
93  | 2.51k  | end:  | 
94  | 2.51k  |     util::free(str);  | 
95  | 2.51k  | #endif  | 
96  | 2.51k  | }  | 
97  |  |  | 
98  | 2.54k  | void Bignum::binaryConversion(void) const { | 
99  | 2.54k  |     uint8_t* data = nullptr;  | 
100  | 2.54k  |     CF_CHECK_EQ(mp_isneg(mp), 0);  | 
101  |  |  | 
102  | 2.54k  | #if LIBWOLFSSL_VERSION_HEX == 0x05005001  | 
103  | 2.54k  |     { | 
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  | 2.54k  |         const auto size = mp_unsigned_bin_size(mp);  | 
114  | 2.54k  |         CF_ASSERT(size >= 0, "mp_unsigned_bin_size returned negative value");  | 
115  |  |  | 
116  | 2.54k  |         data = util::malloc(size);  | 
117  | 2.54k  |         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  | 1.98k  |         const auto cached_disableAllocationFailures = wolfCrypt_detail::disableAllocationFailures;  | 
123  | 1.98k  |         wolfCrypt_detail::disableAllocationFailures = true;  | 
124  |  |  | 
125  | 1.98k  |         CF_ASSERT(mp_read_unsigned_bin(mp, data, size) == MP_OKAY, "Cannot parse output of mp_to_unsigned_bin_len");  | 
126  |  |  | 
127  | 1.98k  |         wolfCrypt_detail::disableAllocationFailures = cached_disableAllocationFailures;  | 
128  | 1.98k  |     }  | 
129  |  | #else  | 
130  |  |     { | 
131  |  |         bool randomSize = false;  | 
132  |  |  | 
133  |  |         try { randomSize = ds.Get<bool>(); } catch ( fuzzing::datasource::Datasource::OutOfData ) { } | 
134  |  |  | 
135  |  |         size_t size = 0;  | 
136  |  |         int numBits = 0;  | 
137  |  |  | 
138  |  |         if ( randomSize == false ) { | 
139  |  |             const auto size2 = mp_unsigned_bin_size(mp);  | 
140  |  |             CF_ASSERT(size2 >= 0, "mp_unsigned_bin_size returned negative value");  | 
141  |  |             size = size2;  | 
142  |  |         } else { | 
143  |  |             try { | 
144  |  |                 size = ds.Get<uint16_t>();  | 
145  |  |             } catch ( fuzzing::datasource::Datasource::OutOfData ) { } | 
146  |  |  | 
147  |  |             numBits = mp_count_bits(mp);  | 
148  |  |             CF_ASSERT(numBits >= 0, "mp_count_bits result is negative");  | 
149  |  |         }  | 
150  |  |  | 
151  |  |         data = util::malloc(size);  | 
152  |  |         CF_CHECK_EQ(mp_to_unsigned_bin_len(mp, data, size), MP_OKAY);  | 
153  |  |  | 
154  |  |         CF_ASSERT(  | 
155  |  |                 size * 8 >= static_cast<size_t>(numBits),  | 
156  |  |                 "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  |  |         const auto cached_disableAllocationFailures = wolfCrypt_detail::disableAllocationFailures;  | 
162  |  |         wolfCrypt_detail::disableAllocationFailures = true;  | 
163  |  |  | 
164  |  |         if ( randomSize == false ) { | 
165  |  |             CF_ASSERT(mp_read_unsigned_bin(mp, data, size) == MP_OKAY, "Cannot parse output of mp_to_unsigned_bin_len");  | 
166  |  |         } 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  |  |             Bignum tmp(ds);  | 
173  |  |             if ( mp_read_unsigned_bin(tmp.GetPtrDirect(), data, size) == MP_OKAY ) { | 
174  |  |                 /* ignore result */ mp_copy(tmp.GetPtrDirect(), mp);  | 
175  |  |             }  | 
176  |  |         }  | 
177  |  |  | 
178  |  |         wolfCrypt_detail::disableAllocationFailures = cached_disableAllocationFailures;  | 
179  |  |     }  | 
180  |  | #endif  | 
181  |  |  | 
182  |  |  | 
183  | 2.54k  | end:  | 
184  | 2.54k  |     util::free(data);  | 
185  | 2.54k  | }  | 
186  |  |  | 
187  | 88.3k  | void Bignum::invariants(void) const { | 
188  | 88.3k  | #if defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)  | 
189  | 88.3k  |     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  | 88.3k  | }  | 
194  |  |  | 
195  |  | Bignum::Bignum(Datasource& ds) :  | 
196  | 28.2k  |     ds(ds) { | 
197  | 28.2k  |     mp = (mp_int*)util::malloc(sizeof(mp_int));  | 
198  |  |  | 
199  | 28.2k  |     if ( init_mp_int(mp, ds) != MP_OKAY ) { | 
200  | 0  |         util::free(mp);  | 
201  | 0  |         throw std::exception();  | 
202  | 0  |     }  | 
203  | 28.2k  | }  | 
204  |  |  | 
205  |  | Bignum::Bignum(mp_int* mp, Datasource& ds) :  | 
206  |  |     mp(mp),  | 
207  |  |     ds(ds),  | 
208  |  |     noFree(true)  | 
209  | 1.06k  | { } | 
210  |  |  | 
211  |  | Bignum::Bignum(const Bignum& other) :  | 
212  | 17.7k  |     ds(other.ds) { | 
213  | 17.7k  |     mp = (mp_int*)util::malloc(sizeof(mp_int));  | 
214  | 17.7k  |     if ( init_mp_int(mp, ds) != MP_OKAY ) { | 
215  | 0  |         util::free(mp);  | 
216  | 0  |         throw std::exception();  | 
217  | 0  |     }  | 
218  | 17.7k  |     if ( mp_copy(other.mp, mp) != MP_OKAY ) { | 
219  | 0  |         util::free(mp);  | 
220  | 0  |         throw std::exception();  | 
221  | 0  |     }  | 
222  | 17.7k  | }  | 
223  |  |  | 
224  |  | Bignum::Bignum(const Bignum&& other) :  | 
225  | 17.7k  |     ds(other.ds) { | 
226  | 17.7k  |     mp = (mp_int*)util::malloc(sizeof(mp_int));  | 
227  | 17.7k  |     if ( init_mp_int(mp, ds) != MP_OKAY ) { | 
228  | 0  |         util::free(mp);  | 
229  | 0  |         throw std::exception();  | 
230  | 0  |     }  | 
231  | 17.7k  |     if ( mp_copy(other.mp, mp) != MP_OKAY ) { | 
232  | 0  |         util::free(mp);  | 
233  | 0  |         throw std::exception();  | 
234  | 0  |     }  | 
235  | 17.7k  | }  | 
236  |  |  | 
237  | 64.7k  | Bignum::~Bignum() { | 
238  | 64.7k  |     invariants();  | 
239  | 64.7k  |     if ( noFree == false ) { | 
240  | 63.6k  |         CF_NORET(mp_clear(mp));  | 
241  | 63.6k  |         util::free(mp);  | 
242  | 63.6k  |     }  | 
243  | 64.7k  | }  | 
244  |  |  | 
245  | 0  | void Bignum::SetNoFree(void) { | 
246  | 0  |     noFree = true;  | 
247  | 0  | }  | 
248  |  |  | 
249  | 19.3k  | bool Bignum::Set(const std::string s) { | 
250  | 19.3k  |     bool ret = false;  | 
251  |  |  | 
252  | 19.3k  |     bool hex = false;  | 
253  | 19.3k  |     try { | 
254  | 19.3k  |         hex = ds.Get<bool>();  | 
255  | 19.3k  |     } catch ( ... ) { } | 
256  |  |  | 
257  |  | #if defined(WOLFSSL_SP_MATH)  | 
258  |  |     hex = true;  | 
259  |  | #endif  | 
260  |  |  | 
261  | 19.3k  |     if ( hex == true ) { | 
262  | 2.58k  |         CF_CHECK_EQ(read_radix(mp, util::DecToHex(s), 16), READ_RADIX_OK);  | 
263  | 16.7k  |     } else { | 
264  | 16.7k  |         CF_CHECK_EQ(read_radix(mp, s, 10), READ_RADIX_OK);  | 
265  | 15.9k  |     }  | 
266  |  |  | 
267  | 18.4k  |     ret = true;  | 
268  | 19.3k  | end:  | 
269  | 19.3k  |     return ret;  | 
270  | 18.4k  | }  | 
271  |  |  | 
272  | 1.66k  | bool Bignum::Set(const component::Bignum i) { | 
273  | 1.66k  |     bool ret = false;  | 
274  |  |  | 
275  | 1.66k  |     CF_CHECK_EQ(Set(i.ToString()), true);  | 
276  |  |  | 
277  | 1.65k  |     ret = true;  | 
278  | 1.66k  | end:  | 
279  | 1.66k  |     return ret;  | 
280  | 1.65k  | }  | 
281  |  |  | 
282  | 23.6k  | mp_int* Bignum::GetPtr(void) const { | 
283  | 23.6k  |     invariants();  | 
284  |  |  | 
285  | 23.6k  |     { | 
286  |  |         /* Optionally clamp the bignum. This should not affect its value. */  | 
287  |  |  | 
288  | 23.6k  |         bool clamp = false;  | 
289  |  |  | 
290  | 23.6k  |         try { clamp = ds.Get<bool>(); } catch ( fuzzing::datasource::Datasource::OutOfData ) { } | 
291  |  |  | 
292  | 23.6k  |         if ( clamp ) { | 
293  |  |             /* Implemented as a macro so CF_NORET cannot be used here */  | 
294  | 2.60k  |             /* noret */ mp_clamp(mp);  | 
295  | 2.60k  |         }  | 
296  | 23.6k  |     }  | 
297  |  |  | 
298  | 23.6k  |     { | 
299  |  |         /* Optionally convert to a random base and back */  | 
300  |  |  | 
301  | 23.6k  |         bool convert = false;  | 
302  |  |  | 
303  | 23.6k  |         try { convert = ds.Get<bool>(); } catch ( fuzzing::datasource::Datasource::OutOfData ) { } | 
304  |  |  | 
305  | 23.6k  |         if ( convert ) { | 
306  | 2.51k  |             baseConversion();  | 
307  | 2.51k  |         }  | 
308  | 23.6k  |     }  | 
309  |  |  | 
310  | 23.6k  |     { | 
311  |  |         /* Optionally convert to bytes and back */  | 
312  |  |  | 
313  | 23.6k  |         bool convert = false;  | 
314  |  |  | 
315  | 23.6k  |         try { convert = ds.Get<bool>(); } catch ( fuzzing::datasource::Datasource::OutOfData ) { } | 
316  |  |  | 
317  | 23.6k  |         if ( convert ) { | 
318  | 2.54k  |             binaryConversion();  | 
319  | 2.54k  |         }  | 
320  | 23.6k  |     }  | 
321  |  |  | 
322  | 23.6k  |     return mp;  | 
323  | 23.6k  | }  | 
324  |  |  | 
325  | 23.9k  | mp_int* Bignum::GetPtrDirect(void) const { | 
326  | 23.9k  |     return mp;  | 
327  | 23.9k  | }  | 
328  |  |  | 
329  | 311  | std::optional<uint64_t> Bignum::AsUint64(void) const { | 
330  | 311  |     std::optional<uint64_t> ret = std::nullopt;  | 
331  | 311  |     uint64_t v = 0;  | 
332  |  |  | 
333  | 311  | #if !defined(WOLFSSL_SP_MATH)  | 
334  | 311  |     CF_CHECK_EQ(mp_isneg(mp), 0);  | 
335  | 311  | #endif  | 
336  | 311  |     CF_CHECK_LTE(mp_count_bits(mp), (int)(sizeof(v) * 8));  | 
337  | 284  |     CF_CHECK_EQ(mp_to_unsigned_bin_len(mp, (uint8_t*)&v, sizeof(v)), MP_OKAY);  | 
338  | 284  |     v =  | 
339  | 284  |         ((v & 0xFF00000000000000) >> 56) |  | 
340  | 284  |         ((v & 0x00FF000000000000) >> 40) |  | 
341  | 284  |         ((v & 0x0000FF0000000000) >> 24) |  | 
342  | 284  |         ((v & 0x000000FF00000000) >>  8) |  | 
343  | 284  |         ((v & 0x00000000FF000000) <<  8) |  | 
344  | 284  |         ((v & 0x0000000000FF0000) << 24) |  | 
345  | 284  |         ((v & 0x000000000000FF00) << 40) |  | 
346  | 284  |         ((v & 0x00000000000000FF) << 56);  | 
347  |  |  | 
348  | 284  |     ret = v;  | 
349  | 311  | end:  | 
350  | 311  |     return ret;  | 
351  | 284  | }  | 
352  |  |  | 
353  | 4.89k  | std::optional<std::string> Bignum::ToDecString(void) { | 
354  | 4.89k  |     std::optional<std::string> ret = std::nullopt;  | 
355  | 4.89k  |     char* str = nullptr;  | 
356  |  |  | 
357  |  | #if defined(WOLFSSL_SP_MATH)  | 
358  |  |     str = (char*)util::malloc(8192);  | 
359  |  |  | 
360  |  |     CF_CHECK_EQ(mp_tohex(mp, str), MP_OKAY);  | 
361  |  |     ret = { util::HexToDec(str) }; | 
362  |  | #else  | 
363  | 4.89k  |     bool hex = false;  | 
364  | 4.89k  |     int size;  | 
365  |  |  | 
366  | 4.89k  |     try { | 
367  | 4.89k  |         hex = ds.Get<bool>();  | 
368  | 4.89k  |     } catch ( ... ) { } | 
369  |  |  | 
370  |  |  | 
371  | 4.89k  |     if ( hex == true ) { | 
372  | 407  |         CF_CHECK_EQ(mp_radix_size(mp, 16, &size), MP_OKAY);  | 
373  | 407  |         CF_ASSERT(size > 0, "Output of mp_radix_size is 0 or less");  | 
374  |  |  | 
375  | 407  |         str = (char*)util::malloc(size+1);  | 
376  |  |  | 
377  | 407  |         CF_CHECK_EQ(mp_tohex(mp, str), MP_OKAY);  | 
378  | 407  |         ret = { util::HexToDec(str) }; | 
379  | 4.48k  |     } else { | 
380  | 4.48k  |         CF_CHECK_EQ(mp_radix_size(mp, 10, &size), MP_OKAY);  | 
381  | 4.34k  |         CF_ASSERT(size > 0, "Output of mp_radix_size is 0 or less");  | 
382  |  |  | 
383  | 4.34k  |         str = (char*)util::malloc(size);  | 
384  |  |  | 
385  | 4.34k  |         CF_CHECK_EQ(mp_toradix(mp, str, 10), MP_OKAY);  | 
386  | 4.27k  |         ret = std::string(str);  | 
387  | 4.27k  |     }  | 
388  | 4.68k  | #endif  | 
389  |  |  | 
390  | 4.89k  | end:  | 
391  | 4.89k  |     free(str);  | 
392  |  |  | 
393  | 4.89k  |     return ret;  | 
394  | 4.89k  | }  | 
395  |  |  | 
396  | 2.59k  | std::optional<component::Bignum> Bignum::ToComponentBignum(void) { | 
397  | 2.59k  |     std::optional<component::Bignum> ret = std::nullopt;  | 
398  |  |  | 
399  | 2.59k  |     auto str = ToDecString();  | 
400  | 2.59k  |     CF_CHECK_NE(str, std::nullopt);  | 
401  | 2.50k  |     ret = { str }; | 
402  | 2.59k  | end:  | 
403  | 2.59k  |     return ret;  | 
404  | 2.50k  | }  | 
405  |  |  | 
406  | 904  | bool Bignum::ToBin(uint8_t* dest, const size_t size) { | 
407  | 904  |     bool ret = false;  | 
408  |  |  | 
409  | 904  |     const auto required = mp_unsigned_bin_size(GetPtr());  | 
410  | 904  |     CF_ASSERT(required >= 0, "mp_unsigned_bin_size returned negative value");  | 
411  |  |  | 
412  | 904  |     CF_CHECK_GTE(size, static_cast<size_t>(required));  | 
413  | 897  |     CF_CHECK_EQ(mp_to_unsigned_bin_len(GetPtr(), dest, size), MP_OKAY);  | 
414  |  |  | 
415  | 880  |     ret = true;  | 
416  | 904  | end:  | 
417  | 904  |     return ret;  | 
418  | 880  | }  | 
419  |  |  | 
420  |  |  | 
421  | 723  | std::optional<std::vector<uint8_t>> Bignum::ToBin(Datasource& ds, const component::Bignum b, std::optional<size_t> size) { | 
422  | 723  |     std::optional<std::vector<uint8_t>> ret = std::nullopt;  | 
423  | 723  |     std::vector<uint8_t> v;  | 
424  | 723  |     Bignum bn(ds);  | 
425  | 723  |     uint16_t padding = 0;  | 
426  |  |  | 
427  | 723  |     CF_CHECK_EQ(bn.Set(b), true);  | 
428  | 721  |     if ( size != std::nullopt ) { | 
429  | 0  |         v.resize(*size);  | 
430  | 721  |     } else { | 
431  | 721  |         try { | 
432  | 721  |             padding = ds.Get<uint16_t>();  | 
433  | 721  |         } catch ( fuzzing::datasource::Datasource::OutOfData ) { } | 
434  |  |  | 
435  | 721  |         v.resize( mp_unsigned_bin_size(bn.GetPtr()) + padding );  | 
436  | 721  |     }  | 
437  |  |  | 
438  | 721  |     CF_CHECK_EQ(bn.ToBin(v.data(), v.size()), true);  | 
439  |  |  | 
440  | 704  |     ret = v;  | 
441  | 723  | end:  | 
442  | 723  |     return ret;  | 
443  | 704  | }  | 
444  |  |  | 
445  |  | bool Bignum::ToBin(  | 
446  |  |         Datasource& ds,  | 
447  |  |         const component::Bignum b,  | 
448  |  |         uint8_t* dest,  | 
449  |  |         const size_t size,  | 
450  | 550  |         const bool mustSucceed) { | 
451  | 550  |     bool ret = false;  | 
452  | 550  |     if ( mustSucceed == true ) { | 
453  | 436  |         std::optional<std::vector<uint8_t>> bytes;  | 
454  | 436  |         CF_CHECK_NE(bytes = util::DecToBin(b.ToTrimmedString(), size), std::nullopt);  | 
455  | 428  |         memcpy(dest, bytes->data(), bytes->size());  | 
456  | 428  |         ret = true;  | 
457  | 428  |     } else { | 
458  | 114  |         Bignum bn(ds);  | 
459  |  |  | 
460  | 114  |         CF_CHECK_EQ(bn.Set(b), true);  | 
461  | 112  |         CF_CHECK_EQ(bn.ToBin(dest, size), true);  | 
462  |  |  | 
463  | 110  |         ret = true;  | 
464  | 110  |     }  | 
465  | 550  | end:  | 
466  | 550  |     return ret;  | 
467  | 550  | }  | 
468  |  |  | 
469  |  | bool Bignum::ToBin(  | 
470  |  |         Datasource& ds,  | 
471  |  |         const component::BignumPair b,  | 
472  |  |         uint8_t* dest,  | 
473  |  |         const size_t size,  | 
474  | 146  |         const bool mustSucceed) { | 
475  | 146  |     CF_ASSERT((size % 2) == 0, "Input size is not multiple of 2 in Bignum::ToBin");  | 
476  |  |  | 
477  | 146  |     bool ret = false;  | 
478  | 146  |     const auto halfSize = size / 2;  | 
479  |  |  | 
480  | 146  |     CF_CHECK_EQ(ToBin(ds, b.first, dest, halfSize, mustSucceed), true);  | 
481  | 142  |     CF_CHECK_EQ(ToBin(ds, b.second, dest + halfSize, halfSize, mustSucceed), true);  | 
482  |  |  | 
483  | 140  |     ret = true;  | 
484  | 146  | end:  | 
485  | 146  |     return ret;  | 
486  | 140  | }  | 
487  |  |  | 
488  | 83  | std::optional<component::Bignum> Bignum::BinToBignum(Datasource& ds, const uint8_t* src, const size_t size) { | 
489  | 83  |     std::optional<component::Bignum> ret = std::nullopt;  | 
490  |  |  | 
491  | 83  |     wolfCrypt_bignum::Bignum bn(ds);  | 
492  | 83  |     CF_CHECK_EQ(mp_read_unsigned_bin(bn.GetPtr(), src, size), MP_OKAY);  | 
493  |  |  | 
494  | 83  |     ret = bn.ToComponentBignum();  | 
495  |  |  | 
496  | 83  | end:  | 
497  | 83  |     return ret;  | 
498  | 83  | }  | 
499  |  |  | 
500  | 73  | std::optional<component::BignumPair> Bignum::BinToBignumPair(Datasource& ds, const uint8_t* src, const size_t size) { | 
501  | 73  |     CF_ASSERT((size % 2) == 0, "Input size is not multiple of 2 in Bignum::BinToBignumPair");  | 
502  |  |  | 
503  | 73  |     std::optional<component::BignumPair> ret = std::nullopt;  | 
504  | 73  |     std::optional<component::Bignum> A, B;  | 
505  | 73  |     const auto halfSize = size / 2;  | 
506  |  |  | 
507  | 73  |     { | 
508  | 73  |         wolfCrypt_bignum::Bignum bn(ds);  | 
509  | 73  |         CF_CHECK_EQ(mp_read_unsigned_bin(bn.GetPtr(), src, halfSize), MP_OKAY);  | 
510  | 72  |         CF_CHECK_NE(A = bn.ToComponentBignum(), std::nullopt);  | 
511  | 71  |     }  | 
512  |  |  | 
513  | 0  |     { | 
514  | 71  |         wolfCrypt_bignum::Bignum bn(ds);  | 
515  | 71  |         CF_CHECK_EQ(mp_read_unsigned_bin(bn.GetPtr(), src + halfSize, halfSize), MP_OKAY);  | 
516  | 69  |         CF_CHECK_NE(B = bn.ToComponentBignum(), std::nullopt);  | 
517  | 68  |     }  | 
518  |  |  | 
519  |  |  | 
520  | 0  |     ret = {A->ToTrimmedString(), B->ToTrimmedString()}; | 
521  |  |  | 
522  | 73  | end:  | 
523  | 73  |     return ret;  | 
524  | 68  | }  | 
525  |  |  | 
526  | 4.42k  | void Bignum::Randomize(void) { | 
527  | 4.42k  |     std::vector<uint8_t> data;  | 
528  | 4.42k  |     try { | 
529  | 4.42k  |         data = ds.GetData(0, 1, 1024);  | 
530  | 4.42k  |     } catch ( fuzzing::datasource::Datasource::OutOfData ) { } | 
531  |  |  | 
532  | 4.42k  |     if ( !data.empty() ) { | 
533  |  |         /* ignore return value */ mp_read_unsigned_bin(GetPtrDirect(), data.data(), data.size());  | 
534  | 404  |     }  | 
535  | 4.42k  | }  | 
536  |  |  | 
537  | 898  | bool Bignum::operator==(const Bignum& rhs) const { | 
538  | 898  |     return mp_cmp(GetPtr(), rhs.GetPtr()) == MP_EQ;  | 
539  | 898  | }  | 
540  |  |  | 
541  |  | BignumCluster::BignumCluster(Datasource& ds, Bignum bn0, Bignum bn1, Bignum bn2, Bignum bn3) :  | 
542  |  |     ds(ds),  | 
543  |  |     bn({bn0, bn1, bn2, bn3}) | 
544  | 4.42k  | { } | 
545  |  |  | 
546  | 4.42k  | BignumCluster::~BignumCluster() { | 
547  | 22.1k  |     for (size_t i = 0; i < 4; i++) { | 
548  | 17.7k  |         if ( cache.bn[i] == nullptr ) { | 
549  | 3.55k  |             continue;  | 
550  | 3.55k  |         }  | 
551  |  |  | 
552  | 14.1k  |         mp_clear(cache.bn[i]);  | 
553  | 14.1k  |         util::free(cache.bn[i]);  | 
554  | 14.1k  |     }  | 
555  | 4.42k  | }  | 
556  |  |  | 
557  | 9.04k  | Bignum& BignumCluster::operator[](const size_t index) { | 
558  | 9.04k  |     CF_ASSERT(index < bn.size(), "Invalid index requested in BignumCluster::operator[]");  | 
559  |  |  | 
560  | 9.04k  |     try { | 
561  |  |         /* Rewire? */  | 
562  | 9.04k  |         if ( ds.Get<bool>() == true ) { | 
563  |  |             /* Pick a random bignum */  | 
564  | 920  |             const auto newIndex = ds.Get<uint8_t>() % 4;  | 
565  |  |  | 
566  |  |             /* Same value? */  | 
567  | 920  |             if ( bn[newIndex] == bn[index] ) { | 
568  |  |                 /* Then return reference to other bignum */  | 
569  | 278  |                 return bn[newIndex];  | 
570  | 278  |             }  | 
571  |  |  | 
572  |  |             /* Fall through */  | 
573  | 920  |         }  | 
574  | 9.04k  |     } catch ( fuzzing::datasource::Datasource::OutOfData ) { } | 
575  |  |  | 
576  | 8.77k  |     return bn[index];  | 
577  | 9.04k  | }  | 
578  |  |  | 
579  | 15.2k  | bool BignumCluster::Set(const size_t index, const std::string s) { | 
580  | 15.2k  |     CF_ASSERT(index < bn.size(), "Invalid index requested in BignumCluster::Set");  | 
581  |  |  | 
582  | 15.2k  |     return bn[index].Set(s);  | 
583  | 15.2k  | }  | 
584  |  |  | 
585  | 28  | 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  | 28  |     InvalidateCache();  | 
591  |  |  | 
592  | 28  |     return bn[index].GetPtr();  | 
593  | 28  | }  | 
594  |  |  | 
595  | 3.53k  | void BignumCluster::Save(void) { | 
596  | 17.6k  |     for (size_t i = 0; i < 4; i++) { | 
597  | 14.1k  |         mp_int* cached_mp = (mp_int*)util::malloc(sizeof(mp_int));  | 
598  |  |  | 
599  | 14.1k  |         wolfCrypt_detail::disableAllocationFailures = true;  | 
600  |  |  | 
601  | 14.1k  |         CF_ASSERT(mp_init(cached_mp) == MP_OKAY, "mp_init failed unexpectedly");  | 
602  | 14.1k  |         CF_ASSERT(mp_copy(bn[i].GetPtrDirect(), cached_mp) == MP_OKAY, "mp_copy failed unexpectedly");  | 
603  |  |  | 
604  | 14.1k  |         wolfCrypt_detail::disableAllocationFailures = false;  | 
605  |  |  | 
606  | 14.1k  |         cache.bn[i] = cached_mp;  | 
607  | 14.1k  |     }  | 
608  | 3.53k  | }  | 
609  |  |  | 
610  | 28  | void BignumCluster::InvalidateCache(void) { | 
611  | 28  |     cache.invalid = true;  | 
612  | 28  | }  | 
613  |  |  | 
614  | 2.37k  | bool BignumCluster::EqualsCache(void) const { | 
615  | 2.37k  |     if ( cache.invalid == true ) { | 
616  | 23  |         return true;  | 
617  | 23  |     }  | 
618  |  |  | 
619  | 11.7k  |     for (size_t i = 0; i < 4; i++) { | 
620  | 9.40k  |         if ( cache.bn[i] == nullptr ) { | 
621  | 0  |             continue;  | 
622  | 0  |         }  | 
623  |  |  | 
624  | 9.40k  |         wolfCrypt_detail::disableAllocationFailures = true;  | 
625  |  |  | 
626  | 9.40k  |         if ( mp_cmp(bn[i].GetPtrDirect(), cache.bn[i]) != MP_EQ ) { | 
627  |  | #if defined(CRYPTOFUZZ_WOLFCRYPT_DEBUG)  | 
628  |  |             char str[8192];  | 
629  |  |  | 
630  |  |             std::cout << "Bignum with index " << std::to_string(i) << " was changed" << std::endl;  | 
631  |  |  | 
632  |  |             wolfCrypt_detail::disableAllocationFailures = true;  | 
633  |  |  | 
634  |  |             CF_ASSERT(mp_tohex(cache.bn[i], str) == MP_OKAY, "mp_tohex failed unexpectedly");  | 
635  |  |             printf("it was: %s\n", str); | 
636  |  |  | 
637  |  |             CF_ASSERT(mp_tohex(bn[i].GetPtrDirect(), str) == MP_OKAY, "mp_tohex failed unexpectedly");  | 
638  |  |             printf("it is now %s\n", str); | 
639  |  |  | 
640  |  | #endif  | 
641  | 0  |             wolfCrypt_detail::disableAllocationFailures = false;  | 
642  |  | 
  | 
643  | 0  |             return false;  | 
644  | 0  |         }  | 
645  |  |  | 
646  | 9.40k  |         wolfCrypt_detail::disableAllocationFailures = false;  | 
647  | 9.40k  |     }  | 
648  |  |  | 
649  | 2.35k  |     return true;  | 
650  | 2.35k  | }  | 
651  |  |  | 
652  |  | } /* namespace wolfCrypt_bignum */  | 
653  |  | } /* namespace module */  | 
654  |  | } /* namespace cryptofuzz */  |