Coverage Report

Created: 2023-02-13 06:21

/src/botan/build/include/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
#if !defined(BOTAN_HAS_KYBER_90S) && !defined(BOTAN_HAS_KYBER)
23
   static_assert(false, "botan module 'kyber_common' is useful only when enabling modules 'kyber', 'kyber_90s' or both");
24
#endif
25
26
namespace Botan {
27
28
class BOTAN_PUBLIC_API(3, 0) KyberMode
29
   {
30
   public:
31
      enum Mode
32
         {
33
         Kyber512,
34
         Kyber512_90s,
35
         Kyber768,
36
         Kyber768_90s,
37
         Kyber1024,
38
         Kyber1024_90s
39
         };
40
41
      KyberMode(Mode mode);
42
      explicit KyberMode(const OID& oid);
43
      explicit KyberMode(const std::string& str);
44
45
      OID object_identifier() const;
46
      std::string to_string() const;
47
48
0
      Mode mode() const { return m_mode; }
49
0
      bool is_90s() const { return m_mode == Kyber512_90s || m_mode == Kyber768_90s || m_mode == Kyber1024_90s; }
50
0
      bool is_modern() const { return !is_90s(); }
51
52
0
      bool operator==(const KyberMode& other) const { return m_mode == other.m_mode; }
53
0
      bool operator!=(const KyberMode& other) const { return !(*this == other); }
54
55
   private:
56
      Mode m_mode;
57
   };
58
59
enum class KyberKeyEncoding
60
   {
61
   Raw, // as implemented in the reference implementation
62
   Full // as described in draft-uni-qsckeys-00 Section 4.3 (private key), 4.5 (public key)
63
   };
64
65
class Kyber_PublicKeyInternal;
66
class Kyber_PrivateKeyInternal;
67
68
class BOTAN_PUBLIC_API(3, 0) Kyber_PublicKey : public virtual Public_Key
69
   {
70
   public:
71
      Kyber_PublicKey(const std::vector<uint8_t>& pub_key,
72
                      KyberMode mode,
73
                      KyberKeyEncoding encoding);
74
75
      Kyber_PublicKey(const AlgorithmIdentifier& alg_id,
76
                      const std::vector<uint8_t>& key_bits);
77
78
      Kyber_PublicKey(const Kyber_PublicKey& other);
79
80
      Kyber_PublicKey& operator=(const Kyber_PublicKey& other) = default;
81
82
0
      virtual ~Kyber_PublicKey() = default;
83
84
      std::string algo_name() const override;
85
86
      AlgorithmIdentifier algorithm_identifier() const override;
87
88
      OID object_identifier() const override;
89
90
      size_t key_length() const override;
91
92
      size_t estimated_strength() const override;
93
94
      std::vector<uint8_t> public_key_bits() const override;
95
96
      bool check_key(RandomNumberGenerator&, bool) const override;
97
98
      std::unique_ptr<PK_Ops::KEM_Encryption> create_kem_encryption_op(RandomNumberGenerator& rng,
99
            const std::string& params,
100
            const std::string& provider) const override;
101
102
      KyberMode mode() const;
103
104
      void set_binary_encoding(KyberKeyEncoding encoding)
105
0
         {
106
0
         m_key_encoding = encoding;
107
0
         }
108
      KyberKeyEncoding binary_encoding() const
109
0
         {
110
0
         return m_key_encoding;
111
0
         }
112
113
   protected:
114
0
      Kyber_PublicKey() {}
Unexecuted instantiation: Botan::Kyber_PublicKey::Kyber_PublicKey()
Unexecuted instantiation: Botan::Kyber_PublicKey::Kyber_PublicKey()
115
116
      void initialize_from_encoding(const std::vector<uint8_t>& pub_key,
117
                                    KyberMode m,
118
                                    KyberKeyEncoding encoding);
119
120
      std::vector<uint8_t> public_key_bits_raw() const;
121
      std::vector<uint8_t> public_key_bits_der() const;
122
123
   protected:
124
      friend class Kyber_KEM_Encryptor;
125
      friend class Kyber_KEM_Decryptor;
126
127
      std::shared_ptr<Kyber_PublicKeyInternal> m_public;
128
      KyberKeyEncoding m_key_encoding = KyberKeyEncoding::Full;
129
   };
130
131
class BOTAN_PUBLIC_API(3, 0) Kyber_PrivateKey final : public virtual Kyber_PublicKey, public virtual Private_Key
132
   {
133
   public:
134
      Kyber_PrivateKey(RandomNumberGenerator& rng, KyberMode mode);
135
136
      Kyber_PrivateKey(const secure_vector<uint8_t>& sk,
137
                       KyberMode mode,
138
                       KyberKeyEncoding encoding);
139
140
      Kyber_PrivateKey(const AlgorithmIdentifier& alg_id,
141
                       const secure_vector<uint8_t>& key_bits);
142
143
      std::unique_ptr<Public_Key> public_key() const override;
144
145
      secure_vector<uint8_t> private_key_bits() const override;
146
147
      std::unique_ptr<PK_Ops::KEM_Decryption> create_kem_decryption_op(RandomNumberGenerator& rng,
148
            const std::string& params,
149
            const std::string& provider) const override;
150
151
   private:
152
      secure_vector<uint8_t> private_key_bits_raw() const;
153
      secure_vector<uint8_t> private_key_bits_der() const;
154
155
   private:
156
      friend class Kyber_KEM_Decryptor;
157
158
      std::shared_ptr<Kyber_PrivateKeyInternal> m_private;
159
   };
160
161
} // namespace Botan
162
163
#endif