1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License");
5*e0c4386eSCy Schubert * you may not use this file except in compliance with the License.
6*e0c4386eSCy Schubert * You may obtain a copy of the License at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert * or in the file LICENSE in the source distribution.
9*e0c4386eSCy Schubert */
10*e0c4386eSCy Schubert
11*e0c4386eSCy Schubert /*
12*e0c4386eSCy Schubert * Confirm that if (d, r) = a / b, then b * d + r == a, and that sign(d) ==
13*e0c4386eSCy Schubert * sign(a), and 0 <= r <= b
14*e0c4386eSCy Schubert */
15*e0c4386eSCy Schubert
16*e0c4386eSCy Schubert #include <stdio.h>
17*e0c4386eSCy Schubert #include <openssl/bn.h>
18*e0c4386eSCy Schubert #include <openssl/err.h>
19*e0c4386eSCy Schubert #include "fuzzer.h"
20*e0c4386eSCy Schubert
21*e0c4386eSCy Schubert /* 256 kB */
22*e0c4386eSCy Schubert #define MAX_LEN (256 * 1000)
23*e0c4386eSCy Schubert
24*e0c4386eSCy Schubert static BN_CTX *ctx;
25*e0c4386eSCy Schubert static BIGNUM *b1;
26*e0c4386eSCy Schubert static BIGNUM *b2;
27*e0c4386eSCy Schubert static BIGNUM *b3;
28*e0c4386eSCy Schubert static BIGNUM *b4;
29*e0c4386eSCy Schubert static BIGNUM *b5;
30*e0c4386eSCy Schubert
FuzzerInitialize(int * argc,char *** argv)31*e0c4386eSCy Schubert int FuzzerInitialize(int *argc, char ***argv)
32*e0c4386eSCy Schubert {
33*e0c4386eSCy Schubert b1 = BN_new();
34*e0c4386eSCy Schubert b2 = BN_new();
35*e0c4386eSCy Schubert b3 = BN_new();
36*e0c4386eSCy Schubert b4 = BN_new();
37*e0c4386eSCy Schubert b5 = BN_new();
38*e0c4386eSCy Schubert ctx = BN_CTX_new();
39*e0c4386eSCy Schubert
40*e0c4386eSCy Schubert OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
41*e0c4386eSCy Schubert ERR_clear_error();
42*e0c4386eSCy Schubert
43*e0c4386eSCy Schubert return 1;
44*e0c4386eSCy Schubert }
45*e0c4386eSCy Schubert
FuzzerTestOneInput(const uint8_t * buf,size_t len)46*e0c4386eSCy Schubert int FuzzerTestOneInput(const uint8_t *buf, size_t len)
47*e0c4386eSCy Schubert {
48*e0c4386eSCy Schubert int success = 0;
49*e0c4386eSCy Schubert size_t l1 = 0, l2 = 0;
50*e0c4386eSCy Schubert /* s1 and s2 will be the signs for b1 and b2. */
51*e0c4386eSCy Schubert int s1 = 0, s2 = 0;
52*e0c4386eSCy Schubert
53*e0c4386eSCy Schubert /* limit the size of the input to avoid timeout */
54*e0c4386eSCy Schubert if (len > MAX_LEN)
55*e0c4386eSCy Schubert len = MAX_LEN;
56*e0c4386eSCy Schubert
57*e0c4386eSCy Schubert /* We are going to split the buffer in two, sizes l1 and l2, giving b1 and
58*e0c4386eSCy Schubert * b2.
59*e0c4386eSCy Schubert */
60*e0c4386eSCy Schubert if (len > 0) {
61*e0c4386eSCy Schubert --len;
62*e0c4386eSCy Schubert /* Use first byte to divide the remaining buffer into 3Fths. I admit
63*e0c4386eSCy Schubert * this disallows some number sizes. If it matters, better ideas are
64*e0c4386eSCy Schubert * welcome (Ben).
65*e0c4386eSCy Schubert */
66*e0c4386eSCy Schubert l1 = ((buf[0] & 0x3f) * len) / 0x3f;
67*e0c4386eSCy Schubert s1 = buf[0] & 0x40;
68*e0c4386eSCy Schubert s2 = buf[0] & 0x80;
69*e0c4386eSCy Schubert ++buf;
70*e0c4386eSCy Schubert l2 = len - l1;
71*e0c4386eSCy Schubert }
72*e0c4386eSCy Schubert OPENSSL_assert(BN_bin2bn(buf, l1, b1) == b1);
73*e0c4386eSCy Schubert BN_set_negative(b1, s1);
74*e0c4386eSCy Schubert OPENSSL_assert(BN_bin2bn(buf + l1, l2, b2) == b2);
75*e0c4386eSCy Schubert BN_set_negative(b2, s2);
76*e0c4386eSCy Schubert
77*e0c4386eSCy Schubert /* divide by 0 is an error */
78*e0c4386eSCy Schubert if (BN_is_zero(b2)) {
79*e0c4386eSCy Schubert success = 1;
80*e0c4386eSCy Schubert goto done;
81*e0c4386eSCy Schubert }
82*e0c4386eSCy Schubert
83*e0c4386eSCy Schubert OPENSSL_assert(BN_div(b3, b4, b1, b2, ctx));
84*e0c4386eSCy Schubert if (BN_is_zero(b1))
85*e0c4386eSCy Schubert success = BN_is_zero(b3) && BN_is_zero(b4);
86*e0c4386eSCy Schubert else if (BN_is_negative(b1))
87*e0c4386eSCy Schubert success = (BN_is_negative(b3) != BN_is_negative(b2) || BN_is_zero(b3))
88*e0c4386eSCy Schubert && (BN_is_negative(b4) || BN_is_zero(b4));
89*e0c4386eSCy Schubert else
90*e0c4386eSCy Schubert success = (BN_is_negative(b3) == BN_is_negative(b2) || BN_is_zero(b3))
91*e0c4386eSCy Schubert && (!BN_is_negative(b4) || BN_is_zero(b4));
92*e0c4386eSCy Schubert OPENSSL_assert(BN_mul(b5, b3, b2, ctx));
93*e0c4386eSCy Schubert OPENSSL_assert(BN_add(b5, b5, b4));
94*e0c4386eSCy Schubert
95*e0c4386eSCy Schubert success = success && BN_cmp(b5, b1) == 0;
96*e0c4386eSCy Schubert if (!success) {
97*e0c4386eSCy Schubert BN_print_fp(stdout, b1);
98*e0c4386eSCy Schubert putchar('\n');
99*e0c4386eSCy Schubert BN_print_fp(stdout, b2);
100*e0c4386eSCy Schubert putchar('\n');
101*e0c4386eSCy Schubert BN_print_fp(stdout, b3);
102*e0c4386eSCy Schubert putchar('\n');
103*e0c4386eSCy Schubert BN_print_fp(stdout, b4);
104*e0c4386eSCy Schubert putchar('\n');
105*e0c4386eSCy Schubert BN_print_fp(stdout, b5);
106*e0c4386eSCy Schubert putchar('\n');
107*e0c4386eSCy Schubert printf("%d %d %d %d %d %d %d\n", BN_is_negative(b1),
108*e0c4386eSCy Schubert BN_is_negative(b2),
109*e0c4386eSCy Schubert BN_is_negative(b3), BN_is_negative(b4), BN_is_zero(b4),
110*e0c4386eSCy Schubert BN_is_negative(b3) != BN_is_negative(b2)
111*e0c4386eSCy Schubert && (BN_is_negative(b4) || BN_is_zero(b4)),
112*e0c4386eSCy Schubert BN_cmp(b5, b1));
113*e0c4386eSCy Schubert puts("----\n");
114*e0c4386eSCy Schubert }
115*e0c4386eSCy Schubert
116*e0c4386eSCy Schubert done:
117*e0c4386eSCy Schubert OPENSSL_assert(success);
118*e0c4386eSCy Schubert ERR_clear_error();
119*e0c4386eSCy Schubert
120*e0c4386eSCy Schubert return 0;
121*e0c4386eSCy Schubert }
122*e0c4386eSCy Schubert
FuzzerCleanup(void)123*e0c4386eSCy Schubert void FuzzerCleanup(void)
124*e0c4386eSCy Schubert {
125*e0c4386eSCy Schubert BN_free(b1);
126*e0c4386eSCy Schubert BN_free(b2);
127*e0c4386eSCy Schubert BN_free(b3);
128*e0c4386eSCy Schubert BN_free(b4);
129*e0c4386eSCy Schubert BN_free(b5);
130*e0c4386eSCy Schubert BN_CTX_free(ctx);
131*e0c4386eSCy Schubert }
132