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 | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 33 | #include "mbedtls/build_info.h" |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 34 | |
| 35 | #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] | 36 | #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] | 37 | #define MBEDTLS_ERR_LMS_VERIFY_FAILED -0x0015 /**< LMS signature verification failed */ |
| 38 | #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] | 39 | #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] | 40 | |
Raef Coles | ab300f1 | 2022-09-28 17:12:41 +0100 | [diff] [blame] | 41 | /* Currently only defined for SHA256, 32 is the max hash output size */ |
| 42 | #define MBEDTLS_LMOTS_N_HASH_LEN_MAX (32u) |
| 43 | #define MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX (34u) |
Raef Coles | f6ddd51 | 2022-10-07 10:49:34 +0100 | [diff] [blame] | 44 | #define MBEDTLS_LMOTS_N_HASH_LEN(type) ((type) == MBEDTLS_LMOTS_SHA256_N32_W8 ? 32u : 0) |
Raef Coles | ab300f1 | 2022-09-28 17:12:41 +0100 | [diff] [blame] | 45 | #define MBEDTLS_LMOTS_I_KEY_ID_LEN (16u) |
| 46 | #define MBEDTLS_LMOTS_Q_LEAF_ID_LEN (4u) |
Raef Coles | 56fe20a | 2022-09-28 17:56:39 +0100 | [diff] [blame] | 47 | #define MBEDTLS_LMOTS_TYPE_LEN (4u) |
Raef Coles | f6ddd51 | 2022-10-07 10:49:34 +0100 | [diff] [blame] | 48 | #define MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(type) ((type) == MBEDTLS_LMOTS_SHA256_N32_W8 ? 34u : 0) |
Raef Coles | 56fe20a | 2022-09-28 17:56:39 +0100 | [diff] [blame] | 49 | #define MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(type) (MBEDTLS_LMOTS_N_HASH_LEN(type)) |
| 50 | |
| 51 | #define MBEDTLS_LMOTS_SIG_LEN(type) (MBEDTLS_LMOTS_TYPE_LEN + \ |
| 52 | MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(type) + \ |
| 53 | (MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(type) * \ |
| 54 | MBEDTLS_LMOTS_N_HASH_LEN(type))) |
| 55 | |
Raef Coles | ab300f1 | 2022-09-28 17:12:41 +0100 | [diff] [blame] | 56 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 57 | #define MBEDTLS_LMS_TYPE_LEN (4) |
Raef Coles | f6ddd51 | 2022-10-07 10:49:34 +0100 | [diff] [blame] | 58 | #define MBEDTLS_LMS_H_TREE_HEIGHT(type) ((type) == MBEDTLS_LMS_SHA256_M32_H10 ? 10u : 0) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 59 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 60 | /* The length of a hash output, Currently only imlemented for SHA256. |
| 61 | * Max is 32 bytes. |
| 62 | */ |
Raef Coles | f6ddd51 | 2022-10-07 10:49:34 +0100 | [diff] [blame] | 63 | #define MBEDTLS_LMS_M_NODE_BYTES(type) ((type) == MBEDTLS_LMS_SHA256_M32_H10 ? 32 : 0) |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 64 | #define MBEDTLS_LMS_M_NODE_BYTES_MAX 32 |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 65 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 66 | #define MBEDTLS_LMS_SIG_LEN(type, otstype) (MBEDTLS_LMOTS_Q_LEAF_ID_LEN + \ |
| 67 | MBEDTLS_LMOTS_SIG_LEN(otstype) + \ |
| 68 | MBEDTLS_LMS_TYPE_LEN + \ |
| 69 | (MBEDTLS_LMS_H_TREE_HEIGHT(type) * \ |
| 70 | MBEDTLS_LMS_M_NODE_BYTES(type))) |
| 71 | |
| 72 | #define MBEDTLS_LMS_PUBLIC_KEY_LEN(type) (MBEDTLS_LMS_TYPE_LEN + \ |
| 73 | MBEDTLS_LMOTS_TYPE_LEN + \ |
| 74 | MBEDTLS_LMOTS_I_KEY_ID_LEN + \ |
| 75 | MBEDTLS_LMS_M_NODE_BYTES(type)) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 76 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 77 | |
| 78 | #ifdef __cplusplus |
| 79 | extern "C" { |
| 80 | #endif |
| 81 | |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 82 | /** The Identifier of the LMS parameter set, as per |
| 83 | * https://www.iana.org/assignments/leighton-micali-signatures/leighton-micali-signatures.xhtml |
Raef Coles | c464746 | 2022-06-15 12:17:51 +0100 | [diff] [blame] | 84 | * We are only implementing a subset of the types, particularly H10, for the sake of simplicty. |
| 85 | */ |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 86 | typedef enum { |
| 87 | MBEDTLS_LMS_SHA256_M32_H10 = 0x6, |
| 88 | } mbedtls_lms_algorithm_type_t; |
| 89 | |
Raef Coles | ab300f1 | 2022-09-28 17:12:41 +0100 | [diff] [blame] | 90 | /** The Identifier of the LMOTS parameter set, as per |
| 91 | * https://www.iana.org/assignments/leighton-micali-signatures/leighton-micali-signatures.xhtml. |
| 92 | * We are only implementing a subset of the types, particularly N32_W8, for the sake of simplicty. |
| 93 | */ |
| 94 | typedef enum { |
| 95 | MBEDTLS_LMOTS_SHA256_N32_W8 = 4 |
| 96 | } mbedtls_lmots_algorithm_type_t; |
| 97 | |
| 98 | /** LMOTS parameters structure. |
| 99 | * |
| 100 | * This contains the metadata associated with an LMOTS key, detailing the |
| 101 | * algorithm type, the key ID, and the leaf identifier should be key be part of |
| 102 | * a LMS key. |
| 103 | */ |
| 104 | typedef struct { |
| 105 | unsigned char MBEDTLS_PRIVATE(I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN]); /*!< The key |
| 106 | identifier. */ |
| 107 | unsigned char MBEDTLS_PRIVATE(q_leaf_identifier[MBEDTLS_LMOTS_Q_LEAF_ID_LEN]); /*!< Which |
| 108 | leaf of the LMS key this is. |
| 109 | 0 if the key is not part of an LMS key. */ |
| 110 | mbedtls_lmots_algorithm_type_t MBEDTLS_PRIVATE(type); /*!< The LM-OTS key type identifier as |
| 111 | per IANA. Only SHA256_N32_W8 is |
| 112 | currently supported. */ |
| 113 | } mbedtls_lmots_parameters_t; |
| 114 | |
| 115 | /** LMOTS public context structure. |
| 116 | * |
| 117 | * A LMOTS public key is a hash output, and the applicable parameter set. |
| 118 | * |
| 119 | * The context must be initialized before it is used. A public key must either |
| 120 | * be imported or generated from a private context. |
| 121 | * |
| 122 | * \dot |
| 123 | * digraph lmots_public_t { |
| 124 | * UNINITIALIZED -> INIT [label="init"]; |
| 125 | * HAVE_PUBLIC_KEY -> INIT [label="free"]; |
| 126 | * INIT -> HAVE_PUBLIC_KEY [label="import_public_key"]; |
| 127 | * INIT -> HAVE_PUBLIC_KEY [label="calculate_public_key from private key"]; |
| 128 | * HAVE_PUBLIC_KEY -> HAVE_PUBLIC_KEY [label="export_public_key"]; |
| 129 | * } |
| 130 | * \enddot |
| 131 | */ |
| 132 | typedef struct { |
| 133 | mbedtls_lmots_parameters_t MBEDTLS_PRIVATE(params); |
| 134 | unsigned char MBEDTLS_PRIVATE(public_key)[MBEDTLS_LMOTS_N_HASH_LEN_MAX]; |
| 135 | unsigned char MBEDTLS_PRIVATE(have_public_key); /*!< Whether the context contains a public key. |
| 136 | Boolean values only. */ |
| 137 | } mbedtls_lmots_public_t; |
| 138 | |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 139 | #if defined(MBEDTLS_LMS_PRIVATE) |
Raef Coles | ab300f1 | 2022-09-28 17:12:41 +0100 | [diff] [blame] | 140 | /** LMOTS private context structure. |
| 141 | * |
| 142 | * A LMOTS private key is one hash output for each of digit of the digest + |
| 143 | * checksum, and the applicable parameter set. |
| 144 | * |
| 145 | * The context must be initialized before it is used. A public key must either |
| 146 | * be imported or generated from a private context. |
| 147 | * |
| 148 | * \dot |
| 149 | * digraph lmots_public_t { |
| 150 | * UNINITIALIZED -> INIT [label="init"]; |
| 151 | * HAVE_PRIVATE_KEY -> INIT [label="free"]; |
| 152 | * INIT -> HAVE_PRIVATE_KEY [label="generate_private_key"]; |
| 153 | * HAVE_PRIVATE_KEY -> INIT [label="sign"]; |
| 154 | * } |
| 155 | * \enddot |
| 156 | */ |
| 157 | typedef struct { |
| 158 | mbedtls_lmots_parameters_t MBEDTLS_PRIVATE(params); |
| 159 | unsigned char MBEDTLS_PRIVATE(private_key)[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX]; |
| 160 | unsigned char MBEDTLS_PRIVATE(have_private_key); /*!< Whether the context contains a private key. |
| 161 | Boolean values only. */ |
| 162 | } mbedtls_lmots_private_t; |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 163 | #endif /* defined(MBEDTLS_LMS_PRIVATE) */ |
Raef Coles | ab300f1 | 2022-09-28 17:12:41 +0100 | [diff] [blame] | 164 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 165 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 166 | /** LMS parameters structure. |
| 167 | * |
| 168 | * This contains the metadata associated with an LMS key, detailing the |
| 169 | * algorithm type, the type of the underlying OTS algorithm, and the key ID. |
| 170 | */ |
| 171 | typedef struct { |
| 172 | unsigned char MBEDTLS_PRIVATE(I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN]); /*!< The key |
| 173 | identifier. */ |
| 174 | mbedtls_lmots_algorithm_type_t MBEDTLS_PRIVATE(otstype); /*!< The LM-OTS key type identifier as |
| 175 | per IANA. Only SHA256_N32_W8 is |
| 176 | currently supported. */ |
| 177 | mbedtls_lms_algorithm_type_t MBEDTLS_PRIVATE(type); /*!< The LMS key type identifier as per |
| 178 | IANA. Only SHA256_M32_H10 is currently |
| 179 | supported. */ |
| 180 | } mbedtls_lms_parameters_t; |
| 181 | |
| 182 | /** LMS public context structure. |
| 183 | * |
Raef Coles | 285d44b | 2022-10-10 15:44:17 +0100 | [diff] [blame^] | 184 | *A LMS public key is the hash output that is the root of the Merkle tree, and |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 185 | * the applicable parameter set |
Raef Coles | 2ad6e61 | 2022-08-24 13:33:35 +0100 | [diff] [blame] | 186 | * |
| 187 | * 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] | 188 | * be imported or generated from a private context. |
Raef Coles | 2ad6e61 | 2022-08-24 13:33:35 +0100 | [diff] [blame] | 189 | * |
| 190 | * \dot |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 191 | * digraph lms_public_t { |
Raef Coles | 2ad6e61 | 2022-08-24 13:33:35 +0100 | [diff] [blame] | 192 | * UNINITIALIZED -> INIT [label="init"]; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 193 | * HAVE_PUBLIC_KEY -> INIT [label="free"]; |
| 194 | * INIT -> HAVE_PUBLIC_KEY [label="import_public_key"]; |
| 195 | * INIT -> HAVE_PUBLIC_KEY [label="calculate_public_key from private key"]; |
| 196 | * HAVE_PUBLIC_KEY -> HAVE_PUBLIC_KEY [label="export_public_key"]; |
Raef Coles | 2ad6e61 | 2022-08-24 13:33:35 +0100 | [diff] [blame] | 197 | * } |
| 198 | * \enddot |
| 199 | */ |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 200 | typedef struct { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 201 | mbedtls_lms_parameters_t MBEDTLS_PRIVATE(params); |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 202 | unsigned char MBEDTLS_PRIVATE(T_1_pub_key)[MBEDTLS_LMS_M_NODE_BYTES_MAX]; /*!< The public key, in |
Raef Coles | 285d44b | 2022-10-10 15:44:17 +0100 | [diff] [blame^] | 203 | the form of the Merkle tree root node. */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 204 | unsigned char MBEDTLS_PRIVATE(have_public_key); /*!< Whether the context contains a public key. |
| 205 | Boolean values only. */ |
| 206 | } mbedtls_lms_public_t; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 207 | |
| 208 | |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 209 | #if defined(MBEDTLS_LMS_PRIVATE) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 210 | /** LMS private context structure. |
| 211 | * |
| 212 | * A LMS private key is a set of LMOTS private keys, an index to the next usable |
| 213 | * key, and the applicable parameter set. |
| 214 | * |
| 215 | * The context must be initialized before it is used. A public key must either |
| 216 | * be imported or generated from a private context. |
| 217 | * |
| 218 | * \dot |
| 219 | * digraph lms_public_t { |
| 220 | * UNINITIALIZED -> INIT [label="init"]; |
| 221 | * HAVE_PRIVATE_KEY -> INIT [label="free"]; |
| 222 | * INIT -> HAVE_PRIVATE_KEY [label="generate_private_key"]; |
| 223 | * } |
| 224 | * \enddot |
| 225 | */ |
| 226 | typedef struct { |
| 227 | mbedtls_lms_parameters_t MBEDTLS_PRIVATE(params); |
| 228 | uint32_t MBEDTLS_PRIVATE(q_next_usable_key); /*!< The index of the next OTS key that has not |
| 229 | been used. */ |
| 230 | mbedtls_lmots_private_t *MBEDTLS_PRIVATE(ots_private_keys); /*!< The private key material. One OTS key |
Raef Coles | 285d44b | 2022-10-10 15:44:17 +0100 | [diff] [blame^] | 231 | for each leaf node in the Merkle tree. */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 232 | mbedtls_lmots_public_t *MBEDTLS_PRIVATE(ots_public_keys); /*!< The OTS key public keys, used to |
Raef Coles | 285d44b | 2022-10-10 15:44:17 +0100 | [diff] [blame^] | 233 | build the Merkle tree. */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 234 | unsigned char MBEDTLS_PRIVATE(have_private_key); /*!< Whether the context contains a private key. |
| 235 | Boolean values only. */ |
| 236 | } mbedtls_lms_private_t; |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 237 | #endif /* defined(MBEDTLS_LMS_PRIVATE) */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 238 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 239 | /** |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 240 | * \brief This function initializes an LMS public context |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 241 | * |
| 242 | * \param ctx The uninitialized LMS context that will then be |
| 243 | * initialized. |
| 244 | */ |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 245 | void mbedtls_lms_public_init( mbedtls_lms_public_t *ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 246 | |
| 247 | /** |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 248 | * \brief This function uninitializes an LMS public context |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 249 | * |
| 250 | * \param ctx The initialized LMS context that will then be |
| 251 | * uninitialized. |
| 252 | */ |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 253 | void mbedtls_lms_public_free( mbedtls_lms_public_t *ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 254 | |
| 255 | /** |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 256 | * \brief This function imports an LMS public key into a |
| 257 | * public LMS context. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 258 | * |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 259 | * \note Before this function is called, the context must |
| 260 | * have been initialized. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 261 | * |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 262 | * \note See IETF RFC8554 for details of the encoding of |
| 263 | * this public key. |
| 264 | * |
| 265 | * \param ctx The initialized LMS context store the key in. |
| 266 | * \param key The buffer from which the key will be read. |
| 267 | * #MBEDTLS_LMS_PUBLIC_KEY_LEN bytes will be read from |
| 268 | * this. |
| 269 | * \param key_size The size of the key being imported. |
| 270 | * |
| 271 | * \return \c 0 on success. |
| 272 | * \return A non-zero error code on failure. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 273 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 274 | int mbedtls_lms_import_public_key( mbedtls_lms_public_t *ctx, |
| 275 | const unsigned char *key, size_t key_size ); |
| 276 | |
| 277 | /** |
Raef Coles | 370cc43 | 2022-10-07 16:07:33 +0100 | [diff] [blame] | 278 | * \brief This function exports an LMS public key from a |
| 279 | * LMS public context that already contains a public |
| 280 | * key. |
| 281 | * |
| 282 | * \note Before this function is called, the context must |
| 283 | * have been initialized and the context must contain |
| 284 | * a public key. |
| 285 | * |
| 286 | * \note See IETF RFC8554 for details of the encoding of |
| 287 | * this public key. |
| 288 | * |
| 289 | * \param ctx The initialized LMS public context that contains |
| 290 | * the public key. |
| 291 | * \param key The buffer into which the key will be output. Must |
| 292 | * be at least #MBEDTLS_LMS_PUBLIC_KEY_LEN in size. |
| 293 | * \param key_size The size of the key buffer. |
| 294 | * \param key_len If not NULL, will be written with the size of the |
| 295 | * key. |
| 296 | * |
| 297 | * \return \c 0 on success. |
| 298 | * \return A non-zero error code on failure. |
| 299 | */ |
| 300 | int mbedtls_lms_export_public_key( const mbedtls_lms_public_t *ctx, |
| 301 | unsigned char *key, size_t key_size, |
| 302 | size_t *key_len ); |
| 303 | |
| 304 | /** |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 305 | * \brief This function verifies a LMS signature, using a |
| 306 | * LMS context that contains a public key. |
| 307 | * |
| 308 | * \note Before this function is called, the context must |
| 309 | * have been initialized and must contain a public key |
| 310 | * (either by import or generation). |
| 311 | * |
| 312 | * \param ctx The initialized LMS public context from which the |
| 313 | * public key will be read. |
| 314 | * \param msg The buffer from which the message will be read. |
| 315 | * \param msg_size The size of the message that will be read. |
| 316 | * \param sig The buf from which the signature will be read. |
| 317 | * #MBEDTLS_LMS_SIG_LEN bytes will be read from |
| 318 | * this. |
| 319 | * \param sig_size The size of the signature to be verified. |
| 320 | * |
| 321 | * \return \c 0 on successful verification. |
| 322 | * \return A non-zero error code on failure. |
| 323 | */ |
| 324 | int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx, |
| 325 | const unsigned char *msg, size_t msg_size, |
| 326 | const unsigned char *sig, size_t sig_size ); |
| 327 | |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 328 | #if defined(MBEDTLS_LMS_PRIVATE) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 329 | /** |
| 330 | * \brief This function initializes an LMS private context |
| 331 | * |
| 332 | * \param ctx The uninitialized LMS private context that will |
| 333 | * then be initialized. */ |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 334 | void mbedtls_lms_private_init( mbedtls_lms_private_t *ctx ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 335 | |
| 336 | /** |
| 337 | * \brief This function uninitializes an LMS private context |
| 338 | * |
| 339 | * \param ctx The initialized LMS private context that will then |
| 340 | * be uninitialized. |
| 341 | */ |
Raef Coles | be3bdd8 | 2022-10-07 12:04:24 +0100 | [diff] [blame] | 342 | void mbedtls_lms_private_free( mbedtls_lms_private_t *ctx ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 343 | |
| 344 | /** |
| 345 | * \brief This function generates an LMS private key, and |
| 346 | * stores in into an LMS private context. |
| 347 | * |
| 348 | * \warning This function is **not intended for use in |
| 349 | * production**, due to as-yet unsolved problems with |
Raef Coles | 9b0daf6 | 2022-10-10 14:25:39 +0100 | [diff] [blame] | 350 | * handling stateful keys. The API for this function |
| 351 | * may change considerably in future versions. |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 352 | * |
| 353 | * \note The seed must have at least 256 bits of entropy. |
| 354 | * |
| 355 | * \param ctx The initialized LMOTS context to generate the key |
| 356 | * into. |
| 357 | * \param type The LMS parameter set identifier. |
| 358 | * \param otstype The LMOTS parameter set identifier. |
| 359 | * \param f_rng The RNG function to be used to generate the key ID. |
| 360 | * \param p_rng The RNG context to be passed to f_rng |
| 361 | * \param seed The seed used to deterministically generate the |
| 362 | * key. |
| 363 | * \param seed_size The length of the seed. |
| 364 | * |
| 365 | * \return \c 0 on success. |
| 366 | * \return A non-zero error code on failure. |
| 367 | */ |
| 368 | int mbedtls_lms_generate_private_key( mbedtls_lms_private_t *ctx, |
| 369 | mbedtls_lms_algorithm_type_t type, |
| 370 | mbedtls_lmots_algorithm_type_t otstype, |
| 371 | int (*f_rng)(void *, unsigned char *, size_t), |
Raef Coles | 2ac352a | 2022-10-07 11:12:27 +0100 | [diff] [blame] | 372 | void* p_rng, const unsigned char *seed, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 373 | size_t seed_size ); |
| 374 | |
| 375 | /** |
| 376 | * \brief This function generates an LMS public key from a |
| 377 | * LMS context that already contains a private key. |
| 378 | * |
| 379 | * \note Before this function is called, the context must |
| 380 | * have been initialized and the context must contain |
| 381 | * a private key. |
| 382 | * |
| 383 | * \param ctx The initialized LMS public context to generate the key |
| 384 | * from and store it into. |
| 385 | * |
Raef Coles | 403558c | 2022-09-23 17:03:53 +0100 | [diff] [blame] | 386 | * \param priv_ctx The LMS private context to read the private key |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 387 | * from. This must have been initialized and contain a |
| 388 | * private key. |
| 389 | * |
| 390 | * \return \c 0 on success. |
| 391 | * \return A non-zero error code on failure. |
| 392 | */ |
| 393 | int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx, |
Raef Coles | 2ac352a | 2022-10-07 11:12:27 +0100 | [diff] [blame] | 394 | const mbedtls_lms_private_t *priv_ctx ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 395 | |
| 396 | /** |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 397 | * \brief This function creates a LMS signature, using a |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 398 | * LMS context that contains unused private keys. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 399 | * |
Raef Coles | 2ad6e61 | 2022-08-24 13:33:35 +0100 | [diff] [blame] | 400 | * \warning This function is **not intended for use in |
| 401 | * production**, due to as-yet unsolved problems with |
Raef Coles | 9b0daf6 | 2022-10-10 14:25:39 +0100 | [diff] [blame] | 402 | * handling stateful keys. The API for this function |
| 403 | * may change considerably in future versions. |
Raef Coles | 0aa18e0 | 2022-06-15 13:05:56 +0100 | [diff] [blame] | 404 | * |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 405 | * \note Before this function is called, the context must |
| 406 | * have been initialized and must contain a private |
| 407 | * key. |
| 408 | * |
| 409 | * \note Each of the LMOTS private keys inside a LMS private |
| 410 | * key can only be used once. If they are reused, then |
| 411 | * attackers may be able to forge signatures with that |
| 412 | * key. This is all handled transparently, but it is |
| 413 | * important to not perform copy operations on LMS |
| 414 | * contexts that contain private key material. |
| 415 | * |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 416 | * \param ctx The initialized LMS private context from which the |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 417 | * private key will be read. |
| 418 | * \param f_rng The RNG function to be used for signature |
| 419 | * generation. |
| 420 | * \param p_rng The RNG context to be passed to f_rng |
| 421 | * \param msg The buffer from which the message will be read. |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 422 | * \param msg_size The size of the message that will be read. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 423 | * \param sig The buf into which the signature will be stored. |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 424 | * Must be at least #MBEDTLS_LMS_SIG_LEN in size. |
| 425 | * \param sig_size The size of the buffer the signature will be |
| 426 | * written into. |
| 427 | * \param sig_len If not NULL, will be written with the size of the |
| 428 | * signature. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 429 | * |
| 430 | * \return \c 0 on success. |
| 431 | * \return A non-zero error code on failure. |
| 432 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 433 | int mbedtls_lms_sign( mbedtls_lms_private_t *ctx, |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 434 | int (*f_rng)(void *, unsigned char *, size_t), |
Raef Coles | 2ac352a | 2022-10-07 11:12:27 +0100 | [diff] [blame] | 435 | void* p_rng, const unsigned char *msg, |
| 436 | unsigned int msg_size, unsigned char *sig, size_t sig_size, |
| 437 | size_t *sig_len ); |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 438 | #endif /* defined(MBEDTLS_LMS_PRIVATE) */ |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 439 | |
| 440 | #ifdef __cplusplus |
| 441 | } |
| 442 | #endif |
| 443 | |
| 444 | #endif /* MBEDTLS_LMS_H */ |