blob: d0379e3a2d67f327f973d9a98b2d2bab86512c75 [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 Colese9479a02022-09-01 16:06:35 +010036/* Currently only defined for SHA256, 32 is the max hash output size */
37#define MBEDTLS_LMOTS_N_HASH_LEN_MAX (32u)
38#define MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX (34u)
39#define MBEDTLS_LMOTS_N_HASH_LEN(type) (type == MBEDTLS_LMOTS_SHA256_N32_W8 ? 32u : 0)
40#define MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(type) (type == MBEDTLS_LMOTS_SHA256_N32_W8 ? 34u : 0)
41#define MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(type) (MBEDTLS_LMOTS_N_HASH_LEN(type))
42#define MBEDTLS_LMOTS_TYPE_LEN (4u)
43#define MBEDTLS_LMOTS_I_KEY_ID_LEN (16u)
44#define MBEDTLS_LMOTS_Q_LEAF_ID_LEN (4u)
Raef Coles8ff6df52021-07-21 12:42:15 +010045
Raef Colese9479a02022-09-01 16:06:35 +010046#define MBEDTLS_LMOTS_SIG_LEN(type) (MBEDTLS_LMOTS_TYPE_LEN + \
47 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(type) + \
48 (MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(type) * \
49 MBEDTLS_LMOTS_N_HASH_LEN(type)))
Raef Coles8ff6df52021-07-21 12:42:15 +010050
Raef Colese9479a02022-09-01 16:06:35 +010051#define MBEDTLS_LMOTS_PUBLIC_KEY_LEN(type) (MBEDTLS_LMOTS_TYPE_LEN + \
52 MBEDTLS_LMOTS_I_KEY_ID_LEN + \
53 MBEDTLS_LMOTS_Q_LEAF_ID_LEN + \
54 MBEDTLS_LMOTS_N_HASH_LEN(type))
Raef Coles8ff6df52021-07-21 12:42:15 +010055
Raef Coles01c71a12022-08-31 15:55:00 +010056#define MBEDTLS_LMOTS_SIG_TYPE_OFFSET (0)
Raef Coles9c9027b2022-09-02 18:26:31 +010057#define MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET (MBEDTLS_LMOTS_SIG_TYPE_OFFSET + \
58 MBEDTLS_LMOTS_TYPE_LEN)
59#define MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(type) (MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET + \
60 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(type))
Raef Coles8ff6df52021-07-21 12:42:15 +010061
62#ifdef __cplusplus
63extern "C" {
64#endif
65
Raef Coles366d67d2022-09-01 17:23:12 +010066/** The Identifier of the LMS parameter set, as per
67 * https://www.iana.org/assignments/leighton-micali-signatures/leighton-micali-signatures.xhtml.
68 * We are only implementing a subset of the types, particularly N32_W8, for the sake of simplicty.
Raef Coles8ff6df52021-07-21 12:42:15 +010069 */
70typedef enum {
71 MBEDTLS_LMOTS_SHA256_N32_W8 = 4
72} mbedtls_lmots_algorithm_type_t;
73
74
Raef Coles01c71a12022-08-31 15:55:00 +010075/** LMOTS parameters structure.
76 *
77 * This contains the metadata associated with an LMOTS key, detailing the
78 * algorithm type, the key ID, and the leaf identifier should be key be part of
79 * a LMS key.
80 */
81typedef struct {
82 unsigned char MBEDTLS_PRIVATE(I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN]); /*!< The key
83 identifier. */
84 unsigned char MBEDTLS_PRIVATE(q_leaf_identifier[MBEDTLS_LMOTS_Q_LEAF_ID_LEN]); /*!< Which
85 leaf of the LMS key this is.
86 0 if the key is not part of an LMS key. */
87 mbedtls_lmots_algorithm_type_t MBEDTLS_PRIVATE(type); /*!< The LM-OTS key type identifier as
88 per IANA. Only SHA256_N32_W8 is
89 currently supported. */
90} mbedtls_lmots_parameters_t;
91
92/** LMOTS public context structure.
93 *
94 * A LMOTS public key is a hash output, and the applicable parameter set.
Raef Coles2ad6e612022-08-24 13:33:35 +010095 *
96 * The context must be initialized before it is used. A public key must either
Raef Coles01c71a12022-08-31 15:55:00 +010097 * be imported or generated from a private context.
Raef Coles2ad6e612022-08-24 13:33:35 +010098 *
99 * \dot
Raef Coles01c71a12022-08-31 15:55:00 +0100100 * digraph lmots_public_t {
Raef Coles2ad6e612022-08-24 13:33:35 +0100101 * UNINITIALIZED -> INIT [label="init"];
Raef Coles01c71a12022-08-31 15:55:00 +0100102 * HAVE_PUBLIC_KEY -> INIT [label="free"];
103 * INIT -> HAVE_PUBLIC_KEY [label="import_public_key"];
104 * INIT -> HAVE_PUBLIC_KEY [label="calculate_public_key from private key"];
105 * HAVE_PUBLIC_KEY -> HAVE_PUBLIC_KEY [label="export_public_key"];
Raef Coles2ad6e612022-08-24 13:33:35 +0100106 * }
107 * \enddot
108 */
Raef Coles8ff6df52021-07-21 12:42:15 +0100109typedef struct {
Raef Coles01c71a12022-08-31 15:55:00 +0100110 mbedtls_lmots_parameters_t MBEDTLS_PRIVATE(params);
Raef Colesfa24f9d2022-09-02 17:46:52 +0100111 unsigned char MBEDTLS_PRIVATE(public_key)[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles01c71a12022-08-31 15:55:00 +0100112 unsigned char MBEDTLS_PRIVATE(have_public_key); /*!< Whether the context contains a public key.
Raef Colesc4647462022-06-15 12:17:51 +0100113 Boolean values only. */
Raef Coles01c71a12022-08-31 15:55:00 +0100114} mbedtls_lmots_public_t;
115
Raef Colesab4f8742022-09-01 12:24:31 +0100116#ifdef MBEDTLS_LMS_PRIVATE
Raef Coles01c71a12022-08-31 15:55:00 +0100117/** LMOTS private context structure.
118 *
119 * A LMOTS private key is one hash output for each of digit of the digest +
120 * checksum, and the applicable parameter set.
121 *
122 * The context must be initialized before it is used. A public key must either
123 * be imported or generated from a private context.
124 *
125 * \dot
126 * digraph lmots_public_t {
127 * UNINITIALIZED -> INIT [label="init"];
128 * HAVE_PRIVATE_KEY -> INIT [label="free"];
129 * INIT -> HAVE_PRIVATE_KEY [label="generate_private_key"];
130 * HAVE_PRIVATE_KEY -> INIT [label="sign"];
131 * }
132 * \enddot
133 */
134typedef struct {
135 mbedtls_lmots_parameters_t MBEDTLS_PRIVATE(params);
Raef Colesfa24f9d2022-09-02 17:46:52 +0100136 unsigned char MBEDTLS_PRIVATE(private_key)[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX];
Raef Coles01c71a12022-08-31 15:55:00 +0100137 unsigned char MBEDTLS_PRIVATE(have_private_key); /*!< Whether the context contains a private key.
Raef Colesc4647462022-06-15 12:17:51 +0100138 Boolean values only. */
Raef Coles01c71a12022-08-31 15:55:00 +0100139} mbedtls_lmots_private_t;
Raef Colesab4f8742022-09-01 12:24:31 +0100140#endif /* MBEDTLS_LMS_PRIVATE */
Raef Coles01c71a12022-08-31 15:55:00 +0100141
Raef Coles40158e12022-09-27 10:23:53 +0100142#if defined(MBEDTLS_TEST_HOOKS)
143extern int( *mbedtls_lmots_sign_private_key_invalidated_hook )( unsigned char * );
144#endif /* defined(MBEDTLS_TEST_HOOKS) */
145
Raef Coles01c71a12022-08-31 15:55:00 +0100146/**
147 * \brief This function converts an unsigned int into a
148 * network-byte-order (big endian) string.
149 *
150 * \param val The unsigned integer value
151 * \param len The length of the string.
152 * \param bytes The string to output into.
Raef Coles01c71a12022-08-31 15:55:00 +0100153 */
Raef Coles9b88ee52022-09-02 12:04:21 +0100154void unsigned_int_to_network_bytes( unsigned int val, size_t len,
155 unsigned char *bytes );
Raef Coles01c71a12022-08-31 15:55:00 +0100156
157/**
158 * \brief This function converts a network-byte-order
159 * (big endian) string into an unsigned integer.
160 *
161 * \param len The length of the string.
162 * \param bytes The string.
163 *
164 * \return The corresponding LMS error code.
165 */
Raef Coles9b88ee52022-09-02 12:04:21 +0100166unsigned int network_bytes_to_unsigned_int( size_t len,
167 const unsigned char *bytes );
Raef Coles8ff6df52021-07-21 12:42:15 +0100168
Raef Colesc8f96042022-08-25 13:49:54 +0100169/**
170 * \brief This function converts a \ref psa_status_t to a
171 * low-level LMS error code.
172 *
173 * \param status The psa_status_t to convert
174 *
175 * \return The corresponding LMS error code.
176 */
Raef Coles9b88ee52022-09-02 12:04:21 +0100177int mbedtls_lms_error_from_psa( psa_status_t status );
Raef Colesc8f96042022-08-25 13:49:54 +0100178
Raef Coles8ff6df52021-07-21 12:42:15 +0100179
180/**
Raef Coles01c71a12022-08-31 15:55:00 +0100181 * \brief This function initializes a public LMOTS context
Raef Coles8ff6df52021-07-21 12:42:15 +0100182 *
183 * \param ctx The uninitialized LMOTS context that will then be
184 * initialized.
185 */
Raef Coles01c71a12022-08-31 15:55:00 +0100186void mbedtls_lmots_init_public( mbedtls_lmots_public_t *ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100187
188/**
Raef Coles01c71a12022-08-31 15:55:00 +0100189 * \brief This function uninitializes a public LMOTS context
Raef Coles8ff6df52021-07-21 12:42:15 +0100190 *
191 * \param ctx The initialized LMOTS context that will then be
192 * uninitialized.
193 */
Raef Coles01c71a12022-08-31 15:55:00 +0100194void mbedtls_lmots_free_public( mbedtls_lmots_public_t *ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100195
196/**
Raef Coles01c71a12022-08-31 15:55:00 +0100197 * \brief This function imports an LMOTS public key into a
198 * LMOTS context.
Raef Coles8ff6df52021-07-21 12:42:15 +0100199 *
Raef Coles01c71a12022-08-31 15:55:00 +0100200 * \note Before this function is called, the context must
201 * have been initialized.
Raef Coles8ff6df52021-07-21 12:42:15 +0100202 *
Raef Coles01c71a12022-08-31 15:55:00 +0100203 * \note See IETF RFC8554 for details of the encoding of
204 * this public key.
205 *
206 * \param ctx The initialized LMOTS context store the key in.
207 * \param key The buffer from which the key will be read.
Raef Coles366d67d2022-09-01 17:23:12 +0100208 * #MBEDTLS_LMOTS_PUBLIC_KEY_LEN bytes will be read
209 * from this.
Raef Colesc8f96042022-08-25 13:49:54 +0100210 *
211 * \return \c 0 on success.
212 * \return A non-zero error code on failure.
Raef Coles8ff6df52021-07-21 12:42:15 +0100213 */
Raef Coles01c71a12022-08-31 15:55:00 +0100214int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx,
215 const unsigned char *key, size_t key_size );
Raef Coles8ff6df52021-07-21 12:42:15 +0100216
217/**
218 * \brief This function creates a candidate public key from
219 * an LMOTS signature. This can then be compared to
220 * the real public key to determine the validity of
221 * the signature.
222 *
223 * \note This function is exposed publicly to be used in LMS
224 * signature verification, it is expected that
225 * mbedtls_lmots_verify will be used for LMOTS
226 * signature verification.
227 *
Raef Coles01c71a12022-08-31 15:55:00 +0100228 * \param params The LMOTS parameter set, q and I values as an
229 * mbedtls_lmots_parameters_t struct.
Raef Coles8ff6df52021-07-21 12:42:15 +0100230 * \param msg The buffer from which the message will be read.
Raef Coles01c71a12022-08-31 15:55:00 +0100231 * \param msg_size The size of the message that will be read.
Raef Coles2ad6e612022-08-24 13:33:35 +0100232 * \param sig The buffer from which the signature will be read.
Raef Coles366d67d2022-09-01 17:23:12 +0100233 * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from
234 * this.
Raef Coles8ff6df52021-07-21 12:42:15 +0100235 * \param out The buffer where the candidate public key will be
236 * stored. Must be at least #MBEDTLS_LMOTS_N_HASH_LEN
237 * bytes in size.
238 *
239 * \return \c 0 on success.
240 * \return A non-zero error code on failure.
241 */
Raef Coles01c71a12022-08-31 15:55:00 +0100242int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters_t *params,
243 const unsigned char *msg,
244 size_t msg_size,
245 const unsigned char *sig,
246 size_t sig_size,
247 unsigned char *out,
248 size_t out_size,
Raef Coles9b88ee52022-09-02 12:04:21 +0100249 size_t *out_len );
Raef Coles8ff6df52021-07-21 12:42:15 +0100250
251/**
Raef Coles01c71a12022-08-31 15:55:00 +0100252 * \brief This function verifies a LMOTS signature, using a
253 * LMOTS context that contains a public key.
254 *
255 * \warning This function is **not intended for use in
256 * production**, due to as-yet unsolved problems with
257 * handling stateful keys.
258 *
259 * \note Before this function is called, the context must
260 * have been initialized and must contain a public key
Raef Coles366d67d2022-09-01 17:23:12 +0100261 * (either by import or calculation from a private
262 * key).
Raef Coles01c71a12022-08-31 15:55:00 +0100263 *
264 * \param ctx The initialized LMOTS context from which the public
265 * key will be read.
266 * \param msg The buffer from which the message will be read.
267 * \param msg_size The size of the message that will be read.
268 * \param sig The buf from which the signature will be read.
269 * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from
270 * this.
271 *
272 * \return \c 0 on successful verification.
273 * \return A non-zero error code on failure.
274 */
275int mbedtls_lmots_verify( mbedtls_lmots_public_t *ctx, const unsigned char *msg,
276 size_t msg_size, const unsigned char *sig,
277 size_t sig_size );
278
Raef Colesab4f8742022-09-01 12:24:31 +0100279#ifdef MBEDTLS_LMS_PRIVATE
280
Raef Coles01c71a12022-08-31 15:55:00 +0100281/**
282 * \brief This function initializes a private LMOTS context
283 *
284 * \param ctx The uninitialized LMOTS context that will then be
285 * initialized.
286 */
287void mbedtls_lmots_init_private( mbedtls_lmots_private_t *ctx );
288
289/**
290 * \brief This function uninitializes a private LMOTS context
291 *
292 * \param ctx The initialized LMOTS context that will then be
293 * uninitialized.
294 */
295void mbedtls_lmots_free_private( mbedtls_lmots_private_t *ctx );
296
297/**
298 * \brief This function generates an LMOTS private key, and
299 * stores in into an LMOTS context.
300 *
301 * \warning This function is **not intended for use in
302 * production**, due to as-yet unsolved problems with
303 * handling stateful keys.
304 *
305 * \note The seed must have at least 256 bits of entropy.
306 *
307 * \param ctx The initialized LMOTS context to generate the key
308 * into.
309 * \param I_key_identifier The key identifier of the key, as a 16-byte string.
310 * \param q_leaf_identifier The leaf identifier of key. If this LMOTS key is
311 * not being used as part of an LMS key, this should
312 * be set to 0.
313 * \param seed The seed used to deterministically generate the
314 * key.
315 * \param seed_size The length of the seed.
316 *
317 * \return \c 0 on success.
318 * \return A non-zero error code on failure.
319 */
320int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx,
321 mbedtls_lmots_algorithm_type_t type,
322 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
323 uint32_t q_leaf_identifier,
324 const unsigned char *seed,
325 size_t seed_size );
326
327/**
328 * \brief This function generates an LMOTS public key from a
329 * LMOTS context that already contains a private key.
330 *
331 * \note Before this function is called, the context must
332 * have been initialized and the context must contain
333 * a private key.
334 *
335 * \param ctx The initialized LMOTS context to generate the key
336 * from and store it into.
337 *
338 * \return \c 0 on success.
339 * \return A non-zero error code on failure.
340 */
341int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx,
Raef Coles9b88ee52022-09-02 12:04:21 +0100342 mbedtls_lmots_private_t *priv_ctx );
Raef Coles01c71a12022-08-31 15:55:00 +0100343
344
345/**
346 * \brief This function exports an LMOTS public key from a
347 * LMOTS context that already contains a public key.
348 *
349 * \note Before this function is called, the context must
350 * have been initialized and the context must contain
351 * a public key.
352 *
353 * \note See IETF RFC8554 for details of the encoding of
354 * this public key.
355 *
356 * \param ctx The initialized LMOTS context that contains the
357 * publc key.
358 * \param key The buffer into which the key will be output. Must
359 * be at least #MBEDTLS_LMOTS_PUBLIC_KEY_LEN in size.
360 *
361 * \return \c 0 on success.
362 * \return A non-zero error code on failure.
363 */
364int mbedtls_lmots_export_public_key( mbedtls_lmots_public_t *ctx,
365 unsigned char *key, size_t key_size,
366 size_t *key_len );
367/**
Raef Coles8ff6df52021-07-21 12:42:15 +0100368 * \brief This function creates a LMOTS signature, using a
369 * LMOTS context that contains a private key.
370 *
371 * \note Before this function is called, the context must
372 * have been initialized and must contain a private
373 * key.
374 *
375 * \note LMOTS private keys can only be used once, otherwise
376 * attackers may be able to create forged signatures.
377 * If the signing operation is successful, the private
378 * key in the context will be erased, and no further
379 * signing will be possible until another private key
380 * is loaded
381 *
382 * \param ctx The initialized LMOTS context from which the
383 * private key will be read.
384 * \param f_rng The RNG function to be used for signature
385 * generation.
386 * \param p_rng The RNG context to be passed to f_rng
387 * \param msg The buffer from which the message will be read.
Raef Coles01c71a12022-08-31 15:55:00 +0100388 * \param msg_size The size of the message that will be read.
Raef Coles8ff6df52021-07-21 12:42:15 +0100389 * \param sig The buf into which the signature will be stored.
390 * Must be at least #MBEDTLS_LMOTS_SIG_LEN in size.
391 *
392 * \return \c 0 on success.
393 * \return A non-zero error code on failure.
394 */
Raef Coles01c71a12022-08-31 15:55:00 +0100395int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
Raef Coles8ff6df52021-07-21 12:42:15 +0100396 int (*f_rng)(void *, unsigned char *, size_t),
Raef Coles01c71a12022-08-31 15:55:00 +0100397 void *p_rng, const unsigned char *msg, size_t msg_size,
398 unsigned char *sig, size_t sig_size, size_t* sig_len );
Raef Coles8ff6df52021-07-21 12:42:15 +0100399
Raef Colesab4f8742022-09-01 12:24:31 +0100400#endif /* MBEDTLS_LMS_PRIVATE */
Raef Coles8ff6df52021-07-21 12:42:15 +0100401
402#ifdef __cplusplus
403}
404#endif
405
406#endif /* MBEDTLS_LMOTS_H */