Coverage Report

Created: 2020-06-30 13:58

/src/botan/build/include/botan/block_cipher.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
* Block Cipher Base Class
3
* (C) 1999-2009 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7
8
#ifndef BOTAN_BLOCK_CIPHER_H_
9
#define BOTAN_BLOCK_CIPHER_H_
10
11
#include <botan/sym_algo.h>
12
#include <string>
13
#include <memory>
14
#include <vector>
15
16
namespace Botan {
17
18
/**
19
* This class represents a block cipher object.
20
*/
21
class BOTAN_PUBLIC_API(2,0) BlockCipher : public SymmetricAlgorithm
22
   {
23
   public:
24
25
      /**
26
      * Create an instance based on a name
27
      * If provider is empty then best available is chosen.
28
      * @param algo_spec algorithm name
29
      * @param provider provider implementation to choose
30
      * @return a null pointer if the algo/provider combination cannot be found
31
      */
32
      static std::unique_ptr<BlockCipher>
33
         create(const std::string& algo_spec,
34
                const std::string& provider = "");
35
36
      /**
37
      * Create an instance based on a name, or throw if the
38
      * algo/provider combination cannot be found. If provider is
39
      * empty then best available is chosen.
40
      */
41
      static std::unique_ptr<BlockCipher>
42
         create_or_throw(const std::string& algo_spec,
43
                         const std::string& provider = "");
44
45
      /**
46
      * @return list of available providers for this algorithm, empty if not available
47
      * @param algo_spec algorithm name
48
      */
49
      static std::vector<std::string> providers(const std::string& algo_spec);
50
51
      /**
52
      * @return block size of this algorithm
53
      */
54
      virtual size_t block_size() const = 0;
55
56
      /**
57
      * @return native parallelism of this cipher in blocks
58
      */
59
1.27k
      virtual size_t parallelism() const { return 1; }
60
61
      /**
62
      * @return prefererred parallelism of this cipher in bytes
63
      */
64
      size_t parallel_bytes() const
65
4.03k
         {
66
4.03k
         return parallelism() * block_size() * BOTAN_BLOCK_CIPHER_PAR_MULT;
67
4.03k
         }
68
69
      /**
70
      * @return provider information about this implementation. Default is "base",
71
      * might also return "sse2", "avx2", "openssl", or some other arbitrary string.
72
      */
73
0
      virtual std::string provider() const { return "base"; }
74
75
      /**
76
      * Encrypt a block.
77
      * @param in The plaintext block to be encrypted as a byte array.
78
      * Must be of length block_size().
79
      * @param out The byte array designated to hold the encrypted block.
80
      * Must be of length block_size().
81
      */
82
      void encrypt(const uint8_t in[], uint8_t out[]) const
83
0
         { encrypt_n(in, out, 1); }
84
85
      /**
86
      * Decrypt a block.
87
      * @param in The ciphertext block to be decypted as a byte array.
88
      * Must be of length block_size().
89
      * @param out The byte array designated to hold the decrypted block.
90
      * Must be of length block_size().
91
      */
92
      void decrypt(const uint8_t in[], uint8_t out[]) const
93
0
         { decrypt_n(in, out, 1); }
94
95
      /**
96
      * Encrypt a block.
97
      * @param block the plaintext block to be encrypted
98
      * Must be of length block_size(). Will hold the result when the function
99
      * has finished.
100
      */
101
2.85k
      void encrypt(uint8_t block[]) const { encrypt_n(block, block, 1); }
102
103
      /**
104
      * Decrypt a block.
105
      * @param block the ciphertext block to be decrypted
106
      * Must be of length block_size(). Will hold the result when the function
107
      * has finished.
108
      */
109
0
      void decrypt(uint8_t block[]) const { decrypt_n(block, block, 1); }
110
111
      /**
112
      * Encrypt one or more blocks
113
      * @param block the input/output buffer (multiple of block_size())
114
      */
115
      template<typename Alloc>
116
      void encrypt(std::vector<uint8_t, Alloc>& block) const
117
8.60k
         {
118
8.60k
         return encrypt_n(block.data(), block.data(), block.size() / block_size());
119
8.60k
         }
120
121
      /**
122
      * Decrypt one or more blocks
123
      * @param block the input/output buffer (multiple of block_size())
124
      */
125
      template<typename Alloc>
126
      void decrypt(std::vector<uint8_t, Alloc>& block) const
127
0
         {
128
0
         return decrypt_n(block.data(), block.data(), block.size() / block_size());
129
0
         }
130
131
      /**
132
      * Encrypt one or more blocks
133
      * @param in the input buffer (multiple of block_size())
134
      * @param out the output buffer (same size as in)
135
      */
136
      template<typename Alloc, typename Alloc2>
137
      void encrypt(const std::vector<uint8_t, Alloc>& in,
138
                   std::vector<uint8_t, Alloc2>& out) const
139
7.06k
         {
140
7.06k
         return encrypt_n(in.data(), out.data(), in.size() / block_size());
141
7.06k
         }
142
143
      /**
144
      * Decrypt one or more blocks
145
      * @param in the input buffer (multiple of block_size())
146
      * @param out the output buffer (same size as in)
147
      */
148
      template<typename Alloc, typename Alloc2>
149
      void decrypt(const std::vector<uint8_t, Alloc>& in,
150
                   std::vector<uint8_t, Alloc2>& out) const
151
         {
152
         return decrypt_n(in.data(), out.data(), in.size() / block_size());
153
         }
154
155
      /**
156
      * Encrypt one or more blocks
157
      * @param in the input buffer (multiple of block_size())
158
      * @param out the output buffer (same size as in)
159
      * @param blocks the number of blocks to process
160
      */
161
      virtual void encrypt_n(const uint8_t in[], uint8_t out[],
162
                             size_t blocks) const = 0;
163
164
      /**
165
      * Decrypt one or more blocks
166
      * @param in the input buffer (multiple of block_size())
167
      * @param out the output buffer (same size as in)
168
      * @param blocks the number of blocks to process
169
      */
170
      virtual void decrypt_n(const uint8_t in[], uint8_t out[],
171
                             size_t blocks) const = 0;
172
173
      virtual void encrypt_n_xex(uint8_t data[],
174
                                 const uint8_t mask[],
175
                                 size_t blocks) const
176
0
         {
177
0
         const size_t BS = block_size();
178
0
         xor_buf(data, mask, blocks * BS);
179
0
         encrypt_n(data, data, blocks);
180
0
         xor_buf(data, mask, blocks * BS);
181
0
         }
182
183
      virtual void decrypt_n_xex(uint8_t data[],
184
                                 const uint8_t mask[],
185
                                 size_t blocks) const
186
0
         {
187
0
         const size_t BS = block_size();
188
0
         xor_buf(data, mask, blocks * BS);
189
0
         decrypt_n(data, data, blocks);
190
0
         xor_buf(data, mask, blocks * BS);
191
0
         }
192
193
      /**
194
      * @return new object representing the same algorithm as *this
195
      */
196
      virtual BlockCipher* clone() const = 0;
197
198
      virtual ~BlockCipher() = default;
199
   };
200
201
/**
202
* Tweakable block ciphers allow setting a tweak which is a non-keyed
203
* value which affects the encryption/decryption operation.
204
*/
205
class BOTAN_PUBLIC_API(2,8) Tweakable_Block_Cipher : public BlockCipher
206
   {
207
   public:
208
      /**
209
      * Set the tweak value. This must be called after setting a key. The value
210
      * persists until either set_tweak, set_key, or clear is called.
211
      * Different algorithms support different tweak length(s). If called with
212
      * an unsupported length, Invalid_Argument will be thrown.
213
      */
214
      virtual void set_tweak(const uint8_t tweak[], size_t len) = 0;
215
   };
216
217
/**
218
* Represents a block cipher with a single fixed block size
219
*/
220
template<size_t BS, size_t KMIN, size_t KMAX = 0, size_t KMOD = 1, typename BaseClass = BlockCipher>
221
class Block_Cipher_Fixed_Params : public BaseClass
222
   {
223
   public:
224
      enum { BLOCK_SIZE = BS };
225
26.2k
      size_t block_size() const final override { return BS; }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 32ul, 0ul, 1ul, Botan::BlockCipher>::block_size() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<64ul, 64ul, 0ul, 1ul, Botan::Tweakable_Block_Cipher>::block_size() const
Botan::Block_Cipher_Fixed_Params<16ul, 16ul, 0ul, 1ul, Botan::BlockCipher>::block_size() const
Line
Count
Source
225
12.5k
      size_t block_size() const final override { return BS; }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 24ul, 0ul, 1ul, Botan::BlockCipher>::block_size() const
Botan::Block_Cipher_Fixed_Params<16ul, 32ul, 0ul, 1ul, Botan::BlockCipher>::block_size() const
Line
Count
Source
225
12.7k
      size_t block_size() const final override { return BS; }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 16ul, 32ul, 8ul, Botan::BlockCipher>::block_size() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<32ul, 16ul, 64ul, 4ul, Botan::BlockCipher>::block_size() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 1ul, 56ul, 1ul, Botan::BlockCipher>::block_size() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 8ul, 0ul, 1ul, Botan::BlockCipher>::block_size() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 24ul, 0ul, 1ul, Botan::BlockCipher>::block_size() const
Botan::Block_Cipher_Fixed_Params<8ul, 16ul, 24ul, 8ul, Botan::BlockCipher>::block_size() const
Line
Count
Source
225
846
      size_t block_size() const final override { return BS; }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 11ul, 16ul, 1ul, Botan::BlockCipher>::block_size() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 4ul, 32ul, 4ul, Botan::BlockCipher>::block_size() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 16ul, 0ul, 1ul, Botan::BlockCipher>::block_size() const
226
227
      // override to take advantage of compile time constant block size
228
      void encrypt_n_xex(uint8_t data[],
229
                         const uint8_t mask[],
230
                         size_t blocks) const final override
231
255
         {
232
255
         xor_buf(data, mask, blocks * BS);
233
255
         this->encrypt_n(data, data, blocks);
234
255
         xor_buf(data, mask, blocks * BS);
235
255
         }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 32ul, 0ul, 1ul, Botan::BlockCipher>::encrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<64ul, 64ul, 0ul, 1ul, Botan::Tweakable_Block_Cipher>::encrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Botan::Block_Cipher_Fixed_Params<16ul, 16ul, 0ul, 1ul, Botan::BlockCipher>::encrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Line
Count
Source
231
96
         {
232
96
         xor_buf(data, mask, blocks * BS);
233
96
         this->encrypt_n(data, data, blocks);
234
96
         xor_buf(data, mask, blocks * BS);
235
96
         }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 24ul, 0ul, 1ul, Botan::BlockCipher>::encrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Botan::Block_Cipher_Fixed_Params<16ul, 32ul, 0ul, 1ul, Botan::BlockCipher>::encrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Line
Count
Source
231
159
         {
232
159
         xor_buf(data, mask, blocks * BS);
233
159
         this->encrypt_n(data, data, blocks);
234
159
         xor_buf(data, mask, blocks * BS);
235
159
         }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 16ul, 32ul, 8ul, Botan::BlockCipher>::encrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<32ul, 16ul, 64ul, 4ul, Botan::BlockCipher>::encrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 1ul, 56ul, 1ul, Botan::BlockCipher>::encrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 8ul, 0ul, 1ul, Botan::BlockCipher>::encrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 24ul, 0ul, 1ul, Botan::BlockCipher>::encrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 16ul, 24ul, 8ul, Botan::BlockCipher>::encrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 11ul, 16ul, 1ul, Botan::BlockCipher>::encrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 4ul, 32ul, 4ul, Botan::BlockCipher>::encrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 16ul, 0ul, 1ul, Botan::BlockCipher>::encrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
236
237
      void decrypt_n_xex(uint8_t data[],
238
                         const uint8_t mask[],
239
                         size_t blocks) const final override
240
1.24k
         {
241
1.24k
         xor_buf(data, mask, blocks * BS);
242
1.24k
         this->decrypt_n(data, data, blocks);
243
1.24k
         xor_buf(data, mask, blocks * BS);
244
1.24k
         }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 32ul, 0ul, 1ul, Botan::BlockCipher>::decrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<64ul, 64ul, 0ul, 1ul, Botan::Tweakable_Block_Cipher>::decrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Botan::Block_Cipher_Fixed_Params<16ul, 16ul, 0ul, 1ul, Botan::BlockCipher>::decrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Line
Count
Source
240
555
         {
241
555
         xor_buf(data, mask, blocks * BS);
242
555
         this->decrypt_n(data, data, blocks);
243
555
         xor_buf(data, mask, blocks * BS);
244
555
         }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 24ul, 0ul, 1ul, Botan::BlockCipher>::decrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Botan::Block_Cipher_Fixed_Params<16ul, 32ul, 0ul, 1ul, Botan::BlockCipher>::decrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Line
Count
Source
240
686
         {
241
686
         xor_buf(data, mask, blocks * BS);
242
686
         this->decrypt_n(data, data, blocks);
243
686
         xor_buf(data, mask, blocks * BS);
244
686
         }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 16ul, 32ul, 8ul, Botan::BlockCipher>::decrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<32ul, 16ul, 64ul, 4ul, Botan::BlockCipher>::decrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 1ul, 56ul, 1ul, Botan::BlockCipher>::decrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 8ul, 0ul, 1ul, Botan::BlockCipher>::decrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 24ul, 0ul, 1ul, Botan::BlockCipher>::decrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 16ul, 24ul, 8ul, Botan::BlockCipher>::decrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 11ul, 16ul, 1ul, Botan::BlockCipher>::decrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 4ul, 32ul, 4ul, Botan::BlockCipher>::decrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 16ul, 0ul, 1ul, Botan::BlockCipher>::decrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
245
246
      Key_Length_Specification key_spec() const final override
247
6.58k
         {
248
6.58k
         return Key_Length_Specification(KMIN, KMAX, KMOD);
249
6.58k
         }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 32ul, 0ul, 1ul, Botan::BlockCipher>::key_spec() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<64ul, 64ul, 0ul, 1ul, Botan::Tweakable_Block_Cipher>::key_spec() const
Botan::Block_Cipher_Fixed_Params<16ul, 16ul, 0ul, 1ul, Botan::BlockCipher>::key_spec() const
Line
Count
Source
247
2.08k
         {
248
2.08k
         return Key_Length_Specification(KMIN, KMAX, KMOD);
249
2.08k
         }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 24ul, 0ul, 1ul, Botan::BlockCipher>::key_spec() const
Botan::Block_Cipher_Fixed_Params<16ul, 32ul, 0ul, 1ul, Botan::BlockCipher>::key_spec() const
Line
Count
Source
247
3.84k
         {
248
3.84k
         return Key_Length_Specification(KMIN, KMAX, KMOD);
249
3.84k
         }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 16ul, 32ul, 8ul, Botan::BlockCipher>::key_spec() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<32ul, 16ul, 64ul, 4ul, Botan::BlockCipher>::key_spec() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 1ul, 56ul, 1ul, Botan::BlockCipher>::key_spec() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 8ul, 0ul, 1ul, Botan::BlockCipher>::key_spec() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 24ul, 0ul, 1ul, Botan::BlockCipher>::key_spec() const
Botan::Block_Cipher_Fixed_Params<8ul, 16ul, 24ul, 8ul, Botan::BlockCipher>::key_spec() const
Line
Count
Source
247
656
         {
248
656
         return Key_Length_Specification(KMIN, KMAX, KMOD);
249
656
         }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 11ul, 16ul, 1ul, Botan::BlockCipher>::key_spec() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 4ul, 32ul, 4ul, Botan::BlockCipher>::key_spec() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 16ul, 0ul, 1ul, Botan::BlockCipher>::key_spec() const
250
   };
251
252
}
253
254
#endif