Botan 3.11.0
Crypto and TLS for C&
xts.cpp
Go to the documentation of this file.
1/*
2* XTS Mode
3* (C) 2009,2013,2026 Jack Lloyd
4* (C) 2016 Daniel Neus, Rohde & Schwarz Cybersecurity
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/internal/xts.h>
10
11#include <botan/exceptn.h>
12#include <botan/mem_ops.h>
13#include <botan/internal/fmt.h>
14#include <botan/internal/poly_dbl.h>
15
16#if defined(BOTAN_HAS_MODE_XTS_AVX512_CLMUL)
17 #include <botan/internal/cpuid.h>
18#endif
19
20namespace Botan {
21
22XTS_Mode::XTS_Mode(std::unique_ptr<BlockCipher> cipher) :
23 m_cipher(std::move(cipher)),
24 m_cipher_block_size(m_cipher->block_size()),
25 m_cipher_parallelism(m_cipher->parallel_bytes()),
26 m_tweak_blocks(m_cipher_parallelism / m_cipher_block_size) {
27 if(!poly_double_supported_size(m_cipher_block_size)) {
28 throw Invalid_Argument(fmt("Cannot use {} with XTS", m_cipher->name()));
29 }
30
31 m_tweak_cipher = m_cipher->new_object();
32}
33
35 m_cipher->clear();
36 m_tweak_cipher->clear();
37 reset();
38}
39
41 return m_cipher_block_size;
42}
43
45 return m_cipher_parallelism;
46}
47
49 m_tweak.clear();
50}
51
52std::string XTS_Mode::name() const {
53 return cipher().name() + "/XTS";
54}
55
57 return cipher_block_size();
58}
59
63
65 return cipher_block_size();
66}
67
68bool XTS_Mode::valid_nonce_length(size_t n) const {
69 return n <= cipher_block_size();
70}
71
73 return m_cipher->has_keying_material() && m_tweak_cipher->has_keying_material();
74}
75
76void XTS_Mode::key_schedule(std::span<const uint8_t> key) {
77 const size_t key_half = key.size() / 2;
78
79 if(key.size() % 2 == 1 || !m_cipher->valid_keylength(key_half)) {
80 throw Invalid_Key_Length(name(), key.size());
81 }
82
83 m_cipher->set_key(key.first(key_half));
84 m_tweak_cipher->set_key(key.last(key_half));
85}
86
87void XTS_Mode::start_msg(const uint8_t nonce[], size_t nonce_len) {
88 if(!valid_nonce_length(nonce_len)) {
89 throw Invalid_IV_Length(name(), nonce_len);
90 }
91
92 m_tweak.resize(m_cipher_parallelism);
93 clear_mem(m_tweak.data(), m_tweak.size());
94 copy_mem(m_tweak.data(), nonce, nonce_len);
95 m_tweak_cipher->encrypt(m_tweak.data());
96
97 // Just repeated doubling from first, remaining contents are junk...
98 xts_compute_tweak_block(m_tweak.data(), m_tweak_cipher->block_size(), tweak_blocks());
99}
100
101//static
102void XTS_Mode::update_tweak_block(uint8_t tweak[], size_t BS, size_t blocks_in_tweak) {
103#if defined(BOTAN_HAS_MODE_XTS_AVX512_CLMUL)
104 if(BS == 16 && blocks_in_tweak % 8 == 0 && CPUID::has(CPUID::Feature::AVX512_CLMUL)) {
105 return update_tweak_block_avx512_clmul(tweak, BS, blocks_in_tweak);
106 }
107#endif
108
109 /*
110 * If we don't have a fast method available, just set the first tweak block to
111 * the doubling of the last tweak block, and recompute all the rest via
112 * successive doublings.
113 */
114 poly_double_n_le(tweak, &tweak[(blocks_in_tweak - 1) * BS], BS);
115 xts_compute_tweak_block(tweak, BS, blocks_in_tweak);
116}
117
118void XTS_Mode::update_tweak(size_t consumed) {
119 const size_t BS = m_tweak_cipher->block_size();
120 const size_t blocks_in_tweak = tweak_blocks();
121
122 BOTAN_ASSERT_NOMSG(consumed > 0 && consumed <= blocks_in_tweak);
123
124 if(consumed == blocks_in_tweak) {
125 // Update all in parallel
126 update_tweak_block(m_tweak.data(), BS, blocks_in_tweak);
127 } else {
128 /*
129 The last remaining tweaks can just be shifted over
130
131 This could be a lot better though! We can copy all of the remaining tweaks
132 and just recompute the last few
133 */
134 copy_mem(m_tweak.data(), &m_tweak[(consumed * BS)], BS);
135 xts_compute_tweak_block(m_tweak.data(), BS, blocks_in_tweak);
136 }
137}
138
139size_t XTS_Encryption::output_length(size_t input_length) const {
140 return input_length;
141}
142
143size_t XTS_Encryption::process_msg(uint8_t buf[], size_t sz) {
145 const size_t BS = cipher_block_size();
146
147 BOTAN_ARG_CHECK(sz % BS == 0, "Input is not full blocks");
148 size_t blocks = sz / BS;
149
150 const size_t blocks_in_tweak = tweak_blocks();
151
152 while(blocks > 0) {
153 const size_t to_proc = std::min(blocks, blocks_in_tweak);
154 const size_t proc_bytes = to_proc * BS;
155
156 xor_buf(buf, tweak(), proc_bytes);
157 cipher().encrypt_n(buf, buf, to_proc);
158 xor_buf(buf, tweak(), proc_bytes);
159
160 buf += proc_bytes;
161 blocks -= to_proc;
162
163 update_tweak(to_proc);
164 }
165
166 return sz;
167}
168
169void XTS_Encryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
170 BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
171 const size_t sz = buffer.size() - offset;
172 uint8_t* buf = buffer.data() + offset;
173
174 BOTAN_ARG_CHECK(sz >= minimum_final_size(), "missing sufficient final input in XTS encrypt");
175
176 const size_t BS = cipher_block_size();
177
178 if(sz % BS == 0) {
179 update(buffer, offset);
180 } else {
181 // steal ciphertext
182 const size_t full_blocks = ((sz / BS) - 1) * BS;
183 const size_t final_bytes = sz - full_blocks;
184 BOTAN_ASSERT(final_bytes > BS && final_bytes < 2 * BS, "Left over size in expected range");
185
186 secure_vector<uint8_t> last(buf + full_blocks, buf + full_blocks + final_bytes);
187 buffer.resize(full_blocks + offset);
188 update(buffer, offset);
189
190 xor_buf(last, tweak(), BS);
191 cipher().encrypt(last);
192 xor_buf(last, tweak(), BS);
193
194 for(size_t i = 0; i != final_bytes - BS; ++i) {
195 last[i] ^= last[i + BS];
196 last[i + BS] ^= last[i];
197 last[i] ^= last[i + BS];
198 }
199
200 xor_buf(last, tweak() + BS, BS);
201 cipher().encrypt(last);
202 xor_buf(last, tweak() + BS, BS);
203
204 buffer += last;
205 }
206}
207
208size_t XTS_Decryption::output_length(size_t input_length) const {
209 return input_length;
210}
211
212size_t XTS_Decryption::process_msg(uint8_t buf[], size_t sz) {
214 const size_t BS = cipher_block_size();
215
216 BOTAN_ARG_CHECK(sz % BS == 0, "Input is not full blocks");
217 size_t blocks = sz / BS;
218
219 const size_t blocks_in_tweak = tweak_blocks();
220
221 while(blocks > 0) {
222 const size_t to_proc = std::min(blocks, blocks_in_tweak);
223 const size_t proc_bytes = to_proc * BS;
224
225 xor_buf(buf, tweak(), proc_bytes);
226 cipher().decrypt_n(buf, buf, to_proc);
227 xor_buf(buf, tweak(), proc_bytes);
228
229 buf += proc_bytes;
230 blocks -= to_proc;
231
232 update_tweak(to_proc);
233 }
234
235 return sz;
236}
237
238void XTS_Decryption::finish_msg(secure_vector<uint8_t>& buffer, size_t offset) {
239 BOTAN_ARG_CHECK(buffer.size() >= offset, "Offset is out of range");
240 const size_t sz = buffer.size() - offset;
241 uint8_t* buf = buffer.data() + offset;
242
243 BOTAN_ARG_CHECK(sz >= minimum_final_size(), "missing sufficient final input in XTS decrypt");
244
245 const size_t BS = cipher_block_size();
246
247 if(sz % BS == 0) {
248 update(buffer, offset);
249 } else {
250 // steal ciphertext
251 const size_t full_blocks = ((sz / BS) - 1) * BS;
252 const size_t final_bytes = sz - full_blocks;
253 BOTAN_ASSERT(final_bytes > BS && final_bytes < 2 * BS, "Left over size in expected range");
254
255 secure_vector<uint8_t> last(buf + full_blocks, buf + full_blocks + final_bytes);
256 buffer.resize(full_blocks + offset);
257 update(buffer, offset);
258
259 xor_buf(last, tweak() + BS, BS);
260 cipher().decrypt(last);
261 xor_buf(last, tweak() + BS, BS);
262
263 for(size_t i = 0; i != final_bytes - BS; ++i) {
264 last[i] ^= last[i + BS];
265 last[i + BS] ^= last[i];
266 last[i] ^= last[i + BS];
267 }
268
269 xor_buf(last, tweak(), BS);
270 cipher().decrypt(last);
271 xor_buf(last, tweak(), BS);
272
273 buffer += last;
274 }
275}
276
277} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:62
void encrypt(const uint8_t in[], uint8_t out[]) const
void decrypt(const uint8_t in[], uint8_t out[]) const
virtual void decrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const =0
virtual void encrypt_n(const uint8_t in[], uint8_t out[], size_t blocks) const =0
static bool has(CPUID::Feature feat)
Definition cpuid.h:94
void update(T &buffer, size_t offset=0)
Key_Length_Specification multiple(size_t n) const
Definition sym_algo.h:67
virtual std::string name() const =0
virtual Key_Length_Specification key_spec() const =0
size_t output_length(size_t input_length) const override
Definition xts.cpp:208
size_t output_length(size_t input_length) const override
Definition xts.cpp:139
void reset() final
Definition xts.cpp:48
const uint8_t * tweak() const
Definition xts.h:45
size_t ideal_granularity() const final
Definition xts.cpp:44
std::string name() const final
Definition xts.cpp:52
void update_tweak(size_t consumed)
Definition xts.cpp:118
size_t default_nonce_length() const final
Definition xts.cpp:64
size_t cipher_block_size() const
Definition xts.h:55
size_t update_granularity() const final
Definition xts.cpp:40
bool has_keying_material() const final
Definition xts.cpp:72
void clear() final
Definition xts.cpp:34
XTS_Mode(std::unique_ptr< BlockCipher > cipher)
Definition xts.cpp:22
const BlockCipher & cipher() const
Definition xts.h:51
size_t tweak_blocks() const
Definition xts.h:49
Key_Length_Specification key_spec() const final
Definition xts.cpp:60
bool tweak_set() const
Definition xts.h:47
bool valid_nonce_length(size_t n) const final
Definition xts.cpp:68
size_t minimum_final_size() const final
Definition xts.cpp:56
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:144
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
void poly_double_n_le(uint8_t out[], const uint8_t in[], size_t n)
Definition poly_dbl.cpp:100
constexpr void xor_buf(ranges::contiguous_output_range< uint8_t > auto &&out, ranges::contiguous_range< uint8_t > auto &&in)
Definition mem_ops.h:341
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68
void xts_compute_tweak_block(uint8_t tweak[], size_t BS, size_t blocks_in_tweak)
Definition poly_dbl.cpp:119
bool poly_double_supported_size(size_t n)
Definition poly_dbl.h:22
constexpr void clear_mem(T *ptr, size_t n)
Definition mem_ops.h:118