blob: ca7d4bf344ea17851ed9921b7a625742bc92c008 [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
Raef Coles01c71a12022-08-31 15:55:00 +010029#include "mbedtls/build_info.h"
Raef Coles8ff6df52021-07-21 12:42:15 +010030
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)
Raef Coles01c71a12022-08-31 15:55:00 +010037#define MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT (34)
Raef Coles8ff6df52021-07-21 12:42:15 +010038#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 + \
Raef Coles01c71a12022-08-31 15:55:00 +010044 (MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT * MBEDTLS_LMOTS_N_HASH_LEN))
Raef Coles8ff6df52021-07-21 12:42:15 +010045
Raef Coles01c71a12022-08-31 15:55:00 +010046#define MBEDTLS_LMOTS_PUBLIC_KEY_LEN (MBEDTLS_LMOTS_TYPE_LEN + MBEDTLS_LMOTS_I_KEY_ID_LEN + \
Raef Coles8ff6df52021-07-21 12:42:15 +010047 MBEDTLS_LMOTS_Q_LEAF_ID_LEN + MBEDTLS_LMOTS_N_HASH_LEN)
48
Raef Coles01c71a12022-08-31 15:55:00 +010049#define MBEDTLS_LMOTS_SIG_TYPE_OFFSET (0)
Raef Coles8ff6df52021-07-21 12:42:15 +010050
51#ifdef __cplusplus
52extern "C" {
53#endif
54
Raef Colesc4647462022-06-15 12:17:51 +010055/* https://www.iana.org/assignments/leighton-micali-signatures/leighton-micali-signatures.xhtml
56 * We are only implementing a subset of the types, particularly n32_w8, for the sake of simplicty.
Raef Coles8ff6df52021-07-21 12:42:15 +010057 */
58typedef enum {
59 MBEDTLS_LMOTS_SHA256_N32_W8 = 4
60} mbedtls_lmots_algorithm_type_t;
61
62
Raef Coles01c71a12022-08-31 15:55:00 +010063/** LMOTS parameters structure.
64 *
65 * This contains the metadata associated with an LMOTS key, detailing the
66 * algorithm type, the key ID, and the leaf identifier should be key be part of
67 * a LMS key.
68 */
69typedef struct {
70 unsigned char MBEDTLS_PRIVATE(I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN]); /*!< The key
71 identifier. */
72 unsigned char MBEDTLS_PRIVATE(q_leaf_identifier[MBEDTLS_LMOTS_Q_LEAF_ID_LEN]); /*!< Which
73 leaf of the LMS key this is.
74 0 if the key is not part of an LMS key. */
75 mbedtls_lmots_algorithm_type_t MBEDTLS_PRIVATE(type); /*!< The LM-OTS key type identifier as
76 per IANA. Only SHA256_N32_W8 is
77 currently supported. */
78} mbedtls_lmots_parameters_t;
79
80/** LMOTS public context structure.
81 *
82 * A LMOTS public key is a hash output, and the applicable parameter set.
Raef Coles2ad6e612022-08-24 13:33:35 +010083 *
84 * The context must be initialized before it is used. A public key must either
Raef Coles01c71a12022-08-31 15:55:00 +010085 * be imported or generated from a private context.
Raef Coles2ad6e612022-08-24 13:33:35 +010086 *
87 * \dot
Raef Coles01c71a12022-08-31 15:55:00 +010088 * digraph lmots_public_t {
Raef Coles2ad6e612022-08-24 13:33:35 +010089 * UNINITIALIZED -> INIT [label="init"];
Raef Coles01c71a12022-08-31 15:55:00 +010090 * HAVE_PUBLIC_KEY -> INIT [label="free"];
91 * INIT -> HAVE_PUBLIC_KEY [label="import_public_key"];
92 * INIT -> HAVE_PUBLIC_KEY [label="calculate_public_key from private key"];
93 * HAVE_PUBLIC_KEY -> HAVE_PUBLIC_KEY [label="export_public_key"];
Raef Coles2ad6e612022-08-24 13:33:35 +010094 * }
95 * \enddot
96 */
Raef Coles8ff6df52021-07-21 12:42:15 +010097typedef struct {
Raef Coles01c71a12022-08-31 15:55:00 +010098 mbedtls_lmots_parameters_t MBEDTLS_PRIVATE(params);
99 unsigned char MBEDTLS_PRIVATE(public_key)[32];
100 unsigned char MBEDTLS_PRIVATE(have_public_key); /*!< Whether the context contains a public key.
Raef Colesc4647462022-06-15 12:17:51 +0100101 Boolean values only. */
Raef Coles01c71a12022-08-31 15:55:00 +0100102} mbedtls_lmots_public_t;
103
104/** LMOTS private context structure.
105 *
106 * A LMOTS private key is one hash output for each of digit of the digest +
107 * checksum, and the applicable parameter set.
108 *
109 * The context must be initialized before it is used. A public key must either
110 * be imported or generated from a private context.
111 *
112 * \dot
113 * digraph lmots_public_t {
114 * UNINITIALIZED -> INIT [label="init"];
115 * HAVE_PRIVATE_KEY -> INIT [label="free"];
116 * INIT -> HAVE_PRIVATE_KEY [label="generate_private_key"];
117 * HAVE_PRIVATE_KEY -> INIT [label="sign"];
118 * }
119 * \enddot
120 */
121typedef struct {
122 mbedtls_lmots_parameters_t MBEDTLS_PRIVATE(params);
123 unsigned char MBEDTLS_PRIVATE(private_key)[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][32];
124 unsigned char MBEDTLS_PRIVATE(have_private_key); /*!< Whether the context contains a private key.
Raef Colesc4647462022-06-15 12:17:51 +0100125 Boolean values only. */
Raef Coles01c71a12022-08-31 15:55:00 +0100126} mbedtls_lmots_private_t;
127
128/**
129 * \brief This function converts an unsigned int into a
130 * network-byte-order (big endian) string.
131 *
132 * \param val The unsigned integer value
133 * \param len The length of the string.
134 * \param bytes The string to output into.
135 *
136 * \return The corresponding LMS error code.
137 */
138void unsigned_int_to_network_bytes(unsigned int val, size_t len, unsigned char *bytes);
139
140/**
141 * \brief This function converts a network-byte-order
142 * (big endian) string into an unsigned integer.
143 *
144 * \param len The length of the string.
145 * \param bytes The string.
146 *
147 * \return The corresponding LMS error code.
148 */
149unsigned int network_bytes_to_unsigned_int(size_t len, const unsigned char *bytes);
Raef Coles8ff6df52021-07-21 12:42:15 +0100150
Raef Colesc8f96042022-08-25 13:49:54 +0100151/**
152 * \brief This function converts a \ref psa_status_t to a
153 * low-level LMS error code.
154 *
155 * \param status The psa_status_t to convert
156 *
157 * \return The corresponding LMS error code.
158 */
159int mbedtls_lms_error_from_psa(psa_status_t status);
160
Raef Coles8ff6df52021-07-21 12:42:15 +0100161
162/**
Raef Coles01c71a12022-08-31 15:55:00 +0100163 * \brief This function initializes a public LMOTS context
Raef Coles8ff6df52021-07-21 12:42:15 +0100164 *
165 * \param ctx The uninitialized LMOTS context that will then be
166 * initialized.
167 */
Raef Coles01c71a12022-08-31 15:55:00 +0100168void mbedtls_lmots_init_public( mbedtls_lmots_public_t *ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100169
170/**
Raef Coles01c71a12022-08-31 15:55:00 +0100171 * \brief This function uninitializes a public LMOTS context
Raef Coles8ff6df52021-07-21 12:42:15 +0100172 *
173 * \param ctx The initialized LMOTS context that will then be
174 * uninitialized.
175 */
Raef Coles01c71a12022-08-31 15:55:00 +0100176void mbedtls_lmots_free_public( mbedtls_lmots_public_t *ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100177
178/**
Raef Coles01c71a12022-08-31 15:55:00 +0100179 * \brief This function imports an LMOTS public key into a
180 * LMOTS context.
Raef Coles8ff6df52021-07-21 12:42:15 +0100181 *
Raef Coles01c71a12022-08-31 15:55:00 +0100182 * \note Before this function is called, the context must
183 * have been initialized.
Raef Coles8ff6df52021-07-21 12:42:15 +0100184 *
Raef Coles01c71a12022-08-31 15:55:00 +0100185 * \note See IETF RFC8554 for details of the encoding of
186 * this public key.
187 *
188 * \param ctx The initialized LMOTS context store the key in.
189 * \param key The buffer from which the key will be read.
190 * #MBEDTLS_LMOTS_PUBLIC_KEY_LEN bytes will be read from
191 * this.
Raef Colesc8f96042022-08-25 13:49:54 +0100192 *
193 * \return \c 0 on success.
194 * \return A non-zero error code on failure.
Raef Coles8ff6df52021-07-21 12:42:15 +0100195 */
Raef Coles01c71a12022-08-31 15:55:00 +0100196int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx,
197 const unsigned char *key, size_t key_size );
Raef Coles8ff6df52021-07-21 12:42:15 +0100198
199/**
200 * \brief This function creates a candidate public key from
201 * an LMOTS signature. This can then be compared to
202 * the real public key to determine the validity of
203 * the signature.
204 *
205 * \note This function is exposed publicly to be used in LMS
206 * signature verification, it is expected that
207 * mbedtls_lmots_verify will be used for LMOTS
208 * signature verification.
209 *
Raef Coles01c71a12022-08-31 15:55:00 +0100210 * \param params The LMOTS parameter set, q and I values as an
211 * mbedtls_lmots_parameters_t struct.
Raef Coles8ff6df52021-07-21 12:42:15 +0100212 * \param msg The buffer from which the message will be read.
Raef Coles01c71a12022-08-31 15:55:00 +0100213 * \param msg_size The size of the message that will be read.
Raef Coles2ad6e612022-08-24 13:33:35 +0100214 * \param sig The buffer from which the signature will be read.
215 * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from this.
Raef Coles8ff6df52021-07-21 12:42:15 +0100216 * \param out The buffer where the candidate public key will be
217 * stored. Must be at least #MBEDTLS_LMOTS_N_HASH_LEN
218 * bytes in size.
219 *
220 * \return \c 0 on success.
221 * \return A non-zero error code on failure.
222 */
Raef Coles01c71a12022-08-31 15:55:00 +0100223int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters_t *params,
224 const unsigned char *msg,
225 size_t msg_size,
226 const unsigned char *sig,
227 size_t sig_size,
228 unsigned char *out,
229 size_t out_size,
230 size_t *out_len);
Raef Coles8ff6df52021-07-21 12:42:15 +0100231
232/**
Raef Coles01c71a12022-08-31 15:55:00 +0100233 * \brief This function verifies a LMOTS signature, using a
234 * LMOTS context that contains a public key.
235 *
236 * \warning This function is **not intended for use in
237 * production**, due to as-yet unsolved problems with
238 * handling stateful keys.
239 *
240 * \note Before this function is called, the context must
241 * have been initialized and must contain a public key
242 * (either by import or calculation from a private key).
243 *
244 * \param ctx The initialized LMOTS context from which the public
245 * key will be read.
246 * \param msg The buffer from which the message will be read.
247 * \param msg_size The size of the message that will be read.
248 * \param sig The buf from which the signature will be read.
249 * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from
250 * this.
251 *
252 * \return \c 0 on successful verification.
253 * \return A non-zero error code on failure.
254 */
255int mbedtls_lmots_verify( mbedtls_lmots_public_t *ctx, const unsigned char *msg,
256 size_t msg_size, const unsigned char *sig,
257 size_t sig_size );
258
259/**
260 * \brief This function initializes a private LMOTS context
261 *
262 * \param ctx The uninitialized LMOTS context that will then be
263 * initialized.
264 */
265void mbedtls_lmots_init_private( mbedtls_lmots_private_t *ctx );
266
267/**
268 * \brief This function uninitializes a private LMOTS context
269 *
270 * \param ctx The initialized LMOTS context that will then be
271 * uninitialized.
272 */
273void mbedtls_lmots_free_private( mbedtls_lmots_private_t *ctx );
274
275/**
276 * \brief This function generates an LMOTS private key, and
277 * stores in into an LMOTS context.
278 *
279 * \warning This function is **not intended for use in
280 * production**, due to as-yet unsolved problems with
281 * handling stateful keys.
282 *
283 * \note The seed must have at least 256 bits of entropy.
284 *
285 * \param ctx The initialized LMOTS context to generate the key
286 * into.
287 * \param I_key_identifier The key identifier of the key, as a 16-byte string.
288 * \param q_leaf_identifier The leaf identifier of key. If this LMOTS key is
289 * not being used as part of an LMS key, this should
290 * be set to 0.
291 * \param seed The seed used to deterministically generate the
292 * key.
293 * \param seed_size The length of the seed.
294 *
295 * \return \c 0 on success.
296 * \return A non-zero error code on failure.
297 */
298int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx,
299 mbedtls_lmots_algorithm_type_t type,
300 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
301 uint32_t q_leaf_identifier,
302 const unsigned char *seed,
303 size_t seed_size );
304
305/**
306 * \brief This function generates an LMOTS public key from a
307 * LMOTS context that already contains a private key.
308 *
309 * \note Before this function is called, the context must
310 * have been initialized and the context must contain
311 * a private key.
312 *
313 * \param ctx The initialized LMOTS context to generate the key
314 * from and store it into.
315 *
316 * \return \c 0 on success.
317 * \return A non-zero error code on failure.
318 */
319int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx,
320 mbedtls_lmots_private_t *priv_ctx);
321
322
323/**
324 * \brief This function exports an LMOTS public key from a
325 * LMOTS context that already contains a public key.
326 *
327 * \note Before this function is called, the context must
328 * have been initialized and the context must contain
329 * a public key.
330 *
331 * \note See IETF RFC8554 for details of the encoding of
332 * this public key.
333 *
334 * \param ctx The initialized LMOTS context that contains the
335 * publc key.
336 * \param key The buffer into which the key will be output. Must
337 * be at least #MBEDTLS_LMOTS_PUBLIC_KEY_LEN in size.
338 *
339 * \return \c 0 on success.
340 * \return A non-zero error code on failure.
341 */
342int mbedtls_lmots_export_public_key( mbedtls_lmots_public_t *ctx,
343 unsigned char *key, size_t key_size,
344 size_t *key_len );
345/**
Raef Coles8ff6df52021-07-21 12:42:15 +0100346 * \brief This function creates a LMOTS signature, using a
347 * LMOTS context that contains a private key.
348 *
349 * \note Before this function is called, the context must
350 * have been initialized and must contain a private
351 * key.
352 *
353 * \note LMOTS private keys can only be used once, otherwise
354 * attackers may be able to create forged signatures.
355 * If the signing operation is successful, the private
356 * key in the context will be erased, and no further
357 * signing will be possible until another private key
358 * is loaded
359 *
360 * \param ctx The initialized LMOTS context from which the
361 * private key will be read.
362 * \param f_rng The RNG function to be used for signature
363 * generation.
364 * \param p_rng The RNG context to be passed to f_rng
365 * \param msg The buffer from which the message will be read.
Raef Coles01c71a12022-08-31 15:55:00 +0100366 * \param msg_size The size of the message that will be read.
Raef Coles8ff6df52021-07-21 12:42:15 +0100367 * \param sig The buf into which the signature will be stored.
368 * Must be at least #MBEDTLS_LMOTS_SIG_LEN in size.
369 *
370 * \return \c 0 on success.
371 * \return A non-zero error code on failure.
372 */
Raef Coles01c71a12022-08-31 15:55:00 +0100373int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
Raef Coles8ff6df52021-07-21 12:42:15 +0100374 int (*f_rng)(void *, unsigned char *, size_t),
Raef Coles01c71a12022-08-31 15:55:00 +0100375 void *p_rng, const unsigned char *msg, size_t msg_size,
376 unsigned char *sig, size_t sig_size, size_t* sig_len );
Raef Coles8ff6df52021-07-21 12:42:15 +0100377
Raef Coles8ff6df52021-07-21 12:42:15 +0100378
379#ifdef __cplusplus
380}
381#endif
382
383#endif /* MBEDTLS_LMOTS_H */