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

#include <ml_kem_impl.h>

Inheritance diagram for Botan::ML_KEM_Encryptor:
Botan::Kyber_KEM_Encryptor_Base Botan::PK_Ops::KEM_Encryption_with_KDF Botan::Kyber_KEM_Operation_Base Botan::PK_Ops::KEM_Encryption

Public Member Functions

size_t encapsulated_key_length () const override
 
void kem_encrypt (std::span< uint8_t > out_encapsulated_key, std::span< uint8_t > out_shared_key, RandomNumberGenerator &rng, size_t desired_shared_key_len, std::span< const uint8_t > salt) final
 
 ML_KEM_Encryptor (std::shared_ptr< const Kyber_PublicKeyInternal > key, std::string_view kdf)
 
void raw_kem_encrypt (std::span< uint8_t > out_encapsulated_key, std::span< uint8_t > out_shared_key, RandomNumberGenerator &rng) 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 encapsulate (StrongSpan< KyberCompressedCiphertext > out_encapsulated_key, StrongSpan< KyberSharedSecret > out_shared_key, RandomNumberGenerator &rng) override
 
const KyberConstantsmode () const override
 
const KyberPolyMatprecomputed_matrix_At () const
 

Detailed Description

Definition at line 24 of file ml_kem_impl.h.

Constructor & Destructor Documentation

◆ ML_KEM_Encryptor()

Botan::ML_KEM_Encryptor::ML_KEM_Encryptor ( std::shared_ptr< const Kyber_PublicKeyInternal > key,
std::string_view kdf )
inline

Definition at line 26 of file ml_kem_impl.h.

26 :
27 Kyber_KEM_Encryptor_Base(kdf, *key), m_public_key(std::move(key)) {}
Kyber_KEM_Encryptor_Base(std::string_view kdf, const Kyber_PublicKeyInternal &pk)

Member Function Documentation

◆ encapsulate()

void Botan::ML_KEM_Encryptor::encapsulate ( StrongSpan< KyberCompressedCiphertext > out_encapsulated_key,
StrongSpan< KyberSharedSecret > out_shared_key,
RandomNumberGenerator & rng )
overrideprotectedvirtual

NIST FIPS 203, Algorithm 17 (ML-KEM.Encaps_internal), and 20 (ML-KEM.Encaps)

Generation of the random value is inlined with its usage. The public matrix A^T as well as H(pk) are precomputed and readily available.

Implements Botan::Kyber_KEM_Encryptor_Base.

Definition at line 25 of file ml_kem_impl.cpp.

27 {
28 const auto& sym = m_public_key->mode().symmetric_primitives();
29
30 const auto m = rng.random_vec<KyberMessage>(KyberConstants::SEED_BYTES);
31 auto scope = CT::scoped_poison(m);
32
33 const auto [K, r] = sym.G(m, m_public_key->H_public_key_bits_raw());
34 m_public_key->indcpa_encrypt(out_encapsulated_key, m, r, precomputed_matrix_At());
35
36 // TODO: avoid this copy by letting sym.G() directly write to the span.
37 copy_mem(out_shared_key, K);
38 CT::unpoison_all(out_shared_key, out_encapsulated_key);
39}
static constexpr size_t SEED_BYTES
const KyberPolyMat & precomputed_matrix_At() const
constexpr void unpoison_all(Ts &&... ts)
Definition ct_utils.h:201
constexpr auto scoped_poison(const Ts &... xs)
Definition ct_utils.h:216
Strong< secure_vector< uint8_t >, struct KyberMessage_ > KyberMessage
Random message value to be encrypted by the CPA-secure Kyber encryption scheme.
Definition kyber_types.h:45
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:146

References Botan::copy_mem(), Botan::Kyber_KEM_Operation_Base::precomputed_matrix_At(), Botan::RandomNumberGenerator::random_vec(), Botan::CT::scoped_poison(), Botan::KyberConstants::SEED_BYTES, and Botan::CT::unpoison_all().

◆ encapsulated_key_length()

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

Implements Botan::PK_Ops::KEM_Encryption.

Definition at line 37 of file kyber_encaps_base.h.

37{ 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_Encryptor_Base::mode().

◆ kem_encrypt()

void Botan::PK_Ops::KEM_Encryption_with_KDF::kem_encrypt ( std::span< uint8_t > out_encapsulated_key,
std::span< uint8_t > out_shared_key,
RandomNumberGenerator & rng,
size_t desired_shared_key_len,
std::span< const uint8_t > salt )
finalvirtualinherited

Implements Botan::PK_Ops::KEM_Encryption.

Definition at line 184 of file pk_ops.cpp.

188 {
189 BOTAN_ARG_CHECK(salt.empty() || m_kdf, "PK_KEM_Encryptor::encrypt requires a KDF to use a salt");
190 BOTAN_ASSERT_NOMSG(out_encapsulated_key.size() == encapsulated_key_length());
191
192 if(m_kdf) {
194 out_shared_key.size(), desired_shared_key_len, "KDF output length and shared key length match");
195
197 this->raw_kem_encrypt(out_encapsulated_key, raw_shared, rng);
198 m_kdf->derive_key(out_shared_key, raw_shared, salt, {});
199 } else {
200 BOTAN_ASSERT_EQUAL(out_shared_key.size(), raw_kem_shared_key_length(), "Shared key has raw KEM output length");
201 this->raw_kem_encrypt(out_encapsulated_key, out_shared_key, rng);
202 }
203}
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition assert.h:68
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
virtual size_t raw_kem_shared_key_length() const =0
virtual void raw_kem_encrypt(std::span< uint8_t > out_encapsulated_key, std::span< uint8_t > out_raw_shared_key, RandomNumberGenerator &rng)=0
virtual size_t encapsulated_key_length() const =0
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61

References BOTAN_ARG_CHECK, BOTAN_ASSERT_EQUAL, and BOTAN_ASSERT_NOMSG.

◆ mode()

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

Implements Botan::Kyber_KEM_Encryptor_Base.

Definition at line 34 of file ml_kem_impl.h.

34{ return m_public_key->mode(); }

◆ precomputed_matrix_At()

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

◆ raw_kem_encrypt()

void Botan::Kyber_KEM_Encryptor_Base::raw_kem_encrypt ( std::span< uint8_t > out_encapsulated_key,
std::span< uint8_t > out_shared_key,
RandomNumberGenerator & rng )
inlinefinalvirtualinherited

Implements Botan::PK_Ops::KEM_Encryption_with_KDF.

Definition at line 39 of file kyber_encaps_base.h.

41 {
42 encapsulate(StrongSpan<KyberCompressedCiphertext>(out_encapsulated_key),
43 StrongSpan<KyberSharedSecret>(out_shared_key),
44 rng);
45 }
virtual void encapsulate(StrongSpan< KyberCompressedCiphertext > out_encapsulated_key, StrongSpan< KyberSharedSecret > out_shared_key, RandomNumberGenerator &rng)=0

References Botan::Kyber_KEM_Encryptor_Base::encapsulate().

◆ raw_kem_shared_key_length()

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

Implements Botan::PK_Ops::KEM_Encryption_with_KDF.

Definition at line 35 of file kyber_encaps_base.h.

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

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

◆ shared_key_length()

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

Implements Botan::PK_Ops::KEM_Encryption.

Definition at line 176 of file pk_ops.cpp.

176 {
177 if(m_kdf) {
178 return desired_shared_key_len;
179 } else {
180 return this->raw_kem_shared_key_length();
181 }
182}

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