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