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

#include <ml_kem_impl.h>

Inheritance diagram for Botan::ML_KEM_Decryptor:
Botan::Kyber_KEM_Decryptor_Base Botan::PK_Ops::KEM_Decryption_with_KDF Botan::Kyber_KEM_Operation_Base Botan::PK_Ops::KEM_Decryption

Public Member Functions

size_t encapsulated_key_length () const override
 
void kem_decrypt (std::span< uint8_t > out_shared_key, std::span< const uint8_t > encapsulated_key, size_t desired_shared_key_len, std::span< const uint8_t > salt) final
 
 ML_KEM_Decryptor (std::shared_ptr< const Kyber_PrivateKeyInternal > private_key, std::shared_ptr< const Kyber_PublicKeyInternal > public_key, std::string_view kdf)
 
void raw_kem_decrypt (std::span< uint8_t > out_shared_key, std::span< const uint8_t > encapsulated_key) final
 
size_t raw_kem_shared_key_length () const override
 
size_t shared_key_length (size_t desired_shared_key_len) const final
 

Protected Member Functions

void decapsulate (StrongSpan< KyberSharedSecret > out_shared_key, StrongSpan< const KyberCompressedCiphertext > encapsulated_key) override
 
const KyberConstantsmode () const override
 
const KyberPolyMatprecomputed_matrix_At () const
 

Detailed Description

Definition at line 40 of file ml_kem_impl.h.

Constructor & Destructor Documentation

◆ ML_KEM_Decryptor()

Botan::ML_KEM_Decryptor::ML_KEM_Decryptor ( std::shared_ptr< const Kyber_PrivateKeyInternal > private_key,
std::shared_ptr< const Kyber_PublicKeyInternal > public_key,
std::string_view kdf )
inline

Definition at line 42 of file ml_kem_impl.h.

44 :
45 Kyber_KEM_Decryptor_Base(kdf, *public_key),
46 m_public_key(std::move(public_key)),
47 m_private_key(std::move(private_key)) {}
Kyber_KEM_Decryptor_Base(std::string_view kdf, const Kyber_PublicKeyInternal &pk)

Member Function Documentation

◆ decapsulate()

void Botan::ML_KEM_Decryptor::decapsulate ( StrongSpan< KyberSharedSecret > out_shared_key,
StrongSpan< const KyberCompressedCiphertext > c )
overrideprotectedvirtual

NIST FIPS 203, Algorithm 18 (ML-KEM.Decaps_internal) and 21 (ML-KEM.Decaps)

The public and private keys are readily available as member variables and don't need to be decoded. The checks stated in FIPS 203, Section 7.3 are performed before decoding the keys and the ciphertext.

Implements Botan::Kyber_KEM_Decryptor_Base.

Definition at line 48 of file ml_kem_impl.cpp.

49 {
50 auto scope = CT::scoped_poison(*m_private_key);
51
52 const auto& sym = m_public_key->mode().symmetric_primitives();
53
54 const auto& h = m_public_key->H_public_key_bits_raw();
55 const auto& z = m_private_key->z();
56
57 const auto m_prime = m_private_key->indcpa_decrypt(c);
58 const auto [K_prime, r_prime] = sym.G(m_prime, h);
59
60 const auto K_bar = sym.J(z, c);
61 const auto c_prime = m_public_key->indcpa_encrypt(m_prime, r_prime, precomputed_matrix_At());
62
63 BOTAN_ASSERT_NOMSG(c.size() == c_prime.size());
64 BOTAN_ASSERT_NOMSG(K_prime.size() == K_bar.size() && out_shared_key.size() == K_bar.size());
65 const auto reencrypt_success = CT::is_equal(c.data(), c_prime.data(), c.size());
66 CT::conditional_copy_mem(reencrypt_success, out_shared_key.data(), K_prime.data(), K_bar.data(), K_prime.size());
67
68 CT::unpoison(out_shared_key);
69}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
const KyberPolyMat & precomputed_matrix_At() const
constexpr auto scoped_poison(const Ts &... xs)
Definition ct_utils.h:216
constexpr Mask< T > conditional_copy_mem(Mask< T > mask, T *to, const T *from0, const T *from1, size_t elems)
Definition ct_utils.h:699
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:759
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:64

References BOTAN_ASSERT_NOMSG, Botan::CT::conditional_copy_mem(), Botan::StrongSpan< T >::data(), Botan::CT::is_equal(), Botan::Kyber_KEM_Operation_Base::precomputed_matrix_At(), Botan::CT::scoped_poison(), Botan::StrongSpan< T >::size(), and Botan::CT::unpoison().

