Botan 3.6.1
Crypto and TLS for C&
Botan::DLIES_Decryptor Class Referencefinal

#include <dlies.h>

Inheritance diagram for Botan::DLIES_Decryptor:
Botan::PK_Decryptor

Public Member Functions

secure_vector< uint8_t > decrypt (const uint8_t in[], size_t length) const
 
secure_vector< uint8_t > decrypt (std::span< const uint8_t > in) const
 
secure_vector< uint8_t > decrypt_or_random (const uint8_t in[], size_t length, size_t expected_pt_len, RandomNumberGenerator &rng) const
 
secure_vector< uint8_t > decrypt_or_random (const uint8_t in[], size_t length, size_t expected_pt_len, RandomNumberGenerator &rng, const uint8_t required_content_bytes[], const uint8_t required_content_offsets[], size_t required_contents) const
 
 DLIES_Decryptor (const DH_PrivateKey &own_priv_key, RandomNumberGenerator &rng, std::unique_ptr< KDF > kdf, std::unique_ptr< Cipher_Mode > cipher, size_t cipher_key_len, std::unique_ptr< MessageAuthenticationCode > mac, size_t mac_key_len=20)
 
 DLIES_Decryptor (const DH_PrivateKey &own_priv_key, RandomNumberGenerator &rng, std::unique_ptr< KDF > kdf, std::unique_ptr< MessageAuthenticationCode > mac, size_t mac_key_len=20)
 
void set_initialization_vector (const InitializationVector &iv)
 Set the initialization vector for the data decryption method.
 

Detailed Description

DLIES Decryption

Definition at line 92 of file dlies.h.

Constructor & Destructor Documentation

◆ DLIES_Decryptor() [1/2]

Botan::DLIES_Decryptor::DLIES_Decryptor ( const DH_PrivateKey & own_priv_key,
RandomNumberGenerator & rng,
std::unique_ptr< KDF > kdf,
std::unique_ptr< MessageAuthenticationCode > mac,
size_t mac_key_len = 20 )

Stream mode: use KDF to provide a stream of bytes to xor with the message

Parameters
own_priv_keyown (ephemeral) DH private key
rngthe RNG to use
kdfthe KDF that should be used
macthe MAC function that should be used
mac_key_lenkey length of the MAC function. Default = 20 bytes

input = (ephemeral) public key + ciphertext + tag

Definition at line 115 of file dlies.cpp.

119 :
120 DLIES_Decryptor(own_priv_key, rng, std::move(kdf), nullptr, 0, std::move(mac), mac_key_length) {}
DLIES_Decryptor(const DH_PrivateKey &own_priv_key, RandomNumberGenerator &rng, std::unique_ptr< KDF > kdf, std::unique_ptr< MessageAuthenticationCode > mac, size_t mac_key_len=20)
Definition dlies.cpp:115

◆ DLIES_Decryptor() [2/2]

Botan::DLIES_Decryptor::DLIES_Decryptor ( const DH_PrivateKey & own_priv_key,
RandomNumberGenerator & rng,
std::unique_ptr< KDF > kdf,
std::unique_ptr< Cipher_Mode > cipher,
size_t cipher_key_len,
std::unique_ptr< MessageAuthenticationCode > mac,
size_t mac_key_len = 20 )

Block cipher mode

Parameters
own_priv_keyown (ephemeral) DH private key
rngthe RNG to use
kdfthe KDF that should be used
cipherthe block cipher that should be used
cipher_key_lenthe key length of the block cipher
macthe MAC function that should be used
mac_key_lenkey length of the MAC function. Default = 20 bytes

input = (ephemeral) public key + ciphertext + tag

Definition at line 96 of file dlies.cpp.

102 :
103 m_pub_key_size(own_priv_key.public_value().size()),
104 m_ka(own_priv_key, rng, "Raw"),
105 m_kdf(std::move(kdf)),
106 m_cipher(std::move(cipher)),
107 m_cipher_key_len(cipher_key_len),
108 m_mac(std::move(mac)),
109 m_mac_keylen(mac_key_length),
110 m_iv() {
113}
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:86

References BOTAN_ASSERT_NONNULL.

Member Function Documentation

◆ decrypt() [1/2]

secure_vector< uint8_t > Botan::PK_Decryptor::decrypt ( const uint8_t in[],
size_t length ) const
inherited

Decrypt a ciphertext, throwing an exception if the input seems to be invalid (eg due to an accidental or malicious error in the ciphertext).

Parameters
inthe ciphertext as a byte array
lengththe length of the above byte array
Returns
decrypted message

Definition at line 23 of file pubkey.cpp.

23 {
24 uint8_t valid_mask = 0;
25
26 secure_vector<uint8_t> decoded = do_decrypt(valid_mask, in, length);
27
28 if(valid_mask == 0) {
29 throw Decoding_Error("Invalid public key ciphertext, cannot decrypt");
30 }
31
32 return decoded;
33}
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61

