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