/src/Botan-3.13.0/build/include/public/botan/dilithium.h
Line | Count | Source |
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 final { |
22 | | public: |
23 | | enum Mode : uint8_t /* NOLINT(*-use-enum-class) */ { |
24 | | Dilithium4x4 BOTAN_DEPRECATED("Dilithium R3 is deprecated - use ML-DSA") = 1, |
25 | | Dilithium4x4_AES BOTAN_DEPRECATED("Dilithium AES mode is deprecated"), |
26 | | Dilithium6x5 BOTAN_DEPRECATED("Dilithium R3 is deprecated - use ML-DSA"), |
27 | | Dilithium6x5_AES BOTAN_DEPRECATED("Dilithium AES mode is deprecated"), |
28 | | Dilithium8x7 BOTAN_DEPRECATED("Dilithium R3 is deprecated - use ML-DSA"), |
29 | | Dilithium8x7_AES BOTAN_DEPRECATED("Dilithium AES mode is deprecated"), |
30 | | ML_DSA_4x4, |
31 | | ML_DSA_6x5, |
32 | | ML_DSA_8x7, |
33 | | }; |
34 | | |
35 | | public: |
36 | | // NOLINTNEXTLINE(*-explicit-conversions) |
37 | 0 | DilithiumMode(Mode mode) : m_mode(mode) {} |
38 | | |
39 | | explicit DilithiumMode(const OID& oid); |
40 | | explicit DilithiumMode(std::string_view str); |
41 | | |
42 | | OID object_identifier() const; |
43 | | std::string to_string() const; |
44 | | |
45 | | BOTAN_DEPRECATED("Dilithium AES mode is deprecated") bool is_aes() const; |
46 | | BOTAN_DEPRECATED("Dilithium AES mode is deprecated") bool is_modern() const; |
47 | | bool is_ml_dsa() const; |
48 | | |
49 | 8.20k | bool is_dilithium_round3() const { return !is_ml_dsa(); } |
50 | | |
51 | | bool is_available() const; |
52 | | |
53 | 12.3k | Mode mode() const { return m_mode; } |
54 | | |
55 | | private: |
56 | | Mode m_mode; |
57 | | }; |
58 | | |
59 | | class Dilithium_PublicKeyInternal; |
60 | | class Dilithium_PrivateKeyInternal; |
61 | | |
62 | | /** |
63 | | * This implementation is based on |
64 | | * https://github.com/pq-crystals/dilithium/commit/3e9b9f1412f6c7435dbeb4e10692ea58f181ee51 |
65 | | * |
66 | | * Note that this is _not_ compatible with the round 3 submission of the NIST competition. |
67 | | */ |
68 | | class BOTAN_PUBLIC_API(3, 0) Dilithium_PublicKey : public virtual Public_Key { |
69 | | public: |
70 | | std::string algo_name() const override; |
71 | | |
72 | | AlgorithmIdentifier algorithm_identifier() const override; |
73 | | |
74 | | OID object_identifier() const override; |
75 | | |
76 | | size_t key_length() const override; |
77 | | |
78 | | size_t estimated_strength() const override; |
79 | | |
80 | | std::vector<uint8_t> raw_public_key_bits() const override; |
81 | | |
82 | | std::vector<uint8_t> public_key_bits() const override; |
83 | | |
84 | | bool check_key(RandomNumberGenerator& rng, bool strong) const override; |
85 | | |
86 | 0 | bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::Signature); } |
87 | | |
88 | | std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final; |
89 | | |
90 | | Dilithium_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> pk); |
91 | | |
92 | | Dilithium_PublicKey(std::span<const uint8_t> pk, DilithiumMode mode); |
93 | | |
94 | | std::unique_ptr<PK_Ops::Verification> create_verification_op(std::string_view params, |
95 | | std::string_view provider) const override; |
96 | | |
97 | | std::unique_ptr<PK_Ops::Verification> create_x509_verification_op(const AlgorithmIdentifier& signature_algorithm, |
98 | | std::string_view provider) const override; |
99 | | |
100 | | protected: |
101 | 0 | Dilithium_PublicKey() = default; |
102 | | |
103 | | friend class Dilithium_Verification_Operation; |
104 | | friend class Dilithium_Signature_Operation; |
105 | | |
106 | | std::shared_ptr<const Dilithium_PublicKeyInternal> m_public; // NOLINT(*non-private-member-variable*) |
107 | | }; |
108 | | |
109 | | BOTAN_DIAGNOSTIC_PUSH |
110 | | BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE |
111 | | |
112 | | class BOTAN_PUBLIC_API(3, 0) Dilithium_PrivateKey final : public virtual Dilithium_PublicKey, |
113 | | public virtual Botan::Private_Key { |
114 | | public: |
115 | | std::unique_ptr<Public_Key> public_key() const override; |
116 | | |
117 | | bool check_key(RandomNumberGenerator& rng, bool strong) const override; |
118 | | |
119 | | /** |
120 | | * Generates a new key pair |
121 | | */ |
122 | | Dilithium_PrivateKey(RandomNumberGenerator& rng, DilithiumMode mode); |
123 | | |
124 | | /** |
125 | | * Read an encoded private key. |
126 | | */ |
127 | | Dilithium_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> sk); |
128 | | |
129 | | /** |
130 | | * Read an encoded private key given the dilithium @p mode. |
131 | | */ |
132 | | Dilithium_PrivateKey(std::span<const uint8_t> sk, DilithiumMode mode); |
133 | | |
134 | | secure_vector<uint8_t> private_key_bits() const override; |
135 | | |
136 | | secure_vector<uint8_t> raw_private_key_bits() const override; |
137 | | |
138 | | /** |
139 | | * Create a signature operation that produces a Dilithium signature either |
140 | | * with "Randomized" or "Deterministic" rhoprime. Pass either of those |
141 | | * strings as @p params. Default (i.e. empty @p params is "Randomized"). |
142 | | */ |
143 | | std::unique_ptr<PK_Ops::Signature> create_signature_op(RandomNumberGenerator& rng, |
144 | | std::string_view params, |
145 | | std::string_view provider) const override; |
146 | | |
147 | | private: |
148 | | friend class Dilithium_Signature_Operation; |
149 | | |
150 | | std::shared_ptr<const Dilithium_PrivateKeyInternal> m_private; |
151 | | }; |
152 | | |
153 | | BOTAN_DIAGNOSTIC_POP |
154 | | |
155 | | } // namespace Botan |
156 | | |
157 | | #endif |