Referenced by Botan::KeyPair::encryption_consistency_check().

◆ decrypt() [2/2]

secure_vector< uint8_t > Botan::PK_Decryptor::decrypt ( std::span< const uint8_t > in) const
inlineinherited

Same as above, but taking a vector

Parameters
inthe ciphertext
Returns
decrypted message

Definition at line 96 of file pubkey.h.

96{ return decrypt(in.data(), in.size()); }
secure_vector< uint8_t > decrypt(const uint8_t in[], size_t length) const
Definition pubkey.cpp:23

References Botan::PK_Decryptor::decrypt().

Referenced by Botan::PK_Decryptor::decrypt().

◆ decrypt_or_random() [1/2]

secure_vector< uint8_t > Botan::PK_Decryptor::decrypt_or_random ( const uint8_t in[],
size_t length,
size_t expected_pt_len,
RandomNumberGenerator & rng ) const
inherited

Decrypt a ciphertext. If the ciphertext is invalid (eg due to invalid padding) or is not the expected length, instead returns a random string of the expected length. Use to avoid oracle attacks, especially against PKCS #1 v1.5 decryption.

Definition at line 80 of file pubkey.cpp.

83 {
84 return decrypt_or_random(in, length, expected_pt_len, rng, nullptr, nullptr, 0);
85}
secure_vector< uint8_t > decrypt_or_random(const uint8_t in[], size_t length, size_t expected_pt_len, RandomNumberGenerator &rng) const
Definition pubkey.cpp:80

References Botan::PK_Decryptor::decrypt_or_random().

Referenced by Botan::TLS::Client_Key_Exchange::Client_Key_Exchange(), and Botan::PK_Decryptor::decrypt_or_random().

◆ decrypt_or_random() [2/2]

secure_vector< uint8_t > Botan::PK_Decryptor::decrypt_or_random ( const uint8_t in[],
size_t length,
size_t expected_pt_len,
RandomNumberGenerator & rng,
const uint8_t required_content_bytes[],
const uint8_t required_content_offsets[],
size_t required_contents ) const
inherited

Decrypt a ciphertext. If the ciphertext is invalid (eg due to invalid padding) or is not the expected length, instead returns a random string of the expected length. Use to avoid oracle attacks, especially against PKCS #1 v1.5 decryption.

Additionally checks (also in const time) that: contents[required_content_offsets[i]] == required_content_bytes[i] for 0 <= i < required_contents

Used for example in TLS, which encodes the client version in the content bytes: if there is any timing variation the version check can be used as an oracle to recover the key.

Definition at line 35 of file pubkey.cpp.

41 {
42 const secure_vector<uint8_t> fake_pms = rng.random_vec(expected_pt_len);
43
44 uint8_t decrypt_valid = 0;
45 secure_vector<uint8_t> decoded = do_decrypt(decrypt_valid, in, length);
46
47 auto valid_mask = CT::Mask<uint8_t>::is_equal(decrypt_valid, 0xFF);
48 valid_mask &= CT::Mask<uint8_t>(CT::Mask<size_t>::is_zero(decoded.size() ^ expected_pt_len));
49
50 decoded.resize(expected_pt_len);
51
52 for(size_t i = 0; i != required_contents_length; ++i) {
53 /*
54 These values are chosen by the application and for TLS are constants,
55 so this early failure via assert is fine since we know 0,1 < 48
56
57 If there is a protocol that has content checks on the key where
58 the expected offsets are controllable by the attacker this could
59 still leak.
60
61 Alternately could always reduce the offset modulo the length?
62 */
63
64 const uint8_t exp = required_content_bytes[i];
65 const uint8_t off = required_content_offsets[i];
66
67 BOTAN_ASSERT(off < expected_pt_len, "Offset in range of plaintext");
68
69 auto eq = CT::Mask<uint8_t>::is_equal(decoded[off], exp);
70
71 valid_mask &= eq;
72 }
73
74 // If valid_mask is false, assign fake pre master instead
75 valid_mask.select_n(decoded.data(), decoded.data(), fake_pms.data(), expected_pt_len);
76
77 return decoded;
78}
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:50
static constexpr Mask< T > is_equal(T x, T y)
Definition ct_utils.h:434
static constexpr Mask< T > is_zero(T x)
Definition ct_utils.h:429

References BOTAN_ASSERT, Botan::CT::Mask< T >::is_equal(), and Botan::RandomNumberGenerator::random_vec().

◆ set_initialization_vector()

void Botan::DLIES_Decryptor::set_initialization_vector ( const InitializationVector & iv)
inline

Set the initialization vector for the data decryption method.

Definition at line 133 of file dlies.h.

133{ m_iv = iv; }

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