Botan 3.6.1
Crypto and TLS for C&
Botan::LMS_PublicKey Class Reference

The LMS public key. More...

#include <lms.h>

Inheritance diagram for Botan::LMS_PublicKey:
Botan::LMS_Instance

Public Member Functions

void _const_time_unpoison () const
 
const LMS_Identifieridentifier () const
 The identifier of this LMS tree ('I' in RFC 8554)
 
const LMOTS_Paramslmots_params () const
 The LMOTS parameters used for OTS instances of this LMS instance.
 
const LMS_Paramslms_params () const
 The LMS parameters for this LMS instance.
 
 LMS_PublicKey (const LMS_PrivateKey &sk)
 Construct a new public key from a given LMS private key (RFC 8554 5.3).
 
 LMS_PublicKey (LMS_Params lms_params, LMOTS_Params lmots_params, LMS_Identifier I, LMS_Tree_Node lms_root)
 Construct a public key for given public key data.
 
std::vector< uint8_t > to_bytes () const
 Bytes of the full lms public key according to 8554 5.3.
 
bool verify_signature (const LMS_Message &msg, const LMS_Signature &sig) const
 Verify a LMS signature.
 

Static Public Member Functions

static LMS_PublicKey from_bytes_or_throw (BufferSlicer &slicer)
 Parse a public LMS key.
 
static size_t size (const LMS_Params &lms_params)
 The expected size of an LMS public key for given lms_params.
 

Detailed Description

The LMS public key.

Format according to RFC 8554: u32str(type) || u32str(otstype) || I || T[1]

Definition at line 225 of file lms.h.

Constructor & Destructor Documentation

◆ LMS_PublicKey() [1/2]

Botan::LMS_PublicKey::LMS_PublicKey ( LMS_Params lms_params,
LMOTS_Params lmots_params,
LMS_Identifier I,
LMS_Tree_Node lms_root )

Construct a public key for given public key data.

Definition at line 304 of file lms.cpp.

307 :
308 LMS_Instance(std::move(lms_params), std::move(lmots_params), std::move(I)), m_lms_root(std::move(lms_root)) {
309 BOTAN_ARG_CHECK(identifier().size() == LMS_IDENTIFIER_LEN, "Invalid LMS identifier");
310 BOTAN_ARG_CHECK(m_lms_root.size() == this->lms_params().m(), "Invalid LMS root");
311}
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:29
const LMS_Params & lms_params() const
The LMS parameters for this LMS instance.
Definition lms.h:164
const LMOTS_Params & lmots_params() const
The LMOTS parameters used for OTS instances of this LMS instance.
Definition lms.h:169
const LMS_Identifier & identifier() const
The identifier of this LMS tree ('I' in RFC 8554)
Definition lms.h:174
LMS_Instance(LMS_Params lms_params, LMOTS_Params lmots_params, LMS_Identifier identifier)
Constructor storing the provided LMS data.
Definition lms.h:156
static size_t size(const LMS_Params &lms_params)
The expected size of an LMS public key for given lms_params.
Definition lms.cpp:313
constexpr size_t LMS_IDENTIFIER_LEN
The length in bytes of the LMS identifier (I).
Definition lms.h:67

References BOTAN_ARG_CHECK, Botan::LMS_Instance::identifier(), Botan::LMS_IDENTIFIER_LEN, and size().

Referenced by from_bytes_or_throw().

◆ LMS_PublicKey() [2/2]

Botan::LMS_PublicKey::LMS_PublicKey ( const LMS_PrivateKey & sk)

Construct a new public key from a given LMS private key (RFC 8554 5.3).

Definition at line 348 of file lms.cpp.

348 : LMS_Instance(sk), m_lms_root(sk.lms_params().m()) {
349 lms_treehash(StrongSpan<LMS_Tree_Node>(m_lms_root), std::nullopt, std::nullopt, sk);
350}

Member Function Documentation

◆ _const_time_unpoison()

void Botan::LMS_PublicKey::_const_time_unpoison ( ) const
inline

Definition at line 269 of file lms.h.

269{ CT::unpoison(m_lms_root); }
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:64

◆ from_bytes_or_throw()

