Botan 3.0.0
Crypto and TLS for C&
kyber_modern.h
Go to the documentation of this file.
1/*
2 * Symmetric primitives for Kyber (modern (non-90s) mode)
3 * (C) 2022 Jack Lloyd
4 * (C) 2022 Hannes Rantzsch, René Meusel, neXenio GmbH
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9#ifndef BOTAN_KYBER_MODERN_H_
10#define BOTAN_KYBER_MODERN_H_
11
12#include <botan/hash.h>
13#include <botan/stream_cipher.h>
14
15#include <botan/internal/kyber_symmetric_primitives.h>
16#include <botan/internal/shake.h>
17#include <botan/internal/shake_cipher.h>
18
19#include <memory>
20#include <vector>
21
22namespace Botan {
23
25 {
26 public:
28 m_sha3_512(HashFunction::create_or_throw("SHA-3(512)")),
29 m_sha3_256(HashFunction::create_or_throw("SHA-3(256)")),
30 m_shake256_256(HashFunction::create_or_throw("SHAKE-256(256)"))
31 {}
32
33 std::unique_ptr<HashFunction> G() const override
34 {
35 return m_sha3_512->new_object();
36 }
37
38 std::unique_ptr<HashFunction> H() const override
39 {
40 return m_sha3_256->new_object();
41 }
42
43 std::unique_ptr<HashFunction> KDF() const override
44 {
45 return m_shake256_256->new_object();
46 }
47
48 std::unique_ptr<Kyber_XOF> XOF(std::span<const uint8_t> seed) const override
49 {
50 class Kyber_Modern_XOF final : public Kyber_XOF
51 {
52 public:
53 Kyber_Modern_XOF(std::span<const uint8_t> seed) :
54 m_cipher(std::make_unique<SHAKE_128_Cipher>())
55 {
56 m_key.reserve(seed.size() + 2);
57 m_key.insert(m_key.end(), seed.begin(), seed.end());
58 m_key.push_back(0);
59 m_key.push_back(0);
60 }
61
62 void set_position(const std::tuple<uint8_t, uint8_t>& matrix_position) override
63 {
64 m_key[m_key.size() - 2] = std::get<0>(matrix_position);
65 m_key[m_key.size() - 1] = std::get<1>(matrix_position);
66 m_cipher->set_key(m_key);
67 }
68
69 void write_output(std::span<uint8_t> out) override
70 {
71 m_cipher->write_keystream(out.data(), out.size());
72 }
73
74 private:
75 std::unique_ptr<StreamCipher> m_cipher;
77 };
78
79 return std::make_unique<Kyber_Modern_XOF>(seed);
80 }
81
82 secure_vector<uint8_t> PRF(std::span<const uint8_t> seed,
83 const uint8_t nonce,
84 const size_t outlen) const override
85 {
86 SHAKE_256 kdf(outlen * 8);
87 kdf.update(seed.data(), seed.size());
88 kdf.update(nonce);
89 return kdf.final();
90 }
91
92 private:
93 std::unique_ptr<HashFunction> m_sha3_512;
94 std::unique_ptr<HashFunction> m_sha3_256;
95 std::unique_ptr<HashFunction> m_shake256_256;
96 };
97
98} // namespace Botan
99
100#endif
void update(const uint8_t in[], size_t length)
Definition: buf_comp.h:35
void final(uint8_t out[])
Definition: buf_comp.h:76
std::unique_ptr< HashFunction > H() const override
Definition: kyber_modern.h:38
std::unique_ptr< HashFunction > KDF() const override
Definition: kyber_modern.h:43
std::unique_ptr< Kyber_XOF > XOF(std::span< const uint8_t > seed) const override
Definition: kyber_modern.h:48
std::unique_ptr< HashFunction > G() const override
Definition: kyber_modern.h:33
secure_vector< uint8_t > PRF(std::span< const uint8_t > seed, const uint8_t nonce, const size_t outlen) const override
Definition: kyber_modern.h:82
int(* final)(unsigned char *, CTX *)
Definition: alg_id.cpp:12
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:64