Coverage Report

Created: 2026-08-14 07:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/freeradius-server/src/fuzzer/fuzzer_base16_32_64.c
Line
Count
Source
1
/*
2
 *   This program is free software; you can redistribute it and/or modify
3
 *   it under the terms of the GNU General Public License as published by
4
 *   the Free Software Foundation; either version 2 of the License, or
5
 *   (at your option) any later version.
6
 *
7
 *   This program is distributed in the hope that it will be useful,
8
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 *   GNU General Public License for more details.
11
 */
12
13
/**
14
 * @file src/fuzzer/fuzzer_base16_32_64.c
15
 * @brief Fuzz the base conversion functions
16
 */
17
RCSID("$Id: d7a711582935deae35e0a2b2bd8cd757aa22cfa3 $")
18
19
#include <freeradius-devel/util/base16.h>
20
#include <freeradius-devel/util/base32.h>
21
#include <freeradius-devel/util/base64.h>
22
#include <freeradius-devel/util/dbuff.h>
23
#include <freeradius-devel/util/sbuff.h>
24
25
int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len);
26
27
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
28
350
{
29
350
    uint8_t decoded[4096];
30
350
    char encoded[8192];
31
32
350
    fr_dbuff_t decode_dbuff, input_dbuff;
33
350
    fr_sbuff_t encode_sbuff, input_sbuff;
34
350
    fr_sbuff_parse_error_t err;
35
36
350
    if (size == 0) return 0;
37
38
    /*
39
     * BASE64 TESTS (4 variants)
40
     */
41
42
    // Test 1: Decode with standard base64 alphabet
43
350
    fr_sbuff_init_in(&input_sbuff, (char const *)data, size);
44
350
    fr_dbuff_init(&decode_dbuff, decoded, sizeof(decoded));
45
350
    (void)fr_base64_decode_nstd(&err, &decode_dbuff, &input_sbuff, 
46
350
                                true, true, fr_base64_alphabet_decode);
47
48
    // Test 2: Decode with URL-safe base64 alphabet
49
350
    fr_sbuff_init_in(&input_sbuff, (char const *)data, size);
50
350
    fr_dbuff_init(&decode_dbuff, decoded, sizeof(decoded));
51
350
    (void)fr_base64_decode_nstd(&err, &decode_dbuff, &input_sbuff, 
52
350
                                false, false, fr_base64_url_alphabet_decode);
53
54
    // Test 3: Encode with standard base64 alphabet (limit input size)
55
350
    if (size <= 2048) {
56
245
        fr_dbuff_init(&input_dbuff, data, size);
57
245
        fr_sbuff_init_out(&encode_sbuff, encoded, sizeof(encoded));
58
245
        (void)fr_base64_encode_nstd(&encode_sbuff, &input_dbuff, 
59
245
                                    true, fr_base64_alphabet_encode);
60
245
    }
61
62
    // Test 4: Encode with URL-safe base64 alphabet (limit input size)
63
350
    if (size <= 2048) {
64
245
        fr_dbuff_init(&input_dbuff, data, size);
65
245
        fr_sbuff_init_out(&encode_sbuff, encoded, sizeof(encoded));
66
245
        (void)fr_base64_encode_nstd(&encode_sbuff, &input_dbuff, 
67
245
                                    false, fr_base64_url_alphabet_encode);
68
245
    }
69
70
    /*
71
     * BASE32 TESTS (4 variants)
72
     */
73
74
    // Test 5: Decode with standard base32 alphabet
75
350
    fr_sbuff_init_in(&input_sbuff, (char const *)data, size);
76
350
    fr_dbuff_init(&decode_dbuff, decoded, sizeof(decoded));
77
350
    (void)fr_base32_decode_nstd(&err, &decode_dbuff, &input_sbuff,
78
350
                                true, true, fr_base32_alphabet_decode);
79
80
    // Test 6: Decode with base32hex alphabet
81
350
    fr_sbuff_init_in(&input_sbuff, (char const *)data, size);
82
350
    fr_dbuff_init(&decode_dbuff, decoded, sizeof(decoded));
83
350
    (void)fr_base32_decode_nstd(&err, &decode_dbuff, &input_sbuff,
84
350
                                false, false, fr_base32_hex_alphabet_decode);
85
86
    // Test 7: Encode with standard base32 alphabet (limit input size)
87
350
    if (size <= 2048) {
88
245
        fr_dbuff_init(&input_dbuff, data, size);
89
245
        fr_sbuff_init_out(&encode_sbuff, encoded, sizeof(encoded));
90
245
        (void)fr_base32_encode_nstd(&encode_sbuff, &input_dbuff,
91
245
                                    true, fr_base32_alphabet_encode);
92
245
    }
93
94
    // Test 8: Encode with base32hex alphabet (limit input size)
95
350
    if (size <= 2048) {
96
245
        fr_dbuff_init(&input_dbuff, data, size);
97
245
        fr_sbuff_init_out(&encode_sbuff, encoded, sizeof(encoded));
98
245
        (void)fr_base32_encode_nstd(&encode_sbuff, &input_dbuff,
99
245
                                    false, fr_base32_hex_alphabet_encode);
100
245
    }
101
102
    /*
103
     * BASE16 TESTS (3 variants)
104
     */
105
106
    // Test 9: Decode with mixed-case base16 alphabet
107
350
    fr_sbuff_init_in(&input_sbuff, (char const *)data, size);
108
350
    fr_dbuff_init(&decode_dbuff, decoded, sizeof(decoded));
109
350
    (void)fr_base16_decode_nstd(&err, &decode_dbuff, &input_sbuff,
110
350
                                true, fr_base16_alphabet_decode_mc);
111
112
    // Test 10: Encode with lowercase base16 alphabet (limit input size)
113
350
    if (size <= 2048) {
114
245
        fr_dbuff_init(&input_dbuff, data, size);
115
245
        fr_sbuff_init_out(&encode_sbuff, encoded, sizeof(encoded));
116
245
        (void)fr_base16_encode_nstd(&encode_sbuff, &input_dbuff,
117
245
                                    fr_base16_alphabet_encode_lc);
118
245
    }
119
120
    // Test 11: Encode with uppercase base16 alphabet (limit input size)
121
350
    if (size <= 2048) {
122
245
        fr_dbuff_init(&input_dbuff, data, size);
123
245
        fr_sbuff_init_out(&encode_sbuff, encoded, sizeof(encoded));
124
245
        (void)fr_base16_encode_nstd(&encode_sbuff, &input_dbuff,
125
245
                                    fr_base16_alphabet_encode_uc);
126
245
    }
127
128
350
    return 0;
129
350
}