LMS_PublicKey Botan::LMS_PublicKey::from_bytes_or_throw ( BufferSlicer & slicer)
static

Parse a public LMS key.

Parameters
slicerThe BufferSlicer at the public key bytes' position
Returns
The LMS public key.
Exceptions
Decoding_ErrorIf parsing the public key fails.

Definition at line 264 of file lms.cpp.

264 {
265 size_t total_remaining_bytes = slicer.remaining();
266 // Alg. 6. 1. (4 bytes are sufficient until the next check)
267 if(total_remaining_bytes < sizeof(LMS_Algorithm_Type)) {
268 throw Decoding_Error("Too few bytes while parsing LMS public key.");
269 }
270 // Alg. 6. 2.a.
271 auto lms_type = load_be<LMS_Algorithm_Type>(slicer.take<sizeof(LMS_Algorithm_Type)>());
272 // Alg. 6. 2.c.
274 // Alg. 6. 2.d.
275 if(total_remaining_bytes < LMS_PublicKey::size(lms_params)) {
276 throw Decoding_Error("Too few bytes while parsing LMS public key.");
277 }
278 // Alg. 6. 2.b.
279 auto lmots_type = load_be<LMOTS_Algorithm_Type>(slicer.take<sizeof(LMOTS_Algorithm_Type)>());
281
283 throw Decoding_Error("No support for HSS-LMS instances with multiple hash functions.");
284 }
285
286 // Alg. 6. 2.e.
287 auto I = slicer.copy<LMS_Identifier>(LMS_IDENTIFIER_LEN);
288 // Alg. 6. 2.f.
289 auto lms_root = slicer.copy<LMS_Tree_Node>(lms_params.m());
290
291 return LMS_PublicKey(std::move(lms_params), std::move(lmots_params), std::move(I), std::move(lms_root));
292}
static LMOTS_Params create_or_throw(LMOTS_Algorithm_Type type)
Create the LM-OTS parameters from a known algorithm type.
Definition lm_ots.cpp:100
const std::string & hash_name() const
Name of the hash function to use.
Definition lm_ots.h:150
const std::string & hash_name() const
Returns the name of the hash function to use.
Definition lms.h:124
size_t m() const
Returns the number of bytes associated with each node.
Definition lms.h:119
static LMS_Params create_or_throw(LMS_Algorithm_Type type)
Create the LMS parameters from a known algorithm type.
Definition lms.cpp:112
LMS_PublicKey(LMS_Params lms_params, LMOTS_Params lmots_params, LMS_Identifier I, LMS_Tree_Node lms_root)
Construct a public key for given public key data.
Definition lms.cpp:304
LMS_Algorithm_Type
Enum of available LMS algorithm types.
Definition lms.h:30
Strong< std::vector< uint8_t >, struct LMS_Tree_Node_ > LMS_Tree_Node
A node with the LMS tree.
Definition lms.h:77
Strong< std::vector< uint8_t >, struct LMS_Identifier_ > LMS_Identifier
The identifier of an LMS tree (I in RFC 8554)
Definition lm_ots.h:50
LMOTS_Algorithm_Type
Enum of available LM-OTS algorithm types.
Definition lm_ots.h:65
constexpr auto load_be(ParamTs &&... params)
Definition loadstor.h:530

References Botan::BufferSlicer::copy(), Botan::LMOTS_Params::create_or_throw(), Botan::LMS_Params::create_or_throw(), Botan::LMOTS_Params::hash_name(), Botan::LMS_Params::hash_name(), Botan::LMS_Instance::lmots_params(), Botan::LMS_IDENTIFIER_LEN, Botan::LMS_Instance::lms_params(), LMS_PublicKey(), Botan::load_be(), Botan::LMS_Params::m(), Botan::BufferSlicer::remaining(), size(), and Botan::BufferSlicer::take().

Referenced by Botan::HSS_LMS_PublicKeyInternal::from_bytes_or_throw(), and Botan::HSS_Signature::from_bytes_or_throw().

◆ identifier()

const LMS_Identifier & Botan::LMS_Instance::identifier ( ) const
inlineinherited

The identifier of this LMS tree ('I' in RFC 8554)

