/src/nss/cryptofuzz/modules/botan/bn_ops.cpp
Line | Count | Source |
1 | | #include <cryptofuzz/util.h> |
2 | | #include <cryptofuzz/repository.h> |
3 | | #include <fuzzing/datasource/id.hpp> |
4 | | #include <botan/numthry.h> |
5 | | #include <botan/reducer.h> |
6 | | #include <botan/internal/divide.h> |
7 | | #include <botan/internal/curve_nistp.h> |
8 | | #include <botan/internal/primality.h> |
9 | | #include <botan/system_rng.h> |
10 | | |
11 | | #include "bn_ops.h" |
12 | | |
13 | | namespace cryptofuzz { |
14 | | namespace module { |
15 | | namespace Botan_bignum { |
16 | | |
17 | | namespace detail { |
18 | 84 | std::optional<size_t> To_size_t(const Bignum& bn) { |
19 | | /* TODO use #if */ |
20 | | |
21 | 84 | if ( sizeof(size_t) == 4 ) { |
22 | 0 | try { |
23 | 0 | return bn.ConstRef().to_u32bit(); |
24 | 0 | } catch ( ::Botan::Encoding_Error ) { |
25 | 0 | return std::nullopt; |
26 | 0 | } |
27 | 84 | } else if ( sizeof(size_t) == 8 ) { |
28 | 84 | if( bn.ConstRef().is_negative() ) { |
29 | 0 | return std::nullopt; |
30 | 0 | } |
31 | | |
32 | 84 | if( bn.ConstRef().bits() > 64 ) { |
33 | 3 | return std::nullopt; |
34 | 3 | } |
35 | | |
36 | 81 | uint64_t out = 0; |
37 | | |
38 | 729 | for (size_t i = 0; i != 8; ++i) { |
39 | 648 | out = (out << 8) | bn.ConstRef().byte_at(7-i); |
40 | 648 | } |
41 | | |
42 | 81 | return out; |
43 | 84 | } else { |
44 | 0 | CF_UNREACHABLE(); |
45 | 0 | } |
46 | 84 | } |
47 | | } |
48 | | |
49 | | #if !defined(CRYPTOFUZZ_BOTAN_IS_ORACLE) |
50 | 381 | #define GET_UINT8_FOR_SWITCH() ds.Get<uint8_t>() |
51 | | #else |
52 | | #define GET_UINT8_FOR_SWITCH() 0 |
53 | | #endif /* CRYPTOFUZZ_BOTAN_IS_ORACLE */ |
54 | | |
55 | 222 | #define APPLY_MODULO if (modulo != std::nullopt) res = (res.ConstRef() % modulo->ConstRef()) |
56 | | |
57 | 16 | bool Add::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
58 | 16 | (void)ds; |
59 | | |
60 | 16 | res = bn[0].Ref() + bn[1].Ref(); |
61 | | |
62 | 16 | APPLY_MODULO; |
63 | | |
64 | 16 | return true; |
65 | 16 | } |
66 | | |
67 | 24 | bool Sub::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
68 | 24 | (void)ds; |
69 | | |
70 | 24 | res = bn[0].Ref() - bn[1].Ref(); |
71 | | |
72 | 24 | APPLY_MODULO; |
73 | | |
74 | 24 | return true; |
75 | 24 | } |
76 | | |
77 | 34 | bool Mul::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
78 | 34 | (void)ds; |
79 | | |
80 | 34 | res = bn[0].Ref() * bn[1].Ref(); |
81 | | |
82 | 34 | APPLY_MODULO; |
83 | | |
84 | 34 | return true; |
85 | 34 | } |
86 | | |
87 | 56 | bool Div::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
88 | 56 | (void)modulo; |
89 | 56 | (void)ds; |
90 | | |
91 | | /* Botan handles negative division different than |
92 | | * other libraries. |
93 | | */ |
94 | 56 | CF_CHECK_TRUE(bn[0].Ref() > 0); |
95 | 55 | CF_CHECK_TRUE(bn[1].Ref() > 0); |
96 | | |
97 | 54 | try { |
98 | 54 | switch ( GET_UINT8_FOR_SWITCH() ) { |
99 | 4 | case 0: |
100 | | /* / operator */ |
101 | 4 | res = bn[0].Ref() / bn[1].Ref(); |
102 | 4 | return true; |
103 | 3 | case 1: |
104 | 3 | { |
105 | 3 | CF_CHECK_TRUE(bn[1].Ref() != 0); |
106 | 3 | Bignum dummy; |
107 | 3 | /* noret */ ::Botan::vartime_divide(bn[0].Ref(), bn[1].Ref(), res.Ref(), dummy.Ref()); |
108 | 3 | } |
109 | 0 | return true; |
110 | 2 | case 2: |
111 | 2 | { |
112 | 2 | CF_CHECK_GT(bn[1].Ref(), 0); |
113 | 2 | CF_CHECK_TRUE(bn[1].Ref() < 256); |
114 | 1 | ::Botan::word dummy; |
115 | 1 | CF_NORET(::Botan::ct_divide_word(bn[0].Ref(), bn[1].Ref().word_at(0), res.Ref(), dummy)); |
116 | 1 | } |
117 | 0 | return true; |
118 | 6 | case 3: |
119 | 6 | CF_CHECK_TRUE(bn[1].Ref() != 0); |
120 | 6 | res = ::Botan::ct_divide(bn[0].Ref(), bn[1].Ref()); |
121 | 6 | return true; |
122 | 4 | case 4: |
123 | | /* /= operator */ |
124 | 4 | res = bn[0].Ref(); |
125 | 4 | res.Ref() /= bn[1].Ref(); |
126 | 4 | return true; |
127 | 54 | } |
128 | 54 | } catch ( fuzzing::datasource::Datasource::OutOfData ) { |
129 | 23 | return false; |
130 | 23 | } catch ( ::Botan::Invalid_Argument& e ) { |
131 | | /* Botan is expected to throw an exception when divisor is 0 */ |
132 | 0 | if ( bn[1].Ref() == 0 ) { |
133 | 0 | return false; |
134 | 0 | } |
135 | | |
136 | | /* Rethrow */ |
137 | 0 | throw e; |
138 | 0 | } |
139 | | |
140 | 15 | end: |
141 | 15 | return false; |
142 | 54 | } |
143 | | |
144 | 100 | bool Mod::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
145 | 100 | (void)modulo; |
146 | 100 | (void)ds; |
147 | | |
148 | 100 | try { |
149 | 100 | switch ( GET_UINT8_FOR_SWITCH() ) { |
150 | 3 | case 0: |
151 | 3 | { |
152 | 3 | try { |
153 | 3 | const Botan::Modular_Reducer reducer(bn[1].Ref()); |
154 | 3 | res = reducer.reduce(bn[0].Ref()); |
155 | 3 | } catch ( ::Botan::Invalid_State& e ) { |
156 | | /* Modular reducer is expected to throw an exception when modulo is 0 */ |
157 | 1 | if ( bn[1].Ref() == 0 ) { |
158 | 1 | return false; |
159 | 1 | } |
160 | | |
161 | | /* Rethrow */ |
162 | 0 | throw e; |
163 | 1 | } |
164 | 3 | } |
165 | 2 | return true; |
166 | 2 | case 1: |
167 | 2 | res = ct_modulo(bn[0].Ref(), bn[1].Ref()); |
168 | 2 | return true; |
169 | 2 | case 2: |
170 | | /* % operator */ |
171 | 2 | res = bn[0].Ref() % bn[1].Ref(); |
172 | 2 | return true; |
173 | 71 | case 3: |
174 | | /* %= operator */ |
175 | 71 | { |
176 | 71 | res = bn[0].Ref(); |
177 | | |
178 | 71 | const ::Botan::word modulo = bn[1].Ref().word_at(0); |
179 | | |
180 | | /* Ensure no truncation occurred */ |
181 | 71 | if ( modulo != bn[1].Ref() ) { |
182 | 21 | return false; |
183 | 21 | } |
184 | | |
185 | 50 | res = bn[0].Ref() %= modulo; |
186 | 50 | } |
187 | 0 | return true; |
188 | 100 | } |
189 | 100 | } catch ( fuzzing::datasource::Datasource::OutOfData ) { |
190 | 17 | return false; |
191 | 17 | } catch ( ::Botan::Invalid_Argument& e ) { |
192 | | /* Botan is expected to throw an exception when modulo is <= 0 */ |
193 | 3 | if ( bn[1].Ref() <= 0 ) { |
194 | 3 | return false; |
195 | 3 | } |
196 | | |
197 | | /* Rethrow */ |
198 | 0 | throw e; |
199 | 3 | } |
200 | | |
201 | 5 | return false; |
202 | 100 | } |
203 | | |
204 | 71 | bool Exp::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
205 | 71 | (void)ds; |
206 | | |
207 | 71 | if ( modulo == std::nullopt ) { |
208 | 66 | return false; |
209 | 66 | } |
210 | | |
211 | 5 | res = ::Botan::power_mod(bn[0].Ref(), bn[1].Ref(), modulo->ConstRef()); |
212 | | |
213 | 5 | return true; |
214 | 71 | } |
215 | | |
216 | 1.61k | bool ExpMod::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
217 | 1.61k | (void)modulo; |
218 | 1.61k | (void)ds; |
219 | | |
220 | | /* Exponent and modulus must be positive, according to the documentation */ |
221 | 1.61k | if ( bn[1].Ref() < 0 || bn[2].Ref() <= 0 ) { |
222 | 5 | return false; |
223 | 5 | } |
224 | | |
225 | 1.60k | res = ::Botan::power_mod(bn[0].Ref(), bn[1].Ref(), bn[2].Ref()); |
226 | | |
227 | 1.60k | return true; |
228 | 1.61k | } |
229 | | |
230 | 87 | bool Sqr::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
231 | 87 | (void)ds; |
232 | | |
233 | 87 | res = ::Botan::square(bn[0].Ref()); |
234 | | |
235 | 87 | APPLY_MODULO; |
236 | | |
237 | 87 | return true; |
238 | 87 | } |
239 | | |
240 | 66 | bool GCD::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
241 | 66 | (void)modulo; |
242 | 66 | (void)ds; |
243 | | |
244 | 66 | res = ::Botan::gcd(bn[0].Ref(), bn[1].Ref()); |
245 | | |
246 | 66 | return true; |
247 | 66 | } |
248 | | |
249 | 56 | bool SqrMod::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
250 | 56 | (void)modulo; |
251 | 56 | (void)ds; |
252 | | |
253 | 56 | if ( bn[1].Ref().is_negative() ) { |
254 | 0 | return false; |
255 | 56 | } else { |
256 | 56 | try { |
257 | 56 | switch ( GET_UINT8_FOR_SWITCH() ) { |
258 | 5 | case 0: |
259 | 5 | { |
260 | 5 | try { |
261 | 5 | ::Botan::Modular_Reducer mod(bn[1].Ref()); |
262 | 5 | res = mod.square(bn[0].Ref()); |
263 | 5 | } catch ( ::Botan::Invalid_State& e ) { |
264 | | /* Modular reducer is expected to throw an exception when modulo is 0 */ |
265 | 2 | if ( bn[1].Ref() == 0 ) { |
266 | 2 | return false; |
267 | 2 | } |
268 | | |
269 | | /* Rethrow */ |
270 | 0 | throw e; |
271 | 2 | } |
272 | 5 | } |
273 | 3 | break; |
274 | 2 | case 1: |
275 | 2 | res = ::Botan::square(bn[0].Ref()) % bn[1].Ref(); |
276 | 2 | break; |
277 | 11 | default: |
278 | 11 | return false; |
279 | 56 | } |
280 | 56 | } catch ( fuzzing::datasource::Datasource::OutOfData ) { |
281 | 38 | return false; |
282 | 38 | } catch ( ::Botan::Invalid_Argument& e ) { |
283 | | /* Botan is expected to throw an exception when modulo is 0 */ |
284 | 1 | if ( bn[1].Ref() == 0 ) { |
285 | 1 | return false; |
286 | 1 | } |
287 | | |
288 | | /* Rethrow */ |
289 | 0 | throw e; |
290 | 1 | } |
291 | 56 | } |
292 | | |
293 | 4 | return true; |
294 | 56 | } |
295 | | |
296 | 489 | bool InvMod::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
297 | 489 | (void)modulo; |
298 | 489 | (void)ds; |
299 | | |
300 | 489 | const auto mod = modulo == std::nullopt ? bn[1].ConstRef() : modulo->ConstRef(); |
301 | | |
302 | 489 | try { |
303 | 489 | res = ::Botan::inverse_mod(bn[0].Ref(), mod); |
304 | 489 | } catch ( ::Botan::Invalid_Argument& e ) { |
305 | | /* inverse_mod() is expected to throw an exception when modulo is 0 */ |
306 | 45 | if ( mod == 0 ) { |
307 | 45 | return false; |
308 | 45 | } |
309 | | |
310 | | /* inverse_mod() is expected to throw an exception when either argument is negative */ |
311 | 0 | if ( bn[0].Ref() < 0 || mod < 0 ) { |
312 | 0 | return false; |
313 | 0 | } |
314 | | |
315 | | /* Rethrow */ |
316 | 0 | throw e; |
317 | 0 | } |
318 | | |
319 | 444 | return true; |
320 | 489 | } |
321 | | |
322 | 15 | bool Cmp::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
323 | 15 | (void)modulo; |
324 | 15 | (void)ds; |
325 | | |
326 | 15 | if ( bn[0].Ref() < bn[1].Ref() ) { |
327 | 5 | res = Bignum("-1"); |
328 | 10 | } else if ( bn[0].Ref() > bn[1].Ref() ) { |
329 | 4 | res = 1; |
330 | 6 | } else { |
331 | 6 | res = 0; |
332 | 6 | } |
333 | | |
334 | 15 | return true; |
335 | 15 | } |
336 | | |
337 | 398 | bool LCM::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
338 | 398 | (void)modulo; |
339 | 398 | (void)ds; |
340 | | |
341 | 398 | try { |
342 | 398 | res = ::Botan::lcm(bn[0].Ref(), bn[1].Ref()); |
343 | 398 | } catch ( ::Botan::Invalid_Argument& e ) { |
344 | | /* lcm() is expected to throw in these cases */ |
345 | 0 | if ( bn[0].Ref() == 0 || bn[1].Ref() == 0 ) { |
346 | 0 | return false; |
347 | 0 | } |
348 | | |
349 | | /* Rethrow */ |
350 | 0 | throw e; |
351 | 0 | } |
352 | | |
353 | | |
354 | 398 | return true; |
355 | 398 | } |
356 | | |
357 | 7 | bool Abs::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
358 | 7 | (void)modulo; |
359 | 7 | (void)ds; |
360 | | |
361 | 7 | res = ::Botan::abs(bn[0].Ref()); |
362 | | |
363 | 7 | return true; |
364 | 7 | } |
365 | | |
366 | 27 | bool Jacobi::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
367 | 27 | (void)modulo; |
368 | 27 | (void)ds; |
369 | | |
370 | | |
371 | 27 | int resInt; |
372 | | |
373 | 27 | try { |
374 | 27 | resInt = ::Botan::jacobi(bn[0].Ref(), bn[1].Ref()); |
375 | 27 | } catch ( ::Botan::Invalid_Argument& e ) { |
376 | | /* jacobi() is expected to throw in these cases */ |
377 | 4 | if ( (bn[1].Ref() % 2) == 0 || bn[1].Ref() <= 1 ) { |
378 | 4 | return false; |
379 | 4 | } |
380 | | |
381 | | /* Rethrow */ |
382 | 0 | throw e; |
383 | 4 | } |
384 | | |
385 | 23 | if ( resInt == -1 ) { |
386 | 9 | res = Bignum("-1"); |
387 | 14 | } else { |
388 | 14 | res = resInt; |
389 | 14 | } |
390 | | |
391 | 23 | return true; |
392 | 27 | } |
393 | | |
394 | 69 | bool Neg::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
395 | 69 | (void)modulo; |
396 | 69 | (void)ds; |
397 | | |
398 | 69 | res = -bn[0].Ref(); |
399 | | |
400 | 69 | return true; |
401 | 69 | } |
402 | | |
403 | 46 | bool IsPrime::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
404 | 46 | (void)modulo; |
405 | 46 | (void)ds; |
406 | | |
407 | 46 | if ( bn[0].Ref().is_negative() ) { |
408 | 0 | return false; |
409 | 0 | } |
410 | | |
411 | | /* Avoid time-outs */ |
412 | 46 | if ( bn[0].Ref().bytes() > 300 ) { |
413 | 1 | return false; |
414 | 1 | } |
415 | | |
416 | 45 | Botan::Modular_Reducer mod_n(bn[0].Ref()); |
417 | 45 | if ( Botan::is_bailie_psw_probable_prime(bn[0].Ref(), mod_n) ) { |
418 | 14 | res = 1; |
419 | 31 | } else { |
420 | 31 | res = 0; |
421 | 31 | } |
422 | | |
423 | 45 | return true; |
424 | 46 | } |
425 | | |
426 | 13 | bool RShift::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
427 | 13 | (void)ds; |
428 | | |
429 | 13 | const auto count = detail::To_size_t(bn[1].Ref()); |
430 | | |
431 | 13 | if ( count == std::nullopt ) { |
432 | 1 | return false; |
433 | 1 | } |
434 | | |
435 | 12 | Bignum toShift = bn[0]; |
436 | 12 | if ( modulo && bn[0].Ref() % 2 ) { |
437 | 1 | toShift = toShift.Ref() + modulo->ConstRef(); |
438 | 1 | } |
439 | | |
440 | 12 | res = toShift.Ref() >> *count; |
441 | | |
442 | 12 | APPLY_MODULO; |
443 | | |
444 | 12 | return true; |
445 | 13 | } |
446 | | |
447 | 18 | bool LShift1::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
448 | 18 | (void)ds; |
449 | | |
450 | 18 | res = bn[0].Ref() << 1; |
451 | | |
452 | 18 | APPLY_MODULO; |
453 | | |
454 | 18 | return true; |
455 | 18 | } |
456 | | |
457 | 1 | bool IsNeg::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
458 | 1 | (void)modulo; |
459 | 1 | (void)ds; |
460 | | |
461 | 1 | res = bn[0].Ref() < 0 ? 1 : 0; |
462 | | |
463 | 1 | return true; |
464 | 1 | } |
465 | | |
466 | 13 | bool IsEq::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
467 | 13 | (void)ds; |
468 | | |
469 | 13 | auto A = modulo == std::nullopt ? bn[0] : bn[0].Ref() % modulo->ConstRef(); |
470 | 13 | auto B = modulo == std::nullopt ? bn[1] : bn[1].Ref() % modulo->ConstRef(); |
471 | | |
472 | 13 | res = A.Ref() == B.Ref() ? 1 : 0; |
473 | | |
474 | 13 | return true; |
475 | 13 | } |
476 | | |
477 | 1 | bool IsGt::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
478 | 1 | (void)modulo; |
479 | 1 | (void)ds; |
480 | | |
481 | 1 | res = bn[0].Ref() > bn[1].Ref() ? 1 : 0; |
482 | | |
483 | 1 | return true; |
484 | 1 | } |
485 | | |
486 | 2 | bool IsGte::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
487 | 2 | (void)modulo; |
488 | 2 | (void)ds; |
489 | | |
490 | 2 | res = bn[0].Ref() >= bn[1].Ref() ? 1 : 0; |
491 | | |
492 | 2 | return true; |
493 | 2 | } |
494 | | |
495 | 1 | bool IsLt::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
496 | 1 | (void)modulo; |
497 | 1 | (void)ds; |
498 | | |
499 | 1 | res = bn[0].Ref() < bn[1].Ref() ? 1 : 0; |
500 | | |
501 | 1 | return true; |
502 | 1 | } |
503 | | |
504 | 2 | bool IsLte::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
505 | 2 | (void)modulo; |
506 | 2 | (void)ds; |
507 | | |
508 | 2 | res = bn[0].Ref() <= bn[1].Ref() ? 1 : 0; |
509 | | |
510 | 2 | return true; |
511 | 2 | } |
512 | | |
513 | 7 | bool IsEven::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
514 | 7 | (void)modulo; |
515 | 7 | (void)ds; |
516 | | |
517 | 7 | res = !(bn[0].Ref() % 2) ? 1 : 0; |
518 | | |
519 | 7 | return true; |
520 | 7 | } |
521 | | |
522 | 6 | bool IsOdd::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
523 | 6 | (void)modulo; |
524 | 6 | (void)ds; |
525 | | |
526 | 6 | res = (bn[0].Ref() % 2) ? 1 : 0; |
527 | | |
528 | 6 | return true; |
529 | 6 | } |
530 | | |
531 | 7 | bool IsZero::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
532 | 7 | (void)ds; |
533 | | |
534 | 7 | auto A = modulo == std::nullopt ? bn[0] : bn[0].Ref() % modulo->ConstRef(); |
535 | | |
536 | 7 | res = A.Ref() == 0 ? 1 : 0; |
537 | | |
538 | 7 | return true; |
539 | 7 | } |
540 | | |
541 | 1 | bool IsNotZero::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
542 | 1 | (void)modulo; |
543 | 1 | (void)ds; |
544 | | |
545 | 1 | res = bn[0].Ref() == 0 ? 0 : 1; |
546 | | |
547 | 1 | return true; |
548 | 1 | } |
549 | | |
550 | 3 | bool IsOne::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
551 | 3 | (void)ds; |
552 | | |
553 | 3 | auto A = modulo == std::nullopt ? bn[0] : bn[0].Ref() % modulo->ConstRef(); |
554 | | |
555 | 3 | res = A.Ref() == 1 ? 1 : 0; |
556 | | |
557 | 3 | return true; |
558 | 3 | } |
559 | | |
560 | 67 | bool MulMod::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
561 | 67 | (void)modulo; |
562 | 67 | (void)ds; |
563 | | |
564 | 67 | try { |
565 | 67 | switch ( GET_UINT8_FOR_SWITCH() ) { |
566 | 7 | case 0: |
567 | 7 | { |
568 | 7 | try { |
569 | 7 | ::Botan::Modular_Reducer mod(bn[2].Ref()); |
570 | 7 | res = mod.multiply(bn[0].Ref(), bn[1].Ref()); |
571 | 7 | } catch ( ::Botan::Invalid_State& e ) { |
572 | | /* Modular reducer is expected to throw an exception when modulo is 0 */ |
573 | 2 | if ( bn[2].Ref() == 0 ) { |
574 | 2 | return false; |
575 | 2 | } |
576 | | |
577 | | /* Rethrow */ |
578 | 0 | throw e; |
579 | 2 | } |
580 | 7 | } |
581 | 5 | break; |
582 | 2 | case 1: |
583 | 2 | res = (bn[0].Ref() * bn[1].Ref()) % bn[2].Ref(); |
584 | 2 | break; |
585 | 9 | default: |
586 | 9 | return false; |
587 | 67 | } |
588 | 67 | } catch ( fuzzing::datasource::Datasource::OutOfData ) { |
589 | 49 | return false; |
590 | 49 | } catch ( ::Botan::Invalid_Argument& e ) { |
591 | | /* Botan is expected to throw an exception when modulo is <= 0 */ |
592 | 1 | if ( bn[2].Ref() <= 0 ) { |
593 | 1 | return false; |
594 | 1 | } |
595 | | |
596 | | /* Rethrow */ |
597 | 0 | throw e; |
598 | 1 | } |
599 | | |
600 | 6 | return true; |
601 | 67 | } |
602 | | |
603 | 21 | bool Bit::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
604 | 21 | (void)modulo; |
605 | 21 | (void)ds; |
606 | | |
607 | 21 | const auto pos = detail::To_size_t(bn[1].Ref()); |
608 | | |
609 | 21 | if ( pos == std::nullopt ) { |
610 | 1 | return false; |
611 | 1 | } |
612 | | |
613 | 20 | res = bn[0].Ref().get_bit(*pos) ? 1 : 0; |
614 | | |
615 | 20 | return true; |
616 | 21 | } |
617 | | |
618 | 4 | bool CmpAbs::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
619 | 4 | (void)modulo; |
620 | 4 | std::vector<Bignum> bnAbs = {bn[0].Ref().abs(), bn[1].Ref().abs()}; |
621 | 4 | auto cmp = std::make_unique<Cmp>(); |
622 | | |
623 | 4 | return cmp->Run(ds, res, bnAbs, modulo); |
624 | 4 | } |
625 | | |
626 | 7 | bool SetBit::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
627 | 7 | (void)modulo; |
628 | 7 | (void)ds; |
629 | | |
630 | 7 | res = bn[0].Ref(); |
631 | | |
632 | 7 | const auto pos = detail::To_size_t(bn[1].Ref()); |
633 | | |
634 | 7 | if ( pos == std::nullopt ) { |
635 | 0 | return false; |
636 | 0 | } |
637 | | |
638 | 7 | res.Ref().set_bit(*pos); |
639 | | |
640 | 7 | return true; |
641 | 7 | } |
642 | | |
643 | 43 | bool ClearBit::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
644 | 43 | (void)modulo; |
645 | 43 | (void)ds; |
646 | | |
647 | 43 | res = bn[0].Ref(); |
648 | | |
649 | 43 | const auto pos = detail::To_size_t(bn[1].Ref()); |
650 | | |
651 | 43 | if ( pos == std::nullopt ) { |
652 | 1 | return false; |
653 | 1 | } |
654 | | |
655 | 42 | res.Ref().clear_bit(*pos); |
656 | | |
657 | 42 | return true; |
658 | 43 | } |
659 | | |
660 | 4 | bool MulAdd::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
661 | 4 | (void)modulo; |
662 | 4 | (void)ds; |
663 | | |
664 | 4 | res = (bn[0].Ref()*bn[1].Ref()) + bn[2].Ref(); |
665 | | |
666 | 4 | return true; |
667 | 4 | } |
668 | | |
669 | 4 | bool MulDiv::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
670 | 4 | (void)modulo; |
671 | 4 | (void)ds; |
672 | | |
673 | 4 | if ( bn[2].Ref() == 0 ) { |
674 | 1 | return false; |
675 | 1 | } |
676 | | |
677 | 3 | res = (bn[0].Ref()*bn[1].Ref()+1) / bn[2].Ref(); |
678 | | |
679 | 3 | return true; |
680 | 4 | } |
681 | | |
682 | 21 | bool MulDivCeil::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
683 | 21 | (void)modulo; |
684 | 21 | (void)ds; |
685 | | |
686 | 21 | if ( bn[2].Ref() <= 0 ) { |
687 | 1 | return false; |
688 | 1 | } |
689 | | |
690 | 20 | const auto mulRes = bn[0].Ref() * bn[1].Ref(); |
691 | 20 | const auto modRes = mulRes % bn[2].Ref(); |
692 | 20 | res = mulRes / bn[2].Ref() + (modRes != 0 ? 1 : 0); |
693 | | |
694 | 20 | return true; |
695 | 21 | } |
696 | | |
697 | 20 | bool Exp2::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
698 | 20 | (void)modulo; |
699 | 20 | (void)ds; |
700 | | |
701 | 20 | if ( bn[0].Ref() < 1 ) { |
702 | 2 | return false; |
703 | 2 | } |
704 | | |
705 | 18 | const size_t exponent = bn[0].Ref().word_at(0) - 1; |
706 | | |
707 | 18 | res = Bignum(2).Ref() << exponent; |
708 | | |
709 | 18 | return true; |
710 | 20 | } |
711 | | |
712 | 7 | bool NumLSZeroBits::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
713 | 7 | (void)modulo; |
714 | 7 | (void)ds; |
715 | | |
716 | 7 | res = ::Botan::low_zero_bits(bn[0].Ref()); |
717 | | |
718 | 7 | return true; |
719 | 7 | } |
720 | | |
721 | 28 | bool Sqrt::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
722 | 28 | (void)ds; |
723 | | |
724 | 28 | try { |
725 | 28 | const auto res2 = ::Botan::is_perfect_square(bn[0].Ref()); |
726 | 28 | if ( res2 == 0 ) { |
727 | 25 | return false; |
728 | 25 | } |
729 | | |
730 | 3 | res = res2; |
731 | 3 | } catch ( ::Botan::Invalid_Argument& e ) { |
732 | | /* is_perfect_square() is expected to throw in this case */ |
733 | 1 | if ( bn[0].Ref() < 1 ) { |
734 | 1 | return false; |
735 | 1 | } |
736 | | |
737 | | /* Rethrow */ |
738 | 0 | throw e; |
739 | 1 | } |
740 | | |
741 | 2 | APPLY_MODULO; |
742 | | |
743 | 2 | return true; |
744 | 28 | } |
745 | | |
746 | 61 | bool AddMod::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
747 | 61 | (void)modulo; |
748 | 61 | (void)ds; |
749 | | |
750 | 61 | try { |
751 | 61 | switch ( GET_UINT8_FOR_SWITCH() ) { |
752 | 3 | case 0: |
753 | 3 | res = (bn[0].Ref() + bn[1].Ref()) % bn[2].Ref(); |
754 | 3 | break; |
755 | 11 | case 1: |
756 | 11 | { |
757 | 11 | if ( bn[0].Ref() >= bn[2].Ref() ) { |
758 | 3 | return false; |
759 | 3 | } |
760 | 8 | if ( bn[1].Ref() >= bn[2].Ref() ) { |
761 | 1 | return false; |
762 | 1 | } |
763 | | |
764 | 7 | ::Botan::secure_vector<::Botan::word> ws; |
765 | 7 | try { |
766 | 7 | res = bn[0].Ref().mod_add(bn[1].Ref(), bn[2].Ref(), ws); |
767 | 7 | } catch ( ::Botan::Invalid_Argument& e ) { |
768 | | /* mod_add is expected to throw an exception when any argument is negative */ |
769 | 0 | if ( bn[0].Ref() < 0 || bn[1].Ref() < 0 || bn[2].Ref() < 0) { |
770 | 0 | return false; |
771 | 0 | } |
772 | | |
773 | | /* Rethrow */ |
774 | 0 | throw e; |
775 | 0 | } |
776 | 7 | } |
777 | 7 | break; |
778 | 9 | default: |
779 | 9 | return false; |
780 | 61 | } |
781 | 61 | } catch ( fuzzing::datasource::Datasource::OutOfData ) { |
782 | 38 | return false; |
783 | 38 | } catch ( ::Botan::Invalid_Argument& e ) { |
784 | | /* Botan is expected to throw an exception when modulo is <= 0 */ |
785 | 1 | if ( bn[2].Ref() <= 0 ) { |
786 | 1 | return false; |
787 | 1 | } |
788 | | |
789 | | /* Rethrow */ |
790 | 0 | throw e; |
791 | 1 | } |
792 | | |
793 | 9 | return true; |
794 | 61 | } |
795 | | |
796 | 43 | bool SubMod::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
797 | 43 | (void)modulo; |
798 | 43 | (void)ds; |
799 | | |
800 | 43 | try { |
801 | 43 | switch ( GET_UINT8_FOR_SWITCH() ) { |
802 | 6 | case 0: |
803 | 6 | res = (bn[0].Ref() - bn[1].Ref()) % bn[2].Ref(); |
804 | 6 | break; |
805 | 11 | case 1: |
806 | 11 | { |
807 | 11 | if ( bn[0].Ref() >= bn[2].Ref() ) { |
808 | 1 | return false; |
809 | 1 | } |
810 | 10 | if ( bn[1].Ref() >= bn[2].Ref() ) { |
811 | 1 | return false; |
812 | 1 | } |
813 | | |
814 | 9 | ::Botan::secure_vector<::Botan::word> ws; |
815 | 9 | try { |
816 | 9 | res = bn[0].Ref().mod_sub(bn[1].Ref(), bn[2].Ref(), ws); |
817 | 9 | } catch ( ::Botan::Invalid_Argument& e ) { |
818 | | /* mod_sub is expected to throw an exception when any argument is negative */ |
819 | 0 | if ( bn[0].Ref() < 0 || bn[1].Ref() < 0 || bn[2].Ref() < 0) { |
820 | 0 | return false; |
821 | 0 | } |
822 | | |
823 | | /* Rethrow */ |
824 | 0 | throw e; |
825 | 0 | } |
826 | 9 | } |
827 | 9 | break; |
828 | 12 | default: |
829 | 12 | return false; |
830 | 43 | } |
831 | 43 | } catch ( fuzzing::datasource::Datasource::OutOfData ) { |
832 | 14 | return false; |
833 | 14 | } catch ( ::Botan::Invalid_Argument& e ) { |
834 | | /* Botan is expected to throw an exception when modulo is <= 0 */ |
835 | 2 | if ( bn[2].Ref() <= 0 ) { |
836 | 2 | return false; |
837 | 2 | } |
838 | | |
839 | | /* Rethrow */ |
840 | 0 | throw e; |
841 | 2 | } |
842 | | |
843 | 13 | return true; |
844 | 43 | } |
845 | | |
846 | 7 | bool NumBits::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
847 | 7 | (void)ds; |
848 | | |
849 | 7 | if ( modulo ) { |
850 | 6 | res = (bn[0].Ref() % modulo->ConstRef()).bits(); |
851 | 6 | } else { |
852 | 1 | res = bn[0].Ref().bits(); |
853 | 1 | } |
854 | | |
855 | 7 | return true; |
856 | 7 | } |
857 | | |
858 | 10 | bool Set::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
859 | 10 | (void)modulo; |
860 | 10 | (void)ds; |
861 | | |
862 | 10 | res = bn[0].Ref(); |
863 | | |
864 | 10 | APPLY_MODULO; |
865 | | |
866 | 10 | return true; |
867 | 10 | } |
868 | | |
869 | 74 | bool CondSet::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
870 | 74 | (void)modulo; |
871 | 74 | (void)ds; |
872 | | |
873 | 74 | res.Ref().ct_cond_assign(bn[1].Ref() != 0, bn[0].Ref()); |
874 | | |
875 | 74 | return true; |
876 | 74 | } |
877 | | |
878 | 155 | bool Ressol::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
879 | 155 | (void)ds; |
880 | | |
881 | 155 | try { |
882 | 155 | auto mod = modulo == std::nullopt ? bn[1] : *modulo; |
883 | | |
884 | 155 | const auto r = ::Botan::sqrt_modulo_prime(bn[0].Ref(), mod.Ref()); |
885 | | |
886 | 155 | if ( r < 1 ) { |
887 | 55 | if ( modulo != std::nullopt ) { |
888 | 55 | res = 0; |
889 | 55 | return true; |
890 | 55 | } else { |
891 | 0 | return false; |
892 | 0 | } |
893 | 55 | } |
894 | | |
895 | 100 | if ( modulo != std::nullopt ) { |
896 | 98 | res = ::Botan::square(r) % mod.Ref(); |
897 | 98 | } |
898 | | |
899 | 100 | return true; |
900 | 155 | } catch ( ::Botan::Invalid_Argument& e ) { |
901 | | /* Expected to throw if called with non-prime argument */ |
902 | | |
903 | 2 | return false; |
904 | 2 | } |
905 | 155 | } |
906 | | |
907 | 20 | bool Not::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
908 | 20 | (void)ds; |
909 | | |
910 | 20 | Bignum max; |
911 | | |
912 | 20 | if ( modulo ) { |
913 | 16 | max = *modulo; |
914 | 16 | } else { |
915 | 4 | const size_t numBits = bn[0].Ref().bits(); |
916 | | |
917 | 4 | if ( numBits == 0 ) { |
918 | 1 | return false; |
919 | 1 | } |
920 | | |
921 | 3 | max = (::Botan::BigInt(1) << numBits) - 1; |
922 | 3 | } |
923 | | |
924 | 19 | res = max.Ref() - bn[0].Ref(); |
925 | | |
926 | 19 | APPLY_MODULO; |
927 | | |
928 | 19 | return true; |
929 | 20 | } |
930 | | |
931 | 294 | bool Prime::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
932 | 294 | (void)ds; |
933 | 294 | (void)bn; |
934 | 294 | (void)modulo; |
935 | | |
936 | 294 | ::Botan::System_RNG rng; |
937 | 294 | res = Botan::random_prime(rng, (rand() % 512) + 2); |
938 | | |
939 | 294 | return true; |
940 | 294 | } |
941 | | |
942 | 5 | bool RandRange::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
943 | 5 | (void)ds; |
944 | 5 | (void)modulo; |
945 | | |
946 | 5 | try { |
947 | 5 | ::Botan::System_RNG rng; |
948 | 5 | res = ::Botan::BigInt::random_integer(rng, bn[0].Ref(), bn[1].Ref()); |
949 | 5 | } catch ( ::Botan::Invalid_Argument ) { |
950 | 1 | return false; |
951 | 1 | } |
952 | | |
953 | 4 | return true; |
954 | 5 | } |
955 | | |
956 | 19 | bool IsSquare::Run(Datasource& ds, Bignum& res, std::vector<Bignum>& bn, const std::optional<Bignum>& modulo) const { |
957 | 19 | (void)ds; |
958 | | |
959 | 19 | if ( modulo != std::nullopt ) { |
960 | 0 | return false; |
961 | 0 | } |
962 | | |
963 | 19 | try { |
964 | 19 | res = ::Botan::is_perfect_square(bn[0].Ref()) == 0 ? 0 : 1; |
965 | 19 | } catch ( ::Botan::Invalid_Argument& e ) { |
966 | | /* is_perfect_square() is expected to throw in this case */ |
967 | 1 | if ( bn[0].Ref() < 1 ) { |
968 | 1 | return false; |
969 | 1 | } |
970 | | |
971 | | /* Rethrow */ |
972 | 0 | throw e; |
973 | 1 | } |
974 | | |
975 | 18 | return true; |
976 | 19 | } |
977 | | |
978 | | } /* namespace Botan_bignum */ |
979 | | } /* namespace module */ |
980 | | } /* namespace cryptofuzz */ |