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