10#ifndef BOTAN_KYBER_SYMMETRIC_PRIMITIVES_H_
11#define BOTAN_KYBER_SYMMETRIC_PRIMITIVES_H_
13#include <botan/hash.h>
15#include <botan/internal/buffer_slicer.h>
16#include <botan/internal/fmt.h>
17#include <botan/internal/hash_engine.h>
18#include <botan/internal/kyber_constants.h>
19#include <botan/internal/kyber_types.h>
49 return G_split<KyberSeedRho, KyberSeedSigma>(seed, *domsep);
51 return G_split<KyberSeedRho, KyberSeedSigma>(seed);
55 std::pair<KyberSharedSecret, KyberEncryptionRandomness>
G(
57 return G_split<KyberSharedSecret, KyberEncryptionRandomness>(msg, pubkey_hash);
63 j->update(rejection_value);
64 j->update(ciphertext);
73 kdf->update(shared_secret);
74 kdf->update(hashed_ciphertext);
80 const size_t outlen)
const {
81 const auto bare_seed_span = std::visit([&](
const auto s) {
return s.get(); }, seed);
88 std::tuple<uint8_t, uint8_t> matrix_position)
const {
92 init_XOF(*xof, seed, matrix_position);
97 void setup_PRF(std::unique_ptr<Botan::XOF>& xof, std::span<const uint8_t> seed, uint8_t nonce)
const {
107 virtual void XOF_batch(std::span<std::span<uint8_t>> outputs,
109 std::span<
const std::tuple<uint8_t, uint8_t>> positions)
const {
111 std::unique_ptr<Botan::XOF> xof;
112 for(
size_t i = 0; i < outputs.size(); ++i) {
115 xof->output(outputs[i]);
126 std::span<const uint8_t> seed,
127 uint8_t first_nonce)
const {
128 std::unique_ptr<Botan::XOF> xof;
129 for(
size_t i = 0; i < outputs.size(); ++i) {
131 setup_PRF(xof, seed,
static_cast<uint8_t
>(first_nonce + i));
132 xof->output(outputs[i]);
140 std::pair<T1, T2> G_split(
const InputTs&... inputs)
const {
142 (g->update(inputs), ...);
143 const auto s = g->final();
146 std::pair<T1, T2> result;
158 std::span<
const std::tuple<uint8_t, uint8_t>> positions) {
160 if(outputs.empty()) {
166 std::vector<uint8_t> pos_bytes(2 * positions.size());
167 std::vector<std::span<const uint8_t>> inputs(positions.size());
168 for(
size_t i = 0; i < positions.size(); ++i) {
170 pos_bytes[2 * i] = std::get<0>(positions[i]);
171 pos_bytes[2 * i + 1] = std::get<1>(positions[i]);
172 inputs[i] = std::span(pos_bytes).subspan(2 * i, 2);
175 engine->batch_hash(outputs, inputs);
181 virtual std::unique_ptr<HashFunction>
create_G()
const = 0;
182 virtual std::unique_ptr<HashFunction>
create_H()
const = 0;
183 virtual std::unique_ptr<HashFunction>
create_J()
const = 0;
184 virtual std::unique_ptr<HashFunction>
create_KDF()
const = 0;
186 virtual std::unique_ptr<Botan::XOF>
create_PRF(std::span<const uint8_t> seed, uint8_t nonce)
const = 0;
189 virtual std::unique_ptr<Botan::XOF>
create_XOF(std::span<const uint8_t> seed,
190 std::tuple<uint8_t, uint8_t> matrix_position)
const = 0;
192 std::span<const uint8_t> seed,
193 std::tuple<uint8_t, uint8_t> matrix_position)
const = 0;
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_DEBUG_ASSERT(expr)
static std::unique_ptr< Hash_Engine > create_or_throw(std::string_view hash_fn, std::span< const uint8_t > common_prefix={}, std::string_view provider="")
static constexpr size_t SEED_BYTES
static void shake_XOF_batch(std::span< std::span< uint8_t > > outputs, StrongSpan< const KyberSeedRho > seed, std::span< const std::tuple< uint8_t, uint8_t > > positions)
virtual ~Kyber_Symmetric_Primitives()=default
KyberMessage H(StrongSpan< const KyberMessage > m) const
void setup_XOF(std::unique_ptr< Botan::XOF > &xof, StrongSpan< const KyberSeedRho > seed, std::tuple< uint8_t, uint8_t > matrix_position) const
Setup an XOF object for matrix sampling.
virtual void init_XOF(Botan::XOF &xof, std::span< const uint8_t > seed, std::tuple< uint8_t, uint8_t > matrix_position) const =0
KyberHashedCiphertext H(StrongSpan< const KyberCompressedCiphertext > r) const
void setup_PRF(std::unique_ptr< Botan::XOF > &xof, std::span< const uint8_t > seed, uint8_t nonce) const
Setup a seeded PRF XOF for polynomial sampling.
virtual std::unique_ptr< Botan::XOF > create_XOF(std::span< const uint8_t > seed, std::tuple< uint8_t, uint8_t > matrix_position) const =0
void KDF(StrongSpan< KyberSharedSecret > out, StrongSpan< const KyberSharedSecret > shared_secret, StrongSpan< const KyberHashedCiphertext > hashed_ciphertext) const
KyberSamplingRandomness PRF(KyberSigmaOrEncryptionRandomness seed, const uint8_t nonce, const size_t outlen) const
virtual std::unique_ptr< Botan::XOF > create_PRF(std::span< const uint8_t > seed, uint8_t nonce) const =0
virtual std::unique_ptr< HashFunction > create_G() const =0
virtual std::unique_ptr< HashFunction > create_H() const =0
std::pair< KyberSharedSecret, KyberEncryptionRandomness > G(StrongSpan< const KyberMessage > msg, StrongSpan< const KyberHashedPublicKey > pubkey_hash) const
void PRF_batch(std::span< const StrongSpan< KyberSamplingRandomness > > outputs, std::span< const uint8_t > seed, uint8_t first_nonce) const
KyberSharedSecret J(StrongSpan< const KyberImplicitRejectionValue > rejection_value, StrongSpan< const KyberCompressedCiphertext > ciphertext) const
virtual std::unique_ptr< HashFunction > create_J() const =0
virtual void XOF_batch(std::span< std::span< uint8_t > > outputs, StrongSpan< const KyberSeedRho > seed, std::span< const std::tuple< uint8_t, uint8_t > > positions) const
std::pair< KyberSeedRho, KyberSeedSigma > G(StrongSpan< const KyberSeedRandomness > seed, const KyberConstants &mode) const
virtual void init_PRF(Botan::XOF &xof, std::span< const uint8_t > seed, uint8_t nonce) const =0
KyberHashedPublicKey H(StrongSpan< const KyberSerializedPublicKey > pk) const
virtual std::unique_ptr< HashFunction > create_KDF() const =0
virtual std::optional< std::array< uint8_t, 1 > > seed_expansion_domain_separator(const KyberConstants &mode) const =0
Strong< secure_vector< uint8_t >, struct KyberMessage_ > KyberMessage
Random message value to be encrypted by the CPA-secure Kyber encryption scheme.
std::string fmt(std::string_view format, const T &... args)
Strong< secure_vector< uint8_t >, struct KyberSamplingRandomness_ > KyberSamplingRandomness
PRF value used for sampling of error polynomials.
Strong< std::vector< uint8_t >, struct KyberHashedCiphertext_ > KyberHashedCiphertext
std::variant< StrongSpan< const KyberSeedSigma >, StrongSpan< const KyberEncryptionRandomness > > KyberSigmaOrEncryptionRandomness
Variant value of either a KyberSeedSigma or a KyberEncryptionRandomness.
Strong< secure_vector< uint8_t >, struct KyberSharedSecret_ > KyberSharedSecret
Shared secret value generated during encapsulation and recovered during decapsulation.
Strong< std::vector< uint8_t >, struct KyberHashedPublicKey_ > KyberHashedPublicKey
Hash value of the serialized public key.