Botan 3.5.0
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 { Dilithium4x4 = 1, Dilithium4x4_AES, Dilithium6x5, Dilithium6x5_AES, Dilithium8x7, Dilithium8x7_AES };
24
25 public:
26 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 bool is_aes() const {
35 return m_mode == Dilithium4x4_AES || m_mode == Dilithium6x5_AES || m_mode == Dilithium8x7_AES;
36 }
37
38 bool is_modern() const { return !is_aes(); }
39
40 Mode mode() const { return m_mode; }
41
42 private:
43 Mode m_mode;
44};
45
46class Dilithium_PublicKeyInternal;
47class 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 */
55class BOTAN_PUBLIC_API(3, 0) Dilithium_PublicKey : public virtual Public_Key {
56 public:
58
59 ~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> raw_public_key_bits() const override;
72
73 std::vector<uint8_t> public_key_bits() const override;
74
75 bool check_key(RandomNumberGenerator&, bool) const override;
76
77 bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::Signature); }
78
79 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
80
81 Dilithium_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> pk);
82
83 Dilithium_PublicKey(std::span<const uint8_t> pk, DilithiumMode mode);
84
85 std::unique_ptr<PK_Ops::Verification> create_verification_op(std::string_view params,
86 std::string_view provider) const override;
87
88 std::unique_ptr<PK_Ops::Verification> create_x509_verification_op(const AlgorithmIdentifier& signature_algorithm,
89 std::string_view provider) const override;
90
91 protected:
93
94 friend class Dilithium_Verification_Operation;
95 friend class Dilithium_Signature_Operation;
96
97 std::shared_ptr<Dilithium_PublicKeyInternal> m_public;
98};
99
102
104 public virtual Botan::Private_Key {
105 public:
106 std::unique_ptr<Public_Key> public_key() const override;
107
108 /**
109 * Generates a new key pair
110 */
112
113 /**
114 * Read an encoded private key.
115 */
116 Dilithium_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> sk);
117
118 /**
119 * Read an encoded private key given the dilithium @p mode.
120 */
121 Dilithium_PrivateKey(std::span<const uint8_t> sk, DilithiumMode mode);
122
123 secure_vector<uint8_t> private_key_bits() const override;
124
125 secure_vector<uint8_t> raw_private_key_bits() const override;
126
127 /**
128 * Create a signature operation that produces a Dilithium signature either
129 * with "Randomized" or "Deterministic" rhoprime. Pass either of those
130 * strings as @p params. Default (i.e. empty @p params is "Randomized").
131 */
132 std::unique_ptr<PK_Ops::Signature> create_signature_op(RandomNumberGenerator&,
133 std::string_view params,
134 std::string_view provider) const override;
135
136 private:
137 friend class Dilithium_Signature_Operation;
138
139 std::shared_ptr<Dilithium_PrivateKeyInternal> m_private;
140};
141
143
144} // namespace Botan
145
146#endif
Mode mode() const
Definition dilithium.h:40
DilithiumMode(Mode mode)
Definition dilithium.h:26
bool is_aes() const
Definition dilithium.h:34
bool is_modern() const
Definition dilithium.h:38
bool supports_operation(PublicKeyOperation op) const override
Definition dilithium.h:77
~Dilithium_PublicKey() override=default
Dilithium_PublicKey & operator=(const Dilithium_PublicKey &other)=default
std::shared_ptr< Dilithium_PublicKeyInternal > m_public
Definition dilithium.h:97
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
PublicKeyOperation
Definition pk_keys.h:45
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61