Coverage Report

Created: 2025-03-09 06:52

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