Botan 3.5.0
Crypto and TLS for C&
Botan::Kyber_KEM_Encryptor Class Referencefinal

#include <kyber_encaps.h>

Inheritance diagram for Botan::Kyber_KEM_Encryptor:
Botan::Kyber_KEM_Encryptor_Base Botan::PK_Ops::KEM_Encryption_with_KDF 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
 
 Kyber_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
 

Detailed Description

Definition at line 20 of file kyber_encaps.h.

Constructor & Destructor Documentation

◆ Kyber_KEM_Encryptor()

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

Definition at line 22 of file kyber_encaps.h.

22 :
23 Kyber_KEM_Encryptor_Base(kdf), m_public_key(std::move(key)) {}
Kyber_KEM_Encryptor_Base(std::string_view kdf)

Member Function Documentation

◆ encapsulate()

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

Crystals Kyber (Version 3.01), Algorithm 8 (Kyber.CCAKEM.Enc())

Implements Botan::Kyber_KEM_Encryptor_Base.

Definition at line 23 of file kyber_encaps.cpp.

25 {
26 const auto& sym = m_public_key->mode().symmetric_primitives();
27
28 const auto m = sym.H(rng.random_vec<KyberMessage>(KyberConstants::kSymBytes));
29 const auto [K_bar, r] = sym.G(m, m_public_key->H_public_key_bits_raw());
30 auto c = m_public_key->indcpa_encrypt(m, r);
31
32 c.to_bytes(out_encapsulated_key);
33 sym.KDF(out_shared_key, K_bar, sym.H(out_encapsulated_key));
34}
static constexpr size_t kSymBytes
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:36

References Botan::KyberConstants::kSymBytes, and Botan::RandomNumberGenerator::random_vec().

◆ encapsulated_key_length()

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

Implements Botan::PK_Ops::KEM_Encryption.

Definition at line 23 of file kyber_encaps_base.h.

23{ return mode().encapsulated_key_length(); }
size_t encapsulated_key_length() const
virtual const KyberConstants & mode() const =0

References Botan::KyberConstants::encapsulated_key_length(), 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 169 of file pk_ops.cpp.

173 {
174 BOTAN_ARG_CHECK(salt.empty() || m_kdf, "PK_KEM_Encryptor::encrypt requires a KDF to use a salt");
175 BOTAN_ASSERT_NOMSG(out_encapsulated_key.size() == encapsulated_key_length());
176
177 if(m_kdf) {
179 out_shared_key.size(), desired_shared_key_len, "KDF output length and shared key length match");
180
182 this->raw_kem_encrypt(out_encapsulated_key, raw_shared, rng);
183 m_kdf->derive_key(out_shared_key, raw_shared, salt, {});
184 } else {
185 BOTAN_ASSERT_EQUAL(out_shared_key.size(), raw_kem_shared_key_length(), "Shared key has raw KEM output length");
186 this->raw_kem_encrypt(out_encapsulated_key, out_shared_key, rng);
187 }
188}
#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::Kyber_KEM_Encryptor::mode ( ) const
inlineoverrideprotectedvirtual

Implements Botan::Kyber_KEM_Encryptor_Base.

Definition at line 30 of file kyber_encaps.h.

30{ return m_public_key->mode(); }

◆ 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 25 of file kyber_encaps_base.h.

27 {
28 encapsulate(StrongSpan<KyberCompressedCiphertext>(out_encapsulated_key),
29 StrongSpan<KyberSharedSecret>(out_shared_key),
30 rng);
31 }
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

◆ 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 161 of file pk_ops.cpp.

161 {
162 if(m_kdf) {
163 return desired_shared_key_len;
164 } else {
165 return this->raw_kem_shared_key_length();
166 }
167}

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