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