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 | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 33 | #include <stdint.h> |
| 34 | #include <stddef.h> |
| 35 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 36 | /* Currently only defined for SHA256, 32 is the max hash output size */ |
| 37 | #define MBEDTLS_LMOTS_N_HASH_LEN_MAX (32u) |
| 38 | #define MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX (34u) |
| 39 | #define MBEDTLS_LMOTS_N_HASH_LEN(type) (type == MBEDTLS_LMOTS_SHA256_N32_W8 ? 32u : 0) |
| 40 | #define MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(type) (type == MBEDTLS_LMOTS_SHA256_N32_W8 ? 34u : 0) |
| 41 | #define MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(type) (MBEDTLS_LMOTS_N_HASH_LEN(type)) |
| 42 | #define MBEDTLS_LMOTS_TYPE_LEN (4u) |
| 43 | #define MBEDTLS_LMOTS_I_KEY_ID_LEN (16u) |
| 44 | #define MBEDTLS_LMOTS_Q_LEAF_ID_LEN (4u) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 45 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 46 | #define MBEDTLS_LMOTS_SIG_LEN(type) (MBEDTLS_LMOTS_TYPE_LEN + \ |
| 47 | MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(type) + \ |
| 48 | (MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(type) * \ |
| 49 | MBEDTLS_LMOTS_N_HASH_LEN(type))) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 50 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 51 | #define MBEDTLS_LMOTS_PUBLIC_KEY_LEN(type) (MBEDTLS_LMOTS_TYPE_LEN + \ |
| 52 | MBEDTLS_LMOTS_I_KEY_ID_LEN + \ |
| 53 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN + \ |
| 54 | MBEDTLS_LMOTS_N_HASH_LEN(type)) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 55 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 56 | #define MBEDTLS_LMOTS_SIG_TYPE_OFFSET (0) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 57 | |
| 58 | #ifdef __cplusplus |
| 59 | extern "C" { |
| 60 | #endif |
| 61 | |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 62 | /** The Identifier of the LMS parameter set, as per |
| 63 | * https://www.iana.org/assignments/leighton-micali-signatures/leighton-micali-signatures.xhtml. |
| 64 | * We are only implementing a subset of the types, particularly N32_W8, for the sake of simplicty. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 65 | */ |
| 66 | typedef enum { |
| 67 | MBEDTLS_LMOTS_SHA256_N32_W8 = 4 |
| 68 | } mbedtls_lmots_algorithm_type_t; |
| 69 | |
| 70 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 71 | /** LMOTS parameters structure. |
| 72 | * |
| 73 | * This contains the metadata associated with an LMOTS key, detailing the |
| 74 | * algorithm type, the key ID, and the leaf identifier should be key be part of |
| 75 | * a LMS key. |
| 76 | */ |
| 77 | typedef struct { |
| 78 | unsigned char MBEDTLS_PRIVATE(I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN]); /*!< The key |
| 79 | identifier. */ |
| 80 | unsigned char MBEDTLS_PRIVATE(q_leaf_identifier[MBEDTLS_LMOTS_Q_LEAF_ID_LEN]); /*!< Which |
| 81 | leaf of the LMS key this is. |
| 82 | 0 if the key is not part of an LMS key. */ |
| 83 | mbedtls_lmots_algorithm_type_t MBEDTLS_PRIVATE(type); /*!< The LM-OTS key type identifier as |
| 84 | per IANA. Only SHA256_N32_W8 is |
| 85 | currently supported. */ |
| 86 | } mbedtls_lmots_parameters_t; |
| 87 | |
| 88 | /** LMOTS public context structure. |
| 89 | * |
| 90 | * A LMOTS public key is a hash output, and the applicable parameter set. |
Raef Coles | 2ad6e61 | 2022-08-24 13:33:35 +0100 | [diff] [blame] | 91 | * |
| 92 | * 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] | 93 | * be imported or generated from a private context. |
Raef Coles | 2ad6e61 | 2022-08-24 13:33:35 +0100 | [diff] [blame] | 94 | * |
| 95 | * \dot |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 96 | * digraph lmots_public_t { |
Raef Coles | 2ad6e61 | 2022-08-24 13:33:35 +0100 | [diff] [blame] | 97 | * UNINITIALIZED -> INIT [label="init"]; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 98 | * HAVE_PUBLIC_KEY -> INIT [label="free"]; |
| 99 | * INIT -> HAVE_PUBLIC_KEY [label="import_public_key"]; |
| 100 | * INIT -> HAVE_PUBLIC_KEY [label="calculate_public_key from private key"]; |
| 101 | * HAVE_PUBLIC_KEY -> HAVE_PUBLIC_KEY [label="export_public_key"]; |
Raef Coles | 2ad6e61 | 2022-08-24 13:33:35 +0100 | [diff] [blame] | 102 | * } |
| 103 | * \enddot |
| 104 | */ |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 105 | typedef struct { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 106 | mbedtls_lmots_parameters_t MBEDTLS_PRIVATE(params); |
Raef Coles | fa24f9d | 2022-09-02 17:46:52 +0100 | [diff] [blame^] | 107 | unsigned char MBEDTLS_PRIVATE(public_key)[MBEDTLS_LMOTS_N_HASH_LEN_MAX]; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 108 | unsigned char MBEDTLS_PRIVATE(have_public_key); /*!< Whether the context contains a public key. |
Raef Coles | c464746 | 2022-06-15 12:17:51 +0100 | [diff] [blame] | 109 | Boolean values only. */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 110 | } mbedtls_lmots_public_t; |
| 111 | |
Raef Coles | ab4f874 | 2022-09-01 12:24:31 +0100 | [diff] [blame] | 112 | #ifdef MBEDTLS_LMS_PRIVATE |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 113 | /** LMOTS private context structure. |
| 114 | * |
| 115 | * A LMOTS private key is one hash output for each of digit of the digest + |
| 116 | * checksum, and the applicable parameter set. |
| 117 | * |
| 118 | * The context must be initialized before it is used. A public key must either |
| 119 | * be imported or generated from a private context. |
| 120 | * |
| 121 | * \dot |
| 122 | * digraph lmots_public_t { |
| 123 | * UNINITIALIZED -> INIT [label="init"]; |
| 124 | * HAVE_PRIVATE_KEY -> INIT [label="free"]; |
| 125 | * INIT -> HAVE_PRIVATE_KEY [label="generate_private_key"]; |
| 126 | * HAVE_PRIVATE_KEY -> INIT [label="sign"]; |
| 127 | * } |
| 128 | * \enddot |
| 129 | */ |
| 130 | typedef struct { |
| 131 | mbedtls_lmots_parameters_t MBEDTLS_PRIVATE(params); |
Raef Coles | fa24f9d | 2022-09-02 17:46:52 +0100 | [diff] [blame^] | 132 | unsigned char MBEDTLS_PRIVATE(private_key)[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX]; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 133 | unsigned char MBEDTLS_PRIVATE(have_private_key); /*!< Whether the context contains a private key. |
Raef Coles | c464746 | 2022-06-15 12:17:51 +0100 | [diff] [blame] | 134 | Boolean values only. */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 135 | } mbedtls_lmots_private_t; |
Raef Coles | ab4f874 | 2022-09-01 12:24:31 +0100 | [diff] [blame] | 136 | #endif /* MBEDTLS_LMS_PRIVATE */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 137 | |
| 138 | /** |
| 139 | * \brief This function converts an unsigned int into a |
| 140 | * network-byte-order (big endian) string. |
| 141 | * |
| 142 | * \param val The unsigned integer value |
| 143 | * \param len The length of the string. |
| 144 | * \param bytes The string to output into. |
| 145 | * |
| 146 | * \return The corresponding LMS error code. |
| 147 | */ |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 148 | void unsigned_int_to_network_bytes( unsigned int val, size_t len, |
| 149 | unsigned char *bytes ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 150 | |
| 151 | /** |
| 152 | * \brief This function converts a network-byte-order |
| 153 | * (big endian) string into an unsigned integer. |
| 154 | * |
| 155 | * \param len The length of the string. |
| 156 | * \param bytes The string. |
| 157 | * |
| 158 | * \return The corresponding LMS error code. |
| 159 | */ |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 160 | unsigned int network_bytes_to_unsigned_int( size_t len, |
| 161 | const unsigned char *bytes ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 162 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 163 | /** |
| 164 | * \brief This function converts a \ref psa_status_t to a |
| 165 | * low-level LMS error code. |
| 166 | * |
| 167 | * \param status The psa_status_t to convert |
| 168 | * |
| 169 | * \return The corresponding LMS error code. |
| 170 | */ |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 171 | int mbedtls_lms_error_from_psa( psa_status_t status ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 172 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 173 | |
| 174 | /** |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 175 | * \brief This function initializes a public LMOTS context |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 176 | * |
| 177 | * \param ctx The uninitialized LMOTS context that will then be |
| 178 | * initialized. |
| 179 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 180 | void mbedtls_lmots_init_public( mbedtls_lmots_public_t *ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 181 | |
| 182 | /** |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 183 | * \brief This function uninitializes a public LMOTS context |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 184 | * |
| 185 | * \param ctx The initialized LMOTS context that will then be |
| 186 | * uninitialized. |
| 187 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 188 | void mbedtls_lmots_free_public( mbedtls_lmots_public_t *ctx ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 189 | |
| 190 | /** |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 191 | * \brief This function imports an LMOTS public key into a |
| 192 | * LMOTS context. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 193 | * |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 194 | * \note Before this function is called, the context must |
| 195 | * have been initialized. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 196 | * |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 197 | * \note See IETF RFC8554 for details of the encoding of |
| 198 | * this public key. |
| 199 | * |
| 200 | * \param ctx The initialized LMOTS context store the key in. |
| 201 | * \param key The buffer from which the key will be read. |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 202 | * #MBEDTLS_LMOTS_PUBLIC_KEY_LEN bytes will be read |
| 203 | * from this. |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 204 | * |
| 205 | * \return \c 0 on success. |
| 206 | * \return A non-zero error code on failure. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 207 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 208 | int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx, |
| 209 | const unsigned char *key, size_t key_size ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 210 | |
| 211 | /** |
| 212 | * \brief This function creates a candidate public key from |
| 213 | * an LMOTS signature. This can then be compared to |
| 214 | * the real public key to determine the validity of |
| 215 | * the signature. |
| 216 | * |
| 217 | * \note This function is exposed publicly to be used in LMS |
| 218 | * signature verification, it is expected that |
| 219 | * mbedtls_lmots_verify will be used for LMOTS |
| 220 | * signature verification. |
| 221 | * |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 222 | * \param params The LMOTS parameter set, q and I values as an |
| 223 | * mbedtls_lmots_parameters_t struct. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 224 | * \param msg The buffer from which the message will be read. |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 225 | * \param msg_size The size of the message that will be read. |
Raef Coles | 2ad6e61 | 2022-08-24 13:33:35 +0100 | [diff] [blame] | 226 | * \param sig The buffer from which the signature will be read. |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 227 | * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from |
| 228 | * this. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 229 | * \param out The buffer where the candidate public key will be |
| 230 | * stored. Must be at least #MBEDTLS_LMOTS_N_HASH_LEN |
| 231 | * bytes in size. |
| 232 | * |
| 233 | * \return \c 0 on success. |
| 234 | * \return A non-zero error code on failure. |
| 235 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 236 | int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters_t *params, |
| 237 | const unsigned char *msg, |
| 238 | size_t msg_size, |
| 239 | const unsigned char *sig, |
| 240 | size_t sig_size, |
| 241 | unsigned char *out, |
| 242 | size_t out_size, |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 243 | size_t *out_len ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 244 | |
| 245 | /** |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 246 | * \brief This function verifies a LMOTS signature, using a |
| 247 | * LMOTS context that contains a public key. |
| 248 | * |
| 249 | * \warning This function is **not intended for use in |
| 250 | * production**, due to as-yet unsolved problems with |
| 251 | * handling stateful keys. |
| 252 | * |
| 253 | * \note Before this function is called, the context must |
| 254 | * have been initialized and must contain a public key |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 255 | * (either by import or calculation from a private |
| 256 | * key). |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 257 | * |
| 258 | * \param ctx The initialized LMOTS context from which the public |
| 259 | * key will be read. |
| 260 | * \param msg The buffer from which the message will be read. |
| 261 | * \param msg_size The size of the message that will be read. |
| 262 | * \param sig The buf from which the signature will be read. |
| 263 | * #MBEDTLS_LMOTS_SIG_LEN bytes will be read from |
| 264 | * this. |
| 265 | * |
| 266 | * \return \c 0 on successful verification. |
| 267 | * \return A non-zero error code on failure. |
| 268 | */ |
| 269 | int mbedtls_lmots_verify( mbedtls_lmots_public_t *ctx, const unsigned char *msg, |
| 270 | size_t msg_size, const unsigned char *sig, |
| 271 | size_t sig_size ); |
| 272 | |
Raef Coles | ab4f874 | 2022-09-01 12:24:31 +0100 | [diff] [blame] | 273 | #ifdef MBEDTLS_LMS_PRIVATE |
| 274 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 275 | /** |
| 276 | * \brief This function initializes a private LMOTS context |
| 277 | * |
| 278 | * \param ctx The uninitialized LMOTS context that will then be |
| 279 | * initialized. |
| 280 | */ |
| 281 | void mbedtls_lmots_init_private( mbedtls_lmots_private_t *ctx ); |
| 282 | |
| 283 | /** |
| 284 | * \brief This function uninitializes a private LMOTS context |
| 285 | * |
| 286 | * \param ctx The initialized LMOTS context that will then be |
| 287 | * uninitialized. |
| 288 | */ |
| 289 | void mbedtls_lmots_free_private( mbedtls_lmots_private_t *ctx ); |
| 290 | |
| 291 | /** |
| 292 | * \brief This function generates an LMOTS private key, and |
| 293 | * stores in into an LMOTS context. |
| 294 | * |
| 295 | * \warning This function is **not intended for use in |
| 296 | * production**, due to as-yet unsolved problems with |
| 297 | * handling stateful keys. |
| 298 | * |
| 299 | * \note The seed must have at least 256 bits of entropy. |
| 300 | * |
| 301 | * \param ctx The initialized LMOTS context to generate the key |
| 302 | * into. |
| 303 | * \param I_key_identifier The key identifier of the key, as a 16-byte string. |
| 304 | * \param q_leaf_identifier The leaf identifier of key. If this LMOTS key is |
| 305 | * not being used as part of an LMS key, this should |
| 306 | * be set to 0. |
| 307 | * \param seed The seed used to deterministically generate the |
| 308 | * key. |
| 309 | * \param seed_size The length of the seed. |
| 310 | * |
| 311 | * \return \c 0 on success. |
| 312 | * \return A non-zero error code on failure. |
| 313 | */ |
| 314 | int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx, |
| 315 | mbedtls_lmots_algorithm_type_t type, |
| 316 | const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN], |
| 317 | uint32_t q_leaf_identifier, |
| 318 | const unsigned char *seed, |
| 319 | size_t seed_size ); |
| 320 | |
| 321 | /** |
| 322 | * \brief This function generates an LMOTS public key from a |
| 323 | * LMOTS context that already contains a private key. |
| 324 | * |
| 325 | * \note Before this function is called, the context must |
| 326 | * have been initialized and the context must contain |
| 327 | * a private key. |
| 328 | * |
| 329 | * \param ctx The initialized LMOTS context to generate the key |
| 330 | * from and store it into. |
| 331 | * |
| 332 | * \return \c 0 on success. |
| 333 | * \return A non-zero error code on failure. |
| 334 | */ |
| 335 | int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx, |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 336 | mbedtls_lmots_private_t *priv_ctx ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 337 | |
| 338 | |
| 339 | /** |
| 340 | * \brief This function exports an LMOTS public key from a |
| 341 | * LMOTS context that already contains a public key. |
| 342 | * |
| 343 | * \note Before this function is called, the context must |
| 344 | * have been initialized and the context must contain |
| 345 | * a public key. |
| 346 | * |
| 347 | * \note See IETF RFC8554 for details of the encoding of |
| 348 | * this public key. |
| 349 | * |
| 350 | * \param ctx The initialized LMOTS context that contains the |
| 351 | * publc key. |
| 352 | * \param key The buffer into which the key will be output. Must |
| 353 | * be at least #MBEDTLS_LMOTS_PUBLIC_KEY_LEN in size. |
| 354 | * |
| 355 | * \return \c 0 on success. |
| 356 | * \return A non-zero error code on failure. |
| 357 | */ |
| 358 | int mbedtls_lmots_export_public_key( mbedtls_lmots_public_t *ctx, |
| 359 | unsigned char *key, size_t key_size, |
| 360 | size_t *key_len ); |
| 361 | /** |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 362 | * \brief This function creates a LMOTS signature, using a |
| 363 | * LMOTS context that contains a private key. |
| 364 | * |
| 365 | * \note Before this function is called, the context must |
| 366 | * have been initialized and must contain a private |
| 367 | * key. |
| 368 | * |
| 369 | * \note LMOTS private keys can only be used once, otherwise |
| 370 | * attackers may be able to create forged signatures. |
| 371 | * If the signing operation is successful, the private |
| 372 | * key in the context will be erased, and no further |
| 373 | * signing will be possible until another private key |
| 374 | * is loaded |
| 375 | * |
| 376 | * \param ctx The initialized LMOTS context from which the |
| 377 | * private key will be read. |
| 378 | * \param f_rng The RNG function to be used for signature |
| 379 | * generation. |
| 380 | * \param p_rng The RNG context to be passed to f_rng |
| 381 | * \param msg The buffer from which the message will be read. |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 382 | * \param msg_size The size of the message that will be read. |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 383 | * \param sig The buf into which the signature will be stored. |
| 384 | * Must be at least #MBEDTLS_LMOTS_SIG_LEN in size. |
| 385 | * |
| 386 | * \return \c 0 on success. |
| 387 | * \return A non-zero error code on failure. |
| 388 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 389 | int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx, |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 390 | int (*f_rng)(void *, unsigned char *, size_t), |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 391 | void *p_rng, const unsigned char *msg, size_t msg_size, |
| 392 | unsigned char *sig, size_t sig_size, size_t* sig_len ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 393 | |
Raef Coles | ab4f874 | 2022-09-01 12:24:31 +0100 | [diff] [blame] | 394 | #endif /* MBEDTLS_LMS_PRIVATE */ |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 395 | |
| 396 | #ifdef __cplusplus |
| 397 | } |
| 398 | #endif |
| 399 | |
| 400 | #endif /* MBEDTLS_LMOTS_H */ |