15#include <botan/kyber.h>
17#include <botan/assert.h>
18#include <botan/mem_ops.h>
19#include <botan/pubkey.h>
21#include <botan/secmem.h>
23#include <botan/internal/ct_utils.h>
24#include <botan/internal/fmt.h>
25#include <botan/internal/kyber_algos.h>
26#include <botan/internal/kyber_constants.h>
27#include <botan/internal/kyber_keys.h>
28#include <botan/internal/kyber_symmetric_primitives.h>
29#include <botan/internal/kyber_types.h>
30#include <botan/internal/stl_util.h>
32#if defined(BOTAN_HAS_KYBER)
33 #include <botan/internal/kyber_modern.h>
36#if defined(BOTAN_HAS_KYBER_90S)
37 #include <botan/internal/kyber_90s.h>
40#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
41 #include <botan/internal/kyber_round3_impl.h>
44#if defined(BOTAN_HAS_ML_KEM)
45 #include <botan/internal/ml_kem_impl.h>
56 if(str ==
"Kyber-512-90s-r3") {
59 if(str ==
"Kyber-768-90s-r3") {
62 if(str ==
"Kyber-1024-90s-r3") {
65 if(str ==
"Kyber-512-r3") {
68 if(str ==
"Kyber-768-r3") {
71 if(str ==
"Kyber-1024-r3") {
74 if(str ==
"ML-KEM-512") {
77 if(str ==
"ML-KEM-768") {
80 if(str ==
"ML-KEM-1024") {
84 throw Invalid_Argument(
fmt(
"'{}' is not a valid Kyber mode name", str));
102 return "Kyber-512-90s-r3";
104 return "Kyber-768-90s-r3";
106 return "Kyber-1024-90s-r3";
108 return "Kyber-512-r3";
110 return "Kyber-768-r3";
112 return "Kyber-1024-r3";
118 return "ML-KEM-1024";
142#if defined(BOTAN_HAS_KYBER)
148#if defined(BOTAN_HAS_KYBER_90S)
154#if defined(BOTAN_HAS_ML_KEM)
184 return m_public->mode().estimated_strength();
196 other.m_public->mode(), other.m_public->t().clone(), other.m_public->
rho())) {}
199 return m_public->public_key_bits_raw().get();
209 return m_public->mode().canonical_parameter_set_identifier();
218 std::vector<uint8_t> test(
m_public->mode().polynomial_vector_bytes());
221 const auto& serialized_pubkey =
m_public->public_key_bits_raw();
222 return test.size() < serialized_pubkey.size() && std::equal(test.begin(), test.end(), serialized_pubkey.begin());
226 return std::make_unique<Kyber_PrivateKey>(rng,
mode());
245 if(
mode.private_key_bytes() != sk.size()) {
249 const auto& codec =
mode.keypair_codec();
250 std::tie(
m_public, m_private) = codec.decode_keypair(sk, std::move(
mode));
254 return std::make_unique<Kyber_PublicKey>(*
this);
262 return m_private->mode().keypair_codec().encode_keypair({
m_public, m_private});
281 const auto K_prime = dec.
decrypt(c);
287 std::string_view provider)
const {
288 if(provider.empty() || provider ==
"base") {
289#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
290 if(
mode().is_kyber_round3()) {
291 return std::make_unique<Kyber_KEM_Encryptor>(
m_public, params);
295#if defined(BOTAN_HAS_ML_KEM)
296 if(
mode().is_ml_kem()) {
297 return std::make_unique<ML_KEM_Encryptor>(
m_public, params);
307 std::string_view params,
308 std::string_view provider)
const {
310 if(provider.empty() || provider ==
"base") {
311#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
312 if(
mode().is_kyber_round3()) {
313 return std::make_unique<Kyber_KEM_Decryptor>(m_private,
m_public, params);
317#if defined(BOTAN_HAS_ML_KEM)
318 if(
mode().is_ml_kem()) {
319 return std::make_unique<ML_KEM_Decryptor>(m_private,
m_public, params);
#define BOTAN_ASSERT_UNREACHABLE()
static std::pair< std::vector< uint8_t >, secure_vector< uint8_t > > destructure(KEM_Encapsulation &&kem)
static constexpr size_t SEED_BYTES
bool is_available() const
bool is_kyber_round3() const
std::string to_string() const
OID object_identifier() const
std::unique_ptr< PK_Ops::KEM_Decryption > create_kem_decryption_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
std::unique_ptr< Public_Key > public_key() const override
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Kyber_PrivateKey(RandomNumberGenerator &rng, KyberMode mode)
secure_vector< uint8_t > raw_private_key_bits() const override
secure_vector< uint8_t > private_key_bits() const override
std::vector< uint8_t > public_key_bits() const override
std::vector< uint8_t > raw_public_key_bits() const override
bool check_key(RandomNumberGenerator &rng, bool strong) const override
std::string algo_name() const override
std::shared_ptr< Kyber_PublicKeyInternal > m_public
size_t key_length() const override
AlgorithmIdentifier algorithm_identifier() const override
Kyber_PublicKey()=default
std::unique_ptr< PK_Ops::KEM_Encryption > create_kem_encryption_op(std::string_view params, std::string_view provider) const override
OID object_identifier() const override
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
size_t estimated_strength() const override
static OID from_string(std::string_view str)
void decrypt(std::span< uint8_t > out_shared_key, std::span< const uint8_t > encap_key, size_t desired_shared_key_len=32, std::span< const uint8_t > salt={})
KEM_Encapsulation encrypt(RandomNumberGenerator &rng, size_t desired_shared_key_len=32, std::span< const uint8_t > salt={})
void random_vec(std::span< uint8_t > v)
void encode_polynomial_vector(std::span< uint8_t > out, const KyberPolyVecNTT &vec)
KyberInternalKeypair expand_keypair(KyberPrivateKeySeed seed, KyberConstants mode)
std::string fmt(std::string_view format, const T &... args)
std::vector< T, secure_allocator< T > > secure_vector
Strong< std::vector< uint8_t >, struct KyberSerializedPublicKey_ > KyberSerializedPublicKey
Public key in serialized form (t || rho)