Botan 3.5.0
Crypto and TLS for C&
kyber_keys.cpp
Go to the documentation of this file.
1/*
2 * Crystals Kyber Internal Key Types
3 *
4 * (C) 2021-2024 Jack Lloyd
5 * (C) 2021-2022 Manuel Glaser and Michael Boric, Rohde & Schwarz Cybersecurity
6 * (C) 2021-2022 René Meusel and Hannes Rantzsch, neXenio GmbH
7 * (C) 2024 René Meusel, Rohde & Schwarz Cybersecurity
8 *
9 * Botan is released under the Simplified BSD License (see license.txt)
10 */
11
12#include <botan/internal/kyber_keys.h>
13
14#include <botan/internal/kyber_symmetric_primitives.h>
15#include <botan/internal/stl_util.h>
16
17namespace Botan {
18
20 m_mode(std::move(mode)),
21 m_t(std::move(t)),
22 m_rho(std::move(rho)),
23 m_public_key_bits_raw(concat(m_t.to_bytes(), m_rho)),
24 m_H_public_key_bits_raw(m_mode.symmetric_primitives().H(m_public_key_bits_raw)) {}
25
26/**
27 * NIST FIPS 203 IPD, Algorithm 13 (K-PKE.Encrypt)
28 */
31 auto at = PolynomialMatrix::generate(m_rho, true /* transposed */, m_mode);
32
33 auto rv = PolynomialVector::getnoise_eta1(r, 0, m_mode);
34 auto e1 = PolynomialVector::getnoise_eta2(r, m_mode.k(), m_mode);
35 auto e2 = Polynomial::getnoise_eta2(r, 2 * m_mode.k(), m_mode);
36
37 rv.ntt();
38
39 auto u = at.pointwise_acc_montgomery(rv);
40 u.invntt_tomont();
41 u += e1;
42 u.reduce();
43
44 auto mu = Polynomial::from_message(m);
46 v.invntt_tomont();
47 v += e2;
48 v += mu;
49 v.reduce();
50
51 return Ciphertext(std::move(u), v, m_mode);
52}
53
54/**
55 * NIST FIPS 203 IPD, Algorithm 14 (K-PKE.Decrypt)
56 */
58 auto& u = ct.b();
59 const auto& v = ct.v();
60
61 u.ntt();
63 w.invntt_tomont();
64
65 w -= v;
66 w.reduce();
67 return w.to_message();
68}
69
70} // namespace Botan
PolynomialVector & b()
KyberMessage indcpa_decrypt(Ciphertext ct) const
Ciphertext indcpa_encrypt(StrongSpan< const KyberMessage > m, StrongSpan< const KyberEncryptionRandomness > r) const
static PolynomialMatrix generate(StrongSpan< const KyberSeedRho > seed, const bool transposed, const KyberConstants &mode)
static PolynomialVector getnoise_eta1(KyberSigmaOrEncryptionRandomness seed, uint8_t nonce, const KyberConstants &mode)
static Polynomial pointwise_acc_montgomery(const PolynomialVector &a, const PolynomialVector &b)
static PolynomialVector getnoise_eta2(StrongSpan< const KyberEncryptionRandomness > seed, uint8_t nonce, const KyberConstants &mode)
static Polynomial getnoise_eta2(StrongSpan< const KyberEncryptionRandomness > seed, uint8_t nonce, const KyberConstants &mode)
static Polynomial from_message(StrongSpan< const KyberMessage > msg)
constexpr T rho(T x)
Definition rotate.h:51
constexpr auto concat(Rs &&... ranges)
Definition stl_util.h:262