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>
26#include <botan/internal/pk_options_impl.h>
28#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
29 #include <botan/internal/kyber_round3_impl.h>
32#if defined(BOTAN_HAS_ML_KEM)
33 #include <botan/internal/ml_kem_impl.h>
44 if(str ==
"Kyber-512-90s-r3") {
47 if(str ==
"Kyber-768-90s-r3") {
50 if(str ==
"Kyber-1024-90s-r3") {
53 if(str ==
"Kyber-512-r3") {
56 if(str ==
"Kyber-768-r3") {
59 if(str ==
"Kyber-1024-r3") {
62 if(str ==
"ML-KEM-512") {
65 if(str ==
"ML-KEM-768") {
68 if(str ==
"ML-KEM-1024") {
76 if(
const auto name = oid.registered_name()) {
77 return kyber_mode_from_string(*
name);
98 return "Kyber-512-90s-r3";
100 return "Kyber-768-90s-r3";
102 return "Kyber-1024-90s-r3";
104 return "Kyber-512-r3";
106 return "Kyber-768-r3";
108 return "Kyber-1024-r3";
114 return "ML-KEM-1024";
138#if defined(BOTAN_HAS_KYBER)
144#if defined(BOTAN_HAS_KYBER_90S)
150#if defined(BOTAN_HAS_ML_KEM)
180 return m_public->mode().estimated_strength();
187 throw Decoding_Error(
"Unexpected parameters for ML-KEM/Kyber public key");
200 return m_public->public_key_bits_raw().get();
210 return m_public->mode().canonical_parameter_set_identifier();
220 return std::make_unique<Kyber_PrivateKey>(rng,
mode());
238 throw Decoding_Error(
"Unexpected parameters for ML-KEM/Kyber private key");
246 if(
mode.mode().is_ml_kem()) {
248 m_public = std::move(decoded.keypair.first);
249 m_private = std::move(decoded.keypair.second);
250 m_private_key_format = decoded.format;
251 }
else if(sk.size() ==
mode.expanded_private_key_bytes()) {
253 }
else if(sk.size() ==
mode.seed_private_key_bytes()) {
254 throw Decoding_Error(
"Kyber round 3 private keys do not support the seed format");
256 throw Decoding_Error(
"Private key does not have the correct byte count");
261 return std::make_unique<Kyber_PublicKey>(*
this);
282 const auto K_prime = dec.
decrypt(c);
291#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
292 if(
mode().is_kyber_round3()) {
293 return std::make_unique<Kyber_KEM_Encryptor>(
m_public, options);
297#if defined(BOTAN_HAS_ML_KEM)
298 if(
mode().is_ml_kem()) {
299 return std::make_unique<ML_KEM_Encryptor>(
m_public, options);
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, options);
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, options);
327 return m_private_key_format;
333 if(
mode().is_kyber_round3()) {
334 throw Encoding_Error(
"Kyber round 3 private keys only support the expanded format");
336 if(!m_private->seed().d.has_value()) {
337 throw Encoding_Error(
"Expanded private keys do not support the seed format");
344 "there is no raw encoding of an ML-KEM private key containing both seed and expanded key");
350 if(
mode().is_ml_kem()) {
355 throw Encoding_Error(
"Kyber round 3 private keys only support the expanded format");
#define BOTAN_ASSERT_UNREACHABLE()
bool parameters_are_empty() const
Codec for expanded private keys (as specified in FIPS 203).
secure_vector< uint8_t > encode_keypair(KyberInternalKeypair private_key) const override
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
std::unique_ptr< PK_Ops::KEM_Decryption > _create_kem_decryption_op(RandomNumberGenerator &rng, const PK_KEM_Options_Reader &options) const override
secure_vector< uint8_t > formatted_private_key_bits(MlPrivateKeyFormat format) 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)
MlPrivateKeyFormat private_key_format() const override
secure_vector< uint8_t > formatted_raw_private_key_bits(MlPrivateKeyFormat format) 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
size_t key_length() const override
AlgorithmIdentifier algorithm_identifier() const override
Kyber_PublicKey()=default
std::shared_ptr< const Kyber_PublicKeyInternal > m_public
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)
std::unique_ptr< PK_Ops::KEM_Encryption > _create_kem_encryption_op(const PK_KEM_Options_Reader &options) const override
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.
secure_vector< uint8_t > encode_keypair(KyberInternalKeypair keypair) const override
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.
KyberDecodedKeypair decode_ml_kem_private_key(std::span< const uint8_t > private_key, KyberConstants mode)
std::string fmt(std::string_view format, const T &... args)
secure_vector< uint8_t > encode_ml_kem_private_key(const KyberInternalKeypair &keypair, MlPrivateKeyFormat format)
BOTAN_FORCE_INLINE constexpr T rho(T x)
Strong< secure_vector< uint8_t >, struct KyberSeedRandomness_ > KyberSeedRandomness
Principal seed used to generate Kyber key pairs.
void require_software_provider(const OptionsT &options, std::string_view algo_name)
std::vector< T, secure_allocator< T > > secure_vector
Strong< std::vector< uint8_t >, struct KyberSerializedPublicKey_ > KyberSerializedPublicKey
Public key in serialized form (t || rho).