/src/Botan-3.4.0/build/include/public/botan/dilithium.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Crystals Dilithium Digital Signature Algorithms |
3 | | * Based on the public domain reference implementation by the |
4 | | * designers (https://github.com/pq-crystals/dilithium) |
5 | | * |
6 | | * Further changes |
7 | | * (C) 2021-2023 Jack Lloyd |
8 | | * (C) 2021-2022 Manuel Glaser - Rohde & Schwarz Cybersecurity |
9 | | * (C) 2021-2023 Michael Boric, René Meusel - Rohde & Schwarz Cybersecurity |
10 | | * |
11 | | * Botan is released under the Simplified BSD License (see license.txt) |
12 | | */ |
13 | | |
14 | | #ifndef BOTAN_DILITHIUM_COMMON_H_ |
15 | | #define BOTAN_DILITHIUM_COMMON_H_ |
16 | | |
17 | | #include <botan/pk_keys.h> |
18 | | |
19 | | namespace Botan { |
20 | | |
21 | | class BOTAN_PUBLIC_API(3, 0) DilithiumMode { |
22 | | public: |
23 | | enum Mode { Dilithium4x4 = 1, Dilithium4x4_AES, Dilithium6x5, Dilithium6x5_AES, Dilithium8x7, Dilithium8x7_AES }; |
24 | | |
25 | | public: |
26 | 0 | DilithiumMode(Mode mode) : m_mode(mode) {} |
27 | | |
28 | | explicit DilithiumMode(const OID& oid); |
29 | | explicit DilithiumMode(std::string_view str); |
30 | | |
31 | | OID object_identifier() const; |
32 | | std::string to_string() const; |
33 | | |
34 | 0 | bool is_aes() const { |
35 | 0 | return m_mode == Dilithium4x4_AES || m_mode == Dilithium6x5_AES || m_mode == Dilithium8x7_AES; |
36 | 0 | } |
37 | | |
38 | 0 | bool is_modern() const { return !is_aes(); } |
39 | | |
40 | 0 | Mode mode() const { return m_mode; } |
41 | | |
42 | | private: |
43 | | Mode m_mode; |
44 | | }; |
45 | | |
46 | | class Dilithium_PublicKeyInternal; |
47 | | class Dilithium_PrivateKeyInternal; |
48 | | |
49 | | /** |
50 | | * This implementation is based on |
51 | | * https://github.com/pq-crystals/dilithium/commit/3e9b9f1412f6c7435dbeb4e10692ea58f181ee51 |
52 | | * |
53 | | * Note that this is _not_ compatible with the round 3 submission of the NIST competition. |
54 | | */ |
55 | | class BOTAN_PUBLIC_API(3, 0) Dilithium_PublicKey : public virtual Public_Key { |
56 | | public: |
57 | | Dilithium_PublicKey& operator=(const Dilithium_PublicKey& other) = default; |
58 | | |
59 | 0 | ~Dilithium_PublicKey() override = default; |
60 | | |
61 | | std::string algo_name() const override; |
62 | | |
63 | | AlgorithmIdentifier algorithm_identifier() const override; |
64 | | |
65 | | OID object_identifier() const override; |
66 | | |
67 | | size_t key_length() const override; |
68 | | |
69 | | size_t estimated_strength() const override; |
70 | | |
71 | | std::vector<uint8_t> public_key_bits() const override; |
72 | | |
73 | | bool check_key(RandomNumberGenerator&, bool) const override; |
74 | | |
75 | 0 | bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::Signature); } |
76 | | |
77 | | std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final; |
78 | | |
79 | | Dilithium_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> pk); |
80 | | |
81 | | Dilithium_PublicKey(std::span<const uint8_t> pk, DilithiumMode mode); |
82 | | |
83 | | std::unique_ptr<PK_Ops::Verification> create_verification_op(std::string_view params, |
84 | | std::string_view provider) const override; |
85 | | |
86 | | std::unique_ptr<PK_Ops::Verification> create_x509_verification_op(const AlgorithmIdentifier& signature_algorithm, |
87 | | std::string_view provider) const override; |
88 | | |
89 | | protected: |
90 | 0 | Dilithium_PublicKey() = default; |
91 | | |
92 | | friend class Dilithium_Verification_Operation; |
93 | | friend class Dilithium_Signature_Operation; |
94 | | |
95 | | std::shared_ptr<Dilithium_PublicKeyInternal> m_public; |
96 | | }; |
97 | | |
98 | | BOTAN_DIAGNOSTIC_PUSH |
99 | | BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE |
100 | | |
101 | | class BOTAN_PUBLIC_API(3, 0) Dilithium_PrivateKey final : public virtual Dilithium_PublicKey, |
102 | | public virtual Botan::Private_Key { |
103 | | public: |
104 | | std::unique_ptr<Public_Key> public_key() const override; |
105 | | |
106 | | /** |
107 | | * Generates a new key pair |
108 | | */ |
109 | | Dilithium_PrivateKey(RandomNumberGenerator& rng, DilithiumMode mode); |
110 | | |
111 | | /** |
112 | | * Read an encoded private key. |
113 | | */ |
114 | | Dilithium_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> sk); |
115 | | |
116 | | /** |
117 | | * Read an encoded private key given the dilithium @p mode. |
118 | | */ |
119 | | Dilithium_PrivateKey(std::span<const uint8_t> sk, DilithiumMode mode); |
120 | | |
121 | | secure_vector<uint8_t> private_key_bits() const override; |
122 | | |
123 | | secure_vector<uint8_t> raw_private_key_bits() const override; |
124 | | |
125 | | /** |
126 | | * Create a signature operation that produces a Dilithium signature either |
127 | | * with "Randomized" or "Deterministic" rhoprime. Pass either of those |
128 | | * strings as @p params. Default (i.e. empty @p params is "Randomized"). |
129 | | */ |
130 | | std::unique_ptr<PK_Ops::Signature> create_signature_op(RandomNumberGenerator&, |
131 | | std::string_view params, |
132 | | std::string_view provider) const override; |
133 | | |
134 | | private: |
135 | | friend class Dilithium_Signature_Operation; |
136 | | |
137 | | std::shared_ptr<Dilithium_PrivateKeyInternal> m_private; |
138 | | }; |
139 | | |
140 | | BOTAN_DIAGNOSTIC_POP |
141 | | |
142 | | } // namespace Botan |
143 | | |
144 | | #endif |