blob: 23c5ebdf46129ba14893cb5e72fdcfcb4cf7c5c8 [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 Coles7dce69a2022-08-24 14:07:06 +010033#include "lmots.h"
34
Raef Coles01c71a12022-08-31 15:55:00 +010035#include "mbedtls/build_info.h"
Raef Coles8ff6df52021-07-21 12:42:15 +010036
37#define MBEDTLS_ERR_LMS_BAD_INPUT_DATA -0x0011 /**< Bad data has been input to an LMS function */
Raef Coles01c71a12022-08-31 15:55:00 +010038#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 +010039#define MBEDTLS_ERR_LMS_VERIFY_FAILED -0x0015 /**< LMS signature verification failed */
40#define MBEDTLS_ERR_LMS_ALLOC_FAILED -0x0017 /**< LMS failed to allocate space for a private key */
Raef Colesc8f96042022-08-25 13:49:54 +010041#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 +010042
Raef Coles01c71a12022-08-31 15:55:00 +010043#define MBEDTLS_LMS_TYPE_LEN (4)
Raef Colese9479a02022-09-01 16:06:35 +010044#define MBEDTLS_LMS_H_TREE_HEIGHT(type) (type == MBEDTLS_LMS_SHA256_M32_H10 ? 10u : 0)
Raef Coles8ff6df52021-07-21 12:42:15 +010045
Raef Colese9479a02022-09-01 16:06:35 +010046/* The length of a hash output, Currently only imlemented for SHA256.
47 * Max is 32 bytes.
48 */
49/* The length of a hash output, Currently only imlemented for SHA256.
50 * Max is 32 bytes.
51 */
52#define MBEDTLS_LMS_M_NODE_BYTES(type) (type == MBEDTLS_LMS_SHA256_M32_H10 ? 32 : 0)
53#define MBEDTLS_LMS_M_NODE_BYTES_MAX 32
Raef Coles8ff6df52021-07-21 12:42:15 +010054
Raef Colese9479a02022-09-01 16:06:35 +010055#define MBEDTLS_LMS_SIG_LEN(type, otstype) (MBEDTLS_LMOTS_Q_LEAF_ID_LEN + \
56 MBEDTLS_LMOTS_SIG_LEN(otstype) + \
57 MBEDTLS_LMS_TYPE_LEN + \
58 (MBEDTLS_LMS_H_TREE_HEIGHT(type) * \
59 MBEDTLS_LMS_M_NODE_BYTES(type)))
60
61#define MBEDTLS_LMS_PUBLIC_KEY_LEN(type) (MBEDTLS_LMS_TYPE_LEN + \
62 MBEDTLS_LMOTS_TYPE_LEN + \
63 MBEDTLS_LMOTS_I_KEY_ID_LEN + \
64 MBEDTLS_LMS_M_NODE_BYTES(type))
Raef Coles8ff6df52021-07-21 12:42:15 +010065
Raef Coles8ff6df52021-07-21 12:42:15 +010066
67#ifdef __cplusplus
68extern "C" {
69#endif
70
Raef Coles366d67d2022-09-01 17:23:12 +010071/** The Identifier of the LMS parameter set, as per
72 * https://www.iana.org/assignments/leighton-micali-signatures/leighton-micali-signatures.xhtml
Raef Colesc4647462022-06-15 12:17:51 +010073 * We are only implementing a subset of the types, particularly H10, for the sake of simplicty.
74 */
Raef Coles8ff6df52021-07-21 12:42:15 +010075typedef enum {
76 MBEDTLS_LMS_SHA256_M32_H10 = 0x6,
77} mbedtls_lms_algorithm_type_t;
78
79
Raef Coles01c71a12022-08-31 15:55:00 +010080/** LMS parameters structure.
81 *
82 * This contains the metadata associated with an LMS key, detailing the
83 * algorithm type, the type of the underlying OTS algorithm, and the key ID.
84 */
85typedef struct {
86 unsigned char MBEDTLS_PRIVATE(I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN]); /*!< The key
87 identifier. */
88 mbedtls_lmots_algorithm_type_t MBEDTLS_PRIVATE(otstype); /*!< The LM-OTS key type identifier as
89 per IANA. Only SHA256_N32_W8 is
90 currently supported. */
91 mbedtls_lms_algorithm_type_t MBEDTLS_PRIVATE(type); /*!< The LMS key type identifier as per
92 IANA. Only SHA256_M32_H10 is currently
93 supported. */
94} mbedtls_lms_parameters_t;
95
96/** LMS public context structure.
97 *
98 *A LMS public key is the hash output that is the root of the merkle tree, and
99 * the applicable parameter set
Raef Coles2ad6e612022-08-24 13:33:35 +0100100 *
101 * The context must be initialized before it is used. A public key must either
Raef Coles01c71a12022-08-31 15:55:00 +0100102 * be imported or generated from a private context.
Raef Coles2ad6e612022-08-24 13:33:35 +0100103 *
104 * \dot
Raef Coles01c71a12022-08-31 15:55:00 +0100105 * digraph lms_public_t {
Raef Coles2ad6e612022-08-24 13:33:35 +0100106 * UNINITIALIZED -> INIT [label="init"];
Raef Coles01c71a12022-08-31 15:55:00 +0100107 * HAVE_PUBLIC_KEY -> INIT [label="free"];
108 * INIT -> HAVE_PUBLIC_KEY [label="import_public_key"];
109 * INIT -> HAVE_PUBLIC_KEY [label="calculate_public_key from private key"];
110 * HAVE_PUBLIC_KEY -> HAVE_PUBLIC_KEY [label="export_public_key"];
Raef Coles2ad6e612022-08-24 13:33:35 +0100111 * }
112 * \enddot
113 */
Raef Coles8ff6df52021-07-21 12:42:15 +0100114typedef struct {
Raef Coles01c71a12022-08-31 15:55:00 +0100115 mbedtls_lms_parameters_t MBEDTLS_PRIVATE(params);
Raef Colese9479a02022-09-01 16:06:35 +0100116 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 +0100117 the form of the merkle tree root node. */
Raef Coles01c71a12022-08-31 15:55:00 +0100118 unsigned char MBEDTLS_PRIVATE(have_public_key); /*!< Whether the context contains a public key.
119 Boolean values only. */
120} mbedtls_lms_public_t;
Raef Coles8ff6df52021-07-21 12:42:15 +0100121
122
Raef Colesab4f8742022-09-01 12:24:31 +0100123#ifdef MBEDTLS_LMS_PRIVATE
Raef Coles01c71a12022-08-31 15:55:00 +0100124/** LMS private context structure.
125 *
126 * A LMS private key is a set of LMOTS private keys, an index to the next usable
127 * key, and the applicable parameter set.
128 *
129 * The context must be initialized before it is used. A public key must either
130 * be imported or generated from a private context.
131 *
132 * \dot
133 * digraph lms_public_t {
134 * UNINITIALIZED -> INIT [label="init"];
135 * HAVE_PRIVATE_KEY -> INIT [label="free"];
136 * INIT -> HAVE_PRIVATE_KEY [label="generate_private_key"];
137 * }
138 * \enddot
139 */
140typedef struct {
141 mbedtls_lms_parameters_t MBEDTLS_PRIVATE(params);
142 uint32_t MBEDTLS_PRIVATE(q_next_usable_key); /*!< The index of the next OTS key that has not
143 been used. */
144 mbedtls_lmots_private_t *MBEDTLS_PRIVATE(ots_private_keys); /*!< The private key material. One OTS key
145 for each leaf node in the merkle tree. */
146 mbedtls_lmots_public_t *MBEDTLS_PRIVATE(ots_public_keys); /*!< The OTS key public keys, used to
147 build the merkle tree. */
148 unsigned char MBEDTLS_PRIVATE(have_private_key); /*!< Whether the context contains a private key.
149 Boolean values only. */
150} mbedtls_lms_private_t;
Raef Colesab4f8742022-09-01 12:24:31 +0100151#endif /* MBEDTLS_LMS_PRIVATE */
Raef Coles01c71a12022-08-31 15:55:00 +0100152
Raef Coles8ff6df52021-07-21 12:42:15 +0100153/**
Raef Coles01c71a12022-08-31 15:55:00 +0100154 * \brief This function initializes an LMS public context
Raef Coles8ff6df52021-07-21 12:42:15 +0100155 *
156 * \param ctx The uninitialized LMS context that will then be
157 * initialized.
158 */
Raef Coles01c71a12022-08-31 15:55:00 +0100159void mbedtls_lms_init_public( mbedtls_lms_public_t *ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100160
161/**
Raef Coles01c71a12022-08-31 15:55:00 +0100162 * \brief This function uninitializes an LMS public context
Raef Coles8ff6df52021-07-21 12:42:15 +0100163 *
164 * \param ctx The initialized LMS context that will then be
165 * uninitialized.
166 */
Raef Coles01c71a12022-08-31 15:55:00 +0100167void mbedtls_lms_free_public( mbedtls_lms_public_t *ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100168
169/**
Raef Coles01c71a12022-08-31 15:55:00 +0100170 * \brief This function imports an LMS public key into a
171 * public LMS context.
Raef Coles8ff6df52021-07-21 12:42:15 +0100172 *
Raef Coles01c71a12022-08-31 15:55:00 +0100173 * \note Before this function is called, the context must
174 * have been initialized.
Raef Coles8ff6df52021-07-21 12:42:15 +0100175 *
Raef Coles01c71a12022-08-31 15:55:00 +0100176 * \note See IETF RFC8554 for details of the encoding of
177 * this public key.
178 *
179 * \param ctx The initialized LMS context store the key in.
180 * \param key The buffer from which the key will be read.
181 * #MBEDTLS_LMS_PUBLIC_KEY_LEN bytes will be read from
182 * this.
183 * \param key_size The size of the key being imported.
184 *
185 * \return \c 0 on success.
186 * \return A non-zero error code on failure.
Raef Coles8ff6df52021-07-21 12:42:15 +0100187 */
Raef Coles01c71a12022-08-31 15:55:00 +0100188int mbedtls_lms_import_public_key( mbedtls_lms_public_t *ctx,
189 const unsigned char *key, size_t key_size );
190
191/**
192 * \brief This function verifies a LMS signature, using a
193 * LMS context that contains a public key.
194 *
195 * \note Before this function is called, the context must
196 * have been initialized and must contain a public key
197 * (either by import or generation).
198 *
199 * \param ctx The initialized LMS public context from which the
200 * public key will be read.
201 * \param msg The buffer from which the message will be read.
202 * \param msg_size The size of the message that will be read.
203 * \param sig The buf from which the signature will be read.
204 * #MBEDTLS_LMS_SIG_LEN bytes will be read from
205 * this.
206 * \param sig_size The size of the signature to be verified.
207 *
208 * \return \c 0 on successful verification.
209 * \return A non-zero error code on failure.
210 */
211int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx,
212 const unsigned char *msg, size_t msg_size,
213 const unsigned char *sig, size_t sig_size );
214
Raef Colesab4f8742022-09-01 12:24:31 +0100215#ifdef MBEDTLS_LMS_PRIVATE
Raef Coles01c71a12022-08-31 15:55:00 +0100216/**
217 * \brief This function initializes an LMS private context
218 *
219 * \param ctx The uninitialized LMS private context that will
220 * then be initialized. */
221void mbedtls_lms_init_private( mbedtls_lms_private_t *ctx );
222
223/**
224 * \brief This function uninitializes an LMS private context
225 *
226 * \param ctx The initialized LMS private context that will then
227 * be uninitialized.
228 */
229void mbedtls_lms_free_private( mbedtls_lms_private_t *ctx );
230
231/**
232 * \brief This function generates an LMS private key, and
233 * stores in into an LMS private context.
234 *
235 * \warning This function is **not intended for use in
236 * production**, due to as-yet unsolved problems with
237 * handling stateful keys.
238 *
239 * \note The seed must have at least 256 bits of entropy.
240 *
241 * \param ctx The initialized LMOTS context to generate the key
242 * into.
243 * \param type The LMS parameter set identifier.
244 * \param otstype The LMOTS parameter set identifier.
245 * \param f_rng The RNG function to be used to generate the key ID.
246 * \param p_rng The RNG context to be passed to f_rng
247 * \param seed The seed used to deterministically generate the
248 * key.
249 * \param seed_size The length of the seed.
250 *
251 * \return \c 0 on success.
252 * \return A non-zero error code on failure.
253 */
254int mbedtls_lms_generate_private_key( mbedtls_lms_private_t *ctx,
255 mbedtls_lms_algorithm_type_t type,
256 mbedtls_lmots_algorithm_type_t otstype,
257 int (*f_rng)(void *, unsigned char *, size_t),
258 void* p_rng, unsigned char *seed,
259 size_t seed_size );
260
261/**
262 * \brief This function generates an LMS public key from a
263 * LMS context that already contains a private key.
264 *
265 * \note Before this function is called, the context must
266 * have been initialized and the context must contain
267 * a private key.
268 *
269 * \param ctx The initialized LMS public context to generate the key
270 * from and store it into.
271 *
272 * \param ctx The LMS private context to read the private key
273 * from. This must have been initialized and contain a
274 * private key.
275 *
276 * \return \c 0 on success.
277 * \return A non-zero error code on failure.
278 */
279int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx,
280 mbedtls_lms_private_t *priv_ctx );
281
282/**
283 * \brief This function exports an LMS public key from a
284 * LMS public context that already contains a public
285 * key.
286 *
287 * \note Before this function is called, the context must
288 * have been initialized and the context must contain
289 * a public key.
290 *
291 * \note See IETF RFC8554 for details of the encoding of
292 * this public key.
293 *
294 * \param ctx The initialized LMS public context that contains
295 * the public key.
296 * \param key The buffer into which the key will be output. Must
297 * be at least #MBEDTLS_LMS_PUBLIC_KEY_LEN in size.
298 * \param key_size The size of the key buffer.
299 * \param key_len If not NULL, will be written with the size of the
300 * key.
301 *
302 * \return \c 0 on success.
303 * \return A non-zero error code on failure.
304 */
305int mbedtls_lms_export_public_key( mbedtls_lms_public_t *ctx, unsigned char *key,
306 size_t key_size, size_t *key_len );
Raef Coles8ff6df52021-07-21 12:42:15 +0100307
308/**
309 * \brief This function creates a LMS signature, using a
Raef Coles01c71a12022-08-31 15:55:00 +0100310 * LMS context that contains unused private keys.
Raef Coles8ff6df52021-07-21 12:42:15 +0100311 *
Raef Coles2ad6e612022-08-24 13:33:35 +0100312 * \warning This function is **not intended for use in
313 * production**, due to as-yet unsolved problems with
314 * handling stateful keys.
Raef Coles0aa18e02022-06-15 13:05:56 +0100315 *
Raef Coles8ff6df52021-07-21 12:42:15 +0100316 * \note Before this function is called, the context must
317 * have been initialized and must contain a private
318 * key.
319 *
320 * \note Each of the LMOTS private keys inside a LMS private
321 * key can only be used once. If they are reused, then
322 * attackers may be able to forge signatures with that
323 * key. This is all handled transparently, but it is
324 * important to not perform copy operations on LMS
325 * contexts that contain private key material.
326 *
Raef Coles01c71a12022-08-31 15:55:00 +0100327 * \param ctx The initialized LMS private context from which the
Raef Coles8ff6df52021-07-21 12:42:15 +0100328 * private key will be read.
329 * \param f_rng The RNG function to be used for signature
330 * generation.
331 * \param p_rng The RNG context to be passed to f_rng
332 * \param msg The buffer from which the message will be read.
Raef Coles01c71a12022-08-31 15:55:00 +0100333 * \param msg_size The size of the message that will be read.
Raef Coles8ff6df52021-07-21 12:42:15 +0100334 * \param sig The buf into which the signature will be stored.
Raef Coles01c71a12022-08-31 15:55:00 +0100335 * Must be at least #MBEDTLS_LMS_SIG_LEN in size.
336 * \param sig_size The size of the buffer the signature will be
337 * written into.
338 * \param sig_len If not NULL, will be written with the size of the
339 * signature.
Raef Coles8ff6df52021-07-21 12:42:15 +0100340 *
341 * \return \c 0 on success.
342 * \return A non-zero error code on failure.
343 */
Raef Coles01c71a12022-08-31 15:55:00 +0100344int mbedtls_lms_sign( mbedtls_lms_private_t *ctx,
Raef Coles8ff6df52021-07-21 12:42:15 +0100345 int (*f_rng)(void *, unsigned char *, size_t),
Raef Coles01c71a12022-08-31 15:55:00 +0100346 void* p_rng, unsigned char *msg, unsigned int msg_size,
347 unsigned char *sig, size_t sig_size, size_t *sig_len);
Raef Colesab4f8742022-09-01 12:24:31 +0100348#endif /* MBEDTLS_LMS_PRIVATE */
Raef Coles8ff6df52021-07-21 12:42:15 +0100349
350#ifdef __cplusplus
351}
352#endif
353
354#endif /* MBEDTLS_LMS_H */