Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file lms.h |
| 3 | * |
| 4 | * \brief This file provides an API for the LMS post-quantum-safe stateful-hash |
Raef Coles | 2ad6e61 | 2022-08-24 13:33:35 +0100 | [diff] [blame] | 5 | 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_LMS_SHA256_M32_H10 in order to reduce complexity. This is one |
| 8 | * of the signature schemes recommended by the IETF draft SUIT standard |
| 9 | * for IOT firmware upgrades (RFC9019). |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 10 | */ |
| 11 | /* |
| 12 | * Copyright The Mbed TLS Contributors |
| 13 | * SPDX-License-Identifier: Apache-2.0 |
| 14 | * |
| 15 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 16 | * not use this file except in compliance with the License. |
| 17 | * You may obtain a copy of the License at |
| 18 | * |
| 19 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 20 | * |
| 21 | * Unless required by applicable law or agreed to in writing, software |
| 22 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 23 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 24 | * See the License for the specific language governing permissions and |
| 25 | * limitations under the License. |
| 26 | */ |
| 27 | #ifndef MBEDTLS_LMS_H |
| 28 | #define MBEDTLS_LMS_H |
| 29 | |
| 30 | #include <stdint.h> |
| 31 | #include <stddef.h> |
| 32 | |
Raef Coles | 7dce69a | 2022-08-24 14:07:06 +0100 | [diff] [blame] | 33 | #include "lmots.h" |
| 34 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 35 | #include "mbedtls/build_info.h" |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 36 | |
| 37 | #define MBEDTLS_ERR_LMS_BAD_INPUT_DATA -0x0011 /**< Bad data has been input to an LMS function */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 38 | #define MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS -0x0013 /**< Specified LMS key has utilised all of its private keys */ |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 39 | #define MBEDTLS_ERR_LMS_VERIFY_FAILED -0x0015 /**< LMS signature verification failed */ |
| 40 | #define MBEDTLS_ERR_LMS_ALLOC_FAILED -0x0017 /**< LMS failed to allocate space for a private key */ |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 41 | #define MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL -0x0019 /**< Input/output buffer is too small to contain requited data */ |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 42 | |
Raef Coles | 2ad6e61 | 2022-08-24 13:33:35 +0100 | [diff] [blame] | 43 | #define MBEDTLS_LMS_M_NODE_BYTES (32) /* The length of a hash output, 32 for SHA256 */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 44 | #define MBEDTLS_LMS_TYPE_LEN (4) |
| 45 | #define MBEDTLS_LMS_H_TREE_HEIGHT (10u) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 46 | |
| 47 | #define MBEDTLS_LMS_SIG_LEN (MBEDTLS_LMOTS_Q_LEAF_ID_LEN + MBEDTLS_LMOTS_SIG_LEN + \ |
| 48 | MBEDTLS_LMS_TYPE_LEN + MBEDTLS_LMS_H_TREE_HEIGHT * MBEDTLS_LMS_M_NODE_BYTES) |
| 49 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 50 | #define MBEDTLS_LMS_PUBLIC_KEY_LEN (MBEDTLS_LMS_TYPE_LEN + MBEDTLS_LMOTS_TYPE_LEN + \ |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 51 | MBEDTLS_LMOTS_I_KEY_ID_LEN + MBEDTLS_LMS_M_NODE_BYTES) |
| 52 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 53 | |
| 54 | #ifdef __cplusplus |
| 55 | extern "C" { |
| 56 | #endif |
| 57 | |
Raef Coles | c464746 | 2022-06-15 12:17:51 +0100 | [diff] [blame] | 58 | /* https://www.iana.org/assignments/leighton-micali-signatures/leighton-micali-signatures.xhtml |
| 59 | * We are only implementing a subset of the types, particularly H10, for the sake of simplicty. |
| 60 | */ |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 61 | typedef enum { |
| 62 | MBEDTLS_LMS_SHA256_M32_H10 = 0x6, |
| 63 | } mbedtls_lms_algorithm_type_t; |
| 64 | |
| 65 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 66 | /** LMS parameters structure. |
| 67 | * |
| 68 | * This contains the metadata associated with an LMS key, detailing the |
| 69 | * algorithm type, the type of the underlying OTS algorithm, and the key ID. |
| 70 | */ |
| 71 | typedef struct { |
| 72 | unsigned char MBEDTLS_PRIVATE(I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN]); /*!< The key |
| 73 | identifier. */ |
| 74 | mbedtls_lmots_algorithm_type_t MBEDTLS_PRIVATE(otstype); /*!< The LM-OTS key type identifier as |
| 75 | per IANA. Only SHA256_N32_W8 is |
| 76 | currently supported. */ |
| 77 | mbedtls_lms_algorithm_type_t MBEDTLS_PRIVATE(type); /*!< The LMS key type identifier as per |
| 78 | IANA. Only SHA256_M32_H10 is currently |
| 79 | supported. */ |
| 80 | } mbedtls_lms_parameters_t; |
| 81 | |
| 82 | /** LMS public context structure. |
| 83 | * |
| 84 | *A LMS public key is the hash output that is the root of the merkle tree, and |
| 85 | * the applicable parameter set |
Raef Coles | 2ad6e61 | 2022-08-24 13:33:35 +0100 | [diff] [blame] | 86 | * |
| 87 | * The context must be initialized before it is used. A public key must either |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 88 | * be imported or generated from a private context. |
Raef Coles | 2ad6e61 | 2022-08-24 13:33:35 +0100 | [diff] [blame] | 89 | * |
| 90 | * \dot |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 91 | * digraph lms_public_t { |
Raef Coles | 2ad6e61 | 2022-08-24 13:33:35 +0100 | [diff] [blame] | 92 | * UNINITIALIZED -> INIT [label="init"]; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 93 | * HAVE_PUBLIC_KEY -> INIT [label="free"]; |
| 94 | * INIT -> HAVE_PUBLIC_KEY [label="import_public_key"]; |
| 95 | * INIT -> HAVE_PUBLIC_KEY [label="calculate_public_key from private key"]; |
| 96 | * HAVE_PUBLIC_KEY -> HAVE_PUBLIC_KEY [label="export_public_key"]; |
Raef Coles | 2ad6e61 | 2022-08-24 13:33:35 +0100 | [diff] [blame] | 97 | * } |
| 98 | * \enddot |
| 99 | */ |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 100 | typedef struct { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 101 | mbedtls_lms_parameters_t MBEDTLS_PRIVATE(params); |
Raef Coles | c464746 | 2022-06-15 12:17:51 +0100 | [diff] [blame] | 102 | unsigned char MBEDTLS_PRIVATE(T_1_pub_key)[MBEDTLS_LMS_M_NODE_BYTES]; /*!< The public key, in |
| 103 | the form of the merkle tree root node. */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 104 | unsigned char MBEDTLS_PRIVATE(have_public_key); /*!< Whether the context contains a public key. |
| 105 | Boolean values only. */ |
| 106 | } mbedtls_lms_public_t; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 107 | |
| 108 | |
Raef Coles | ab4f874 | 2022-09-01 12:24:31 +0100 | [diff] [blame^] | 109 | #ifdef MBEDTLS_LMS_PRIVATE |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 110 | /** LMS private context structure. |
| 111 | * |
| 112 | * A LMS private key is a set of LMOTS private keys, an index to the next usable |
| 113 | * key, and the applicable parameter set. |
| 114 | * |
| 115 | * The context must be initialized before it is used. A public key must either |
| 116 | * be imported or generated from a private context. |
| 117 | * |
| 118 | * \dot |
| 119 | * digraph lms_public_t { |
| 120 | * UNINITIALIZED -> INIT [label="init"]; |
| 121 | * HAVE_PRIVATE_KEY -> INIT [label="free"]; |
| 122 | * INIT -> HAVE_PRIVATE_KEY [label="generate_private_key"]; |
| 123 | * } |
| 124 | * \enddot |
| 125 | */ |
| 126 | typedef struct { |
| 127 | mbedtls_lms_parameters_t MBEDTLS_PRIVATE(params); |
| 128 | uint32_t MBEDTLS_PRIVATE(q_next_usable_key); /*!< The index of the next OTS key that has not |
| 129 | been used. */ |
| 130 | mbedtls_lmots_private_t *MBEDTLS_PRIVATE(ots_private_keys); /*!< The private key material. One OTS key |
| 131 | for each leaf node in the merkle tree. */ |
| 132 | mbedtls_lmots_public_t *MBEDTLS_PRIVATE(ots_public_keys); /*!< The OTS key public keys, used to |
| 133 | build the merkle tree. */ |
| 134 | unsigned char MBEDTLS_PRIVATE(have_private_key); /*!< Whether the context contains a private key. |
| 135 | Boolean values only. */ |
| 136 | } mbedtls_lms_private_t; |
Raef Coles | ab4f874 | 2022-09-01 12:24:31 +0100 | [diff] [blame^] | 137 | #endif /* MBEDTLS_LMS_PRIVATE */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 138 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 139 | /** |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 140 | * \brief This function initializes an LMS public context |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 141 | * |
| 142 | * \param ctx The uninitialized LMS context that will then be |
| 143 | * initialized. |
| 144 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 145 | void mbedtls_lms_init_public( mbedtls_lms_public_t *ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 146 | |
| 147 | /** |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 148 | * \brief This function uninitializes an LMS public context |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 149 | * |
| 150 | * \param ctx The initialized LMS context that will then be |
| 151 | * uninitialized. |
| 152 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 153 | void mbedtls_lms_free_public( mbedtls_lms_public_t *ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 154 | |
| 155 | /** |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 156 | * \brief This function imports an LMS public key into a |
| 157 | * public LMS context. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 158 | * |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 159 | * \note Before this function is called, the context must |
| 160 | * have been initialized. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 161 | * |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 162 | * \note See IETF RFC8554 for details of the encoding of |
| 163 | * this public key. |
| 164 | * |
| 165 | * \param ctx The initialized LMS context store the key in. |
| 166 | * \param key The buffer from which the key will be read. |
| 167 | * #MBEDTLS_LMS_PUBLIC_KEY_LEN bytes will be read from |
| 168 | * this. |
| 169 | * \param key_size The size of the key being imported. |
| 170 | * |
| 171 | * \return \c 0 on success. |
| 172 | * \return A non-zero error code on failure. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 173 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 174 | int mbedtls_lms_import_public_key( mbedtls_lms_public_t *ctx, |
| 175 | const unsigned char *key, size_t key_size ); |
| 176 | |
| 177 | /** |
| 178 | * \brief This function verifies a LMS signature, using a |
| 179 | * LMS 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 LMS public context from which the |
| 186 | * public 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_LMS_SIG_LEN bytes will be read from |
| 191 | * this. |
| 192 | * \param sig_size The size of the signature to be verified. |
| 193 | * |
| 194 | * \return \c 0 on successful verification. |
| 195 | * \return A non-zero error code on failure. |
| 196 | */ |
| 197 | int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx, |
| 198 | const unsigned char *msg, size_t msg_size, |
| 199 | const unsigned char *sig, size_t sig_size ); |
| 200 | |
Raef Coles | ab4f874 | 2022-09-01 12:24:31 +0100 | [diff] [blame^] | 201 | #ifdef MBEDTLS_LMS_PRIVATE |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 202 | /** |
| 203 | * \brief This function initializes an LMS private context |
| 204 | * |
| 205 | * \param ctx The uninitialized LMS private context that will |
| 206 | * then be initialized. */ |
| 207 | void mbedtls_lms_init_private( mbedtls_lms_private_t *ctx ); |
| 208 | |
| 209 | /** |
| 210 | * \brief This function uninitializes an LMS private context |
| 211 | * |
| 212 | * \param ctx The initialized LMS private context that will then |
| 213 | * be uninitialized. |
| 214 | */ |
| 215 | void mbedtls_lms_free_private( mbedtls_lms_private_t *ctx ); |
| 216 | |
| 217 | /** |
| 218 | * \brief This function generates an LMS private key, and |
| 219 | * stores in into an LMS private context. |
| 220 | * |
| 221 | * \warning This function is **not intended for use in |
| 222 | * production**, due to as-yet unsolved problems with |
| 223 | * handling stateful keys. |
| 224 | * |
| 225 | * \note The seed must have at least 256 bits of entropy. |
| 226 | * |
| 227 | * \param ctx The initialized LMOTS context to generate the key |
| 228 | * into. |
| 229 | * \param type The LMS parameter set identifier. |
| 230 | * \param otstype The LMOTS parameter set identifier. |
| 231 | * \param f_rng The RNG function to be used to generate the key ID. |
| 232 | * \param p_rng The RNG context to be passed to f_rng |
| 233 | * \param seed The seed used to deterministically generate the |
| 234 | * key. |
| 235 | * \param seed_size The length of the seed. |
| 236 | * |
| 237 | * \return \c 0 on success. |
| 238 | * \return A non-zero error code on failure. |
| 239 | */ |
| 240 | int mbedtls_lms_generate_private_key( mbedtls_lms_private_t *ctx, |
| 241 | mbedtls_lms_algorithm_type_t type, |
| 242 | mbedtls_lmots_algorithm_type_t otstype, |
| 243 | int (*f_rng)(void *, unsigned char *, size_t), |
| 244 | void* p_rng, unsigned char *seed, |
| 245 | size_t seed_size ); |
| 246 | |
| 247 | /** |
| 248 | * \brief This function generates an LMS public key from a |
| 249 | * LMS context that already contains a private key. |
| 250 | * |
| 251 | * \note Before this function is called, the context must |
| 252 | * have been initialized and the context must contain |
| 253 | * a private key. |
| 254 | * |
| 255 | * \param ctx The initialized LMS public context to generate the key |
| 256 | * from and store it into. |
| 257 | * |
| 258 | * \param ctx The LMS private context to read the private key |
| 259 | * from. This must have been initialized and contain a |
| 260 | * private key. |
| 261 | * |
| 262 | * \return \c 0 on success. |
| 263 | * \return A non-zero error code on failure. |
| 264 | */ |
| 265 | int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx, |
| 266 | mbedtls_lms_private_t *priv_ctx ); |
| 267 | |
| 268 | /** |
| 269 | * \brief This function exports an LMS public key from a |
| 270 | * LMS public context that already contains a public |
| 271 | * key. |
| 272 | * |
| 273 | * \note Before this function is called, the context must |
| 274 | * have been initialized and the context must contain |
| 275 | * a public key. |
| 276 | * |
| 277 | * \note See IETF RFC8554 for details of the encoding of |
| 278 | * this public key. |
| 279 | * |
| 280 | * \param ctx The initialized LMS public context that contains |
| 281 | * the public key. |
| 282 | * \param key The buffer into which the key will be output. Must |
| 283 | * be at least #MBEDTLS_LMS_PUBLIC_KEY_LEN in size. |
| 284 | * \param key_size The size of the key buffer. |
| 285 | * \param key_len If not NULL, will be written with the size of the |
| 286 | * key. |
| 287 | * |
| 288 | * \return \c 0 on success. |
| 289 | * \return A non-zero error code on failure. |
| 290 | */ |
| 291 | int mbedtls_lms_export_public_key( mbedtls_lms_public_t *ctx, unsigned char *key, |
| 292 | size_t key_size, size_t *key_len ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 293 | |
| 294 | /** |
| 295 | * \brief This function creates a LMS signature, using a |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 296 | * LMS context that contains unused private keys. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 297 | * |
Raef Coles | 2ad6e61 | 2022-08-24 13:33:35 +0100 | [diff] [blame] | 298 | * \warning This function is **not intended for use in |
| 299 | * production**, due to as-yet unsolved problems with |
| 300 | * handling stateful keys. |
Raef Coles | 0aa18e0 | 2022-06-15 13:05:56 +0100 | [diff] [blame] | 301 | * |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 302 | * \note Before this function is called, the context must |
| 303 | * have been initialized and must contain a private |
| 304 | * key. |
| 305 | * |
| 306 | * \note Each of the LMOTS private keys inside a LMS private |
| 307 | * key can only be used once. If they are reused, then |
| 308 | * attackers may be able to forge signatures with that |
| 309 | * key. This is all handled transparently, but it is |
| 310 | * important to not perform copy operations on LMS |
| 311 | * contexts that contain private key material. |
| 312 | * |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 313 | * \param ctx The initialized LMS private context from which the |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 314 | * private key will be read. |
| 315 | * \param f_rng The RNG function to be used for signature |
| 316 | * generation. |
| 317 | * \param p_rng The RNG context to be passed to f_rng |
| 318 | * \param msg The buffer from which the message will be read. |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 319 | * \param msg_size The size of the message that will be read. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 320 | * \param sig The buf into which the signature will be stored. |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 321 | * Must be at least #MBEDTLS_LMS_SIG_LEN in size. |
| 322 | * \param sig_size The size of the buffer the signature will be |
| 323 | * written into. |
| 324 | * \param sig_len If not NULL, will be written with the size of the |
| 325 | * signature. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 326 | * |
| 327 | * \return \c 0 on success. |
| 328 | * \return A non-zero error code on failure. |
| 329 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 330 | int mbedtls_lms_sign( mbedtls_lms_private_t *ctx, |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 331 | int (*f_rng)(void *, unsigned char *, size_t), |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 332 | void* p_rng, unsigned char *msg, unsigned int msg_size, |
| 333 | unsigned char *sig, size_t sig_size, size_t *sig_len); |
Raef Coles | ab4f874 | 2022-09-01 12:24:31 +0100 | [diff] [blame^] | 334 | #endif /* MBEDTLS_LMS_PRIVATE */ |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 335 | |
| 336 | #ifdef __cplusplus |
| 337 | } |
| 338 | #endif |
| 339 | |
| 340 | #endif /* MBEDTLS_LMS_H */ |