Botan 3.0.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)
25 static_assert(false, "botan module 'kyber_common' is useful only when enabling modules 'kyber', 'kyber_90s' or both");
26#endif
27
28namespace Botan {
29
31 {
32 public:
33 enum Mode
34 {
40 Kyber1024_90s
41 };
42
43 KyberMode(Mode mode);
44 explicit KyberMode(const OID& oid);
45 explicit KyberMode(std::string_view str);
46
47 OID object_identifier() const;
48 std::string to_string() const;
49
50 Mode mode() const { return m_mode; }
51 bool is_90s() const { return m_mode == Kyber512_90s || m_mode == Kyber768_90s || m_mode == Kyber1024_90s; }
52 bool is_modern() const { return !is_90s(); }
53
54 bool operator==(const KyberMode& other) const { return m_mode == other.m_mode; }
55 bool operator!=(const KyberMode& other) const { return !(*this == other); }
56
57 private:
58 Mode m_mode;
59 };
60
61class Kyber_PublicKeyInternal;
62class Kyber_PrivateKeyInternal;
63
64class BOTAN_PUBLIC_API(3, 0) Kyber_PublicKey : public virtual Public_Key
65 {
66 public:
67 Kyber_PublicKey(std::span<const uint8_t> pub_key, KyberMode mode);
68
70 std::span<const uint8_t> key_bits);
71
72 Kyber_PublicKey(const Kyber_PublicKey& other);
73
74 Kyber_PublicKey& operator=(const Kyber_PublicKey& other) = default;
75
76 virtual ~Kyber_PublicKey() = default;
77
78 std::string algo_name() const override;
79
80 AlgorithmIdentifier algorithm_identifier() const override;
81
82 OID object_identifier() const override;
83
84 size_t key_length() const override;
85
86 size_t estimated_strength() const override;
87
88 std::vector<uint8_t> public_key_bits() const override;
89
90 bool check_key(RandomNumberGenerator&, bool) const override;
91
92 bool supports_operation(PublicKeyOperation op) const override
93 {
94 return (op == PublicKeyOperation::KeyEncapsulation);
95 }
96
97 std::unique_ptr<PK_Ops::KEM_Encryption> create_kem_encryption_op(
98 std::string_view params,
99 std::string_view provider) const override;
100
101 KyberMode mode() const;
102
103 protected:
105
106 static std::shared_ptr<Kyber_PublicKeyInternal> initialize_from_encoding(std::span<const uint8_t> pub_key, KyberMode m);
107
108 const std::vector<uint8_t>& public_key_bits_raw() const;
109 const std::vector<uint8_t>& H_public_key_bits_raw() const;
110
111 protected:
112 friend class Kyber_KEM_Encryptor;
113 friend class Kyber_KEM_Decryptor;
114
115 std::shared_ptr<Kyber_PublicKeyInternal> m_public;
116 };
117
120
121class BOTAN_PUBLIC_API(3, 0) Kyber_PrivateKey final : public virtual Kyber_PublicKey, public virtual Private_Key
122 {
123 public:
125
126 Kyber_PrivateKey(std::span<const uint8_t> sk, KyberMode mode);
127
129 std::span<const uint8_t> key_bits);
130
131 std::unique_ptr<Public_Key> public_key() const override;
132
133 secure_vector<uint8_t> private_key_bits() const override;
134
135 secure_vector<uint8_t> raw_private_key_bits() const override;
136
137 std::unique_ptr<PK_Ops::KEM_Decryption> create_kem_decryption_op(RandomNumberGenerator& rng,
138 std::string_view params,
139 std::string_view provider) const override;
140
141 private:
142 friend class Kyber_KEM_Decryptor;
143
144 std::shared_ptr<Kyber_PrivateKeyInternal> m_private;
145 };
146
148
149} // namespace Botan
150
151#endif
bool operator==(const KyberMode &other) const
Definition: kyber.h:54
bool is_modern() const
Definition: kyber.h:52
bool operator!=(const KyberMode &other) const
Definition: kyber.h:55
bool is_90s() const
Definition: kyber.h:51
Mode mode() const
Definition: kyber.h:50
virtual ~Kyber_PublicKey()=default
Kyber_PublicKey & operator=(const Kyber_PublicKey &other)=default
std::shared_ptr< Kyber_PublicKeyInternal > m_public
Definition: kyber.h:115
bool supports_operation(PublicKeyOperation op) const override
Definition: kyber.h:92
int(* final)(unsigned char *, CTX *)
#define BOTAN_DIAGNOSTIC_POP
Definition: compiler.h:204
#define BOTAN_DIAGNOSTIC_PUSH
Definition: compiler.h:201
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition: compiler.h:203
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Definition: alg_id.cpp:12
PublicKeyOperation
Definition: pk_keys.h:43
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:64