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