Botan 3.3.0
Crypto and TLS for C&
kyber.h
Go to the documentation of this file.
1/*
2 * Crystals Kyber key encapsulation mechanism
3 * Based on the public domain reference implementation by the
4 * designers (https://github.com/pq-crystals/kyber)
5 *
6 * Further changes
7 * (C) 2021-2022 Jack Lloyd
8 * (C) 2021-2022 Manuel Glaser and Michael Boric, Rohde & Schwarz Cybersecurity
9 * (C) 2021-2022 René Meusel and Hannes Rantzsch, neXenio GmbH
10 *
11 * Botan is released under the Simplified BSD License (see license.txt)
12 */
13
14#ifndef BOTAN_KYBER_COMMON_H_
15#define BOTAN_KYBER_COMMON_H_
16
17#include <botan/asn1_obj.h>
18#include <botan/der_enc.h>
19#include <botan/exceptn.h>
20#include <botan/pk_keys.h>
21
22#include <span>
23
24#if !defined(BOTAN_HAS_KYBER_90S) && !defined(BOTAN_HAS_KYBER)
25static_assert(false, "botan module 'kyber_common' is useful only when enabling modules 'kyber', 'kyber_90s' or both");
26#endif
27
28namespace Botan {
29
31 public:
32 enum Mode {
33 // Kyber512 as proposed in round 3 of the NIST competition
35 // Kyber768 as proposed in round 3 of the NIST competition
37 // Kyber1024 as proposed in round 3 of the NIST competition
39
40 Kyber512 BOTAN_DEPRECATED("Use Kyber512_R3") = Kyber512_R3,
41 Kyber768 BOTAN_DEPRECATED("Use Kyber768_R3") = Kyber768_R3,
42 Kyber1024 BOTAN_DEPRECATED("Use Kyber1024_R3") = Kyber1024_R3,
43
44 Kyber512_90s BOTAN_DEPRECATED("Kyber 90s mode is deprecated"),
45 Kyber768_90s BOTAN_DEPRECATED("Kyber 90s mode is deprecated"),
46 Kyber1024_90s BOTAN_DEPRECATED("Kyber 90s mode is deprecated"),
47 };
48
49 KyberMode(Mode mode);
50 explicit KyberMode(const OID& oid);
51 explicit KyberMode(std::string_view str);
52
53 OID object_identifier() const;
54 std::string to_string() const;
55
56 Mode mode() const { return m_mode; }
57
58 BOTAN_DEPRECATED("Kyber 90s mode is deprecated") bool is_90s() const;
59
60 BOTAN_DEPRECATED("Kyber 90s mode is deprecated") bool is_modern() const;
61
62 BOTAN_DEPRECATED("Kyber 90s mode is deprecated") bool is_available() const;
63
64 bool operator==(const KyberMode& other) const { return m_mode == other.m_mode; }
65
66 bool operator!=(const KyberMode& other) const { return !(*this == other); }
67
68 private:
69 Mode m_mode;
70};
71
72class Kyber_PublicKeyInternal;
73class Kyber_PrivateKeyInternal;
74
75class BOTAN_PUBLIC_API(3, 0) Kyber_PublicKey : public virtual Public_Key {
76 public:
77 Kyber_PublicKey(std::span<const uint8_t> pub_key, KyberMode mode);
78
79 Kyber_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
80
81 Kyber_PublicKey(const Kyber_PublicKey& other);
82
83 Kyber_PublicKey& operator=(const Kyber_PublicKey& other) = default;
84
85 ~Kyber_PublicKey() override = default;
86
87 std::string algo_name() const override { return "Kyber"; }
88
89 AlgorithmIdentifier algorithm_identifier() const override;
90
91 OID object_identifier() const override;
92
93 size_t key_length() const override;
94
95 size_t estimated_strength() const override;
96
97 std::vector<uint8_t> public_key_bits() const override;
98
99 bool check_key(RandomNumberGenerator&, bool) const override;
100
101 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
102
103 bool supports_operation(PublicKeyOperation op) const override {
104 return (op == PublicKeyOperation::KeyEncapsulation);
105 }
106
107 std::unique_ptr<PK_Ops::KEM_Encryption> create_kem_encryption_op(std::string_view params,
108 std::string_view provider) const override;
109
110 KyberMode mode() const;
111
112 protected:
113 Kyber_PublicKey() = default;
114
115 static std::shared_ptr<Kyber_PublicKeyInternal> initialize_from_encoding(std::span<const uint8_t> pub_key,
116 KyberMode m);
117
118 const std::vector<uint8_t>& public_key_bits_raw() const;
119 const std::vector<uint8_t>& H_public_key_bits_raw() const;
120
121 protected:
122 friend class Kyber_KEM_Encryptor;
123 friend class Kyber_KEM_Decryptor;
124
125 std::shared_ptr<Kyber_PublicKeyInternal> m_public;
126};
127
130
132 public virtual Private_Key {
133 public:
135
136 Kyber_PrivateKey(std::span<const uint8_t> sk, KyberMode mode);
137
138 Kyber_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
139
140 std::unique_ptr<Public_Key> public_key() const override;
141
142 secure_vector<uint8_t> private_key_bits() const override;
143
144 secure_vector<uint8_t> raw_private_key_bits() const override;
145
146 std::unique_ptr<PK_Ops::KEM_Decryption> create_kem_decryption_op(RandomNumberGenerator& rng,
147 std::string_view params,
148 std::string_view provider) const override;
149
150 private:
151 friend class Kyber_KEM_Decryptor;
152
153 std::shared_ptr<Kyber_PrivateKeyInternal> m_private;
154};
155
157
158} // namespace Botan
159
160#endif
bool operator!=(const KyberMode &other) const
Definition kyber.h:66
Mode mode() const
Definition kyber.h:56
Kyber_PublicKey & operator=(const Kyber_PublicKey &other)=default
std::string algo_name() const override
Definition kyber.h:87
std::shared_ptr< Kyber_PublicKeyInternal > m_public
Definition kyber.h:125
~Kyber_PublicKey() override=default
bool supports_operation(PublicKeyOperation op) const override
Definition kyber.h:103
int(* final)(unsigned char *, CTX *)
#define BOTAN_DIAGNOSTIC_POP
Definition compiler.h:191
#define BOTAN_DIAGNOSTIC_PUSH
Definition compiler.h:188
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition compiler.h:190
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
#define BOTAN_DEPRECATED(msg)
Definition compiler.h:125
PublicKeyOperation
Definition pk_keys.h:45
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61