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