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