Coverage Report

Created: 2026-12-01 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Botan-3.13.0/build/include/public/botan/kyber.h
Line
Count
Source
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/pk_keys.h>
18
#include <span>
19
20
#if !defined(BOTAN_HAS_KYBER_90S) && !defined(BOTAN_HAS_KYBER) && !defined(BOTAN_HAS_ML_KEM)
21
static_assert(
22
   false,
23
   "botan module 'kyber_common' is useful only when enabling at least one of those modules: 'kyber', 'kyber_90s', 'ml_kem'");
24
#endif
25
26
namespace Botan {
27
28
class BOTAN_PUBLIC_API(3, 0) KyberMode final {
29
   public:
30
      enum Mode : uint8_t /* NOLINT(*-use-enum-class) */ {
31
         // Kyber512 as proposed in round 3 of the NIST competition
32
         Kyber512_R3 BOTAN_DEPRECATED("Kyber R3 is deprecated - use ML-KEM") = 0,
33
         // Kyber768 as proposed in round 3 of the NIST competition
34
         Kyber768_R3 BOTAN_DEPRECATED("Kyber R3 is deprecated - use ML-KEM") = 1,
35
         // Kyber1024 as proposed in round 3 of the NIST competition
36
         Kyber1024_R3 BOTAN_DEPRECATED("Kyber R3 is deprecated - use ML-KEM") = 2,
37
38
         Kyber512 BOTAN_DEPRECATED("Kyber R3 is deprecated - use ML-KEM") = 0,
39
         Kyber768 BOTAN_DEPRECATED("Kyber R3 is deprecated - use ML-KEM") = 1,
40
         Kyber1024 BOTAN_DEPRECATED("Kyber R3 is deprecated - use ML-KEM") = 2,
41
42
         ML_KEM_512 = 3,
43
         ML_KEM_768 = 4,
44
         ML_KEM_1024 = 5,
45
46
         Kyber512_90s BOTAN_DEPRECATED("Kyber 90s mode is deprecated") = 6,
47
         Kyber768_90s BOTAN_DEPRECATED("Kyber 90s mode is deprecated") = 7,
48
         Kyber1024_90s BOTAN_DEPRECATED("Kyber 90s mode is deprecated") = 8,
49
      };
50
51
      // NOLINTNEXTLINE(*-explicit-conversions)
52
      KyberMode(Mode mode);
53
54
      explicit KyberMode(const OID& oid);
55
      explicit KyberMode(std::string_view str);
56
57
      OID object_identifier() const;
58
      std::string to_string() const;
59
60
0
      Mode mode() const { return m_mode; }
61
62
      BOTAN_DEPRECATED("Kyber 90s mode is deprecated") bool is_90s() const;
63
64
      BOTAN_DEPRECATED("Kyber 90s mode is deprecated") bool is_modern() const;
65
66
      bool is_ml_kem() const;
67
68
      bool is_kyber_round3() const;
69
70
      bool is_available() const;
71
72
0
      bool operator==(const KyberMode& other) const { return m_mode == other.m_mode; }
73
74
0
      bool operator!=(const KyberMode& other) const { return !(*this == other); }
75
76
   private:
77
      Mode m_mode;
78
};
79
80
/// Byte encoding format of ML-KEM and ML-DSA the private key
81
enum class MlPrivateKeyFormat : uint8_t {
82
   /// Only supported for ML-KEM/ML-DSA keys:
83
   /// - ML-KEM: 64-byte seed: d || z
84
   /// - ML-DSA: 32-byte seed: xi (private_key_bits_with_format not yet
85
   ///   yet supported for ML-DSA)
86
   Seed,
87
   /// The expanded format, i.e., the format specified in FIPS-203/204.
88
   Expanded,
89
};
90
91
class Kyber_PublicKeyInternal;
92
class Kyber_PrivateKeyInternal;
93
94
class BOTAN_PUBLIC_API(3, 0) Kyber_PublicKey : public virtual Public_Key {
95
   public:
96
      Kyber_PublicKey(std::span<const uint8_t> pub_key, KyberMode mode);
97
98
      Kyber_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
99
100
      Kyber_PublicKey(const Kyber_PublicKey& other);
101
      Kyber_PublicKey& operator=(const Kyber_PublicKey& other) = default;
102
      Kyber_PublicKey(Kyber_PublicKey&& other) = default;
103
      Kyber_PublicKey& operator=(Kyber_PublicKey&& other) = default;
104
105
0
      ~Kyber_PublicKey() override = default;
106
107
      std::string algo_name() const override;
108
109
      AlgorithmIdentifier algorithm_identifier() const override;
110
111
      OID object_identifier() const override;
112
113
      size_t key_length() const override;
114
115
      size_t estimated_strength() const override;
116
117
      std::vector<uint8_t> raw_public_key_bits() const override;
118
119
      std::vector<uint8_t> public_key_bits() const override;
120
121
      bool check_key(RandomNumberGenerator& rng, bool strong) const override;
122
123
      std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
124
125
0
      bool supports_operation(PublicKeyOperation op) const override {
126
0
         return (op == PublicKeyOperation::KeyEncapsulation);
127
0
      }
128
129
      std::unique_ptr<PK_Ops::KEM_Encryption> create_kem_encryption_op(std::string_view params,
130
                                                                       std::string_view provider) const override;
131
132
      KyberMode mode() const;
133
134
   protected:
135
0
      Kyber_PublicKey() = default;
136
137
      static std::shared_ptr<Kyber_PublicKeyInternal> initialize_from_encoding(std::span<const uint8_t> pub_key,
138
                                                                               KyberMode m);
139
140
   protected:
141
      friend class Kyber_KEM_Encryptor;
142
      friend class Kyber_KEM_Decryptor;
143
144
      std::shared_ptr<const Kyber_PublicKeyInternal> m_public;  // NOLINT(*non-private-member-variable*)
145
};
146
147
BOTAN_DIAGNOSTIC_PUSH
148
BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
149
150
class BOTAN_PUBLIC_API(3, 0) Kyber_PrivateKey final : public virtual Kyber_PublicKey,
151
                                                      public virtual Private_Key {
152
   public:
153
      /**
154
       * Create a new private key. The private key will be encoded as the 64 byte
155
       * seed.
156
       */
157
      Kyber_PrivateKey(RandomNumberGenerator& rng, KyberMode mode);
158
159
      /**
160
       * Import a private key using its key bytes. Supported are key bytes as
161
       * 64-byte seeds (not supported for Kyber Round 3 instances),
162
       * as well as the expanded encoding specified by FIPS 203. Note that the
163
       * encoding used in this constructor is reflected by the calls for
164
       * private_key_bits, private_key_info, etc.
165
       */
166
      Kyber_PrivateKey(std::span<const uint8_t> sk, KyberMode mode);
167
168
      /**
169
       * Import a private key using its key bytes. Supported are key bytes as
170
       * 64-byte seeds (not supported for Kyber Round 3 instances),
171
       * as well as the expanded encoding specified by FIPS 203. Note that the
172
       * encoding used in this constructor is reflected by the calls for
173
       * private_key_bits, private_key_info, etc.
174
       */
175
      Kyber_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
176
177
      std::unique_ptr<Public_Key> public_key() const override;
178
179
      secure_vector<uint8_t> private_key_bits() const override;
180
181
      secure_vector<uint8_t> raw_private_key_bits() const override;
182
183
      bool check_key(RandomNumberGenerator& rng, bool strong) const override;
184
185
      std::unique_ptr<PK_Ops::KEM_Decryption> create_kem_decryption_op(RandomNumberGenerator& rng,
186
                                                                       std::string_view params,
187
                                                                       std::string_view provider) const override;
188
189
      /**
190
       * The private key format from which the key was loaded. It is the format
191
       * used for the private_key_bits(), raw_private_key_bits() andFIPS
192
       * private_key_info() methods.
193
       *
194
       * Note that keys in Seed format can be serialized to Expanded format
195
       * using the method private_key_bits_with_format but NOT the other way
196
       * around.
197
       */
198
      MlPrivateKeyFormat private_key_format() const;
199
200
      /**
201
       * Encode the private key in the specified format. Note that the seed
202
       * format is only available for new ML-KEM keys and those loaded from
203
       * seeds.
204
       * @throws Encoding_Error if the private key cannot be encoded in the
205
       *         requested format.
206
       */
207
      secure_vector<uint8_t> private_key_bits_with_format(MlPrivateKeyFormat format) const;
208
209
   private:
210
      friend class Kyber_KEM_Decryptor;
211
212
      std::shared_ptr<const Kyber_PrivateKeyInternal> m_private;
213
};
214
215
BOTAN_DIAGNOSTIC_POP
216
217
}  // namespace Botan
218
219
#endif