Botan 3.11.0
Crypto and TLS for C&
ml_kem_impl.h
Go to the documentation of this file.
1/*
2 * Module-Lattice Key Encapsulation Mechanism (ML-KEM)
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#ifndef BOTAN_ML_KEM_IMPL_H_
11#define BOTAN_ML_KEM_IMPL_H_
12
13#include <botan/hash.h>
14#include <botan/rng.h>
15#include <botan/xof.h>
16
17#include <botan/internal/kyber_encaps_base.h>
18#include <botan/internal/kyber_keys.h>
19#include <botan/internal/kyber_symmetric_primitives.h>
20#include <botan/internal/kyber_types.h>
21#include <botan/internal/loadstor.h>
22
23namespace Botan {
24
26 public:
27 ML_KEM_Encryptor(std::shared_ptr<const Kyber_PublicKeyInternal> key, std::string_view kdf) :
28 Kyber_KEM_Encryptor_Base(kdf, *key), m_public_key(std::move(key)) {}
29
30 protected:
31 void encapsulate(StrongSpan<KyberCompressedCiphertext> out_encapsulated_key,
32 StrongSpan<KyberSharedSecret> out_shared_key,
33 RandomNumberGenerator& rng) override;
34
35 private:
36 std::shared_ptr<const Kyber_PublicKeyInternal> m_public_key;
37};
38
40 public:
41 ML_KEM_Decryptor(std::shared_ptr<const Kyber_PrivateKeyInternal> private_key,
42 std::shared_ptr<const Kyber_PublicKeyInternal> public_key,
43 std::string_view kdf) :
44 Kyber_KEM_Decryptor_Base(kdf, *public_key),
45 m_public_key(std::move(public_key)),
46 m_private_key(std::move(private_key)) {}
47
48 protected:
50 StrongSpan<const KyberCompressedCiphertext> encapsulated_key) override;
51
52 private:
53 std::shared_ptr<const Kyber_PublicKeyInternal> m_public_key;
54 std::shared_ptr<const Kyber_PrivateKeyInternal> m_private_key;
55};
56
58 protected:
59 std::optional<std::array<uint8_t, 1>> seed_expansion_domain_separator(const KyberConstants& mode) const override {
60 // NIST FIPS 203, Algorithm 13 (K-PKE.KeyGen)
61 // Byte 33 of the input to G is the module dimension k from {2,3,4}.
62 // This is included to establish domain separation between the three
63 // parameter sets
64 return std::array{mode.k()};
65 }
66
67 std::unique_ptr<HashFunction> create_G() const override { return HashFunction::create_or_throw("SHA-3(512)"); }
68
69 std::unique_ptr<HashFunction> create_H() const override { return HashFunction::create_or_throw("SHA-3(256)"); }
70
71 std::unique_ptr<HashFunction> create_J() const override {
72 return HashFunction::create_or_throw("SHAKE-256(256)");
73 }
74
75 std::unique_ptr<HashFunction> create_KDF() const override {
76 throw Invalid_State("ML-KEM does not support KDF()");
77 }
78
79 std::unique_ptr<Botan::XOF> create_PRF(std::span<const uint8_t> seed, const uint8_t nonce) const override {
80 auto xof = Botan::XOF::create_or_throw("SHAKE-256");
81 init_PRF(*xof, seed, nonce);
82 return xof;
83 }
84
85 void init_PRF(Botan::XOF& xof, std::span<const uint8_t> seed, const uint8_t nonce) const override {
86 xof.clear();
87 xof.update(seed);
88 xof.update(store_be(nonce));
89 }
90
91 std::unique_ptr<Botan::XOF> create_XOF(std::span<const uint8_t> seed,
92 std::tuple<uint8_t, uint8_t> matrix_position) const override {
93 auto xof = Botan::XOF::create_or_throw("SHAKE-128");
94 init_XOF(*xof, seed, matrix_position);
95 return xof;
96 }
97
99 std::span<const uint8_t> seed,
100 std::tuple<uint8_t, uint8_t> matrix_position) const override {
101 xof.clear();
102 xof.update(seed);
103 xof.update(store_be(make_uint16(std::get<0>(matrix_position), std::get<1>(matrix_position))));
104 }
105};
106
107} // namespace Botan
108
109#endif
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:308
Kyber_KEM_Decryptor_Base(std::string_view kdf, const Kyber_PublicKeyInternal &pk)
Kyber_KEM_Encryptor_Base(std::string_view kdf, const Kyber_PublicKeyInternal &pk)
void decapsulate(StrongSpan< KyberSharedSecret > out_shared_key, StrongSpan< const KyberCompressedCiphertext > encapsulated_key) override
ML_KEM_Decryptor(std::shared_ptr< const Kyber_PrivateKeyInternal > private_key, std::shared_ptr< const Kyber_PublicKeyInternal > public_key, std::string_view kdf)
Definition ml_kem_impl.h:41
void encapsulate(StrongSpan< KyberCompressedCiphertext > out_encapsulated_key, StrongSpan< KyberSharedSecret > out_shared_key, RandomNumberGenerator &rng) override
ML_KEM_Encryptor(std::shared_ptr< const Kyber_PublicKeyInternal > key, std::string_view kdf)
Definition ml_kem_impl.h:27
void init_XOF(Botan::XOF &xof, std::span< const uint8_t > seed, std::tuple< uint8_t, uint8_t > matrix_position) const override
Definition ml_kem_impl.h:98
std::unique_ptr< HashFunction > create_KDF() const override
Definition ml_kem_impl.h:75
std::unique_ptr< HashFunction > create_H() const override
Definition ml_kem_impl.h:69
std::optional< std::array< uint8_t, 1 > > seed_expansion_domain_separator(const KyberConstants &mode) const override
Definition ml_kem_impl.h:59
std::unique_ptr< HashFunction > create_J() const override
Definition ml_kem_impl.h:71
std::unique_ptr< Botan::XOF > create_PRF(std::span< const uint8_t > seed, const uint8_t nonce) const override
Definition ml_kem_impl.h:79
std::unique_ptr< HashFunction > create_G() const override
Definition ml_kem_impl.h:67
std::unique_ptr< Botan::XOF > create_XOF(std::span< const uint8_t > seed, std::tuple< uint8_t, uint8_t > matrix_position) const override
Definition ml_kem_impl.h:91
void init_PRF(Botan::XOF &xof, std::span< const uint8_t > seed, const uint8_t nonce) const override
Definition ml_kem_impl.h:85
void clear()
Definition xof.h:64
static std::unique_ptr< XOF > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition xof.cpp:54
void update(std::span< const uint8_t > input)
Definition xof.h:140
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745
constexpr uint16_t make_uint16(uint8_t i0, uint8_t i1)
Definition loadstor.h:92