Botan 3.11.0
Crypto and TLS for C&
Botan::Cipher_Mode Class Referenceabstract

#include <cipher_mode.h>

Inheritance diagram for Botan::Cipher_Mode:
Botan::SymmetricAlgorithm Botan::AEAD_Mode Botan::CBC_Mode Botan::CFB_Mode Botan::XTS_Mode Botan::Ascon_AEAD128_Mode Botan::CCM_Mode Botan::ChaCha20Poly1305_Mode Botan::EAX_Mode Botan::GCM_Mode Botan::OCB_Mode Botan::SIV_Mode Botan::TLS::TLS_CBC_HMAC_AEAD_Mode Botan::TLS::TLS_NULL_HMAC_AEAD_Mode Botan::CBC_Decryption Botan::CBC_Encryption Botan::CFB_Decryption Botan::CFB_Encryption Botan::XTS_Decryption Botan::XTS_Encryption

Public Member Functions

bool authenticated () const
virtual void clear ()=0
virtual size_t default_nonce_length () const =0
void finish (secure_vector< uint8_t > &final_block, size_t offset=0)
template<concepts::resizable_byte_buffer T>
void finish (T &final_block, size_t offset=0)
virtual bool has_keying_material () const =0
virtual size_t ideal_granularity () const =0
virtual Key_Length_Specification key_spec () const =0
size_t maximum_keylength () const
virtual size_t minimum_final_size () const =0
size_t minimum_keylength () const
virtual std::string name () const =0
virtual size_t output_length (size_t input_length) const =0
size_t process (std::span< uint8_t > msg)
size_t process (uint8_t msg[], size_t msg_len)
virtual std::string provider () const
virtual bool requires_entire_message () const
virtual void reset ()=0
void set_key (const OctetString &key)
void set_key (const uint8_t key[], size_t length)
void set_key (std::span< const uint8_t > key)
void start ()
void start (const uint8_t nonce[], size_t nonce_len)
void start (std::span< const uint8_t > nonce)
virtual size_t tag_size () const
template<concepts::resizable_byte_buffer T>
void update (T &buffer, size_t offset=0)
virtual size_t update_granularity () const =0
bool valid_keylength (size_t length) const
virtual bool valid_nonce_length (size_t nonce_len) const =0

Static Public Member Functions

static std::unique_ptr< Cipher_Modecreate (std::string_view algo, Cipher_Dir direction, std::string_view provider="")
static std::unique_ptr< Cipher_Modecreate_or_throw (std::string_view algo, Cipher_Dir direction, std::string_view provider="")
static std::vector< std::string > providers (std::string_view algo_spec)

Protected Member Functions

void assert_key_material_set () const
void assert_key_material_set (bool predicate) const
virtual void finish_msg (secure_vector< uint8_t > &final_block, size_t offset=0)=0
virtual size_t process_msg (uint8_t msg[], size_t msg_len)=0
virtual void start_msg (const uint8_t nonce[], size_t nonce_len)=0

Detailed Description

Interface for cipher modes

Definition at line 36 of file cipher_mode.h.

Member Function Documentation

◆ assert_key_material_set() [1/2]

◆ assert_key_material_set() [2/2]

void Botan::SymmetricAlgorithm::assert_key_material_set ( bool predicate) const
inlineprotectedinherited

Definition at line 147 of file sym_algo.h.

147 {
148 if(!predicate) {
149 throw_key_not_set_error();
150 }
151 }

◆ authenticated()

bool Botan::Cipher_Mode::authenticated ( ) const
inline

Return the length in bytes of the authentication tag this algorithm generates. If the mode is not authenticated, this will return 0.

Returns
true iff this mode provides authentication as well as confidentiality.

Definition at line 262 of file cipher_mode.h.

262{ return this->tag_size() > 0; }
virtual size_t tag_size() const

References tag_size().

◆ clear()

◆ create()

std::unique_ptr< Cipher_Mode > Botan::Cipher_Mode::create ( std::string_view algo,
Cipher_Dir direction,
std::string_view provider = "" )
static

