Coverage Report

Created: 2025-12-31 06:58

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