Coverage Report

Created: 2025-07-11 06:15

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