Create an AEAD mode

Parameters
algothe algorithm to create
directionspecify if this should be an encryption or decryption AEAD
provideroptional specification for provider to use
Returns
an AEAD mode or a null pointer if not available

Definition at line 54 of file cipher_mode.cpp.

56 {
57#if defined(BOTAN_HAS_COMMONCRYPTO)
58 if(provider.empty() || provider == "commoncrypto") {
59 if(auto cm = make_commoncrypto_cipher_mode(algo, direction))
60 return cm;
61
62 if(!provider.empty())
63 return nullptr;
64 }
65#endif
66
67 if(provider != "base" && !provider.empty()) {
68 return nullptr;
69 }
70
71#if defined(BOTAN_HAS_STREAM_CIPHER)
72 if(auto sc = StreamCipher::create(algo)) {
73 return std::make_unique<Stream_Cipher_Mode>(std::move(sc));
74 }
75#endif
76
77#if defined(BOTAN_HAS_AEAD_MODES)
78 if(auto aead = AEAD_Mode::create(algo, direction)) {
79 return aead;
80 }
81#endif
82
83 if(algo.find('/') != std::string::npos) {
84 const std::vector<std::string> algo_parts = split_on(algo, '/');
85 const std::string_view cipher_name = algo_parts[0];
86 const std::vector<std::string> mode_info = parse_algorithm_name(algo_parts[1]);
87
88 if(mode_info.empty()) {
89 return std::unique_ptr<Cipher_Mode>();
90 }
91
92 std::ostringstream mode_name;
93
94 mode_name << mode_info[0] << '(' << cipher_name;
95 for(size_t i = 1; i < mode_info.size(); ++i) {
96 mode_name << ',' << mode_info[i];
97 }
98 for(size_t i = 2; i < algo_parts.size(); ++i) {
99 mode_name << ',' << algo_parts[i];
100 }
101 mode_name << ')';
102
103 return Cipher_Mode::create(mode_name.str(), direction, provider);
104 }
105
106#if defined(BOTAN_HAS_BLOCK_CIPHER)
107
108 const SCAN_Name spec(algo);
109
110 if(spec.arg_count() == 0) {
111 return std::unique_ptr<Cipher_Mode>();
112 }
113
114 auto bc = BlockCipher::create(spec.arg(0), provider);
115
116 if(!bc) {
117 return std::unique_ptr<Cipher_Mode>();
118 }
119
120 #if defined(BOTAN_HAS_MODE_CBC)
121 if(spec.algo_name() == "CBC") {
122 const std::string padding = spec.arg(1, "PKCS7");
123
124 if(padding == "CTS") {
125 if(direction == Cipher_Dir::Encryption) {
126 return std::make_unique<CTS_Encryption>(std::move(bc));
127 } else {
128 return std::make_unique<CTS_Decryption>(std::move(bc));
129 }
130 } else {
131 auto pad = BlockCipherModePaddingMethod::create(padding);
132
133 if(pad) {
134 if(direction == Cipher_Dir::Encryption) {
135 return std::make_unique<CBC_Encryption>(std::move(bc), std::move(pad));
136 } else {
137 return std::make_unique<CBC_Decryption>(std::move(bc), std::move(pad));
138 }
139 }
140 }
141 }
142 #endif
143
144 #if defined(BOTAN_HAS_MODE_XTS)
145 if(spec.algo_name() == "XTS") {
146 if(direction == Cipher_Dir::Encryption) {
147 return std::make_unique<XTS_Encryption>(std::move(bc));
148 } else {
149 return std::make_unique<XTS_Decryption>(std::move(bc));
150 }
151 }
152 #endif
153
154 #if defined(BOTAN_HAS_MODE_CFB)
155 if(spec.algo_name() == "CFB") {
156 const size_t feedback_bits = spec.arg_as_integer(1, 8 * bc->block_size());
157 if(direction == Cipher_Dir::Encryption) {
158 return std::make_unique<CFB_Encryption>(std::move(bc), feedback_bits);
159 } else {
160 return std::make_unique<CFB_Decryption>(std::move(bc), feedback_bits);
161 }
162 }
163 #endif
164
165#endif
166
167 return std::unique_ptr<Cipher_Mode>();
168}
static std::unique_ptr< AEAD_Mode > create(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
Definition aead.cpp:59
static std::unique_ptr< BlockCipherModePaddingMethod > create(std::string_view algo_spec)
Definition mode_pad.cpp:19
static std::unique_ptr< BlockCipher > create(std::string_view algo_spec, std::string_view provider="")
static std::unique_ptr< Cipher_Mode > create(std::string_view algo, Cipher_Dir direction, std::string_view provider="")
virtual std::string provider() const
static std::unique_ptr< StreamCipher > create(std::string_view algo_spec, std::string_view provider="")
std::vector< std::string > split_on(std::string_view str, char delim)
Definition parsing.cpp:111
std::unique_ptr< Cipher_Mode > make_commoncrypto_cipher_mode(std::string_view name, Cipher_Dir direction)
std::vector< std::string > parse_algorithm_name(std::string_view scan_name)
Definition parsing.cpp:57

References Botan::SCAN_Name::algo_name(), Botan::SCAN_Name::arg(), Botan::SCAN_Name::arg_as_integer(), Botan::SCAN_Name::arg_count(), Botan::AEAD_Mode::create(), Botan::BlockCipher::create(), Botan::BlockCipherModePaddingMethod::create(), create(), Botan::StreamCipher::create(), Botan::Encryption, Botan::make_commoncrypto_cipher_mode(), Botan::parse_algorithm_name(), provider(), and Botan::split_on().

Referenced by botan_cipher_init(), create(), create_or_throw(), Botan::get_cipher_mode(), Botan::pbes2_decrypt(), and providers().

◆ create_or_throw()

std::unique_ptr< Cipher_Mode > Botan::Cipher_Mode::create_or_throw ( std::string_view algo,
Cipher_Dir direction,
std::string_view provider = "" )
static

Create an AEAD mode, or throw

Parameters
algothe algorithm to create
directionspecify if this should be an encryption or decryption AEAD
provideroptional specification for provider to use
Returns
an AEAD mode, or throw an exception

Definition at line 44 of file cipher_mode.cpp.

46 {
47 if(auto mode = Cipher_Mode::create(algo, direction, provider)) {
48 return mode;
49 }
50
51 throw Lookup_Error("Cipher mode", algo, provider);
52}

References create(), and provider().

Referenced by Botan::ECIES_System_Params::create_cipher(), Botan::CryptoBox::decrypt_bin(), Botan::CryptoBox::encrypt(), and Botan::get_cipher().

◆ default_nonce_length()

virtual size_t Botan::Cipher_Mode::default_nonce_length ( ) const
pure virtual

◆ finish() [1/2]

void Botan::Cipher_Mode::finish ( secure_vector< uint8_t > & final_block,
size_t offset = 0 )
inline

Complete procession of a message with a final input of buffer, which is treated the same as with update(). If you have the entire message in hand, calling finish() without ever calling update() is both efficient and convenient.

When using an AEAD_Mode, if the supplied authentication tag does not validate, this will throw an instance of Invalid_Authentication_Tag.

If this occurs, all plaintext previously output via calls to update must be destroyed and not used in any way that an attacker could observe the effects of. This could be anything from echoing the plaintext back (perhaps in an error message), or by making an external RPC whose destination or contents depend on the plaintext. The only thing you can do is buffer it, and in the event of an invalid tag, erase the previously decrypted content from memory.

One simple way to assure this could never happen is to never call update, and instead always marshal the entire message into a single buffer and call finish on it when decrypting.

Parameters
final_blockin/out parameter which must be at least minimum_final_size() bytes, and will be set to any final output
offsetan offset into final_block to begin processing

Definition at line 178 of file cipher_mode.h.

178{ finish_msg(final_block, offset); }
virtual void finish_msg(secure_vector< uint8_t > &final_block, size_t offset=0)=0

References finish_msg().

Referenced by botan_cipher_update(), and Botan::TLS::write_record().

◆ finish() [2/2]

template<concepts::resizable_byte_buffer T>
void Botan::Cipher_Mode::finish ( T & final_block,
size_t offset = 0 )
inline

Complete procession of a message.

Note: Using this overload with anything but a Botan::secure_vector<> is copying the bytes in the in/out buffer.

Parameters
final_blockin/out parameter which must be at least minimum_final_size() bytes, and will be set to any final output
offsetan offset into final_block to begin processing

Definition at line 191 of file cipher_mode.h.

191 {
192 Botan::secure_vector<uint8_t> tmp(final_block.begin(), final_block.end());
193 finish_msg(tmp, offset);
194 final_block.resize(tmp.size());
195 std::copy(tmp.begin(), tmp.end(), final_block.begin());
196 }
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68

References finish_msg().

◆ finish_msg()

virtual void Botan::Cipher_Mode::finish_msg ( secure_vector< uint8_t > & final_block,
size_t offset = 0 )
protectedpure virtual

Implemented in Botan::TLS::TLS_NULL_HMAC_AEAD_Decryption.

Referenced by finish(), and finish().

◆ has_keying_material()

◆ ideal_granularity()

virtual size_t Botan::Cipher_Mode::ideal_granularity ( ) const
pure virtual

Return an ideal granularity. This will be a multiple of the result of update_granularity but may be larger. If so it indicates that better performance may be achieved by providing buffers that are at least that size (due to SIMD execution, etc).

Implemented in Botan::Ascon_AEAD128_Mode, Botan::CBC_Mode, Botan::CCM_Mode, Botan::CFB_Mode, Botan::ChaCha20Poly1305_Mode, Botan::EAX_Mode, Botan::GCM_Mode, Botan::OCB_Mode, Botan::SIV_Mode, Botan::TLS::TLS_CBC_HMAC_AEAD_Mode, Botan::TLS::TLS_NULL_HMAC_AEAD_Mode, and Botan::XTS_Mode.

◆ key_spec()

virtual Key_Length_Specification Botan::SymmetricAlgorithm::key_spec ( ) const
pure virtualinherited

◆ maximum_keylength()

size_t Botan::SymmetricAlgorithm::maximum_keylength ( ) const
inlineinherited
Returns
maximum allowed key length

Definition at line 101 of file sym_algo.h.

101{ return key_spec().maximum_keylength(); }
size_t maximum_keylength() const
Definition sym_algo.h:55
virtual Key_Length_Specification key_spec() const =0

References key_spec().

◆ minimum_final_size()

◆ minimum_keylength()

size_t Botan::SymmetricAlgorithm::minimum_keylength ( ) const
inlineinherited
Returns
minimum allowed key length

Definition at line 106 of file sym_algo.h.

106{ return key_spec().minimum_keylength(); }
size_t minimum_keylength() const
Definition sym_algo.h:50

References key_spec().

◆ name()

◆ output_length()

◆ process() [1/2]

size_t Botan::Cipher_Mode::process ( std::span< uint8_t > msg)
inline

Process message blocks

Input must be a multiple of update_granularity

Processes msg in place and returns bytes written. Normally this will be either msg_len (indicating the entire message was processed) or for certain AEAD modes zero (indicating that the mode requires the entire message be processed in one pass).

Parameters
msgthe message to be processed
Returns
bytes written in-place

Definition at line 131 of file cipher_mode.h.

131{ return this->process_msg(msg.data(), msg.size()); }
virtual size_t process_msg(uint8_t msg[], size_t msg_len)=0

References process_msg().

Referenced by botan_cipher_update(), Botan::TLS::TLS_NULL_HMAC_AEAD_Decryption::finish_msg(), and update().

◆ process() [2/2]

size_t Botan::Cipher_Mode::process ( uint8_t msg[],
size_t msg_len )
inline

Definition at line 133 of file cipher_mode.h.

133{ return this->process_msg(msg, msg_len); }

References process_msg().

◆ process_msg()

virtual size_t Botan::Cipher_Mode::process_msg ( uint8_t msg[],
size_t msg_len )
protectedpure virtual

Referenced by process(), and process().

◆ provider()

virtual std::string Botan::Cipher_Mode::provider ( ) const
inlinevirtual
Returns
provider information about this implementation. Default is "base", might also return "sse2", "avx2", "openssl", or some other arbitrary string.

Reimplemented in Botan::GCM_Mode.

Definition at line 273 of file cipher_mode.h.

273{ return "base"; }

Referenced by Botan::AEAD_Mode::create(), create(), Botan::AEAD_Mode::create_or_throw(), and create_or_throw().

◆ providers()

std::vector< std::string > Botan::Cipher_Mode::providers ( std::string_view algo_spec)
static
Returns
list of available providers for this algorithm, empty if not available
Parameters
algo_specalgorithm name

Definition at line 171 of file cipher_mode.cpp.

171 {
172 const std::vector<std::string>& possible = {"base", "commoncrypto"};
173 std::vector<std::string> providers;
174 for(auto&& prov : possible) {
175 auto mode = Cipher_Mode::create(algo_spec, Cipher_Dir::Encryption, prov);
176 if(mode) {
177 providers.push_back(prov); // available
178 }
179 }
180 return providers;
181}
static std::vector< std::string > providers(std::string_view algo_spec)

References create(), Botan::Encryption, and providers().

Referenced by providers().

◆ requires_entire_message()

virtual bool Botan::Cipher_Mode::requires_entire_message ( ) const
inlinevirtual

Certain modes require the entire message be available before any processing can occur. For such modes, input will be consumed but not returned, until finish is called, which returns the entire message.

This function returns true if this mode has this style of operation.

Reimplemented in Botan::CCM_Mode, and Botan::SIV_Mode.

Definition at line 232 of file cipher_mode.h.

232{ return false; }

Referenced by botan_cipher_update().

◆ reset()

virtual void Botan::Cipher_Mode::reset ( )
pure virtual

◆ set_key() [1/3]

◆ set_key() [2/3]

void Botan::SymmetricAlgorithm::set_key ( const uint8_t key[],
size_t length )
inlineinherited

Set the symmetric key of this object.

Parameters
keythe to be set as a byte array.
lengthin bytes of key param

Definition at line 132 of file sym_algo.h.

132{ set_key(std::span{key, length}); }

References set_key().

Referenced by set_key().

◆ set_key() [3/3]

void Botan::SymmetricAlgorithm::set_key ( std::span< const uint8_t > key)
inherited

Set the symmetric key of this object.

Parameters
keythe contiguous byte range to be set.

Definition at line 22 of file sym_algo.cpp.

22 {
23 if(!valid_keylength(key.size())) {
24 throw Invalid_Key_Length(name(), key.size());
25 }
26 key_schedule(key);
27}
bool valid_keylength(size_t length) const
Definition sym_algo.h:113
virtual std::string name() const =0

References name(), and valid_keylength().

◆ start() [1/3]

void Botan::Cipher_Mode::start ( )
inline

Begin processing a message.

The exact semantics of this depend on the mode. For many modes, the call will fail since a nonce must be provided.

For certain modes such as CBC this will instead cause the last ciphertext block to be used as the nonce of the new message; doing this isn't a good idea, but some (mostly older) protocols do this.

Definition at line 116 of file cipher_mode.h.

116{ return start_msg(nullptr, 0); }
virtual void start_msg(const uint8_t nonce[], size_t nonce_len)=0

References start_msg().

◆ start() [2/3]

void Botan::Cipher_Mode::start ( const uint8_t nonce[],
size_t nonce_len )
inline

Begin processing a message with a fresh nonce.

Parameters
noncethe per message nonce
nonce_lenlength of nonce

Definition at line 104 of file cipher_mode.h.

104{ start_msg(nonce, nonce_len); }

References start_msg().

◆ start() [3/3]

void Botan::Cipher_Mode::start ( std::span< const uint8_t > nonce)
inline

Begin processing a message with a fresh nonce.

Warning
Typically one must not reuse the same nonce for more than one message under the same key. For most cipher modes this would mean total loss of security and/or authenticity guarantees.
Note
If reliably generating unique nonces is difficult in your environment, use SIV which retains security even if nonces are repeated.
Parameters
noncethe per message nonce

Definition at line 97 of file cipher_mode.h.

97{ start_msg(nonce.data(), nonce.size()); }

References start_msg().

Referenced by botan_cipher_start(), and Botan::TLS::write_record().

◆ start_msg()

virtual void Botan::Cipher_Mode::start_msg ( const uint8_t nonce[],
size_t nonce_len )
protectedpure virtual

Implemented in Botan::Ascon_AEAD128_Mode.

Referenced by start(), start(), and start().

◆ tag_size()

virtual size_t Botan::Cipher_Mode::tag_size ( ) const
inlinevirtual
Returns
the size of the authentication tag used (in bytes)

Reimplemented in Botan::Ascon_AEAD128_Mode, Botan::CCM_Mode, Botan::ChaCha20Poly1305_Mode, Botan::EAX_Mode, Botan::GCM_Mode, Botan::OCB_Mode, Botan::SIV_Mode, Botan::TLS::TLS_CBC_HMAC_AEAD_Mode, and Botan::TLS::TLS_NULL_HMAC_AEAD_Mode.

Definition at line 267 of file cipher_mode.h.

267{ return 0; }

Referenced by authenticated().

◆ update()

template<concepts::resizable_byte_buffer T>
void Botan::Cipher_Mode::update ( T & buffer,
size_t offset = 0 )
inline

Process some data. Input must be in size update_granularity() uint8_t blocks. The buffer is an in/out parameter and may be resized. In particular, some modes require that all input be consumed before any output is produced; with these modes, buffer will be returned empty.

The first offset bytes of buffer will be ignored (this allows in place processing of a buffer that contains an initial plaintext header).

Parameters
bufferin/out parameter which will possibly be resized
offsetan offset into blocks to begin processing

Definition at line 148 of file cipher_mode.h.

148 {
149 const size_t written = process(std::span(buffer).subspan(offset));
150 buffer.resize(offset + written);
151 }
size_t process(std::span< uint8_t > msg)

References process().

◆ update_granularity()

virtual size_t Botan::Cipher_Mode::update_granularity ( ) const
pure virtual

The :cpp:class:Cipher_Mode interface requires message processing in multiples of the block size. This returns size of required blocks to update. If the mode implementation does not require buffering it will return 1.

Returns
size of required blocks to update

Implemented in Botan::Ascon_AEAD128_Mode, Botan::CBC_Mode, Botan::CCM_Mode, Botan::CFB_Mode, Botan::ChaCha20Poly1305_Mode, Botan::EAX_Mode, Botan::GCM_Mode, Botan::OCB_Mode, Botan::SIV_Mode, Botan::TLS::TLS_CBC_HMAC_AEAD_Mode, Botan::TLS::TLS_NULL_HMAC_AEAD_Mode, and Botan::XTS_Mode.

◆ valid_keylength()

bool Botan::SymmetricAlgorithm::valid_keylength ( size_t length) const
inlineinherited

Check whether a given key length is valid for this algorithm.

Parameters
lengththe key length to be checked.
Returns
true if the key length is valid.

Definition at line 113 of file sym_algo.h.

113{ return key_spec().valid_keylength(length); }
bool valid_keylength(size_t length) const
Definition sym_algo.h:43

References key_spec().

Referenced by set_key().

◆ valid_nonce_length()

virtual bool Botan::Cipher_Mode::valid_nonce_length ( size_t nonce_len) const
pure virtual

The documentation for this class was generated from the following files: