blob: db945941cbe1e3ae611214d1ddd8bd3160b52ee3 [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 Colese9479a02022-09-01 16:06:35 +010038#define MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(type) (type == MBEDTLS_LMOTS_SHA256_N32_W8 ? 34u : 0)
39#define MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(type) (MBEDTLS_LMOTS_N_HASH_LEN(type))
40#define MBEDTLS_LMOTS_TYPE_LEN (4u)
Raef Coles8ff6df52021-07-21 12:42:15 +010041
Raef Colese9479a02022-09-01 16:06:35 +010042#define MBEDTLS_LMOTS_SIG_LEN(type) (MBEDTLS_LMOTS_TYPE_LEN + \
43 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(type) + \
44 (MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(type) * \
45 MBEDTLS_LMOTS_N_HASH_LEN(type)))
Raef Coles8ff6df52021-07-21 12:42:15 +010046
Raef Colese9479a02022-09-01 16:06:35 +010047#define MBEDTLS_LMOTS_PUBLIC_KEY_LEN(type) (MBEDTLS_LMOTS_TYPE_LEN + \
48 MBEDTLS_LMOTS_I_KEY_ID_LEN + \
49 MBEDTLS_LMOTS_Q_LEAF_ID_LEN + \
50 MBEDTLS_LMOTS_N_HASH_LEN(type))
Raef Coles8ff6df52021-07-21 12:42:15 +010051
Raef Coles01c71a12022-08-31 15:55:00 +010052#define MBEDTLS_LMOTS_SIG_TYPE_OFFSET (0)
Raef Coles9c9027b2022-09-02 18:26:31 +010053#define MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET (MBEDTLS_LMOTS_SIG_TYPE_OFFSET + \
54 MBEDTLS_LMOTS_TYPE_LEN)
55#define MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(type) (MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET + \
56 MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(type))
Raef Coles8ff6df52021-07-21 12:42:15 +010057
58#ifdef __cplusplus
59extern "C" {
60#endif
61
Raef Coles01c71a12022-08-31 15:55:00 +010062
Raef Coles40158e12022-09-27 10:23:53 +010063#if defined(MBEDTLS_TEST_HOOKS)
64extern int( *mbedtls_lmots_sign_private_key_invalidated_hook )( unsigned char * );
65#endif /* defined(MBEDTLS_TEST_HOOKS) */
66
Raef Coles01c71a12022-08-31 15:55:00 +010067/**
68 * \brief This function converts an unsigned int into a
69 * network-byte-order (big endian) string.
70 *
71 * \param val The unsigned integer value
72 * \param len The length of the string.
73 * \param bytes The string to output into.
Raef Coles01c71a12022-08-31 15:55:00 +010074 */
Raef Colesad054252022-09-27 10:59:16 +010075void mbedtls_lms_unsigned_int_to_network_bytes( unsigned int val, size_t len,
76 unsigned char *bytes );
Raef Coles01c71a12022-08-31 15:55:00 +010077
78/**
79 * \brief This function converts a network-byte-order
80 * (big endian) string into an unsigned integer.
81 *
82 * \param len The length of the string.
83 * \param bytes The string.
84 *
85 * \return The corresponding LMS error code.
86 */
Raef Colesad054252022-09-27 10:59:16 +010087unsigned int mbedtls_lms_network_bytes_to_unsigned_int( size_t len,
88 const unsigned char *bytes );
Raef Coles8ff6df52021-07-21 12:42:15 +010089
Raef Colesc8f96042022-08-25 13:49:54 +010090/**
91 * \brief This function converts a \ref psa_status_t to a
92 * low-level LMS error code.
93 *
94 * \param status The psa_status_t to convert
95 *
96 * \return The corresponding LMS error code.
97 */
Raef Coles9b88ee52022-09-02 12:04:21 +010098int mbedtls_lms_error_from_psa( psa_status_t status );
Raef Colesc8f96042022-08-25 13:49:54 +010099
Raef Coles8ff6df52021-07-21 12:42:15 +0100100
101/**
Raef Coles01c71a12022-08-31 15:55:00 +0100102 * \brief This function initializes a public LMOTS context
Raef Coles8ff6df52021-07-21 12:42:15 +0100103 *
104 * \param ctx The uninitialized LMOTS context that will then be
105 * initialized.
106 */
Raef Coles01c71a12022-08-31 15:55:00 +0100107void mbedtls_lmots_init_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 uninitializes a public LMOTS context
Raef Coles8ff6df52021-07-21 12:42:15 +0100111 *
112 * \param ctx The initialized LMOTS context that will then be
113 * uninitialized.
114 */
Raef Coles01c71a12022-08-31 15:55:00 +0100115void mbedtls_lmots_free_public( mbedtls_lmots_public_t *ctx );
Raef Coles8ff6df52021-07-21 12:42:15 +0100116
117/**
Raef Coles01c71a12022-08-31 15:55:00 +0100118 * \brief This function imports an LMOTS public key into a
119 * LMOTS context.
Raef Coles8ff6df52021-07-21 12:42:15 +0100120 *
Raef Coles01c71a12022-08-31 15:55:00 +0100121 * \note Before this function is called, the context must
122 * have been initialized.
Raef Coles8ff6df52021-07-21 12:42:15 +0100123 *
Raef Coles01c71a12022-08-31 15:55:00 +0100124 * \note See IETF RFC8554 for details of the encoding of
125 * this public key.
126 *
127 * \param ctx The initialized LMOTS context store the key in.
128 * \param key The buffer from which the key will be read.
Raef Coles366d67d2022-09-01 17:23:12 +0100129 * #MBEDTLS_LMOTS_PUBLIC_KEY_LEN bytes will be read
130 * from this.
Raef Colesc8f96042022-08-25 13:49:54 +0100131 *
132 * \return \c 0 on success.
133 * \return A non-zero error code on failure.
Raef Coles8ff6df52021-07-21 12:42:15 +0100134 */
Raef Coles01c71a12022-08-31 15:55:00 +0100135int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx,
136 const unsigned char *key, size_t key_size );
Raef Coles8ff6df52021-07-21 12:42:15 +0100137
138/**
139 * \brief This function creates a candidate public key from
140 * an LMOTS signature. This can then be compared to
141 * the real public key to determine the validity of
142 * the signature.
143 *
144 * \note This function is exposed publicly to be used in LMS
145 * signature verification, it is expected that
146 * mbedtls_lmots_verify will be used for LMOTS
147 * signature verification.
148 *
Raef Coles01c71a12022-08-31 15:55:00 +0100149 * \param params The LMOTS parameter set, q and I values as an
150 * mbedtls_lmots_parameters_t struct.
Raef Coles8ff6df52021-07-21 12:42:15 +0100151 * \param msg The buffer from which the message will be read.
Raef Coles01c71a12022-08-31 15:55:00 +0100152 * \param msg_size The size of the message that will be read.
Raef Coles2ad6e612022-08-24 13:33:35 +0100153 * \param sig The buffer from which the signature will be read.
Raef Coles366d67d2022-09-01 17:23:12 +0100154 * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from
155 * this.
Raef Coles8ff6df52021-07-21 12:42:15 +0100156 * \param out The buffer where the candidate public key will be
157 * stored. Must be at least #MBEDTLS_LMOTS_N_HASH_LEN
158 * bytes in size.
159 *
160 * \return \c 0 on success.
161 * \return A non-zero error code on failure.
162 */
Raef Coles01c71a12022-08-31 15:55:00 +0100163int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters_t *params,
164 const unsigned char *msg,
165 size_t msg_size,
166 const unsigned char *sig,
167 size_t sig_size,
168 unsigned char *out,
169 size_t out_size,
Raef Coles9b88ee52022-09-02 12:04:21 +0100170 size_t *out_len );
Raef Coles8ff6df52021-07-21 12:42:15 +0100171
172/**
Raef Coles01c71a12022-08-31 15:55:00 +0100173 * \brief This function verifies a LMOTS signature, using a
174 * LMOTS context that contains a public key.
175 *
176 * \warning This function is **not intended for use in
177 * production**, due to as-yet unsolved problems with
178 * handling stateful keys.
179 *
180 * \note Before this function is called, the context must
181 * have been initialized and must contain a public key
Raef Coles366d67d2022-09-01 17:23:12 +0100182 * (either by import or calculation from a private
183 * key).
Raef Coles01c71a12022-08-31 15:55:00 +0100184 *
185 * \param ctx The initialized LMOTS context from which the public
186 * key will be read.
187 * \param msg The buffer from which the message will be read.
188 * \param msg_size The size of the message that will be read.
189 * \param sig The buf from which the signature will be read.
190 * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from
191 * this.
192 *
193 * \return \c 0 on successful verification.
194 * \return A non-zero error code on failure.
195 */
196int mbedtls_lmots_verify( mbedtls_lmots_public_t *ctx, const unsigned char *msg,
197 size_t msg_size, const unsigned char *sig,
198 size_t sig_size );
199
Raef Colesab4f8742022-09-01 12:24:31 +0100200#ifdef MBEDTLS_LMS_PRIVATE
201
Raef Coles01c71a12022-08-31 15:55:00 +0100202/**
203 * \brief This function initializes a private LMOTS context
204 *
205 * \param ctx The uninitialized LMOTS context that will then be
206 * initialized.
207 */
208void mbedtls_lmots_init_private( mbedtls_lmots_private_t *ctx );
209
210/**
211 * \brief This function uninitializes a private LMOTS context
212 *
213 * \param ctx The initialized LMOTS context that will then be
214 * uninitialized.
215 */
216void mbedtls_lmots_free_private( mbedtls_lmots_private_t *ctx );
217
218/**
219 * \brief This function generates an LMOTS private key, and
220 * stores in into an LMOTS context.
221 *
222 * \warning This function is **not intended for use in
223 * production**, due to as-yet unsolved problems with
224 * handling stateful keys.
225 *
226 * \note The seed must have at least 256 bits of entropy.
227 *
228 * \param ctx The initialized LMOTS context to generate the key
229 * into.
230 * \param I_key_identifier The key identifier of the key, as a 16-byte string.
231 * \param q_leaf_identifier The leaf identifier of key. If this LMOTS key is
232 * not being used as part of an LMS key, this should
233 * be set to 0.
234 * \param seed The seed used to deterministically generate the
235 * key.
236 * \param seed_size The length of the seed.
237 *
238 * \return \c 0 on success.
239 * \return A non-zero error code on failure.
240 */
241int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx,
242 mbedtls_lmots_algorithm_type_t type,
243 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
244 uint32_t q_leaf_identifier,
245 const unsigned char *seed,
246 size_t seed_size );
247
248/**
249 * \brief This function generates an LMOTS public key from a
250 * LMOTS context that already contains a private key.
251 *
252 * \note Before this function is called, the context must
253 * have been initialized and the context must contain
254 * a private key.
255 *
256 * \param ctx The initialized LMOTS context to generate the key
257 * from and store it into.
258 *
259 * \return \c 0 on success.
260 * \return A non-zero error code on failure.
261 */
262int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx,
Raef Coles9b88ee52022-09-02 12:04:21 +0100263 mbedtls_lmots_private_t *priv_ctx );
Raef Coles01c71a12022-08-31 15:55:00 +0100264
265
266/**
267 * \brief This function exports an LMOTS public key from a
268 * LMOTS context that already contains a public key.
269 *
270 * \note Before this function is called, the context must
271 * have been initialized and the context must contain
272 * a public key.
273 *
274 * \note See IETF RFC8554 for details of the encoding of
275 * this public key.
276 *
277 * \param ctx The initialized LMOTS context that contains the
278 * publc key.
279 * \param key The buffer into which the key will be output. Must
280 * be at least #MBEDTLS_LMOTS_PUBLIC_KEY_LEN in size.
281 *
282 * \return \c 0 on success.
283 * \return A non-zero error code on failure.
284 */
285int mbedtls_lmots_export_public_key( mbedtls_lmots_public_t *ctx,
286 unsigned char *key, size_t key_size,
287 size_t *key_len );
288/**
Raef Coles8ff6df52021-07-21 12:42:15 +0100289 * \brief This function creates a LMOTS signature, using a
290 * LMOTS context that contains a private key.
291 *
292 * \note Before this function is called, the context must
293 * have been initialized and must contain a private
294 * key.
295 *
296 * \note LMOTS private keys can only be used once, otherwise
297 * attackers may be able to create forged signatures.
298 * If the signing operation is successful, the private
299 * key in the context will be erased, and no further
300 * signing will be possible until another private key
301 * is loaded
302 *
303 * \param ctx The initialized LMOTS context from which the
304 * private key will be read.
305 * \param f_rng The RNG function to be used for signature
306 * generation.
307 * \param p_rng The RNG context to be passed to f_rng
308 * \param msg The buffer from which the message will be read.
Raef Coles01c71a12022-08-31 15:55:00 +0100309 * \param msg_size The size of the message that will be read.
Raef Coles8ff6df52021-07-21 12:42:15 +0100310 * \param sig The buf into which the signature will be stored.
311 * Must be at least #MBEDTLS_LMOTS_SIG_LEN in size.
312 *
313 * \return \c 0 on success.
314 * \return A non-zero error code on failure.
315 */
Raef Coles01c71a12022-08-31 15:55:00 +0100316int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx,
Raef Coles8ff6df52021-07-21 12:42:15 +0100317 int (*f_rng)(void *, unsigned char *, size_t),
Raef Coles01c71a12022-08-31 15:55:00 +0100318 void *p_rng, const unsigned char *msg, size_t msg_size,
319 unsigned char *sig, size_t sig_size, size_t* sig_len );
Raef Coles8ff6df52021-07-21 12:42:15 +0100320
Raef Colesab4f8742022-09-01 12:24:31 +0100321#endif /* MBEDTLS_LMS_PRIVATE */
Raef Coles8ff6df52021-07-21 12:42:15 +0100322
323#ifdef __cplusplus
324}
325#endif
326
327#endif /* MBEDTLS_LMOTS_H */