blob: d89f5bb1c0750f62594f4b47bc0ee3ed78a90947 [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
31#include <stdint.h>
32#include <stddef.h>
33
34#define MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA -0x0076 /**< Bad data has been input to an LMOTS function */
35#define MBEDTLS_ERR_LMOTS_VERIFY_FAILED -0x0078 /**< LMOTS signature verification failed */
36
37#define MBEDTLS_LMOTS_N_HASH_LEN (32)
38#define MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN (34)
39#define MBEDTLS_LMOTS_TYPE_LEN (4)
40#define MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN (MBEDTLS_LMOTS_N_HASH_LEN)
41#define MBEDTLS_LMOTS_I_KEY_ID_LEN (16)
42#define MBEDTLS_LMOTS_Q_LEAF_ID_LEN (4)
43
44#define MBEDTLS_LMOTS_SIG_LEN (MBEDTLS_LMOTS_TYPE_LEN + MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN + \
45 (MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN * MBEDTLS_LMOTS_N_HASH_LEN))
46
47#define MBEDTLS_LMOTS_PUBKEY_LEN (MBEDTLS_LMOTS_TYPE_LEN + MBEDTLS_LMOTS_I_KEY_ID_LEN + \
48 MBEDTLS_LMOTS_Q_LEAF_ID_LEN + MBEDTLS_LMOTS_N_HASH_LEN)
49
50#define MBEDTLS_LMOTS_SIG_TYPE_OFFSET (0)
51#define MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET (MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN)
52#define MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET (MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET + MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN)
53
54#define MBEDTLS_LMOTS_PUBKEY_TYPE_OFFSET (0)
55#define MBEDTLS_LMOTS_PUBKEY_I_KEY_ID_OFFSET (MBEDTLS_LMOTS_PUBKEY_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN)
56#define MBEDTLS_LMOTS_PUBKEY_Q_LEAF_ID_OFFSET (MBEDTLS_LMOTS_PUBKEY_I_KEY_ID_OFFSET + MBEDTLS_LMOTS_I_KEY_ID_LEN)
57#define MBEDTLS_LMOTS_PUBKEY_KEY_HASH_OFFSET (MBEDTLS_LMOTS_PUBKEY_Q_LEAF_ID_OFFSET + MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
58
59
60#ifdef __cplusplus
61extern "C" {
62#endif
63
Raef Colesc4647462022-06-15 12:17:51 +010064/* https://www.iana.org/assignments/leighton-micali-signatures/leighton-micali-signatures.xhtml
65 * We are only implementing a subset of the types, particularly n32_w8, for the sake of simplicty.
Raef Coles8ff6df52021-07-21 12:42:15 +010066 */
67typedef enum {
68 MBEDTLS_LMOTS_SHA256_N32_W8 = 4
69} mbedtls_lmots_algorithm_type_t;
70
71
Raef Coles2ad6e612022-08-24 13:33:35 +010072/** LMOTS context structure.
73 *
74 * The context must be initialized before it is used. A public key must either
75 * be imported, or an algorithm type set, a private key generated and the public
76 * key calculated from it. A context that does not contain a public key cannot
77 * verify, and a context that does not contain a private key cannot sign.
78 * Signing a message will remove the private key from the context, as private
79 * keys can only be used a single time.
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 * PRIVATE -> TYPE_SET [label="sign"];
90 * "PRIVATE+PUBLIC" -> PUBLIC [label="sign"];
91 * INIT -> PUBLIC [label="import_public"];
92 * PUBLIC -> PUBLIC [label="export_pubkey"];
93 * "PRIVATE+PUBLIC" -> "PRIVATE+PUBLIC" [label="export_pubkey"];
94 * PRIVATE -> "PRIVATE+PUBLIC" [label="gen_pubkey"];
95 * TYPE_SET -> PRIVATE [label="gen_privkey"];
96 * }
97 * \enddot
98 */
Raef Coles8ff6df52021-07-21 12:42:15 +010099typedef struct {
Raef Colesc4647462022-06-15 12:17:51 +0100100 unsigned char MBEDTLS_PRIVATE(have_privkey); /*!< Whether the context contains a private key.
101 Boolean values only. */
102 unsigned char MBEDTLS_PRIVATE(have_pubkey); /*!< Whether the context contains a public key.
103 Boolean values only. */
104 unsigned char MBEDTLS_PRIVATE(I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN]); /*!< The key
105 identifier. */
106 unsigned int MBEDTLS_PRIVATE(q_leaf_identifier); /*!< Which leaf of the LMS key this is.
107 0 if the key is not part of an LMS key. */
108 unsigned char MBEDTLS_PRIVATE(q_leaf_identifier_bytes)[MBEDTLS_LMOTS_Q_LEAF_ID_LEN];/*!< The
109 leaf identifier in network bytes form. */
110 mbedtls_lmots_algorithm_type_t MBEDTLS_PRIVATE(type); /*!< The LM-OTS key type identifier as
111 per IANA. Only SHA256_N32_W8 is currently
112 supported. */
113 unsigned char MBEDTLS_PRIVATE(priv_key[MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN][32]); /*!< The private
114 key, one hash output per byte of the encoded
115 symbol string P (32 bytes of hash output +
116 2 bytes of checksum). */
117 unsigned char MBEDTLS_PRIVATE(pub_key[32]); /*!< The public key, in the form of a SHA256
118 output. */
Raef Coles8ff6df52021-07-21 12:42:15 +0100119} mbedtls_lmots_context;
120
121
122/**
123 * \brief This function initializes an LMOTS context
124 *
125 * \param ctx The uninitialized LMOTS context that will then be
126 * initialized.
127 */
128void mbedtls_lmots_init( mbedtls_lmots_context *ctx );
129
130/**
131 * \brief This function uninitializes an LMOTS context
132 *
133 * \param ctx The initialized LMOTS context that will then be
134 * uninitialized.
135 */
136void mbedtls_lmots_free( mbedtls_lmots_context *ctx );
137
138/**
139 * \brief This function sets the type of an LMOTS context
140 *
141 * \note The parameter set in the context will then be used
142 * for keygen operations etc.
143 *
144 * \param ctx The initialized LMOTS context.
145 * \param type The type that will be set in the context.
146 */
147int mbedtls_lmots_set_algorithm_type( mbedtls_lmots_context *ctx,
148 mbedtls_lmots_algorithm_type_t type );
149
150/**
151 * \brief This function creates a candidate public key from
152 * an LMOTS signature. This can then be compared to
153 * the real public key to determine the validity of
154 * the signature.
155 *
156 * \note This function is exposed publicly to be used in LMS
157 * signature verification, it is expected that
158 * mbedtls_lmots_verify will be used for LMOTS
159 * signature verification.
160 *
Raef Coles2ad6e612022-08-24 13:33:35 +0100161 * \param I_key_identifier The key identifier of the key, as a 16-byte string.
Raef Coles8ff6df52021-07-21 12:42:15 +0100162 * \param q_leaf_identifier The leaf identifier of key. If this LMOTS key is
163 * not being used as part of an LMS key, this should
164 * be set to 0.
165 * \param msg The buffer from which the message will be read.
166 * \param msg_len The size of the message that will be read.
Raef Coles2ad6e612022-08-24 13:33:35 +0100167 * \param sig The buffer from which the signature will be read.
168 * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from this.
Raef Coles8ff6df52021-07-21 12:42:15 +0100169 * \param out The buffer where the candidate public key will be
170 * stored. Must be at least #MBEDTLS_LMOTS_N_HASH_LEN
171 * bytes in size.
172 *
173 * \return \c 0 on success.
174 * \return A non-zero error code on failure.
175 */
176int mbedtls_lmots_generate_pub_key_candidate( const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
177 const unsigned char q_leaf_identifier[MBEDTLS_LMOTS_Q_LEAF_ID_LEN],
178 const unsigned char *msg,
179 size_t msg_len,
180 const unsigned char *sig,
181 unsigned char *out );
182
183/**
184 * \brief This function creates a LMOTS signature, using a
185 * LMOTS context that contains a private key.
186 *
187 * \note Before this function is called, the context must
188 * have been initialized and must contain a private
189 * key.
190 *
191 * \note LMOTS private keys can only be used once, otherwise
192 * attackers may be able to create forged signatures.
193 * If the signing operation is successful, the private
194 * key in the context will be erased, and no further
195 * signing will be possible until another private key
196 * is loaded
197 *
198 * \param ctx The initialized LMOTS context from which the
199 * private key will be read.
200 * \param f_rng The RNG function to be used for signature
201 * generation.
202 * \param p_rng The RNG context to be passed to f_rng
203 * \param msg The buffer from which the message will be read.
204 * \param msg_len The size of the message that will be read.
205 * \param sig The buf into which the signature will be stored.
206 * Must be at least #MBEDTLS_LMOTS_SIG_LEN in size.
207 *
208 * \return \c 0 on success.
209 * \return A non-zero error code on failure.
210 */
211int mbedtls_lmots_sign( mbedtls_lmots_context *ctx,
212 int (*f_rng)(void *, unsigned char *, size_t),
213 void *p_rng, const unsigned char *msg, size_t msg_len,
214 unsigned char *sig );
215
216/**
217 * \brief This function verifies a LMOTS signature, using a
218 * LMOTS context that contains a public key.
219 *
Raef Coles2ad6e612022-08-24 13:33:35 +0100220 * \warning This function is **not intended for use in
221 * production**, due to as-yet unsolved problems with
222 * handling stateful keys.
223 *
Raef Coles8ff6df52021-07-21 12:42:15 +0100224 * \note Before this function is called, the context must
225 * have been initialized and must contain a public key
226 * (either by import or generation).
227 *
228 * \param ctx The initialized LMOTS context from which the public
229 * key will be read.
230 * \param msg The buffer from which the message will be read.
231 * \param msg_len The size of the message that will be read.
232 * \param sig The buf from which the signature will be read.
233 * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from
234 * this.
235 *
236 * \return \c 0 on successful verification.
237 * \return A non-zero error code on failure.
238 */
239int mbedtls_lmots_verify( mbedtls_lmots_context *ctx, const unsigned char *msg,
240 size_t msg_len, const unsigned char *sig );
241
242/**
243 * \brief This function imports an LMOTS public key into a
244 * LMOTS context.
245 *
246 * \note Before this function is called, the context must
247 * have been initialized.
248 *
249 * \note See IETF RFC8554 for details of the encoding of
250 * this public key.
251 *
252 * \param ctx The initialized LMOTS context store the key in.
253 * \param key The buffer from which the key will be read.
254 * #MBEDTLS_LMOTS_PUBKEY_LEN bytes will be read from
255 * this.
256 *
257 * \return \c 0 on success.
258 * \return A non-zero error code on failure.
259 */
260int mbedtls_lmots_import_pubkey( mbedtls_lmots_context *ctx,
261 const unsigned char *key );
262
263/**
264 * \brief This function exports an LMOTS public key from a
265 * LMOTS context that already contains a public key.
266 *
267 * \note Before this function is called, the context must
268 * have been initialized and the context must contain
269 * a public key.
270 *
271 * \note See IETF RFC8554 for details of the encoding of
272 * this public key.
273 *
274 * \param ctx The initialized LMOTS context that contains the
275 * publc key.
276 * \param key The buffer into which the key will be output. Must
277 * be at least #MBEDTLS_LMOTS_PUBKEY_LEN in size.
278 *
279 * \return \c 0 on success.
280 * \return A non-zero error code on failure.
281 */
282int mbedtls_lmots_export_pubkey( mbedtls_lmots_context *ctx,
283 unsigned char *key );
284
285/**
286 * \brief This function generates an LMOTS public key from a
287 * LMOTS context that already contains a private key.
288 *
289 * \note Before this function is called, the context must
290 * have been initialized and the context must contain
291 * a private key.
292 *
293 * \param ctx The initialized LMOTS context to generate the key
294 * from and store it into.
295 *
296 * \return \c 0 on success.
297 * \return A non-zero error code on failure.
298 */
299int mbedtls_lmots_gen_pubkey( mbedtls_lmots_context *ctx );
300
301/**
302 * \brief This function generates an LMOTS private key, and
303 * stores in into an LMOTS context.
304 *
Raef Coles2ad6e612022-08-24 13:33:35 +0100305 * \warning This function is **not intended for use in
306 * production**, due to as-yet unsolved problems with
307 * handling stateful keys.
308 *
Raef Coles8ff6df52021-07-21 12:42:15 +0100309 * \note Before this function is called, the context must
310 * have been initialized and the type of the LMOTS
311 * context set using mbedtls_lmots_set_algorithm_type
312 *
313 * \note The seed must have at least 256 bits of entropy.
314 *
315 * \param ctx The initialized LMOTS context to generate the key
316 * into.
Raef Coles2ad6e612022-08-24 13:33:35 +0100317 * \param I_key_identifier The key identifier of the key, as a 16-byte string.
Raef Coles8ff6df52021-07-21 12:42:15 +0100318 * \param q_leaf_identifier The leaf identifier of key. If this LMOTS key is
319 * not being used as part of an LMS key, this should
320 * be set to 0.
321 * \param seed The seed used to deterministically generate the
322 * key.
323 * \param seed_len The length of the seed.
324 *
325 * \return \c 0 on success.
326 * \return A non-zero error code on failure.
327 */
328int mbedtls_lmots_gen_privkey( mbedtls_lmots_context *ctx,
329 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
330 unsigned int q_leaf_identifier,
331 const unsigned char *seed,
332 size_t seed_len );
333
334#ifdef __cplusplus
335}
336#endif
337
338#endif /* MBEDTLS_LMOTS_H */