Coverage Report

Created: 2021-04-07 06:07

/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
255
      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
1.64k
         {
66
1.64k
         return parallelism() * block_size() * BOTAN_BLOCK_CIPHER_PAR_MULT;
67
1.64k
         }
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
885
      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
1.44k
         {
118
1.44k
         return encrypt_n(block.data(), block.data(), block.size() / block_size());
119
1.44k
         }
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
1.08k
         {
140
1.08k
         return encrypt_n(in.data(), out.data(), in.size() / block_size());
141
1.08k
         }
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 std::unique_ptr<BlockCipher> new_object() const = 0;
197
198
      BlockCipher* clone() const
199
0
         {
200
0
         return this->new_object().release();
201
0
         }
202
203
      virtual ~BlockCipher() = default;
204
   };
205
206
/**
207
* Tweakable block ciphers allow setting a tweak which is a non-keyed
208
* value which affects the encryption/decryption operation.
209
*/
210
class BOTAN_PUBLIC_API(2,8) Tweakable_Block_Cipher : public BlockCipher
211
   {
212
   public:
213
      /**
214
      * Set the tweak value. This must be called after setting a key. The value
215
      * persists until either set_tweak, set_key, or clear is called.
216
      * Different algorithms support different tweak length(s). If called with
217
      * an unsupported length, Invalid_Argument will be thrown.
218
      */
219
      virtual void set_tweak(const uint8_t tweak[], size_t len) = 0;
220
   };
221
222
/**
223
* Represents a block cipher with a single fixed block size
224
*/
225
template<size_t BS, size_t KMIN, size_t KMAX = 0, size_t KMOD = 1, typename BaseClass = BlockCipher>
226
class Block_Cipher_Fixed_Params : public BaseClass
227
   {
228
   public:
229
      enum { BLOCK_SIZE = BS };
230
6.84k
      size_t block_size() const final override { return BS; }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<64ul, 64ul, 0ul, 1ul, Botan::Tweakable_Block_Cipher>::block_size() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 32ul, 0ul, 1ul, Botan::BlockCipher>::block_size() const
Botan::Block_Cipher_Fixed_Params<16ul, 16ul, 0ul, 1ul, Botan::BlockCipher>::block_size() const
Line
Count
Source
230
1.02k
      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
230
5.49k
      size_t block_size() const final override { return BS; }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 1ul, 56ul, 1ul, Botan::BlockCipher>::block_size() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 11ul, 16ul, 1ul, Botan::BlockCipher>::block_size() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 8ul, 0ul, 1ul, Botan::BlockCipher>::block_size() const
Botan::Block_Cipher_Fixed_Params<8ul, 16ul, 24ul, 8ul, Botan::BlockCipher>::block_size() const
Line
Count
Source
230
329
      size_t block_size() const final override { return BS; }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 16ul, 0ul, 1ul, Botan::BlockCipher>::block_size() const
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
231
232
      // override to take advantage of compile time constant block size
233
      void encrypt_n_xex(uint8_t data[],
234
                         const uint8_t mask[],
235
                         size_t blocks) const final override
236
86
         {
237
86
         xor_buf(data, mask, blocks * BS);
238
86
         this->encrypt_n(data, data, blocks);
239
86
         xor_buf(data, mask, blocks * BS);
240
86
         }
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
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<16ul, 16ul, 0ul, 1ul, Botan::BlockCipher>::encrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
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
236
86
         {
237
86
         xor_buf(data, mask, blocks * BS);
238
86
         this->encrypt_n(data, data, blocks);
239
86
         xor_buf(data, mask, blocks * BS);
240
86
         }
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, 11ul, 16ul, 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, 16ul, 24ul, 8ul, 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
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
241
242
      void decrypt_n_xex(uint8_t data[],
243
                         const uint8_t mask[],
244
                         size_t blocks) const final override
245
44
         {
246
44
         xor_buf(data, mask, blocks * BS);
247
44
         this->decrypt_n(data, data, blocks);
248
44
         xor_buf(data, mask, blocks * BS);
249
44
         }
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
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<16ul, 16ul, 0ul, 1ul, Botan::BlockCipher>::decrypt_n_xex(unsigned char*, unsigned char const*, unsigned long) const
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
245
44
         {
246
44
         xor_buf(data, mask, blocks * BS);
247
44
         this->decrypt_n(data, data, blocks);
248
44
         xor_buf(data, mask, blocks * BS);
249
44
         }
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, 11ul, 16ul, 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, 16ul, 24ul, 8ul, 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
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
250
251
      Key_Length_Specification key_spec() const final override
252
2.97k
         {
253
2.97k
         return Key_Length_Specification(KMIN, KMAX, KMOD);
254
2.97k
         }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<64ul, 64ul, 0ul, 1ul, Botan::Tweakable_Block_Cipher>::key_spec() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 32ul, 0ul, 1ul, Botan::BlockCipher>::key_spec() const
Botan::Block_Cipher_Fixed_Params<16ul, 16ul, 0ul, 1ul, Botan::BlockCipher>::key_spec() const
Line
Count
Source
252
497
         {
253
497
         return Key_Length_Specification(KMIN, KMAX, KMOD);
254
497
         }
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
252
2.22k
         {
253
2.22k
         return Key_Length_Specification(KMIN, KMAX, KMOD);
254
2.22k
         }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 1ul, 56ul, 1ul, Botan::BlockCipher>::key_spec() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 11ul, 16ul, 1ul, Botan::BlockCipher>::key_spec() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 8ul, 0ul, 1ul, Botan::BlockCipher>::key_spec() const
Botan::Block_Cipher_Fixed_Params<8ul, 16ul, 24ul, 8ul, Botan::BlockCipher>::key_spec() const
Line
Count
Source
252
258
         {
253
258
         return Key_Length_Specification(KMIN, KMAX, KMOD);
254
258
         }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<8ul, 16ul, 0ul, 1ul, Botan::BlockCipher>::key_spec() const
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
255
   };
256
257
}
258
259
#endif