Coverage Report

Created: 2025-07-11 06:15

/src/Botan-3.4.0/build/include/public/botan/kyber.h
Line
Count
Source (jump to first uncovered line)
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
28
namespace Botan {
29
30
class BOTAN_PUBLIC_API(3, 0) KyberMode {
31
   public:
32
      enum Mode {
33
         // Kyber512 as proposed in round 3 of the NIST competition
34
         Kyber512_R3,
35
         // Kyber768 as proposed in round 3 of the NIST competition
36
         Kyber768_R3,
37
         // Kyber1024 as proposed in round 3 of the NIST competition
38
         Kyber1024_R3,
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
0
      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
0
      bool operator==(const KyberMode& other) const { return m_mode == other.m_mode; }
65
66
0
      bool operator!=(const KyberMode& other) const { return !(*this == other); }
67
68
   private:
69
      Mode m_mode;
70
};
71
72
class Kyber_PublicKeyInternal;
73
class Kyber_PrivateKeyInternal;
74
75
class 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
0
      ~Kyber_PublicKey() override = default;
86
87
0
      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
0
      bool supports_operation(PublicKeyOperation op) const override {
104
0
         return (op == PublicKeyOperation::KeyEncapsulation);
105
0
      }
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
0
      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
128
BOTAN_DIAGNOSTIC_PUSH
129
BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
130
131
class BOTAN_PUBLIC_API(3, 0) Kyber_PrivateKey final : public virtual Kyber_PublicKey,
132
                                                      public virtual Private_Key {
133
   public:
134
      Kyber_PrivateKey(RandomNumberGenerator& rng, KyberMode mode);
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
156
BOTAN_DIAGNOSTIC_POP
157
158
}  // namespace Botan
159
160
#endif