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