1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 /*
6  * This target fuzzes NSS mpi against openssl bignum.
7  * It therefore requires openssl to be installed.
8  */
9 
10 #include "mpi_helper.h"
11 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)12 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
13   // We require at least size 3 to get two integers from Data.
14   if (size < 3) {
15     return 0;
16   }
17   INIT_FOUR_NUMBERS
18 
19   // Compare with OpenSSL addition
20   assert(mp_add(&a, &b, &c) == MP_OKAY);
21   (void)BN_add(C, A, B);
22   check_equal(C, &c, max_size);
23 
24   // Check a + b == a - -b
25   mp_neg(&b, &b);
26   assert(mp_sub(&a, &b, &r) == MP_OKAY);
27   bool eq = mp_cmp(&r, &c) == 0;
28   if (!eq) {
29     char rC[max_size], cC[max_size], aC[max_size], bC[max_size];
30     mp_tohex(&r, rC);
31     mp_tohex(&c, cC);
32     mp_tohex(&a, aC);
33     mp_tohex(&b, bC);
34     std::cout << "a = " << std::hex << aC << std::endl;
35     std::cout << "-b = " << std::hex << bC << std::endl;
36     std::cout << "a + b = " << std::hex << cC << std::endl;
37     std::cout << "a - -b = " << std::hex << rC << std::endl;
38   }
39   assert(eq);
40 
41   CLEANUP_AND_RETURN
42 }
43