Coverage Report

Created: 2020-10-17 06:46

/src/botan/build/include/botan/mode_pad.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* CBC Padding Methods
3
* (C) 1999-2008,2013 Jack Lloyd
4
* (C) 2016 René Korthaus, Rohde & Schwarz Cybersecurity
5
*
6
* Botan is released under the Simplified BSD License (see license.txt)
7
*/
8
9
#ifndef BOTAN_MODE_PADDING_H_
10
#define BOTAN_MODE_PADDING_H_
11
12
#include <botan/secmem.h>
13
#include <string>
14
15
BOTAN_FUTURE_INTERNAL_HEADER(mode_pad.h)
16
17
namespace Botan {
18
19
/**
20
* Block Cipher Mode Padding Method
21
* This class is pretty limited, it cannot deal well with
22
* randomized padding methods, or any padding method that
23
* wants to add more than one block. For instance, it should
24
* be possible to define cipher text stealing mode as simply
25
* a padding mode for CBC, which happens to consume the last
26
* two block (and requires use of the block cipher).
27
*/
28
class BOTAN_PUBLIC_API(2,0) BlockCipherModePaddingMethod
29
   {
30
   public:
31
      /**
32
      * Add padding bytes to buffer.
33
      * @param buffer data to pad
34
      * @param final_block_bytes size of the final block in bytes
35
      * @param block_size size of each block in bytes
36
      */
37
      virtual void add_padding(secure_vector<uint8_t>& buffer,
38
                               size_t final_block_bytes,
39
                               size_t block_size) const = 0;
40
41
      /**
42
      * Remove padding bytes from block
43
      * @param block the last block
44
      * @param len the size of the block in bytes
45
      * @return number of data bytes, or if the padding is invalid returns len
46
      */
47
      virtual size_t unpad(const uint8_t block[], size_t len) const = 0;
48
49
      /**
50
      * @param block_size of the cipher
51
      * @return valid block size for this padding mode
52
      */
53
      virtual bool valid_blocksize(size_t block_size) const = 0;
54
55
      /**
56
      * @return name of the mode
57
      */
58
      virtual std::string name() const = 0;
59
60
      /**
61
      * virtual destructor
62
      */
63
1.37k
      virtual ~BlockCipherModePaddingMethod() = default;
64
   };
65
66
/**
67
* PKCS#7 Padding
68
*/
69
class BOTAN_PUBLIC_API(2,0) PKCS7_Padding final : public BlockCipherModePaddingMethod
70
   {
71
   public:
72
      void add_padding(secure_vector<uint8_t>& buffer,
73
                       size_t final_block_bytes,
74
                       size_t block_size) const override;
75
76
      size_t unpad(const uint8_t[], size_t) const override;
77
78
377
      bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); }
79
80
0
      std::string name() const override { return "PKCS7"; }
81
   };
82
83
/**
84
* ANSI X9.23 Padding
85
*/
86
class BOTAN_PUBLIC_API(2,0) ANSI_X923_Padding final : public BlockCipherModePaddingMethod
87
   {
88
   public:
89
      void add_padding(secure_vector<uint8_t>& buffer,
90
                       size_t final_block_bytes,
91
                       size_t block_size) const override;
92
93
      size_t unpad(const uint8_t[], size_t) const override;
94
95
377
      bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); }
96
97
0
      std::string name() const override { return "X9.23"; }
98
   };
99
100
/**
101
* One And Zeros Padding (ISO/IEC 9797-1, padding method 2)
102
*/
103
class BOTAN_PUBLIC_API(2,0) OneAndZeros_Padding final : public BlockCipherModePaddingMethod
104
   {
105
   public:
106
      void add_padding(secure_vector<uint8_t>& buffer,
107
                       size_t final_block_bytes,
108
                       size_t block_size) const override;
109
110
      size_t unpad(const uint8_t[], size_t) const override;
111
112
434
      bool valid_blocksize(size_t bs) const override { return (bs > 2); }
113
114
0
      std::string name() const override { return "OneAndZeros"; }
115
   };
116
117
/**
118
* ESP Padding (RFC 4304)
119
*/
120
class BOTAN_PUBLIC_API(2,0) ESP_Padding final : public BlockCipherModePaddingMethod
121
   {
122
   public:
123
      void add_padding(secure_vector<uint8_t>& buffer,
124
                       size_t final_block_bytes,
125
                       size_t block_size) const override;
126
127
      size_t unpad(const uint8_t[], size_t) const override;
128
129
377
      bool valid_blocksize(size_t bs) const override { return (bs > 2 && bs < 256); }
130
131
0
      std::string name() const override { return "ESP"; }
132
   };
133
134
/**
135
* Null Padding
136
*/
137
class BOTAN_PUBLIC_API(2,0) Null_Padding final : public BlockCipherModePaddingMethod
138
   {
139
   public:
140
      void add_padding(secure_vector<uint8_t>&, size_t, size_t) const override
141
0
         {
142
         /* no padding */
143
0
         }
144
145
0
      size_t unpad(const uint8_t[], size_t size) const override { return size; }
146
147
1.36k
      bool valid_blocksize(size_t) const override { return true; }
148
149
0
      std::string name() const override { return "NoPadding"; }
150
   };
151
152
/**
153
* Get a block cipher padding mode by name (eg "NoPadding" or "PKCS7")
154
* @param algo_spec block cipher padding mode name
155
*/
156
BOTAN_PUBLIC_API(2,0) BlockCipherModePaddingMethod* get_bc_pad(const std::string& algo_spec);
157
158
}
159
160
#endif