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