Botan 3.14.1
Crypto and TLS for C&
Botan::ML_DSA_Expanding_Keypair_Codec Class Referencefinal

#include <ml_dsa_impl.h>

Inheritance diagram for Botan::ML_DSA_Expanding_Keypair_Codec:
Botan::Dilithium_Keypair_Codec

Public Member Functions

DilithiumDecodedKeypair decode_keypair (std::span< const uint8_t > private_key, DilithiumConstants mode) const override
secure_vector< uint8_t > encode_keypair (const DilithiumInternalKeypair &keypair, MlPrivateKeyFormat format) const override

Static Public Member Functions

static std::unique_ptr< Dilithium_Keypair_Codec > create (DilithiumMode mode)

Detailed Description

Definition at line 21 of file ml_dsa_impl.h.

Member Function Documentation

◆ create()

std::unique_ptr< Dilithium_Keypair_Codec > Botan::Dilithium_Keypair_Codec::create ( DilithiumMode mode)
staticinherited

Definition at line 22 of file dilithium_keys.cpp.

22 {
23#if defined(BOTAN_HAS_DILITHIUM) || defined(BOTAN_HAS_DILITHIUM_AES)
24 if(mode.is_dilithium_round3()) {
25 return std::make_unique<Dilithium_Expanded_Keypair_Codec>();
26 }
27#endif
28
29#if defined(BOTAN_HAS_ML_DSA)
30 if(mode.is_ml_dsa()) {
31 return std::make_unique<ML_DSA_Expanding_Keypair_Codec>();
32 }
33#endif
34
35 throw Not_Implemented("requested ML-DSA/Dilithium mode is not implemented in this build");
36}

References Botan::DilithiumMode::is_dilithium_round3(), and Botan::DilithiumMode::is_ml_dsa().

Referenced by Botan::DilithiumConstants::DilithiumConstants().

◆ decode_keypair()

DilithiumDecodedKeypair Botan::ML_DSA_Expanding_Keypair_Codec::decode_keypair ( std::span< const uint8_t > private_key,
DilithiumConstants mode ) const
overridevirtual

Decodes any of the three RFC 9881 CHOICE alternatives. For backwards compatibility, also the raw 32-byte seed and the raw expanded key of FIPS 204 (without any ASN.1 wrapping) are accepted.

Implements Botan::Dilithium_Keypair_Codec.

Definition at line 113 of file ml_dsa_impl.cpp.

114 {
115 if(private_key_bits.size() == DilithiumConstants::SEED_RANDOMNESS_BYTES) {
116 // backwards compatibility (not RFC 9881 conforming) to the raw seed format
117 return {
118 .keypair = Dilithium_Algos::expand_keypair(DilithiumSeedRandomness(private_key_bits), std::move(mode)),
119 .format = MlPrivateKeyFormat::Seed,
120 };
121 }
122 if(private_key_bits.size() == mode.private_key_bytes()) {
123 // raw expanded key in the FIPS 204 sk encoding without ASN.1 wrapping (not RFC 9881 conforming),
124 // as used e.g. by the NIST ACVP and Wycheproof test vectors. The length cannot collide with any
125 // of the RFC 9881 encodings.
126 return {
127 .keypair = Dilithium_Algos::decode_keypair(StrongSpan<const DilithiumSerializedPrivateKey>(private_key_bits),
128 std::move(mode)),
130 };
131 }
132 // "seed-only" format from RFC 9881
133 BER_Decoder ber_dec(private_key_bits, BER_Decoder::Limits::DER());
134 auto obj = ber_dec.peek_next_object();
135 if(obj.type() == ASN1_Type(0) && obj.class_tag() == ASN1_Class::ContextSpecific) {
136 return {
137 .keypair = decode_seed_only(private_key_bits, std::move(mode)),
138 .format = MlPrivateKeyFormat::Seed,
139 };
140 }
141 // now it could still be "expanded-only" or "both"
142 if(obj.type() == ASN1_Type::OctetString && obj.class_tag() == ASN1_Class::Universal) {
143 return {
144 .keypair = decode_expanded_only(private_key_bits, std::move(mode)),
146 };
147 }
148 return {
149 .keypair = decode_seed_plus_expanded(private_key_bits, std::move(mode)),
150 .format = MlPrivateKeyFormat::Both,
151 };
152}
static Limits DER()
Definition ber_dec.h:44
static constexpr size_t SEED_RANDOMNESS_BYTES
DilithiumInternalKeypair expand_keypair(DilithiumSeedRandomness xi, DilithiumConstants mode)
DilithiumInternalKeypair decode_keypair(StrongSpan< const DilithiumSerializedPrivateKey > sk, DilithiumConstants mode)
ASN1_Type
Definition asn1_obj.h:47
Strong< secure_vector< uint8_t >, struct DilithiumSeedRandomness_ > DilithiumSeedRandomness
Principal seed used to generate Dilithium key pairs.

