2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.openssl.org/source/license.html
8 * or in the file LICENSE in the source distribution.
12 * Confirm that if (d, r) = a / b, then b * d + r == a, and that sign(d) ==
13 * sign(a), and 0 <= r <= b
17 #include <openssl/bn.h>
18 #include <openssl/err.h>
22 #define MAX_LEN (256 * 1000)
31 int FuzzerInitialize(int *argc
, char ***argv
)
40 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS
, NULL
);
46 int FuzzerTestOneInput(const uint8_t *buf
, size_t len
)
49 size_t l1
= 0, l2
= 0;
50 /* s1 and s2 will be the signs for b1 and b2. */
53 /* limit the size of the input to avoid timeout */
57 /* We are going to split the buffer in two, sizes l1 and l2, giving b1 and
62 /* Use first byte to divide the remaining buffer into 3Fths. I admit
63 * this disallows some number sizes. If it matters, better ideas are
66 l1
= ((buf
[0] & 0x3f) * len
) / 0x3f;
72 OPENSSL_assert(BN_bin2bn(buf
, l1
, b1
) == b1
);
73 BN_set_negative(b1
, s1
);
74 OPENSSL_assert(BN_bin2bn(buf
+ l1
, l2
, b2
) == b2
);
75 BN_set_negative(b2
, s2
);
77 /* divide by 0 is an error */
83 OPENSSL_assert(BN_div(b3
, b4
, b1
, b2
, ctx
));
85 success
= BN_is_zero(b3
) && BN_is_zero(b4
);
86 else if (BN_is_negative(b1
))
87 success
= (BN_is_negative(b3
) != BN_is_negative(b2
) || BN_is_zero(b3
))
88 && (BN_is_negative(b4
) || BN_is_zero(b4
));
90 success
= (BN_is_negative(b3
) == BN_is_negative(b2
) || BN_is_zero(b3
))
91 && (!BN_is_negative(b4
) || BN_is_zero(b4
));
92 OPENSSL_assert(BN_mul(b5
, b3
, b2
, ctx
));
93 OPENSSL_assert(BN_add(b5
, b5
, b4
));
95 success
= success
&& BN_cmp(b5
, b1
) == 0;
97 BN_print_fp(stdout
, b1
);
99 BN_print_fp(stdout
, b2
);
101 BN_print_fp(stdout
, b3
);
103 BN_print_fp(stdout
, b4
);
105 BN_print_fp(stdout
, b5
);
107 printf("%d %d %d %d %d %d %d\n", BN_is_negative(b1
),
109 BN_is_negative(b3
), BN_is_negative(b4
), BN_is_zero(b4
),
110 BN_is_negative(b3
) != BN_is_negative(b2
)
111 && (BN_is_negative(b4
) || BN_is_zero(b4
)),
117 OPENSSL_assert(success
);
123 void FuzzerCleanup(void)