blob: 6ada0bad6426558d9b2571b076f1e3ed17b8a212 [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 Coles8ff6df52021-07-21 12:42:15 +010057
58#ifdef __cplusplus
59extern "C" {
60#endif
61
Raef Colesc4647462022-06-15 12:17:51 +010062/* https://www.iana.org/assignments/leighton-micali-signatures/leighton-micali-signatures.xhtml
63 * We are only implementing a subset of the types, particularly n32_w8, for the sake of simplicty.
Raef Coles8ff6df52021-07-21 12:42:15 +010064 */
65typedef enum {
66 MBEDTLS_LMOTS_SHA256_N32_W8 = 4
67} mbedtls_lmots_algorithm_type_t;
68
69
Raef Coles01c71a12022-08-31 15:55:00 +010070/** LMOTS parameters structure.
71 *
72 * This contains the metadata associated with an LMOTS key, detailing the
73 * algorithm type, the key ID, and the leaf identifier should be key be part of
74 * a LMS key.
75 */
76typedef struct {
77 unsigned char MBEDTLS_PRIVATE(I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN]); /*!< The key
78 identifier. */
79 unsigned char MBEDTLS_PRIVATE(q_leaf_identifier[MBEDTLS_LMOTS_Q_LEAF_ID_LEN]); /*!< Which
80 leaf of the LMS key this is.
81 0 if the key is not part of an LMS key. */
82 mbedtls_lmots_algorithm_type_t MBEDTLS_PRIVATE(type); /*!< The LM-OTS key type identifier as
83 per IANA. Only SHA256_N32_W8 is
84 currently supported. */
85} mbedtls_lmots_parameters_t;
86
87/** LMOTS public context structure.
88 *
89 * A LMOTS public key is a hash output, and the applicable parameter set.
Raef Coles2ad6e612022-08-24 13:33:35 +010090 *
91 * The context must be initialized before it is used. A public key must either
Raef Coles01c71a12022-08-31 15:55:00 +010092 * be imported or generated from a private context.
Raef Coles2ad6e612022-08-24 13:33:35 +010093 *
94 * \dot
Raef Coles01c71a12022-08-31 15:55:00 +010095 * digraph lmots_public_t {
Raef Coles2ad6e612022-08-24 13:33:35 +010096 * UNINITIALIZED -> INIT [label="init"];
Raef Coles01c71a12022-08-31 15:55:00 +010097 * HAVE_PUBLIC_KEY -> INIT [label="free"];
98 * INIT -> HAVE_PUBLIC_KEY [label="import_public_key"];
99 * INIT -> HAVE_PUBLIC_KEY [label="calculate_public_key from private key"];
100 * HAVE_PUBLIC_KEY -> HAVE_PUBLIC_KEY [label="export_public_key"];
Raef Coles2ad6e612022-08-24 13:33:35 +0100101 * }
102 * \enddot
103 */
Raef Coles8ff6df52021-07-21 12:42:15 +0100104typedef struct {
Raef Coles01c71a12022-08-31 15:55:00 +0100105 mbedtls_lmots_parameters_t MBEDTLS_PRIVATE(params);
106 unsigned char MBEDTLS_PRIVATE(public_key)[32];
107 unsigned char MBEDTLS_PRIVATE(have_public_key); /*!< Whether the context contains a public key.
Raef Colesc4647462022-06-15 12:17:51 +0100108 Boolean values only. */
Raef Coles01c71a12022-08-31 15:55:00 +0100109} mbedtls_lmots_public_t;
110
Raef Colesab4f8742022-09-01 12:24:31 +0100111#ifdef MBEDTLS_LMS_PRIVATE
Raef Coles01c71a12022-08-31 15:55:00 +0100112/** LMOTS private context structure.
113 *
114 * A LMOTS private key is one hash output for each of digit of the digest +
115 * checksum, and the applicable parameter set.
116 *
117 * The context must be initialized before it is used. A public key must either
118 * be imported or generated from a private context.
119 *
120 * \dot
121 * digraph lmots_public_t {
122 * UNINITIALIZED -> INIT [label="init"];
123 * HAVE_PRIVATE_KEY -> INIT [label="free"];
124 * INIT -> HAVE_PRIVATE_KEY [label="generate_private_key"];
125 * HAVE_PRIVATE_KEY -> INIT [label="sign"];
126 * }
127 * \enddot
128 */
129typedef struct {
130 mbedtls_lmots_parameters_t MBEDTLS_PRIVATE(params);
Raef Colese9479a02022-09-01 16:06:35 +0100131 unsigned char MBEDTLS_PRIVATE(private_key)[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][32];
Raef Coles01c71a12022-08-31 15:55:00 +0100132 unsigned char MBEDTLS_PRIVATE(have_private_key); /*!< Whether the context contains a private key.
Raef Colesc4647462022-06-15 12:17:51 +0100133 Boolean values only. */
Raef Coles01c71a12022-08-31 15:55:00 +0100134} mbedtls_lmots_private_t;
Raef Colesab4f8742022-09-01 12:24:31 +0100135#endif /* MBEDTLS_LMS_PRIVATE */
Raef Coles01c71a12022-08-31 15:55:00 +0100136
137/**
138 * \brief This function converts an unsigned int into a
139 * network-byte-order (big endian) string.
140 *
141 * \param val The unsigned integer value
142 * \param len The length of the string.
143 * \param bytes The string to output into.
144 *
145 * \return The corresponding LMS error code.
146 */
147void unsigned_int_to_network_bytes(unsigned int val, size_t len, unsigned char *bytes);
148
149/**
150 * \brief This function converts a network-byte-order
151 * (big endian) string into an unsigned integer.
152 *
153 * \param len The length of the string.
154 * \param bytes The string.
155 *
156 * \return The corresponding LMS error code.
157 */
158unsigned int network_bytes_to_unsigned_int(size_t len, const unsigned char *bytes);
Raef Coles8ff6df52021-07-21 12:42:15 +0100159
Raef Colesc8f96042022-08-25 13:49:54 +0100160/**
161 * \brief This function converts a \ref psa_status_t to a
162 * low-level LMS error code.
163 *
164 * \param status The psa_status_t to convert
165 *
166 * \return The corresponding LMS error code.
167 */
168int mbedtls_lms_error_from_psa(psa_status_t status);
169
Raef Coles8ff6df52021-07-21 12:42:15 +0100170
171/**
Raef Coles01c71a12022-08-31 15:55:00 +0100172 * \brief This function initializes a public LMOTS context
Raef Coles8ff6df52021-07-21 12:42:15 +0100173 *
174 * \param ctx The uninitialized LMOTS context that will then be
175 * initialized.
176 */
Raef Coles01c71a12022-08-31 15:55:00 +0100177void mbedtls_lmots_init_public( mbedtls_lmots_public_t *ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100178
179/**
Raef Coles01c71a12022-08-31 15:55:00 +0100180 * \brief This function uninitializes a public LMOTS context
Raef Coles8ff6df52021-07-21 12:42:15 +0100181 *
182 * \param ctx The initialized LMOTS context that will then be
183 * uninitialized.
184 */
Raef Coles01c71a12022-08-31 15:55:00 +0100185void mbedtls_lmots_free_public( mbedtls_lmots_public_t *ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100186
187/**
Raef Coles01c71a12022-08-31 15:55:00 +0100188 * \brief This function imports an LMOTS public key into a
189 * LMOTS context.
Raef Coles8ff6df52021-07-21 12:42:15 +0100190 *
Raef Coles01c71a12022-08-31 15:55:00 +0100191 * \note Before this function is called, the context must
192 * have been initialized.
Raef Coles8ff6df52021-07-21 12:42:15 +0100193 *
Raef Coles01c71a12022-08-31 15:55:00 +0100194 * \note See IETF RFC8554 for details of the encoding of
195 * this public key.
196 *
197 * \param ctx The initialized LMOTS context store the key in.
198 * \param key The buffer from which the key will be read.
199 * #MBEDTLS_LMOTS_PUBLIC_KEY_LEN bytes will be read from
200 * this.
Raef Colesc8f96042022-08-25 13:49:54 +0100201 *
202 * \return \c 0 on success.
203 * \return A non-zero error code on failure.
Raef Coles8ff6df52021-07-21 12:42:15 +0100204 */
Raef Coles01c71a12022-08-31 15:55:00 +0100205int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx,
206 const unsigned char *key, size_t key_size );
Raef Coles8ff6df52021-07-21 12:42:15 +0100207
208/**
209 * \brief This function creates a candidate public key from
210 * an LMOTS signature. This can then be compared to
211 * the real public key to determine the validity of
212 * the signature.
213 *
214 * \note This function is exposed publicly to be used in LMS
215 * signature verification, it is expected that
216 * mbedtls_lmots_verify will be used for LMOTS
217 * signature verification.
218 *
Raef Coles01c71a12022-08-31 15:55:00 +0100219 * \param params The LMOTS parameter set, q and I values as an
220 * mbedtls_lmots_parameters_t struct.
Raef Coles8ff6df52021-07-21 12:42:15 +0100221 * \param msg The buffer from which the message will be read.
Raef Coles01c71a12022-08-31 15:55:00 +0100222 * \param msg_size The size of the message that will be read.
Raef Coles2ad6e612022-08-24 13:33:35 +0100223 * \param sig The buffer from which the signature will be read.
224 * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from this.
Raef Coles8ff6df52021-07-21 12:42:15 +0100225 * \param out The buffer where the candidate public key will be
226 * stored. Must be at least #MBEDTLS_LMOTS_N_HASH_LEN
227 * bytes in size.
228 *
229 * \return \c 0 on success.
230 * \return A non-zero error code on failure.
231 */
Raef Coles01c71a12022-08-31 15:55:00 +0100232int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters_t *params,
233 const unsigned char *msg,
234 size_t msg_size,
235 const unsigned char *sig,
236 size_t sig_size,
237 unsigned char *out,
238 size_t out_size,
239 size_t *out_len);
Raef Coles8ff6df52021-07-21 12:42:15 +0100240
241/**
Raef Coles01c71a12022-08-31 15:55:00 +0100242 * \brief This function verifies a LMOTS signature, using a
243 * LMOTS context that contains a public key.
244 *
245 * \warning This function is **not intended for use in
246 * production**, due to as-yet unsolved problems with
247 * handling stateful keys.
248 *
249 * \note Before this function is called, the context must
250 * have been initialized and must contain a public key
251 * (either by import or calculation from a private key).
252 *
253 * \param ctx The initialized LMOTS context from which the public
254 * key will be read.
255 * \param msg The buffer from which the message will be read.
256 * \param msg_size The size of the message that will be read.
257 * \param sig The buf from which the signature will be read.
258 * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from
259 * this.
260 *
261 * \return \c 0 on successful verification.
262 * \return A non-zero error code on failure.
263 */
264int mbedtls_lmots_verify( mbedtls_lmots_public_t *ctx, const unsigned char *msg,
265 size_t msg_size, const unsigned char *sig,
266 size_t sig_size );
267
Raef Colesab4f8742022-09-01 12:24:31 +0100268#ifdef MBEDTLS_LMS_PRIVATE
269
Raef Coles01c71a12022-08-31 15:55:00 +0100270/**
271 * \brief This function initializes a private LMOTS context
272 *
273 * \param ctx The uninitialized LMOTS context that will then be
274 * initialized.
275 */
276void mbedtls_lmots_init_private( mbedtls_lmots_private_t *ctx );
277
278/**
279 * \brief This function uninitializes a private LMOTS context
280 *
281 * \param ctx The initialized LMOTS context that will then be
282 * uninitialized.
283 */
284void mbedtls_lmots_free_private( mbedtls_lmots_private_t *ctx );
285
286/**
287 * \brief This function generates an LMOTS private key, and
288 * stores in into an LMOTS context.
289 *
290 * \warning This function is **not intended for use in
291 * production**, due to as-yet unsolved problems with
292 * handling stateful keys.
293 *
294 * \note The seed must have at least 256 bits of entropy.
295 *
296 * \param ctx The initialized LMOTS context to generate the key
297 * into.
298 * \param I_key_identifier The key identifier of the key, as a 16-byte string.
299 * \param q_leaf_identifier The leaf identifier of key. If this LMOTS key is
300 * not being used as part of an LMS key, this should
301 * be set to 0.
302 * \param seed The seed used to deterministically generate the
303 * key.
304 * \param seed_size The length of the seed.
305 *
306 * \return \c 0 on success.
307 * \return A non-zero error code on failure.
308 */
309int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx,
310 mbedtls_lmots_algorithm_type_t type,
311 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
312 uint32_t q_leaf_identifier,
313 const unsigned char *seed,
314 size_t seed_size );
315
316/**
317 * \brief This function generates an LMOTS public key from a
318 * LMOTS context that already contains a private key.
319 *
320 * \note Before this function is called, the context must
321 * have been initialized and the context must contain
322 * a private key.
323 *
324 * \param ctx The initialized LMOTS context to generate the key
325 * from and store it into.
326 *
327 * \return \c 0 on success.
328 * \return A non-zero error code on failure.
329 */
330int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx,
331 mbedtls_lmots_private_t *priv_ctx);
332
333
334/**
335 * \brief This function exports an LMOTS public key from a
336 * LMOTS context that already contains a public key.
337 *
338 * \note Before this function is called, the context must
339 * have been initialized and the context must contain
340 * a public key.
341 *
342 * \note See IETF RFC8554 for details of the encoding of
343 * this public key.
344 *
345 * \param ctx The initialized LMOTS context that contains the
346 * publc key.
347 * \param key The buffer into which the key will be output. Must
348 * be at least #MBEDTLS_LMOTS_PUBLIC_KEY_LEN in size.
349 *
350 * \return \c 0 on success.
351 * \return A non-zero error code on failure.
352 */
353int mbedtls_lmots_export_public_key( mbedtls_lmots_public_t *ctx,
354 unsigned char *key, size_t key_size,
355 size_t *key_len );
356/**
Raef Coles8ff6df52021-07-21 12:42:15 +0100357 * \brief This function creates a LMOTS signature, using a
358 * LMOTS context that contains a private key.
359 *
360 * \note Before this function is called, the context must
361 * have been initialized and must contain a private
362 * key.
363 *
364 * \note LMOTS private keys can only be used once, otherwise
365 * attackers may be able to create forged signatures.
366 * If the signing operation is successful, the private
367 * key in the context will be erased, and no further
368 * signing will be possible until another private key
369 * is loaded
370 *
371 * \param ctx The initialized LMOTS context from which the
372 * private key will be read.
373 * \param f_rng The RNG function to be used for signature
374 * generation.
375 * \param p_rng The RNG context to be passed to f_rng
376 * \param msg The buffer from which the message will be read.
Raef Coles01c71a12022-08-31 15:55:00 +0100377 * \param msg_size The size of the message that will be read.
Raef Coles8ff6df52021-07-21 12:42:15 +0100378 * \param sig The buf into which the signature will be stored.
379 * Must be at least #MBEDTLS_LMOTS_SIG_LEN in size.
380 *
381 * \return \c 0 on success.
382 * \return A non-zero error code on failure.
383 */
Raef Coles01c71a12022-08-31 15:55:00 +0100384int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
Raef Coles8ff6df52021-07-21 12:42:15 +0100385 int (*f_rng)(void *, unsigned char *, size_t),
Raef Coles01c71a12022-08-31 15:55:00 +0100386 void *p_rng, const unsigned char *msg, size_t msg_size,
387 unsigned char *sig, size_t sig_size, size_t* sig_len );
Raef Coles8ff6df52021-07-21 12:42:15 +0100388
Raef Colesab4f8742022-09-01 12:24:31 +0100389#endif /* MBEDTLS_LMS_PRIVATE */
Raef Coles8ff6df52021-07-21 12:42:15 +0100390
391#ifdef __cplusplus
392}
393#endif
394
395#endif /* MBEDTLS_LMOTS_H */