Botan 3.9.0
Crypto and TLS for C&
kyber_round3_impl.cpp
Go to the documentation of this file.
1/*
2 * Crystals Kyber key encapsulation mechanism and key codec
3 *
4 * (C) 2024 Jack Lloyd
5 * (C) 2024 René Meusel, Rohde & Schwarz Cybersecurity
6 *
7 * Botan is released under the Simplified BSD License (see license.txt)
8 */
9
10#include <botan/internal/kyber_round3_impl.h>
11
12#include <botan/internal/ct_utils.h>
13#include <botan/internal/kyber_constants.h>
14#include <botan/internal/kyber_symmetric_primitives.h>
15#include <botan/internal/kyber_types.h>
16
17namespace Botan {
18
19/**
20 * Crystals Kyber (Version 3.01), Algorithm 8 (Kyber.CCAKEM.Enc())
21 */
23 StrongSpan<KyberSharedSecret> out_shared_key,
25 const auto& sym = m_public_key->mode().symmetric_primitives();
26
27 const auto seed_m = rng.random_vec<KyberMessage>(KyberConstants::SEED_BYTES);
28 CT::poison(seed_m);
29
30 const auto m = sym.H(seed_m);
31 const auto [K_bar, r] = sym.G(m, m_public_key->H_public_key_bits_raw());
32 m_public_key->indcpa_encrypt(out_encapsulated_key, m, r, precomputed_matrix_At());
33
34 sym.KDF(out_shared_key, K_bar, sym.H(out_encapsulated_key));
35 CT::unpoison_all(out_shared_key, out_encapsulated_key);
36}
37
38/**
39 * Crystals Kyber (Version 3.01), Algorithm 9 (Kyber.CCAKEM.Dec())
40 */
43 auto scope = CT::scoped_poison(*m_private_key);
44
45 const auto& sym = m_public_key->mode().symmetric_primitives();
46
47 const auto& h = m_public_key->H_public_key_bits_raw();
48 const auto& z = m_private_key->z();
49
50 const auto m_prime = m_private_key->indcpa_decrypt(encapsulated_key);
51 const auto [K_bar_prime, r_prime] = sym.G(m_prime, h);
52
53 const auto c_prime = m_public_key->indcpa_encrypt(m_prime, r_prime, precomputed_matrix_At());
54
56 BOTAN_ASSERT_NOMSG(encapsulated_key.size() == c_prime.size());
57 BOTAN_ASSERT_NOMSG(K_bar_prime.size() == K.size());
58 const auto reencrypt_success = CT::is_equal(encapsulated_key.data(), c_prime.data(), encapsulated_key.size());
59 CT::conditional_copy_mem(reencrypt_success, K.data(), K_bar_prime.data(), z.data(), K_bar_prime.size());
60
61 sym.KDF(out_shared_key, K, sym.H(encapsulated_key));
62 CT::unpoison(out_shared_key);
63}
64
65} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
static constexpr size_t SEED_BYTES
void decapsulate(StrongSpan< KyberSharedSecret > out_shared_key, StrongSpan< const KyberCompressedCiphertext > encapsulated_key) override
void encapsulate(StrongSpan< KyberCompressedCiphertext > out_encapsulated_key, StrongSpan< KyberSharedSecret > out_shared_key, RandomNumberGenerator &rng) override
const KyberPolyMat & precomputed_matrix_At() const
void random_vec(std::span< uint8_t > v)
Definition rng.h:199
decltype(auto) data() noexcept(noexcept(this->m_span.data()))
decltype(auto) size() const noexcept(noexcept(this->m_span.size()))
size_type size() const noexcept(noexcept(this->get().size()))
decltype(auto) data() noexcept(noexcept(this->get().data()))
constexpr Mask< T > conditional_copy_mem(Mask< T > mask, T *dest, const T *if_set, const T *if_unset, size_t elems)
Definition ct_utils.h:760
constexpr auto scoped_poison(const Ts &... xs)
Definition ct_utils.h:220
constexpr void unpoison_all(const Ts &... ts)
Definition ct_utils.h:205
constexpr CT::Mask< T > is_equal(const T x[], const T y[], size_t len)
Definition ct_utils.h:826
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:65
constexpr void poison(const T *p, size_t n)
Definition ct_utils.h:54
Strong< secure_vector< uint8_t >, struct KyberMessage_ > KyberMessage
Random message value to be encrypted by the CPA-secure Kyber encryption scheme.
Definition kyber_types.h:45
Strong< secure_vector< uint8_t >, struct KyberSharedSecret_ > KyberSharedSecret
Shared secret value generated during encapsulation and recovered during decapsulation.
Definition kyber_types.h:54