Botan 3.4.0
Crypto and TLS for C&
Public Member Functions | List of all members
Botan::PK_Decryptor Class Referenceabstract

#include <pubkey.h>

Inheritance diagram for Botan::PK_Decryptor:
Botan::DLIES_Decryptor Botan::ECIES_Decryptor Botan::PK_Decryptor_EME

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
 
PK_Decryptoroperator= (const PK_Decryptor &)=delete
 
PK_Decryptoroperator= (PK_Decryptor &&) noexcept=default
 
 PK_Decryptor ()=default
 
 PK_Decryptor (const PK_Decryptor &)=delete
 
 PK_Decryptor (PK_Decryptor &&) noexcept=default
 
virtual size_t plaintext_length (size_t ctext_len) const =0
 
virtual ~PK_Decryptor ()=default
 

Detailed Description

Public Key Decryptor

Definition at line 78 of file pubkey.h.

Constructor & Destructor Documentation

◆ PK_Decryptor() [1/3]

Botan::PK_Decryptor::PK_Decryptor ( )
default

◆ ~PK_Decryptor()

virtual Botan::PK_Decryptor::~PK_Decryptor ( )
virtualdefault

◆ PK_Decryptor() [2/3]

Botan::PK_Decryptor::PK_Decryptor ( const PK_Decryptor & )
delete

◆ PK_Decryptor() [3/3]

Botan::PK_Decryptor::PK_Decryptor ( PK_Decryptor && )
defaultnoexcept

Member Function Documentation

◆ decrypt() [1/2]

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

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 22 of file pubkey.cpp.

22 {
23 uint8_t valid_mask = 0;
24
25 secure_vector<uint8_t> decoded = do_decrypt(valid_mask, in, length);
26
27 if(valid_mask == 0) {
28 throw Decoding_Error("Invalid public key ciphertext, cannot decrypt");
29 }
30
31 return decoded;
32}

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
inline

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:22

References decrypt().

Referenced by 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

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 79 of file pubkey.cpp.

82 {
83 return decrypt_or_random(in, length, expected_pt_len, rng, nullptr, nullptr, 0);
84}
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:79

References decrypt_or_random().

Referenced by Botan::TLS::Client_Key_Exchange::Client_Key_Exchange(), and 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

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 34 of file pubkey.cpp.

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

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

◆ operator=() [1/2]

PK_Decryptor & Botan::PK_Decryptor::operator= ( const PK_Decryptor & )
delete

◆ operator=() [2/2]

PK_Decryptor & Botan::PK_Decryptor::operator= ( PK_Decryptor && )
defaultnoexcept

◆ plaintext_length()

virtual size_t Botan::PK_Decryptor::plaintext_length ( size_t ctext_len) const
pure virtual

Return an upper bound on the plaintext length for a particular ciphertext input length

Implemented in Botan::PK_Decryptor_EME.


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