blob: dba8835f079c977f76f9470cb9c749a2d2bb8edf [file] [log] [blame]
Raef Coles8ff6df52021-07-21 12:42:15 +01001/**
2 * \file lms.h
3 *
4 * \brief This file provides an API for the LMS post-quantum-safe stateful-hash
Raef Coles2ad6e612022-08-24 13:33:35 +01005 public-key signature scheme as defined in RFC8554 and NIST.SP.200-208.
6 * This implementation currently only supports a single parameter set
7 * MBEDTLS_LMS_SHA256_M32_H10 in order to reduce complexity. This is one
8 * of the signature schemes recommended by the IETF draft SUIT standard
9 * for IOT firmware upgrades (RFC9019).
Raef Coles8ff6df52021-07-21 12:42:15 +010010 */
11/*
12 * Copyright The Mbed TLS Contributors
13 * SPDX-License-Identifier: Apache-2.0
14 *
15 * Licensed under the Apache License, Version 2.0 (the "License"); you may
16 * not use this file except in compliance with the License.
17 * You may obtain a copy of the License at
18 *
19 * http://www.apache.org/licenses/LICENSE-2.0
20 *
21 * Unless required by applicable law or agreed to in writing, software
22 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
23 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24 * See the License for the specific language governing permissions and
25 * limitations under the License.
26 */
27#ifndef MBEDTLS_LMS_H
28#define MBEDTLS_LMS_H
29
30#include <stdint.h>
31#include <stddef.h>
32
Raef Coles01c71a12022-08-31 15:55:00 +010033#include "mbedtls/build_info.h"
Raef Coles8ff6df52021-07-21 12:42:15 +010034
35#define MBEDTLS_ERR_LMS_BAD_INPUT_DATA -0x0011 /**< Bad data has been input to an LMS function */
Raef Coles01c71a12022-08-31 15:55:00 +010036#define MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS -0x0013 /**< Specified LMS key has utilised all of its private keys */
Raef Coles8ff6df52021-07-21 12:42:15 +010037#define MBEDTLS_ERR_LMS_VERIFY_FAILED -0x0015 /**< LMS signature verification failed */
38#define MBEDTLS_ERR_LMS_ALLOC_FAILED -0x0017 /**< LMS failed to allocate space for a private key */
Raef Colesc8f96042022-08-25 13:49:54 +010039#define MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL -0x0019 /**< Input/output buffer is too small to contain requited data */
Raef Coles8ff6df52021-07-21 12:42:15 +010040
Raef Colesab300f12022-09-28 17:12:41 +010041/* Currently only defined for SHA256, 32 is the max hash output size */
42#define MBEDTLS_LMOTS_N_HASH_LEN_MAX (32u)
43#define MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX (34u)
44#define MBEDTLS_LMOTS_N_HASH_LEN(type) (type == MBEDTLS_LMOTS_SHA256_N32_W8 ? 32u : 0)
45#define MBEDTLS_LMOTS_I_KEY_ID_LEN (16u)
46#define MBEDTLS_LMOTS_Q_LEAF_ID_LEN (4u)
47
Raef Coles01c71a12022-08-31 15:55:00 +010048#define MBEDTLS_LMS_TYPE_LEN (4)
Raef Colese9479a02022-09-01 16:06:35 +010049#define MBEDTLS_LMS_H_TREE_HEIGHT(type) (type == MBEDTLS_LMS_SHA256_M32_H10 ? 10u : 0)
Raef Coles8ff6df52021-07-21 12:42:15 +010050
Raef Colese9479a02022-09-01 16:06:35 +010051/* The length of a hash output, Currently only imlemented for SHA256.
52 * Max is 32 bytes.
53 */
54/* The length of a hash output, Currently only imlemented for SHA256.
55 * Max is 32 bytes.
56 */
57#define MBEDTLS_LMS_M_NODE_BYTES(type) (type == MBEDTLS_LMS_SHA256_M32_H10 ? 32 : 0)
58#define MBEDTLS_LMS_M_NODE_BYTES_MAX 32
Raef Coles8ff6df52021-07-21 12:42:15 +010059
Raef Colese9479a02022-09-01 16:06:35 +010060#define MBEDTLS_LMS_SIG_LEN(type, otstype) (MBEDTLS_LMOTS_Q_LEAF_ID_LEN + \
61 MBEDTLS_LMOTS_SIG_LEN(otstype) + \
62 MBEDTLS_LMS_TYPE_LEN + \
63 (MBEDTLS_LMS_H_TREE_HEIGHT(type) * \
64 MBEDTLS_LMS_M_NODE_BYTES(type)))
65
66#define MBEDTLS_LMS_PUBLIC_KEY_LEN(type) (MBEDTLS_LMS_TYPE_LEN + \
67 MBEDTLS_LMOTS_TYPE_LEN + \
68 MBEDTLS_LMOTS_I_KEY_ID_LEN + \
69 MBEDTLS_LMS_M_NODE_BYTES(type))
Raef Coles8ff6df52021-07-21 12:42:15 +010070
Raef Coles8ff6df52021-07-21 12:42:15 +010071
72#ifdef __cplusplus
73extern "C" {
74#endif
75
Raef Coles366d67d2022-09-01 17:23:12 +010076/** The Identifier of the LMS parameter set, as per
77 * https://www.iana.org/assignments/leighton-micali-signatures/leighton-micali-signatures.xhtml
Raef Colesc4647462022-06-15 12:17:51 +010078 * We are only implementing a subset of the types, particularly H10, for the sake of simplicty.
79 */
Raef Coles8ff6df52021-07-21 12:42:15 +010080typedef enum {
81 MBEDTLS_LMS_SHA256_M32_H10 = 0x6,
82} mbedtls_lms_algorithm_type_t;
83
Raef Colesab300f12022-09-28 17:12:41 +010084/** The Identifier of the LMOTS parameter set, as per
85 * https://www.iana.org/assignments/leighton-micali-signatures/leighton-micali-signatures.xhtml.
86 * We are only implementing a subset of the types, particularly N32_W8, for the sake of simplicty.
87 */
88typedef enum {
89 MBEDTLS_LMOTS_SHA256_N32_W8 = 4
90} mbedtls_lmots_algorithm_type_t;
91
92/** LMOTS parameters structure.
93 *
94 * This contains the metadata associated with an LMOTS key, detailing the
95 * algorithm type, the key ID, and the leaf identifier should be key be part of
96 * a LMS key.
97 */
98typedef struct {
99 unsigned char MBEDTLS_PRIVATE(I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN]); /*!< The key
100 identifier. */
101 unsigned char MBEDTLS_PRIVATE(q_leaf_identifier[MBEDTLS_LMOTS_Q_LEAF_ID_LEN]); /*!< Which
102 leaf of the LMS key this is.
103 0 if the key is not part of an LMS key. */
104 mbedtls_lmots_algorithm_type_t MBEDTLS_PRIVATE(type); /*!< The LM-OTS key type identifier as
105 per IANA. Only SHA256_N32_W8 is
106 currently supported. */
107} mbedtls_lmots_parameters_t;
108
109/** LMOTS public context structure.
110 *
111 * A LMOTS public key is a hash output, and the applicable parameter set.
112 *
113 * The context must be initialized before it is used. A public key must either
114 * be imported or generated from a private context.
115 *
116 * \dot
117 * digraph lmots_public_t {
118 * UNINITIALIZED -> INIT [label="init"];
119 * HAVE_PUBLIC_KEY -> INIT [label="free"];
120 * INIT -> HAVE_PUBLIC_KEY [label="import_public_key"];
121 * INIT -> HAVE_PUBLIC_KEY [label="calculate_public_key from private key"];
122 * HAVE_PUBLIC_KEY -> HAVE_PUBLIC_KEY [label="export_public_key"];
123 * }
124 * \enddot
125 */
126typedef struct {
127 mbedtls_lmots_parameters_t MBEDTLS_PRIVATE(params);
128 unsigned char MBEDTLS_PRIVATE(public_key)[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
129 unsigned char MBEDTLS_PRIVATE(have_public_key); /*!< Whether the context contains a public key.
130 Boolean values only. */
131} mbedtls_lmots_public_t;
132
133#ifdef MBEDTLS_LMS_PRIVATE
134/** LMOTS private context structure.
135 *
136 * A LMOTS private key is one hash output for each of digit of the digest +
137 * checksum, and the applicable parameter set.
138 *
139 * The context must be initialized before it is used. A public key must either
140 * be imported or generated from a private context.
141 *
142 * \dot
143 * digraph lmots_public_t {
144 * UNINITIALIZED -> INIT [label="init"];
145 * HAVE_PRIVATE_KEY -> INIT [label="free"];
146 * INIT -> HAVE_PRIVATE_KEY [label="generate_private_key"];
147 * HAVE_PRIVATE_KEY -> INIT [label="sign"];
148 * }
149 * \enddot
150 */
151typedef struct {
152 mbedtls_lmots_parameters_t MBEDTLS_PRIVATE(params);
153 unsigned char MBEDTLS_PRIVATE(private_key)[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
154 unsigned char MBEDTLS_PRIVATE(have_private_key); /*!< Whether the context contains a private key.
155 Boolean values only. */
156} mbedtls_lmots_private_t;
157#endif /* MBEDTLS_LMS_PRIVATE */
158
Raef Coles8ff6df52021-07-21 12:42:15 +0100159
Raef Coles01c71a12022-08-31 15:55:00 +0100160/** LMS parameters structure.
161 *
162 * This contains the metadata associated with an LMS key, detailing the
163 * algorithm type, the type of the underlying OTS algorithm, and the key ID.
164 */
165typedef struct {
166 unsigned char MBEDTLS_PRIVATE(I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN]); /*!< The key
167 identifier. */
168 mbedtls_lmots_algorithm_type_t MBEDTLS_PRIVATE(otstype); /*!< The LM-OTS key type identifier as
169 per IANA. Only SHA256_N32_W8 is
170 currently supported. */
171 mbedtls_lms_algorithm_type_t MBEDTLS_PRIVATE(type); /*!< The LMS key type identifier as per
172 IANA. Only SHA256_M32_H10 is currently
173 supported. */
174} mbedtls_lms_parameters_t;
175
176/** LMS public context structure.
177 *
178 *A LMS public key is the hash output that is the root of the merkle tree, and
179 * the applicable parameter set
Raef Coles2ad6e612022-08-24 13:33:35 +0100180 *
181 * The context must be initialized before it is used. A public key must either
Raef Coles01c71a12022-08-31 15:55:00 +0100182 * be imported or generated from a private context.
Raef Coles2ad6e612022-08-24 13:33:35 +0100183 *
184 * \dot
Raef Coles01c71a12022-08-31 15:55:00 +0100185 * digraph lms_public_t {
Raef Coles2ad6e612022-08-24 13:33:35 +0100186 * UNINITIALIZED -> INIT [label="init"];
Raef Coles01c71a12022-08-31 15:55:00 +0100187 * HAVE_PUBLIC_KEY -> INIT [label="free"];
188 * INIT -> HAVE_PUBLIC_KEY [label="import_public_key"];
189 * INIT -> HAVE_PUBLIC_KEY [label="calculate_public_key from private key"];
190 * HAVE_PUBLIC_KEY -> HAVE_PUBLIC_KEY [label="export_public_key"];
Raef Coles2ad6e612022-08-24 13:33:35 +0100191 * }
192 * \enddot
193 */
Raef Coles8ff6df52021-07-21 12:42:15 +0100194typedef struct {
Raef Coles01c71a12022-08-31 15:55:00 +0100195 mbedtls_lms_parameters_t MBEDTLS_PRIVATE(params);
Raef Colese9479a02022-09-01 16:06:35 +0100196 unsigned char MBEDTLS_PRIVATE(T_1_pub_key)[MBEDTLS_LMS_M_NODE_BYTES_MAX]; /*!< The public key, in
Raef Colesc4647462022-06-15 12:17:51 +0100197 the form of the merkle tree root node. */
Raef Coles01c71a12022-08-31 15:55:00 +0100198 unsigned char MBEDTLS_PRIVATE(have_public_key); /*!< Whether the context contains a public key.
199 Boolean values only. */
200} mbedtls_lms_public_t;
Raef Coles8ff6df52021-07-21 12:42:15 +0100201
202
Raef Colesab4f8742022-09-01 12:24:31 +0100203#ifdef MBEDTLS_LMS_PRIVATE
Raef Coles01c71a12022-08-31 15:55:00 +0100204/** LMS private context structure.
205 *
206 * A LMS private key is a set of LMOTS private keys, an index to the next usable
207 * key, and the applicable parameter set.
208 *
209 * The context must be initialized before it is used. A public key must either
210 * be imported or generated from a private context.
211 *
212 * \dot
213 * digraph lms_public_t {
214 * UNINITIALIZED -> INIT [label="init"];
215 * HAVE_PRIVATE_KEY -> INIT [label="free"];
216 * INIT -> HAVE_PRIVATE_KEY [label="generate_private_key"];
217 * }
218 * \enddot
219 */
220typedef struct {
221 mbedtls_lms_parameters_t MBEDTLS_PRIVATE(params);
222 uint32_t MBEDTLS_PRIVATE(q_next_usable_key); /*!< The index of the next OTS key that has not
223 been used. */
224 mbedtls_lmots_private_t *MBEDTLS_PRIVATE(ots_private_keys); /*!< The private key material. One OTS key
225 for each leaf node in the merkle tree. */
226 mbedtls_lmots_public_t *MBEDTLS_PRIVATE(ots_public_keys); /*!< The OTS key public keys, used to
227 build the merkle tree. */
228 unsigned char MBEDTLS_PRIVATE(have_private_key); /*!< Whether the context contains a private key.
229 Boolean values only. */
230} mbedtls_lms_private_t;
Raef Colesab4f8742022-09-01 12:24:31 +0100231#endif /* MBEDTLS_LMS_PRIVATE */
Raef Coles01c71a12022-08-31 15:55:00 +0100232
Raef Coles8ff6df52021-07-21 12:42:15 +0100233/**
Raef Coles01c71a12022-08-31 15:55:00 +0100234 * \brief This function initializes an LMS public context
Raef Coles8ff6df52021-07-21 12:42:15 +0100235 *
236 * \param ctx The uninitialized LMS context that will then be
237 * initialized.
238 */
Raef Coles01c71a12022-08-31 15:55:00 +0100239void mbedtls_lms_init_public( mbedtls_lms_public_t *ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100240
241/**
Raef Coles01c71a12022-08-31 15:55:00 +0100242 * \brief This function uninitializes an LMS public context
Raef Coles8ff6df52021-07-21 12:42:15 +0100243 *
244 * \param ctx The initialized LMS context that will then be
245 * uninitialized.
246 */
Raef Coles01c71a12022-08-31 15:55:00 +0100247void mbedtls_lms_free_public( mbedtls_lms_public_t *ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100248
249/**
Raef Coles01c71a12022-08-31 15:55:00 +0100250 * \brief This function imports an LMS public key into a
251 * public LMS context.
Raef Coles8ff6df52021-07-21 12:42:15 +0100252 *
Raef Coles01c71a12022-08-31 15:55:00 +0100253 * \note Before this function is called, the context must
254 * have been initialized.
Raef Coles8ff6df52021-07-21 12:42:15 +0100255 *
Raef Coles01c71a12022-08-31 15:55:00 +0100256 * \note See IETF RFC8554 for details of the encoding of
257 * this public key.
258 *
259 * \param ctx The initialized LMS context store the key in.
260 * \param key The buffer from which the key will be read.
261 * #MBEDTLS_LMS_PUBLIC_KEY_LEN bytes will be read from
262 * this.
263 * \param key_size The size of the key being imported.
264 *
265 * \return \c 0 on success.
266 * \return A non-zero error code on failure.
Raef Coles8ff6df52021-07-21 12:42:15 +0100267 */
Raef Coles01c71a12022-08-31 15:55:00 +0100268int mbedtls_lms_import_public_key( mbedtls_lms_public_t *ctx,
269 const unsigned char *key, size_t key_size );
270
271/**
272 * \brief This function verifies a LMS signature, using a
273 * LMS context that contains a public key.
274 *
275 * \note Before this function is called, the context must
276 * have been initialized and must contain a public key
277 * (either by import or generation).
278 *
279 * \param ctx The initialized LMS public context from which the
280 * public key will be read.
281 * \param msg The buffer from which the message will be read.
282 * \param msg_size The size of the message that will be read.
283 * \param sig The buf from which the signature will be read.
284 * #MBEDTLS_LMS_SIG_LEN bytes will be read from
285 * this.
286 * \param sig_size The size of the signature to be verified.
287 *
288 * \return \c 0 on successful verification.
289 * \return A non-zero error code on failure.
290 */
291int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx,
292 const unsigned char *msg, size_t msg_size,
293 const unsigned char *sig, size_t sig_size );
294
Raef Colesab4f8742022-09-01 12:24:31 +0100295#ifdef MBEDTLS_LMS_PRIVATE
Raef Coles01c71a12022-08-31 15:55:00 +0100296/**
297 * \brief This function initializes an LMS private context
298 *
299 * \param ctx The uninitialized LMS private context that will
300 * then be initialized. */
301void mbedtls_lms_init_private( mbedtls_lms_private_t *ctx );
302
303/**
304 * \brief This function uninitializes an LMS private context
305 *
306 * \param ctx The initialized LMS private context that will then
307 * be uninitialized.
308 */
309void mbedtls_lms_free_private( mbedtls_lms_private_t *ctx );
310
311/**
312 * \brief This function generates an LMS private key, and
313 * stores in into an LMS private context.
314 *
315 * \warning This function is **not intended for use in
316 * production**, due to as-yet unsolved problems with
317 * handling stateful keys.
318 *
319 * \note The seed must have at least 256 bits of entropy.
320 *
321 * \param ctx The initialized LMOTS context to generate the key
322 * into.
323 * \param type The LMS parameter set identifier.
324 * \param otstype The LMOTS parameter set identifier.
325 * \param f_rng The RNG function to be used to generate the key ID.
326 * \param p_rng The RNG context to be passed to f_rng
327 * \param seed The seed used to deterministically generate the
328 * key.
329 * \param seed_size The length of the seed.
330 *
331 * \return \c 0 on success.
332 * \return A non-zero error code on failure.
333 */
334int mbedtls_lms_generate_private_key( mbedtls_lms_private_t *ctx,
335 mbedtls_lms_algorithm_type_t type,
336 mbedtls_lmots_algorithm_type_t otstype,
337 int (*f_rng)(void *, unsigned char *, size_t),
338 void* p_rng, unsigned char *seed,
339 size_t seed_size );
340
341/**
342 * \brief This function generates an LMS public key from a
343 * LMS context that already contains a private key.
344 *
345 * \note Before this function is called, the context must
346 * have been initialized and the context must contain
347 * a private key.
348 *
349 * \param ctx The initialized LMS public context to generate the key
350 * from and store it into.
351 *
Raef Coles403558c2022-09-23 17:03:53 +0100352 * \param priv_ctx The LMS private context to read the private key
Raef Coles01c71a12022-08-31 15:55:00 +0100353 * from. This must have been initialized and contain a
354 * private key.
355 *
356 * \return \c 0 on success.
357 * \return A non-zero error code on failure.
358 */
359int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx,
360 mbedtls_lms_private_t *priv_ctx );
361
362/**
363 * \brief This function exports an LMS public key from a
364 * LMS public context that already contains a public
365 * key.
366 *
367 * \note Before this function is called, the context must
368 * have been initialized and the context must contain
369 * a public key.
370 *
371 * \note See IETF RFC8554 for details of the encoding of
372 * this public key.
373 *
374 * \param ctx The initialized LMS public context that contains
375 * the public key.
376 * \param key The buffer into which the key will be output. Must
377 * be at least #MBEDTLS_LMS_PUBLIC_KEY_LEN in size.
378 * \param key_size The size of the key buffer.
379 * \param key_len If not NULL, will be written with the size of the
380 * key.
381 *
382 * \return \c 0 on success.
383 * \return A non-zero error code on failure.
384 */
385int mbedtls_lms_export_public_key( mbedtls_lms_public_t *ctx, unsigned char *key,
386 size_t key_size, size_t *key_len );
Raef Coles8ff6df52021-07-21 12:42:15 +0100387
388/**
389 * \brief This function creates a LMS signature, using a
Raef Coles01c71a12022-08-31 15:55:00 +0100390 * LMS context that contains unused private keys.
Raef Coles8ff6df52021-07-21 12:42:15 +0100391 *
Raef Coles2ad6e612022-08-24 13:33:35 +0100392 * \warning This function is **not intended for use in
393 * production**, due to as-yet unsolved problems with
394 * handling stateful keys.
Raef Coles0aa18e02022-06-15 13:05:56 +0100395 *
Raef Coles8ff6df52021-07-21 12:42:15 +0100396 * \note Before this function is called, the context must
397 * have been initialized and must contain a private
398 * key.
399 *
400 * \note Each of the LMOTS private keys inside a LMS private
401 * key can only be used once. If they are reused, then
402 * attackers may be able to forge signatures with that
403 * key. This is all handled transparently, but it is
404 * important to not perform copy operations on LMS
405 * contexts that contain private key material.
406 *
Raef Coles01c71a12022-08-31 15:55:00 +0100407 * \param ctx The initialized LMS private context from which the
Raef Coles8ff6df52021-07-21 12:42:15 +0100408 * private key will be read.
409 * \param f_rng The RNG function to be used for signature
410 * generation.
411 * \param p_rng The RNG context to be passed to f_rng
412 * \param msg The buffer from which the message will be read.
Raef Coles01c71a12022-08-31 15:55:00 +0100413 * \param msg_size The size of the message that will be read.
Raef Coles8ff6df52021-07-21 12:42:15 +0100414 * \param sig The buf into which the signature will be stored.
Raef Coles01c71a12022-08-31 15:55:00 +0100415 * Must be at least #MBEDTLS_LMS_SIG_LEN in size.
416 * \param sig_size The size of the buffer the signature will be
417 * written into.
418 * \param sig_len If not NULL, will be written with the size of the
419 * signature.
Raef Coles8ff6df52021-07-21 12:42:15 +0100420 *
421 * \return \c 0 on success.
422 * \return A non-zero error code on failure.
423 */
Raef Coles01c71a12022-08-31 15:55:00 +0100424int mbedtls_lms_sign( mbedtls_lms_private_t *ctx,
Raef Coles8ff6df52021-07-21 12:42:15 +0100425 int (*f_rng)(void *, unsigned char *, size_t),
Raef Coles01c71a12022-08-31 15:55:00 +0100426 void* p_rng, unsigned char *msg, unsigned int msg_size,
Raef Coles9b88ee52022-09-02 12:04:21 +0100427 unsigned char *sig, size_t sig_size, size_t *sig_len );
Raef Colesab4f8742022-09-01 12:24:31 +0100428#endif /* MBEDTLS_LMS_PRIVATE */
Raef Coles8ff6df52021-07-21 12:42:15 +0100429
430#ifdef __cplusplus
431}
432#endif
433
434#endif /* MBEDTLS_LMS_H */