15#include <botan/kyber.h>
17#include <botan/assert.h>
18#include <botan/pubkey.h>
20#include <botan/secmem.h>
21#include <botan/internal/fmt.h>
22#include <botan/internal/kyber_algos.h>
23#include <botan/internal/kyber_constants.h>
24#include <botan/internal/kyber_keys.h>
25#include <botan/internal/kyber_types.h>
27#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
28 #include <botan/internal/kyber_round3_impl.h>
31#if defined(BOTAN_HAS_ML_KEM)
32 #include <botan/internal/ml_kem_impl.h>
43 if(str ==
"Kyber-512-90s-r3") {
46 if(str ==
"Kyber-768-90s-r3") {
49 if(str ==
"Kyber-1024-90s-r3") {
52 if(str ==
"Kyber-512-r3") {
55 if(str ==
"Kyber-768-r3") {
58 if(str ==
"Kyber-1024-r3") {
61 if(str ==
"ML-KEM-512") {
64 if(str ==
"ML-KEM-768") {
67 if(str ==
"ML-KEM-1024") {
89 return "Kyber-512-90s-r3";
91 return "Kyber-768-90s-r3";
93 return "Kyber-1024-90s-r3";
95 return "Kyber-512-r3";
97 return "Kyber-768-r3";
99 return "Kyber-1024-r3";
105 return "ML-KEM-1024";
129#if defined(BOTAN_HAS_KYBER)
135#if defined(BOTAN_HAS_KYBER_90S)
141#if defined(BOTAN_HAS_ML_KEM)
171 return m_public->mode().estimated_strength();
186 return m_public->public_key_bits_raw().get();
196 return m_public->mode().canonical_parameter_set_identifier();
205 std::vector<uint8_t> test(
m_public->mode().polynomial_vector_bytes());
208 const auto& serialized_pubkey =
m_public->public_key_bits_raw();
209 return test.size() < serialized_pubkey.size() && std::equal(test.begin(), test.end(), serialized_pubkey.begin());
213 return std::make_unique<Kyber_PrivateKey>(rng,
mode());
232 if(
mode.mode().is_ml_kem() && sk.size() ==
mode.seed_private_key_bytes()) {
234 }
else if(sk.size() ==
mode.expanded_private_key_bytes()) {
236 }
else if(!
mode.mode().is_ml_kem() && sk.size() ==
mode.seed_private_key_bytes()) {
237 throw Invalid_Argument(
"Kyber round 3 private keys do not support the seed format");
244 return std::make_unique<Kyber_PublicKey>(*
this);
271 const auto K_prime = dec.
decrypt(c);
277 std::string_view provider)
const {
278 if(provider.empty() || provider ==
"base") {
279#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
280 if(
mode().is_kyber_round3()) {
281 return std::make_unique<Kyber_KEM_Encryptor>(
m_public, params);
285#if defined(BOTAN_HAS_ML_KEM)
286 if(
mode().is_ml_kem()) {
287 return std::make_unique<ML_KEM_Encryptor>(
m_public, params);
297 std::string_view params,
298 std::string_view provider)
const {
300 if(provider.empty() || provider ==
"base") {
301#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
302 if(
mode().is_kyber_round3()) {
303 return std::make_unique<Kyber_KEM_Decryptor>(m_private,
m_public, params);
307#if defined(BOTAN_HAS_ML_KEM)
308 if(
mode().is_ml_kem()) {
309 return std::make_unique<ML_KEM_Decryptor>(m_private,
m_public, params);
319 if(
mode().is_ml_kem() && m_private->seed().d.has_value()) {
327 throw Encoding_Error(
"Expanded private keys do not support the seed format");
329 const auto codec = [&]() -> std::unique_ptr<Kyber_Keypair_Codec> {
332 return std::make_unique<Seed_Expanding_Keypair_Codec>();
334 return std::make_unique<Expanded_Keypair_Codec>();
338 return codec->encode_keypair({
m_public, m_private});
#define BOTAN_ASSERT_UNREACHABLE()
Codec for expanded private keys (as specified in FIPS 203).
KyberInternalKeypair decode_keypair(std::span< const uint8_t > buffer, KyberConstants mode) const override
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
MlPrivateKeyFormat private_key_format() const
secure_vector< uint8_t > private_key_bits_with_format(MlPrivateKeyFormat format) 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
Kyber_PublicKey(std::span< const uint8_t > pub_key, KyberMode mode)
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)
Codec for private keys as 64-byte seeds: d || z.
KyberInternalKeypair decode_keypair(std::span< const uint8_t > buffer, KyberConstants mode) const override
void encode_polynomial_vector(std::span< uint8_t > out, const KyberPolyVecNTT &vec)
KyberInternalKeypair expand_keypair(KyberPrivateKeySeed seed, KyberConstants mode)
Strong< secure_vector< uint8_t >, struct KyberImplicitRejectionValue_ > KyberImplicitRejectionValue
Secret random value (called Z in the spec), used for implicit rejection in the decapsulation.
std::string fmt(std::string_view format, const T &... args)
MlPrivateKeyFormat
Byte encoding format of ML-KEM and ML-DSA the private key.
@ Expanded
The expanded format, i.e., the format specified in FIPS-203/204.
BOTAN_FORCE_INLINE constexpr T rho(T x)
Strong< secure_vector< uint8_t >, struct KyberSeedRandomness_ > KyberSeedRandomness
Principal seed used to generate Kyber key pairs.
std::vector< T, secure_allocator< T > > secure_vector
Strong< std::vector< uint8_t >, struct KyberSerializedPublicKey_ > KyberSerializedPublicKey
Public key in serialized form (t || rho).