Coverage Report

Created: 2023-06-07 07:00

/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 <memory>
13
#include <string>
14
#include <string_view>
15
#include <vector>
16
17
namespace Botan {
18
19
/**
20
* This class represents a block cipher object.
21
*/
22
class BOTAN_PUBLIC_API(2, 0) BlockCipher : public SymmetricAlgorithm {
23
   public:
24
      /**
25
      * Create an instance based on a name
26
      * If provider is empty then best available is chosen.
27
      * @param algo_spec algorithm name
28
      * @param provider provider implementation to choose
29
      * @return a null pointer if the algo/provider combination cannot be found
30
      */
31
      static std::unique_ptr<BlockCipher> create(std::string_view algo_spec, std::string_view provider = "");
32
33
      /**
34
      * Create an instance based on a name, or throw if the
35
      * algo/provider combination cannot be found. If provider is
36
      * empty then best available is chosen.
37
      */
38
      static std::unique_ptr<BlockCipher> create_or_throw(std::string_view algo_spec, std::string_view provider = "");
39
40
      /**
41
      * @return list of available providers for this algorithm, empty if not available
42
      * @param algo_spec algorithm name
43
      */
44
      static std::vector<std::string> providers(std::string_view algo_spec);
45
46
      /**
47
      * @return block size of this algorithm
48
      */
49
      virtual size_t block_size() const = 0;
50
51
      /**
52
      * @return native parallelism of this cipher in blocks
53
      */
54
0
      virtual size_t parallelism() const { return 1; }
55
56
      /**
57
      * @return prefererred parallelism of this cipher in bytes
58
      */
59
0
      size_t parallel_bytes() const { return parallelism() * block_size() * BOTAN_BLOCK_CIPHER_PAR_MULT; }
60
61
      /**
62
      * @return provider information about this implementation. Default is "base",
63
      * might also return "sse2", "avx2", "openssl", or some other arbitrary string.
64
      */
65
0
      virtual std::string provider() const { return "base"; }
66
67
      /**
68
      * Encrypt a block.
69
      * @param in The plaintext block to be encrypted as a byte array.
70
      * Must be of length block_size().
71
      * @param out The byte array designated to hold the encrypted block.
72
      * Must be of length block_size().
73
      */
74
0
      void encrypt(const uint8_t in[], uint8_t out[]) const { encrypt_n(in, out, 1); }
75
76
      /**
77
      * Decrypt a block.
78
      * @param in The ciphertext block to be decypted as a byte array.
79
      * Must be of length block_size().
80
      * @param out The byte array designated to hold the decrypted block.
81
      * Must be of length block_size().
82
      */
83
0
      void decrypt(const uint8_t in[], uint8_t out[]) const { decrypt_n(in, out, 1); }
84
85
      /**
86
      * Encrypt a block.
87
      * @param block the plaintext block to be encrypted
88
      * Must be of length block_size(). Will hold the result when the function
89
      * has finished.
90
      */
91
0
      void encrypt(uint8_t block[]) const { encrypt_n(block, block, 1); }
92
93
      /**
94
      * Decrypt a block.
95
      * @param block the ciphertext block to be decrypted
96
      * Must be of length block_size(). Will hold the result when the function
97
      * has finished.
98
      */
99
0
      void decrypt(uint8_t block[]) const { decrypt_n(block, block, 1); }
100
101
      /**
102
      * Encrypt one or more blocks
103
      * @param block the input/output buffer (multiple of block_size())
104
      */
105
0
      void encrypt(std::span<uint8_t> block) const {
106
0
         return encrypt_n(block.data(), block.data(), block.size() / block_size());
107
0
      }
108
109
      /**
110
      * Decrypt one or more blocks
111
      * @param block the input/output buffer (multiple of block_size())
112
      */
113
0
      void decrypt(std::span<uint8_t> block) const {
114
0
         return decrypt_n(block.data(), block.data(), block.size() / block_size());
115
0
      }
116
117
      /**
118
      * Encrypt one or more blocks
119
      * @param in the input buffer (multiple of block_size())
120
      * @param out the output buffer (same size as in)
121
      */
122
0
      void encrypt(std::span<const uint8_t> in, std::span<uint8_t> out) const {
123
0
         return encrypt_n(in.data(), out.data(), in.size() / block_size());
124
0
      }
125
126
      /**
127
      * Decrypt one or more blocks
128
      * @param in the input buffer (multiple of block_size())
129
      * @param out the output buffer (same size as in)
130
      */
131
0
      void decrypt(std::span<const uint8_t> in, std::span<uint8_t> out) const {
132
0
         return decrypt_n(in.data(), out.data(), in.size() / block_size());
133
0
      }
134
135
      /**
136
      * Encrypt one or more blocks
137
      * @param in the input buffer (multiple of block_size())
138
      * @param out the output buffer (same size as in)
139
      * @param blocks the number of blocks to process
140
      */
141
      virtual void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const = 0;
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
      * @param blocks the number of blocks to process
148
      */
149
      virtual void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const = 0;
150
151
0
      virtual void encrypt_n_xex(uint8_t data[], const uint8_t mask[], size_t blocks) const {
152
0
         const size_t BS = block_size();
153
0
         xor_buf(data, mask, blocks * BS);
154
0
         encrypt_n(data, data, blocks);
155
0
         xor_buf(data, mask, blocks * BS);
156
0
      }
157
158
0
      virtual void decrypt_n_xex(uint8_t data[], const uint8_t mask[], size_t blocks) const {
159
0
         const size_t BS = block_size();
160
0
         xor_buf(data, mask, blocks * BS);
161
0
         decrypt_n(data, data, blocks);
162
0
         xor_buf(data, mask, blocks * BS);
163
0
      }
164
165
      /**
166
      * @return new object representing the same algorithm as *this
167
      */
168
      virtual std::unique_ptr<BlockCipher> new_object() const = 0;
169
170
0
      BlockCipher* clone() const { return this->new_object().release(); }
171
172
      virtual ~BlockCipher() = default;
173
};
174
175
/**
176
* Tweakable block ciphers allow setting a tweak which is a non-keyed
177
* value which affects the encryption/decryption operation.
178
*/
179
class BOTAN_PUBLIC_API(2, 8) Tweakable_Block_Cipher : public BlockCipher {
180
   public:
181
      /**
182
      * Set the tweak value. This must be called after setting a key. The value
183
      * persists until either set_tweak, set_key, or clear is called.
184
      * Different algorithms support different tweak length(s). If called with
185
      * an unsupported length, Invalid_Argument will be thrown.
186
      */
187
      virtual void set_tweak(const uint8_t tweak[], size_t len) = 0;
188
};
189
190
/**
191
* Represents a block cipher with a single fixed block size
192
*/
193
template <size_t BS, size_t KMIN, size_t KMAX = 0, size_t KMOD = 1, typename BaseClass = BlockCipher>
194
class Block_Cipher_Fixed_Params : public BaseClass {
195
   public:
196
      enum { BLOCK_SIZE = BS };
197
198
0
      size_t block_size() const final override { return BS; }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 16ul, 0ul, 1ul, Botan::BlockCipher>::block_size() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 24ul, 0ul, 1ul, Botan::BlockCipher>::block_size() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 32ul, 0ul, 1ul, 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, 11ul, 16ul, 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, 16ul, 24ul, 8ul, Botan::BlockCipher>::block_size() const
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
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
199
200
      // override to take advantage of compile time constant block size
201
0
      void encrypt_n_xex(uint8_t data[], const uint8_t mask[], size_t blocks) const final override {
202
0
         xor_buf(data, mask, blocks * BS);
203
0
         this->encrypt_n(data, data, blocks);
204
0
         xor_buf(data, mask, blocks * BS);
205
0
      }
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
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 32ul, 0ul, 1ul, 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, 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
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
206
207
0
      void decrypt_n_xex(uint8_t data[], const uint8_t mask[], size_t blocks) const final override {
208
0
         xor_buf(data, mask, blocks * BS);
209
0
         this->decrypt_n(data, data, blocks);
210
0
         xor_buf(data, mask, blocks * BS);
211
0
      }
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
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 32ul, 0ul, 1ul, 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, 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
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
212
213
0
      Key_Length_Specification key_spec() const final override { return Key_Length_Specification(KMIN, KMAX, KMOD); }
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 16ul, 0ul, 1ul, Botan::BlockCipher>::key_spec() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 24ul, 0ul, 1ul, Botan::BlockCipher>::key_spec() const
Unexecuted instantiation: Botan::Block_Cipher_Fixed_Params<16ul, 32ul, 0ul, 1ul, 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, 11ul, 16ul, 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, 16ul, 24ul, 8ul, Botan::BlockCipher>::key_spec() const
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
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
214
};
215
216
}  // namespace Botan
217
218
#endif