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