blob: 9a332636e116d251f43122cdbc5006e6a3d02a17 [file] [log] [blame]
Antonio de Angelis695d75b2022-08-22 15:06:24 +01001From 99a390b1a71005db8b6c92956a2c86cc3182a6f3 Mon Sep 17 00:00:00 2001
Raef Coles8a3bf392022-06-22 13:53:59 +01002From: Raef Coles <raef.coles@arm.com>
3Date: Wed, 21 Jul 2021 12:42:15 +0100
Antonio de Angelis2718b582022-08-23 14:58:40 +01004Subject: [PATCH 5/9] Add LMS implementation
Raef Coles8a3bf392022-06-22 13:53:59 +01005
6Also an LM-OTS implementation as one is required for LMS.
7
8Signed-off-by: Raef Coles <raef.coles@arm.com>
9---
10 ChangeLog.d/LMS.txt | 12 +
11 include/mbedtls/check_config.h | 10 +
12 include/mbedtls/error.h | 2 +
13 include/mbedtls/lmots.h | 303 +++++++++++
14 include/mbedtls/lms.h | 271 ++++++++++
15 include/mbedtls/mbedtls_config.h | 28 +
16 library/CMakeLists.txt | 2 +
17 library/Makefile | 2 +
18 library/lmots.c | 684 +++++++++++++++++++++++
19 library/lms.c | 718 +++++++++++++++++++++++++
20 scripts/generate_errors.pl | 2 +-
21 tests/suites/test_suite_lmots.data | 29 +
22 tests/suites/test_suite_lmots.function | 108 ++++
23 tests/suites/test_suite_lms.data | 32 ++
Antonio de Angelis90bee0f2022-07-13 11:22:41 +010024 tests/suites/test_suite_lms.function | 84 +++
25 15 files changed, 2286 insertions(+), 1 deletion(-)
Raef Coles8a3bf392022-06-22 13:53:59 +010026 create mode 100644 ChangeLog.d/LMS.txt
27 create mode 100644 include/mbedtls/lmots.h
28 create mode 100644 include/mbedtls/lms.h
29 create mode 100644 library/lmots.c
30 create mode 100644 library/lms.c
31 create mode 100644 tests/suites/test_suite_lmots.data
32 create mode 100644 tests/suites/test_suite_lmots.function
33 create mode 100644 tests/suites/test_suite_lms.data
34 create mode 100644 tests/suites/test_suite_lms.function
35
36diff --git a/ChangeLog.d/LMS.txt b/ChangeLog.d/LMS.txt
37new file mode 100644
Antonio de Angelis90bee0f2022-07-13 11:22:41 +010038index 000000000..0f09f0186
Raef Coles8a3bf392022-06-22 13:53:59 +010039--- /dev/null
40+++ b/ChangeLog.d/LMS.txt
41@@ -0,0 +1,12 @@
42+Features
43+ * Add the LMS post-quantum-safe stateful-hash asymmetric signature scheme
44+ as defined in RFC8554 and NIST.SP.200-208. This currently only supports
45+ one parameter set (LMS_SHA256_M32_H10), meaning that each private key can
46+ be used to sign 1024 messages. As such, it is not intended for use in TLS,
47+ but instead for verification of assets transmitted over an insecure
48+ channel, particularly firmware images. This is one of the signature
49+ schemes recommended by the IETF draft SUIT standard for IOT firmware
50+ upgrades (RFC9019).
51+ * Add the LM-OTS post-quantum-safe one-time signature scheme, which is
52+ required for LMS. This can be used independently, but each key can only be
53+ used to sign one message so is impractical for most circumstances.
54diff --git a/include/mbedtls/check_config.h b/include/mbedtls/check_config.h
Antonio de Angelis90bee0f2022-07-13 11:22:41 +010055index 5fe984984..c3017aef3 100644
Raef Coles8a3bf392022-06-22 13:53:59 +010056--- a/include/mbedtls/check_config.h
57+++ b/include/mbedtls/check_config.h
Antonio de Angelis90bee0f2022-07-13 11:22:41 +010058@@ -333,6 +333,16 @@
Raef Coles8a3bf392022-06-22 13:53:59 +010059 #error "!MBEDTLS_SSL_KEEP_PEER_CERTIFICATE requires MBEDTLS_SHA512_C, MBEDTLS_SHA256_C or MBEDTLS_SHA1_C"
60 #endif
61
62+#if defined(MBEDTLS_LMOTS_C) && \
63+ ( !defined(MBEDTLS_MD_C) )
64+#error "MBEDTLS_LMOTS_C requires MBEDTLS_MD_C"
65+#endif
66+
67+#if defined(MBEDTLS_LMS_C) && \
68+ ( !defined(MBEDTLS_LMOTS_C) || !defined(MBEDTLS_MD_C) )
69+#error "MBEDTLS_LMS_C requires MBEDTLS_LMOTS_C and MBEDTLS_MD_C"
70+#endif
71+
72 #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C) && \
73 ( !defined(MBEDTLS_PLATFORM_C) || !defined(MBEDTLS_PLATFORM_MEMORY) )
74 #error "MBEDTLS_MEMORY_BUFFER_ALLOC_C defined, but not all prerequisites"
75diff --git a/include/mbedtls/error.h b/include/mbedtls/error.h
Antonio de Angelis90bee0f2022-07-13 11:22:41 +010076index 8b2b9ea58..73d61dbc6 100644
Raef Coles8a3bf392022-06-22 13:53:59 +010077--- a/include/mbedtls/error.h
78+++ b/include/mbedtls/error.h
79@@ -82,6 +82,8 @@
80 * POLY1305 3 0x0057-0x005B
81 * CHACHAPOLY 2 0x0054-0x0056
82 * PLATFORM 2 0x0070-0x0072
83+ * LMOTS 2 0x0076-0x0078
84+ * LMS 2 0x0011-0x0017
85 *
86 * High-level module nr (3 bits - 0x0...-0x7...)
87 * Name ID Nr of Errors
88diff --git a/include/mbedtls/lmots.h b/include/mbedtls/lmots.h
89new file mode 100644
Antonio de Angelis90bee0f2022-07-13 11:22:41 +010090index 000000000..c98f3bfd7
Raef Coles8a3bf392022-06-22 13:53:59 +010091--- /dev/null
92+++ b/include/mbedtls/lmots.h
93@@ -0,0 +1,303 @@
94+/**
95+ * \file lmots.h
96+ *
97+ * \brief This file provides an API for the LM-OTS post-quantum-safe one-time
98+ * public-key signature scheme.
99+ */
100+/*
101+ * Copyright The Mbed TLS Contributors
102+ * SPDX-License-Identifier: Apache-2.0
103+ *
104+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
105+ * not use this file except in compliance with the License.
106+ * You may obtain a copy of the License at
107+ *
108+ * http://www.apache.org/licenses/LICENSE-2.0
109+ *
110+ * Unless required by applicable law or agreed to in writing, software
111+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
112+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
113+ * See the License for the specific language governing permissions and
114+ * limitations under the License.
115+ */
116+
117+#ifndef MBEDTLS_LMOTS_H
118+#define MBEDTLS_LMOTS_H
119+
120+#include "mbedtls/private_access.h"
121+
122+#include <stdint.h>
123+#include <stddef.h>
124+
125+#define MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA -0x0076 /**< Bad data has been input to an LMOTS function */
126+#define MBEDTLS_ERR_LMOTS_VERIFY_FAILED -0x0078 /**< LMOTS signature verification failed */
127+
128+#define MBEDTLS_LMOTS_N_HASH_LEN (32)
129+#define MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN (34)
130+#define MBEDTLS_LMOTS_TYPE_LEN (4)
131+#define MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN (MBEDTLS_LMOTS_N_HASH_LEN)
132+#define MBEDTLS_LMOTS_I_KEY_ID_LEN (16)
133+#define MBEDTLS_LMOTS_Q_LEAF_ID_LEN (4)
134+
135+#define MBEDTLS_LMOTS_SIG_LEN (MBEDTLS_LMOTS_TYPE_LEN + MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN + \
136+ (MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN * MBEDTLS_LMOTS_N_HASH_LEN))
137+
138+#define MBEDTLS_LMOTS_PUBKEY_LEN (MBEDTLS_LMOTS_TYPE_LEN + MBEDTLS_LMOTS_I_KEY_ID_LEN + \
139+ MBEDTLS_LMOTS_Q_LEAF_ID_LEN + MBEDTLS_LMOTS_N_HASH_LEN)
140+
141+#define MBEDTLS_LMOTS_SIG_TYPE_OFFSET (0)
142+#define MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET (MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN)
143+#define MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET (MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET + MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN)
144+
145+#define MBEDTLS_LMOTS_PUBKEY_TYPE_OFFSET (0)
146+#define MBEDTLS_LMOTS_PUBKEY_I_KEY_ID_OFFSET (MBEDTLS_LMOTS_PUBKEY_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN)
147+#define MBEDTLS_LMOTS_PUBKEY_Q_LEAF_ID_OFFSET (MBEDTLS_LMOTS_PUBKEY_I_KEY_ID_OFFSET + MBEDTLS_LMOTS_I_KEY_ID_LEN)
148+#define MBEDTLS_LMOTS_PUBKEY_KEY_HASH_OFFSET (MBEDTLS_LMOTS_PUBKEY_Q_LEAF_ID_OFFSET + MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
149+
150+
151+#ifdef __cplusplus
152+extern "C" {
153+#endif
154+
155+/* https://www.iana.org/assignments/leighton-micali-signatures/leighton-micali-signatures.xhtml
156+ * We are only implementing a subset of the types, particularly n32_w8, for the sake of simplicty.
157+ */
158+typedef enum {
159+ MBEDTLS_LMOTS_SHA256_N32_W8 = 4
160+} mbedtls_lmots_algorithm_type_t;
161+
162+
163+typedef struct {
164+ unsigned char MBEDTLS_PRIVATE(have_privkey); /*!< Whether the context contains a private key.
165+ Boolean values only. */
166+ unsigned char MBEDTLS_PRIVATE(have_pubkey); /*!< Whether the context contains a public key.
167+ Boolean values only. */
168+ unsigned char MBEDTLS_PRIVATE(I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN]); /*!< The key
169+ identifier. */
170+ unsigned int MBEDTLS_PRIVATE(q_leaf_identifier); /*!< Which leaf of the LMS key this is.
171+ 0 if the key is not part of an LMS key. */
172+ unsigned char MBEDTLS_PRIVATE(q_leaf_identifier_bytes)[MBEDTLS_LMOTS_Q_LEAF_ID_LEN];/*!< The
173+ leaf identifier in network bytes form. */
174+ mbedtls_lmots_algorithm_type_t MBEDTLS_PRIVATE(type); /*!< The LM-OTS key type identifier as
175+ per IANA. Only SHA256_N32_W8 is currently
176+ supported. */
177+ unsigned char MBEDTLS_PRIVATE(priv_key[MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN][32]); /*!< The private
178+ key, one hash output per byte of the encoded
179+ symbol string P (32 bytes of hash output +
180+ 2 bytes of checksum). */
181+ unsigned char MBEDTLS_PRIVATE(pub_key[32]); /*!< The public key, in the form of a SHA256
182+ output. */
183+} mbedtls_lmots_context;
184+
185+
186+/**
187+ * \brief This function initializes an LMOTS context
188+ *
189+ * \param ctx The uninitialized LMOTS context that will then be
190+ * initialized.
191+ */
192+void mbedtls_lmots_init( mbedtls_lmots_context *ctx );
193+
194+/**
195+ * \brief This function uninitializes an LMOTS context
196+ *
197+ * \param ctx The initialized LMOTS context that will then be
198+ * uninitialized.
199+ */
200+void mbedtls_lmots_free( mbedtls_lmots_context *ctx );
201+
202+/**
203+ * \brief This function sets the type of an LMOTS context
204+ *
205+ * \note The parameter set in the context will then be used
206+ * for keygen operations etc.
207+ *
208+ * \param ctx The initialized LMOTS context.
209+ * \param type The type that will be set in the context.
210+ */
211+int mbedtls_lmots_set_algorithm_type( mbedtls_lmots_context *ctx,
212+ mbedtls_lmots_algorithm_type_t type );
213+
214+/**
215+ * \brief This function creates a candidate public key from
216+ * an LMOTS signature. This can then be compared to
217+ * the real public key to determine the validity of
218+ * the signature.
219+ *
220+ * \note This function is exposed publicly to be used in LMS
221+ * signature verification, it is expected that
222+ * mbedtls_lmots_verify will be used for LMOTS
223+ * signature verification.
224+ *
225+ * \param I_key_identifier The key identifier of the key, as a 16 byte
226+ * bytestring.
227+ * \param q_leaf_identifier The leaf identifier of key. If this LMOTS key is
228+ * not being used as part of an LMS key, this should
229+ * be set to 0.
230+ * \param msg The buffer from which the message will be read.
231+ * \param msg_len The size of the message that will be read.
232+ * \param sig The buff from which the signature will be read.
233+ * MBEDTLS_LMOTS_SIG_LEN bytes will be read from this.
234+ * \param out The buffer where the candidate public key will be
235+ * stored. Must be at least #MBEDTLS_LMOTS_N_HASH_LEN
236+ * bytes in size.
237+ *
238+ * \return \c 0 on success.
239+ * \return A non-zero error code on failure.
240+ */
241+int mbedtls_lmots_generate_pub_key_candidate( const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
242+ const unsigned char q_leaf_identifier[MBEDTLS_LMOTS_Q_LEAF_ID_LEN],
243+ const unsigned char *msg,
244+ size_t msg_len,
245+ const unsigned char *sig,
246+ unsigned char *out );
247+
248+/**
249+ * \brief This function creates a LMOTS signature, using a
250+ * LMOTS context that contains a private key.
251+ *
252+ * \note Before this function is called, the context must
253+ * have been initialized and must contain a private
254+ * key.
255+ *
256+ * \note LMOTS private keys can only be used once, otherwise
257+ * attackers may be able to create forged signatures.
258+ * If the signing operation is successful, the private
259+ * key in the context will be erased, and no further
260+ * signing will be possible until another private key
261+ * is loaded
262+ *
263+ * \param ctx The initialized LMOTS context from which the
264+ * private key will be read.
265+ * \param f_rng The RNG function to be used for signature
266+ * generation.
267+ * \param p_rng The RNG context to be passed to f_rng
268+ * \param msg The buffer from which the message will be read.
269+ * \param msg_len The size of the message that will be read.
270+ * \param sig The buf into which the signature will be stored.
271+ * Must be at least #MBEDTLS_LMOTS_SIG_LEN in size.
272+ *
273+ * \return \c 0 on success.
274+ * \return A non-zero error code on failure.
275+ */
276+int mbedtls_lmots_sign( mbedtls_lmots_context *ctx,
277+ int (*f_rng)(void *, unsigned char *, size_t),
278+ void *p_rng, const unsigned char *msg, size_t msg_len,
279+ unsigned char *sig );
280+
281+/**
282+ * \brief This function verifies a LMOTS signature, using a
283+ * LMOTS context that contains a public key.
284+ *
285+ * \note Before this function is called, the context must
286+ * have been initialized and must contain a public key
287+ * (either by import or generation).
288+ *
289+ * \param ctx The initialized LMOTS context from which the public
290+ * key will be read.
291+ * \param msg The buffer from which the message will be read.
292+ * \param msg_len The size of the message that will be read.
293+ * \param sig The buf from which the signature will be read.
294+ * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from
295+ * this.
296+ *
297+ * \return \c 0 on successful verification.
298+ * \return A non-zero error code on failure.
299+ */
300+int mbedtls_lmots_verify( mbedtls_lmots_context *ctx, const unsigned char *msg,
301+ size_t msg_len, const unsigned char *sig );
302+
303+/**
304+ * \brief This function imports an LMOTS public key into a
305+ * LMOTS context.
306+ *
307+ * \note Before this function is called, the context must
308+ * have been initialized.
309+ *
310+ * \note See IETF RFC8554 for details of the encoding of
311+ * this public key.
312+ *
313+ * \param ctx The initialized LMOTS context store the key in.
314+ * \param key The buffer from which the key will be read.
315+ * #MBEDTLS_LMOTS_PUBKEY_LEN bytes will be read from
316+ * this.
317+ *
318+ * \return \c 0 on success.
319+ * \return A non-zero error code on failure.
320+ */
321+int mbedtls_lmots_import_pubkey( mbedtls_lmots_context *ctx,
322+ const unsigned char *key );
323+
324+/**
325+ * \brief This function exports an LMOTS public key from a
326+ * LMOTS context that already contains a public key.
327+ *
328+ * \note Before this function is called, the context must
329+ * have been initialized and the context must contain
330+ * a public key.
331+ *
332+ * \note See IETF RFC8554 for details of the encoding of
333+ * this public key.
334+ *
335+ * \param ctx The initialized LMOTS context that contains the
336+ * publc key.
337+ * \param key The buffer into which the key will be output. Must
338+ * be at least #MBEDTLS_LMOTS_PUBKEY_LEN in size.
339+ *
340+ * \return \c 0 on success.
341+ * \return A non-zero error code on failure.
342+ */
343+int mbedtls_lmots_export_pubkey( mbedtls_lmots_context *ctx,
344+ unsigned char *key );
345+
346+/**
347+ * \brief This function generates an LMOTS public key from a
348+ * LMOTS context that already contains a private key.
349+ *
350+ * \note Before this function is called, the context must
351+ * have been initialized and the context must contain
352+ * a private key.
353+ *
354+ * \param ctx The initialized LMOTS context to generate the key
355+ * from and store it into.
356+ *
357+ * \return \c 0 on success.
358+ * \return A non-zero error code on failure.
359+ */
360+int mbedtls_lmots_gen_pubkey( mbedtls_lmots_context *ctx );
361+
362+/**
363+ * \brief This function generates an LMOTS private key, and
364+ * stores in into an LMOTS context.
365+ *
366+ * \note Before this function is called, the context must
367+ * have been initialized and the type of the LMOTS
368+ * context set using mbedtls_lmots_set_algorithm_type
369+ *
370+ * \note The seed must have at least 256 bits of entropy.
371+ *
372+ * \param ctx The initialized LMOTS context to generate the key
373+ * into.
374+ * \param I_key_identifier The key identifier of the key, as a 16 byte
375+ * bytestring.
376+ * \param q_leaf_identifier The leaf identifier of key. If this LMOTS key is
377+ * not being used as part of an LMS key, this should
378+ * be set to 0.
379+ * \param seed The seed used to deterministically generate the
380+ * key.
381+ * \param seed_len The length of the seed.
382+ *
383+ * \return \c 0 on success.
384+ * \return A non-zero error code on failure.
385+ */
386+int mbedtls_lmots_gen_privkey( mbedtls_lmots_context *ctx,
387+ const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
388+ unsigned int q_leaf_identifier,
389+ const unsigned char *seed,
390+ size_t seed_len );
391+
392+#ifdef __cplusplus
393+}
394+#endif
395+
396+#endif /* MBEDTLS_LMOTS_H */
397diff --git a/include/mbedtls/lms.h b/include/mbedtls/lms.h
398new file mode 100644
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100399index 000000000..77559e24b
Raef Coles8a3bf392022-06-22 13:53:59 +0100400--- /dev/null
401+++ b/include/mbedtls/lms.h
402@@ -0,0 +1,271 @@
403+/**
404+ * \file lms.h
405+ *
406+ * \brief This file provides an API for the LMS post-quantum-safe stateful-hash
407+ * public-key signature scheme.
408+ */
409+/*
410+ * Copyright The Mbed TLS Contributors
411+ * SPDX-License-Identifier: Apache-2.0
412+ *
413+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
414+ * not use this file except in compliance with the License.
415+ * You may obtain a copy of the License at
416+ *
417+ * http://www.apache.org/licenses/LICENSE-2.0
418+ *
419+ * Unless required by applicable law or agreed to in writing, software
420+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
421+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
422+ * See the License for the specific language governing permissions and
423+ * limitations under the License.
424+ */
425+#ifndef MBEDTLS_LMS_H
426+#define MBEDTLS_LMS_H
427+
428+#include <stdint.h>
429+#include <stddef.h>
430+
431+#include "mbedtls/private_access.h"
432+#include "mbedtls/lmots.h"
433+
434+#define MBEDTLS_ERR_LMS_BAD_INPUT_DATA -0x0011 /**< Bad data has been input to an LMS function */
435+#define MBEDTLS_ERR_LMS_OUT_OF_PRIV_KEYS -0x0013 /**< Specified LMS key has utilised all of its private keys */
436+#define MBEDTLS_ERR_LMS_VERIFY_FAILED -0x0015 /**< LMS signature verification failed */
437+#define MBEDTLS_ERR_LMS_ALLOC_FAILED -0x0017 /**< LMS failed to allocate space for a private key */
438+
439+#define MBEDTLS_LMS_TYPE_LEN (4)
440+#define MBEDTLS_LMS_H_TREE_HEIGHT (10)
441+#define MBEDTLS_LMS_M_NODE_BYTES (32)
442+
443+#define MBEDTLS_LMS_SIG_LEN (MBEDTLS_LMOTS_Q_LEAF_ID_LEN + MBEDTLS_LMOTS_SIG_LEN + \
444+ MBEDTLS_LMS_TYPE_LEN + MBEDTLS_LMS_H_TREE_HEIGHT * MBEDTLS_LMS_M_NODE_BYTES)
445+
446+#define MBEDTLS_LMS_PUBKEY_LEN (MBEDTLS_LMS_TYPE_LEN + MBEDTLS_LMOTS_TYPE_LEN + \
447+ MBEDTLS_LMOTS_I_KEY_ID_LEN + MBEDTLS_LMS_M_NODE_BYTES)
448+
449+#define MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET (0)
450+#define MBEDTLS_LMS_SIG_OTS_SIG_OFFSET (MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET + MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
451+#define MBEDTLS_LMS_SIG_TYPE_OFFSET (MBEDTLS_LMS_SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_LEN)
452+#define MBEDTLS_LMS_SIG_PATH_OFFSET (MBEDTLS_LMS_SIG_TYPE_OFFSET + MBEDTLS_LMS_TYPE_LEN)
453+
454+#define MBEDTLS_LMS_PUBKEY_TYPE_OFFSET (0)
455+#define MBEDTLS_LMS_PUBKEY_OTSTYPE_OFFSET (MBEDTLS_LMS_PUBKEY_TYPE_OFFSET + MBEDTLS_LMS_TYPE_LEN)
456+#define MBEDTLS_LMS_PUBKEY_I_KEY_ID_OFFSET (MBEDTLS_LMS_PUBKEY_OTSTYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN)
457+#define MBEDTLS_LMS_PUBKEY_ROOT_NODE_OFFSET (MBEDTLS_LMS_PUBKEY_I_KEY_ID_OFFSET + MBEDTLS_LMOTS_I_KEY_ID_LEN)
458+
459+#ifdef __cplusplus
460+extern "C" {
461+#endif
462+
463+/* https://www.iana.org/assignments/leighton-micali-signatures/leighton-micali-signatures.xhtml
464+ * We are only implementing a subset of the types, particularly H10, for the sake of simplicty.
465+ */
466+typedef enum {
467+ MBEDTLS_LMS_SHA256_M32_H10 = 0x6,
468+} mbedtls_lms_algorithm_type_t;
469+
470+
471+typedef struct {
472+ unsigned char MBEDTLS_PRIVATE(have_privkey); /*!< Whether the context contains a private key.
473+ Boolean values only. */
474+ unsigned char MBEDTLS_PRIVATE(have_pubkey); /*!< Whether the context contains a public key.
475+ Boolean values only. */
476+ unsigned char MBEDTLS_PRIVATE(I_key_identifier)[MBEDTLS_LMOTS_I_KEY_ID_LEN]; /*!< The key
477+ identifier. */
478+ mbedtls_lms_algorithm_type_t MBEDTLS_PRIVATE(type); /*!< The LMS key type identifier as per
479+ IANA. Only SHA256_M32_H10 is currently
480+ supported. */
481+ mbedtls_lmots_algorithm_type_t MBEDTLS_PRIVATE(otstype); /*!< The LM-OTS key type identifier as
482+ per IANA. Only SHA256_N32_W8 is currently
483+ supported. */
484+ unsigned int MBEDTLS_PRIVATE(q_next_usable_key); /*!< The index of the next OTS key that has not
485+ been used. */
486+ mbedtls_lmots_context *MBEDTLS_PRIVATE(priv_keys); /*!< The private key material. One OTS key
487+ for each leaf node in the merkle tree. */
488+ unsigned char MBEDTLS_PRIVATE(T_1_pub_key)[MBEDTLS_LMS_M_NODE_BYTES]; /*!< The public key, in
489+ the form of the merkle tree root node. */
490+} mbedtls_lms_context;
491+
492+
493+/**
494+ * \brief This function initializes an LMS context
495+ *
496+ * \param ctx The uninitialized LMS context that will then be
497+ * initialized.
498+ */
499+void mbedtls_lms_init( mbedtls_lms_context *ctx );
500+
501+/**
502+ * \brief This function uninitializes an LMS context
503+ *
504+ * \param ctx The initialized LMS context that will then be
505+ * uninitialized.
506+ */
507+void mbedtls_lms_free( mbedtls_lms_context *ctx );
508+
509+/**
510+ * \brief This function sets the type of an LMS context
511+ *
512+ * \note The parameter set in the context will then be used
513+ * for keygen operations etc.
514+ *
515+ * \param ctx The initialized LMS context.
516+ * \param type The type that will be set in the context.
517+ * \param otstype The type of the LMOTS implementation used by this
518+ * context.
519+ */
520+int mbedtls_lms_set_algorithm_type( mbedtls_lms_context *ctx,
521+ mbedtls_lms_algorithm_type_t type,
522+ mbedtls_lmots_algorithm_type_t otstype);
523+
524+/**
525+ * \brief This function creates a LMS signature, using a
526+ * LMOTS context that contains a private key.
527+ *
528+ * \note This function is intended for _testing purposes
529+ * only_, due to complexities around updating stateful
530+ * keys.
531+ *
532+ * \note Before this function is called, the context must
533+ * have been initialized and must contain a private
534+ * key.
535+ *
536+ * \note Each of the LMOTS private keys inside a LMS private
537+ * key can only be used once. If they are reused, then
538+ * attackers may be able to forge signatures with that
539+ * key. This is all handled transparently, but it is
540+ * important to not perform copy operations on LMS
541+ * contexts that contain private key material.
542+ *
543+ * \param ctx The initialized LMS context from which the
544+ * private key will be read.
545+ * \param f_rng The RNG function to be used for signature
546+ * generation.
547+ * \param p_rng The RNG context to be passed to f_rng
548+ * \param msg The buffer from which the message will be read.
549+ * \param msg_len The size of the message that will be read.
550+ * \param sig The buf into which the signature will be stored.
551+ * Must be at least #MBEDTLS_LMOTS_SIG_LEN in size.
552+ *
553+ * \return \c 0 on success.
554+ * \return A non-zero error code on failure.
555+ */
556+int mbedtls_lms_sign( mbedtls_lms_context *ctx,
557+ int (*f_rng)(void *, unsigned char *, size_t),
558+ void* p_rng, unsigned char *msg, unsigned int msg_len,
559+ unsigned char *sig);
560+
561+/**
562+ * \brief This function verifies a LMS signature, using a
563+ * LMS context that contains a public key.
564+ *
565+ * \note Before this function is called, the context must
566+ * have been initialized and must contain a public key
567+ * (either by import or generation).
568+ *
569+ * \param ctx The initialized LMS context from which the public
570+ * key will be read.
571+ * \param msg The buffer from which the message will be read.
572+ * \param msg_len The size of the message that will be read.
573+ * \param sig The buf from which the signature will be read.
574+ * #MBEDTLS_LMS_SIG_LEN bytes will be read from
575+ * this.
576+ *
577+ * \return \c 0 on successful verification.
578+ * \return A non-zero error code on failure.
579+ */
580+int mbedtls_lms_verify( const mbedtls_lms_context *ctx,
581+ const unsigned char *msg, unsigned int msg_len,
582+ const unsigned char *sig );
583+
584+/**
585+ * \brief This function imports an LMOTS public key into a
586+ * LMS context.
587+ *
588+ * \note Before this function is called, the context must
589+ * have been initialized.
590+ *
591+ * \note See IETF RFC8554 for details of the encoding of
592+ * this public key.
593+ *
594+ * \param ctx The initialized LMS context store the key in.
595+ * \param key The buffer from which the key will be read.
596+ * #MBEDTLS_LMS_PUBKEY_LEN bytes will be read from
597+ * this.
598+ *
599+ * \return \c 0 on success.
600+ * \return A non-zero error code on failure.
601+ */
602+int mbedtls_lms_import_pubkey( mbedtls_lms_context *ctx,
603+ const unsigned char *key );
604+
605+/**
606+ * \brief This function exports an LMOTS public key from a
607+ * LMS context that already contains a public key.
608+ *
609+ * \note Before this function is called, the context must
610+ * have been initialized and the context must contain
611+ * a public key.
612+ *
613+ * \note See IETF RFC8554 for details of the encoding of
614+ * this public key.
615+ *
616+ * \param ctx The initialized LMS context that contains the
617+ * publc key.
618+ * \param key The buffer into which the key will be output. Must
619+ * be at least #MBEDTLS_LMS_PUBKEY_LEN in size.
620+ *
621+ * \return \c 0 on success.
622+ * \return A non-zero error code on failure.
623+ */
624+int mbedtls_lms_export_pubkey( mbedtls_lms_context *ctx,
625+ unsigned char *key );
626+
627+/**
628+ * \brief This function generates an LMS public key from a
629+ * LMS context that already contains a private key.
630+ *
631+ * \note Before this function is called, the context must
632+ * have been initialized and the context must contain
633+ * a private key.
634+ *
635+ * \param ctx The initialized LMS context to generate the key
636+ * from and store it into.
637+ *
638+ * \return \c 0 on success.
639+ * \return A non-zero error code on failure.
640+ */
641+int mbedtls_lms_gen_pubkey( mbedtls_lms_context *ctx );
642+
643+/**
644+ * \brief This function generates an LMS private key, and
645+ * stores in into an LMS context.
646+ *
647+ * \note Before this function is called, the context must
648+ * have been initialized and the type of the LMS
649+ * context set using mbedtls_lmots_set_algorithm_type
650+ *
651+ * \note The seed must have at least 256 bits of entropy.
652+ *
653+ * \param ctx The initialized LMOTS context to generate the key
654+ * into.
655+ * \param f_rng The RNG function to be used to generate the key ID.
656+ * \param p_rng The RNG context to be passed to f_rng
657+ * \param seed The seed used to deterministically generate the
658+ * key.
659+ * \param seed_len The length of the seed.
660+ *
661+ * \return \c 0 on success.
662+ * \return A non-zero error code on failure.
663+ */
664+int mbedtls_lms_gen_privkey( mbedtls_lms_context *ctx,
665+ int (*f_rng)(void *, unsigned char *, size_t),
666+ void* p_rng, unsigned char *seed,
667+ size_t seed_len );
668+
669+#ifdef __cplusplus
670+}
671+#endif
672+
673+#endif /* MBEDTLS_LMS_H */
674diff --git a/include/mbedtls/mbedtls_config.h b/include/mbedtls/mbedtls_config.h
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100675index 1c60ec8e4..dd2841459 100644
Raef Coles8a3bf392022-06-22 13:53:59 +0100676--- a/include/mbedtls/mbedtls_config.h
677+++ b/include/mbedtls/mbedtls_config.h
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100678@@ -2405,6 +2405,34 @@
Raef Coles8a3bf392022-06-22 13:53:59 +0100679 */
680 #define MBEDTLS_HMAC_DRBG_C
681
682+/**
683+ * \def MBEDTLS_LMOTS_C
684+ *
685+ * Enable the LMOTS one-time asymmetric hash signature algorithm.
686+ *
687+ * Module: library/lm_ots.c
688+ * Caller:
689+ *
690+ * Requires: MBEDTLS_SHA256_C
691+ *
692+ * Uncomment to enable the LMOTS signature algorithm.
693+ */
694+#define MBEDTLS_LMOTS_C
695+
696+/**
697+ * \def MBEDTLS_LMS_C
698+ *
699+ * Enable the LMS stateful-hash asymmetric signature algorithm.
700+ *
701+ * Module: library/lms.c
702+ * Caller:
703+ *
704+ * Requires: MBEDTLS_LMS_C
705+ *
706+ * Uncomment to enable the LMS signature algorithm.
707+ */
708+#define MBEDTLS_LMS_C
709+
710 /**
711 * \def MBEDTLS_NIST_KW_C
712 *
713diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100714index 0884f57ae..f52195be3 100644
Raef Coles8a3bf392022-06-22 13:53:59 +0100715--- a/library/CMakeLists.txt
716+++ b/library/CMakeLists.txt
717@@ -40,6 +40,8 @@ set(src_crypto
718 gcm.c
719 hkdf.c
720 hmac_drbg.c
721+ lmots.c
722+ lms.c
723 md.c
724 md5.c
725 memory_buffer_alloc.c
726diff --git a/library/Makefile b/library/Makefile
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100727index f5ff474ec..dfe76c139 100644
Raef Coles8a3bf392022-06-22 13:53:59 +0100728--- a/library/Makefile
729+++ b/library/Makefile
730@@ -105,6 +105,8 @@ OBJS_CRYPTO= \
731 gcm.o \
732 hkdf.o \
733 hmac_drbg.o \
734+ lmots.o \
735+ lms.o \
736 md.o \
737 md5.o \
738 memory_buffer_alloc.o \
739diff --git a/library/lmots.c b/library/lmots.c
740new file mode 100644
Antonio de Angelis90bee0f2022-07-13 11:22:41 +0100741index 000000000..7319d29be
Raef Coles8a3bf392022-06-22 13:53:59 +0100742--- /dev/null
743+++ b/library/lmots.c
744@@ -0,0 +1,684 @@
745+/*
746+ * The LM-OTS one-time public-key signature scheme
747+ *
748+ * Copyright The Mbed TLS Contributors
749+ * SPDX-License-Identifier: Apache-2.0
750+ *
751+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
752+ * not use this file except in compliance with the License.
753+ * You may obtain a copy of the License at
754+ *
755+ * http://www.apache.org/licenses/LICENSE-2.0
756+ *
757+ * Unless required by applicable law or agreed to in writing, software
758+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
759+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
760+ * See the License for the specific language governing permissions and
761+ * limitations under the License.
762+ */
763+
764+/*
765+ * The following sources were referenced in the design of this implementation
766+ * of the LM-OTS algorithm:
767+ *
768+ * [1] IETF RFC8554
769+ * D. McGrew, M. Curcio, S.Fluhrer
770+ * https://datatracker.ietf.org/doc/html/rfc8554
771+ *
772+ * [2] NIST Special Publication 800-208
773+ * David A. Cooper et. al.
774+ * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-208.pdf
775+ */
776+
777+#include "common.h"
778+
779+#ifdef MBEDTLS_LMOTS_C
780+
781+#include <string.h>
782+
783+#include "mbedtls/lmots.h"
784+#include "mbedtls/md.h"
785+#include "mbedtls/platform_util.h"
786+#include "mbedtls/error.h"
787+
788+#define W_SYMBOL_BIT_LEN (8)
789+#define CHECKSUM_LEN (2)
790+#define I_SYMBOL_IDX_LEN (2)
791+#define J_HASH_IDX_LEN (1)
792+#define D_CONST_LEN (2)
793+
794+#define SYMBOL_MAX_VAL ((1 << W_SYMBOL_BIT_LEN) - 1)
795+
796+#define D_PBLC_CONSTANT (0x8080)
797+#define D_MESG_CONSTANT (0x8181)
798+
799+static void val_to_network_bytes(unsigned int val, size_t len, unsigned char *bytes)
800+{
801+ size_t idx;
802+
803+ for (idx = 0; idx < len; idx++) {
804+ bytes[idx] = (val >> ((len - 1 - idx) * 8)) & 0xFF;
805+ }
806+}
807+
808+static unsigned int network_bytes_to_val(size_t len, const unsigned char *bytes)
809+{
810+ size_t idx;
811+ unsigned int val = 0;
812+
813+ for (idx = 0; idx < len; idx++) {
814+ val |= ((unsigned int)bytes[idx]) << (8 * (len - 1 - idx));
815+ }
816+
817+ return val;
818+}
819+
820+static unsigned short lmots_checksum_generate( const unsigned char* digest )
821+{
822+ size_t idx;
823+ unsigned short sum = 0;
824+
825+ for ( idx = 0; idx < MBEDTLS_LMOTS_N_HASH_LEN; idx++ )
826+ {
827+ sum += ( 1 << W_SYMBOL_BIT_LEN ) - 1 - digest[idx];
828+ }
829+
830+ return sum;
831+}
832+
833+static int create_symbol_array( const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
834+ const unsigned char q_leaf_identifier[MBEDTLS_LMOTS_Q_LEAF_ID_LEN],
835+ const unsigned char *msg,
836+ size_t msg_len,
837+ const unsigned char C_random_value[MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN],
838+ unsigned char out[MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN] )
839+{
840+ mbedtls_md_context_t hash_ctx;
841+ unsigned char D_MESG_BYTES[D_CONST_LEN];
842+ unsigned short checksum;
843+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
844+
845+ mbedtls_md_init( &hash_ctx );
846+ ret = mbedtls_md_setup( &hash_ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 0 );
847+ if( ret )
848+ {
849+ goto out;
850+ }
851+ ret = mbedtls_md_starts( &hash_ctx );
852+ if ( ret )
853+ {
854+ goto out;
855+ }
856+
857+ ret = mbedtls_md_update( &hash_ctx, I_key_identifier, MBEDTLS_LMOTS_I_KEY_ID_LEN );
858+ if ( ret )
859+ {
860+ goto out;
861+ }
862+
863+ ret = mbedtls_md_update( &hash_ctx, q_leaf_identifier, MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
864+ if ( ret )
865+ {
866+ goto out;
867+ }
868+
869+ val_to_network_bytes( D_MESG_CONSTANT, D_CONST_LEN, D_MESG_BYTES );
870+ ret = mbedtls_md_update( &hash_ctx, D_MESG_BYTES, sizeof( D_MESG_BYTES ) );
871+ if ( ret )
872+ {
873+ goto out;
874+ }
875+
876+ ret = mbedtls_md_update( &hash_ctx, C_random_value, MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN );
877+ if ( ret )
878+ {
879+ goto out;
880+ }
881+
882+ ret = mbedtls_md_update( &hash_ctx, msg, msg_len );
883+ if ( ret )
884+ {
885+ goto out;
886+ }
887+
888+ ret = mbedtls_md_finish( &hash_ctx, out );
889+ if ( ret )
890+ {
891+ goto out;
892+ }
893+
894+ checksum = lmots_checksum_generate( out );
895+ val_to_network_bytes( checksum, CHECKSUM_LEN, out + MBEDTLS_LMOTS_N_HASH_LEN );
896+
897+out:
898+ mbedtls_md_free( &hash_ctx );
899+
900+ return( ret );
901+}
902+
903+static int hash_symbol_array( const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
904+ const unsigned char q_leaf_identifier[MBEDTLS_LMOTS_Q_LEAF_ID_LEN],
905+ const unsigned char x_symbol_array[MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN][32],
906+ const unsigned char hash_idx_min_values[MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN],
907+ const unsigned char hash_idx_max_values[MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN],
908+ unsigned char output[MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN][32] )
909+{
910+ unsigned char i_symbol_idx;
911+ unsigned char j_hash_idx;
912+ unsigned char i_symbol_idx_bytes[I_SYMBOL_IDX_LEN];
913+ unsigned char j_hash_idx_bytes[1];
914+ unsigned short j_hash_idx_min;
915+ unsigned short j_hash_idx_max;
916+ mbedtls_md_context_t hash_ctx;
917+ unsigned char tmp_hash[32];
918+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
919+
920+ for ( i_symbol_idx = 0; i_symbol_idx < MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN; i_symbol_idx++ )
921+ {
922+
923+ memcpy( tmp_hash, &x_symbol_array[i_symbol_idx], MBEDTLS_LMOTS_N_HASH_LEN );
924+
925+ j_hash_idx_min = hash_idx_min_values != NULL ? hash_idx_min_values[i_symbol_idx] : 0;
926+ j_hash_idx_max = hash_idx_max_values != NULL ? hash_idx_max_values[i_symbol_idx] : SYMBOL_MAX_VAL;
927+
928+ for ( j_hash_idx = (unsigned char)j_hash_idx_min; j_hash_idx < j_hash_idx_max; j_hash_idx++ )
929+ {
930+ mbedtls_md_init( &hash_ctx );
931+ ret = mbedtls_md_setup( &hash_ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 0 );
932+ if( ret )
933+ {
934+ goto out;
935+ }
936+ ret = mbedtls_md_starts( &hash_ctx );
937+ if ( ret )
938+ {
939+ goto out;
940+ }
941+
942+ ret = mbedtls_md_update( &hash_ctx, I_key_identifier, MBEDTLS_LMOTS_I_KEY_ID_LEN );
943+ if ( ret )
944+ {
945+ goto out;
946+ }
947+
948+ ret = mbedtls_md_update( &hash_ctx, q_leaf_identifier, MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
949+ if ( ret )
950+ {
951+ goto out;
952+ }
953+
954+ val_to_network_bytes( i_symbol_idx, I_SYMBOL_IDX_LEN, i_symbol_idx_bytes );
955+ ret = mbedtls_md_update( &hash_ctx, i_symbol_idx_bytes, I_SYMBOL_IDX_LEN );
956+ if ( ret )
957+ {
958+ goto out;
959+ }
960+
961+ val_to_network_bytes( j_hash_idx, J_HASH_IDX_LEN, j_hash_idx_bytes );
962+ ret = mbedtls_md_update( &hash_ctx, j_hash_idx_bytes, J_HASH_IDX_LEN );
963+ if ( ret )
964+ {
965+ goto out;
966+ }
967+
968+ ret = mbedtls_md_update( &hash_ctx, tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN );
969+ if ( ret )
970+ {
971+ goto out;
972+ }
973+
974+ ret = mbedtls_md_finish( &hash_ctx, tmp_hash );
975+ if ( ret )
976+ {
977+ goto out;
978+ }
979+
980+ mbedtls_md_free( &hash_ctx );
981+ }
982+
983+ memcpy( &output[i_symbol_idx], tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN );
984+ }
985+
986+out:
987+ if( ret )
988+ {
989+ mbedtls_md_free( &hash_ctx );
990+ return( ret );
991+ }
992+
993+ return ret;
994+}
995+
996+static int public_key_from_hashed_symbol_array( const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
997+ const unsigned char q_leaf_identifier[MBEDTLS_LMOTS_Q_LEAF_ID_LEN],
998+ const unsigned char y_hashed_symbols[MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN][32],
999+ unsigned char *pub_key )
1000+{
1001+ unsigned char D_PBLC_bytes[D_CONST_LEN];
1002+ mbedtls_md_context_t hash_ctx;
1003+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1004+
1005+ mbedtls_md_init( &hash_ctx );
1006+ ret = mbedtls_md_setup( &hash_ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 0 );
1007+ if( ret )
1008+ {
1009+ goto out;
1010+ }
1011+ ret = mbedtls_md_starts( &hash_ctx );
1012+ if ( ret )
1013+ {
1014+ goto out;
1015+ }
1016+
1017+ ret = mbedtls_md_update( &hash_ctx, I_key_identifier,
1018+ MBEDTLS_LMOTS_I_KEY_ID_LEN );
1019+ if ( ret )
1020+ {
1021+ goto out;
1022+ }
1023+
1024+ ret = mbedtls_md_update( &hash_ctx, q_leaf_identifier,
1025+ MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
1026+ if ( ret )
1027+ {
1028+ goto out;
1029+ }
1030+
1031+ val_to_network_bytes( D_PBLC_CONSTANT, D_CONST_LEN, D_PBLC_bytes );
1032+ ret = mbedtls_md_update( &hash_ctx, D_PBLC_bytes, D_CONST_LEN );
1033+ if ( ret )
1034+ {
1035+ goto out;
1036+ }
1037+
1038+ ret = mbedtls_md_update( &hash_ctx, ( unsigned char * )y_hashed_symbols,
1039+ MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN * MBEDTLS_LMOTS_N_HASH_LEN );
1040+ if ( ret )
1041+ {
1042+ goto out;
1043+ }
1044+
1045+ ret = mbedtls_md_finish( &hash_ctx, pub_key );
1046+
1047+out:
1048+ mbedtls_md_free( &hash_ctx );
1049+ return( ret );
1050+}
1051+
1052+void mbedtls_lmots_init( mbedtls_lmots_context *ctx )
1053+{
1054+ if( ctx == NULL ) {
1055+ return;
1056+ }
1057+
1058+ mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_context ) ) ;
1059+}
1060+
1061+void mbedtls_lmots_free( mbedtls_lmots_context *ctx )
1062+{
1063+ if( ctx == NULL )
1064+ {
1065+ return;
1066+ }
1067+
1068+ mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_context ) ) ;
1069+}
1070+
1071+int mbedtls_lmots_set_algorithm_type( mbedtls_lmots_context *ctx,
1072+ mbedtls_lmots_algorithm_type_t type )
1073+{
1074+ if( ctx == NULL )
1075+ {
1076+ return( MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA );
1077+ }
1078+
1079+ ctx->MBEDTLS_PRIVATE(type) = type;
1080+
1081+ return( 0 );
1082+}
1083+
1084+int mbedtls_lmots_generate_pub_key_candidate( const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
1085+ const unsigned char q_leaf_identifier[MBEDTLS_LMOTS_Q_LEAF_ID_LEN],
1086+ const unsigned char *msg,
1087+ size_t msg_len,
1088+ const unsigned char *sig,
1089+ unsigned char *out )
1090+{
1091+ unsigned char tmp_symbol_array[MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN];
1092+ unsigned char y_hashed_symbols[MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN][32];
1093+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1094+
1095+ if (I_key_identifier == NULL || msg == NULL || sig == NULL || out == NULL)
1096+ {
1097+ return( MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA );
1098+ }
1099+
1100+ ret = create_symbol_array( I_key_identifier, q_leaf_identifier, msg, msg_len,
1101+ sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, tmp_symbol_array );
1102+ if ( ret )
1103+ {
1104+ return ( ret );
1105+ }
1106+
1107+ ret = hash_symbol_array( I_key_identifier, q_leaf_identifier,
1108+ ( const unsigned char( *)[32] )(sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET),
1109+ tmp_symbol_array, NULL, y_hashed_symbols );
1110+ if ( ret )
1111+ {
1112+ return ( ret );
1113+ }
1114+
1115+ ret = public_key_from_hashed_symbol_array( I_key_identifier, q_leaf_identifier,
1116+ ( const unsigned char( *)[32] )y_hashed_symbols,
1117+ out );
1118+ if ( ret )
1119+ {
1120+ return ( ret );
1121+ }
1122+
1123+ return( 0 );
1124+}
1125+
1126+int mbedtls_lmots_sign( mbedtls_lmots_context *ctx,
1127+ int (*f_rng)(void *, unsigned char *, size_t),
1128+ void *p_rng, const unsigned char *msg, size_t msg_len,
1129+ unsigned char *sig )
1130+{
1131+ unsigned char tmp_symbol_array[MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN];
1132+ unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN][MBEDTLS_LMOTS_N_HASH_LEN];
1133+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1134+
1135+ if( ctx == NULL || f_rng == NULL || p_rng == NULL || msg == NULL || sig == NULL)
1136+ {
1137+ return( MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA );
1138+ }
1139+
1140+ /* Check that a private key is loaded */
1141+ if ( !ctx->MBEDTLS_PRIVATE(have_privkey) )
1142+ {
1143+ return( MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA );
1144+ }
1145+
1146+ ret = f_rng( p_rng, sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, MBEDTLS_LMOTS_N_HASH_LEN );
1147+ if ( ret )
1148+ {
1149+ return( ret );
1150+ }
1151+
1152+ ret = create_symbol_array( ctx->MBEDTLS_PRIVATE(I_key_identifier),
1153+ ctx->MBEDTLS_PRIVATE(q_leaf_identifier_bytes),
1154+ msg, msg_len, sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET,
1155+ tmp_symbol_array );
1156+ if ( ret )
1157+ {
1158+ return( ret );
1159+ }
1160+
1161+ ret = hash_symbol_array( ctx->MBEDTLS_PRIVATE(I_key_identifier),
1162+ ctx->MBEDTLS_PRIVATE(q_leaf_identifier_bytes),
1163+ ( const unsigned char( *)[32] )(ctx->MBEDTLS_PRIVATE(priv_key)),
1164+ NULL, tmp_symbol_array, tmp_sig );
1165+ if ( ret )
1166+ {
1167+ return( ret );
1168+ }
1169+
1170+ val_to_network_bytes( ctx->MBEDTLS_PRIVATE(type), MBEDTLS_LMOTS_TYPE_LEN,
1171+ sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
1172+
1173+ /* We've got a valid signature now, so it's time to make sure the private
1174+ * key can't be reused.
1175+ */
1176+ ctx->MBEDTLS_PRIVATE(have_privkey) = 0;
1177+ mbedtls_platform_zeroize(ctx->MBEDTLS_PRIVATE(priv_key),
1178+ sizeof(ctx->MBEDTLS_PRIVATE(priv_key)));
1179+
1180+ memcpy(sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET, tmp_sig,
1181+ MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN * MBEDTLS_LMOTS_N_HASH_LEN);
1182+
1183+ return( 0 );
1184+}
1185+
1186+int mbedtls_lmots_verify( mbedtls_lmots_context *ctx, const unsigned char *msg,
1187+ size_t msg_len, const unsigned char *sig )
1188+{
1189+ unsigned char Kc_public_key_candidate[32];
1190+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1191+
1192+ if( ctx == NULL || msg == NULL || sig == NULL)
1193+ {
1194+ return( MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA );
1195+ }
1196+
1197+ if ( !ctx->MBEDTLS_PRIVATE(have_pubkey) )
1198+ {
1199+ return( MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA );
1200+ }
1201+
1202+ if( ctx->MBEDTLS_PRIVATE(type ) != MBEDTLS_LMOTS_SHA256_N32_W8 )
1203+ {
1204+ return( MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA );
1205+ }
1206+
1207+ if ( network_bytes_to_val( MBEDTLS_LMOTS_TYPE_LEN,
1208+ sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ) != MBEDTLS_LMOTS_SHA256_N32_W8 )
1209+ {
1210+ return( MBEDTLS_ERR_LMOTS_VERIFY_FAILED );
1211+ }
1212+
1213+ ret = mbedtls_lmots_generate_pub_key_candidate( ctx->MBEDTLS_PRIVATE(I_key_identifier),
1214+ ctx->MBEDTLS_PRIVATE(q_leaf_identifier_bytes),
1215+ msg, msg_len, sig,
1216+ Kc_public_key_candidate );
1217+ if ( ret )
1218+ {
1219+ return( ret );
1220+ }
1221+
1222+ if ( memcmp( &Kc_public_key_candidate, ctx->MBEDTLS_PRIVATE(pub_key),
1223+ sizeof( ctx->MBEDTLS_PRIVATE(pub_key) ) ) )
1224+ {
1225+ return( MBEDTLS_ERR_LMOTS_VERIFY_FAILED );
1226+ }
1227+
1228+ return( 0 );
1229+}
1230+
1231+int mbedtls_lmots_import_pubkey( mbedtls_lmots_context *ctx,
1232+ const unsigned char *key )
1233+{
1234+ if ( ctx == NULL || key == NULL)
1235+ {
1236+ return( MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA );
1237+ }
1238+
1239+ ctx->MBEDTLS_PRIVATE(type) = network_bytes_to_val( MBEDTLS_LMOTS_TYPE_LEN,
1240+ key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
1241+
1242+ memcpy( ctx->MBEDTLS_PRIVATE(I_key_identifier), key + MBEDTLS_LMOTS_PUBKEY_I_KEY_ID_OFFSET,
1243+ MBEDTLS_LMOTS_I_KEY_ID_LEN );
1244+
1245+ memcpy( ctx->MBEDTLS_PRIVATE(q_leaf_identifier_bytes), key + MBEDTLS_LMOTS_PUBKEY_Q_LEAF_ID_OFFSET,
1246+ MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
1247+ ctx->MBEDTLS_PRIVATE(q_leaf_identifier) = network_bytes_to_val( MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
1248+ ctx->MBEDTLS_PRIVATE(q_leaf_identifier_bytes) );
1249+
1250+ memcpy( ctx->MBEDTLS_PRIVATE(pub_key), key + MBEDTLS_LMOTS_PUBKEY_KEY_HASH_OFFSET, MBEDTLS_LMOTS_N_HASH_LEN );
1251+
1252+ ctx->MBEDTLS_PRIVATE(have_pubkey) = 1;
1253+
1254+ return( 0 );
1255+}
1256+
1257+int mbedtls_lmots_export_pubkey( mbedtls_lmots_context *ctx,
1258+ unsigned char *key )
1259+{
1260+ if ( ctx == NULL || key == NULL)
1261+ {
1262+ return( MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA );
1263+ }
1264+
1265+ if ( ! ctx->MBEDTLS_PRIVATE(have_pubkey) )
1266+ {
1267+ return( MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA );
1268+ }
1269+
1270+ val_to_network_bytes( ctx->MBEDTLS_PRIVATE(type), MBEDTLS_LMOTS_TYPE_LEN,
1271+ key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET );
1272+
1273+ memcpy( key + MBEDTLS_LMOTS_PUBKEY_I_KEY_ID_OFFSET, ctx->MBEDTLS_PRIVATE(I_key_identifier),
1274+ MBEDTLS_LMOTS_I_KEY_ID_LEN );
1275+
1276+ memcpy( key + MBEDTLS_LMOTS_PUBKEY_Q_LEAF_ID_OFFSET, ctx->MBEDTLS_PRIVATE(q_leaf_identifier_bytes),
1277+ MBEDTLS_LMOTS_Q_LEAF_ID_LEN );
1278+
1279+ memcpy( key + MBEDTLS_LMOTS_PUBKEY_KEY_HASH_OFFSET, ctx->MBEDTLS_PRIVATE(pub_key),
1280+ MBEDTLS_LMOTS_N_HASH_LEN );
1281+
1282+ return( 0 );
1283+}
1284+
1285+
1286+int mbedtls_lmots_gen_pubkey( mbedtls_lmots_context *ctx )
1287+{
1288+ unsigned char y_hashed_symbols[MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN][32];
1289+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1290+
1291+ if( ctx == NULL )
1292+ {
1293+ return( MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA );
1294+ }
1295+
1296+ /* Check that a private key is loaded */
1297+ if ( !ctx->MBEDTLS_PRIVATE(have_privkey) )
1298+ {
1299+ return( MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA );
1300+ }
1301+
1302+ ret = hash_symbol_array( ctx->MBEDTLS_PRIVATE(I_key_identifier),
1303+ ctx->MBEDTLS_PRIVATE(q_leaf_identifier_bytes),
1304+ ( const unsigned char( *)[32] )(ctx->MBEDTLS_PRIVATE(priv_key)),
1305+ NULL, NULL, y_hashed_symbols );
1306+ if ( ret )
1307+ {
1308+ return( ret );
1309+ }
1310+
1311+ ret = public_key_from_hashed_symbol_array( ctx->MBEDTLS_PRIVATE(I_key_identifier),
1312+ ctx->MBEDTLS_PRIVATE(q_leaf_identifier_bytes),
1313+ ( const unsigned char( *)[32] )y_hashed_symbols,
1314+ ctx->MBEDTLS_PRIVATE(pub_key) );
1315+ if ( ret )
1316+ {
1317+ return( ret );
1318+ }
1319+
1320+ ctx->MBEDTLS_PRIVATE(have_pubkey = 1);
1321+
1322+ return( ret );
1323+}
1324+
1325+int mbedtls_lmots_gen_privkey( mbedtls_lmots_context *ctx,
1326+ const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
1327+ unsigned int q_leaf_identifier,
1328+ const unsigned char *seed,
1329+ size_t seed_len )
1330+{
1331+ mbedtls_md_context_t hash_ctx;
1332+ unsigned int i_symbol_idx;
1333+ unsigned char i_symbol_idx_bytes[2];
1334+ unsigned char const_bytes[1];
1335+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1336+
1337+ if( ctx == NULL || I_key_identifier == NULL || seed == NULL)
1338+ {
1339+ return( MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA );
1340+ }
1341+
1342+ if ( ctx->MBEDTLS_PRIVATE(have_privkey) )
1343+ {
1344+ return( MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA );
1345+ }
1346+
1347+ if ( ctx->MBEDTLS_PRIVATE(type) != MBEDTLS_LMOTS_SHA256_N32_W8 ) {
1348+ return( MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA );
1349+ }
1350+
1351+ memcpy( ctx->MBEDTLS_PRIVATE(I_key_identifier), I_key_identifier,
1352+ sizeof( ctx->MBEDTLS_PRIVATE(I_key_identifier) ) );
1353+
1354+ ctx->MBEDTLS_PRIVATE(q_leaf_identifier) = q_leaf_identifier;
1355+
1356+ val_to_network_bytes( ctx->MBEDTLS_PRIVATE(q_leaf_identifier), MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
1357+ ctx->MBEDTLS_PRIVATE(q_leaf_identifier_bytes) );
1358+
1359+ val_to_network_bytes( 0xFF, sizeof( const_bytes ), const_bytes );
1360+
1361+ for ( i_symbol_idx = 0; i_symbol_idx < MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN; i_symbol_idx++ )
1362+ {
1363+ mbedtls_md_init( &hash_ctx );
1364+ ret = mbedtls_md_setup( &hash_ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 0 );
1365+ if( ret )
1366+ {
1367+ goto out;
1368+ }
1369+ ret = mbedtls_md_starts( &hash_ctx );
1370+ if ( ret )
1371+ {
1372+ goto out;
1373+ }
1374+
1375+ ret = mbedtls_md_update( &hash_ctx, ctx->MBEDTLS_PRIVATE(I_key_identifier),
1376+ sizeof( ctx->MBEDTLS_PRIVATE(I_key_identifier) ) );
1377+ if ( ret ) {
1378+ goto out;
1379+ }
1380+
1381+ ret = mbedtls_md_update( &hash_ctx, ctx->MBEDTLS_PRIVATE(q_leaf_identifier_bytes),
1382+ sizeof( ctx->MBEDTLS_PRIVATE(q_leaf_identifier_bytes) ) );
1383+ if ( ret )
1384+ {
1385+ goto out;
1386+ }
1387+
1388+ val_to_network_bytes( i_symbol_idx, I_SYMBOL_IDX_LEN, i_symbol_idx_bytes );
1389+ ret = mbedtls_md_update( &hash_ctx, i_symbol_idx_bytes, I_SYMBOL_IDX_LEN );
1390+ if ( ret )
1391+ {
1392+ goto out;
1393+ }
1394+
1395+ ret = mbedtls_md_update( &hash_ctx, const_bytes, sizeof( const_bytes) );
1396+ if ( ret )
1397+ {
1398+ goto out;
1399+ }
1400+
1401+ ret = mbedtls_md_update( &hash_ctx, seed, seed_len );
1402+ if ( ret )
1403+ {
1404+ goto out;
1405+ }
1406+
1407+ ret = mbedtls_md_finish( &hash_ctx, ctx->MBEDTLS_PRIVATE(priv_key)[i_symbol_idx] );
1408+ if ( ret )
1409+ {
1410+ goto out;
1411+ }
1412+
1413+ mbedtls_md_free( &hash_ctx);
1414+ }
1415+
1416+ ctx->MBEDTLS_PRIVATE(have_privkey) = 1;
1417+
1418+out:
1419+ if( ret )
1420+ {
1421+ mbedtls_md_free( &hash_ctx );
1422+ return( ret );
1423+ }
1424+
1425+ return ret;
1426+}
1427+
1428+#endif /* MBEDTLS_LMOTS_C */
1429diff --git a/library/lms.c b/library/lms.c
1430new file mode 100644
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01001431index 000000000..e1ac7b935
Raef Coles8a3bf392022-06-22 13:53:59 +01001432--- /dev/null
1433+++ b/library/lms.c
1434@@ -0,0 +1,718 @@
1435+/*
1436+ * The LMS stateful-hash public-key signature scheme
1437+ *
1438+ * Copyright The Mbed TLS Contributors
1439+ * SPDX-License-Identifier: Apache-2.0
1440+ *
1441+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
1442+ * not use this file except in compliance with the License.
1443+ * You may obtain a copy of the License at
1444+ *
1445+ * http://www.apache.org/licenses/LICENSE-2.0
1446+ *
1447+ * Unless required by applicable law or agreed to in writing, software
1448+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
1449+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1450+ * See the License for the specific language governing permissions and
1451+ * limitations under the License.
1452+ */
1453+
1454+/*
1455+ * The following sources were referenced in the design of this implementation
1456+ * of the LMS algorithm:
1457+ *
1458+ * [1] IETF RFC8554
1459+ * D. McGrew, M. Curcio, S.Fluhrer
1460+ * https://datatracker.ietf.org/doc/html/rfc8554
1461+ *
1462+ * [2] NIST Special Publication 800-208
1463+ * David A. Cooper et. al.
1464+ * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-208.pdf
1465+ */
1466+
1467+#include "common.h"
1468+
1469+#ifdef MBEDTLS_LMS_C
1470+
1471+#include <string.h>
1472+
1473+#include "mbedtls/lms.h"
1474+#include "mbedtls/lmots.h"
1475+#include "mbedtls/md.h"
1476+#include "mbedtls/error.h"
1477+#include "mbedtls/platform_util.h"
1478+
1479+#if defined(MBEDTLS_PLATFORM_C)
1480+#include "mbedtls/platform.h"
1481+#else
1482+#include <stdlib.h>
1483+#include <stdio.h>
1484+#define mbedtls_printf printf
1485+#define mbedtls_calloc calloc
1486+#define mbedtls_free free
1487+#endif
1488+
1489+#define MERKLE_TREE_NODE_AM (1 << (MBEDTLS_LMS_H_TREE_HEIGHT + 1))
1490+#define MERKLE_TREE_LEAF_AM (1 << MBEDTLS_LMS_H_TREE_HEIGHT)
1491+#define MERKLE_TREE_INTR_AM (1 << MBEDTLS_LMS_H_TREE_HEIGHT)
1492+
1493+#define D_CONST_LEN (2)
1494+
1495+#define D_LEAF_CONSTANT (0x8282)
1496+#define D_INTR_CONSTANT (0x8383)
1497+
1498+static void val_to_network_bytes(unsigned int val, size_t len, unsigned char *bytes)
1499+{
1500+ size_t idx;
1501+
1502+ for (idx = 0; idx < len; idx++) {
1503+ bytes[idx] = (val >> ((len - 1 - idx) * 8)) & 0xFF;
1504+ }
1505+}
1506+
1507+static unsigned int network_bytes_to_val(size_t len, const unsigned char *bytes)
1508+{
1509+ size_t idx;
1510+ unsigned int val = 0;
1511+
1512+ for (idx = 0; idx < len; idx++) {
1513+ val |= ((unsigned int)bytes[idx]) << (8 * (len - 1 - idx));
1514+ }
1515+
1516+ return val;
1517+}
1518+
1519+static int create_merkle_leaf_node( const mbedtls_lms_context *ctx,
1520+ unsigned char pub_key[MBEDTLS_LMOTS_N_HASH_LEN],
1521+ unsigned int r_node_idx,
1522+ unsigned char out[32] )
1523+{
1524+ mbedtls_md_context_t hash_ctx;
1525+ unsigned char D_LEAF_bytes[D_CONST_LEN];
1526+ unsigned char r_node_idx_bytes[4];
1527+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1528+
1529+ mbedtls_md_init( &hash_ctx );
1530+ ret = mbedtls_md_setup( &hash_ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 0 );
1531+ if( ret )
1532+ {
1533+ goto out;
1534+ }
1535+ ret = mbedtls_md_starts( &hash_ctx );
1536+ if( ret )
1537+ {
1538+ goto out;
1539+ }
1540+
1541+ ret = mbedtls_md_update( &hash_ctx,
1542+ ctx->MBEDTLS_PRIVATE(I_key_identifier),
1543+ MBEDTLS_LMOTS_I_KEY_ID_LEN );
1544+ if( ret )
1545+ {
1546+ goto out;
1547+ }
1548+
1549+ val_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
1550+ ret = mbedtls_md_update( &hash_ctx, r_node_idx_bytes, 4 );
1551+ if( ret )
1552+ {
1553+ goto out;
1554+ }
1555+
1556+ val_to_network_bytes( D_LEAF_CONSTANT, D_CONST_LEN, D_LEAF_bytes );
1557+ ret = mbedtls_md_update( &hash_ctx, D_LEAF_bytes, D_CONST_LEN );
1558+ if( ret )
1559+ {
1560+ goto out;
1561+ }
1562+
1563+ ret = mbedtls_md_update( &hash_ctx, pub_key, MBEDTLS_LMOTS_N_HASH_LEN );
1564+ if( ret )
1565+ {
1566+ goto out;
1567+ }
1568+
1569+ ret = mbedtls_md_finish( &hash_ctx, out );
1570+ if( ret )
1571+ {
1572+ goto out;
1573+ }
1574+
1575+out:
1576+ mbedtls_md_free( &hash_ctx );
1577+
1578+ return( ret );
1579+}
1580+
1581+static int create_merkle_intr_node( const mbedtls_lms_context *ctx,
1582+ const unsigned char left_node[32],
1583+ const unsigned char rght_node[32],
1584+ unsigned int r_node_idx,
1585+ unsigned char out[32] )
1586+{
1587+ mbedtls_md_context_t hash_ctx;
1588+ unsigned char D_INTR_bytes[D_CONST_LEN];
1589+ unsigned char r_node_idx_bytes[4];
1590+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1591+
1592+ mbedtls_md_init( &hash_ctx );
1593+ ret = mbedtls_md_setup( &hash_ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 0 );
1594+ if( ret )
1595+ {
1596+ goto out;
1597+ }
1598+ ret = mbedtls_md_starts( &hash_ctx );
1599+ if( ret )
1600+ {
1601+ goto out;
1602+ }
1603+
1604+ ret = mbedtls_md_update( &hash_ctx, ctx->MBEDTLS_PRIVATE(I_key_identifier),
1605+ MBEDTLS_LMOTS_I_KEY_ID_LEN );
1606+ if( ret )
1607+ {
1608+ goto out;
1609+ }
1610+
1611+ val_to_network_bytes( r_node_idx, 4, r_node_idx_bytes );
1612+ ret = mbedtls_md_update( &hash_ctx, r_node_idx_bytes, 4 );
1613+ if( ret )
1614+ {
1615+ goto out;
1616+ }
1617+
1618+ val_to_network_bytes( D_INTR_CONSTANT, D_CONST_LEN, D_INTR_bytes );
1619+ ret = mbedtls_md_update( &hash_ctx, D_INTR_bytes, D_CONST_LEN );
1620+ if( ret )
1621+ {
1622+ goto out;
1623+ }
1624+
1625+ ret = mbedtls_md_update( &hash_ctx, left_node, MBEDTLS_LMOTS_N_HASH_LEN );
1626+ if( ret )
1627+ {
1628+ goto out;
1629+ }
1630+
1631+ ret = mbedtls_md_update( &hash_ctx, rght_node, MBEDTLS_LMOTS_N_HASH_LEN );
1632+ if( ret )
1633+ {
1634+ goto out;
1635+ }
1636+
1637+ ret = mbedtls_md_finish( &hash_ctx, out );
1638+ if( ret )
1639+ {
1640+ goto out;
1641+ }
1642+
1643+out:
1644+ mbedtls_md_free( &hash_ctx );
1645+
1646+ return ret;
1647+}
1648+
1649+static int generate_merkle_tree( mbedtls_lms_context *ctx,
1650+ unsigned char tree[MERKLE_TREE_NODE_AM][32] )
1651+{
1652+ unsigned int priv_key_idx;
1653+ unsigned int r_node_idx;
1654+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1655+
1656+ /* First create the leaf nodes, in ascending order */
1657+ for( priv_key_idx = 0; priv_key_idx < MERKLE_TREE_INTR_AM; priv_key_idx++ )
1658+ {
1659+ r_node_idx = MERKLE_TREE_INTR_AM + priv_key_idx;
1660+
1661+ ret = create_merkle_leaf_node( ctx, ctx->MBEDTLS_PRIVATE(priv_keys)[priv_key_idx].pub_key,
1662+ r_node_idx, tree[r_node_idx] );
1663+ if( ret )
1664+ {
1665+ return( ret );
1666+ }
1667+ }
1668+
1669+ /* Then the internal nodes, in reverse order so that we can guarantee the
1670+ * parent has been created */
1671+ for( r_node_idx = MERKLE_TREE_INTR_AM - 1; r_node_idx > 0; r_node_idx-- )
1672+ {
1673+ ret = create_merkle_intr_node( ctx, tree[(r_node_idx * 2)],
1674+ tree[(r_node_idx * 2 + 1)],
1675+ r_node_idx, tree[r_node_idx] );
1676+ if( ret )
1677+ {
1678+ return( ret );
1679+ }
1680+ }
1681+
1682+ return( 0 );
1683+}
1684+
1685+static int get_merkle_path( mbedtls_lms_context *ctx,
1686+ unsigned int leaf_node_id, unsigned char path[MBEDTLS_LMS_H_TREE_HEIGHT][32] )
1687+{
1688+ unsigned char tree[MERKLE_TREE_NODE_AM][32];
1689+ unsigned int curr_node_id = leaf_node_id;
1690+ unsigned int parent_node_id;
1691+ unsigned char sibling_relative_id;
1692+ unsigned int adjacent_node_id;
1693+ unsigned int height;
1694+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1695+
1696+ ret = generate_merkle_tree( ctx, tree);
1697+ if( ret )
1698+ {
1699+ return( ret );
1700+ }
1701+
1702+ for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT; height++ )
1703+ {
1704+ parent_node_id = ( curr_node_id / 2 );
1705+
1706+ /* 0 if the node is a left child, 1 if the node is a right child */
1707+ sibling_relative_id = curr_node_id & 1;
1708+
1709+ adjacent_node_id = ( parent_node_id * 2 ) + ( 1 - sibling_relative_id );
1710+
1711+ memcpy( &path[height], &tree[adjacent_node_id], MBEDTLS_LMOTS_N_HASH_LEN );
1712+
1713+ curr_node_id = parent_node_id;
1714+ }
1715+
1716+ return( 0 );
1717+}
1718+
1719+void mbedtls_lms_init( mbedtls_lms_context *ctx )
1720+{
1721+ if( ctx == NULL )
1722+ {
1723+ return;
1724+ }
1725+
1726+ mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_context ) ) ;
1727+}
1728+
1729+void mbedtls_lms_free( mbedtls_lms_context *ctx )
1730+{
1731+ unsigned int idx;
1732+
1733+ if( ctx == NULL )
1734+ {
1735+ return;
1736+ }
1737+
1738+ if( ctx->MBEDTLS_PRIVATE(have_privkey) )
1739+ {
1740+ for( idx = 0; idx < MERKLE_TREE_LEAF_AM; idx++ )
1741+ {
1742+ mbedtls_lmots_free( &ctx->MBEDTLS_PRIVATE(priv_keys)[idx] );
1743+ }
1744+
1745+ mbedtls_free( ctx->MBEDTLS_PRIVATE(priv_keys) );
1746+ }
1747+
1748+ mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_context ) );
1749+}
1750+
1751+int mbedtls_lms_set_algorithm_type( mbedtls_lms_context *ctx,
1752+ mbedtls_lms_algorithm_type_t type,
1753+ mbedtls_lmots_algorithm_type_t otstype )
1754+{
1755+ if( ctx == NULL )
1756+ {
1757+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
1758+ }
1759+
1760+ ctx->MBEDTLS_PRIVATE(type) = type;
1761+ ctx->MBEDTLS_PRIVATE(otstype) = otstype;
1762+
1763+ return( 0 );
1764+}
1765+
1766+int mbedtls_lms_sign( mbedtls_lms_context *ctx,
1767+ int ( *f_rng)(void *, unsigned char *, size_t),
1768+ void* p_rng, unsigned char *msg, unsigned int msg_len,
1769+ unsigned char *sig )
1770+{
1771+ unsigned int q_leaf_identifier;
1772+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1773+
1774+ if( ctx == NULL )
1775+ {
1776+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
1777+ }
1778+
1779+ if( ! ctx->MBEDTLS_PRIVATE(have_privkey) )
1780+ {
1781+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
1782+ }
1783+
1784+ if( msg == NULL )
1785+ {
1786+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
1787+ }
1788+
1789+ if( sig == NULL )
1790+ {
1791+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
1792+ }
1793+
1794+
1795+ if( ctx->MBEDTLS_PRIVATE(type) != MBEDTLS_LMS_SHA256_M32_H10 )
1796+ {
1797+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
1798+ }
1799+
1800+ if( ctx->MBEDTLS_PRIVATE(otstype) != MBEDTLS_LMOTS_SHA256_N32_W8 )
1801+ {
1802+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
1803+ }
1804+
1805+
1806+ if( ctx->MBEDTLS_PRIVATE(q_next_usable_key) >= MERKLE_TREE_LEAF_AM )
1807+ {
1808+ return( MBEDTLS_ERR_LMS_OUT_OF_PRIV_KEYS );
1809+ }
1810+
1811+
1812+ q_leaf_identifier = ctx->MBEDTLS_PRIVATE(q_next_usable_key);
1813+ /* This new value must _always_ be written back to the disk before the
1814+ * signature is returned.
1815+ */
1816+ ctx->MBEDTLS_PRIVATE(q_next_usable_key) += 1;
1817+
1818+ ret = mbedtls_lmots_sign( &ctx->MBEDTLS_PRIVATE(priv_keys)[q_leaf_identifier],
1819+ f_rng, p_rng, msg, msg_len,
1820+ sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET );
1821+ if( ret )
1822+ {
1823+ return( ret );
1824+ }
1825+
1826+ val_to_network_bytes( ctx->MBEDTLS_PRIVATE(type), MBEDTLS_LMS_TYPE_LEN,
1827+ sig + MBEDTLS_LMS_SIG_TYPE_OFFSET );
1828+ val_to_network_bytes( q_leaf_identifier, MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
1829+ sig + MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET);
1830+
1831+ ret = get_merkle_path( ctx, MERKLE_TREE_INTR_AM + q_leaf_identifier,
1832+ ( unsigned char( * )[32] )( sig + MBEDTLS_LMS_SIG_PATH_OFFSET ) );
1833+ if( ret )
1834+ {
1835+ return( ret );
1836+ }
1837+
1838+ return( 0 );
1839+}
1840+
1841+int mbedtls_lms_verify( const mbedtls_lms_context *ctx,
1842+ const unsigned char *msg, unsigned int msg_len,
1843+ const unsigned char *sig )
1844+{
1845+ unsigned int q_leaf_identifier;
1846+ unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN];
1847+ unsigned char Tc_candidate_root_node[32];
1848+ unsigned int height;
1849+ unsigned int curr_node_id;
1850+ unsigned int parent_node_id;
1851+ const unsigned char* left_node;
1852+ const unsigned char* rght_node;
1853+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1854+
1855+ if( ctx == NULL )
1856+ {
1857+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
1858+ }
1859+
1860+ if( ! ctx->MBEDTLS_PRIVATE(have_pubkey) )
1861+ {
1862+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
1863+ }
1864+
1865+ if( msg == NULL)
1866+ {
1867+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
1868+ }
1869+
1870+ if( sig == NULL)
1871+ {
1872+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
1873+ }
1874+
1875+ if( ctx->MBEDTLS_PRIVATE(type) != MBEDTLS_LMS_SHA256_M32_H10 )
1876+ {
1877+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
1878+ }
1879+
1880+ if( ctx->MBEDTLS_PRIVATE(otstype) != MBEDTLS_LMOTS_SHA256_N32_W8 )
1881+ {
1882+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
1883+ }
1884+
1885+
1886+ if( network_bytes_to_val( MBEDTLS_LMS_TYPE_LEN,
1887+ sig + MBEDTLS_LMS_SIG_TYPE_OFFSET) != MBEDTLS_LMS_SHA256_M32_H10 )
1888+ {
1889+ return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
1890+ }
1891+
1892+ if( network_bytes_to_val( MBEDTLS_LMOTS_TYPE_LEN,
1893+ sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET)
1894+ != MBEDTLS_LMOTS_SHA256_N32_W8 )
1895+ {
1896+ return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
1897+ }
1898+
1899+
1900+ q_leaf_identifier = network_bytes_to_val( MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
1901+ sig + MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET );
1902+
1903+ if( q_leaf_identifier >= MERKLE_TREE_LEAF_AM )
1904+ {
1905+ return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
1906+ }
1907+
1908+ ret = mbedtls_lmots_generate_pub_key_candidate( ctx->MBEDTLS_PRIVATE(I_key_identifier),
1909+ sig + MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET,
1910+ msg, msg_len,
1911+ sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET,
1912+ Kc_candidate_ots_pub_key );
1913+ if( ret )
1914+ {
1915+ return( ret );
1916+ }
1917+
1918+ create_merkle_leaf_node( ctx, Kc_candidate_ots_pub_key,
1919+ MERKLE_TREE_INTR_AM + q_leaf_identifier,
1920+ Tc_candidate_root_node );
1921+
1922+ curr_node_id = MERKLE_TREE_INTR_AM + q_leaf_identifier;
1923+
1924+ for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT; height++ )
1925+ {
1926+ parent_node_id = curr_node_id / 2;
1927+
1928+ /* Left/right node ordering matters for the hash */
1929+ if( curr_node_id & 1 )
1930+ {
1931+ left_node = ( ( const unsigned char( * )[32] )( sig + MBEDTLS_LMS_SIG_PATH_OFFSET ) )[height];
1932+ rght_node = Tc_candidate_root_node;
1933+ }
1934+ else
1935+ {
1936+ left_node = Tc_candidate_root_node;
1937+ rght_node = ( ( const unsigned char( * )[32] )( sig + MBEDTLS_LMS_SIG_PATH_OFFSET ) )[height];
1938+ }
1939+
1940+ create_merkle_intr_node( ctx, left_node, rght_node, parent_node_id,
1941+ Tc_candidate_root_node);
1942+
1943+ curr_node_id /= 2;
1944+ }
1945+
1946+ if( memcmp( Tc_candidate_root_node, ctx->MBEDTLS_PRIVATE(T_1_pub_key),
1947+ MBEDTLS_LMOTS_N_HASH_LEN) )
1948+ {
1949+ return( MBEDTLS_ERR_LMS_VERIFY_FAILED );
1950+ }
1951+
1952+ return( 0 );
1953+}
1954+
1955+int mbedtls_lms_import_pubkey( mbedtls_lms_context *ctx,
1956+ const unsigned char *key )
1957+{
1958+ mbedtls_lms_algorithm_type_t type;
1959+ mbedtls_lmots_algorithm_type_t otstype;
1960+
1961+ if( ctx == NULL )
1962+ {
1963+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
1964+ }
1965+
1966+ if( key == NULL )
1967+ {
1968+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
1969+ }
1970+
1971+ type = network_bytes_to_val( MBEDTLS_LMS_TYPE_LEN, key + MBEDTLS_LMS_PUBKEY_TYPE_OFFSET );
1972+ if( type != MBEDTLS_LMS_SHA256_M32_H10 )
1973+ {
1974+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
1975+ }
1976+ ctx->MBEDTLS_PRIVATE(type) = type;
1977+
1978+ otstype = network_bytes_to_val( MBEDTLS_LMOTS_TYPE_LEN,
1979+ key + MBEDTLS_LMS_PUBKEY_OTSTYPE_OFFSET );
1980+ if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 )
1981+ {
1982+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
1983+ }
1984+ ctx->MBEDTLS_PRIVATE(otstype) = otstype;
1985+
1986+ memcpy( ctx->MBEDTLS_PRIVATE(I_key_identifier), key + MBEDTLS_LMS_PUBKEY_I_KEY_ID_OFFSET,
1987+ MBEDTLS_LMOTS_I_KEY_ID_LEN );
1988+ memcpy( ctx->MBEDTLS_PRIVATE(T_1_pub_key), key + MBEDTLS_LMS_PUBKEY_ROOT_NODE_OFFSET,
1989+ MBEDTLS_LMOTS_N_HASH_LEN );
1990+
1991+ ctx->MBEDTLS_PRIVATE(have_pubkey) = 1;
1992+
1993+ return( 0 );
1994+}
1995+
1996+int mbedtls_lms_export_pubkey( mbedtls_lms_context *ctx,
1997+ unsigned char *key )
1998+{
1999+ if( ctx == NULL )
2000+ {
2001+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
2002+ }
2003+
2004+ if( key == NULL )
2005+ {
2006+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
2007+ }
2008+
2009+ if( ! ctx->MBEDTLS_PRIVATE(have_pubkey) )
2010+ {
2011+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
2012+ }
2013+
2014+ val_to_network_bytes( ctx->MBEDTLS_PRIVATE(type),
2015+ MBEDTLS_LMS_TYPE_LEN, key + MBEDTLS_LMS_PUBKEY_TYPE_OFFSET );
2016+ val_to_network_bytes( ctx->MBEDTLS_PRIVATE(otstype),
2017+ MBEDTLS_LMOTS_TYPE_LEN, key + MBEDTLS_LMS_PUBKEY_OTSTYPE_OFFSET );
2018+ memcpy( key + MBEDTLS_LMS_PUBKEY_I_KEY_ID_OFFSET,
2019+ ctx->MBEDTLS_PRIVATE(I_key_identifier),
2020+ MBEDTLS_LMOTS_I_KEY_ID_LEN );
2021+ memcpy( key + MBEDTLS_LMS_PUBKEY_ROOT_NODE_OFFSET,
2022+ ctx->MBEDTLS_PRIVATE(T_1_pub_key),
2023+ MBEDTLS_LMOTS_N_HASH_LEN );
2024+
2025+ return( 0 );
2026+}
2027+
2028+int mbedtls_lms_gen_pubkey( mbedtls_lms_context *ctx )
2029+{
2030+ unsigned char tree[MERKLE_TREE_NODE_AM][32];
2031+ unsigned int idx;
2032+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2033+
2034+ if( ctx == NULL )
2035+ {
2036+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
2037+ }
2038+
2039+ if( ! ctx->MBEDTLS_PRIVATE( have_privkey ) )
2040+ {
2041+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
2042+ }
2043+
2044+ if( ctx->MBEDTLS_PRIVATE(type) != MBEDTLS_LMS_SHA256_M32_H10 )
2045+ {
2046+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
2047+ }
2048+
2049+ if( ctx->MBEDTLS_PRIVATE(otstype) != MBEDTLS_LMOTS_SHA256_N32_W8 )
2050+ {
2051+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
2052+ }
2053+
2054+ for( idx = 0; idx < MERKLE_TREE_LEAF_AM; idx++ )
2055+ {
2056+ ret = mbedtls_lmots_gen_pubkey( &ctx->MBEDTLS_PRIVATE(priv_keys)[idx] );
2057+ if( ret )
2058+ {
2059+ return( ret );
2060+ }
2061+ }
2062+
2063+ ret = generate_merkle_tree( ctx, tree);
2064+ if( ret )
2065+ {
2066+ return( ret );
2067+ }
2068+
2069+ /* Root node is always at position 1, due to 1-based indexing */
2070+ memcpy( ctx->MBEDTLS_PRIVATE(T_1_pub_key), &tree[1], MBEDTLS_LMOTS_N_HASH_LEN );
2071+
2072+ ctx->MBEDTLS_PRIVATE(have_pubkey) = 1;
2073+
2074+ return( 0 );
2075+}
2076+
2077+int mbedtls_lms_gen_privkey( mbedtls_lms_context *ctx,
2078+ int ( *f_rng)(void *, unsigned char *, size_t),
2079+ void* p_rng, unsigned char *seed,
2080+ size_t seed_len )
2081+{
2082+ unsigned int idx;
2083+ int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
2084+
2085+ if( ctx == NULL )
2086+ {
2087+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
2088+ }
2089+
2090+ if( ctx->MBEDTLS_PRIVATE(type) != MBEDTLS_LMS_SHA256_M32_H10 )
2091+ {
2092+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
2093+ }
2094+
2095+ if( ctx->MBEDTLS_PRIVATE(otstype) != MBEDTLS_LMOTS_SHA256_N32_W8 )
2096+ {
2097+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
2098+ }
2099+
2100+ if( ctx->MBEDTLS_PRIVATE(have_privkey) )
2101+ {
2102+ return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA );
2103+ }
2104+
2105+ f_rng( p_rng, ctx->MBEDTLS_PRIVATE(I_key_identifier),
2106+ sizeof( ctx->MBEDTLS_PRIVATE(I_key_identifier) ) );
2107+
2108+ ctx->MBEDTLS_PRIVATE(priv_keys) = mbedtls_calloc( MERKLE_TREE_LEAF_AM,
2109+ sizeof( mbedtls_lmots_context));
2110+ if( ctx->MBEDTLS_PRIVATE(priv_keys) == NULL )
2111+ {
2112+ ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
2113+ goto out;
2114+ }
2115+
2116+ for( idx = 0; idx < MERKLE_TREE_LEAF_AM; idx++ )
2117+ {
2118+ mbedtls_lmots_init( &ctx->MBEDTLS_PRIVATE(priv_keys)[idx] );
2119+ ret = mbedtls_lmots_set_algorithm_type( &ctx->MBEDTLS_PRIVATE(priv_keys)[idx],
2120+ ctx->MBEDTLS_PRIVATE(otstype) );
2121+ if( ret)
2122+ {
2123+ goto out;
2124+ }
2125+ }
2126+
2127+
2128+ for( idx = 0; idx < MERKLE_TREE_LEAF_AM; idx++ )
2129+ {
2130+ ret = mbedtls_lmots_gen_privkey( &ctx->MBEDTLS_PRIVATE(priv_keys)[idx],
2131+ ctx->MBEDTLS_PRIVATE(I_key_identifier),
2132+ idx, seed, seed_len );
2133+ if( ret)
2134+ {
2135+ goto out;
2136+ }
2137+ }
2138+
2139+ ctx->MBEDTLS_PRIVATE(q_next_usable_key) = 0;
2140+ ctx->MBEDTLS_PRIVATE(have_privkey) = 1;
2141+
2142+out:
2143+ if( ret )
2144+ {
2145+ mbedtls_free( ctx->MBEDTLS_PRIVATE(priv_keys) );
2146+ return( ret );
2147+ }
2148+
2149+ return( 0 );
2150+}
2151+
2152+#endif /* MBEDTLS_LMS_C */
2153diff --git a/scripts/generate_errors.pl b/scripts/generate_errors.pl
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002154index 0a03f02e9..d333f6590 100755
Raef Coles8a3bf392022-06-22 13:53:59 +01002155--- a/scripts/generate_errors.pl
2156+++ b/scripts/generate_errors.pl
2157@@ -47,7 +47,7 @@ my $error_format_file = $data_dir.'/error.fmt';
2158
2159 my @low_level_modules = qw( AES ARIA ASN1 BASE64 BIGNUM
2160 CAMELLIA CCM CHACHA20 CHACHAPOLY CMAC CTR_DRBG DES
2161- ENTROPY ERROR GCM HKDF HMAC_DRBG MD5
2162+ ENTROPY ERROR GCM HKDF HMAC_DRBG LMS LMOTS MD5
2163 NET OID PADLOCK PBKDF2 PLATFORM POLY1305 RIPEMD160
2164 SHA1 SHA256 SHA512 THREADING );
2165 my @high_level_modules = qw( CIPHER DHM ECP MD
2166diff --git a/tests/suites/test_suite_lmots.data b/tests/suites/test_suite_lmots.data
2167new file mode 100644
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002168index 000000000..ed192bf7d
Raef Coles8a3bf392022-06-22 13:53:59 +01002169--- /dev/null
2170+++ b/tests/suites/test_suite_lmots.data
2171@@ -0,0 +1,29 @@
2172+LMOTS sign-verify test #1
2173+lmots_sign_verify_test:"c41ba177a0ca1ec31dfb2e145237e65b"
2174+
2175+LMOTS sign-verify test #2
2176+lmots_sign_verify_test:"55a6647a581004306792b653a561d9f3"
2177+
2178+LMOTS hash-sigs interop test #1
2179+lmots_verify_test:"C80B24A82D52963AB241C84DCF0EEE55BB24F9F0":"00000004DB3B6B24FD191CE8894AE3C4E2CE2DE0C3F5D508691A9162F37704E07838488466CCD746E55BC8E3055C7A4D4DA2E10775DAFA2E8E5FEFB1CEB25973BC3323B61DE5E2EA7DCFF15DA482320F958023CA2E718C69B977019E8F2FCD47151388E2E5E11170AFE93BDEB508362B3A317835A9DDEB18F0CDCCC0731902DB3D37F441C93A490DE53962A915AB5060A1C157D61DDF061F272362AB7FD9EE95AE48D3448F204C81A3F260792784E1BFB49871A27C09CC549A406520F0B40BC74CAAD082EAAB12C994B8495B8C80E96384FF2222255BE6C4EE5AF439534E616F9C0B53E951F69D59BD0506C0C0366A679A8329ACF6E2D1D4E4EF49D35375A8EA46FCF3C9B2F8E033C242EC61B796E43B901407077A2AF3F0AABD2D0CB9004F10D91B57C2D5E8BA7BA9268FF94962CC102F55B5120D7D2F7A3BE281BA01D78895ADA2F05B77967EDA0E1EDEAFBB9BBC3D68DCBF682FBC70467FA2BEE5DE65F54247C4BE5BEF41F5108B6CD09E7698E3AAEA04B60746EDD0E2623B66B092DDA21EFA5A0D36805D101D805CC06F0E818B46059B3984C8B8A526C4239F66ED34B8CA57E178DC5E7B8D2BE029114B4CE466E2B5A081729B3F3A3F3845DDE177062F201C09125ED3CC381AF35EAD795440C421A136F941F09F3E4BA9E0203CBA875AF477AB0246342700F323E65DC327D27966377D871FD9C58BEC8797DEF35E1D0751A554719828B87332F601884EBFECB63A8D4F390785B3826BFA384BE502D2322C919ABD12A486C2AB124DCB7B74DE91241A30EF0388411E52145A88971C6C0A4E7C4F23EDD8D6008065A3D108C6B1EC5219BB0DFDBD37EE3A7A8DD37E3563A5777838FCA61E9E744813F39CF70B5A0F50E1BD4FF8733A3BDA76D2135969809D91A9786F22DC2ACBA4E0164C411019EC77A0BD253A42AC7528FC7C0DA1711FBD6C23825207060463080F9E04B7D819C8B150C22B8BA87C6277696EC409369C48AC0E3233DE52D31EF6D2207D2B57DFC2D0C43BE8212EAE6CB1BACBC2D3568E5527A14065D5F1F56AAE2AFB5FB1CEFFC228A30692BD030C71F4872DB54F2632CD919DA61576CF58D1EBE3D7988183A9C789EB74A3D7F6BBEBAC035A43397BF684C9E1130B252940DBA4454311A6A3D54D9386D48E1D5A3E70944EDF725AEDC5440CD610F79AB05A43C917FFC15213295EB8CB8432B6554A47C2AD419ADD52E0F5E0BD7A1E0F873257E69F8647F3A07093387B7A64C4812CDBEE536E45D531F89653AC5F14A4715CFF40692346FE6CBF2F9B92D9F1101C379AFD5E6154605059C1DA463B407E79C139396623DC7F15EEFCE424C8E214C6A645EF002F90A230D8F62177CBCF2A688D4F822B119835AD3D3A619F46230257A5AD59CB0924B2584DBA96AADE0A2487E7409EE5993A4F0E3DC46C10B96595CDD17D72C35EF4A52C5906655B0AE649B5DE03B7D46F3839E808761EE05E9300050647593C048669A952324B0188ED225AD11BED3FD94E44E134FB9D6DAD53CC34ECF62695E30637C4528C450D62174E2F8ABA2C09F134412EF889C24B36224BE4259B363A9D8EB89BAEE16BE1898D46":"000000048440BBD3F47A167DD2D0E446DBCEA774000000138686E25BC07C69B43A2D3B7165DFF85C134177C876EA47D96FEF069BC96A981A":0
2180+
2181+LMOTS hash-sigs interop test #2
2182+lmots_verify_test:"85A77D026E8C704B14EF665DEB0A648350FA8859":"00000004BEEF900CA9A14982E011A5A94503D566288932AD4B9CB85D1551C766A56F788B0D223FBA0E1AD722BE48365B4CE5DA21051B3A79EAD8C1046944192EE51E162DE1E9AE9B13DD8FA90867A17666CB8967F7CD7F4F5383703CCFF555EEBDC8123AF3C4E39D0A5D61725B6A7F23CE587D256C08D31510BCD895106CD524B432A9211CDCBDF5ADDAD5F1D76D428A76DF2556FFA8AB546CFAAA7F2FE86164B976FDA2940498DB0D1A2DE29972FFD55D83E7CA3318DBAC31670565A1E5F59E36342F887E0EF7855AD93CA5F1951307D79EEA375168BC35A3A0B053AEBDB4D7157AD929B0D7DE9FC1E8C32C9A2D679843CBC77560EEDA5959D0AC8407648344C6952649A303E7B6FCC2EE979E1B2786B898A01E2918894DB4E37A0ED79A30260A45959B4BB3016F081181190CB740376389827B2D56DF7EC00871DC9A198B74C7C6086C940A845D54198F2D5DD7A47F97A192F33A85AAA1304A3251B82AC33C5E7B3BA20D2A9BD49BBEE0B2DA2338E578E6F139BB7596DC3BD89E86CB393C42765B9FE85457116906C3F9A8499CF5E539A5CCB3F6D1F36CA209DE6942F807E579AF0EBF072EA110A812C9E420647CE7C8B2BDBB5F56C5B3B7EA80A53C3574F4ED32E4708DFEED60280ABBE2021B3791B0CB09C1F0731353234A6A327CDDFD4E3E2D9DD5A16FCDE3EEF09C67065BD702C07B53A005D3FE7D23FFD77D40E49C82165EB104343A166E808A3CAEDE1A43AE3A82E1788B49C565CF88A2AB8E2FD37657D53E3679D7A818D864F55144011AB498A4A985C46342F3562FD80ECB86497C3DBB759006E5FFFDC01CAA15C69B716174EDCB6E9870CF391003D3826451D1BEFDCC84C093428EE01DAF883190F5D2542B36A7DE44A453AECD5E93B768ACEE75076BE3D73A66F17CFD8E4A49B1F61CE9446815A86FF5FB0EA070A751893C85360C038A161D3DD4D2C66F440E7265153AB346EF620156605C028DD9636FAE0C9A20DF09303ECC5E57A6424505530F70D25F1C95FE51CBD82C2AD0015EB9AD5379CEC463FA0331A14DD971B7C2311FC45979C531653E7252884BAB7C49F8CD652BDF6FDFA76984445C63B54ED22B4A8A267D091381BE7B9B7608133968BA46106BF42B9091F78C085E674D1F70FB91C68D07733F6412B1583DD2F37C6ECAD6BCCE1A1C7D0A7CA80677F679A5AFE08D15427E5C78CE6EB9AA90F51F40343DC9FD1316DCEB2C1EF8EA217B714B0DE1AEECE04D19D0D7757481EDA6E8C51BE85B7B24720E8D62B8AEC56C1A1B9D278B874AACC0B492CF44ED4E7B1200C82323C1AFA0FC776E92B227E8979E3A92EAB05FCF18A43AE648397088F4991F73ECE22C03B3F42F51C0C0FE0DF37919D048FB473F7AB0E33310B9782DE56384BD888CE5E2A644E20A52DD47F710DB0D3169991E29E716ABFD84CA4850080B6C252CB96CD8979189819E532DF56ECB172F773919733BF4D442901EBFB656EBFED4C6D83FAFF288279779499091C94432ECDF83188048AB596D65BC48FA708D485F9CDC50C8B470DFE22157E8F5EE366722A04E8CE7B861573E5FC97D34055BB50B562738F803B202F7F8":"00000004DE9CE10EA7125AC6399B6B3C7EE24224000000161D61E675F3EA19C5B95DA4EE2E35BA061B39E7639F3989F8AE4B0696B3F87E4E":0
2183+
2184+LMOTS hash-sigs interop test #3
2185+lmots_verify_test:"C32F83EFBFD87F386A6C0850CBB93A75F2506A35":"00000004BD2D174BEBEEF1CAF06E4BF1088DE2AAB17C0528579BD4E1C4A1ED583C36BDACA49798373961B605EAEFAEFC0B4BC39C7AD30572CD29BEBE9AEE503CA2D8BF8C31C8A1B71CF67A18EE01A8A878699F22A1AEE32731E51E3EAD3775EFD8339E263C5A4544559506BA5502B9AEF59217ABC24923EC5E28D18BA18B0D964DB277007A76B8075B39F48CDA92148C9BAE1C7E5421CA753FA2D6BEAE8F49977E4E5B6F38D35BA28A526A53061E57BB92DA0EBBD4AE01AE9FADBED74F503DC39FA2E10C20B47DFB3DFBE25EC35618D2307D21716B10F8FB5095B42C289D1847E5D6F9988C6763D288667D3B658A4F3613E084DAE8B78E0B295A6ED28E88C676995AA5EB1533CDF8EB6F95A5E5117F06B1759495A9CB6E40FBF1F97FF73FDCBFD315C48DA631AB0425CA0633817C46F25E84AEEA37DD77310EE56815E83F862EF14E15FC1246243AA02F40EA32567237D5ADC2944BD63CF55FA4F0DE251B3F5C067D9EC396D5E20F9CEF2C555D89DA721D91D6D607653B97636AB10B74F39FA984D23A3D276EFF5F49C336274A66AC491EDE34686C6CFC17F5312FD3E3E5749A2E472011FA391A5ACF09D918B01704B447FD5E3EA6BB726A3475775DFE6A98CE5473CDEDB630EA4D604BAF36A8B8A8E567F05929E8A74970AA742FBC945021017E464E753D5AC497925AA4AECA0CBF562B2E39F891E177FD8E4E61A698B099D21F13EFD0DE5357A1970314D8E3AA1D2A84D3BCF75A7876C16F585322CC4C613FE3AC8FEA5F258FC9C7200765E9209378C362AFC1A478A117D913CE2BEFEB51103E48D0802618C50918005F1AA4228B67BA1A1B001A91A032019A135B8AEEE3D0158A602C8DCCE5A6580DECC16204E410CBB15FCF36704BB2ECB826A229E45C454B4A5DFC12796E636B300C624DB4E6EAB424B17A18A5A5F52399F247A1507A5985D06F90889FE381129148AF8447B392D4EC0775D91502B48D9F212FCE3F81639901C462F752E27FBEEC9E2B7F8CCD16053FB839E8ADF8CD3E8FF8AF3B3E884F4F524C2026BD3B337B7058B53CFC7596F9C813FFD746B8AC0012C60E96140934B4EED1D8602E57A1A6EBC01FCFD66053AF9614FAF0D0F7320D50D440F2A3148A0DAEF5E2FA31F854D56045065AFAA52A60DC3321E2D7C104FF505057D55CD94C53C31C14DB0DAA4D55C4065CD9BCD78E1B8532A680F7DC3544021346CC59ADEC061DDA1B7606BAF28AD87C39AB8AF3D03E981EFFE50B4D5347175517EF212E61F02B594A96492091AC82625D334504EF19BEEE52E01B111D43313F35EC69C88EF38926071506AB3A5B372DD6F2B901AC1E12E61CCB3ACD3D0777A7A10F137126DAD0D1970D369A067C3A1F19D9CB8756D7130B7EB0C08CF725EB2ADFAD61204195CE14F3C99A88A9B8FA2FDCBD612DF9266614DEA073C9EDABE07B3793048167D4DA49B305AE27974D48A296871350DE036CAA348D2F9A9CB19DC094E5904E25DDCF5657227DCD2A4E620121FBDA032A58836EDC14F3A7C4E51319A60F91F941CC61757498B769799394574C9D198426AC3499F0D0BA1770AD6BAA0D3716333F785A9D7D":"00000004DA66203A7E7BCA2362DB3C8E897A84B10000000D1BD4EE08FAA341C2CE018BD12776E1B8E6B8B2C1EEDAE6BD0998E52F089936FE":0
2186+
2187+LMOTS hash-sigs interop negative test (altered random value)
2188+lmots_verify_test:"C80B24A82D52963AB241C84DCF0EEE55BB24F9F0":"00000004CB3B6B24FD191CE8894AE3C4E2CE2DE0C3F5D508691A9162F37704E07838488466CCD746E55BC8E3055C7A4D4DA2E10775DAFA2E8E5FEFB1CEB25973BC3323B61DE5E2EA7DCFF15DA482320F958023CA2E718C69B977019E8F2FCD47151388E2E5E11170AFE93BDEB508362B3A317835A9DDEB18F0CDCCC0731902DB3D37F441C93A490DE53962A915AB5060A1C157D61DDF061F272362AB7FD9EE95AE48D3448F204C81A3F260792784E1BFB49871A27C09CC549A406520F0B40BC74CAAD082EAAB12C994B8495B8C80E96384FF2222255BE6C4EE5AF439534E616F9C0B53E951F69D59BD0506C0C0366A679A8329ACF6E2D1D4E4EF49D35375A8EA46FCF3C9B2F8E033C242EC61B796E43B901407077A2AF3F0AABD2D0CB9004F10D91B57C2D5E8BA7BA9268FF94962CC102F55B5120D7D2F7A3BE281BA01D78895ADA2F05B77967EDA0E1EDEAFBB9BBC3D68DCBF682FBC70467FA2BEE5DE65F54247C4BE5BEF41F5108B6CD09E7698E3AAEA04B60746EDD0E2623B66B092DDA21EFA5A0D36805D101D805CC06F0E818B46059B3984C8B8A526C4239F66ED34B8CA57E178DC5E7B8D2BE029114B4CE466E2B5A081729B3F3A3F3845DDE177062F201C09125ED3CC381AF35EAD795440C421A136F941F09F3E4BA9E0203CBA875AF477AB0246342700F323E65DC327D27966377D871FD9C58BEC8797DEF35E1D0751A554719828B87332F601884EBFECB63A8D4F390785B3826BFA384BE502D2322C919ABD12A486C2AB124DCB7B74DE91241A30EF0388411E52145A88971C6C0A4E7C4F23EDD8D6008065A3D108C6B1EC5219BB0DFDBD37EE3A7A8DD37E3563A5777838FCA61E9E744813F39CF70B5A0F50E1BD4FF8733A3BDA76D2135969809D91A9786F22DC2ACBA4E0164C411019EC77A0BD253A42AC7528FC7C0DA1711FBD6C23825207060463080F9E04B7D819C8B150C22B8BA87C6277696EC409369C48AC0E3233DE52D31EF6D2207D2B57DFC2D0C43BE8212EAE6CB1BACBC2D3568E5527A14065D5F1F56AAE2AFB5FB1CEFFC228A30692BD030C71F4872DB54F2632CD919DA61576CF58D1EBE3D7988183A9C789EB74A3D7F6BBEBAC035A43397BF684C9E1130B252940DBA4454311A6A3D54D9386D48E1D5A3E70944EDF725AEDC5440CD610F79AB05A43C917FFC15213295EB8CB8432B6554A47C2AD419ADD52E0F5E0BD7A1E0F873257E69F8647F3A07093387B7A64C4812CDBEE536E45D531F89653AC5F14A4715CFF40692346FE6CBF2F9B92D9F1101C379AFD5E6154605059C1DA463B407E79C139396623DC7F15EEFCE424C8E214C6A645EF002F90A230D8F62177CBCF2A688D4F822B119835AD3D3A619F46230257A5AD59CB0924B2584DBA96AADE0A2487E7409EE5993A4F0E3DC46C10B96595CDD17D72C35EF4A52C5906655B0AE649B5DE03B7D46F3839E808761EE05E9300050647593C048669A952324B0188ED225AD11BED3FD94E44E134FB9D6DAD53CC34ECF62695E30637C4528C450D62174E2F8ABA2C09F134412EF889C24B36224BE4259B363A9D8EB89BAEE16BE1898D46":"000000048440BBD3F47A167DD2D0E446DBCEA774000000138686E25BC07C69B43A2D3B7165DFF85C134177C876EA47D96FEF069BC96A981A":MBEDTLS_ERR_LMOTS_VERIFY_FAILED
2189+
2190+LMOTS negative test (invalid type) #1
2191+lmots_verify_test:"0000000000000000000000000000000000000000":"0000000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":"0000000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA
2192+
2193+LMOTS negative test (invalid type) #2
2194+lmots_verify_test:"0000000000000000000000000000000000000000":"0000000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":"0000000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA
2195+
2196+LMOTS key import / export test
2197+lmots_import_export_test:"000000048440BBD3F47A167DD2D0E446DBCEA774000000138686E25BC07C69B43A2D3B7165DFF85C134177C876EA47D96FEF069BC96A981A"
2198+
2199+LMOTS key reuse test
2200+lmots_reuse_test:"cfcd1e81193e310c9d931d1b00818d14"
2201diff --git a/tests/suites/test_suite_lmots.function b/tests/suites/test_suite_lmots.function
2202new file mode 100644
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002203index 000000000..6de94d124
Raef Coles8a3bf392022-06-22 13:53:59 +01002204--- /dev/null
2205+++ b/tests/suites/test_suite_lmots.function
2206@@ -0,0 +1,108 @@
2207+/* BEGIN_HEADER */
2208+#include "mbedtls/lmots.h"
2209+#include "mbedtls/entropy.h"
2210+#include "mbedtls/ctr_drbg.h"
2211+
2212+/* END_HEADER */
2213+
2214+/* BEGIN_DEPENDENCIES
2215+ * depends_on:MBEDTLS_LMOTS_C:MBEDTLS_SHA256_C:MBEDTLS_CTR_DRBG_C
2216+ * END_DEPENDENCIES
2217+ */
2218+
2219+/* BEGIN_CASE */
2220+void lmots_sign_verify_test ( data_t * msg )
2221+{
2222+ mbedtls_lmots_context ctx;
2223+ unsigned char sig[MBEDTLS_LMOTS_SIG_LEN];
2224+ mbedtls_entropy_context entropy_ctx;
2225+ mbedtls_ctr_drbg_context drbg_ctx;
2226+ uint8_t seed[16];
2227+
2228+ mbedtls_entropy_init( &entropy_ctx );
2229+ mbedtls_ctr_drbg_init( &drbg_ctx );
2230+ mbedtls_lmots_init( &ctx );
2231+
2232+ TEST_ASSERT( mbedtls_ctr_drbg_seed( &drbg_ctx, mbedtls_entropy_func,
2233+ &entropy_ctx, (uint8_t*)"", 0 ) == 0 );
2234+ TEST_ASSERT( mbedtls_ctr_drbg_random( &drbg_ctx, seed, sizeof( seed ) ) == 0 );
2235+
2236+ TEST_ASSERT( mbedtls_lmots_set_algorithm_type(&ctx, MBEDTLS_LMOTS_SHA256_N32_W8) == 0 );
2237+ TEST_ASSERT( mbedtls_lmots_gen_privkey(&ctx, (uint8_t[16]){0}, 0x12, seed, sizeof( seed ) ) == 0 );
2238+ TEST_ASSERT( mbedtls_lmots_gen_pubkey(&ctx) == 0 );
2239+ TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_ctr_drbg_random, &drbg_ctx, msg->x, msg->len, sig ) == 0 );
2240+ TEST_ASSERT( mbedtls_lmots_verify(&ctx, msg->x, msg->len, sig) == 0 );
2241+
2242+exit:
2243+ mbedtls_entropy_free( &entropy_ctx );
2244+ mbedtls_ctr_drbg_free( &drbg_ctx );
2245+ mbedtls_lmots_free( &ctx );
2246+}
2247+/* END_CASE */
2248+
2249+/* BEGIN_CASE */
2250+void lmots_verify_test ( data_t * msg, data_t * sig, data_t * pub_key,
2251+ int expected_rc )
2252+{
2253+ mbedtls_lmots_context ctx;
2254+
2255+ mbedtls_lmots_init( &ctx );
2256+
2257+ mbedtls_lmots_import_pubkey( &ctx, pub_key->x );
2258+
2259+ TEST_ASSERT(mbedtls_lmots_verify( &ctx, msg->x, msg->len, sig->x ) == expected_rc );
2260+
2261+exit:
2262+ mbedtls_lmots_free( &ctx );
2263+}
2264+/* END_CASE */
2265+
2266+/* BEGIN_CASE */
2267+void lmots_import_export_test ( data_t * pub_key )
2268+{
2269+ mbedtls_lmots_context ctx;
2270+ uint8_t exported_pub_key[MBEDTLS_LMOTS_PUBKEY_LEN];
2271+
2272+ mbedtls_lmots_init( &ctx );
2273+ TEST_ASSERT( mbedtls_lmots_import_pubkey( &ctx, pub_key->x ) == 0 );
2274+ TEST_ASSERT( mbedtls_lmots_export_pubkey( &ctx, exported_pub_key ) == 0 );
2275+
2276+ TEST_ASSERT( memcmp( pub_key->x, exported_pub_key, MBEDTLS_LMOTS_PUBKEY_LEN ) == 0 );
2277+
2278+exit:
2279+ mbedtls_lmots_free( &ctx );
2280+}
2281+/* END_CASE */
2282+
2283+/* BEGIN_CASE */
2284+void lmots_reuse_test ( data_t * msg )
2285+{
2286+ mbedtls_lmots_context ctx;
2287+ unsigned char sig[MBEDTLS_LMOTS_SIG_LEN];
2288+ mbedtls_entropy_context entropy_ctx;
2289+ mbedtls_ctr_drbg_context drbg_ctx;
2290+ uint8_t seed[16];
2291+
2292+ mbedtls_entropy_init( &entropy_ctx );
2293+ mbedtls_ctr_drbg_init( &drbg_ctx );
2294+ TEST_ASSERT( mbedtls_ctr_drbg_seed(&drbg_ctx, mbedtls_entropy_func,
2295+ &entropy_ctx, (uint8_t*)"", 0 ) == 0 );
2296+
2297+ mbedtls_ctr_drbg_random( &drbg_ctx, seed, sizeof( seed ) );
2298+
2299+ mbedtls_lmots_init( &ctx );
2300+ TEST_ASSERT( mbedtls_lmots_set_algorithm_type( &ctx, MBEDTLS_LMOTS_SHA256_N32_W8 ) == 0 );
2301+ TEST_ASSERT( mbedtls_lmots_gen_privkey(&ctx, (uint8_t[16]){0}, 0x12, seed, sizeof( seed ) ) == 0 );
2302+ TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_ctr_drbg_random, &drbg_ctx, msg->x, msg->len, sig ) == 0 );
2303+
2304+ /* Running another sign operation should fail, since the key should now have
2305+ * been erased.
2306+ */
2307+ TEST_ASSERT( mbedtls_lmots_sign(&ctx, mbedtls_ctr_drbg_random, &drbg_ctx, msg->x, msg->len, sig ) != 0 );
2308+
2309+exit:
2310+ mbedtls_entropy_free( &entropy_ctx );
2311+ mbedtls_ctr_drbg_free( &drbg_ctx );
2312+ mbedtls_lmots_free( &ctx );
2313+}
2314+/* END_CASE */
2315diff --git a/tests/suites/test_suite_lms.data b/tests/suites/test_suite_lms.data
2316new file mode 100644
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002317index 000000000..b17fddc15
Raef Coles8a3bf392022-06-22 13:53:59 +01002318--- /dev/null
2319+++ b/tests/suites/test_suite_lms.data
2320@@ -0,0 +1,32 @@
2321+LMS sign-verify test
2322+lms_sign_verify_test:"c41ba177a0ca1ec31dfb2e145237e65b"
2323+
2324+LMS hash-sigs interop test #1
2325+lms_verify_test:"D5557C719EBB0DBECF563E5CDB16568BB11CD779":"0000000000000004C167A9AC495BD4EA34CD8EE5AAA2A656D518C33612FD87171421BFC3977CFA99765C6D496499C72A1DE21360DA57EB96BC83DB8AA92E560054C7805B04E336162FB4C411B509F76959F2458B0E53CF830E0145CCD439D494259EA4818CA68924A7E8B9DD36D6A9C7849B72F9338ED6C80A3E70B717E8E65B991B2FF9D8B49820E8ABC9E2ECC17DB38E855DA75D84DF9885C7F9DFB4ABC209CFF1D37D66595371D688A203CB89168945200C39169F784B19665CE1FB47D58BFA734C3E0E7E31D1206A033C6D8E25B7E45CA779A5FDE00C6B1CAC44884F2B52A380E1F6D8753549F7F4948A95AEA83703CF3AA108FA4F735AFC0DA1A03C378033D8B5959E7BE05D3C5070E709181AC09EFEC04128ABD7E8F37304FAD4B66373D4A83CFC1EF632DF6DB95577C2C6101CBDC807109ED8AE831FFB73DBC80942C58F334663B980F982C74B943BF7C57147250AADE595310387E3BB1A2705E9EC73DE7FABDA5EC0B1141A18798215B9A70F8D688357C833ED869059A2AA3360155EF84426288198D0FBB78223816B17093684C48942ED18FCD351C34E108E5B71D1CE39E318B5D991B650C46A91112E013E1180F2054C7A22429CAB31512BA34EA3AD9B68C5001EB70C993297CCF11914ECAF059922DAEE7D90ACE2567495ADA066E7DA1679CA45DAC1990B17184E7BE2E6A0F26AD77F19855D074F5B37372277484CE30B80A0540173C1B310C3E7B683A487B5D0676218EA1F65FEA444C493FC535E948EAB62252DCC90516BB45B60D4253DB6979FE342DC5CA1B86B01B2D8EBA79B0BC7B6984535616B792BB45F3C0E20B506E0694E1D5BA28FE96D34FE2BE354777D090404DD3508E9F7918FF5593ADB468478CA8A1F6AF752CC76F401E373B71471D9D70F455C8A73E4E7B6714394B1DD0E2A816AF3D5149835DAE477A70DEE0BDAC22F99A04BFB7C2D4AD53079C326F620DFD3F7CED4AB7F2E291507AA046331050F9E2205C52B36CBAEE817C5C3B1FBCDE61C54C8CB7B67E0570FA44728EC8FD091D5CEDC19C6B99840F7A0E49086F707E959D34B30E255B67BBAA24FADE532BF3D21825626E114BD8213170B0C2F01733D4ED420D01EE3ACD5F84DACE674AE7127DB0A80ACE252CAD9ADADDAFAB27281AFD6DDD72DB5AF878326C45D7DB1EFF8BC40895A3473A52461D076881310AD9937307217B5C0448B509EF9BA075936CC09E11B8838D3A6BC5EF9FAEA85A3EC87EEFDF2E38CD9732730085375A4FFC4E0A213B0E1FC3DE2D37F1EDACC3030F617F3459A03BFCF776A05FD3B7FD135782F6D6E7C5E92B56A1316525B26D3AE1CEA3C0C7CF3AA7B1E72B7599A31B50837D79A7AB61B9A9E2B7AABD2D605C97E302EB4B66C0588C24147955EA0892A54D42843568FE0863E7EFEAE336D302E672EA62689B4DAA02DD5BC99D93886EC7F411C53CE1CAEAB59FBC0B06E0E294F1900F8C626C6FF520AE2323DA797CDC120DBC19F7FEBA0E13429508C5B838A0F8B9B28A069C5DD40E2F6CC2C95FC6ACE7E1351516817BD2DC1AE08D498AD2B0BD1D8374942FF31FC6A4689C592244C919C3561E73DD4986FA500000006BBF34F6EE152B64FCDD1CB6848D2DA761798707060431761006E2EBD9312851F4F3DF3C46E10F643DDD58CB3D9F4D371F655EE26271F2DDE84A14CAA6A077DD96AF83849DE6CA8F2F3248902CBF49630C18C3EE3123D951CE9162D0E742B899AF9E5DA8D28A41C7CADF0194CDB09418BF48BF322F8C5E9563524196FA8AB785B43C4EA41A36148028D2F4C7356CDEEB09532CD7F2C80FC36589FF7A9954100C8697AEB014997C3088C242B4F70D26CE7F7E77384A9CF536EC5C5329E08BD6C1D65EFEFC1389A42D16FFB43A0E1D7661220E92A4A59703FB28410E73A677E803D4441929DFD7269E6F77AE8CA8C70B67B250A8728291EA5D4E3F03D505639408C88156DCBECC137142FA3585C09D99B84D8C380A5D29CE2ADA10A25F7CF939FE23288551F37FE2B7233BF97C0F5726B972E087BCBA095957CCD794794A4F50027":"00000006000000046B0927585C8547228D495361D73B970C287A2254BF8F1B170E55ACC9520A56CE5D2C711B6617718B49247D28CCC6D11D":0
2326+
2327+LMS hash-sigs interop test #2
2328+lms_verify_test:"DA16BE0B8FB691248486ACAFD531BE6EE4C362E0":"000000000000000484FCD0D791D175A0F86D64B2E8949F793CA9FFDC0347DE125DDB2F895BB9D2B43740B9B326B24F934D67586812BE6F3FB57E76FB12FBAD60A685F22A82C95C84AFAE63F47BB3CD951D483F61F5626B2B5FB6FDCA0CF02293EFDE1EB0AF6712D635678E825099E95435B43EF83C49C6589054D0905D82D597FB11A721D2232AD168FD179724539699C21163D5ADEB52290CB711C368572FF8BB95AA61DF2AD128307E768E73D3CF2BAFEAC8B6CD165BDD0316D2663D1ED61A15FB974082FC66A55E13ABA4FD084970EF6A59B0DFA1E934BF0E056C86E9B4C5B94CF863AB9F23BE2DB6A140A9CAA8DB31C83B21BDBDCD07304A52EB8501869D86BFDB68A376D94F847EED7E4CAB6A6EEC063945AE4DAF657D5509249E7FE430F5A13B188C4DD311F01746CE28F4F6540041EF6ADB299F712F591C83B0C12C1FB3E4A878C63217E25E08C004571FFC69E9C684E46F4D36C36409EBF3EB15F32A187176F4D177E0FE0E71ADFD4DA4AD2D57A0256B29AD5DAA6867AED20CC862AF5729D030514D41BB8D74551D8E925322C81A357A586227BBC885AB683BF835E9333A056AFEE96F2AD0FF6D6E235A9E2BC342ACBCF0B8EACC95E7B74215F6C65F12829433301F004EE1D9CFD16A4A1D092F48E99AEAE9E1FA4619ECE5E05F5C19F33C4C94A774EB8955409E3CFA73D8807CAA7C55FE45E980C7E7626AAA193F18A3AA7E525FFA6466D728FA25563DD383D855A0B6F8C011AC8C79C853CBED3A016DC26EF6E90B3E78119E465B9962A42B6AC168C1CDC9DB860D740B0C797303E2A62445FA618B5EB417BD4385C15BC548FEDF4D4842CA43F95188FFF63EB5D4AC85DAE618FDFB6CF5969EA0A3A52F73A4AC4957BC4EBCFEF593923EC79196021B25ED8D7558E4AF41ED74941585AC575CF1971D4F4C7C9E9516276734FF9FAFC7DE661F3090F71C98962789B31EA0FE406E2EF02F6F16B1708258C7559B8E05E27D647AD472805C865299FE30A5FE451DA7F2C493A37AA1655D492EC891B9AF559E12104CDD2EEB2E54138A1FB5A403AA32CEEB3946471A299604FA2DD3CA3E9567D01A3CEE5D09A1C2768B521C0C6142AF297CA5BFB3878B32D37D415542C15F655CB051240F3BA8FCE0E38449A0D7010A9B56BA2283E3A2047215813ED2090F7BDF16A40ADE32AB4E669684E6DEB6A94633E6643F29D10914F5A361C964CA9145514D4B80B45F3276EB0C649622034E71925FA038EB35E64C71CBDB11E91D779339516A351BD2A722CB60C2CBF145689B2E3F6FAEB74C3B58283929F70023503A96FED6A5D7D8A9E495FE1D85E0FCEC555F86747347D2FB5219FF65EFD144A5E1E88C63BE4259C42F6899C103536D75E0526508649E2836CACB94E88BD954B88EAC26F17B27BF62546C5C7573E2BC9EF4B65B8AE4951AF532F968FF050E504CC236DC48379E4390079DE451DCE710F9674D753C85B9FF7E7B09ED051EDD14C33AAFC8A188AE06234DFB61FE5A75C7A760B5286E1D6993BCEA0AB8A2C1D632145BD6A9F109ABB04E0B102D50DCB8C607AD6BA8C5FA5B21663E5A40194CA5DC2294BE10044E8D96AA0000000694ABC63BC5B27730C5223943C8341461474033BE3A221AFFDE66242AF14510CC656480CBDFC0B35205C89258A18BF6C29C4708CB2572DE15EE5DD481BC47060254954B5C5DD881AE6B358F7CDAB6F117235AAAC625B2750DB72BA4A96D7DFAA889BE780416E1CB264A413C6713710102D1D433BC6D0A47BF08AA74FD613D292A867261181BBD73557EE3AEB0F63579B71E58E97BAC1AACA3F34646350A13BB7ACE0AB3B062C41518768ABF3D32FB2F6A5E5C7F6B8B04C943D25A82F03F977755D74FD717A4B7E7674B03B577405210E23A2FE050E036DB0730359366A9436AD2CBCCE3E649F9E40023B2C12D9F5AA824319EAF571FD4842E573BB100BE9715D7B71F75521640D9B69B889349A283D62350D3A37264C89930F40603A5458B124EA850BA59024A46A8F325C9A9776817D739692FFAEA2758249888BF79D66FD496":"00000006000000043FC8322D04908C7C06C0D8B7A0CE24FA3AC253393CF9A56CF760294A06E75223E38C9E5329DDC493D8B51B1A4BBE41F8":0
2329+
2330+LMS hash-sigs interop test #3
2331+lms_verify_test:"331D543362A163390508127AFB9894B8C18C6E0A":"0000000000000004F5378439E9C229550D40B725FD56BE48DB785093E62BD29B723C61FABEDCFD486EFA120445339DE2A21A8C7465073ACBFD6DE3E50F395AAC20E0BCB23B088C416199F80B540AA81B2C0B12B7785152263522E8F79AEBE3B28315CC834AEB68475CADBC724DB6B7B7F594A7F9DA2505F5F44DAA7EEF70B72665A250C1F61A19F3FA4CBF389BEB9B31DC327882D7983EEED46DA8E00AEBEF85AE179EBF6D8CC7F720E9F963C4D30DD4015DAA27993D0780AFD7A45688422B1444AD866FEFE12EBD94B4D313517708A6E652D6206A8B263E234685D8133C2258EF6CA9E9C6FFD6D153598B13B59576897DC4F77C71609427866A347AE62B5C3BACB0A2E44B60F2CCB4989B0C57F3E785CDCF22B1FC8C3460A163FF2BE7A578E82429BA823F392A13C11A5639A42453972D2185E81809EF0666F8F01F575FBD9A46135F45651AA814D9BA84F774A9E9303FD55038CA41A21484BA9C38E69BCE4E37052971690ED3EEC4ED9AD41B0AEAE4DCC913443B9FA5418FB75DC1725FA989BA8DB5D9E221804FC7F36F3135C8B93AFF66DF89408CFD50993D308E51DF00540F380C0AD06266B80F646B917BA58384B55658EAD2D453766C4843FCDD934E8352A6DF6A081A15BDE07BF67E977E72BFE1AC37F411111A0A4D101A2CCF95EBEC7FCFC82B45DBA88939B0831987AE4D15C05A2E08F713BB0B6BB0E2436B7F9C83D2D869432465DEB9185913DE215937EFB4A52DA50BEF88688F5AB4397A04B14CDBFA5BFD948CD6EA1122D9D3C2927DE9D066297AA2C6FE8E478EC0F41459287EF9B8A1A56164C72AE3DCE5E914E8BC3C3821E0ADD6D1C9048D71BD71F71F3A6E04E63687298DE5A3704ADA82AA369CCD7F342F79E988A7BE066CA55944E0E3712F472891761E5617DC048C69AA4C250AA1560D6591FC0E7492027BEED67310E3482B1487E41DEA5E070894A5FB93FF4462D1F60C4B1CA7C15275EEA2B3790ED12EA930FD7F7F07D60807E4AAB73D1F889DABF2E687A487F331AC17D8DE24E8448E672F87424F0D1A73721A1A987519D0E3BB91D15D012B1FCDB6E23EEA17E93869C5199984CE8A068CA96C3096273F8B23160A79EE0C208D9B70ED5E23CB3586DFD33E02D06F1C646250BD664C27D2BB9614FF5F043A6FEE1A235DA10DCAADB19205CB839BD616BB36B738AA28E1D4F767BD8BAB6C2F84887C7B2E16CF6E07AF90C1FCB6E6E5A4CC894072AF4393C63F7119FF694BF0A043AF5F0825557A99C40BABBDA97D5648687D493367812743335A8AD7696562538C8BA5DED182C4DC818E7E9F630B29A9534E2583E0F4B5862D4E4DB250A350BAF360EF133838FE55AA683E253746A704654EF692F4F818F5A172AB0B84673D0AF77CC1DF189AA5BB013E833D1B0943918768AC6A83E6BFB306D3C3786BD2C87129BFEA1C380A84C4983D262184427284BF3DEB9B4C58FB1899B07B9F60B4402618168B1445653E8E48CD92C048684302A6F5C217F110D6699707BA42316CB31FE8F4DA6B82243CF1264751225594AF1BB670339A9189163DB9E985A99BCF83A3039AF3E65BBCD8364745356B29D761853E00000006CDE5B63B9763DA3EABCFFDA517688BDEC2AE9213E6B0FD7003D95458798AE9449DE4F1135E093B39F597A34B14AAB7F596E25BA469533442F54C14921ABCC5D04A05486CD16C8564E6A19C11BEDA574A9800107DCEAD013A7E6A32966B5BBE9FDFDB0184FE0707209B6D9EC43066899717E487E5FDEE02061EA5069B2D6C9C87D6BEB1310F1B9E723AE372DB7BE9EF6657F51FD0DE62464D3B37755095829F625EA76F5FD7FCD5829863F963FCD7F9FFFF3729688D025DF7952B067C62198E4C6CE06E960B0BAC6ADBC9459D9AC0BE1BAD46F95A121BBBE6953BAA10252419E2AB6BCA1B0AA1FA64DF728160B4FB7A62499C24D269FF59977649064C5986D615E6952EA0DA5B1C04C443BC27A63D391D5BFAE824F0161791E65896DC100EAF80037FD800A5079337554BD990E0D0A1A4C4C45741E72FB3E840665F2881D2CCC5":"000000060000000461F2DF219685CF313043780A57C18071725490AB8D53B676D484238BA8C373572407938CC578045649964958C0A872FA":0
2332+
2333+LMS hash-sigs interop negative test (altered random value)
2334+lms_verify_test:"D5557C719EBB0DBECF563E5CDB16568BB11CD779":"0000000000000004B167A9AC495BD4EA34CD8EE5AAA2A656D518C33612FD87171421BFC3977CFA99765C6D496499C72A1DE21360DA57EB96BC83DB8AA92E560054C7805B04E336162FB4C411B509F76959F2458B0E53CF830E0145CCD439D494259EA4818CA68924A7E8B9DD36D6A9C7849B72F9338ED6C80A3E70B717E8E65B991B2FF9D8B49820E8ABC9E2ECC17DB38E855DA75D84DF9885C7F9DFB4ABC209CFF1D37D66595371D688A203CB89168945200C39169F784B19665CE1FB47D58BFA734C3E0E7E31D1206A033C6D8E25B7E45CA779A5FDE00C6B1CAC44884F2B52A380E1F6D8753549F7F4948A95AEA83703CF3AA108FA4F735AFC0DA1A03C378033D8B5959E7BE05D3C5070E709181AC09EFEC04128ABD7E8F37304FAD4B66373D4A83CFC1EF632DF6DB95577C2C6101CBDC807109ED8AE831FFB73DBC80942C58F334663B980F982C74B943BF7C57147250AADE595310387E3BB1A2705E9EC73DE7FABDA5EC0B1141A18798215B9A70F8D688357C833ED869059A2AA3360155EF84426288198D0FBB78223816B17093684C48942ED18FCD351C34E108E5B71D1CE39E318B5D991B650C46A91112E013E1180F2054C7A22429CAB31512BA34EA3AD9B68C5001EB70C993297CCF11914ECAF059922DAEE7D90ACE2567495ADA066E7DA1679CA45DAC1990B17184E7BE2E6A0F26AD77F19855D074F5B37372277484CE30B80A0540173C1B310C3E7B683A487B5D0676218EA1F65FEA444C493FC535E948EAB62252DCC90516BB45B60D4253DB6979FE342DC5CA1B86B01B2D8EBA79B0BC7B6984535616B792BB45F3C0E20B506E0694E1D5BA28FE96D34FE2BE354777D090404DD3508E9F7918FF5593ADB468478CA8A1F6AF752CC76F401E373B71471D9D70F455C8A73E4E7B6714394B1DD0E2A816AF3D5149835DAE477A70DEE0BDAC22F99A04BFB7C2D4AD53079C326F620DFD3F7CED4AB7F2E291507AA046331050F9E2205C52B36CBAEE817C5C3B1FBCDE61C54C8CB7B67E0570FA44728EC8FD091D5CEDC19C6B99840F7A0E49086F707E959D34B30E255B67BBAA24FADE532BF3D21825626E114BD8213170B0C2F01733D4ED420D01EE3ACD5F84DACE674AE7127DB0A80ACE252CAD9ADADDAFAB27281AFD6DDD72DB5AF878326C45D7DB1EFF8BC40895A3473A52461D076881310AD9937307217B5C0448B509EF9BA075936CC09E11B8838D3A6BC5EF9FAEA85A3EC87EEFDF2E38CD9732730085375A4FFC4E0A213B0E1FC3DE2D37F1EDACC3030F617F3459A03BFCF776A05FD3B7FD135782F6D6E7C5E92B56A1316525B26D3AE1CEA3C0C7CF3AA7B1E72B7599A31B50837D79A7AB61B9A9E2B7AABD2D605C97E302EB4B66C0588C24147955EA0892A54D42843568FE0863E7EFEAE336D302E672EA62689B4DAA02DD5BC99D93886EC7F411C53CE1CAEAB59FBC0B06E0E294F1900F8C626C6FF520AE2323DA797CDC120DBC19F7FEBA0E13429508C5B838A0F8B9B28A069C5DD40E2F6CC2C95FC6ACE7E1351516817BD2DC1AE08D498AD2B0BD1D8374942FF31FC6A4689C592244C919C3561E73DD4986FA500000006BBF34F6EE152B64FCDD1CB6848D2DA761798707060431761006E2EBD9312851F4F3DF3C46E10F643DDD58CB3D9F4D371F655EE26271F2DDE84A14CAA6A077DD96AF83849DE6CA8F2F3248902CBF49630C18C3EE3123D951CE9162D0E742B899AF9E5DA8D28A41C7CADF0194CDB09418BF48BF322F8C5E9563524196FA8AB785B43C4EA41A36148028D2F4C7356CDEEB09532CD7F2C80FC36589FF7A9954100C8697AEB014997C3088C242B4F70D26CE7F7E77384A9CF536EC5C5329E08BD6C1D65EFEFC1389A42D16FFB43A0E1D7661220E92A4A59703FB28410E73A677E803D4441929DFD7269E6F77AE8CA8C70B67B250A8728291EA5D4E3F03D505639408C88156DCBECC137142FA3585C09D99B84D8C380A5D29CE2ADA10A25F7CF939FE23288551F37FE2B7233BF97C0F5726B972E087BCBA095957CCD794794A4F50027":"00000006000000046B0927585C8547228D495361D73B970C287A2254BF8F1B170E55ACC9520A56CE5D2C711B6617718B49247D28CCC6D11D":MBEDTLS_ERR_LMS_VERIFY_FAILED
2335+
2336+LMS negative test (invalid lms type) #1
2337+lms_verify_test:"0000000000000000000000000000000000000000":"000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000070000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":"0000000700000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":MBEDTLS_ERR_LMS_BAD_INPUT_DATA
2338+
2339+LMS negative test (invalid lms type) #2
2340+lms_verify_test:"0000000000000000000000000000000000000000":"000000000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":"0000000500000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":MBEDTLS_ERR_LMS_BAD_INPUT_DATA
2341+
2342+LMS negative test (invalid lm_ots type) #1
2343+lms_verify_test:"0000000000000000000000000000000000000000":"000000000000000300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":"0000000600000003000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":MBEDTLS_ERR_LMS_BAD_INPUT_DATA
2344+
2345+LMS negative test (invalid lm_ots type) #2
2346+lms_verify_test:"0000000000000000000000000000000000000000":"000000000000000500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":"0000000600000005000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":MBEDTLS_ERR_LMS_BAD_INPUT_DATA
2347+
2348+LMS negative test (invalid leaf ID)
2349+lms_verify_test:"0000000000000000000000000000000000000000":"000004000000000400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":"0000000600000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000":MBEDTLS_ERR_LMS_VERIFY_FAILED
2350+
2351+LMS import/export test
2352+lms_import_export_test:"00000006000000046B0927585C8547228D495361D73B970C287A2254BF8F1B170E55ACC9520A56CE5D2C711B6617718B49247D28CCC6D11D"
2353diff --git a/tests/suites/test_suite_lms.function b/tests/suites/test_suite_lms.function
2354new file mode 100644
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002355index 000000000..c3ebb9214
Raef Coles8a3bf392022-06-22 13:53:59 +01002356--- /dev/null
2357+++ b/tests/suites/test_suite_lms.function
Antonio de Angelis90bee0f2022-07-13 11:22:41 +01002358@@ -0,0 +1,84 @@
Raef Coles8a3bf392022-06-22 13:53:59 +01002359+/* BEGIN_HEADER */
2360+#include "mbedtls/lms.h"
2361+#include "mbedtls/entropy.h"
2362+#include "mbedtls/ctr_drbg.h"
2363+
2364+/* END_HEADER */
2365+
2366+/* BEGIN_DEPENDENCIES
2367+ * depends_on:MBEDTLS_LMS_C:MBEDTLS_SHA256_C:MBEDTLS_CTR_DRBG_C
2368+ * END_DEPENDENCIES
2369+ */
2370+
2371+/* BEGIN_CASE */
2372+void lms_sign_verify_test ( data_t * msg )
2373+{
2374+ mbedtls_lms_context ctx;
2375+ unsigned char sig[MBEDTLS_LMS_SIG_LEN];
2376+ mbedtls_entropy_context entropy_ctx;
2377+ mbedtls_ctr_drbg_context drbg_ctx;
2378+ uint8_t seed[16];
2379+ int rc;
2380+
2381+ mbedtls_entropy_init( &entropy_ctx );
2382+ mbedtls_ctr_drbg_init( &drbg_ctx );
2383+ mbedtls_lms_init( &ctx );
2384+
2385+ TEST_ASSERT( mbedtls_ctr_drbg_seed( &drbg_ctx, mbedtls_entropy_func,
2386+ &entropy_ctx, ( uint8_t* )"", 0 ) == 0 );
2387+ TEST_ASSERT( mbedtls_ctr_drbg_random( &drbg_ctx, seed, sizeof( seed ) ) == 0 );
2388+
2389+ TEST_ASSERT( mbedtls_lms_set_algorithm_type( &ctx, MBEDTLS_LMS_SHA256_M32_H10, MBEDTLS_LMOTS_SHA256_N32_W8 ) == 0 );
2390+
2391+ /* Allocation failure isn't a test failure, since it likely just means there's not enough memory to run the test */
2392+ rc = mbedtls_lms_gen_privkey( &ctx, mbedtls_ctr_drbg_random, &drbg_ctx, seed, sizeof( seed ) );
2393+ TEST_ASSUME( rc != MBEDTLS_ERR_LMS_ALLOC_FAILED );
2394+ TEST_ASSERT( rc == 0 );
2395+
2396+ TEST_ASSERT( mbedtls_lms_gen_pubkey( &ctx) == 0 );
2397+
2398+ TEST_ASSERT( mbedtls_lms_sign( &ctx, mbedtls_ctr_drbg_random, &drbg_ctx, msg->x, msg->len, sig ) == 0 );
2399+
2400+ TEST_ASSERT( mbedtls_lms_verify( &ctx, msg->x, msg->len, sig) == 0 );
2401+
2402+exit:
2403+ mbedtls_entropy_free( &entropy_ctx );
2404+ mbedtls_ctr_drbg_free( &drbg_ctx );
2405+ mbedtls_lms_free( &ctx );
2406+}
2407+/* END_CASE */
2408+
2409+/* BEGIN_CASE */
2410+void lms_verify_test ( data_t * msg, data_t * sig, data_t * pub_key,
2411+ int expected_rc )
2412+{
2413+ mbedtls_lms_context ctx;
2414+
2415+ mbedtls_lms_init( &ctx);
2416+
2417+ mbedtls_lms_import_pubkey( &ctx, pub_key->x );
2418+
2419+ TEST_ASSERT( mbedtls_lms_verify( &ctx, msg->x, msg->len, sig->x ) == expected_rc );
2420+
2421+exit:
2422+ mbedtls_lms_free( &ctx );
2423+}
2424+/* END_CASE */
2425+
2426+/* BEGIN_CASE */
2427+void lms_import_export_test ( data_t * pub_key )
2428+{
2429+ mbedtls_lms_context ctx;
2430+ uint8_t exported_pub_key[MBEDTLS_LMS_PUBKEY_LEN];
2431+
2432+ mbedtls_lms_init(&ctx);
2433+ TEST_ASSERT( mbedtls_lms_import_pubkey( &ctx, pub_key->x ) == 0 );
2434+ TEST_ASSERT( mbedtls_lms_export_pubkey( &ctx, exported_pub_key) == 0 );
2435+
2436+ ASSERT_COMPARE( pub_key->x, MBEDTLS_LMS_PUBKEY_LEN,
2437+ exported_pub_key, MBEDTLS_LMS_PUBKEY_LEN );
2438+
2439+exit:
2440+ mbedtls_lms_free( &ctx );
2441+}
2442+/* END_CASE */
Raef Coles8a3bf392022-06-22 13:53:59 +01002443--
Antonio de Angelis90bee0f2022-07-13 11:22:41 +010024442.25.1
Raef Coles8a3bf392022-06-22 13:53:59 +01002445