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 |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame^] | 11 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #ifndef MBEDTLS_LMOTS_H |
| 15 | #define MBEDTLS_LMOTS_H |
| 16 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 17 | #include "mbedtls/build_info.h" |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 18 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 19 | #include "psa/crypto.h" |
| 20 | |
Raef Coles | ab300f1 | 2022-09-28 17:12:41 +0100 | [diff] [blame] | 21 | #include "mbedtls/lms.h" |
| 22 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 23 | #include <stdint.h> |
| 24 | #include <stddef.h> |
| 25 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 26 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 27 | #define MBEDTLS_LMOTS_PUBLIC_KEY_LEN(type) (MBEDTLS_LMOTS_TYPE_LEN + \ |
| 28 | MBEDTLS_LMOTS_I_KEY_ID_LEN + \ |
| 29 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN + \ |
| 30 | MBEDTLS_LMOTS_N_HASH_LEN(type)) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 31 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 32 | #define MBEDTLS_LMOTS_SIG_TYPE_OFFSET (0) |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 33 | #define MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET (MBEDTLS_LMOTS_SIG_TYPE_OFFSET + \ |
| 34 | MBEDTLS_LMOTS_TYPE_LEN) |
| 35 | #define MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(type) (MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET + \ |
| 36 | MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(type)) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 37 | |
| 38 | #ifdef __cplusplus |
| 39 | extern "C" { |
| 40 | #endif |
| 41 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 42 | |
Raef Coles | 40158e1 | 2022-09-27 10:23:53 +0100 | [diff] [blame] | 43 | #if defined(MBEDTLS_TEST_HOOKS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 44 | extern int (*mbedtls_lmots_sign_private_key_invalidated_hook)(unsigned char *); |
Raef Coles | 40158e1 | 2022-09-27 10:23:53 +0100 | [diff] [blame] | 45 | #endif /* defined(MBEDTLS_TEST_HOOKS) */ |
| 46 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 47 | /** |
| 48 | * \brief This function converts an unsigned int into a |
| 49 | * network-byte-order (big endian) string. |
| 50 | * |
| 51 | * \param val The unsigned integer value |
| 52 | * \param len The length of the string. |
| 53 | * \param bytes The string to output into. |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 54 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 55 | void mbedtls_lms_unsigned_int_to_network_bytes(unsigned int val, size_t len, |
| 56 | unsigned char *bytes); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * \brief This function converts a network-byte-order |
| 60 | * (big endian) string into an unsigned integer. |
| 61 | * |
| 62 | * \param len The length of the string. |
| 63 | * \param bytes The string. |
| 64 | * |
| 65 | * \return The corresponding LMS error code. |
| 66 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 67 | unsigned int mbedtls_lms_network_bytes_to_unsigned_int(size_t len, |
| 68 | const unsigned char *bytes); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 69 | |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 70 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 71 | /** |
| 72 | * \brief This function converts a \ref psa_status_t to a |
| 73 | * low-level LMS error code. |
| 74 | * |
| 75 | * \param status The psa_status_t to convert |
| 76 | * |
| 77 | * \return The corresponding LMS error code. |
| 78 | */ |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 79 | int MBEDTLS_DEPRECATED mbedtls_lms_error_from_psa(psa_status_t status); |
| 80 | #endif |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 81 | |
| 82 | /** |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 83 | * \brief This function initializes a public LMOTS context |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 84 | * |
| 85 | * \param ctx The uninitialized LMOTS context that will then be |
| 86 | * initialized. |
| 87 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 88 | void mbedtls_lmots_public_init(mbedtls_lmots_public_t *ctx); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 89 | |
| 90 | /** |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 91 | * \brief This function uninitializes a public LMOTS context |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 92 | * |
| 93 | * \param ctx The initialized LMOTS context that will then be |
| 94 | * uninitialized. |
| 95 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 96 | void mbedtls_lmots_public_free(mbedtls_lmots_public_t *ctx); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 97 | |
| 98 | /** |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 99 | * \brief This function imports an LMOTS public key into a |
| 100 | * LMOTS context. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 101 | * |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 102 | * \note Before this function is called, the context must |
| 103 | * have been initialized. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 104 | * |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 105 | * \note See IETF RFC8554 for details of the encoding of |
| 106 | * this public key. |
| 107 | * |
| 108 | * \param ctx The initialized LMOTS context store the key in. |
| 109 | * \param key The buffer from which the key will be read. |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 110 | * #MBEDTLS_LMOTS_PUBLIC_KEY_LEN bytes will be read |
| 111 | * from this. |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 112 | * |
| 113 | * \return \c 0 on success. |
| 114 | * \return A non-zero error code on failure. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 115 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | int mbedtls_lmots_import_public_key(mbedtls_lmots_public_t *ctx, |
| 117 | const unsigned char *key, size_t key_size); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 118 | |
| 119 | /** |
Raef Coles | 370cc43 | 2022-10-07 16:07:33 +0100 | [diff] [blame] | 120 | * \brief This function exports an LMOTS public key from a |
| 121 | * LMOTS context that already contains a public key. |
| 122 | * |
| 123 | * \note Before this function is called, the context must |
| 124 | * have been initialized and the context must contain |
| 125 | * a public key. |
| 126 | * |
| 127 | * \note See IETF RFC8554 for details of the encoding of |
| 128 | * this public key. |
| 129 | * |
| 130 | * \param ctx The initialized LMOTS context that contains the |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame] | 131 | * public key. |
Raef Coles | 370cc43 | 2022-10-07 16:07:33 +0100 | [diff] [blame] | 132 | * \param key The buffer into which the key will be output. Must |
| 133 | * be at least #MBEDTLS_LMOTS_PUBLIC_KEY_LEN in size. |
| 134 | * |
| 135 | * \return \c 0 on success. |
| 136 | * \return A non-zero error code on failure. |
| 137 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 138 | int mbedtls_lmots_export_public_key(const mbedtls_lmots_public_t *ctx, |
| 139 | unsigned char *key, size_t key_size, |
| 140 | size_t *key_len); |
Raef Coles | 370cc43 | 2022-10-07 16:07:33 +0100 | [diff] [blame] | 141 | |
| 142 | /** |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 143 | * \brief This function creates a candidate public key from |
| 144 | * an LMOTS signature. This can then be compared to |
| 145 | * the real public key to determine the validity of |
| 146 | * the signature. |
| 147 | * |
| 148 | * \note This function is exposed publicly to be used in LMS |
| 149 | * signature verification, it is expected that |
| 150 | * mbedtls_lmots_verify will be used for LMOTS |
| 151 | * signature verification. |
| 152 | * |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 153 | * \param params The LMOTS parameter set, q and I values as an |
| 154 | * mbedtls_lmots_parameters_t struct. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 155 | * \param msg The buffer from which the message will be read. |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 156 | * \param msg_size The size of the message that will be read. |
Raef Coles | 2ad6e61 | 2022-08-24 13:33:35 +0100 | [diff] [blame] | 157 | * \param sig The buffer from which the signature will be read. |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 158 | * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from |
| 159 | * this. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 160 | * \param out The buffer where the candidate public key will be |
| 161 | * stored. Must be at least #MBEDTLS_LMOTS_N_HASH_LEN |
| 162 | * bytes in size. |
| 163 | * |
| 164 | * \return \c 0 on success. |
| 165 | * \return A non-zero error code on failure. |
| 166 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 167 | int mbedtls_lmots_calculate_public_key_candidate(const mbedtls_lmots_parameters_t *params, |
| 168 | const unsigned char *msg, |
| 169 | size_t msg_size, |
| 170 | const unsigned char *sig, |
| 171 | size_t sig_size, |
| 172 | unsigned char *out, |
| 173 | size_t out_size, |
| 174 | size_t *out_len); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 175 | |
| 176 | /** |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 177 | * \brief This function verifies a LMOTS signature, using a |
| 178 | * LMOTS context that contains a public key. |
| 179 | * |
| 180 | * \warning This function is **not intended for use in |
| 181 | * production**, due to as-yet unsolved problems with |
Raef Coles | 9b0daf6 | 2022-10-10 14:25:39 +0100 | [diff] [blame] | 182 | * handling stateful keys. The API for this function |
| 183 | * may change considerably in future versions. |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 184 | * |
| 185 | * \note Before this function is called, the context must |
| 186 | * have been initialized and must contain a public key |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 187 | * (either by import or calculation from a private |
| 188 | * key). |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 189 | * |
| 190 | * \param ctx The initialized LMOTS context from which the public |
| 191 | * key will be read. |
| 192 | * \param msg The buffer from which the message will be read. |
| 193 | * \param msg_size The size of the message that will be read. |
| 194 | * \param sig The buf from which the signature will be read. |
| 195 | * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from |
| 196 | * this. |
| 197 | * |
| 198 | * \return \c 0 on successful verification. |
| 199 | * \return A non-zero error code on failure. |
| 200 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 201 | int mbedtls_lmots_verify(const mbedtls_lmots_public_t *ctx, |
| 202 | const unsigned char *msg, |
| 203 | size_t msg_size, const unsigned char *sig, |
| 204 | size_t sig_size); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 205 | |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 206 | #if defined(MBEDTLS_LMS_PRIVATE) |
Raef Coles | ab4f874 | 2022-09-01 12:24:31 +0100 | [diff] [blame] | 207 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 208 | /** |
| 209 | * \brief This function initializes a private LMOTS context |
| 210 | * |
| 211 | * \param ctx The uninitialized LMOTS context that will then be |
| 212 | * initialized. |
| 213 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 214 | void mbedtls_lmots_private_init(mbedtls_lmots_private_t *ctx); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 215 | |
| 216 | /** |
| 217 | * \brief This function uninitializes a private LMOTS context |
| 218 | * |
| 219 | * \param ctx The initialized LMOTS context that will then be |
| 220 | * uninitialized. |
| 221 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 222 | void mbedtls_lmots_private_free(mbedtls_lmots_private_t *ctx); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 223 | |
| 224 | /** |
Raef Coles | 2ac352a | 2022-10-07 11:12:27 +0100 | [diff] [blame] | 225 | * \brief This function calculates an LMOTS private key, and |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 226 | * stores in into an LMOTS context. |
| 227 | * |
| 228 | * \warning This function is **not intended for use in |
| 229 | * production**, due to as-yet unsolved problems with |
Raef Coles | 9b0daf6 | 2022-10-10 14:25:39 +0100 | [diff] [blame] | 230 | * handling stateful keys. The API for this function |
| 231 | * may change considerably in future versions. |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 232 | * |
| 233 | * \note The seed must have at least 256 bits of entropy. |
| 234 | * |
| 235 | * \param ctx The initialized LMOTS context to generate the key |
| 236 | * into. |
| 237 | * \param I_key_identifier The key identifier of the key, as a 16-byte string. |
| 238 | * \param q_leaf_identifier The leaf identifier of key. If this LMOTS key is |
| 239 | * not being used as part of an LMS key, this should |
| 240 | * be set to 0. |
| 241 | * \param seed The seed used to deterministically generate the |
| 242 | * key. |
| 243 | * \param seed_size The length of the seed. |
| 244 | * |
| 245 | * \return \c 0 on success. |
| 246 | * \return A non-zero error code on failure. |
| 247 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 248 | int mbedtls_lmots_generate_private_key(mbedtls_lmots_private_t *ctx, |
| 249 | mbedtls_lmots_algorithm_type_t type, |
| 250 | const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN], |
| 251 | uint32_t q_leaf_identifier, |
| 252 | const unsigned char *seed, |
| 253 | size_t seed_size); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 254 | |
| 255 | /** |
| 256 | * \brief This function generates an LMOTS public key from a |
| 257 | * LMOTS context that already contains a private key. |
| 258 | * |
| 259 | * \note Before this function is called, the context must |
| 260 | * have been initialized and the context must contain |
| 261 | * a private key. |
| 262 | * |
| 263 | * \param ctx The initialized LMOTS context to generate the key |
| 264 | * from and store it into. |
| 265 | * |
| 266 | * \return \c 0 on success. |
| 267 | * \return A non-zero error code on failure. |
| 268 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 269 | int mbedtls_lmots_calculate_public_key(mbedtls_lmots_public_t *ctx, |
| 270 | const mbedtls_lmots_private_t *priv_ctx); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 271 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 272 | /** |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 273 | * \brief This function creates a LMOTS signature, using a |
| 274 | * LMOTS context that contains a private key. |
| 275 | * |
| 276 | * \note Before this function is called, the context must |
| 277 | * have been initialized and must contain a private |
| 278 | * key. |
| 279 | * |
| 280 | * \note LMOTS private keys can only be used once, otherwise |
| 281 | * attackers may be able to create forged signatures. |
| 282 | * If the signing operation is successful, the private |
| 283 | * key in the context will be erased, and no further |
| 284 | * signing will be possible until another private key |
| 285 | * is loaded |
| 286 | * |
| 287 | * \param ctx The initialized LMOTS context from which the |
| 288 | * private key will be read. |
| 289 | * \param f_rng The RNG function to be used for signature |
| 290 | * generation. |
| 291 | * \param p_rng The RNG context to be passed to f_rng |
| 292 | * \param msg The buffer from which the message will be read. |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 293 | * \param msg_size The size of the message that will be read. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 294 | * \param sig The buf into which the signature will be stored. |
| 295 | * Must be at least #MBEDTLS_LMOTS_SIG_LEN in size. |
| 296 | * |
| 297 | * \return \c 0 on success. |
| 298 | * \return A non-zero error code on failure. |
| 299 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 300 | int mbedtls_lmots_sign(mbedtls_lmots_private_t *ctx, |
| 301 | int (*f_rng)(void *, unsigned char *, size_t), |
| 302 | void *p_rng, const unsigned char *msg, size_t msg_size, |
| 303 | unsigned char *sig, size_t sig_size, size_t *sig_len); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 304 | |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 305 | #endif /* defined(MBEDTLS_LMS_PRIVATE) */ |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 306 | |
| 307 | #ifdef __cplusplus |
| 308 | } |
| 309 | #endif |
| 310 | |
| 311 | #endif /* MBEDTLS_LMOTS_H */ |