Coverage Report

Created: 2025-04-11 06:34

/src/botan/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 {
24
         Dilithium4x4 = 1,
25
         Dilithium4x4_AES BOTAN_DEPRECATED("Dilithium AES mode is deprecated"),
26
         Dilithium6x5,
27
         Dilithium6x5_AES BOTAN_DEPRECATED("Dilithium AES mode is deprecated"),
28
         Dilithium8x7,
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
0
      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
51
      bool is_dilithium_round3() const { return !is_ml_dsa(); }
49
50
      bool is_available() const;
51
52
51
      Mode mode() const { return m_mode; }
53
54
   private:
55
      Mode m_mode;
56
};
57
58
class Dilithium_PublicKeyInternal;
59
class 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
 */
67
class BOTAN_PUBLIC_API(3, 0) Dilithium_PublicKey : public virtual Public_Key {
68
   public:
69
      Dilithium_PublicKey& operator=(const Dilithium_PublicKey& other) = default;
70
71
0
      ~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
0
      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:
104
0
      Dilithium_PublicKey() = default;
105
106
      friend class Dilithium_Verification_Operation;
107
      friend class Dilithium_Signature_Operation;
108
109
      std::shared_ptr<Dilithium_PublicKeyInternal> m_public;
110
};
111
112
BOTAN_DIAGNOSTIC_PUSH
113
BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
114
115
class BOTAN_PUBLIC_API(3, 0) Dilithium_PrivateKey final : public virtual Dilithium_PublicKey,
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
       */
123
      Dilithium_PrivateKey(RandomNumberGenerator& rng, DilithiumMode mode);
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
154
BOTAN_DIAGNOSTIC_POP
155
156
}  // namespace Botan
157
158
#endif