Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/fuzz/bignum.c
Line
Count
Source
1
/*
2
 * Copyright 2016-2026 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 a^b mod c agrees when calculated cleverly vs naively, for
13
 * random a, b and c.
14
 */
15
16
#include <stdio.h>
17
#include <openssl/bn.h>
18
#include <openssl/err.h>
19
#include "fuzzer.h"
20
21
int FuzzerInitialize(int *argc, char ***argv)
22
229
{
23
229
    OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
24
229
    ERR_clear_error();
25
26
229
    return 1;
27
229
}
28
29
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
30
2.76k
{
31
2.76k
    size_t l1 = 0, l2 = 0, l3 = 0;
32
2.76k
    int s1 = 0, s3 = 0;
33
2.76k
    BN_CTX *ctx;
34
2.76k
    BIGNUM *b1;
35
2.76k
    BIGNUM *b2;
36
2.76k
    BIGNUM *b3;
37
2.76k
    BIGNUM *b4;
38
2.76k
    BIGNUM *b5;
39
40
2.76k
    b1 = BN_new();
41
2.76k
    b2 = BN_new();
42
2.76k
    b3 = BN_new();
43
2.76k
    b4 = BN_new();
44
2.76k
    b5 = BN_new();
45
2.76k
    ctx = BN_CTX_new();
46
47
2.76k
    if (b1 == NULL || b2 == NULL || b3 == NULL || b4 == NULL || b5 == NULL
48
2.76k
        || ctx == NULL)
49
0
        goto done;
50
51
    /* Divide the input into three parts, using the values of the first two
52
     * bytes to choose lengths, which generate b1, b2 and b3. Use three bits
53
     * of the third byte to choose signs for the three numbers.
54
     */
55
2.76k
    if (len > 2) {
56
2.76k
        len -= 3;
57
        /* limit l1, l2, and l3 to be no more than 512 bytes */
58
2.76k
        l1 = ((buf[0] * len) / 255) % 512;
59
2.76k
        ++buf;
60
2.76k
        l2 = ((buf[0] * (len - l1)) / 255) % 512;
61
2.76k
        ++buf;
62
2.76k
        l3 = (len - l1 - l2) % 512;
63
64
2.76k
        s1 = buf[0] & 1;
65
2.76k
        s3 = buf[0] & 4;
66
2.76k
        ++buf;
67
2.76k
    }
68
2.76k
    if (BN_bin2bn(buf, (int)l1, b1) != b1)
69
0
        goto done;
70
2.76k
    BN_set_negative(b1, s1);
71
2.76k
    if (BN_bin2bn(buf + l1, (int)l2, b2) != b2)
72
0
        goto done;
73
2.76k
    if (BN_bin2bn(buf + l1 + l2, (int)l3, b3) != b3)
74
0
        goto done;
75
2.76k
    BN_set_negative(b3, s3);
76
77
    /* mod 0 is undefined */
78
2.76k
    if (BN_is_zero(b3))
79
40
        goto done;
80
81
2.72k
    if (!BN_mod_exp(b4, b1, b2, b3, ctx))
82
0
        goto done;
83
2.72k
    if (!BN_mod_exp_simple(b5, b1, b2, b3, ctx))
84
0
        goto done;
85
86
2.72k
    if (BN_cmp(b4, b5) != 0) {
87
0
        BN_print_fp(stdout, b1);
88
0
        putchar('\n');
89
0
        BN_print_fp(stdout, b2);
90
0
        putchar('\n');
91
0
        BN_print_fp(stdout, b3);
92
0
        putchar('\n');
93
0
        BN_print_fp(stdout, b4);
94
0
        putchar('\n');
95
0
        BN_print_fp(stdout, b5);
96
0
        putchar('\n');
97
0
    }
98
99
2.76k
done:
100
2.76k
    BN_free(b1);
101
2.76k
    BN_free(b2);
102
2.76k
    BN_free(b3);
103
2.76k
    BN_free(b4);
104
2.76k
    BN_free(b5);
105
2.76k
    BN_CTX_free(ctx);
106
2.76k
    ERR_clear_error();
107
108
2.76k
    return 0;
109
2.72k
}
110
111
void FuzzerCleanup(void)
112
0
{
113
0
}