15#include <botan/kyber.h>
17#include <botan/assert.h>
18#include <botan/mem_ops.h>
20#include <botan/secmem.h>
22#include <botan/internal/fmt.h>
23#include <botan/internal/kyber_constants.h>
24#include <botan/internal/kyber_keys.h>
25#include <botan/internal/kyber_symmetric_primitives.h>
26#include <botan/internal/kyber_types.h>
27#include <botan/internal/stl_util.h>
29#if defined(BOTAN_HAS_KYBER)
30 #include <botan/internal/kyber_modern.h>
33#if defined(BOTAN_HAS_KYBER_90S)
34 #include <botan/internal/kyber_90s.h>
37#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
38 #include <botan/internal/kyber_encaps.h>
51 if(str ==
"Kyber-512-90s-r3") {
54 if(str ==
"Kyber-768-90s-r3") {
57 if(str ==
"Kyber-1024-90s-r3") {
60 if(str ==
"Kyber-512-r3") {
63 if(str ==
"Kyber-768-r3") {
66 if(str ==
"Kyber-1024-r3") {
70 throw Invalid_Argument(
fmt(
"'{}' is not a valid Kyber mode name", str));
88 return "Kyber-512-90s-r3";
90 return "Kyber-768-90s-r3";
92 return "Kyber-1024-90s-r3";
94 return "Kyber-512-r3";
96 return "Kyber-768-r3";
98 return "Kyber-1024-r3";
118#if defined(BOTAN_HAS_KYBER)
124#if defined(BOTAN_HAS_KYBER_90S)
150 return m_public->mode().estimated_strength();
157 if(pub_key.size() !=
mode.public_key_byte_length()) {
158 throw Invalid_Argument(
"kyber public key does not have the correct byte count");
163 auto poly_vec = s.
take(
mode.polynomial_vector_byte_length());
167 return std::make_shared<Kyber_PublicKeyInternal>(std::move(
mode), poly_vec, std::move(seed));
174 m_public(initialize_from_encoding(pub_key, m)) {}
180 return m_public->public_key_bits_raw().get();
191 return m_public->mode().public_key_byte_length();
199 return std::make_unique<Kyber_PrivateKey>(rng,
mode());
220 auto t = a.pointwise_acc_montgomery(s,
true);
226 m_public = std::make_shared<Kyber_PublicKeyInternal>(
mode, std::move(t), std::move(
rho));
227 m_private = std::make_shared<Kyber_PrivateKeyInternal>(
237 if(
mode.private_key_byte_length() != sk.size()) {
238 throw Invalid_Argument(
"kyber private key does not have the correct byte count");
251 m_private = std::make_shared<Kyber_PrivateKeyInternal>(std::move(
mode), std::move(skpv), std::move(z));
255 std::equal(
m_public->H_public_key_bits_raw().begin(),
256 m_public->H_public_key_bits_raw().end(),
257 puk_key_hash.
begin()));
261 return std::make_unique<Kyber_PublicKey>(*
this);
276 std::string_view provider)
const {
277 if(provider.empty() || provider ==
"base") {
278#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
279 if(
mode().is_kyber_round3()) {
280 return std::make_unique<Kyber_KEM_Encryptor>(
m_public, params);
290 std::string_view params,
291 std::string_view provider)
const {
293 if(provider.empty() || provider ==
"base") {
294#if defined(BOTAN_HAS_KYBER) || defined(BOTAN_HAS_KYBER_90S)
295 if(
mode().is_kyber_round3()) {
296 return std::make_unique<Kyber_KEM_Decryptor>(m_private,
m_public, params);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ASSERT(expr, assertion_made)
#define BOTAN_ASSERT_UNREACHABLE()
auto copy(const size_t count)
std::span< const uint8_t > take(const size_t count)
static constexpr size_t kPublicKeyHashLength
static constexpr size_t kZLength
static constexpr size_t kSeedLength
static constexpr size_t kSymBytes
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
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
static std::shared_ptr< Kyber_PublicKeyInternal > initialize_from_encoding(std::span< const uint8_t > pub_key, KyberMode m)
std::vector< uint8_t > public_key_bits() const override
std::vector< uint8_t > raw_public_key_bits() const override
bool check_key(RandomNumberGenerator &, bool) 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)
static PolynomialMatrix generate(StrongSpan< const KyberSeedRho > seed, const bool transposed, const KyberConstants &mode)
static PolynomialVector from_bytes(std::span< const uint8_t > a, const KyberConstants &mode)
static PolynomialVector getnoise_eta1(KyberSigmaOrEncryptionRandomness seed, uint8_t nonce, const KyberConstants &mode)
void random_vec(std::span< uint8_t > v)
decltype(auto) begin() noexcept(noexcept(this->get().begin()))
size_type size() const noexcept(noexcept(this->get().size()))
std::string fmt(std::string_view format, const T &... args)
constexpr auto concat(Rs &&... ranges)
std::vector< T, secure_allocator< T > > secure_vector