Botan 3.11.0
Crypto and TLS for C&
kyber_encaps_base.h
Go to the documentation of this file.
1/*
2 * Key encapsulation base operations for Kyber
3 * (C) 2024 Jack Lloyd
4 * (C) 2024 René Meusel, Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9#ifndef BOTAN_KYBER_KEY_ENCAPSULATION_BASE_H_
10#define BOTAN_KYBER_KEY_ENCAPSULATION_BASE_H_
11
12#include <botan/internal/kyber_constants.h>
13#include <botan/internal/kyber_keys.h>
14#include <botan/internal/pk_ops_impl.h>
15
16namespace Botan {
17
19 protected:
21 m_mode(pk.mode()), m_At(Kyber_Algos::sample_matrix(pk.rho(), true /* transposed */, m_mode)) {}
22
23 const KyberConstants& mode() const { return m_mode; }
24
25 const KyberPolyMat& precomputed_matrix_At() const { return m_At; }
26
27 private:
28 const KyberConstants& m_mode;
29
30 // The public key's matrix is pre-computed to avoid redundant work when
31 // encapsulating multiple keys. This matrix is needed for encapsulation as
32 // well as for the Fujisaki-Okamoto transform in the decapsulation.
33 KyberPolyMat m_At;
34};
35
37 protected Kyber_KEM_Operation_Base {
38 public:
39 size_t raw_kem_shared_key_length() const override { return mode().shared_key_bytes(); }
40
41 size_t encapsulated_key_length() const override { return mode().ciphertext_bytes(); }
42
43 void raw_kem_encrypt(std::span<uint8_t> out_encapsulated_key,
44 std::span<uint8_t> out_shared_key,
45 RandomNumberGenerator& rng) final {
47 StrongSpan<KyberSharedSecret>(out_shared_key),
48 rng);
49 }
50
51 protected:
54
55 virtual void encapsulate(StrongSpan<KyberCompressedCiphertext> out_encapsulated_key,
56 StrongSpan<KyberSharedSecret> out_shared_key,
57 RandomNumberGenerator& rng) = 0;
58};
59
61 protected Kyber_KEM_Operation_Base {
62 public:
63 size_t raw_kem_shared_key_length() const override { return mode().shared_key_bytes(); }
64
65 size_t encapsulated_key_length() const override { return mode().ciphertext_bytes(); }
66
67 void raw_kem_decrypt(std::span<uint8_t> out_shared_key, std::span<const uint8_t> encapsulated_key) final {
70 }
71
72 protected:
75
76 virtual void decapsulate(StrongSpan<KyberSharedSecret> out_shared_key,
78};
79
80} // namespace Botan
81
82#endif
constexpr size_t shared_key_bytes() const
byte length of the shared key
size_t ciphertext_bytes() const
byte length of an encoded ciphertext
size_t raw_kem_shared_key_length() const override
size_t encapsulated_key_length() const override
void raw_kem_decrypt(std::span< uint8_t > out_shared_key, std::span< const uint8_t > encapsulated_key) final
Kyber_KEM_Decryptor_Base(std::string_view kdf, const Kyber_PublicKeyInternal &pk)
virtual void decapsulate(StrongSpan< KyberSharedSecret > out_shared_key, StrongSpan< const KyberCompressedCiphertext > encapsulated_key)=0
virtual void encapsulate(StrongSpan< KyberCompressedCiphertext > out_encapsulated_key, StrongSpan< KyberSharedSecret > out_shared_key, RandomNumberGenerator &rng)=0
void raw_kem_encrypt(std::span< uint8_t > out_encapsulated_key, std::span< uint8_t > out_shared_key, RandomNumberGenerator &rng) final
size_t encapsulated_key_length() const override
size_t raw_kem_shared_key_length() const override
Kyber_KEM_Encryptor_Base(std::string_view kdf, const Kyber_PublicKeyInternal &pk)
Kyber_KEM_Operation_Base(const Kyber_PublicKeyInternal &pk)
const KyberPolyMat & precomputed_matrix_At() const
const KyberConstants & mode() const
KEM_Decryption_with_KDF(std::string_view kdf)
Definition pk_ops.cpp:266
KEM_Encryption_with_KDF(std::string_view kdf)
Definition pk_ops.cpp:231
BOTAN_FORCE_INLINE constexpr T rho(T x)
Definition rotate.h:53
Botan::CRYSTALS::PolynomialMatrix< KyberPolyTraits > KyberPolyMat
Definition kyber_types.h:27