blob: ec68967bea6609cda466890d78136d7da975e35d [file] [log] [blame]
Raef Coles8ff6df52021-07-21 12:42:15 +01001/**
2 * \file lmots.h
3 *
4 * \brief This file provides an API for the LM-OTS post-quantum-safe one-time
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_LMOTS_SHA256_N32_W8 in order to reduce complexity.
Raef Coles8ff6df52021-07-21 12:42:15 +01008 */
9/*
10 * Copyright The Mbed TLS Contributors
11 * SPDX-License-Identifier: Apache-2.0
12 *
13 * Licensed under the Apache License, Version 2.0 (the "License"); you may
14 * not use this file except in compliance with the License.
15 * You may obtain a copy of the License at
16 *
17 * http://www.apache.org/licenses/LICENSE-2.0
18 *
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
21 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
24 */
25
26#ifndef MBEDTLS_LMOTS_H
27#define MBEDTLS_LMOTS_H
28
29#include "mbedtls/private_access.h"
30
Raef Colesc8f96042022-08-25 13:49:54 +010031#include "psa/crypto.h"
32
Raef Coles8ff6df52021-07-21 12:42:15 +010033#include <stdint.h>
34#include <stddef.h>
35
Raef Coles8ff6df52021-07-21 12:42:15 +010036#define MBEDTLS_LMOTS_N_HASH_LEN (32)
37#define MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN (34)
38#define MBEDTLS_LMOTS_TYPE_LEN (4)
39#define MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN (MBEDTLS_LMOTS_N_HASH_LEN)
40#define MBEDTLS_LMOTS_I_KEY_ID_LEN (16)
41#define MBEDTLS_LMOTS_Q_LEAF_ID_LEN (4)
42
43#define MBEDTLS_LMOTS_SIG_LEN (MBEDTLS_LMOTS_TYPE_LEN + MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN + \
44 (MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN * MBEDTLS_LMOTS_N_HASH_LEN))
45
46#define MBEDTLS_LMOTS_PUBKEY_LEN (MBEDTLS_LMOTS_TYPE_LEN + MBEDTLS_LMOTS_I_KEY_ID_LEN + \
47 MBEDTLS_LMOTS_Q_LEAF_ID_LEN + MBEDTLS_LMOTS_N_HASH_LEN)
48
49#define MBEDTLS_LMOTS_SIG_TYPE_OFFSET (0)
50#define MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET (MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN)
51#define MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET (MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET + MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN)
52
53#define MBEDTLS_LMOTS_PUBKEY_TYPE_OFFSET (0)
54#define MBEDTLS_LMOTS_PUBKEY_I_KEY_ID_OFFSET (MBEDTLS_LMOTS_PUBKEY_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN)
55#define MBEDTLS_LMOTS_PUBKEY_Q_LEAF_ID_OFFSET (MBEDTLS_LMOTS_PUBKEY_I_KEY_ID_OFFSET + MBEDTLS_LMOTS_I_KEY_ID_LEN)
56#define MBEDTLS_LMOTS_PUBKEY_KEY_HASH_OFFSET (MBEDTLS_LMOTS_PUBKEY_Q_LEAF_ID_OFFSET + MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
57
58
59#ifdef __cplusplus
60extern "C" {
61#endif
62
Raef Colesc4647462022-06-15 12:17:51 +010063/* https://www.iana.org/assignments/leighton-micali-signatures/leighton-micali-signatures.xhtml
64 * We are only implementing a subset of the types, particularly n32_w8, for the sake of simplicty.
Raef Coles8ff6df52021-07-21 12:42:15 +010065 */
66typedef enum {
67 MBEDTLS_LMOTS_SHA256_N32_W8 = 4
68} mbedtls_lmots_algorithm_type_t;
69
70
Raef Coles2ad6e612022-08-24 13:33:35 +010071/** LMOTS context structure.
72 *
73 * The context must be initialized before it is used. A public key must either
74 * be imported, or an algorithm type set, a private key generated and the public
75 * key calculated from it. A context that does not contain a public key cannot
76 * verify, and a context that does not contain a private key cannot sign.
77 * Signing a message will remove the private key from the context, as private
78 * keys can only be used a single time.
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 * PRIVATE -> TYPE_SET [label="sign"];
89 * "PRIVATE+PUBLIC" -> PUBLIC [label="sign"];
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 unsigned int MBEDTLS_PRIVATE(q_leaf_identifier); /*!< Which leaf of the LMS key this is.
106 0 if the key is not part of an LMS key. */
107 unsigned char MBEDTLS_PRIVATE(q_leaf_identifier_bytes)[MBEDTLS_LMOTS_Q_LEAF_ID_LEN];/*!< The
108 leaf identifier in network bytes form. */
109 mbedtls_lmots_algorithm_type_t MBEDTLS_PRIVATE(type); /*!< The LM-OTS key type identifier as
110 per IANA. Only SHA256_N32_W8 is currently
111 supported. */
112 unsigned char MBEDTLS_PRIVATE(priv_key[MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN][32]); /*!< The private
113 key, one hash output per byte of the encoded
114 symbol string P (32 bytes of hash output +
115 2 bytes of checksum). */
116 unsigned char MBEDTLS_PRIVATE(pub_key[32]); /*!< The public key, in the form of a SHA256
117 output. */
Raef Coles8ff6df52021-07-21 12:42:15 +0100118} mbedtls_lmots_context;
119
Raef Colesc8f96042022-08-25 13:49:54 +0100120/**
121 * \brief This function converts a \ref psa_status_t to a
122 * low-level LMS error code.
123 *
124 * \param status The psa_status_t to convert
125 *
126 * \return The corresponding LMS error code.
127 */
128int mbedtls_lms_error_from_psa(psa_status_t status);
129
Raef Coles8ff6df52021-07-21 12:42:15 +0100130
131/**
132 * \brief This function initializes an LMOTS context
133 *
134 * \param ctx The uninitialized LMOTS context that will then be
135 * initialized.
136 */
137void mbedtls_lmots_init( mbedtls_lmots_context *ctx );
138
139/**
140 * \brief This function uninitializes an LMOTS context
141 *
142 * \param ctx The initialized LMOTS context that will then be
143 * uninitialized.
144 */
145void mbedtls_lmots_free( mbedtls_lmots_context *ctx );
146
147/**
148 * \brief This function sets the type of an LMOTS context
149 *
150 * \note The parameter set in the context will then be used
151 * for keygen operations etc.
152 *
153 * \param ctx The initialized LMOTS context.
154 * \param type The type that will be set in the context.
Raef Colesc8f96042022-08-25 13:49:54 +0100155 *
156 * \return \c 0 on success.
157 * \return A non-zero error code on failure.
Raef Coles8ff6df52021-07-21 12:42:15 +0100158 */
159int mbedtls_lmots_set_algorithm_type( mbedtls_lmots_context *ctx,
160 mbedtls_lmots_algorithm_type_t type );
161
162/**
163 * \brief This function creates a candidate public key from
164 * an LMOTS signature. This can then be compared to
165 * the real public key to determine the validity of
166 * the signature.
167 *
168 * \note This function is exposed publicly to be used in LMS
169 * signature verification, it is expected that
170 * mbedtls_lmots_verify will be used for LMOTS
171 * signature verification.
172 *
Raef Coles2ad6e612022-08-24 13:33:35 +0100173 * \param I_key_identifier The key identifier of the key, as a 16-byte string.
Raef Coles8ff6df52021-07-21 12:42:15 +0100174 * \param q_leaf_identifier The leaf identifier of key. If this LMOTS key is
175 * not being used as part of an LMS key, this should
176 * be set to 0.
177 * \param msg The buffer from which the message will be read.
178 * \param msg_len The size of the message that will be read.
Raef Coles2ad6e612022-08-24 13:33:35 +0100179 * \param sig The buffer from which the signature will be read.
180 * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from this.
Raef Coles8ff6df52021-07-21 12:42:15 +0100181 * \param out The buffer where the candidate public key will be
182 * stored. Must be at least #MBEDTLS_LMOTS_N_HASH_LEN
183 * bytes in size.
184 *
185 * \return \c 0 on success.
186 * \return A non-zero error code on failure.
187 */
188int mbedtls_lmots_generate_pub_key_candidate( const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
189 const unsigned char q_leaf_identifier[MBEDTLS_LMOTS_Q_LEAF_ID_LEN],
190 const unsigned char *msg,
191 size_t msg_len,
192 const unsigned char *sig,
193 unsigned char *out );
194
195/**
196 * \brief This function creates a LMOTS signature, using a
197 * LMOTS context that contains a private key.
198 *
199 * \note Before this function is called, the context must
200 * have been initialized and must contain a private
201 * key.
202 *
203 * \note LMOTS private keys can only be used once, otherwise
204 * attackers may be able to create forged signatures.
205 * If the signing operation is successful, the private
206 * key in the context will be erased, and no further
207 * signing will be possible until another private key
208 * is loaded
209 *
210 * \param ctx The initialized LMOTS context from which the
211 * private key will be read.
212 * \param f_rng The RNG function to be used for signature
213 * generation.
214 * \param p_rng The RNG context to be passed to f_rng
215 * \param msg The buffer from which the message will be read.
216 * \param msg_len The size of the message that will be read.
217 * \param sig The buf into which the signature will be stored.
218 * Must be at least #MBEDTLS_LMOTS_SIG_LEN in size.
219 *
220 * \return \c 0 on success.
221 * \return A non-zero error code on failure.
222 */
223int mbedtls_lmots_sign( mbedtls_lmots_context *ctx,
224 int (*f_rng)(void *, unsigned char *, size_t),
225 void *p_rng, const unsigned char *msg, size_t msg_len,
226 unsigned char *sig );
227
228/**
229 * \brief This function verifies a LMOTS signature, using a
230 * LMOTS context that contains a public key.
231 *
Raef Coles2ad6e612022-08-24 13:33:35 +0100232 * \warning This function is **not intended for use in
233 * production**, due to as-yet unsolved problems with
234 * handling stateful keys.
235 *
Raef Coles8ff6df52021-07-21 12:42:15 +0100236 * \note Before this function is called, the context must
237 * have been initialized and must contain a public key
238 * (either by import or generation).
239 *
240 * \param ctx The initialized LMOTS context from which the public
241 * key will be read.
242 * \param msg The buffer from which the message will be read.
243 * \param msg_len The size of the message that will be read.
244 * \param sig The buf from which the signature will be read.
245 * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from
246 * this.
247 *
248 * \return \c 0 on successful verification.
249 * \return A non-zero error code on failure.
250 */
251int mbedtls_lmots_verify( mbedtls_lmots_context *ctx, const unsigned char *msg,
252 size_t msg_len, const unsigned char *sig );
253
254/**
255 * \brief This function imports an LMOTS public key into a
256 * LMOTS context.
257 *
258 * \note Before this function is called, the context must
259 * have been initialized.
260 *
261 * \note See IETF RFC8554 for details of the encoding of
262 * this public key.
263 *
264 * \param ctx The initialized LMOTS context store the key in.
265 * \param key The buffer from which the key will be read.
266 * #MBEDTLS_LMOTS_PUBKEY_LEN bytes will be read from
267 * this.
268 *
269 * \return \c 0 on success.
270 * \return A non-zero error code on failure.
271 */
272int mbedtls_lmots_import_pubkey( mbedtls_lmots_context *ctx,
273 const unsigned char *key );
274
275/**
276 * \brief This function exports an LMOTS public key from a
277 * LMOTS context that already contains a public key.
278 *
279 * \note Before this function is called, the context must
280 * have been initialized and the context must contain
281 * a public key.
282 *
283 * \note See IETF RFC8554 for details of the encoding of
284 * this public key.
285 *
286 * \param ctx The initialized LMOTS context that contains the
287 * publc key.
288 * \param key The buffer into which the key will be output. Must
289 * be at least #MBEDTLS_LMOTS_PUBKEY_LEN in size.
290 *
291 * \return \c 0 on success.
292 * \return A non-zero error code on failure.
293 */
294int mbedtls_lmots_export_pubkey( mbedtls_lmots_context *ctx,
295 unsigned char *key );
296
297/**
298 * \brief This function generates an LMOTS public key from a
299 * LMOTS context that already contains a private key.
300 *
301 * \note Before this function is called, the context must
302 * have been initialized and the context must contain
303 * a private key.
304 *
305 * \param ctx The initialized LMOTS context to generate the key
306 * from and store it into.
307 *
308 * \return \c 0 on success.
309 * \return A non-zero error code on failure.
310 */
311int mbedtls_lmots_gen_pubkey( mbedtls_lmots_context *ctx );
312
313/**
314 * \brief This function generates an LMOTS private key, and
315 * stores in into an LMOTS context.
316 *
Raef Coles2ad6e612022-08-24 13:33:35 +0100317 * \warning This function is **not intended for use in
318 * production**, due to as-yet unsolved problems with
319 * handling stateful keys.
320 *
Raef Coles8ff6df52021-07-21 12:42:15 +0100321 * \note Before this function is called, the context must
322 * have been initialized and the type of the LMOTS
323 * context set using mbedtls_lmots_set_algorithm_type
324 *
325 * \note The seed must have at least 256 bits of entropy.
326 *
327 * \param ctx The initialized LMOTS context to generate the key
328 * into.
Raef Coles2ad6e612022-08-24 13:33:35 +0100329 * \param I_key_identifier The key identifier of the key, as a 16-byte string.
Raef Coles8ff6df52021-07-21 12:42:15 +0100330 * \param q_leaf_identifier The leaf identifier of key. If this LMOTS key is
331 * not being used as part of an LMS key, this should
332 * be set to 0.
333 * \param seed The seed used to deterministically generate the
334 * key.
335 * \param seed_len The length of the seed.
336 *
337 * \return \c 0 on success.
338 * \return A non-zero error code on failure.
339 */
340int mbedtls_lmots_gen_privkey( mbedtls_lmots_context *ctx,
341 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
342 unsigned int q_leaf_identifier,
343 const unsigned char *seed,
344 size_t seed_len );
345
346#ifdef __cplusplus
347}
348#endif
349
350#endif /* MBEDTLS_LMOTS_H */