]> git.ipfire.org Git - thirdparty/openssl.git/blame - fuzz/bndiv.c
CMS fuzzer: also use id2
[thirdparty/openssl.git] / fuzz / bndiv.c
CommitLineData
c38bb727
BL
1/*
2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL licenses, (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.
9 */
10
11/*
12 * Confirm that if (d, r) = a / b, then b * d + r == a, and that sign(d) ==
13 * sign(a), and 0 <= r <= b
14 */
15
16#include <stdio.h>
17#include <openssl/bn.h>
18#include "fuzzer.h"
19
8087bcb3
KR
20static BN_CTX *ctx;
21static BIGNUM *b1;
22static BIGNUM *b2;
23static BIGNUM *b3;
24static BIGNUM *b4;
25static BIGNUM *b5;
26
f3e911d5
KR
27int FuzzerInitialize(int *argc, char ***argv)
28{
8087bcb3
KR
29 b1 = BN_new();
30 b2 = BN_new();
31 b3 = BN_new();
32 b4 = BN_new();
33 b5 = BN_new();
34 ctx = BN_CTX_new();
35
90d28f05
BL
36 return 1;
37}
38
f3e911d5
KR
39int FuzzerTestOneInput(const uint8_t *buf, size_t len)
40{
90d28f05
BL
41 int success = 0;
42 size_t l1 = 0, l2 = 0;
43 /* s1 and s2 will be the signs for b1 and b2. */
44 int s1 = 0, s2 = 0;
c38bb727 45
90d28f05
BL
46 /* We are going to split the buffer in two, sizes l1 and l2, giving b1 and
47 * b2.
48 */
c38bb727
BL
49 if (len > 0) {
50 --len;
90d28f05
BL
51 /* Use first byte to divide the remaining buffer into 3Fths. I admit
52 * this disallows some number sizes. If it matters, better ideas are
53 * welcome (Ben).
54 */
c38bb727
BL
55 l1 = ((buf[0] & 0x3f) * len) / 0x3f;
56 s1 = buf[0] & 0x40;
57 s2 = buf[0] & 0x80;
58 ++buf;
59 l2 = len - l1;
60 }
61 OPENSSL_assert(BN_bin2bn(buf, l1, b1) == b1);
62 BN_set_negative(b1, s1);
63 OPENSSL_assert(BN_bin2bn(buf + l1, l2, b2) == b2);
64 BN_set_negative(b2, s2);
65
90d28f05 66 /* divide by 0 is an error */
c38bb727
BL
67 if (BN_is_zero(b2)) {
68 success = 1;
69 goto done;
70 }
71
72 OPENSSL_assert(BN_div(b3, b4, b1, b2, ctx));
73 if (BN_is_zero(b1))
74 success = BN_is_zero(b3) && BN_is_zero(b4);
75 else if (BN_is_negative(b1))
76 success = (BN_is_negative(b3) != BN_is_negative(b2) || BN_is_zero(b3))
77 && (BN_is_negative(b4) || BN_is_zero(b4));
78 else
79 success = (BN_is_negative(b3) == BN_is_negative(b2) || BN_is_zero(b3))
80 && (!BN_is_negative(b4) || BN_is_zero(b4));
81 OPENSSL_assert(BN_mul(b5, b3, b2, ctx));
82 OPENSSL_assert(BN_add(b5, b5, b4));
83
84 success = success && BN_cmp(b5, b1) == 0;
85 if (!success) {
86 BN_print_fp(stdout, b1);
87 putchar('\n');
88 BN_print_fp(stdout, b2);
89 putchar('\n');
90 BN_print_fp(stdout, b3);
91 putchar('\n');
92 BN_print_fp(stdout, b4);
93 putchar('\n');
94 BN_print_fp(stdout, b5);
95 putchar('\n');
96 printf("%d %d %d %d %d %d %d\n", BN_is_negative(b1),
97 BN_is_negative(b2),
98 BN_is_negative(b3), BN_is_negative(b4), BN_is_zero(b4),
99 BN_is_negative(b3) != BN_is_negative(b2)
100 && (BN_is_negative(b4) || BN_is_zero(b4)),
101 BN_cmp(b5, b1));
102 puts("----\n");
103 }
104
105 done:
106 OPENSSL_assert(success);
107
108 return 0;
109}
ad4da7fb
KR
110
111void FuzzerCleanup(void)
112{
8087bcb3
KR
113 BN_free(b1);
114 BN_free(b2);
115 BN_free(b3);
116 BN_free(b4);
117 BN_free(b5);
118 BN_CTX_free(ctx);
ad4da7fb 119}