References Botan::Both, Botan::ContextSpecific, Botan::Dilithium_Algos::decode_keypair(), Botan::BER_Decoder::Limits::DER(), Botan::Dilithium_Algos::expand_keypair(), Botan::Expanded, Botan::OctetString, Botan::BER_Decoder::peek_next_object(), Botan::DilithiumConstants::private_key_bytes(), Botan::Seed, Botan::DilithiumConstants::SEED_RANDOMNESS_BYTES, and Botan::Universal.

◆ encode_keypair()

secure_vector< uint8_t > Botan::ML_DSA_Expanding_Keypair_Codec::encode_keypair ( const DilithiumInternalKeypair & keypair,
MlPrivateKeyFormat format ) const
overridevirtual

Encodes the private key as one of the three CHOICE alternatives of the ML-DSA-PrivateKey structure specified in RFC 9881, Section 6.

Implements Botan::Dilithium_Keypair_Codec.

Definition at line 71 of file ml_dsa_impl.cpp.

72 {
73 BOTAN_ASSERT_NONNULL(keypair.second);
74 const auto& seed = keypair.second->seed();
75 if(format != MlPrivateKeyFormat::Expanded && !seed.has_value()) {
76 throw Encoding_Error("ML-DSA private key does not contain the seed, cannot encode it in the requested format");
77 }
78
80 DER_Encoder der_enc(result);
81 switch(format) {
83 /*
84 * RFC 9881 "seed" CHOICE alternative: [0] IMPLICIT OCTET STRING, i.e.
85 *
86 * <80 20>
87 * 0 32: [0]
88 * : 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
89 * : 10 11 12 13 14 15 16 17 18 19 1A 1B 1C 1D 1E 1F
90 *
91 * Note: The previous, non-standard format, which only contained the
92 * seed as a raw byte string, is still accepted by decode_keypair().
93 */
94 der_enc.encode(seed->get(), ASN1_Type::OctetString, ASN1_Type(0), ASN1_Class::ContextSpecific);
95 break;
97 // RFC 9881 "expandedKey" CHOICE alternative: OCTET STRING
98 der_enc.encode(Dilithium_Algos::encode_keypair(keypair).get(), ASN1_Type::OctetString);
99 break;
101 // RFC 9881 "both" CHOICE alternative:
102 // SEQUENCE { seed OCTET STRING, expandedKey OCTET STRING }
103 der_enc.start_sequence()
104 .encode(seed->get(), ASN1_Type::OctetString)
106 .end_cons();
107 break;
108 }
109
110 return result;
111}
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:121
DilithiumSerializedPrivateKey encode_keypair(const DilithiumInternalKeypair &keypair)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128

References BOTAN_ASSERT_NONNULL, Botan::Both, Botan::ContextSpecific, Botan::DER_Encoder::encode(), Botan::Dilithium_Algos::encode_keypair(), Botan::DER_Encoder::end_cons(), Botan::Expanded, Botan::OctetString, Botan::Seed, and Botan::DER_Encoder::start_sequence().


The documentation for this class was generated from the following files: