blob: a177ad4a577832b8276819d27fb10df8f0ed3ba4 [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
5 * public-key signature scheme.
6 */
7/*
8 * Copyright The Mbed TLS Contributors
9 * SPDX-License-Identifier: Apache-2.0
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License"); you may
12 * not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
19 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23
24#ifndef MBEDTLS_LMOTS_H
25#define MBEDTLS_LMOTS_H
26
27#include "mbedtls/private_access.h"
28
29#include <stdint.h>
30#include <stddef.h>
31
32#define MBEDTLS_ERR_LMOTS_BAD_INPUT_DATA -0x0076 /**< Bad data has been input to an LMOTS function */
33#define MBEDTLS_ERR_LMOTS_VERIFY_FAILED -0x0078 /**< LMOTS signature verification failed */
34
35#define MBEDTLS_LMOTS_N_HASH_LEN (32)
36#define MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN (34)
37#define MBEDTLS_LMOTS_TYPE_LEN (4)
38#define MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN (MBEDTLS_LMOTS_N_HASH_LEN)
39#define MBEDTLS_LMOTS_I_KEY_ID_LEN (16)
40#define MBEDTLS_LMOTS_Q_LEAF_ID_LEN (4)
41
42#define MBEDTLS_LMOTS_SIG_LEN (MBEDTLS_LMOTS_TYPE_LEN + MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN + \
43 (MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN * MBEDTLS_LMOTS_N_HASH_LEN))
44
45#define MBEDTLS_LMOTS_PUBKEY_LEN (MBEDTLS_LMOTS_TYPE_LEN + MBEDTLS_LMOTS_I_KEY_ID_LEN + \
46 MBEDTLS_LMOTS_Q_LEAF_ID_LEN + MBEDTLS_LMOTS_N_HASH_LEN)
47
48#define MBEDTLS_LMOTS_SIG_TYPE_OFFSET (0)
49#define MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET (MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN)
50#define MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET (MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET + MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN)
51
52#define MBEDTLS_LMOTS_PUBKEY_TYPE_OFFSET (0)
53#define MBEDTLS_LMOTS_PUBKEY_I_KEY_ID_OFFSET (MBEDTLS_LMOTS_PUBKEY_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN)
54#define MBEDTLS_LMOTS_PUBKEY_Q_LEAF_ID_OFFSET (MBEDTLS_LMOTS_PUBKEY_I_KEY_ID_OFFSET + MBEDTLS_LMOTS_I_KEY_ID_LEN)
55#define MBEDTLS_LMOTS_PUBKEY_KEY_HASH_OFFSET (MBEDTLS_LMOTS_PUBKEY_Q_LEAF_ID_OFFSET + MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
56
57
58#ifdef __cplusplus
59extern "C" {
60#endif
61
62/* We are only implementing a subset of the types, particularly n32_w8, for the
63 * sake of simplicty
64 */
65typedef enum {
66 MBEDTLS_LMOTS_SHA256_N32_W8 = 4
67} mbedtls_lmots_algorithm_type_t;
68
69
70typedef struct {
71 unsigned char MBEDTLS_PRIVATE(have_privkey);
72 unsigned char MBEDTLS_PRIVATE(have_pubkey);
73 unsigned char MBEDTLS_PRIVATE(I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN]);
74 unsigned int MBEDTLS_PRIVATE(q_leaf_identifier);
75 unsigned char MBEDTLS_PRIVATE(q_leaf_identifier_bytes)[MBEDTLS_LMOTS_Q_LEAF_ID_LEN];
76 mbedtls_lmots_algorithm_type_t MBEDTLS_PRIVATE(type);
77 unsigned char MBEDTLS_PRIVATE(priv_key[MBEDTLS_LMOTS_P_SIG_SYMBOL_LEN][32]);
78 unsigned char MBEDTLS_PRIVATE(pub_key[32]);
79} mbedtls_lmots_context;
80
81
82/**
83 * \brief This function initializes an LMOTS context
84 *
85 * \param ctx The uninitialized LMOTS context that will then be
86 * initialized.
87 */
88void mbedtls_lmots_init( mbedtls_lmots_context *ctx );
89
90/**
91 * \brief This function uninitializes an LMOTS context
92 *
93 * \param ctx The initialized LMOTS context that will then be
94 * uninitialized.
95 */
96void mbedtls_lmots_free( mbedtls_lmots_context *ctx );
97
98/**
99 * \brief This function sets the type of an LMOTS context
100 *
101 * \note The parameter set in the context will then be used
102 * for keygen operations etc.
103 *
104 * \param ctx The initialized LMOTS context.
105 * \param type The type that will be set in the context.
106 */
107int mbedtls_lmots_set_algorithm_type( mbedtls_lmots_context *ctx,
108 mbedtls_lmots_algorithm_type_t type );
109
110/**
111 * \brief This function creates a candidate public key from
112 * an LMOTS signature. This can then be compared to
113 * the real public key to determine the validity of
114 * the signature.
115 *
116 * \note This function is exposed publicly to be used in LMS
117 * signature verification, it is expected that
118 * mbedtls_lmots_verify will be used for LMOTS
119 * signature verification.
120 *
121 * \param I_key_identifier The key identifier of the key, as a 16 byte
122 * bytestring.
123 * \param q_leaf_identifier The leaf identifier of key. If this LMOTS key is
124 * not being used as part of an LMS key, this should
125 * be set to 0.
126 * \param msg The buffer from which the message will be read.
127 * \param msg_len The size of the message that will be read.
128 * \param sig The buff from which the signature will be read.
129 * MBEDTLS_LMOTS_SIG_LEN bytes will be read from this.
130 * \param out The buffer where the candidate public key will be
131 * stored. Must be at least #MBEDTLS_LMOTS_N_HASH_LEN
132 * bytes in size.
133 *
134 * \return \c 0 on success.
135 * \return A non-zero error code on failure.
136 */
137int mbedtls_lmots_generate_pub_key_candidate( const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
138 const unsigned char q_leaf_identifier[MBEDTLS_LMOTS_Q_LEAF_ID_LEN],
139 const unsigned char *msg,
140 size_t msg_len,
141 const unsigned char *sig,
142 unsigned char *out );
143
144/**
145 * \brief This function creates a LMOTS signature, using a
146 * LMOTS context that contains a private key.
147 *
148 * \note Before this function is called, the context must
149 * have been initialized and must contain a private
150 * key.
151 *
152 * \note LMOTS private keys can only be used once, otherwise
153 * attackers may be able to create forged signatures.
154 * If the signing operation is successful, the private
155 * key in the context will be erased, and no further
156 * signing will be possible until another private key
157 * is loaded
158 *
159 * \param ctx The initialized LMOTS context from which the
160 * private key will be read.
161 * \param f_rng The RNG function to be used for signature
162 * generation.
163 * \param p_rng The RNG context to be passed to f_rng
164 * \param msg The buffer from which the message will be read.
165 * \param msg_len The size of the message that will be read.
166 * \param sig The buf into which the signature will be stored.
167 * Must be at least #MBEDTLS_LMOTS_SIG_LEN in size.
168 *
169 * \return \c 0 on success.
170 * \return A non-zero error code on failure.
171 */
172int mbedtls_lmots_sign( mbedtls_lmots_context *ctx,
173 int (*f_rng)(void *, unsigned char *, size_t),
174 void *p_rng, const unsigned char *msg, size_t msg_len,
175 unsigned char *sig );
176
177/**
178 * \brief This function verifies a LMOTS signature, using a
179 * LMOTS context that contains a public key.
180 *
181 * \note Before this function is called, the context must
182 * have been initialized and must contain a public key
183 * (either by import or generation).
184 *
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_len 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_context *ctx, const unsigned char *msg,
197 size_t msg_len, const unsigned char *sig );
198
199/**
200 * \brief This function imports an LMOTS public key into a
201 * LMOTS context.
202 *
203 * \note Before this function is called, the context must
204 * have been initialized.
205 *
206 * \note See IETF RFC8554 for details of the encoding of
207 * this public key.
208 *
209 * \param ctx The initialized LMOTS context store the key in.
210 * \param key The buffer from which the key will be read.
211 * #MBEDTLS_LMOTS_PUBKEY_LEN bytes will be read from
212 * this.
213 *
214 * \return \c 0 on success.
215 * \return A non-zero error code on failure.
216 */
217int mbedtls_lmots_import_pubkey( mbedtls_lmots_context *ctx,
218 const unsigned char *key );
219
220/**
221 * \brief This function exports an LMOTS public key from a
222 * LMOTS context that already contains a public key.
223 *
224 * \note Before this function is called, the context must
225 * have been initialized and the context must contain
226 * a public key.
227 *
228 * \note See IETF RFC8554 for details of the encoding of
229 * this public key.
230 *
231 * \param ctx The initialized LMOTS context that contains the
232 * publc key.
233 * \param key The buffer into which the key will be output. Must
234 * be at least #MBEDTLS_LMOTS_PUBKEY_LEN in size.
235 *
236 * \return \c 0 on success.
237 * \return A non-zero error code on failure.
238 */
239int mbedtls_lmots_export_pubkey( mbedtls_lmots_context *ctx,
240 unsigned char *key );
241
242/**
243 * \brief This function generates an LMOTS public key from a
244 * LMOTS context that already contains a private key.
245 *
246 * \note Before this function is called, the context must
247 * have been initialized and the context must contain
248 * a private key.
249 *
250 * \param ctx The initialized LMOTS context to generate the key
251 * from and store it into.
252 *
253 * \return \c 0 on success.
254 * \return A non-zero error code on failure.
255 */
256int mbedtls_lmots_gen_pubkey( mbedtls_lmots_context *ctx );
257
258/**
259 * \brief This function generates an LMOTS private key, and
260 * stores in into an LMOTS context.
261 *
262 * \note Before this function is called, the context must
263 * have been initialized and the type of the LMOTS
264 * context set using mbedtls_lmots_set_algorithm_type
265 *
266 * \note The seed must have at least 256 bits of entropy.
267 *
268 * \param ctx The initialized LMOTS context to generate the key
269 * into.
270 * \param I_key_identifier The key identifier of the key, as a 16 byte
271 * bytestring.
272 * \param q_leaf_identifier The leaf identifier of key. If this LMOTS key is
273 * not being used as part of an LMS key, this should
274 * be set to 0.
275 * \param seed The seed used to deterministically generate the
276 * key.
277 * \param seed_len The length of the seed.
278 *
279 * \return \c 0 on success.
280 * \return A non-zero error code on failure.
281 */
282int mbedtls_lmots_gen_privkey( mbedtls_lmots_context *ctx,
283 const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN],
284 unsigned int q_leaf_identifier,
285 const unsigned char *seed,
286 size_t seed_len );
287
288#ifdef __cplusplus
289}
290#endif
291
292#endif /* MBEDTLS_LMOTS_H */