Coverage Report

Created: 2021-02-21 07:20

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