Coverage Report

Created: 2022-11-24 06:56

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