Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * The LM-OTS one-time public-key signature scheme |
| 3 | * |
| 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame^] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * The following sources were referenced in the design of this implementation |
| 10 | * of the LM-OTS algorithm: |
| 11 | * |
| 12 | * [1] IETF RFC8554 |
| 13 | * D. McGrew, M. Curcio, S.Fluhrer |
| 14 | * https://datatracker.ietf.org/doc/html/rfc8554 |
| 15 | * |
| 16 | * [2] NIST Special Publication 800-208 |
| 17 | * David A. Cooper et. al. |
| 18 | * https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-208.pdf |
| 19 | */ |
| 20 | |
| 21 | #include "common.h" |
| 22 | |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 23 | #if defined(MBEDTLS_LMS_C) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 24 | |
| 25 | #include <string.h> |
| 26 | |
Raef Coles | 7dce69a | 2022-08-24 14:07:06 +0100 | [diff] [blame] | 27 | #include "lmots.h" |
| 28 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 29 | #include "mbedtls/lms.h" |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 30 | #include "mbedtls/platform_util.h" |
| 31 | #include "mbedtls/error.h" |
Manuel Pégourié-Gonnard | 2be8c63 | 2023-06-07 13:07:21 +0200 | [diff] [blame] | 32 | #include "psa_util_internal.h" |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 33 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 34 | #include "psa/crypto.h" |
| 35 | |
Andrzej Kurek | 0064484 | 2023-05-30 05:45:00 -0400 | [diff] [blame] | 36 | /* Define a local translating function to save code size by not using too many |
| 37 | * arguments in each translating place. */ |
| 38 | static int local_err_translation(psa_status_t status) |
| 39 | { |
| 40 | return psa_status_to_mbedtls(status, psa_to_lms_errors, |
Andrzej Kurek | 1e4a030 | 2023-05-30 09:45:17 -0400 | [diff] [blame] | 41 | ARRAY_LENGTH(psa_to_lms_errors), |
Andrzej Kurek | 0064484 | 2023-05-30 05:45:00 -0400 | [diff] [blame] | 42 | psa_generic_status_to_mbedtls); |
| 43 | } |
| 44 | #define PSA_TO_MBEDTLS_ERR(status) local_err_translation(status) |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 45 | |
Raef Coles | 57d5328 | 2022-09-27 11:30:51 +0100 | [diff] [blame] | 46 | #define PUBLIC_KEY_TYPE_OFFSET (0) |
| 47 | #define PUBLIC_KEY_I_KEY_ID_OFFSET (PUBLIC_KEY_TYPE_OFFSET + \ |
| 48 | MBEDTLS_LMOTS_TYPE_LEN) |
| 49 | #define PUBLIC_KEY_Q_LEAF_ID_OFFSET (PUBLIC_KEY_I_KEY_ID_OFFSET + \ |
| 50 | MBEDTLS_LMOTS_I_KEY_ID_LEN) |
| 51 | #define PUBLIC_KEY_KEY_HASH_OFFSET (PUBLIC_KEY_Q_LEAF_ID_OFFSET + \ |
| 52 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 53 | |
| 54 | /* We only support parameter sets that use 8-bit digits, as it does not require |
| 55 | * translation logic between digits and bytes */ |
| 56 | #define W_WINTERNITZ_PARAMETER (8u) |
| 57 | #define CHECKSUM_LEN (2) |
| 58 | #define I_DIGIT_IDX_LEN (2) |
| 59 | #define J_HASH_IDX_LEN (1) |
| 60 | #define D_CONST_LEN (2) |
| 61 | |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 62 | #define DIGIT_MAX_VALUE ((1u << W_WINTERNITZ_PARAMETER) - 1u) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 63 | |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 64 | #define D_CONST_LEN (2) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 65 | static const unsigned char D_PUBLIC_CONSTANT_BYTES[D_CONST_LEN] = { 0x80, 0x80 }; |
| 66 | static const unsigned char D_MESSAGE_CONSTANT_BYTES[D_CONST_LEN] = { 0x81, 0x81 }; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 67 | |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 68 | #if defined(MBEDTLS_TEST_HOOKS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 69 | int (*mbedtls_lmots_sign_private_key_invalidated_hook)(unsigned char *) = NULL; |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 70 | #endif /* defined(MBEDTLS_TEST_HOOKS) */ |
| 71 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 72 | void mbedtls_lms_unsigned_int_to_network_bytes(unsigned int val, size_t len, |
| 73 | unsigned char *bytes) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 74 | { |
| 75 | size_t idx; |
| 76 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 77 | for (idx = 0; idx < len; idx++) { |
| 78 | bytes[idx] = (val >> ((len - 1 - idx) * 8)) & 0xFF; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 82 | unsigned int mbedtls_lms_network_bytes_to_unsigned_int(size_t len, |
| 83 | const unsigned char *bytes) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 84 | { |
| 85 | size_t idx; |
| 86 | unsigned int val = 0; |
| 87 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 88 | for (idx = 0; idx < len; idx++) { |
| 89 | val |= ((unsigned int) bytes[idx]) << (8 * (len - 1 - idx)); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 90 | } |
| 91 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | return val; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 95 | /* Calculate the checksum digits that are appended to the end of the LMOTS digit |
| 96 | * string. See NIST SP800-208 section 3.1 or RFC8554 Algorithm 2 for details of |
| 97 | * the checksum algorithm. |
| 98 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 99 | * params The LMOTS parameter set, I and q values which |
| 100 | * describe the key being used. |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 101 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 102 | * digest The digit string to create the digest from. As |
| 103 | * this does not contain a checksum, it is the same |
| 104 | * size as a hash output. |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 105 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 106 | static unsigned short lmots_checksum_calculate(const mbedtls_lmots_parameters_t *params, |
| 107 | const unsigned char *digest) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 108 | { |
| 109 | size_t idx; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 110 | unsigned sum = 0; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 111 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | for (idx = 0; idx < MBEDTLS_LMOTS_N_HASH_LEN(params->type); idx++) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 113 | sum += DIGIT_MAX_VALUE - digest[idx]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 114 | } |
| 115 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | return sum; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 119 | /* Create the string of digest digits (in the base determined by the Winternitz |
| 120 | * parameter with the checksum appended to the end (Q || cksm(Q)). See NIST |
| 121 | * SP800-208 section 3.1 or RFC8554 Algorithm 3 step 5 (also used in Algorithm |
| 122 | * 4b step 3) for details. |
| 123 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 124 | * params The LMOTS parameter set, I and q values which |
| 125 | * describe the key being used. |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 126 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 127 | * msg The message that will be hashed to create the |
| 128 | * digest. |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 129 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 130 | * msg_size The size of the message. |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 131 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 132 | * C_random_value The random value that will be combined with the |
| 133 | * message digest. This is always the same size as a |
| 134 | * hash output for whichever hash algorithm is |
| 135 | * determined by the parameter set. |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 136 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 137 | * output An output containing the digit string (+ |
| 138 | * checksum) of length P digits (in the case of |
| 139 | * MBEDTLS_LMOTS_SHA256_N32_W8, this means it is of |
| 140 | * size P bytes). |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 141 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 142 | static int create_digit_array_with_checksum(const mbedtls_lmots_parameters_t *params, |
| 143 | const unsigned char *msg, |
| 144 | size_t msg_len, |
| 145 | const unsigned char *C_random_value, |
| 146 | unsigned char *out) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 147 | { |
Raef Coles | be0c2f9 | 2022-10-07 11:27:35 +0100 | [diff] [blame] | 148 | psa_hash_operation_t op = PSA_HASH_OPERATION_INIT; |
| 149 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 150 | size_t output_hash_len; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 151 | unsigned short checksum; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 152 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 153 | status = psa_hash_setup(&op, PSA_ALG_SHA_256); |
| 154 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 155 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 156 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 157 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 158 | status = psa_hash_update(&op, params->I_key_identifier, |
| 159 | MBEDTLS_LMOTS_I_KEY_ID_LEN); |
| 160 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 161 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 162 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 164 | status = psa_hash_update(&op, params->q_leaf_identifier, |
| 165 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN); |
| 166 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 167 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 168 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 170 | status = psa_hash_update(&op, D_MESSAGE_CONSTANT_BYTES, D_CONST_LEN); |
| 171 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 172 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 173 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 175 | status = psa_hash_update(&op, C_random_value, |
| 176 | MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(params->type)); |
| 177 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 178 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 179 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 180 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 181 | status = psa_hash_update(&op, msg, msg_len); |
| 182 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 183 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 184 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 186 | status = psa_hash_finish(&op, out, |
| 187 | MBEDTLS_LMOTS_N_HASH_LEN(params->type), |
| 188 | &output_hash_len); |
| 189 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 190 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 191 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 192 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 193 | checksum = lmots_checksum_calculate(params, out); |
| 194 | mbedtls_lms_unsigned_int_to_network_bytes(checksum, CHECKSUM_LEN, |
| 195 | out + MBEDTLS_LMOTS_N_HASH_LEN(params->type)); |
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 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 198 | psa_hash_abort(&op); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 199 | |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 200 | return PSA_TO_MBEDTLS_ERR(status); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 203 | /* Hash each element of the string of digits (+ checksum), producing a hash |
| 204 | * output for each element. This is used in several places (by varying the |
| 205 | * hash_idx_min/max_values) in order to calculate a public key from a private |
| 206 | * key (RFC8554 Algorithm 1 step 4), in order to sign a message (RFC8554 |
| 207 | * Algorithm 3 step 5), and to calculate a public key candidate from a |
| 208 | * signature and message (RFC8554 Algorithm 4b step 3). |
| 209 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 210 | * params The LMOTS parameter set, I and q values which |
| 211 | * describe the key being used. |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 212 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 213 | * x_digit_array The array of digits (of size P, 34 in the case of |
| 214 | * MBEDTLS_LMOTS_SHA256_N32_W8). |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 215 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 216 | * hash_idx_min_values An array of the starting values of the j iterator |
| 217 | * for each of the members of the digit array. If |
| 218 | * this value in NULL, then all iterators will start |
| 219 | * at 0. |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 220 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 221 | * hash_idx_max_values An array of the upper bound values of the j |
| 222 | * iterator for each of the members of the digit |
| 223 | * array. If this value in NULL, then iterator is |
| 224 | * bounded to be less than 2^w - 1 (255 in the case |
| 225 | * of MBEDTLS_LMOTS_SHA256_N32_W8) |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 226 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 227 | * output An array containing a hash output for each member |
| 228 | * of the digit string P. In the case of |
| 229 | * MBEDTLS_LMOTS_SHA256_N32_W8, this is of size 32 * |
| 230 | * 34. |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 231 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 232 | static int hash_digit_array(const mbedtls_lmots_parameters_t *params, |
| 233 | const unsigned char *x_digit_array, |
| 234 | const unsigned char *hash_idx_min_values, |
| 235 | const unsigned char *hash_idx_max_values, |
| 236 | unsigned char *output) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 237 | { |
Raef Coles | 8738a49 | 2022-09-02 17:13:01 +0100 | [diff] [blame] | 238 | unsigned int i_digit_idx; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 239 | unsigned char i_digit_idx_bytes[I_DIGIT_IDX_LEN]; |
Raef Coles | 8738a49 | 2022-09-02 17:13:01 +0100 | [diff] [blame] | 240 | unsigned int j_hash_idx; |
| 241 | unsigned char j_hash_idx_bytes[J_HASH_IDX_LEN]; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 242 | unsigned int j_hash_idx_min; |
| 243 | unsigned int j_hash_idx_max; |
Raef Coles | be0c2f9 | 2022-10-07 11:27:35 +0100 | [diff] [blame] | 244 | psa_hash_operation_t op = PSA_HASH_OPERATION_INIT; |
| 245 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 246 | size_t output_hash_len; |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 247 | unsigned char tmp_hash[MBEDTLS_LMOTS_N_HASH_LEN_MAX]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 248 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 249 | for (i_digit_idx = 0; |
| 250 | i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type); |
| 251 | i_digit_idx++) { |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 252 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 253 | memcpy(tmp_hash, |
| 254 | &x_digit_array[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)], |
| 255 | MBEDTLS_LMOTS_N_HASH_LEN(params->type)); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 256 | |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 257 | j_hash_idx_min = hash_idx_min_values != NULL ? |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 258 | hash_idx_min_values[i_digit_idx] : 0; |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 259 | j_hash_idx_max = hash_idx_max_values != NULL ? |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 260 | hash_idx_max_values[i_digit_idx] : DIGIT_MAX_VALUE; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 261 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 262 | for (j_hash_idx = j_hash_idx_min; |
| 263 | j_hash_idx < j_hash_idx_max; |
| 264 | j_hash_idx++) { |
| 265 | status = psa_hash_setup(&op, PSA_ALG_SHA_256); |
| 266 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 267 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 268 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 269 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 270 | status = psa_hash_update(&op, |
| 271 | params->I_key_identifier, |
| 272 | MBEDTLS_LMOTS_I_KEY_ID_LEN); |
| 273 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 274 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 275 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 276 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 277 | status = psa_hash_update(&op, |
| 278 | params->q_leaf_identifier, |
| 279 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN); |
| 280 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 281 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 282 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 283 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | mbedtls_lms_unsigned_int_to_network_bytes(i_digit_idx, |
| 285 | I_DIGIT_IDX_LEN, |
| 286 | i_digit_idx_bytes); |
| 287 | status = psa_hash_update(&op, i_digit_idx_bytes, I_DIGIT_IDX_LEN); |
| 288 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 289 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 290 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 291 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 292 | mbedtls_lms_unsigned_int_to_network_bytes(j_hash_idx, |
| 293 | J_HASH_IDX_LEN, |
| 294 | j_hash_idx_bytes); |
| 295 | status = psa_hash_update(&op, j_hash_idx_bytes, J_HASH_IDX_LEN); |
| 296 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 297 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 298 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 299 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 300 | status = psa_hash_update(&op, tmp_hash, |
| 301 | MBEDTLS_LMOTS_N_HASH_LEN(params->type)); |
| 302 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 303 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 304 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 306 | status = psa_hash_finish(&op, tmp_hash, sizeof(tmp_hash), |
| 307 | &output_hash_len); |
| 308 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 309 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 310 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 312 | psa_hash_abort(&op); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 313 | } |
| 314 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 315 | memcpy(&output[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)], |
| 316 | tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN(params->type)); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 317 | } |
| 318 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 319 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 320 | psa_hash_abort(&op); |
| 321 | mbedtls_platform_zeroize(tmp_hash, sizeof(tmp_hash)); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 322 | |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 323 | return PSA_TO_MBEDTLS_ERR(status); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 324 | } |
| 325 | |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 326 | /* Combine the hashes of the digit array into a public key. This is used in |
| 327 | * in order to calculate a public key from a private key (RFC8554 Algorithm 1 |
| 328 | * step 4), and to calculate a public key candidate from a signature and message |
| 329 | * (RFC8554 Algorithm 4b step 3). |
| 330 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 331 | * params The LMOTS parameter set, I and q values which describe |
| 332 | * the key being used. |
| 333 | * y_hashed_digits The array of hashes, one hash for each digit of the |
| 334 | * symbol array (which is of size P, 34 in the case of |
| 335 | * MBEDTLS_LMOTS_SHA256_N32_W8) |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 336 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 337 | * pub_key The output public key (or candidate public key in |
| 338 | * case this is being run as part of signature |
| 339 | * verification), in the form of a hash output. |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 340 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 341 | static int public_key_from_hashed_digit_array(const mbedtls_lmots_parameters_t *params, |
| 342 | const unsigned char *y_hashed_digits, |
| 343 | unsigned char *pub_key) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 344 | { |
Raef Coles | be0c2f9 | 2022-10-07 11:27:35 +0100 | [diff] [blame] | 345 | psa_hash_operation_t op = PSA_HASH_OPERATION_INIT; |
| 346 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 347 | size_t output_hash_len; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 349 | status = psa_hash_setup(&op, PSA_ALG_SHA_256); |
| 350 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 351 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 352 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 353 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 354 | status = psa_hash_update(&op, |
| 355 | params->I_key_identifier, |
| 356 | MBEDTLS_LMOTS_I_KEY_ID_LEN); |
| 357 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 358 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 359 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 360 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 361 | status = psa_hash_update(&op, params->q_leaf_identifier, |
| 362 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN); |
| 363 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 364 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 365 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 366 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 367 | status = psa_hash_update(&op, D_PUBLIC_CONSTANT_BYTES, D_CONST_LEN); |
| 368 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 369 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 370 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | status = psa_hash_update(&op, y_hashed_digits, |
| 373 | MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type) * |
| 374 | MBEDTLS_LMOTS_N_HASH_LEN(params->type)); |
| 375 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 376 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 377 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 378 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 379 | status = psa_hash_finish(&op, pub_key, |
| 380 | MBEDTLS_LMOTS_N_HASH_LEN(params->type), |
| 381 | &output_hash_len); |
| 382 | if (status != PSA_SUCCESS) { |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 383 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 384 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 385 | psa_hash_abort(&op); |
| 386 | } |
Raef Coles | 29117d2 | 2022-10-07 11:46:06 +0100 | [diff] [blame] | 387 | |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 388 | return PSA_TO_MBEDTLS_ERR(status); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 389 | } |
| 390 | |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 391 | #if !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 392 | int mbedtls_lms_error_from_psa(psa_status_t status) |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 393 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 394 | switch (status) { |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 395 | case PSA_SUCCESS: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 396 | return 0; |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 397 | case PSA_ERROR_HARDWARE_FAILURE: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 398 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 399 | case PSA_ERROR_NOT_SUPPORTED: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | return MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED; |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 401 | case PSA_ERROR_BUFFER_TOO_SMALL: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 402 | return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL; |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 403 | case PSA_ERROR_INVALID_ARGUMENT: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 404 | return MBEDTLS_ERR_LMS_BAD_INPUT_DATA; |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 405 | default: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 406 | return MBEDTLS_ERR_ERROR_GENERIC_ERROR; |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 407 | } |
| 408 | } |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 409 | #endif /* !MBEDTLS_DEPRECATED_REMOVED */ |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 410 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 411 | void mbedtls_lmots_public_init(mbedtls_lmots_public_t *ctx) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 412 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 413 | memset(ctx, 0, sizeof(*ctx)); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 414 | } |
| 415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 416 | void mbedtls_lmots_public_free(mbedtls_lmots_public_t *ctx) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 417 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 418 | mbedtls_platform_zeroize(ctx, sizeof(*ctx)); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 419 | } |
| 420 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 421 | int mbedtls_lmots_import_public_key(mbedtls_lmots_public_t *ctx, |
| 422 | const unsigned char *key, size_t key_len) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 423 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 424 | if (key_len < MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) { |
| 425 | return MBEDTLS_ERR_LMS_BAD_INPUT_DATA; |
Raef Coles | e89488d | 2022-10-07 16:06:35 +0100 | [diff] [blame] | 426 | } |
| 427 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 428 | ctx->params.type = |
Agathiyan Bragadeesh | 10b6775 | 2023-07-12 11:19:17 +0100 | [diff] [blame] | 429 | (mbedtls_lmots_algorithm_type_t) mbedtls_lms_network_bytes_to_unsigned_int( |
| 430 | MBEDTLS_LMOTS_TYPE_LEN, |
| 431 | key + |
| 432 | MBEDTLS_LMOTS_SIG_TYPE_OFFSET); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 433 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 434 | if (key_len != MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type)) { |
| 435 | return MBEDTLS_ERR_LMS_BAD_INPUT_DATA; |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 436 | } |
| 437 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 438 | memcpy(ctx->params.I_key_identifier, |
| 439 | key + PUBLIC_KEY_I_KEY_ID_OFFSET, |
| 440 | MBEDTLS_LMOTS_I_KEY_ID_LEN); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 441 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 442 | memcpy(ctx->params.q_leaf_identifier, |
| 443 | key + PUBLIC_KEY_Q_LEAF_ID_OFFSET, |
| 444 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 445 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 446 | memcpy(ctx->public_key, |
| 447 | key + PUBLIC_KEY_KEY_HASH_OFFSET, |
| 448 | MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type)); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 449 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 450 | ctx->have_public_key = 1; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 451 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 452 | return 0; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 453 | } |
| 454 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 455 | int mbedtls_lmots_export_public_key(const mbedtls_lmots_public_t *ctx, |
| 456 | unsigned char *key, size_t key_size, |
| 457 | size_t *key_len) |
Raef Coles | 370cc43 | 2022-10-07 16:07:33 +0100 | [diff] [blame] | 458 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 459 | if (key_size < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type)) { |
| 460 | return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL; |
Raef Coles | 370cc43 | 2022-10-07 16:07:33 +0100 | [diff] [blame] | 461 | } |
| 462 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 463 | if (!ctx->have_public_key) { |
| 464 | return MBEDTLS_ERR_LMS_BAD_INPUT_DATA; |
Raef Coles | 370cc43 | 2022-10-07 16:07:33 +0100 | [diff] [blame] | 465 | } |
| 466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 467 | mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.type, |
| 468 | MBEDTLS_LMOTS_TYPE_LEN, |
| 469 | key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET); |
Raef Coles | 370cc43 | 2022-10-07 16:07:33 +0100 | [diff] [blame] | 470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 471 | memcpy(key + PUBLIC_KEY_I_KEY_ID_OFFSET, |
| 472 | ctx->params.I_key_identifier, |
| 473 | MBEDTLS_LMOTS_I_KEY_ID_LEN); |
Raef Coles | 370cc43 | 2022-10-07 16:07:33 +0100 | [diff] [blame] | 474 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 475 | memcpy(key + PUBLIC_KEY_Q_LEAF_ID_OFFSET, |
| 476 | ctx->params.q_leaf_identifier, |
| 477 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN); |
Raef Coles | 370cc43 | 2022-10-07 16:07:33 +0100 | [diff] [blame] | 478 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 479 | memcpy(key + PUBLIC_KEY_KEY_HASH_OFFSET, ctx->public_key, |
| 480 | MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type)); |
Raef Coles | 370cc43 | 2022-10-07 16:07:33 +0100 | [diff] [blame] | 481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | if (key_len != NULL) { |
Raef Coles | 370cc43 | 2022-10-07 16:07:33 +0100 | [diff] [blame] | 483 | *key_len = MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type); |
| 484 | } |
| 485 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 486 | return 0; |
Raef Coles | 370cc43 | 2022-10-07 16:07:33 +0100 | [diff] [blame] | 487 | } |
| 488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 489 | int mbedtls_lmots_calculate_public_key_candidate(const mbedtls_lmots_parameters_t *params, |
| 490 | const unsigned char *msg, |
| 491 | size_t msg_size, |
| 492 | const unsigned char *sig, |
| 493 | size_t sig_size, |
| 494 | unsigned char *out, |
| 495 | size_t out_size, |
| 496 | size_t *out_len) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 497 | { |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 498 | unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX]; |
| 499 | unsigned char y_hashed_digits[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 500 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 502 | if (msg == NULL && msg_size != 0) { |
| 503 | return MBEDTLS_ERR_LMS_BAD_INPUT_DATA; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 504 | } |
| 505 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 506 | if (sig_size != MBEDTLS_LMOTS_SIG_LEN(params->type) || |
| 507 | out_size < MBEDTLS_LMOTS_N_HASH_LEN(params->type)) { |
| 508 | return MBEDTLS_ERR_LMS_BAD_INPUT_DATA; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 509 | } |
| 510 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 511 | ret = create_digit_array_with_checksum(params, msg, msg_size, |
| 512 | sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, |
| 513 | tmp_digit_array); |
| 514 | if (ret) { |
| 515 | return ret; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 516 | } |
| 517 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 518 | ret = hash_digit_array(params, |
| 519 | sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(params->type), |
| 520 | tmp_digit_array, NULL, (unsigned char *) y_hashed_digits); |
| 521 | if (ret) { |
| 522 | return ret; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 523 | } |
| 524 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 525 | ret = public_key_from_hashed_digit_array(params, |
| 526 | (unsigned char *) y_hashed_digits, |
| 527 | out); |
| 528 | if (ret) { |
| 529 | return ret; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 530 | } |
| 531 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 532 | if (out_len != NULL) { |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 533 | *out_len = MBEDTLS_LMOTS_N_HASH_LEN(params->type); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 534 | } |
| 535 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 536 | return 0; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 537 | } |
| 538 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 539 | int mbedtls_lmots_verify(const mbedtls_lmots_public_t *ctx, |
| 540 | const unsigned char *msg, size_t msg_size, |
| 541 | const unsigned char *sig, size_t sig_size) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 542 | { |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 543 | unsigned char Kc_public_key_candidate[MBEDTLS_LMOTS_N_HASH_LEN_MAX]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 544 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 545 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 546 | if (msg == NULL && msg_size != 0) { |
| 547 | return MBEDTLS_ERR_LMS_BAD_INPUT_DATA; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 548 | } |
| 549 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 550 | if (!ctx->have_public_key) { |
| 551 | return MBEDTLS_ERR_LMS_BAD_INPUT_DATA; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 552 | } |
| 553 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 554 | if (ctx->params.type != MBEDTLS_LMOTS_SHA256_N32_W8) { |
| 555 | return MBEDTLS_ERR_LMS_BAD_INPUT_DATA; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 556 | } |
| 557 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 558 | if (sig_size < MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) { |
| 559 | return MBEDTLS_ERR_LMS_VERIFY_FAILED; |
Raef Coles | 4829459 | 2022-10-10 16:40:00 +0100 | [diff] [blame] | 560 | } |
| 561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 562 | if (mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMOTS_TYPE_LEN, |
| 563 | sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET) != |
| 564 | MBEDTLS_LMOTS_SHA256_N32_W8) { |
| 565 | return MBEDTLS_ERR_LMS_VERIFY_FAILED; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 566 | } |
| 567 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 568 | ret = mbedtls_lmots_calculate_public_key_candidate(&ctx->params, |
| 569 | msg, msg_size, sig, sig_size, |
| 570 | Kc_public_key_candidate, |
| 571 | MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type), |
| 572 | NULL); |
| 573 | if (ret) { |
| 574 | return MBEDTLS_ERR_LMS_VERIFY_FAILED; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 575 | } |
| 576 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 577 | if (memcmp(&Kc_public_key_candidate, ctx->public_key, |
| 578 | sizeof(ctx->public_key))) { |
| 579 | return MBEDTLS_ERR_LMS_VERIFY_FAILED; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 580 | } |
| 581 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 582 | return 0; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 583 | } |
| 584 | |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 585 | #if defined(MBEDTLS_LMS_PRIVATE) |
Raef Coles | ab4f874 | 2022-09-01 12:24:31 +0100 | [diff] [blame] | 586 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 587 | void mbedtls_lmots_private_init(mbedtls_lmots_private_t *ctx) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 588 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 589 | memset(ctx, 0, sizeof(*ctx)); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 590 | } |
| 591 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 592 | void mbedtls_lmots_private_free(mbedtls_lmots_private_t *ctx) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 593 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 594 | mbedtls_platform_zeroize(ctx, |
| 595 | sizeof(*ctx)); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 596 | } |
| 597 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 598 | int mbedtls_lmots_generate_private_key(mbedtls_lmots_private_t *ctx, |
| 599 | mbedtls_lmots_algorithm_type_t type, |
| 600 | const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN], |
| 601 | uint32_t q_leaf_identifier, |
| 602 | const unsigned char *seed, |
| 603 | size_t seed_size) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 604 | { |
Raef Coles | be0c2f9 | 2022-10-07 11:27:35 +0100 | [diff] [blame] | 605 | psa_hash_operation_t op = PSA_HASH_OPERATION_INIT; |
| 606 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 607 | size_t output_hash_len; |
| 608 | unsigned int i_digit_idx; |
| 609 | unsigned char i_digit_idx_bytes[2]; |
| 610 | unsigned char const_bytes[1]; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 611 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 612 | if (ctx->have_private_key) { |
| 613 | return MBEDTLS_ERR_LMS_BAD_INPUT_DATA; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 614 | } |
| 615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 616 | if (type != MBEDTLS_LMOTS_SHA256_N32_W8) { |
| 617 | return MBEDTLS_ERR_LMS_BAD_INPUT_DATA; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 618 | } |
| 619 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 620 | ctx->params.type = type; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 621 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 622 | memcpy(ctx->params.I_key_identifier, |
| 623 | I_key_identifier, |
| 624 | sizeof(ctx->params.I_key_identifier)); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 625 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 626 | mbedtls_lms_unsigned_int_to_network_bytes(q_leaf_identifier, |
| 627 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN, |
| 628 | ctx->params.q_leaf_identifier); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 629 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 630 | mbedtls_lms_unsigned_int_to_network_bytes(0xFF, sizeof(const_bytes), |
| 631 | const_bytes); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 632 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 633 | for (i_digit_idx = 0; |
| 634 | i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type); |
| 635 | i_digit_idx++) { |
| 636 | status = psa_hash_setup(&op, PSA_ALG_SHA_256); |
| 637 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 638 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 639 | } |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 640 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 641 | status = psa_hash_update(&op, |
| 642 | ctx->params.I_key_identifier, |
| 643 | sizeof(ctx->params.I_key_identifier)); |
| 644 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 645 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 646 | } |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 647 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 648 | status = psa_hash_update(&op, |
| 649 | ctx->params.q_leaf_identifier, |
| 650 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN); |
| 651 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 652 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 653 | } |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 654 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 655 | mbedtls_lms_unsigned_int_to_network_bytes(i_digit_idx, I_DIGIT_IDX_LEN, |
| 656 | i_digit_idx_bytes); |
| 657 | status = psa_hash_update(&op, i_digit_idx_bytes, I_DIGIT_IDX_LEN); |
| 658 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 659 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 660 | } |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 661 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 662 | status = psa_hash_update(&op, const_bytes, sizeof(const_bytes)); |
| 663 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 664 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 665 | } |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 667 | status = psa_hash_update(&op, seed, seed_size); |
| 668 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 669 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 670 | } |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 672 | status = psa_hash_finish(&op, |
| 673 | ctx->private_key[i_digit_idx], |
| 674 | MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type), |
| 675 | &output_hash_len); |
| 676 | if (status != PSA_SUCCESS) { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 677 | goto exit; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 678 | } |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 679 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 680 | psa_hash_abort(&op); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 681 | } |
| 682 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 683 | ctx->have_private_key = 1; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 684 | |
| 685 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 686 | psa_hash_abort(&op); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 687 | |
Andrzej Kurek | 8a045ce | 2022-12-23 11:00:06 -0500 | [diff] [blame] | 688 | return PSA_TO_MBEDTLS_ERR(status); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 689 | } |
| 690 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 691 | int mbedtls_lmots_calculate_public_key(mbedtls_lmots_public_t *ctx, |
| 692 | const mbedtls_lmots_private_t *priv_ctx) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 693 | { |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 694 | unsigned char y_hashed_digits[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] | 695 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 696 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 697 | /* Check that a private key is loaded */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 698 | if (!priv_ctx->have_private_key) { |
| 699 | return MBEDTLS_ERR_LMS_BAD_INPUT_DATA; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 700 | } |
| 701 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 702 | ret = hash_digit_array(&priv_ctx->params, |
| 703 | (unsigned char *) priv_ctx->private_key, NULL, |
| 704 | NULL, (unsigned char *) y_hashed_digits); |
| 705 | if (ret) { |
Raef Coles | 142e577 | 2022-10-12 10:47:27 +0100 | [diff] [blame] | 706 | goto exit; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 707 | } |
| 708 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 709 | ret = public_key_from_hashed_digit_array(&priv_ctx->params, |
| 710 | (unsigned char *) y_hashed_digits, |
| 711 | ctx->public_key); |
| 712 | if (ret) { |
Raef Coles | 142e577 | 2022-10-12 10:47:27 +0100 | [diff] [blame] | 713 | goto exit; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 714 | } |
| 715 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 716 | memcpy(&ctx->params, &priv_ctx->params, |
| 717 | sizeof(ctx->params)); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 718 | |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 719 | ctx->have_public_key = 1; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 720 | |
Raef Coles | 142e577 | 2022-10-12 10:47:27 +0100 | [diff] [blame] | 721 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 722 | mbedtls_platform_zeroize(y_hashed_digits, sizeof(y_hashed_digits)); |
Raef Coles | 142e577 | 2022-10-12 10:47:27 +0100 | [diff] [blame] | 723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 724 | return ret; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 725 | } |
| 726 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 727 | int mbedtls_lmots_sign(mbedtls_lmots_private_t *ctx, |
| 728 | int (*f_rng)(void *, unsigned char *, size_t), |
| 729 | void *p_rng, const unsigned char *msg, size_t msg_size, |
| 730 | unsigned char *sig, size_t sig_size, size_t *sig_len) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 731 | { |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 732 | unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX]; |
Raef Coles | 891c613 | 2022-09-01 11:05:48 +0100 | [diff] [blame] | 733 | /* Create a temporary buffer to prepare the signature in. This allows us to |
| 734 | * finish creating a signature (ensuring the process doesn't fail), and then |
| 735 | * erase the private key **before** writing any data into the sig parameter |
| 736 | * buffer. If data were directly written into the sig buffer, it might leak |
| 737 | * a partial signature on failure, which effectively compromises the private |
| 738 | * key. |
| 739 | */ |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 740 | unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX][MBEDTLS_LMOTS_N_HASH_LEN_MAX]; |
Raef Coles | d48f7e9 | 2022-10-10 13:10:07 +0100 | [diff] [blame] | 741 | unsigned char tmp_c_random[MBEDTLS_LMOTS_N_HASH_LEN_MAX]; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 742 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 743 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 744 | if (msg == NULL && msg_size != 0) { |
| 745 | return MBEDTLS_ERR_LMS_BAD_INPUT_DATA; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 746 | } |
| 747 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 748 | if (sig_size < MBEDTLS_LMOTS_SIG_LEN(ctx->params.type)) { |
| 749 | return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | /* Check that a private key is loaded */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 753 | if (!ctx->have_private_key) { |
| 754 | return MBEDTLS_ERR_LMS_BAD_INPUT_DATA; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 755 | } |
| 756 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 757 | ret = f_rng(p_rng, tmp_c_random, |
| 758 | MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type)); |
| 759 | if (ret) { |
| 760 | return ret; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 761 | } |
| 762 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 763 | ret = create_digit_array_with_checksum(&ctx->params, |
| 764 | msg, msg_size, |
| 765 | tmp_c_random, |
| 766 | tmp_digit_array); |
| 767 | if (ret) { |
Raef Coles | 142e577 | 2022-10-12 10:47:27 +0100 | [diff] [blame] | 768 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 769 | } |
| 770 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 771 | ret = hash_digit_array(&ctx->params, (unsigned char *) ctx->private_key, |
| 772 | NULL, tmp_digit_array, (unsigned char *) tmp_sig); |
| 773 | if (ret) { |
Raef Coles | 142e577 | 2022-10-12 10:47:27 +0100 | [diff] [blame] | 774 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 775 | } |
| 776 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 777 | mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.type, |
| 778 | MBEDTLS_LMOTS_TYPE_LEN, |
| 779 | sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 780 | |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 781 | /* Test hook to check if sig is being written to before we invalidate the |
| 782 | * private key. |
| 783 | */ |
| 784 | #if defined(MBEDTLS_TEST_HOOKS) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 785 | if (mbedtls_lmots_sign_private_key_invalidated_hook != NULL) { |
| 786 | ret = (*mbedtls_lmots_sign_private_key_invalidated_hook)(sig); |
| 787 | if (ret != 0) { |
| 788 | return ret; |
| 789 | } |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 790 | } |
| 791 | #endif /* defined(MBEDTLS_TEST_HOOKS) */ |
| 792 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 793 | /* We've got a valid signature now, so it's time to make sure the private |
| 794 | * key can't be reused. |
| 795 | */ |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 796 | ctx->have_private_key = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 797 | mbedtls_platform_zeroize(ctx->private_key, |
| 798 | sizeof(ctx->private_key)); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 799 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 800 | memcpy(sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, tmp_c_random, |
| 801 | MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(ctx->params.type)); |
Raef Coles | 891c613 | 2022-09-01 11:05:48 +0100 | [diff] [blame] | 802 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 803 | memcpy(sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(ctx->params.type), tmp_sig, |
| 804 | MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type) |
| 805 | * MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type)); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 806 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 807 | if (sig_len != NULL) { |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 808 | *sig_len = MBEDTLS_LMOTS_SIG_LEN(ctx->params.type); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 809 | } |
| 810 | |
Raef Coles | 142e577 | 2022-10-12 10:47:27 +0100 | [diff] [blame] | 811 | ret = 0; |
| 812 | |
| 813 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 814 | mbedtls_platform_zeroize(tmp_digit_array, sizeof(tmp_digit_array)); |
| 815 | mbedtls_platform_zeroize(tmp_sig, sizeof(tmp_sig)); |
Raef Coles | 142e577 | 2022-10-12 10:47:27 +0100 | [diff] [blame] | 816 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 817 | return ret; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 818 | } |
| 819 | |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame] | 820 | #endif /* defined(MBEDTLS_LMS_PRIVATE) */ |
| 821 | #endif /* defined(MBEDTLS_LMS_C) */ |