blob: e784bf586ede2924328478c3573ac7078ab033ad [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
Raef Colesab4f8742022-09-01 12:24:31 +0100104#ifdef MBEDTLS_LMS_PRIVATE
Raef Coles01c71a12022-08-31 15:55:00 +0100105/** LMOTS private context structure.
106 *
107 * A LMOTS private key is one hash output for each of digit of the digest +
108 * checksum, and the applicable parameter set.
109 *
110 * The context must be initialized before it is used. A public key must either
111 * be imported or generated from a private context.
112 *
113 * \dot
114 * digraph lmots_public_t {
115 * UNINITIALIZED -> INIT [label="init"];
116 * HAVE_PRIVATE_KEY -> INIT [label="free"];
117 * INIT -> HAVE_PRIVATE_KEY [label="generate_private_key"];
118 * HAVE_PRIVATE_KEY -> INIT [label="sign"];
119 * }
120 * \enddot
121 */
122typedef struct {
123 mbedtls_lmots_parameters_t MBEDTLS_PRIVATE(params);
124 unsigned char MBEDTLS_PRIVATE(private_key)[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][32];
125 unsigned char MBEDTLS_PRIVATE(have_private_key); /*!< Whether the context contains a private key.
Raef Colesc4647462022-06-15 12:17:51 +0100126 Boolean values only. */
Raef Coles01c71a12022-08-31 15:55:00 +0100127} mbedtls_lmots_private_t;
Raef Colesab4f8742022-09-01 12:24:31 +0100128#endif /* MBEDTLS_LMS_PRIVATE */
Raef Coles01c71a12022-08-31 15:55:00 +0100129
130/**
131 * \brief This function converts an unsigned int into a
132 * network-byte-order (big endian) string.
133 *
134 * \param val The unsigned integer value
135 * \param len The length of the string.
136 * \param bytes The string to output into.
137 *
138 * \return The corresponding LMS error code.
139 */
140void unsigned_int_to_network_bytes(unsigned int val, size_t len, unsigned char *bytes);
141
142/**
143 * \brief This function converts a network-byte-order
144 * (big endian) string into an unsigned integer.
145 *
146 * \param len The length of the string.
147 * \param bytes The string.
148 *
149 * \return The corresponding LMS error code.
150 */
151unsigned int network_bytes_to_unsigned_int(size_t len, const unsigned char *bytes);
Raef Coles8ff6df52021-07-21 12:42:15 +0100152
Raef Colesc8f96042022-08-25 13:49:54 +0100153/**
154 * \brief This function converts a \ref psa_status_t to a
155 * low-level LMS error code.
156 *
157 * \param status The psa_status_t to convert
158 *
159 * \return The corresponding LMS error code.
160 */
161int mbedtls_lms_error_from_psa(psa_status_t status);
162
Raef Coles8ff6df52021-07-21 12:42:15 +0100163
164/**
Raef Coles01c71a12022-08-31 15:55:00 +0100165 * \brief This function initializes a public LMOTS context
Raef Coles8ff6df52021-07-21 12:42:15 +0100166 *
167 * \param ctx The uninitialized LMOTS context that will then be
168 * initialized.
169 */
Raef Coles01c71a12022-08-31 15:55:00 +0100170void mbedtls_lmots_init_public( mbedtls_lmots_public_t *ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100171
172/**
Raef Coles01c71a12022-08-31 15:55:00 +0100173 * \brief This function uninitializes a public LMOTS context
Raef Coles8ff6df52021-07-21 12:42:15 +0100174 *
175 * \param ctx The initialized LMOTS context that will then be
176 * uninitialized.
177 */
Raef Coles01c71a12022-08-31 15:55:00 +0100178void mbedtls_lmots_free_public( mbedtls_lmots_public_t *ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100179
180/**
Raef Coles01c71a12022-08-31 15:55:00 +0100181 * \brief This function imports an LMOTS public key into a
182 * LMOTS context.
Raef Coles8ff6df52021-07-21 12:42:15 +0100183 *
Raef Coles01c71a12022-08-31 15:55:00 +0100184 * \note Before this function is called, the context must
185 * have been initialized.
Raef Coles8ff6df52021-07-21 12:42:15 +0100186 *
Raef Coles01c71a12022-08-31 15:55:00 +0100187 * \note See IETF RFC8554 for details of the encoding of
188 * this public key.
189 *
190 * \param ctx The initialized LMOTS context store the key in.
191 * \param key The buffer from which the key will be read.
192 * #MBEDTLS_LMOTS_PUBLIC_KEY_LEN bytes will be read from
193 * this.
Raef Colesc8f96042022-08-25 13:49:54 +0100194 *
195 * \return \c 0 on success.
196 * \return A non-zero error code on failure.
Raef Coles8ff6df52021-07-21 12:42:15 +0100197 */
Raef Coles01c71a12022-08-31 15:55:00 +0100198int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx,
199 const unsigned char *key, size_t key_size );
Raef Coles8ff6df52021-07-21 12:42:15 +0100200
201/**
202 * \brief This function creates a candidate public key from
203 * an LMOTS signature. This can then be compared to
204 * the real public key to determine the validity of
205 * the signature.
206 *
207 * \note This function is exposed publicly to be used in LMS
208 * signature verification, it is expected that
209 * mbedtls_lmots_verify will be used for LMOTS
210 * signature verification.
211 *
Raef Coles01c71a12022-08-31 15:55:00 +0100212 * \param params The LMOTS parameter set, q and I values as an
213 * mbedtls_lmots_parameters_t struct.
Raef Coles8ff6df52021-07-21 12:42:15 +0100214 * \param msg The buffer from which the message will be read.
Raef Coles01c71a12022-08-31 15:55:00 +0100215 * \param msg_size The size of the message that will be read.
Raef Coles2ad6e612022-08-24 13:33:35 +0100216 * \param sig The buffer from which the signature will be read.
217 * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from this.
Raef Coles8ff6df52021-07-21 12:42:15 +0100218 * \param out The buffer where the candidate public key will be
219 * stored. Must be at least #MBEDTLS_LMOTS_N_HASH_LEN
220 * bytes in size.
221 *
222 * \return \c 0 on success.
223 * \return A non-zero error code on failure.
224 */
Raef Coles01c71a12022-08-31 15:55:00 +0100225int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters_t *params,
226 const unsigned char *msg,
227 size_t msg_size,
228 const unsigned char *sig,
229 size_t sig_size,
230 unsigned char *out,
231 size_t out_size,
232 size_t *out_len);
Raef Coles8ff6df52021-07-21 12:42:15 +0100233
234/**
Raef Coles01c71a12022-08-31 15:55:00 +0100235 * \brief This function verifies a LMOTS signature, using a
236 * LMOTS context that contains a public key.
237 *
238 * \warning This function is **not intended for use in
239 * production**, due to as-yet unsolved problems with
240 * handling stateful keys.
241 *
242 * \note Before this function is called, the context must
243 * have been initialized and must contain a public key
244 * (either by import or calculation from a private key).
245 *
246 * \param ctx The initialized LMOTS context from which the public
247 * key will be read.
248 * \param msg The buffer from which the message will be read.
249 * \param msg_size The size of the message that will be read.
250 * \param sig The buf from which the signature will be read.
251 * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from
252 * this.
253 *
254 * \return \c 0 on successful verification.
255 * \return A non-zero error code on failure.
256 */
257int mbedtls_lmots_verify( mbedtls_lmots_public_t *ctx, const unsigned char *msg,
258 size_t msg_size, const unsigned char *sig,
259 size_t sig_size );
260
Raef Colesab4f8742022-09-01 12:24:31 +0100261#ifdef MBEDTLS_LMS_PRIVATE
262
Raef Coles01c71a12022-08-31 15:55:00 +0100263/**
264 * \brief This function initializes a private LMOTS context
265 *
266 * \param ctx The uninitialized LMOTS context that will then be
267 * initialized.
268 */
269void mbedtls_lmots_init_private( mbedtls_lmots_private_t *ctx );
270
271/**
272 * \brief This function uninitializes a private LMOTS context
273 *
274 * \param ctx The initialized LMOTS context that will then be
275 * uninitialized.
276 */
277void mbedtls_lmots_free_private( mbedtls_lmots_private_t *ctx );
278
279/**
280 * \brief This function generates an LMOTS private key, and
281 * stores in into an LMOTS context.
282 *
283 * \warning This function is **not intended for use in
284 * production**, due to as-yet unsolved problems with
285 * handling stateful keys.
286 *
287 * \note The seed must have at least 256 bits of entropy.
288 *
289 * \param ctx The initialized LMOTS context to generate the key
290 * into.
291 * \param I_key_identifier The key identifier of the key, as a 16-byte string.
292 * \param q_leaf_identifier The leaf identifier of key. If this LMOTS key is
293 * not being used as part of an LMS key, this should
294 * be set to 0.
295 * \param seed The seed used to deterministically generate the
296 * key.
297 * \param seed_size The length of the seed.
298 *
299 * \return \c 0 on success.
300 * \return A non-zero error code on failure.
301 */
302int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx,
303 mbedtls_lmots_algorithm_type_t type,
304 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
305 uint32_t q_leaf_identifier,
306 const unsigned char *seed,
307 size_t seed_size );
308
309/**
310 * \brief This function generates an LMOTS public key from a
311 * LMOTS context that already contains a private key.
312 *
313 * \note Before this function is called, the context must
314 * have been initialized and the context must contain
315 * a private key.
316 *
317 * \param ctx The initialized LMOTS context to generate the key
318 * from and store it into.
319 *
320 * \return \c 0 on success.
321 * \return A non-zero error code on failure.
322 */
323int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx,
324 mbedtls_lmots_private_t *priv_ctx);
325
326
327/**
328 * \brief This function exports an LMOTS public key from a
329 * LMOTS context that already contains a public key.
330 *
331 * \note Before this function is called, the context must
332 * have been initialized and the context must contain
333 * a public key.
334 *
335 * \note See IETF RFC8554 for details of the encoding of
336 * this public key.
337 *
338 * \param ctx The initialized LMOTS context that contains the
339 * publc key.
340 * \param key The buffer into which the key will be output. Must
341 * be at least #MBEDTLS_LMOTS_PUBLIC_KEY_LEN in size.
342 *
343 * \return \c 0 on success.
344 * \return A non-zero error code on failure.
345 */
346int mbedtls_lmots_export_public_key( mbedtls_lmots_public_t *ctx,
347 unsigned char *key, size_t key_size,
348 size_t *key_len );
349/**
Raef Coles8ff6df52021-07-21 12:42:15 +0100350 * \brief This function creates a LMOTS signature, using a
351 * LMOTS context that contains a private key.
352 *
353 * \note Before this function is called, the context must
354 * have been initialized and must contain a private
355 * key.
356 *
357 * \note LMOTS private keys can only be used once, otherwise
358 * attackers may be able to create forged signatures.
359 * If the signing operation is successful, the private
360 * key in the context will be erased, and no further
361 * signing will be possible until another private key
362 * is loaded
363 *
364 * \param ctx The initialized LMOTS context from which the
365 * private key will be read.
366 * \param f_rng The RNG function to be used for signature
367 * generation.
368 * \param p_rng The RNG context to be passed to f_rng
369 * \param msg The buffer from which the message will be read.
Raef Coles01c71a12022-08-31 15:55:00 +0100370 * \param msg_size The size of the message that will be read.
Raef Coles8ff6df52021-07-21 12:42:15 +0100371 * \param sig The buf into which the signature will be stored.
372 * Must be at least #MBEDTLS_LMOTS_SIG_LEN in size.
373 *
374 * \return \c 0 on success.
375 * \return A non-zero error code on failure.
376 */
Raef Coles01c71a12022-08-31 15:55:00 +0100377int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
Raef Coles8ff6df52021-07-21 12:42:15 +0100378 int (*f_rng)(void *, unsigned char *, size_t),
Raef Coles01c71a12022-08-31 15:55:00 +0100379 void *p_rng, const unsigned char *msg, size_t msg_size,
380 unsigned char *sig, size_t sig_size, size_t* sig_len );
Raef Coles8ff6df52021-07-21 12:42:15 +0100381
Raef Colesab4f8742022-09-01 12:24:31 +0100382#endif /* MBEDTLS_LMS_PRIVATE */
Raef Coles8ff6df52021-07-21 12:42:15 +0100383
384#ifdef __cplusplus
385}
386#endif
387
388#endif /* MBEDTLS_LMOTS_H */