Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file lmots.h |
| 3 | * |
| 4 | * \brief This file provides an API for the LM-OTS post-quantum-safe one-time |
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_LMOTS_SHA256_N32_W8 in order to reduce complexity. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 8 | */ |
| 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 Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 29 | #include "mbedtls/build_info.h" |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 30 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 31 | #include "psa/crypto.h" |
| 32 | |
Raef Coles | ab300f1 | 2022-09-28 17:12:41 +0100 | [diff] [blame^] | 33 | #include "mbedtls/lms.h" |
| 34 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 35 | #include <stdint.h> |
| 36 | #include <stddef.h> |
| 37 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 38 | #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 Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 41 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 42 | #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 Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 46 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 47 | #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 Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 51 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 52 | #define MBEDTLS_LMOTS_SIG_TYPE_OFFSET (0) |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 53 | #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 Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 57 | |
| 58 | #ifdef __cplusplus |
| 59 | extern "C" { |
| 60 | #endif |
| 61 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 62 | |
Raef Coles | 40158e1 | 2022-09-27 10:23:53 +0100 | [diff] [blame] | 63 | #if defined(MBEDTLS_TEST_HOOKS) |
| 64 | extern int( *mbedtls_lmots_sign_private_key_invalidated_hook )( unsigned char * ); |
| 65 | #endif /* defined(MBEDTLS_TEST_HOOKS) */ |
| 66 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 67 | /** |
| 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 Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 74 | */ |
Raef Coles | ad05425 | 2022-09-27 10:59:16 +0100 | [diff] [blame] | 75 | void mbedtls_lms_unsigned_int_to_network_bytes( unsigned int val, size_t len, |
| 76 | unsigned char *bytes ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 77 | |
| 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 Coles | ad05425 | 2022-09-27 10:59:16 +0100 | [diff] [blame] | 87 | unsigned int mbedtls_lms_network_bytes_to_unsigned_int( size_t len, |
| 88 | const unsigned char *bytes ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 89 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 90 | /** |
| 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 Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 98 | int mbedtls_lms_error_from_psa( psa_status_t status ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 99 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 100 | |
| 101 | /** |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 102 | * \brief This function initializes a public LMOTS context |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 103 | * |
| 104 | * \param ctx The uninitialized LMOTS context that will then be |
| 105 | * initialized. |
| 106 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 107 | void mbedtls_lmots_init_public( mbedtls_lmots_public_t *ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 108 | |
| 109 | /** |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 110 | * \brief This function uninitializes a public LMOTS context |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 111 | * |
| 112 | * \param ctx The initialized LMOTS context that will then be |
| 113 | * uninitialized. |
| 114 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 115 | void mbedtls_lmots_free_public( mbedtls_lmots_public_t *ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 116 | |
| 117 | /** |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 118 | * \brief This function imports an LMOTS public key into a |
| 119 | * LMOTS context. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 120 | * |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 121 | * \note Before this function is called, the context must |
| 122 | * have been initialized. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 123 | * |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 124 | * \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 Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 129 | * #MBEDTLS_LMOTS_PUBLIC_KEY_LEN bytes will be read |
| 130 | * from this. |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 131 | * |
| 132 | * \return \c 0 on success. |
| 133 | * \return A non-zero error code on failure. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 134 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 135 | int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx, |
| 136 | const unsigned char *key, size_t key_size ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 137 | |
| 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 Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 149 | * \param params The LMOTS parameter set, q and I values as an |
| 150 | * mbedtls_lmots_parameters_t struct. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 151 | * \param msg The buffer from which the message will be read. |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 152 | * \param msg_size The size of the message that will be read. |
Raef Coles | 2ad6e61 | 2022-08-24 13:33:35 +0100 | [diff] [blame] | 153 | * \param sig The buffer from which the signature will be read. |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 154 | * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from |
| 155 | * this. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 156 | * \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 Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 163 | int 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 Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 170 | size_t *out_len ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 171 | |
| 172 | /** |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 173 | * \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 Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 182 | * (either by import or calculation from a private |
| 183 | * key). |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 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_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 | */ |
| 196 | int 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 Coles | ab4f874 | 2022-09-01 12:24:31 +0100 | [diff] [blame] | 200 | #ifdef MBEDTLS_LMS_PRIVATE |
| 201 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 202 | /** |
| 203 | * \brief This function initializes a private LMOTS context |
| 204 | * |
| 205 | * \param ctx The uninitialized LMOTS context that will then be |
| 206 | * initialized. |
| 207 | */ |
| 208 | void 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 | */ |
| 216 | void 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 | */ |
| 241 | int 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 | */ |
| 262 | int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx, |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 263 | mbedtls_lmots_private_t *priv_ctx ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 264 | |
| 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 | */ |
| 285 | int 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 Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 289 | * \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 Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 309 | * \param msg_size The size of the message that will be read. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 310 | * \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 Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 316 | int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx, |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 317 | int (*f_rng)(void *, unsigned char *, size_t), |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 318 | void *p_rng, const unsigned char *msg, size_t msg_size, |
| 319 | unsigned char *sig, size_t sig_size, size_t* sig_len ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 320 | |
Raef Coles | ab4f874 | 2022-09-01 12:24:31 +0100 | [diff] [blame] | 321 | #endif /* MBEDTLS_LMS_PRIVATE */ |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 322 | |
| 323 | #ifdef __cplusplus |
| 324 | } |
| 325 | #endif |
| 326 | |
| 327 | #endif /* MBEDTLS_LMOTS_H */ |