Definition at line 174 of file lms.h.

174{ return m_identifier; }

Referenced by LMS_PublicKey(), Botan::LMS_PrivateKey::sign_and_get_pk(), and to_bytes().

◆ lmots_params()

const LMOTS_Params & Botan::LMS_Instance::lmots_params ( ) const
inlineinherited

The LMOTS parameters used for OTS instances of this LMS instance.

Definition at line 169 of file lms.h.

169{ return m_lmots_params; }

Referenced by from_bytes_or_throw(), Botan::LMS_PrivateKey::sign_and_get_pk(), to_bytes(), Botan::HSS_LMS_PublicKeyInternal::verify_signature(), and verify_signature().

◆ lms_params()

const LMS_Params & Botan::LMS_Instance::lms_params ( ) const
inlineinherited

The LMS parameters for this LMS instance.

Definition at line 164 of file lms.h.

164{ return m_lms_params; }

Referenced by from_bytes_or_throw(), Botan::LMS_PrivateKey::sign_and_get_pk(), Botan::HSS_LMS_PublicKeyInternal::size(), size(), to_bytes(), Botan::HSS_LMS_PublicKeyInternal::verify_signature(), and verify_signature().

◆ size()

size_t Botan::LMS_PublicKey::size ( const LMS_Params & lms_params)
static

◆ to_bytes()

std::vector< uint8_t > Botan::LMS_PublicKey::to_bytes ( ) const

Bytes of the full lms public key according to 8554 5.3.

pub_key_bytes = u32str(type) || u32str(otstype) || I || T[1]

Definition at line 294 of file lms.cpp.

294 {
295 // clang-format off
297 store_be(lms_params().algorithm_type()),
298 store_be(lmots_params().algorithm_type()),
299 identifier(),
300 m_lms_root);
301 // clang-format on
302}
constexpr auto concat(Rs &&... ranges)
Definition stl_util.h:263
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:773

References Botan::concat(), Botan::LMS_Instance::identifier(), Botan::LMS_Instance::lmots_params(), Botan::LMS_Instance::lms_params(), and Botan::store_be().

Referenced by Botan::HSS_LMS_PublicKeyInternal::to_bytes(), and Botan::HSS_LMS_PublicKeyInternal::verify_signature().

◆ verify_signature()

bool Botan::LMS_PublicKey::verify_signature ( const LMS_Message & msg,
const LMS_Signature & sig ) const

Verify a LMS signature.

See RFC 8554 5.4.2 - Algorithm 6.

Parameters
msgThe signed message.
sigThe already parsed LMS signature.
Returns
True if the signature is valid, false otherwise.

Definition at line 352 of file lms.cpp.

352 {
353 if(lms_root().size() != lms_params().m()) {
354 // LMS public key (T[1] part) has unexpected length
355 return false;
356 }
357 if(lms_params().algorithm_type() != sig.lms_type()) {
358 // LMS algorithm type does not match with the signature's
359 return false;
360 }
361 // Alg. 6a 2.g.
362 if(lmots_params().algorithm_type() != sig.lmots_sig().algorithm_type()) {
363 // LMOTS algorithm type does not match with the signature's
364 return false;
365 }
366 // Alg. 6a 2.i.
367 if(sig.q() >= (1ULL << uint64_t(lms_params().h()))) {
368 return false;
369 }
370 // Alg 6. 3.
371 std::optional<LMS_Tree_Node> Tc = lms_compute_root_from_sig(msg, sig);
372 if(!Tc.has_value()) {
373 return false;
374 }
375 // Alg 6. 4.
376 return Tc.value() == lms_root();
377}
LMOTS_Algorithm_Type algorithm_type() const
Returns the LM-OTS algorithm type.
Definition lm_ots.h:120

References Botan::LMOTS_Signature::algorithm_type(), Botan::LMS_Instance::lmots_params(), Botan::LMS_Signature::lmots_sig(), Botan::LMS_Instance::lms_params(), Botan::LMS_Signature::lms_type(), Botan::LMS_Signature::q(), and size().

Referenced by Botan::HSS_LMS_PublicKeyInternal::verify_signature().


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