blob: 8430309d466ce998d65b7ddebe1d88a72614a662 [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 Coles8ff6df52021-07-21 12:42:15 +010035#include "mbedtls/private_access.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 */
38#define MBEDTLS_ERR_LMS_OUT_OF_PRIV_KEYS -0x0013 /**< Specified LMS key has utilised all of its private keys */
39#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 Coles0aa18e02022-06-15 13:05:56 +010043#define MBEDTLS_LMS_TYPE_LEN (4)
Raef Coles8ff6df52021-07-21 12:42:15 +010044#define MBEDTLS_LMS_H_TREE_HEIGHT (10)
Raef Coles2ad6e612022-08-24 13:33:35 +010045#define MBEDTLS_LMS_M_NODE_BYTES (32) /* The length of a hash output, 32 for SHA256 */
Raef Coles8ff6df52021-07-21 12:42:15 +010046
47#define MBEDTLS_LMS_SIG_LEN (MBEDTLS_LMOTS_Q_LEAF_ID_LEN + MBEDTLS_LMOTS_SIG_LEN + \
48 MBEDTLS_LMS_TYPE_LEN + MBEDTLS_LMS_H_TREE_HEIGHT * MBEDTLS_LMS_M_NODE_BYTES)
49
50#define MBEDTLS_LMS_PUBKEY_LEN (MBEDTLS_LMS_TYPE_LEN + MBEDTLS_LMOTS_TYPE_LEN + \
51 MBEDTLS_LMOTS_I_KEY_ID_LEN + MBEDTLS_LMS_M_NODE_BYTES)
52
53#define MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET (0)
54#define MBEDTLS_LMS_SIG_OTS_SIG_OFFSET (MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET + MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
55#define MBEDTLS_LMS_SIG_TYPE_OFFSET (MBEDTLS_LMS_SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_LEN)
56#define MBEDTLS_LMS_SIG_PATH_OFFSET (MBEDTLS_LMS_SIG_TYPE_OFFSET + MBEDTLS_LMS_TYPE_LEN)
57
58#define MBEDTLS_LMS_PUBKEY_TYPE_OFFSET (0)
59#define MBEDTLS_LMS_PUBKEY_OTSTYPE_OFFSET (MBEDTLS_LMS_PUBKEY_TYPE_OFFSET + MBEDTLS_LMS_TYPE_LEN)
60#define MBEDTLS_LMS_PUBKEY_I_KEY_ID_OFFSET (MBEDTLS_LMS_PUBKEY_OTSTYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN)
61#define MBEDTLS_LMS_PUBKEY_ROOT_NODE_OFFSET (MBEDTLS_LMS_PUBKEY_I_KEY_ID_OFFSET + MBEDTLS_LMOTS_I_KEY_ID_LEN)
62
63#ifdef __cplusplus
64extern "C" {
65#endif
66
Raef Colesc4647462022-06-15 12:17:51 +010067/* https://www.iana.org/assignments/leighton-micali-signatures/leighton-micali-signatures.xhtml
68 * We are only implementing a subset of the types, particularly H10, for the sake of simplicty.
69 */
Raef Coles8ff6df52021-07-21 12:42:15 +010070typedef enum {
71 MBEDTLS_LMS_SHA256_M32_H10 = 0x6,
72} mbedtls_lms_algorithm_type_t;
73
74
Raef Coles2ad6e612022-08-24 13:33:35 +010075/** LMS context structure.
76 *
77 * The context must be initialized before it is used. A public key must either
78 * be imported, or an algorithm type set, a private key generated and the public
79 * key calculated from it. A context that does not contain a public key cannot
80 * verify, and a context that does not contain a private key cannot sign.
81 *
82 * \dot
83 * digraph lmots {
84 * UNINITIALIZED -> INIT [label="init"];
85 * TYPE_SET -> INIT [label="free"];
86 * PRIVATE -> INIT [label="free"];
87 * PUBLIC -> INIT [label="free"];
88 * "PRIVATE+PUBLIC" -> INIT [label="free"];
89 * INIT -> TYPE_SET [label="set_algorithm_type"];
90 * INIT -> PUBLIC [label="import_public"];
91 * PUBLIC -> PUBLIC [label="export_pubkey"];
92 * "PRIVATE+PUBLIC" -> "PRIVATE+PUBLIC" [label="export_pubkey"];
93 * PRIVATE -> "PRIVATE+PUBLIC" [label="gen_pubkey"];
94 * TYPE_SET -> PRIVATE [label="gen_privkey"];
95 * }
96 * \enddot
97 */
Raef Coles8ff6df52021-07-21 12:42:15 +010098typedef struct {
Raef Colesc4647462022-06-15 12:17:51 +010099 unsigned char MBEDTLS_PRIVATE(have_privkey); /*!< Whether the context contains a private key.
100 Boolean values only. */
101 unsigned char MBEDTLS_PRIVATE(have_pubkey); /*!< Whether the context contains a public key.
102 Boolean values only. */
103 unsigned char MBEDTLS_PRIVATE(I_key_identifier)[MBEDTLS_LMOTS_I_KEY_ID_LEN]; /*!< The key
104 identifier. */
105 mbedtls_lms_algorithm_type_t MBEDTLS_PRIVATE(type); /*!< The LMS key type identifier as per
106 IANA. Only SHA256_M32_H10 is currently
107 supported. */
108 mbedtls_lmots_algorithm_type_t MBEDTLS_PRIVATE(otstype); /*!< The LM-OTS key type identifier as
109 per IANA. Only SHA256_N32_W8 is currently
110 supported. */
111 unsigned int MBEDTLS_PRIVATE(q_next_usable_key); /*!< The index of the next OTS key that has not
112 been used. */
113 mbedtls_lmots_context *MBEDTLS_PRIVATE(priv_keys); /*!< The private key material. One OTS key
114 for each leaf node in the merkle tree. */
115 unsigned char MBEDTLS_PRIVATE(T_1_pub_key)[MBEDTLS_LMS_M_NODE_BYTES]; /*!< The public key, in
116 the form of the merkle tree root node. */
Raef Coles8ff6df52021-07-21 12:42:15 +0100117} mbedtls_lms_context;
118
119
120/**
121 * \brief This function initializes an LMS context
122 *
123 * \param ctx The uninitialized LMS context that will then be
124 * initialized.
125 */
126void mbedtls_lms_init( mbedtls_lms_context *ctx );
127
128/**
129 * \brief This function uninitializes an LMS context
130 *
131 * \param ctx The initialized LMS context that will then be
132 * uninitialized.
133 */
134void mbedtls_lms_free( mbedtls_lms_context *ctx );
135
136/**
137 * \brief This function sets the type of an LMS context
138 *
139 * \note The parameter set in the context will then be used
140 * for keygen operations etc.
141 *
142 * \param ctx The initialized LMS context.
143 * \param type The type that will be set in the context.
144 * \param otstype The type of the LMOTS implementation used by this
145 * context.
146 */
147int mbedtls_lms_set_algorithm_type( mbedtls_lms_context *ctx,
148 mbedtls_lms_algorithm_type_t type,
149 mbedtls_lmots_algorithm_type_t otstype);
150
151/**
152 * \brief This function creates a LMS signature, using a
153 * LMOTS context that contains a private key.
154 *
Raef Coles2ad6e612022-08-24 13:33:35 +0100155 * \warning This function is **not intended for use in
156 * production**, due to as-yet unsolved problems with
157 * handling stateful keys.
Raef Coles0aa18e02022-06-15 13:05:56 +0100158 *
Raef Coles8ff6df52021-07-21 12:42:15 +0100159 * \note Before this function is called, the context must
160 * have been initialized and must contain a private
161 * key.
162 *
163 * \note Each of the LMOTS private keys inside a LMS private
164 * key can only be used once. If they are reused, then
165 * attackers may be able to forge signatures with that
166 * key. This is all handled transparently, but it is
167 * important to not perform copy operations on LMS
168 * contexts that contain private key material.
169 *
170 * \param ctx The initialized LMS context from which the
171 * private key will be read.
172 * \param f_rng The RNG function to be used for signature
173 * generation.
174 * \param p_rng The RNG context to be passed to f_rng
175 * \param msg The buffer from which the message will be read.
176 * \param msg_len The size of the message that will be read.
177 * \param sig The buf into which the signature will be stored.
178 * Must be at least #MBEDTLS_LMOTS_SIG_LEN in size.
179 *
180 * \return \c 0 on success.
181 * \return A non-zero error code on failure.
182 */
183int mbedtls_lms_sign( mbedtls_lms_context *ctx,
184 int (*f_rng)(void *, unsigned char *, size_t),
185 void* p_rng, unsigned char *msg, unsigned int msg_len,
186 unsigned char *sig);
187
188/**
189 * \brief This function verifies a LMS signature, using a
190 * LMS context that contains a public key.
191 *
192 * \note Before this function is called, the context must
193 * have been initialized and must contain a public key
194 * (either by import or generation).
195 *
196 * \param ctx The initialized LMS context from which the public
197 * key will be read.
198 * \param msg The buffer from which the message will be read.
199 * \param msg_len The size of the message that will be read.
200 * \param sig The buf from which the signature will be read.
201 * #MBEDTLS_LMS_SIG_LEN bytes will be read from
202 * this.
203 *
204 * \return \c 0 on successful verification.
205 * \return A non-zero error code on failure.
206 */
207int mbedtls_lms_verify( const mbedtls_lms_context *ctx,
208 const unsigned char *msg, unsigned int msg_len,
209 const unsigned char *sig );
210
211/**
212 * \brief This function imports an LMOTS public key into a
213 * LMS context.
214 *
215 * \note Before this function is called, the context must
216 * have been initialized.
217 *
218 * \note See IETF RFC8554 for details of the encoding of
219 * this public key.
220 *
221 * \param ctx The initialized LMS context store the key in.
222 * \param key The buffer from which the key will be read.
223 * #MBEDTLS_LMS_PUBKEY_LEN bytes will be read from
224 * this.
225 *
226 * \return \c 0 on success.
227 * \return A non-zero error code on failure.
228 */
229int mbedtls_lms_import_pubkey( mbedtls_lms_context *ctx,
230 const unsigned char *key );
231
232/**
233 * \brief This function exports an LMOTS public key from a
234 * LMS context that already contains a public key.
235 *
236 * \note Before this function is called, the context must
237 * have been initialized and the context must contain
238 * a public key.
239 *
240 * \note See IETF RFC8554 for details of the encoding of
241 * this public key.
242 *
243 * \param ctx The initialized LMS context that contains the
244 * publc key.
245 * \param key The buffer into which the key will be output. Must
246 * be at least #MBEDTLS_LMS_PUBKEY_LEN in size.
247 *
248 * \return \c 0 on success.
249 * \return A non-zero error code on failure.
250 */
251int mbedtls_lms_export_pubkey( mbedtls_lms_context *ctx,
252 unsigned char *key );
253
254/**
255 * \brief This function generates an LMS public key from a
256 * LMS context that already contains a private key.
257 *
258 * \note Before this function is called, the context must
259 * have been initialized and the context must contain
260 * a private key.
261 *
262 * \param ctx The initialized LMS context to generate the key
263 * from and store it into.
264 *
265 * \return \c 0 on success.
266 * \return A non-zero error code on failure.
267 */
268int mbedtls_lms_gen_pubkey( mbedtls_lms_context *ctx );
269
270/**
271 * \brief This function generates an LMS private key, and
272 * stores in into an LMS context.
273 *
Raef Coles2ad6e612022-08-24 13:33:35 +0100274 * \warning This function is **not intended for use in
275 * production**, due to as-yet unsolved problems with
276 * handling stateful keys.
277 *
Raef Coles8ff6df52021-07-21 12:42:15 +0100278 * \note Before this function is called, the context must
279 * have been initialized and the type of the LMS
280 * context set using mbedtls_lmots_set_algorithm_type
281 *
282 * \note The seed must have at least 256 bits of entropy.
283 *
284 * \param ctx The initialized LMOTS context to generate the key
285 * into.
286 * \param f_rng The RNG function to be used to generate the key ID.
287 * \param p_rng The RNG context to be passed to f_rng
288 * \param seed The seed used to deterministically generate the
289 * key.
290 * \param seed_len The length of the seed.
291 *
292 * \return \c 0 on success.
293 * \return A non-zero error code on failure.
294 */
295int mbedtls_lms_gen_privkey( mbedtls_lms_context *ctx,
296 int (*f_rng)(void *, unsigned char *, size_t),
297 void* p_rng, unsigned char *seed,
298 size_t seed_len );
299
300#ifdef __cplusplus
301}
302#endif
303
304#endif /* MBEDTLS_LMS_H */