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