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 | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 155 | psa_hash_operation_t op; |
| 156 | psa_status_t status; |
| 157 | size_t output_hash_len; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 158 | unsigned short checksum; |
| 159 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 160 | |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 161 | op = psa_hash_operation_init( ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 162 | status = psa_hash_setup( &op, PSA_ALG_SHA_256 ); |
| 163 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 164 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 165 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 166 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 167 | status = psa_hash_update( &op, params->I_key_identifier, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 168 | MBEDTLS_LMOTS_I_KEY_ID_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 169 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 170 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 171 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 172 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 173 | status = psa_hash_update( &op, params->q_leaf_identifier, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 174 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 175 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 176 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 177 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 178 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 179 | status = psa_hash_update( &op, D_MESSAGE_CONSTANT_BYTES, D_CONST_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 180 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 181 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 182 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 183 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 184 | status = psa_hash_update( &op, C_random_value, |
| 185 | MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(params->type) ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 186 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 187 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 188 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 189 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 190 | status = psa_hash_update( &op, msg, msg_len ); |
| 191 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 192 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 193 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 194 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 195 | status = psa_hash_finish( &op, out, |
Raef Coles | fa24f9d | 2022-09-02 17:46:52 +0100 | [diff] [blame] | 196 | MBEDTLS_LMOTS_N_HASH_LEN(params->type), |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 197 | &output_hash_len ); |
| 198 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 199 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 200 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 201 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 202 | checksum = lmots_checksum_calculate( params, out ); |
Raef Coles | ad05425 | 2022-09-27 10:59:16 +0100 | [diff] [blame] | 203 | mbedtls_lms_unsigned_int_to_network_bytes( checksum, CHECKSUM_LEN, |
| 204 | out + MBEDTLS_LMOTS_N_HASH_LEN(params->type) ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 205 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 206 | exit: |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 207 | psa_hash_abort( &op ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 208 | |
| 209 | return( ret ); |
| 210 | } |
| 211 | |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 212 | /* Hash each element of the string of digits (+ checksum), producing a hash |
| 213 | * output for each element. This is used in several places (by varying the |
| 214 | * hash_idx_min/max_values) in order to calculate a public key from a private |
| 215 | * key (RFC8554 Algorithm 1 step 4), in order to sign a message (RFC8554 |
| 216 | * Algorithm 3 step 5), and to calculate a public key candidate from a |
| 217 | * signature and message (RFC8554 Algorithm 4b step 3). |
| 218 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 219 | * params The LMOTS parameter set, I and q values which |
| 220 | * describe the key being used. |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 221 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 222 | * x_digit_array The array of digits (of size P, 34 in the case of |
| 223 | * MBEDTLS_LMOTS_SHA256_N32_W8). |
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 | * hash_idx_min_values An array of the starting values of the j iterator |
| 226 | * for each of the members of the digit array. If |
| 227 | * this value in NULL, then all iterators will start |
| 228 | * at 0. |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 229 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 230 | * hash_idx_max_values An array of the upper bound values of the j |
| 231 | * iterator for each of the members of the digit |
| 232 | * array. If this value in NULL, then iterator is |
| 233 | * bounded to be less than 2^w - 1 (255 in the case |
| 234 | * of MBEDTLS_LMOTS_SHA256_N32_W8) |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 235 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 236 | * output An array containing a hash output for each member |
| 237 | * of the digit string P. In the case of |
| 238 | * MBEDTLS_LMOTS_SHA256_N32_W8, this is of size 32 * |
| 239 | * 34. |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 240 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 241 | static int hash_digit_array( const mbedtls_lmots_parameters_t *params, |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 242 | const unsigned char *x_digit_array, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 243 | const unsigned char *hash_idx_min_values, |
| 244 | const unsigned char *hash_idx_max_values, |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 245 | unsigned char *output ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 246 | { |
Raef Coles | 8738a49 | 2022-09-02 17:13:01 +0100 | [diff] [blame] | 247 | unsigned int i_digit_idx; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 248 | unsigned char i_digit_idx_bytes[I_DIGIT_IDX_LEN]; |
Raef Coles | 8738a49 | 2022-09-02 17:13:01 +0100 | [diff] [blame] | 249 | unsigned int j_hash_idx; |
| 250 | unsigned char j_hash_idx_bytes[J_HASH_IDX_LEN]; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 251 | unsigned int j_hash_idx_min; |
| 252 | unsigned int j_hash_idx_max; |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 253 | psa_hash_operation_t op; |
| 254 | psa_status_t status; |
| 255 | size_t output_hash_len; |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 256 | unsigned char tmp_hash[MBEDTLS_LMOTS_N_HASH_LEN_MAX]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 257 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 258 | |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 259 | op = psa_hash_operation_init( ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 260 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +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 | { |
| 265 | |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 266 | memcpy( tmp_hash, |
| 267 | &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] | 268 | MBEDTLS_LMOTS_N_HASH_LEN(params->type) ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 269 | |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 270 | j_hash_idx_min = hash_idx_min_values != NULL ? |
| 271 | hash_idx_min_values[i_digit_idx] : 0; |
| 272 | j_hash_idx_max = hash_idx_max_values != NULL ? |
| 273 | hash_idx_max_values[i_digit_idx] : DIGIT_MAX_VALUE; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 274 | |
Raef Coles | 8738a49 | 2022-09-02 17:13:01 +0100 | [diff] [blame] | 275 | for ( j_hash_idx = j_hash_idx_min; |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 276 | j_hash_idx < j_hash_idx_max; |
| 277 | j_hash_idx++ ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 278 | { |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 279 | status = psa_hash_setup( &op, PSA_ALG_SHA_256 ); |
| 280 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 281 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 282 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 283 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 284 | status = psa_hash_update( &op, |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 285 | params->I_key_identifier, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 286 | MBEDTLS_LMOTS_I_KEY_ID_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 287 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 288 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 289 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 290 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 291 | status = psa_hash_update( &op, |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 292 | params->q_leaf_identifier, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 293 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 294 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 295 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 296 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 297 | |
Raef Coles | ad05425 | 2022-09-27 10:59:16 +0100 | [diff] [blame] | 298 | mbedtls_lms_unsigned_int_to_network_bytes( i_digit_idx, |
| 299 | I_DIGIT_IDX_LEN, |
| 300 | i_digit_idx_bytes ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 301 | status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 302 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 303 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 304 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 305 | |
Raef Coles | ad05425 | 2022-09-27 10:59:16 +0100 | [diff] [blame] | 306 | mbedtls_lms_unsigned_int_to_network_bytes( j_hash_idx, |
| 307 | J_HASH_IDX_LEN, |
| 308 | j_hash_idx_bytes ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 309 | status = psa_hash_update( &op, j_hash_idx_bytes, J_HASH_IDX_LEN ); |
| 310 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 311 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 312 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 313 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 314 | status = psa_hash_update( &op, tmp_hash, |
| 315 | MBEDTLS_LMOTS_N_HASH_LEN(params->type) ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 316 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 317 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 318 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 319 | |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 320 | status = psa_hash_finish( &op, tmp_hash, sizeof( tmp_hash ), |
| 321 | &output_hash_len ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 322 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 323 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 324 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 325 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 326 | psa_hash_abort( &op ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 327 | } |
| 328 | |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 329 | memcpy( &output[i_digit_idx * MBEDTLS_LMOTS_N_HASH_LEN(params->type)], |
| 330 | tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN(params->type) ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 331 | } |
| 332 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 333 | exit: |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 334 | if( ret != 0 ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 335 | { |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 336 | psa_hash_abort( &op ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 337 | return( ret ); |
| 338 | } |
| 339 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 340 | mbedtls_platform_zeroize( tmp_hash, sizeof( tmp_hash ) ); |
| 341 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 342 | return ret; |
| 343 | } |
| 344 | |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 345 | /* Combine the hashes of the digit array into a public key. This is used in |
| 346 | * in order to calculate a public key from a private key (RFC8554 Algorithm 1 |
| 347 | * step 4), and to calculate a public key candidate from a signature and message |
| 348 | * (RFC8554 Algorithm 4b step 3). |
| 349 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 350 | * params The LMOTS parameter set, I and q values which describe |
| 351 | * the key being used. |
| 352 | * y_hashed_digits The array of hashes, one hash for each digit of the |
| 353 | * symbol array (which is of size P, 34 in the case of |
| 354 | * MBEDTLS_LMOTS_SHA256_N32_W8) |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 355 | * |
Raef Coles | 98d6e22 | 2022-09-23 09:04:04 +0100 | [diff] [blame] | 356 | * pub_key The output public key (or candidate public key in |
| 357 | * case this is being run as part of signature |
| 358 | * verification), in the form of a hash output. |
Raef Coles | 0a967cc | 2022-09-02 17:46:15 +0100 | [diff] [blame] | 359 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 360 | 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] | 361 | const unsigned char *y_hashed_digits, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 362 | unsigned char *pub_key ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 363 | { |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 364 | psa_hash_operation_t op; |
| 365 | psa_status_t status; |
| 366 | size_t output_hash_len; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 367 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 368 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 369 | op = psa_hash_operation_init( ); |
| 370 | status = psa_hash_setup( &op, PSA_ALG_SHA_256 ); |
| 371 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 372 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 373 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 374 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 375 | status = psa_hash_update( &op, |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 376 | params->I_key_identifier, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 377 | MBEDTLS_LMOTS_I_KEY_ID_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 378 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 379 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 380 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 381 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 382 | status = psa_hash_update( &op, params->q_leaf_identifier, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 383 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 384 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 385 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 386 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 387 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 388 | status = psa_hash_update( &op, D_PUBLIC_CONSTANT_BYTES, D_CONST_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 389 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 390 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 391 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 392 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 393 | status = psa_hash_update( &op, y_hashed_digits, |
| 394 | MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(params->type) * |
| 395 | MBEDTLS_LMOTS_N_HASH_LEN(params->type) ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 396 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 397 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 398 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 399 | |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 400 | status = psa_hash_finish( &op, pub_key, |
| 401 | MBEDTLS_LMOTS_N_HASH_LEN(params->type), |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 402 | &output_hash_len ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 403 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 404 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 405 | exit: |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 406 | psa_hash_abort( &op ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 407 | return( ret ); |
| 408 | } |
| 409 | |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 410 | int mbedtls_lms_error_from_psa( psa_status_t status ) |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 411 | { |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 412 | switch( status ) |
| 413 | { |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 414 | case PSA_SUCCESS: |
| 415 | return( 0 ); |
| 416 | case PSA_ERROR_HARDWARE_FAILURE: |
| 417 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
| 418 | case PSA_ERROR_NOT_SUPPORTED: |
| 419 | return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ); |
| 420 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 421 | return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL ); |
| 422 | case PSA_ERROR_INVALID_ARGUMENT: |
| 423 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 424 | default: |
| 425 | return( MBEDTLS_ERR_ERROR_GENERIC_ERROR ); |
| 426 | } |
| 427 | } |
| 428 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 429 | void mbedtls_lmots_init_public( mbedtls_lmots_public_t *ctx ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 430 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 431 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_public_t ) ) ; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 432 | } |
| 433 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 434 | void mbedtls_lmots_free_public( mbedtls_lmots_public_t *ctx ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 435 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 436 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_public_t ) ) ; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 437 | } |
| 438 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 439 | int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx, |
| 440 | const unsigned char *key, size_t key_len ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 441 | { |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 442 | ctx->params.type = |
Raef Coles | ad05425 | 2022-09-27 10:59:16 +0100 | [diff] [blame] | 443 | mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN, |
| 444 | key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 445 | |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 446 | if( key_len < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type) ) |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 447 | { |
| 448 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 449 | } |
| 450 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 451 | memcpy( ctx->params.I_key_identifier, |
Raef Coles | 57d5328 | 2022-09-27 11:30:51 +0100 | [diff] [blame] | 452 | key + PUBLIC_KEY_I_KEY_ID_OFFSET, |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 453 | MBEDTLS_LMOTS_I_KEY_ID_LEN ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 454 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 455 | memcpy( ctx->params.q_leaf_identifier, |
Raef Coles | 57d5328 | 2022-09-27 11:30:51 +0100 | [diff] [blame] | 456 | key + PUBLIC_KEY_Q_LEAF_ID_OFFSET, |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 457 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 458 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 459 | memcpy( ctx->public_key, |
Raef Coles | 57d5328 | 2022-09-27 11:30:51 +0100 | [diff] [blame] | 460 | key + PUBLIC_KEY_KEY_HASH_OFFSET, |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 461 | MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 462 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 463 | ctx->have_public_key = 1; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 464 | |
| 465 | return( 0 ); |
| 466 | } |
| 467 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 468 | int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters_t *params, |
| 469 | const unsigned char *msg, |
| 470 | size_t msg_size, |
| 471 | const unsigned char *sig, |
| 472 | size_t sig_size, |
| 473 | unsigned char *out, |
| 474 | size_t out_size, |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 475 | size_t *out_len ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 476 | { |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 477 | unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX]; |
| 478 | 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] | 479 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 480 | |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 481 | if( msg == NULL && msg_size != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 482 | { |
| 483 | return ( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 484 | } |
| 485 | |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 486 | if( sig_size != MBEDTLS_LMOTS_SIG_LEN(params->type) || |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 487 | out_size < MBEDTLS_LMOTS_N_HASH_LEN(params->type) ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 488 | { |
Raef Coles | 7dce69a | 2022-08-24 14:07:06 +0100 | [diff] [blame] | 489 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 490 | } |
| 491 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 492 | ret = create_digit_array_with_checksum( params, msg, msg_size, |
| 493 | sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, |
| 494 | tmp_digit_array ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 495 | if( ret ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 496 | { |
| 497 | return ( ret ); |
| 498 | } |
| 499 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 500 | ret = hash_digit_array( params, |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 501 | sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(params->type), |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 502 | tmp_digit_array, NULL, ( unsigned char * )y_hashed_digits ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 503 | if( ret ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 504 | { |
| 505 | return ( ret ); |
| 506 | } |
| 507 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 508 | ret = public_key_from_hashed_digit_array( params, |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 509 | ( unsigned char * )y_hashed_digits, |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 510 | out ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 511 | if( ret ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 512 | { |
| 513 | return ( ret ); |
| 514 | } |
| 515 | |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 516 | if( out_len != NULL ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 517 | { |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 518 | *out_len = MBEDTLS_LMOTS_N_HASH_LEN(params->type); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 519 | } |
| 520 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 521 | return( 0 ); |
| 522 | } |
| 523 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 524 | int mbedtls_lmots_verify( mbedtls_lmots_public_t *ctx, const unsigned char *msg, |
| 525 | size_t msg_size, const unsigned char *sig, |
| 526 | size_t sig_size ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 527 | { |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 528 | unsigned char Kc_public_key_candidate[MBEDTLS_LMOTS_N_HASH_LEN_MAX]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 529 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 530 | |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 531 | if( msg == NULL && msg_size != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 532 | { |
| 533 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 534 | } |
| 535 | |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 536 | if( !ctx->have_public_key ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 537 | { |
| 538 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 539 | } |
| 540 | |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 541 | if( ctx->params.type != MBEDTLS_LMOTS_SHA256_N32_W8 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 542 | { |
| 543 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 544 | } |
| 545 | |
Raef Coles | ad05425 | 2022-09-27 10:59:16 +0100 | [diff] [blame] | 546 | if( mbedtls_lms_network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN, |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 547 | sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ) != MBEDTLS_LMOTS_SHA256_N32_W8 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 548 | { |
| 549 | return( MBEDTLS_ERR_LMS_VERIFY_FAILED ); |
| 550 | } |
| 551 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 552 | ret = mbedtls_lmots_calculate_public_key_candidate( &ctx->params, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 553 | msg, msg_size, sig, sig_size, |
| 554 | Kc_public_key_candidate, |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 555 | MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type), |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 556 | NULL ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 557 | if( ret ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 558 | { |
| 559 | return( ret ); |
| 560 | } |
| 561 | |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 562 | if( memcmp( &Kc_public_key_candidate, ctx->public_key, |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 563 | sizeof( ctx->public_key ) ) ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 564 | { |
| 565 | return( MBEDTLS_ERR_LMS_VERIFY_FAILED ); |
| 566 | } |
| 567 | |
| 568 | return( 0 ); |
| 569 | } |
| 570 | |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame^] | 571 | #if defined(MBEDTLS_LMS_PRIVATE) |
Raef Coles | ab4f874 | 2022-09-01 12:24:31 +0100 | [diff] [blame] | 572 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 573 | void mbedtls_lmots_init_private( mbedtls_lmots_private_t *ctx ) |
| 574 | { |
| 575 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ; |
| 576 | } |
| 577 | |
| 578 | void mbedtls_lmots_free_private( mbedtls_lmots_private_t *ctx ) |
| 579 | { |
| 580 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ; |
| 581 | } |
| 582 | |
| 583 | int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx, |
| 584 | mbedtls_lmots_algorithm_type_t type, |
| 585 | const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN], |
| 586 | uint32_t q_leaf_identifier, |
| 587 | const unsigned char *seed, |
| 588 | size_t seed_size ) |
| 589 | { |
| 590 | psa_hash_operation_t op; |
| 591 | psa_status_t status; |
| 592 | size_t output_hash_len; |
| 593 | unsigned int i_digit_idx; |
| 594 | unsigned char i_digit_idx_bytes[2]; |
| 595 | unsigned char const_bytes[1]; |
| 596 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 597 | |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 598 | if( ctx->have_private_key ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 599 | { |
| 600 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 601 | } |
| 602 | |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 603 | if( type != MBEDTLS_LMOTS_SHA256_N32_W8 ) |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 604 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 605 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 606 | } |
| 607 | |
Raef Coles | ebd35b5 | 2022-09-01 11:52:17 +0100 | [diff] [blame] | 608 | op = psa_hash_operation_init( ); |
| 609 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 610 | ctx->params.type = type; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 611 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 612 | memcpy( ctx->params.I_key_identifier, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 613 | I_key_identifier, |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 614 | sizeof( ctx->params.I_key_identifier ) ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 615 | |
Raef Coles | ad05425 | 2022-09-27 10:59:16 +0100 | [diff] [blame] | 616 | mbedtls_lms_unsigned_int_to_network_bytes( q_leaf_identifier, |
| 617 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN, |
| 618 | ctx->params.q_leaf_identifier ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 619 | |
Raef Coles | ad05425 | 2022-09-27 10:59:16 +0100 | [diff] [blame] | 620 | mbedtls_lms_unsigned_int_to_network_bytes( 0xFF, sizeof( const_bytes ), |
| 621 | const_bytes ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 622 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 623 | for ( i_digit_idx = 0; |
| 624 | i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type); |
| 625 | i_digit_idx++ ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 626 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 627 | status = psa_hash_setup( &op, PSA_ALG_SHA_256 ); |
| 628 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 629 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 630 | goto exit; |
| 631 | |
| 632 | ret = psa_hash_update( &op, |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 633 | ctx->params.I_key_identifier, |
| 634 | sizeof( ctx->params.I_key_identifier ) ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 635 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 636 | if( ret ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 637 | goto exit; |
| 638 | |
| 639 | status = psa_hash_update( &op, |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 640 | ctx->params.q_leaf_identifier, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 641 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN ); |
| 642 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 643 | if( ret ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 644 | goto exit; |
| 645 | |
Raef Coles | ad05425 | 2022-09-27 10:59:16 +0100 | [diff] [blame] | 646 | mbedtls_lms_unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN, |
| 647 | i_digit_idx_bytes ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 648 | status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN ); |
| 649 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 650 | if( ret ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 651 | goto exit; |
| 652 | |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 653 | status = psa_hash_update( &op, const_bytes, sizeof( const_bytes ) ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 654 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 655 | if( ret ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 656 | goto exit; |
| 657 | |
| 658 | status = psa_hash_update( &op, seed, seed_size ); |
| 659 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 660 | if( ret ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 661 | goto exit; |
| 662 | |
| 663 | status = psa_hash_finish( &op, |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 664 | ctx->private_key[i_digit_idx], |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 665 | MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type), |
| 666 | &output_hash_len ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 667 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 668 | if( ret ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 669 | goto exit; |
| 670 | |
| 671 | psa_hash_abort( &op ); |
| 672 | } |
| 673 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 674 | ctx->have_private_key = 1; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 675 | |
| 676 | exit: |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 677 | if( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 678 | { |
| 679 | psa_hash_abort( &op ); |
| 680 | return( ret ); |
| 681 | } |
| 682 | |
| 683 | return ret; |
| 684 | } |
| 685 | |
| 686 | int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx, |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 687 | mbedtls_lmots_private_t *priv_ctx ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 688 | { |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 689 | 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] | 690 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 691 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 692 | /* Check that a private key is loaded */ |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 693 | if( !priv_ctx->have_private_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 | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 698 | ret = hash_digit_array( &priv_ctx->params, |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 699 | ( unsigned char * )priv_ctx->private_key, NULL, |
| 700 | NULL, ( unsigned char * )y_hashed_digits ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 701 | if( ret ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 702 | { |
| 703 | return( ret ); |
| 704 | } |
| 705 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 706 | ret = public_key_from_hashed_digit_array( &priv_ctx->params, |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 707 | ( unsigned char * )y_hashed_digits, |
Raef Coles | 0c88d4e | 2022-09-01 10:48:32 +0100 | [diff] [blame] | 708 | ctx->public_key ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 709 | if( ret ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 710 | { |
| 711 | return( ret ); |
| 712 | } |
| 713 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 714 | memcpy( &ctx->params, &priv_ctx->params, |
| 715 | sizeof( ctx->params ) ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 716 | |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 717 | ctx->have_public_key = 1; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 718 | |
| 719 | return( ret ); |
| 720 | } |
| 721 | |
| 722 | |
| 723 | int mbedtls_lmots_export_public_key( mbedtls_lmots_public_t *ctx, |
| 724 | unsigned char *key, size_t key_size, |
| 725 | size_t *key_len ) |
| 726 | { |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 727 | if( key_size < MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type) ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 728 | { |
| 729 | return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL ); |
| 730 | } |
| 731 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 732 | if( ! ctx->have_public_key ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 733 | { |
| 734 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 735 | } |
| 736 | |
Raef Coles | ad05425 | 2022-09-27 10:59:16 +0100 | [diff] [blame] | 737 | mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.type, |
| 738 | MBEDTLS_LMOTS_TYPE_LEN, |
| 739 | key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 740 | |
Raef Coles | 57d5328 | 2022-09-27 11:30:51 +0100 | [diff] [blame] | 741 | memcpy( key + PUBLIC_KEY_I_KEY_ID_OFFSET, |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 742 | ctx->params.I_key_identifier, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 743 | MBEDTLS_LMOTS_I_KEY_ID_LEN ); |
| 744 | |
Raef Coles | 57d5328 | 2022-09-27 11:30:51 +0100 | [diff] [blame] | 745 | memcpy( key + PUBLIC_KEY_Q_LEAF_ID_OFFSET, |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 746 | ctx->params.q_leaf_identifier, |
| 747 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 748 | |
Raef Coles | 57d5328 | 2022-09-27 11:30:51 +0100 | [diff] [blame] | 749 | memcpy( key + PUBLIC_KEY_KEY_HASH_OFFSET, ctx->public_key, |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 750 | MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 751 | |
| 752 | if( key_len != NULL ) |
| 753 | { |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 754 | *key_len = MBEDTLS_LMOTS_PUBLIC_KEY_LEN(ctx->params.type); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | return( 0 ); |
| 758 | } |
| 759 | |
| 760 | int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx, |
| 761 | int (*f_rng)(void *, unsigned char *, size_t), |
| 762 | void *p_rng, const unsigned char *msg, size_t msg_size, |
| 763 | unsigned char *sig, size_t sig_size, size_t* sig_len ) |
| 764 | { |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 765 | unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT_MAX]; |
Raef Coles | 891c613 | 2022-09-01 11:05:48 +0100 | [diff] [blame] | 766 | /* Create a temporary buffer to prepare the signature in. This allows us to |
| 767 | * finish creating a signature (ensuring the process doesn't fail), and then |
| 768 | * erase the private key **before** writing any data into the sig parameter |
| 769 | * buffer. If data were directly written into the sig buffer, it might leak |
| 770 | * a partial signature on failure, which effectively compromises the private |
| 771 | * key. |
| 772 | */ |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 773 | 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] | 774 | unsigned char tmp_c_random[C_RANDOM_VALUE_LEN_MAX]; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 775 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 776 | |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 777 | if( msg == NULL && msg_size != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 778 | { |
| 779 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 780 | } |
| 781 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 782 | if( sig_size < MBEDTLS_LMOTS_SIG_LEN(ctx->params.type) ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 783 | { |
| 784 | return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL ); |
| 785 | } |
| 786 | |
| 787 | /* Check that a private key is loaded */ |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 788 | if( !ctx->have_private_key ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 789 | { |
Raef Coles | 7dce69a | 2022-08-24 14:07:06 +0100 | [diff] [blame] | 790 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 791 | } |
| 792 | |
Raef Coles | 366d67d | 2022-09-01 17:23:12 +0100 | [diff] [blame] | 793 | ret = f_rng( p_rng, tmp_c_random, |
| 794 | MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 795 | if( ret ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 796 | { |
| 797 | return( ret ); |
| 798 | } |
| 799 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 800 | ret = create_digit_array_with_checksum( &ctx->params, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 801 | msg, msg_size, |
Raef Coles | 891c613 | 2022-09-01 11:05:48 +0100 | [diff] [blame] | 802 | tmp_c_random, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 803 | tmp_digit_array ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 804 | if( ret ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 805 | { |
| 806 | return( ret ); |
| 807 | } |
| 808 | |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 809 | ret = hash_digit_array( &ctx->params, ( unsigned char * )ctx->private_key, |
| 810 | NULL, tmp_digit_array, ( unsigned char * )tmp_sig ); |
Raef Coles | e0a1761 | 2022-09-02 16:04:47 +0100 | [diff] [blame] | 811 | if( ret ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 812 | { |
| 813 | return( ret ); |
| 814 | } |
| 815 | |
Raef Coles | ad05425 | 2022-09-27 10:59:16 +0100 | [diff] [blame] | 816 | mbedtls_lms_unsigned_int_to_network_bytes( ctx->params.type, |
| 817 | MBEDTLS_LMOTS_TYPE_LEN, |
| 818 | sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 819 | |
Raef Coles | 9c9027b | 2022-09-02 18:26:31 +0100 | [diff] [blame] | 820 | /* Test hook to check if sig is being written to before we invalidate the |
| 821 | * private key. |
| 822 | */ |
| 823 | #if defined(MBEDTLS_TEST_HOOKS) |
| 824 | if( mbedtls_lmots_sign_private_key_invalidated_hook != NULL ) |
| 825 | { |
| 826 | ret = ( *mbedtls_lmots_sign_private_key_invalidated_hook )( sig ); |
| 827 | if( ret != 0 ) |
| 828 | return( ret ); |
| 829 | } |
| 830 | #endif /* defined(MBEDTLS_TEST_HOOKS) */ |
| 831 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 832 | /* We've got a valid signature now, so it's time to make sure the private |
| 833 | * key can't be reused. |
| 834 | */ |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 835 | ctx->have_private_key = 0; |
Raef Coles | 9b88ee5 | 2022-09-02 12:04:21 +0100 | [diff] [blame] | 836 | mbedtls_platform_zeroize( ctx->private_key, |
| 837 | sizeof( ctx->private_key ) ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 838 | |
Raef Coles | 891c613 | 2022-09-01 11:05:48 +0100 | [diff] [blame] | 839 | memcpy( sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, tmp_c_random, |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 840 | MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN(ctx->params.type) ); |
Raef Coles | 891c613 | 2022-09-01 11:05:48 +0100 | [diff] [blame] | 841 | |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 842 | memcpy( sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET(ctx->params.type), tmp_sig, |
| 843 | MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT(ctx->params.type) |
| 844 | * MBEDTLS_LMOTS_N_HASH_LEN(ctx->params.type) ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 845 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 846 | if( sig_len != NULL ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 847 | { |
Raef Coles | e9479a0 | 2022-09-01 16:06:35 +0100 | [diff] [blame] | 848 | *sig_len = MBEDTLS_LMOTS_SIG_LEN(ctx->params.type); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | return( 0 ); |
| 852 | } |
| 853 | |
Raef Coles | 5127e85 | 2022-10-07 10:35:56 +0100 | [diff] [blame^] | 854 | #endif /* defined(MBEDTLS_LMS_PRIVATE) */ |
| 855 | #endif /* defined(MBEDTLS_LMS_C) */ |