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