The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
fuzzer_base16_32_64.c
Go to the documentation of this file.
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 */
17RCSID("$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
25int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len);
26
27int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
28{
29 uint8_t decoded[4096];
30 char encoded[8192];
31
32 fr_dbuff_t decode_dbuff, input_dbuff;
33 fr_sbuff_t encode_sbuff, input_sbuff;
35
36 if (size == 0) return 0;
37
38 /*
39 * BASE64 TESTS (4 variants)
40 */
41
42 // Test 1: Decode with standard base64 alphabet
43 fr_sbuff_init_in(&input_sbuff, (char const *)data, size);
44 fr_dbuff_init(&decode_dbuff, decoded, sizeof(decoded));
45 (void)fr_base64_decode_nstd(&err, &decode_dbuff, &input_sbuff,
46 true, true, fr_base64_alphabet_decode);
47
48 // Test 2: Decode with URL-safe base64 alphabet
49 fr_sbuff_init_in(&input_sbuff, (char const *)data, size);
50 fr_dbuff_init(&decode_dbuff, decoded, sizeof(decoded));
51 (void)fr_base64_decode_nstd(&err, &decode_dbuff, &input_sbuff,
52 false, false, fr_base64_url_alphabet_decode);
53
54 // Test 3: Encode with standard base64 alphabet (limit input size)
55 if (size <= 2048) {
56 fr_dbuff_init(&input_dbuff, data, size);
57 fr_sbuff_init_out(&encode_sbuff, encoded, sizeof(encoded));
58 (void)fr_base64_encode_nstd(&encode_sbuff, &input_dbuff,
60 }
61
62 // Test 4: Encode with URL-safe base64 alphabet (limit input size)
63 if (size <= 2048) {
64 fr_dbuff_init(&input_dbuff, data, size);
65 fr_sbuff_init_out(&encode_sbuff, encoded, sizeof(encoded));
66 (void)fr_base64_encode_nstd(&encode_sbuff, &input_dbuff,
68 }
69
70 /*
71 * BASE32 TESTS (4 variants)
72 */
73
74 // Test 5: Decode with standard base32 alphabet
75 fr_sbuff_init_in(&input_sbuff, (char const *)data, size);
76 fr_dbuff_init(&decode_dbuff, decoded, sizeof(decoded));
77 (void)fr_base32_decode_nstd(&err, &decode_dbuff, &input_sbuff,
78 true, true, fr_base32_alphabet_decode);
79
80 // Test 6: Decode with base32hex alphabet
81 fr_sbuff_init_in(&input_sbuff, (char const *)data, size);
82 fr_dbuff_init(&decode_dbuff, decoded, sizeof(decoded));
83 (void)fr_base32_decode_nstd(&err, &decode_dbuff, &input_sbuff,
84 false, false, fr_base32_hex_alphabet_decode);
85
86 // Test 7: Encode with standard base32 alphabet (limit input size)
87 if (size <= 2048) {
88 fr_dbuff_init(&input_dbuff, data, size);
89 fr_sbuff_init_out(&encode_sbuff, encoded, sizeof(encoded));
90 (void)fr_base32_encode_nstd(&encode_sbuff, &input_dbuff,
92 }
93
94 // Test 8: Encode with base32hex alphabet (limit input size)
95 if (size <= 2048) {
96 fr_dbuff_init(&input_dbuff, data, size);
97 fr_sbuff_init_out(&encode_sbuff, encoded, sizeof(encoded));
98 (void)fr_base32_encode_nstd(&encode_sbuff, &input_dbuff,
100 }
101
102 /*
103 * BASE16 TESTS (3 variants)
104 */
105
106 // Test 9: Decode with mixed-case base16 alphabet
107 fr_sbuff_init_in(&input_sbuff, (char const *)data, size);
108 fr_dbuff_init(&decode_dbuff, decoded, sizeof(decoded));
109 (void)fr_base16_decode_nstd(&err, &decode_dbuff, &input_sbuff,
111
112 // Test 10: Encode with lowercase base16 alphabet (limit input size)
113 if (size <= 2048) {
114 fr_dbuff_init(&input_dbuff, data, size);
115 fr_sbuff_init_out(&encode_sbuff, encoded, sizeof(encoded));
116 (void)fr_base16_encode_nstd(&encode_sbuff, &input_dbuff,
118 }
119
120 // Test 11: Encode with uppercase base16 alphabet (limit input size)
121 if (size <= 2048) {
122 fr_dbuff_init(&input_dbuff, data, size);
123 fr_sbuff_init_out(&encode_sbuff, encoded, sizeof(encoded));
124 (void)fr_base16_encode_nstd(&encode_sbuff, &input_dbuff,
126 }
127
128 return 0;
129}
char const fr_base16_alphabet_encode_uc[SBUFF_CHAR_CLASS]
lower case encode alphabet for base16
Definition base16.c:53
char const fr_base16_alphabet_encode_lc[SBUFF_CHAR_CLASS]
lower case encode alphabet for base16
Definition base16.c:31
uint8_t const fr_base16_alphabet_decode_mc[SBUFF_CHAR_CLASS]
Mixed case decode alphabet for base16.
Definition base16.c:75
char const fr_base32_alphabet_encode[SBUFF_CHAR_CLASS]
Encode/decode binary data using printable characters (base32 format)
Definition base32.c:32
char const fr_base32_hex_alphabet_encode[SBUFF_CHAR_CLASS]
Definition base32.c:105
ssize_t fr_base32_encode_nstd(fr_sbuff_t *out, fr_dbuff_t *in, bool add_padding, char const alphabet[static SBUFF_CHAR_CLASS])
Base 64 encode binary data.
Definition base32.c:190
uint8_t const fr_base32_alphabet_decode[SBUFF_CHAR_CLASS]
Definition base32.c:67
fr_slen_t fr_base32_decode_nstd(fr_sbuff_parse_error_t *err, fr_dbuff_t *out, fr_sbuff_t *in, bool expect_padding, bool no_trailing, uint8_t const alphabet[static SBUFF_CHAR_CLASS])
Definition base32.c:315
uint8_t const fr_base32_hex_alphabet_decode[SBUFF_CHAR_CLASS]
Definition base32.c:140
fr_slen_t fr_base64_decode_nstd(fr_sbuff_parse_error_t *err, fr_dbuff_t *out, fr_sbuff_t *in, bool expect_padding, bool no_trailing, uint8_t const alphabet[static SBUFF_CHAR_CLASS])
Definition base64.c:401
char const fr_base64_url_alphabet_encode[SBUFF_CHAR_CLASS]
Definition base64.c:173
char const fr_base64_alphabet_encode[SBUFF_CHAR_CLASS]
Definition base64.c:32
uint8_t const fr_base64_alphabet_decode[SBUFF_CHAR_CLASS]
Definition base64.c:99
uint8_t const fr_base64_url_alphabet_decode[SBUFF_CHAR_CLASS]
Definition base64.c:240
ssize_t fr_base64_encode_nstd(fr_sbuff_t *out, fr_dbuff_t *in, bool add_padding, char const alphabet[static SBUFF_CHAR_CLASS])
Base 64 encode binary data.
Definition base64.c:326
#define RCSID(id)
Definition build.h:512
#define fr_dbuff_init(_out, _start, _len_or_end)
Initialise an dbuff for encoding or decoding.
Definition dbuff.h:362
static fr_slen_t err
Definition dict.h:882
int LLVMFuzzerTestOneInput(const uint8_t *buf, size_t len)
fr_slen_t fr_base16_decode_nstd(fr_sbuff_parse_error_t *err, fr_dbuff_t *out, fr_sbuff_t *in, bool no_trailing, uint8_t const alphabet[static SBUFF_CHAR_CLASS])
unsigned char uint8_t
fr_sbuff_parse_error_t
fr_slen_t fr_base16_encode_nstd(fr_sbuff_t *out, fr_dbuff_t *in, char const alphabet[static SBUFF_CHAR_CLASS])
#define fr_sbuff_init_out(_out, _start, _len_or_end)
#define fr_sbuff_init_in(_out, _start, _len_or_end)
static fr_slen_t data
Definition value.h:1340