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