Botan 3.11.0
Crypto and TLS for C&
xts.h
Go to the documentation of this file.
1/*
2* XTS mode, from IEEE P1619
3* (C) 2009,2013 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#ifndef BOTAN_MODE_XTS_H_
10#define BOTAN_MODE_XTS_H_
11
12#include <botan/block_cipher.h>
13#include <botan/cipher_mode.h>
14
15namespace Botan {
16
17/**
18* IEEE P1619 XTS Mode
19*/
20class XTS_Mode : public Cipher_Mode {
21 public:
22 std::string name() const final;
23
24 size_t update_granularity() const final;
25
26 size_t ideal_granularity() const final;
27
28 size_t minimum_final_size() const final;
29
31
32 size_t default_nonce_length() const final;
33
34 bool valid_nonce_length(size_t n) const final;
35
36 void clear() final;
37
38 void reset() final;
39
40 bool has_keying_material() const final;
41
42 protected:
43 explicit XTS_Mode(std::unique_ptr<BlockCipher> cipher);
44
45 const uint8_t* tweak() const { return m_tweak.data(); }
46
47 bool tweak_set() const { return !m_tweak.empty(); }
48
49 size_t tweak_blocks() const { return m_tweak_blocks; }
50
51 const BlockCipher& cipher() const { return *m_cipher; }
52
53 void update_tweak(size_t consumed);
54
55 size_t cipher_block_size() const { return m_cipher_block_size; }
56
57 private:
58 void start_msg(const uint8_t nonce[], size_t nonce_len) override;
59 void key_schedule(std::span<const uint8_t> key) override;
60
61 /*
62 * Tweak block update step for XTS
63 *
64 * Assumes tweak is BS * n bytes long.
65 *
66 * Assumes that each block of tweak is already set to the successive doublings
67 * of the block prior.
68 */
69 static void update_tweak_block(uint8_t tweak[], size_t BS, size_t blocks_in_tweak);
70
71#if defined(BOTAN_HAS_MODE_XTS_AVX512_CLMUL)
72 static void update_tweak_block_avx512_clmul(uint8_t tweak[], size_t BS, size_t blocks_in_tweak);
73#endif
74
75 std::unique_ptr<BlockCipher> m_cipher;
76 std::unique_ptr<BlockCipher> m_tweak_cipher;
78 const size_t m_cipher_block_size;
79 const size_t m_cipher_parallelism;
80 const size_t m_tweak_blocks;
81};
82
83/**
84* IEEE P1619 XTS Encryption
85*/
86class XTS_Encryption final : public XTS_Mode {
87 public:
88 /**
89 * @param cipher underlying block cipher
90 */
91 explicit XTS_Encryption(std::unique_ptr<BlockCipher> cipher) : XTS_Mode(std::move(cipher)) {}
92
93 size_t output_length(size_t input_length) const override;
94
95 private:
96 size_t process_msg(uint8_t buf[], size_t size) override;
97 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
98};
99
100/**
101* IEEE P1619 XTS Decryption
102*/
103class XTS_Decryption final : public XTS_Mode {
104 public:
105 /**
106 * @param cipher underlying block cipher
107 */
108 explicit XTS_Decryption(std::unique_ptr<BlockCipher> cipher) : XTS_Mode(std::move(cipher)) {}
109
110 size_t output_length(size_t input_length) const override;
111
112 private:
113 size_t process_msg(uint8_t buf[], size_t size) override;
114 void finish_msg(secure_vector<uint8_t>& final_block, size_t offset = 0) override;
115};
116
117} // namespace Botan
118
119#endif
XTS_Decryption(std::unique_ptr< BlockCipher > cipher)
Definition xts.h:108
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
XTS_Encryption(std::unique_ptr< BlockCipher > cipher)
Definition xts.h:91
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
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68