blob: 2f42a592cce84bd0e290c6468790a1ab97bdd4d7 [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 Colesab300f12022-09-28 17:12:41 +010033#include "mbedtls/lms.h"
34
Raef Coles8ff6df52021-07-21 12:42:15 +010035#include <stdint.h>
36#include <stddef.h>
37
Raef Coles8ff6df52021-07-21 12:42:15 +010038
Raef Colese9479a02022-09-01 16:06:35 +010039#define MBEDTLS_LMOTS_PUBLIC_KEY_LEN(type) (MBEDTLS_LMOTS_TYPE_LEN + \
40 MBEDTLS_LMOTS_I_KEY_ID_LEN + \
41 MBEDTLS_LMOTS_Q_LEAF_ID_LEN + \
42 MBEDTLS_LMOTS_N_HASH_LEN(type))
Raef Coles8ff6df52021-07-21 12:42:15 +010043
Raef Coles01c71a12022-08-31 15:55:00 +010044#define MBEDTLS_LMOTS_SIG_TYPE_OFFSET (0)
Raef Coles9c9027b2022-09-02 18:26:31 +010045#define MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET (MBEDTLS_LMOTS_SIG_TYPE_OFFSET + \
46 MBEDTLS_LMOTS_TYPE_LEN)
47#define MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(type) (MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET + \
48 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(type))
Raef Coles8ff6df52021-07-21 12:42:15 +010049
50#ifdef __cplusplus
51extern "C" {
52#endif
53
Raef Coles01c71a12022-08-31 15:55:00 +010054
Raef Coles40158e12022-09-27 10:23:53 +010055#if defined(MBEDTLS_TEST_HOOKS)
56extern int( *mbedtls_lmots_sign_private_key_invalidated_hook )( unsigned char * );
57#endif /* defined(MBEDTLS_TEST_HOOKS) */
58
Raef Coles01c71a12022-08-31 15:55:00 +010059/**
60 * \brief This function converts an unsigned int into a
61 * network-byte-order (big endian) string.
62 *
63 * \param val The unsigned integer value
64 * \param len The length of the string.
65 * \param bytes The string to output into.
Raef Coles01c71a12022-08-31 15:55:00 +010066 */
Raef Colesad054252022-09-27 10:59:16 +010067void mbedtls_lms_unsigned_int_to_network_bytes( unsigned int val, size_t len,
68 unsigned char *bytes );
Raef Coles01c71a12022-08-31 15:55:00 +010069
70/**
71 * \brief This function converts a network-byte-order
72 * (big endian) string into an unsigned integer.
73 *
74 * \param len The length of the string.
75 * \param bytes The string.
76 *
77 * \return The corresponding LMS error code.
78 */
Raef Colesad054252022-09-27 10:59:16 +010079unsigned int mbedtls_lms_network_bytes_to_unsigned_int( size_t len,
80 const unsigned char *bytes );
Raef Coles8ff6df52021-07-21 12:42:15 +010081
Raef Colesc8f96042022-08-25 13:49:54 +010082/**
83 * \brief This function converts a \ref psa_status_t to a
84 * low-level LMS error code.
85 *
86 * \param status The psa_status_t to convert
87 *
88 * \return The corresponding LMS error code.
89 */
Raef Coles9b88ee52022-09-02 12:04:21 +010090int mbedtls_lms_error_from_psa( psa_status_t status );
Raef Colesc8f96042022-08-25 13:49:54 +010091
Raef Coles8ff6df52021-07-21 12:42:15 +010092
93/**
Raef Coles01c71a12022-08-31 15:55:00 +010094 * \brief This function initializes a public LMOTS context
Raef Coles8ff6df52021-07-21 12:42:15 +010095 *
96 * \param ctx The uninitialized LMOTS context that will then be
97 * initialized.
98 */
Raef Coles01c71a12022-08-31 15:55:00 +010099void mbedtls_lmots_init_public( mbedtls_lmots_public_t *ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100100
101/**
Raef Coles01c71a12022-08-31 15:55:00 +0100102 * \brief This function uninitializes a public LMOTS context
Raef Coles8ff6df52021-07-21 12:42:15 +0100103 *
104 * \param ctx The initialized LMOTS context that will then be
105 * uninitialized.
106 */
Raef Coles01c71a12022-08-31 15:55:00 +0100107void mbedtls_lmots_free_public( mbedtls_lmots_public_t *ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100108
109/**
Raef Coles01c71a12022-08-31 15:55:00 +0100110 * \brief This function imports an LMOTS public key into a
111 * LMOTS context.
Raef Coles8ff6df52021-07-21 12:42:15 +0100112 *
Raef Coles01c71a12022-08-31 15:55:00 +0100113 * \note Before this function is called, the context must
114 * have been initialized.
Raef Coles8ff6df52021-07-21 12:42:15 +0100115 *
Raef Coles01c71a12022-08-31 15:55:00 +0100116 * \note See IETF RFC8554 for details of the encoding of
117 * this public key.
118 *
119 * \param ctx The initialized LMOTS context store the key in.
120 * \param key The buffer from which the key will be read.
Raef Coles366d67d2022-09-01 17:23:12 +0100121 * #MBEDTLS_LMOTS_PUBLIC_KEY_LEN bytes will be read
122 * from this.
Raef Colesc8f96042022-08-25 13:49:54 +0100123 *
124 * \return \c 0 on success.
125 * \return A non-zero error code on failure.
Raef Coles8ff6df52021-07-21 12:42:15 +0100126 */
Raef Coles01c71a12022-08-31 15:55:00 +0100127int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx,
128 const unsigned char *key, size_t key_size );
Raef Coles8ff6df52021-07-21 12:42:15 +0100129
130/**
131 * \brief This function creates a candidate public key from
132 * an LMOTS signature. This can then be compared to
133 * the real public key to determine the validity of
134 * the signature.
135 *
136 * \note This function is exposed publicly to be used in LMS
137 * signature verification, it is expected that
138 * mbedtls_lmots_verify will be used for LMOTS
139 * signature verification.
140 *
Raef Coles01c71a12022-08-31 15:55:00 +0100141 * \param params The LMOTS parameter set, q and I values as an
142 * mbedtls_lmots_parameters_t struct.
Raef Coles8ff6df52021-07-21 12:42:15 +0100143 * \param msg The buffer from which the message will be read.
Raef Coles01c71a12022-08-31 15:55:00 +0100144 * \param msg_size The size of the message that will be read.
Raef Coles2ad6e612022-08-24 13:33:35 +0100145 * \param sig The buffer from which the signature will be read.
Raef Coles366d67d2022-09-01 17:23:12 +0100146 * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from
147 * this.
Raef Coles8ff6df52021-07-21 12:42:15 +0100148 * \param out The buffer where the candidate public key will be
149 * stored. Must be at least #MBEDTLS_LMOTS_N_HASH_LEN
150 * bytes in size.
151 *
152 * \return \c 0 on success.
153 * \return A non-zero error code on failure.
154 */
Raef Coles01c71a12022-08-31 15:55:00 +0100155int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters_t *params,
156 const unsigned char *msg,
157 size_t msg_size,
158 const unsigned char *sig,
159 size_t sig_size,
160 unsigned char *out,
161 size_t out_size,
Raef Coles9b88ee52022-09-02 12:04:21 +0100162 size_t *out_len );
Raef Coles8ff6df52021-07-21 12:42:15 +0100163
164/**
Raef Coles01c71a12022-08-31 15:55:00 +0100165 * \brief This function verifies a LMOTS signature, using a
166 * LMOTS context that contains a public key.
167 *
168 * \warning This function is **not intended for use in
169 * production**, due to as-yet unsolved problems with
170 * handling stateful keys.
171 *
172 * \note Before this function is called, the context must
173 * have been initialized and must contain a public key
Raef Coles366d67d2022-09-01 17:23:12 +0100174 * (either by import or calculation from a private
175 * key).
Raef Coles01c71a12022-08-31 15:55:00 +0100176 *
177 * \param ctx The initialized LMOTS context from which the public
178 * key will be read.
179 * \param msg The buffer from which the message will be read.
180 * \param msg_size The size of the message that will be read.
181 * \param sig The buf from which the signature will be read.
182 * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from
183 * this.
184 *
185 * \return \c 0 on successful verification.
186 * \return A non-zero error code on failure.
187 */
Raef Coles2ac352a2022-10-07 11:12:27 +0100188int mbedtls_lmots_verify( const mbedtls_lmots_public_t *ctx,
189 const unsigned char *msg,
Raef Coles01c71a12022-08-31 15:55:00 +0100190 size_t msg_size, const unsigned char *sig,
191 size_t sig_size );
192
Raef Coles5127e852022-10-07 10:35:56 +0100193#if defined(MBEDTLS_LMS_PRIVATE)
Raef Colesab4f8742022-09-01 12:24:31 +0100194
Raef Coles01c71a12022-08-31 15:55:00 +0100195/**
196 * \brief This function initializes a private LMOTS context
197 *
198 * \param ctx The uninitialized LMOTS context that will then be
199 * initialized.
200 */
201void mbedtls_lmots_init_private( mbedtls_lmots_private_t *ctx );
202
203/**
204 * \brief This function uninitializes a private LMOTS context
205 *
206 * \param ctx The initialized LMOTS context that will then be
207 * uninitialized.
208 */
209void mbedtls_lmots_free_private( mbedtls_lmots_private_t *ctx );
210
211/**
Raef Coles2ac352a2022-10-07 11:12:27 +0100212 * \brief This function calculates an LMOTS private key, and
Raef Coles01c71a12022-08-31 15:55:00 +0100213 * stores in into an LMOTS context.
214 *
215 * \warning This function is **not intended for use in
216 * production**, due to as-yet unsolved problems with
217 * handling stateful keys.
218 *
219 * \note The seed must have at least 256 bits of entropy.
220 *
221 * \param ctx The initialized LMOTS context to generate the key
222 * into.
223 * \param I_key_identifier The key identifier of the key, as a 16-byte string.
224 * \param q_leaf_identifier The leaf identifier of key. If this LMOTS key is
225 * not being used as part of an LMS key, this should
226 * be set to 0.
227 * \param seed The seed used to deterministically generate the
228 * key.
229 * \param seed_size The length of the seed.
230 *
231 * \return \c 0 on success.
232 * \return A non-zero error code on failure.
233 */
234int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx,
235 mbedtls_lmots_algorithm_type_t type,
236 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
237 uint32_t q_leaf_identifier,
238 const unsigned char *seed,
239 size_t seed_size );
240
241/**
242 * \brief This function generates an LMOTS public key from a
243 * LMOTS context that already contains a private key.
244 *
245 * \note Before this function is called, the context must
246 * have been initialized and the context must contain
247 * a private key.
248 *
249 * \param ctx The initialized LMOTS context to generate the key
250 * from and store it into.
251 *
252 * \return \c 0 on success.
253 * \return A non-zero error code on failure.
254 */
255int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx,
Raef Coles2ac352a2022-10-07 11:12:27 +0100256 const mbedtls_lmots_private_t *priv_ctx );
Raef Coles01c71a12022-08-31 15:55:00 +0100257
258
259/**
260 * \brief This function exports an LMOTS public key from a
261 * LMOTS context that already contains a public key.
262 *
263 * \note Before this function is called, the context must
264 * have been initialized and the context must contain
265 * a public key.
266 *
267 * \note See IETF RFC8554 for details of the encoding of
268 * this public key.
269 *
270 * \param ctx The initialized LMOTS context that contains the
271 * publc key.
272 * \param key The buffer into which the key will be output. Must
273 * be at least #MBEDTLS_LMOTS_PUBLIC_KEY_LEN in size.
274 *
275 * \return \c 0 on success.
276 * \return A non-zero error code on failure.
277 */
Raef Coles2ac352a2022-10-07 11:12:27 +0100278int mbedtls_lmots_export_public_key( const mbedtls_lmots_public_t *ctx,
Raef Coles01c71a12022-08-31 15:55:00 +0100279 unsigned char *key, size_t key_size,
280 size_t *key_len );
281/**
Raef Coles8ff6df52021-07-21 12:42:15 +0100282 * \brief This function creates a LMOTS signature, using a
283 * LMOTS context that contains a private key.
284 *
285 * \note Before this function is called, the context must
286 * have been initialized and must contain a private
287 * key.
288 *
289 * \note LMOTS private keys can only be used once, otherwise
290 * attackers may be able to create forged signatures.
291 * If the signing operation is successful, the private
292 * key in the context will be erased, and no further
293 * signing will be possible until another private key
294 * is loaded
295 *
296 * \param ctx The initialized LMOTS context from which the
297 * private key will be read.
298 * \param f_rng The RNG function to be used for signature
299 * generation.
300 * \param p_rng The RNG context to be passed to f_rng
301 * \param msg The buffer from which the message will be read.
Raef Coles01c71a12022-08-31 15:55:00 +0100302 * \param msg_size The size of the message that will be read.
Raef Coles8ff6df52021-07-21 12:42:15 +0100303 * \param sig The buf into which the signature will be stored.
304 * Must be at least #MBEDTLS_LMOTS_SIG_LEN in size.
305 *
306 * \return \c 0 on success.
307 * \return A non-zero error code on failure.
308 */
Raef Coles01c71a12022-08-31 15:55:00 +0100309int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
Raef Coles8ff6df52021-07-21 12:42:15 +0100310 int (*f_rng)(void *, unsigned char *, size_t),
Raef Coles01c71a12022-08-31 15:55:00 +0100311 void *p_rng, const unsigned char *msg, size_t msg_size,
312 unsigned char *sig, size_t sig_size, size_t* sig_len );
Raef Coles8ff6df52021-07-21 12:42:15 +0100313
Raef Coles5127e852022-10-07 10:35:56 +0100314#endif /* defined(MBEDTLS_LMS_PRIVATE) */
Raef Coles8ff6df52021-07-21 12:42:15 +0100315
316#ifdef __cplusplus
317}
318#endif
319
320#endif /* MBEDTLS_LMOTS_H */