1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
/*
* (C) 2018 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include "fuzzers.h"
#include <botan/mode_pad.h>
#include <botan/internal/tls_cbc.h>
namespace {
size_t ref_pkcs7_unpad(const uint8_t in[], size_t len)
{
if(len <= 2)
return len;
const size_t padding_length = in[len-1];
if(padding_length == 0 || padding_length > len)
return len;
const size_t padding_start = len - padding_length;
for(size_t i = padding_start; i != len; ++i)
{
if(in[i] != padding_length)
return len;
}
return len - padding_length;
}
size_t ref_x923_unpad(const uint8_t in[], size_t len)
{
if(len <= 2)
return len;
const size_t padding_length = in[len-1];
if(padding_length == 0 || padding_length > len)
return len;
const size_t padding_start = len - padding_length;
for(size_t i = padding_start; i != len - 1; ++i)
{
if(in[i] != 0)
{
return len;
}
}
return len - padding_length;
}
size_t ref_oneandzero_unpad(const uint8_t in[], size_t len)
{
if(len <= 2)
return len;
size_t idx = len - 1;
for(;;)
{
if(in[idx] == 0)
{
if(idx == 0)
return len;
idx -= 1;
continue;
}
else if(in[idx] == 0x80)
{
return idx;
}
else
return len;
}
return len;
}
size_t ref_esp_unpad(const uint8_t in[], size_t len)
{
if(len <= 2)
return len;
const size_t padding_bytes = in[len - 1];
if(padding_bytes == 0 || padding_bytes > len)
{
return len;
}
const size_t padding_start = len - padding_bytes;
for(size_t i = padding_start; i != len; ++i)
{
if(in[i] != (i - padding_start + 1))
{
return len;
}
}
return len - padding_bytes;
}
uint16_t ref_tls_cbc_unpad(const uint8_t in[], size_t len)
{
if(len == 0)
return 0;
const size_t padding_length = in[(len-1)];
if(padding_length >= len)
return 0;
/*
* TLS v1.0 and up require all the padding bytes be the same value
* and allows up to 255 bytes.
*/
for(size_t i = 0; i != 1 + padding_length; ++i)
{
if(in[(len-i-1)] != padding_length)
return 0;
}
return padding_length + 1;
}
}
void fuzz(const uint8_t in[], size_t len)
{
static Botan::PKCS7_Padding pkcs7;
static Botan::ANSI_X923_Padding x923;
static Botan::OneAndZeros_Padding oneandzero;
static Botan::ESP_Padding esp;
if(pkcs7.valid_blocksize(len))
{
const size_t ct_pkcs7 = pkcs7.unpad(in, len);
const size_t ref_pkcs7 = ref_pkcs7_unpad(in, len);
FUZZER_ASSERT_EQUAL(ct_pkcs7, ref_pkcs7);
}
if(x923.valid_blocksize(len))
{
const size_t ct_x923 = x923.unpad(in, len);
const size_t ref_x923 = ref_x923_unpad(in, len);
FUZZER_ASSERT_EQUAL(ct_x923, ref_x923);
}
if(oneandzero.valid_blocksize(len))
{
const size_t ct_oneandzero = oneandzero.unpad(in, len);
const size_t ref_oneandzero = ref_oneandzero_unpad(in, len);
FUZZER_ASSERT_EQUAL(ct_oneandzero, ref_oneandzero);
}
if(esp.valid_blocksize(len))
{
const size_t ct_esp = esp.unpad(in, len);
const size_t ref_esp = ref_esp_unpad(in, len);
FUZZER_ASSERT_EQUAL(ct_esp, ref_esp);
}
const uint16_t ct_cbc = Botan::TLS::check_tls_cbc_padding(in, len);
const uint16_t ref_cbc = ref_tls_cbc_unpad(in, len);
FUZZER_ASSERT_EQUAL(ct_cbc, ref_cbc);
}
|