Botan 3.6.1
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_At(Kyber_Algos::sample_matrix(pk.rho(), true /* transposed */, pk.mode())) {}
22
23 const KyberPolyMat& precomputed_matrix_At() const { return m_At; }
24
25 private:
26 // The public key's matrix is pre-computed to avoid redundant work when
27 // encapsulating multiple keys. This matrix is needed for encapsulation as
28 // well as for the FO transform in the decapsulation.
29 KyberPolyMat m_At;
30};
31
33 protected Kyber_KEM_Operation_Base {
34 public:
35 size_t raw_kem_shared_key_length() const override { return mode().shared_key_bytes(); }
36
37 size_t encapsulated_key_length() const override { return mode().ciphertext_bytes(); }
38
39 void raw_kem_encrypt(std::span<uint8_t> out_encapsulated_key,
40 std::span<uint8_t> out_shared_key,
41 RandomNumberGenerator& rng) final {
43 StrongSpan<KyberSharedSecret>(out_shared_key),
44 rng);
45 }
46
47 protected:
50
51 virtual void encapsulate(StrongSpan<KyberCompressedCiphertext> out_encapsulated_key,
52 StrongSpan<KyberSharedSecret> out_shared_key,
53 RandomNumberGenerator& rng) = 0;
54
55 virtual const KyberConstants& mode() const = 0;
56};
57
59 protected Kyber_KEM_Operation_Base {
60 public:
61 size_t raw_kem_shared_key_length() const override { return mode().shared_key_bytes(); }
62
63 size_t encapsulated_key_length() const override { return mode().ciphertext_bytes(); }
64
65 void raw_kem_decrypt(std::span<uint8_t> out_shared_key, std::span<const uint8_t> encapsulated_key) final {
68 }
69
70 protected:
73
74 virtual void decapsulate(StrongSpan<KyberSharedSecret> out_shared_key,
76
77 virtual const KyberConstants& mode() const = 0;
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
virtual const KyberConstants & mode() const =0
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
virtual const KyberConstants & mode() const =0
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
KEM_Decryption_with_KDF(std::string_view kdf)
Definition pk_ops.cpp:238
KEM_Encryption_with_KDF(std::string_view kdf)
Definition pk_ops.cpp:205
constexpr T rho(T x)
Definition rotate.h:51