◆ encapsulated_key_length()

size_t Botan::Kyber_KEM_Decryptor_Base::encapsulated_key_length ( ) const
inlineoverridevirtualinherited

Implements Botan::PK_Ops::KEM_Decryption.

Definition at line 63 of file kyber_encaps_base.h.

63{ return mode().ciphertext_bytes(); }
size_t ciphertext_bytes() const
byte length of an encoded ciphertext
virtual const KyberConstants & mode() const =0

References Botan::KyberConstants::ciphertext_bytes(), and Botan::Kyber_KEM_Decryptor_Base::mode().

◆ kem_decrypt()

void Botan::PK_Ops::KEM_Decryption_with_KDF::kem_decrypt ( std::span< uint8_t > out_shared_key,
std::span< const uint8_t > encapsulated_key,
size_t desired_shared_key_len,
std::span< const uint8_t > salt )
finalvirtualinherited

Implements Botan::PK_Ops::KEM_Decryption.

Definition at line 219 of file pk_ops.cpp.

222 {
223 BOTAN_ARG_CHECK(salt.empty() || m_kdf, "PK_KEM_Decryptor::decrypt requires a KDF to use a salt");
224
225 if(m_kdf) {
227 out_shared_key.size(), desired_shared_key_len, "KDF output length and shared key length match");
228
230 this->raw_kem_decrypt(raw_shared, encapsulated_key);
231 m_kdf->derive_key(out_shared_key, raw_shared, salt, {});
232 } else {
233 BOTAN_ASSERT_EQUAL(out_shared_key.size(), raw_kem_shared_key_length(), "Shared key has raw KEM output length");
234 this->raw_kem_decrypt(out_shared_key, encapsulated_key);
235 }
236}
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition assert.h:68
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
virtual void raw_kem_decrypt(std::span< uint8_t > out_raw_shared_key, std::span< const uint8_t > encapsulated_key)=0
virtual size_t raw_kem_shared_key_length() const =0
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61

References BOTAN_ARG_CHECK, and BOTAN_ASSERT_EQUAL.

◆ mode()

const KyberConstants & Botan::ML_KEM_Decryptor::mode ( ) const
inlineoverrideprotectedvirtual

Implements Botan::Kyber_KEM_Decryptor_Base.

Definition at line 53 of file ml_kem_impl.h.

53{ return m_private_key->mode(); }

◆ precomputed_matrix_At()

const KyberPolyMat & Botan::Kyber_KEM_Operation_Base::precomputed_matrix_At ( ) const
inlineprotectedinherited

◆ raw_kem_decrypt()

void Botan::Kyber_KEM_Decryptor_Base::raw_kem_decrypt ( std::span< uint8_t > out_shared_key,
std::span< const uint8_t > encapsulated_key )
inlinefinalvirtualinherited

Implements Botan::PK_Ops::KEM_Decryption_with_KDF.

Definition at line 65 of file kyber_encaps_base.h.

65 {
66 decapsulate(StrongSpan<KyberSharedSecret>(out_shared_key),
67 StrongSpan<const KyberCompressedCiphertext>(encapsulated_key));
68 }
virtual void decapsulate(StrongSpan< KyberSharedSecret > out_shared_key, StrongSpan< const KyberCompressedCiphertext > encapsulated_key)=0

References Botan::Kyber_KEM_Decryptor_Base::decapsulate().

◆ raw_kem_shared_key_length()

size_t Botan::Kyber_KEM_Decryptor_Base::raw_kem_shared_key_length ( ) const
inlineoverridevirtualinherited

Implements Botan::PK_Ops::KEM_Decryption_with_KDF.

Definition at line 61 of file kyber_encaps_base.h.

61{ return mode().shared_key_bytes(); }
constexpr size_t shared_key_bytes() const
byte length of the shared key

References Botan::Kyber_KEM_Decryptor_Base::mode(), and Botan::KyberConstants::shared_key_bytes().

◆ shared_key_length()

size_t Botan::PK_Ops::KEM_Decryption_with_KDF::shared_key_length ( size_t desired_shared_key_len) const
finalvirtualinherited

Implements Botan::PK_Ops::KEM_Decryption.

Definition at line 211 of file pk_ops.cpp.

211 {
212 if(m_kdf) {
213 return desired_shared_key_len;
214 } else {
215 return this->raw_kem_shared_key_length();
216 }
217}

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