Coverage Report

Created: 2025-03-09 06:52

/src/libressl.fuzzers/bndiv.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3
 *
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.
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 <openssl/err.h>
19
#include "fuzzer.h"
20
21
/* 256 kB */
22
1.87k
#define MAX_LEN (256 * 1000)
23
24
static BN_CTX *ctx;
25
static BIGNUM *b1;
26
static BIGNUM *b2;
27
static BIGNUM *b3;
28
static BIGNUM *b4;
29
static BIGNUM *b5;
30
31
int FuzzerInitialize(int *argc, char ***argv)
32
16
{
33
16
    b1 = BN_new();
34
16
    b2 = BN_new();
35
16
    b3 = BN_new();
36
16
    b4 = BN_new();
37
16
    b5 = BN_new();
38
16
    ctx = BN_CTX_new();
39
40
16
    OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
41
42
16
    return 1;
43
16
}
44
45
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
46
1.87k
{
47
1.87k
    int success = 0;
48
1.87k
    size_t l1 = 0, l2 = 0;
49
    /* s1 and s2 will be the signs for b1 and b2. */
50
1.87k
    int s1 = 0, s2 = 0;
51
52
    /* limit the size of the input to avoid timeout */
53
1.87k
    if (len > MAX_LEN)
54
3
        len = MAX_LEN;
55
56
    /* We are going to split the buffer in two, sizes l1 and l2, giving b1 and
57
     * b2.
58
     */
59
1.87k
    if (len > 0) {
60
1.87k
        --len;
61
        /* Use first byte to divide the remaining buffer into 3Fths. I admit
62
         * this disallows some number sizes. If it matters, better ideas are
63
         * welcome (Ben).
64
         */
65
1.87k
        l1 = ((buf[0] & 0x3f) * len) / 0x3f;
66
1.87k
        s1 = buf[0] & 0x40;
67
1.87k
        s2 = buf[0] & 0x80;
68
1.87k
        ++buf;
69
1.87k
        l2 = len - l1;
70
1.87k
    }
71
1.87k
    OPENSSL_assert(BN_bin2bn(buf, l1, b1) == b1);
72
0
    BN_set_negative(b1, s1);
73
1.87k
    OPENSSL_assert(BN_bin2bn(buf + l1, l2, b2) == b2);
74
0
    BN_set_negative(b2, s2);
75
76
    /* divide by 0 is an error */
77
1.87k
    if (BN_is_zero(b2)) {
78
51
        success = 1;
79
51
        goto done;
80
51
    }
81
82
1.82k
    OPENSSL_assert(BN_div(b3, b4, b1, b2, ctx));
83
1.82k
    if (BN_is_zero(b1))
84
212
        success = BN_is_zero(b3) && BN_is_zero(b4);
85
1.60k
    else if (BN_is_negative(b1))
86
937
        success = (BN_is_negative(b3) != BN_is_negative(b2) || BN_is_zero(b3))
87
937
            && (BN_is_negative(b4) || BN_is_zero(b4));
88
672
    else
89
672
        success = (BN_is_negative(b3) == BN_is_negative(b2)  || BN_is_zero(b3))
90
672
            && (!BN_is_negative(b4) || BN_is_zero(b4));
91
1.82k
    OPENSSL_assert(BN_mul(b5, b3, b2, ctx));
92
1.82k
    OPENSSL_assert(BN_add(b5, b5, b4));
93
94
1.82k
    success = success && BN_cmp(b5, b1) == 0;
95
1.82k
    if (!success) {
96
0
        BN_print_fp(stdout, b1);
97
0
        putchar('\n');
98
0
        BN_print_fp(stdout, b2);
99
0
        putchar('\n');
100
0
        BN_print_fp(stdout, b3);
101
0
        putchar('\n');
102
0
        BN_print_fp(stdout, b4);
103
0
        putchar('\n');
104
0
        BN_print_fp(stdout, b5);
105
0
        putchar('\n');
106
0
        printf("%d %d %d %d %d %d %d\n", BN_is_negative(b1),
107
0
               BN_is_negative(b2),
108
0
               BN_is_negative(b3), BN_is_negative(b4), BN_is_zero(b4),
109
0
               BN_is_negative(b3) != BN_is_negative(b2)
110
0
               && (BN_is_negative(b4) || BN_is_zero(b4)),
111
0
               BN_cmp(b5, b1));
112
0
        puts("----\n");
113
0
    }
114
115
1.87k
 done:
116
1.87k
    OPENSSL_assert(success);
117
0
    ERR_clear_error();
118
119
1.87k
    return 0;
120
1.82k
}
121
122
void FuzzerCleanup(void)
123
0
{
124
0
    BN_free(b1);
125
0
    BN_free(b2);
126
0
    BN_free(b3);
127
0
    BN_free(b4);
128
0
    BN_free(b5);
129
0
    BN_CTX_free(ctx);
130
0
}