Botan 3.6.0
Crypto and TLS for C&
ml_dsa_impl.h
Go to the documentation of this file.
1/*
2* Asymmetric primitives for ML-DSA
3* (C) 2024 Jack Lloyd
4* (C) 2024 Fabian Albert, René Meusel - Rohde & Schwarz Cybersecurity
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#ifndef BOTAN_ML_DSA_SYM_PRIMITIVES_H_
10#define BOTAN_ML_DSA_SYM_PRIMITIVES_H_
11
12#include <botan/internal/dilithium_symmetric_primitives.h>
13
14#include <botan/rng.h>
15#include <botan/internal/dilithium_keys.h>
16#include <botan/internal/dilithium_shake_xof.h>
17#include <botan/internal/int_utils.h>
18
19namespace Botan {
20
22 public:
24 DilithiumInternalKeypair decode_keypair(std::span<const uint8_t> private_key,
25 DilithiumConstants mode) const override;
26};
27
29 public:
31
32 bool is_valid_user_context(std::span<const uint8_t> user_context) const final {
33 return user_context.size() <= 255;
34 }
35
36 void start(std::span<const uint8_t> user_context) final {
37 // ML-DSA introduced an application-specific context string that is
38 // empty by default and can be set by the application.
39 //
40 // In HashML-DSA, there's an additional domain information, namely
41 // the serialized OID of the hash function used to hash the message.
42 //
43 // See FIPS 204, Algorithm 2, line 10 and Algorithm 7, line 6, and
44 // FIPS 204, Section 5.4
45
46 DilithiumMessageHash::start(user_context);
47 constexpr uint8_t domain_separator = 0x00; // HashML-DSA would use 0x01
48 const uint8_t context_length = checked_cast_to<uint8_t>(user_context.size());
49 update(std::array{domain_separator, context_length});
50 update(user_context);
51 }
52};
53
55 private:
56 /// Rho prime computation for ML-DSA
61 }
62
63 public:
66 m_seed_expansion_domain_separator({mode.k(), mode.l()}) {}
67
71 std::optional<std::reference_wrapper<RandomNumberGenerator>> rng) const override {
72 // NIST FIPS 204, Algorithm 2 (ML-DSA.Sign), line 5-8:
73 const auto rnd = [&] {
75 if(rng.has_value()) {
76 rng->get().randomize(optional_randomness);
77 }
78 return optional_randomness;
79 }();
80 return H(k, rnd, mu);
81 }
82
87
88 std::unique_ptr<DilithiumMessageHash> get_message_hash(DilithiumHashedPublicKey tr) const override {
89 return std::make_unique<ML_DSA_MessageHash>(std::move(tr));
90 }
91
92 std::optional<std::array<uint8_t, 2>> seed_expansion_domain_separator() const override {
93 return m_seed_expansion_domain_separator;
94 }
95
96 private:
97 std::array<uint8_t, 2> m_seed_expansion_domain_separator;
98};
99
100} // namespace Botan
101
102#endif
uint8_t l() const
dimensions of the expanded matrix A
static constexpr size_t OPTIONAL_RANDOMNESS_BYTES
static constexpr size_t SEED_RHOPRIME_BYTES
uint8_t k() const
dimensions of the expanded matrix A
virtual void start(std::span< const uint8_t > user_context)
DilithiumMessageHash(DilithiumHashedPublicKey tr)
OutT H_256(size_t outbytes, InTs &&... ins) const
secure_vector< uint8_t > encode_keypair(DilithiumInternalKeypair keypair) const override
DilithiumInternalKeypair decode_keypair(std::span< const uint8_t > private_key, DilithiumConstants mode) const override
void start(std::span< const uint8_t > user_context) final
Definition ml_dsa_impl.h:36
bool is_valid_user_context(std::span< const uint8_t > user_context) const final
Definition ml_dsa_impl.h:32
StrongSpan< const DilithiumCommitmentHash > truncate_commitment_hash(StrongSpan< const DilithiumCommitmentHash > seed) const override
Definition ml_dsa_impl.h:83
std::optional< std::array< uint8_t, 2 > > seed_expansion_domain_separator() const override
Definition ml_dsa_impl.h:92
ML_DSA_Symmetric_Primitives(const DilithiumConstants &mode)
Definition ml_dsa_impl.h:64
DilithiumSeedRhoPrime H_maybe_randomized(StrongSpan< const DilithiumSigningSeedK > k, StrongSpan< const DilithiumMessageRepresentative > mu, std::optional< std::reference_wrapper< RandomNumberGenerator > > rng) const override
Definition ml_dsa_impl.h:68
std::unique_ptr< DilithiumMessageHash > get_message_hash(DilithiumHashedPublicKey tr) const override
Definition ml_dsa_impl.h:88
int(* update)(CTX *, const void *, CC_LONG len)
int(* final)(unsigned char *, CTX *)
constexpr RT checked_cast_to(AT i)
Definition int_utils.h:74
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
std::pair< std::shared_ptr< Dilithium_PublicKeyInternal >, std::shared_ptr< Dilithium_PrivateKeyInternal > > DilithiumInternalKeypair
Internal representation of a Dilithium key pair.