• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 21944630505

12 Feb 2026 11:24AM UTC coverage: 90.076% (+0.01%) from 90.063%
21944630505

Pull #5307

github

web-flow
Merge c4cab7420 into f97d7db3f
Pull Request #5307: Draft: Fix ML-DSA private key encoding

102323 of 113596 relevant lines covered (90.08%)

11536245.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.47
/src/tests/test_dilithium.cpp
1
/*
2
 * Tests for Crystals Dilithium
3
 * - KAT tests using the KAT vectors from
4
 *   https://csrc.nist.gov/CSRC/media/Projects/post-quantum-cryptography/documents/round-3/submissions/Dilithium-Round3.zip
5
 *
6
 * (C) 2022,2023 Jack Lloyd
7
 * (C) 2022 Manuel Glaser, Michael Boric, René Meusel - Rohde & Schwarz Cybersecurity
8
 *
9
 * Botan is released under the Simplified BSD License (see license.txt)
10
 */
11

12
#include "botan/pk_keys.h"
13
#include "test_rng.h"
14
#include "tests.h"
15
#include <memory>
16
#include <vector>
17

18
#if defined(BOTAN_HAS_DILITHIUM_COMMON)
19
   #include <botan/dilithium.h>
20
   #include <botan/hash.h>
21
   #include <botan/pk_algs.h>
22
   #include <botan/pubkey.h>
23

24
   #include "test_pubkey.h"
25
   #include "test_rng.h"
26
#endif
27

28
namespace Botan_Tests {
29

30
#if defined(BOTAN_HAS_DILITHIUM_COMMON) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_SHA3)
31

32
template <typename DerivedT>
33
class Dilithium_KAT_Tests : public Text_Based_Test {
×
34
   public:
35
      // NOLINTNEXTLINE(*crtp-constructor-accessibility)
36
      Dilithium_KAT_Tests() : Text_Based_Test(DerivedT::test_vector, "Seed,Msg,HashPk,HashSk,HashSig", "Sig") {}
36✔
37

38
      Test::Result run_one_test(const std::string& name, const VarMap& vars) override {
1,350✔
39
         Test::Result result(name);
2,700✔
40

41
         // read input from test file
42
         const auto ref_seed = vars.get_req_bin("Seed");
2,700✔
43
         const auto ref_msg = vars.get_req_bin("Msg");
2,700✔
44
         const auto ref_pk_hash = vars.get_req_bin("HashPk");
2,700✔
45
         const auto ref_sk_hash = vars.get_req_bin("HashSk");
2,700✔
46
         const auto ref_sig_hash = vars.get_req_bin("HashSig");
2,700✔
47
         const auto ref_sig = vars.get_opt_bin("Sig");
2,700✔
48

49
         auto sha3_256 = Botan::HashFunction::create_or_throw("SHA-3(256)");
1,350✔
50

51
         auto dilithium_test_rng = std::make_unique<CTR_DRBG_AES256>(ref_seed);
1,350✔
52

53
         const Botan::Dilithium_PrivateKey priv_key(*dilithium_test_rng, DerivedT::mode);
1,350✔
54

55
         if(!priv_key.is_mldsa()) {
1,350✔
56
            result.test_eq(
4,800✔
57
               "generated expected private key hash", sha3_256->process(priv_key.private_key_bits()), ref_sk_hash);
1,200✔
58
         }
59

60
         result.test_eq(
5,400✔
61
            "generated expected public key hash", sha3_256->process(priv_key.public_key_bits()), ref_pk_hash);
1,350✔
62

63
         auto signer = Botan::PK_Signer(priv_key, *dilithium_test_rng, DerivedT::sign_param);
1,350✔
64
         auto signature = signer.sign_message(ref_msg.data(), ref_msg.size(), *dilithium_test_rng);
2,700✔
65

66
         result.test_eq("generated expected signature hash", sha3_256->process(signature), ref_sig_hash);
4,050✔
67
         if(!ref_sig.empty()) {
1,350✔
68
            result.test_eq("generated expected signature", signature, ref_sig);
24✔
69
         }
70

71
         const Botan::Dilithium_PublicKey pub_key(priv_key.public_key_bits(), DerivedT::mode);
2,700✔
72
         auto verifier = Botan::PK_Verifier(pub_key, "");
1,350✔
73
         verifier.update(ref_msg.data(), ref_msg.size());
1,350✔
74
         result.confirm("signature verifies", verifier.check_signature(signature.data(), signature.size()));
2,700✔
75

76
         // test validating incorrect wrong signature
77
         auto mutated_signature = Test::mutate_vec(signature, this->rng());
1,350✔
78
         result.confirm("invalid signature rejected",
1,350✔
79
                        !verifier.check_signature(mutated_signature.data(), mutated_signature.size()));
1,350✔
80

81
         verifier.update(ref_msg.data(), ref_msg.size());
1,350✔
82
         result.confirm("signature verifies", verifier.check_signature(signature.data(), signature.size()));
2,700✔
83

84
         return result;
1,350✔
85
      }
12,162✔
86
};
87

88
   // NOLINTNEXTLINE(*-macro-usage)
89
   #define REGISTER_DILITHIUM_KAT_TEST(m, rand)                                          \
90
      class DILITHIUM##m##rand final : public Dilithium_KAT_Tests<DILITHIUM##m##rand> {  \
91
         public:                                                                         \
92
            constexpr static auto test_vector = "pubkey/dilithium_" #m "_" #rand ".vec"; \
93
            constexpr static auto mode = Botan::DilithiumMode::Dilithium##m;             \
94
            constexpr static auto sign_param = #rand;                                    \
95
      };                                                                                 \
96
      BOTAN_REGISTER_TEST("pubkey", "dilithium_kat_" #m "_" #rand, DILITHIUM##m##rand)
97

98
   // NOLINTNEXTLINE(*-macro-usage)
99
   #define REGISTER_ML_DSA_KAT_TEST(m, rand)                                          \
100
      class ML_DSA##m##rand final : public Dilithium_KAT_Tests<ML_DSA##m##rand> {     \
101
         public:                                                                      \
102
            constexpr static auto test_vector = "pubkey/ml-dsa-" #m "_" #rand ".vec"; \
103
            constexpr static auto mode = Botan::DilithiumMode::ML_DSA_##m;            \
104
            constexpr static auto sign_param = #rand;                                 \
105
      };                                                                              \
106
      BOTAN_REGISTER_TEST("pubkey", "ml-dsa_kat_" #m "_" #rand, ML_DSA##m##rand)
107

108
   #if defined(BOTAN_HAS_DILITHIUM)
109
REGISTER_DILITHIUM_KAT_TEST(4x4, Deterministic);
1✔
110
REGISTER_DILITHIUM_KAT_TEST(6x5, Deterministic);
1✔
111
REGISTER_DILITHIUM_KAT_TEST(8x7, Deterministic);
1✔
112
REGISTER_DILITHIUM_KAT_TEST(4x4, Randomized);
1✔
113
REGISTER_DILITHIUM_KAT_TEST(6x5, Randomized);
1✔
114
REGISTER_DILITHIUM_KAT_TEST(8x7, Randomized);
1✔
115
   #endif
116

117
   #if defined(BOTAN_HAS_DILITHIUM_AES)
118
REGISTER_DILITHIUM_KAT_TEST(4x4_AES, Deterministic);
1✔
119
REGISTER_DILITHIUM_KAT_TEST(6x5_AES, Deterministic);
1✔
120
REGISTER_DILITHIUM_KAT_TEST(8x7_AES, Deterministic);
1✔
121
REGISTER_DILITHIUM_KAT_TEST(4x4_AES, Randomized);
1✔
122
REGISTER_DILITHIUM_KAT_TEST(6x5_AES, Randomized);
1✔
123
REGISTER_DILITHIUM_KAT_TEST(8x7_AES, Randomized);
1✔
124
   #endif
125

126
   #if defined(BOTAN_HAS_ML_DSA)
127
REGISTER_ML_DSA_KAT_TEST(4x4, Deterministic);
1✔
128
REGISTER_ML_DSA_KAT_TEST(6x5, Deterministic);
1✔
129
REGISTER_ML_DSA_KAT_TEST(8x7, Deterministic);
1✔
130
REGISTER_ML_DSA_KAT_TEST(4x4, Randomized);
1✔
131
REGISTER_ML_DSA_KAT_TEST(6x5, Randomized);
1✔
132
REGISTER_ML_DSA_KAT_TEST(8x7, Randomized);
1✔
133
   #endif
134

135
class DilithiumRoundtripTests final : public Test {
1✔
136
   public:
137
      static Test::Result run_roundtrip(
18✔
138
         const char* test_name, Botan::DilithiumMode mode, bool randomized, size_t strength, size_t psid) {
139
         Test::Result result(test_name);
18✔
140
         if(!mode.is_available()) {
18✔
141
            result.note_missing(mode.to_string());
×
142
            return result;
×
143
         }
144

145
         auto rng = Test::new_rng(test_name);
18✔
146

147
         auto sign = [randomized, &rng](const auto& private_key, const auto& msg) {
72✔
148
            const std::string param = (randomized) ? "Randomized" : "Deterministic";
81✔
149
            auto signer = Botan::PK_Signer(private_key, *rng, param);
54✔
150
            return signer.sign_message(msg, *rng);
54✔
151
         };
72✔
152

153
         auto verify = [](const auto& public_key, const auto& msg, const auto& signature) {
288✔
154
            auto verifier = Botan::PK_Verifier(public_key, "");
270✔
155
            verifier.update(msg);
270✔
156
            return verifier.check_signature(signature);
540✔
157
         };
270✔
158

159
         const std::string msg = "The quick brown fox jumps over the lazy dog.";
18✔
160
         const std::vector<uint8_t> msgvec(msg.data(), msg.data() + msg.size());
18✔
161

162
         const Botan::Dilithium_PrivateKey priv_key(*rng, mode);
18✔
163
         const Botan::Dilithium_PublicKey& pub_key = priv_key;
18✔
164

165
         result.test_eq("key strength", priv_key.estimated_strength(), strength);
18✔
166
         result.test_eq("key length", priv_key.key_length(), psid);
18✔
167
         result.test_eq("key strength", pub_key.estimated_strength(), strength);
18✔
168
         result.test_eq("key length", pub_key.key_length(), psid);
18✔
169

170
         const auto sig_before_codec = sign(priv_key, msgvec);
18✔
171

172
         const auto priv_key_encoded = priv_key.private_key_bits();
18✔
173
         const auto pub_key_encoded = priv_key.public_key_bits();
18✔
174

175
         const Botan::Dilithium_PrivateKey priv_key_decoded(priv_key_encoded, mode);
18✔
176
         const Botan::Dilithium_PublicKey pub_key_decoded(pub_key_encoded, mode);
18✔
177

178
         const auto sig_after_codec = sign(priv_key_decoded, msgvec);
18✔
179

180
         result.confirm("Pubkey: before,   Sig: before", verify(pub_key, msgvec, sig_before_codec));
36✔
181
         result.confirm("Pubkey: before,   Sig: after", verify(pub_key, msgvec, sig_after_codec));
36✔
182
         result.confirm("Pubkey: after,    Sig: after", verify(pub_key_decoded, msgvec, sig_after_codec));
36✔
183
         result.confirm("Pubkey: after,    Sig: before", verify(pub_key_decoded, msgvec, sig_before_codec));
36✔
184
         result.confirm("Pubkey: recalc'ed Sig: before", verify(priv_key_decoded, msgvec, sig_before_codec));
36✔
185
         result.confirm("Pubkey: recalc'ed Sig: after", verify(priv_key_decoded, msgvec, sig_after_codec));
36✔
186

187
         auto tampered_msgvec = msgvec;
18✔
188
         tampered_msgvec.front() = 'X';
18✔
189
         result.confirm("Pubkey: before,   Broken Sig: before", !verify(pub_key, tampered_msgvec, sig_before_codec));
36✔
190
         result.confirm("Pubkey: before,   Broken Sig: after", !verify(pub_key, tampered_msgvec, sig_after_codec));
36✔
191
         result.confirm("Pubkey: after,    Broken Sig: after",
18✔
192
                        !verify(pub_key_decoded, tampered_msgvec, sig_after_codec));
18✔
193
         result.confirm("Pubkey: after,    Broken Sig: before",
18✔
194
                        !verify(pub_key_decoded, tampered_msgvec, sig_before_codec));
18✔
195
         result.confirm("Pubkey: recalc'ed Sig: before", !verify(priv_key_decoded, tampered_msgvec, sig_before_codec));
36✔
196
         result.confirm("Pubkey: recalc'ed Sig: after", !verify(priv_key_decoded, tampered_msgvec, sig_after_codec));
36✔
197

198
         // decoding via generic pk_algs.h
199
         const auto generic_pubkey_decoded = Botan::load_public_key(pub_key.algorithm_identifier(), pub_key_encoded);
18✔
200
         const auto generic_privkey_decoded =
18✔
201
            Botan::load_private_key(priv_key.algorithm_identifier(), priv_key_encoded);
18✔
202

203
         result.test_not_null("generic pubkey", generic_pubkey_decoded);
18✔
204
         result.test_not_null("generic privkey", generic_privkey_decoded);
18✔
205

206
         const auto sig_after_generic_codec = sign(*generic_privkey_decoded, msgvec);
18✔
207

208
         result.confirm("verification with generic public key",
36✔
209
                        verify(*generic_pubkey_decoded, msgvec, sig_before_codec));
18✔
210
         result.confirm("verification of signature with generic private key",
36✔
211
                        verify(*generic_pubkey_decoded, msgvec, sig_after_generic_codec));
18✔
212
         result.confirm("verification with generic private key",
36✔
213
                        verify(*generic_privkey_decoded, msgvec, sig_before_codec));
18✔
214

215
         return result;
18✔
216
      }
162✔
217

218
      std::vector<Test::Result> run() override {
1✔
219
         return {
1✔
220
            run_roundtrip("Dilithium_4x4_Common", Botan::DilithiumMode::Dilithium4x4, false, 128, 44),
221
            run_roundtrip("Dilithium_6x5_Common", Botan::DilithiumMode::Dilithium6x5, false, 192, 65),
222
            run_roundtrip("Dilithium_8x7_Common", Botan::DilithiumMode::Dilithium8x7, false, 256, 87),
223
            run_roundtrip("Dilithium_4x4_Common_Randomized", Botan::DilithiumMode::Dilithium4x4, true, 128, 44),
224
            run_roundtrip("Dilithium_6x5_Common_Randomized", Botan::DilithiumMode::Dilithium6x5, true, 192, 65),
225
            run_roundtrip("Dilithium_8x7_Common_Randomized", Botan::DilithiumMode::Dilithium8x7, true, 256, 87),
226
            run_roundtrip("Dilithium_4x4_AES", Botan::DilithiumMode::Dilithium4x4_AES, false, 128, 44),
227
            run_roundtrip("Dilithium_6x5_AES", Botan::DilithiumMode::Dilithium6x5_AES, false, 192, 65),
228
            run_roundtrip("Dilithium_8x7_AES", Botan::DilithiumMode::Dilithium8x7_AES, false, 256, 87),
229
            run_roundtrip("Dilithium_4x4_AES_Randomized", Botan::DilithiumMode::Dilithium4x4_AES, true, 128, 44),
230
            run_roundtrip("Dilithium_6x5_AES_Randomized", Botan::DilithiumMode::Dilithium6x5_AES, true, 192, 65),
231
            run_roundtrip("Dilithium_8x7_AES_Randomized", Botan::DilithiumMode::Dilithium8x7_AES, true, 256, 87),
232
            run_roundtrip("ML-DSA_4x4", Botan::DilithiumMode::ML_DSA_4x4, false, 128, 44),
233
            run_roundtrip("ML-DSA_6x5", Botan::DilithiumMode::ML_DSA_6x5, false, 192, 65),
234
            run_roundtrip("ML-DSA_8x7", Botan::DilithiumMode::ML_DSA_8x7, false, 256, 87),
235
            run_roundtrip("ML-DSA_4x4_Randomized", Botan::DilithiumMode::ML_DSA_4x4, true, 128, 44),
236
            run_roundtrip("ML-DSA_6x5_Randomized", Botan::DilithiumMode::ML_DSA_6x5, true, 192, 65),
237
            run_roundtrip("ML-DSA_8x7_Randomized", Botan::DilithiumMode::ML_DSA_8x7, true, 256, 87),
238
         };
19✔
239
      }
2✔
240
};
241

242
BOTAN_REGISTER_TEST("pubkey", "dilithium_roundtrips", DilithiumRoundtripTests);
243

244
class Dilithium_Keygen_Tests final : public PK_Key_Generation_Test {
1✔
245
   public:
246
      std::vector<std::string> keygen_params() const override {
1✔
247
         const std::vector<std::string> all_instances = {
1✔
248
            "Dilithium-4x4-AES-r3",
249
            "Dilithium-6x5-AES-r3",
250
            "Dilithium-8x7-AES-r3",
251
            "Dilithium-4x4-r3",
252
            "Dilithium-6x5-r3",
253
            "Dilithium-8x7-r3",
254
            "ML-DSA-4x4",
255
            "ML-DSA-6x5",
256
            "ML-DSA-8x7",
257
         };
1✔
258

259
         std::vector<std::string> available_instances;
1✔
260

261
         for(const auto& mode : all_instances) {
10✔
262
            if(Botan::DilithiumMode(mode).is_available()) {
9✔
263
               available_instances.push_back(mode);
9✔
264
            }
265
         }
266
         return available_instances;
1✔
267
      }
1✔
268

269
      std::string algo_name(std::string_view param) const override {
9✔
270
         if(param.starts_with("Dilithium-")) {
9✔
271
            return "Dilithium";
6✔
272
         } else {
273
            return "ML-DSA";
3✔
274
         }
275
      }
276

277
      std::string algo_name() const override { throw Test_Error("No default algo name set for Dilithium"); }
×
278

279
      std::unique_ptr<Botan::Public_Key> public_key_from_raw(std::string_view keygen_params,
9✔
280
                                                             std::string_view /* provider */,
281
                                                             std::span<const uint8_t> raw_pk) const override {
282
         return std::make_unique<Botan::Dilithium_PublicKey>(raw_pk, Botan::DilithiumMode(keygen_params));
9✔
283
      }
284
};
285

286
BOTAN_REGISTER_TEST("pubkey", "dilithium_keygen", Dilithium_Keygen_Tests);
287

288
#endif
289

290
#if defined(BOTAN_HAS_DILITHIUM_COMMON) && defined(BOTAN_HAS_SHA3)
291
class MLDSA_Privkey_Tests : public Text_Based_Test {
292
   public:
293
      MLDSA_Privkey_Tests() : Text_Based_Test("mldsa_privkey.vec", "key") {}
2✔
294

295
      Test::Result run_one_test(const std::string& name, const VarMap& vars) override {
12✔
296
         Test::Result result(name);
12✔
297
         const std::vector<uint8_t> key_bits = vars.get_req_bin("key");
12✔
298
         bool expect_decoding_failure = false;
12✔
299
         Botan::DilithiumMode mode = Botan::DilithiumMode::ML_DSA_4x4;
12✔
300
         if(name.starts_with("mldsa-44")) {
12✔
301
            mode = Botan::DilithiumMode::ML_DSA_4x4;
7✔
302
         } else if(name.starts_with("mldsa-65")) {
5✔
303
            mode = Botan::DilithiumMode::ML_DSA_6x5;
3✔
304
         } else if(name.starts_with("mldsa-87")) {
2✔
305
            mode = Botan::DilithiumMode::ML_DSA_8x7;
2✔
306
         } else {
307
            throw Botan_Tests::Test_Error(
×
308
               "internal error for ML-DSA test vector: encountered unknown ml-dsa mode string (test-case-specific token)");
×
309
         }
310
         if(name.ends_with("-invalid")) {
12✔
311
            expect_decoding_failure = true;
312
         }
313
         std::unique_ptr<Botan::Dilithium_PrivateKey> priv_key;
12✔
314
         try {
12✔
315
            priv_key = std::make_unique<Botan::Dilithium_PrivateKey>(key_bits, mode);
20✔
316
         } catch(const Botan::Decoding_Error& e) {
4✔
317
            result.confirm("invalid ML-DSA key rejected", expect_decoding_failure);
8✔
318
            return result;
4✔
319
         }
4✔
320
         std::vector<uint8_t> ref_msg = {0, 1, 2, 4};
8✔
321
         std::vector<uint8_t> rng_seed(48);
8✔
322
         Botan_Tests::CTR_DRBG_AES256 rng(rng_seed);
8✔
323
         auto signer = Botan::PK_Signer(*priv_key, rng, "Randomized");
8✔
324
         auto signature = signer.sign_message(ref_msg.data(), ref_msg.size(), rng);
8✔
325

326
         const Botan::Dilithium_PublicKey pub_key(priv_key->public_key_bits(), mode);
8✔
327
         auto verifier = Botan::PK_Verifier(pub_key, "");
8✔
328
         verifier.update(ref_msg.data(), ref_msg.size());
8✔
329
         result.confirm("signature verifies", verifier.check_signature(signature.data(), signature.size()));
16✔
330

331
         auto reencoded_priv_key = priv_key->private_key_bits();
8✔
332
         auto redecoded_priv_key = Botan::Dilithium_PrivateKey(reencoded_priv_key, mode);
8✔
333
         result.confirm("re-encoding and subsequent re-decoding of private ML-DSA key without error", true);
16✔
334
         return result;
8✔
335
      }
56✔
336
};
337

338
BOTAN_REGISTER_TEST("pubkey", "mldsa_private_key", MLDSA_Privkey_Tests);
339

340
#endif
341

342
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc