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 | 7dce69a | 2022-08-24 14:07:06 +0100 | [diff] [blame] | 35 | #ifdef 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 | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 47 | #define MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET (MBEDTLS_LMOTS_SIG_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) |
| 48 | #define MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET (MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET + MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN) |
| 49 | |
| 50 | #define MBEDTLS_LMOTS_PUBLIC_KEY_TYPE_OFFSET (0) |
| 51 | #define MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET (MBEDTLS_LMOTS_PUBLIC_KEY_TYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) |
| 52 | #define MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET (MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET + MBEDTLS_LMOTS_I_KEY_ID_LEN) |
| 53 | #define MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET (MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET + MBEDTLS_LMOTS_Q_LEAF_ID_LEN) |
| 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 | |
| 63 | #define DIGIT_MAX_VALUE ((1u << W_WINTERNITZ_PARAMETER) - 1u) |
| 64 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 65 | #define D_CONST_LEN (2) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 66 | static const unsigned char D_PUBLIC_CONSTANT_BYTES[D_CONST_LEN] = {0x80, 0x80}; |
| 67 | 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] | 68 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 69 | void unsigned_int_to_network_bytes(unsigned int val, size_t len, unsigned char *bytes) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 70 | { |
| 71 | size_t idx; |
| 72 | |
| 73 | for (idx = 0; idx < len; idx++) { |
| 74 | bytes[idx] = (val >> ((len - 1 - idx) * 8)) & 0xFF; |
| 75 | } |
| 76 | } |
| 77 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 78 | unsigned int network_bytes_to_unsigned_int(size_t len, const unsigned char *bytes) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 79 | { |
| 80 | size_t idx; |
| 81 | unsigned int val = 0; |
| 82 | |
| 83 | for (idx = 0; idx < len; idx++) { |
| 84 | val |= ((unsigned int)bytes[idx]) << (8 * (len - 1 - idx)); |
| 85 | } |
| 86 | |
| 87 | return val; |
| 88 | } |
| 89 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 90 | static unsigned short lmots_checksum_calculate( const unsigned char* digest ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 91 | { |
| 92 | size_t idx; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 93 | unsigned sum = 0; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 94 | |
| 95 | for ( idx = 0; idx < MBEDTLS_LMOTS_N_HASH_LEN; idx++ ) |
| 96 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 97 | sum += DIGIT_MAX_VALUE - digest[idx]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | return sum; |
| 101 | } |
| 102 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 103 | static int create_digit_array_with_checksum( const mbedtls_lmots_parameters_t *params, |
| 104 | const unsigned char *msg, |
| 105 | size_t msg_len, |
| 106 | const unsigned char C_random_value[MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN], |
| 107 | unsigned char out[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT] ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 108 | { |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 109 | psa_hash_operation_t op; |
| 110 | psa_status_t status; |
| 111 | size_t output_hash_len; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 112 | unsigned short checksum; |
| 113 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 114 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 115 | op = psa_hash_operation_init(); |
| 116 | status = psa_hash_setup( &op, PSA_ALG_SHA_256 ); |
| 117 | ret = mbedtls_lms_error_from_psa( status ); |
| 118 | if ( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 119 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 120 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 121 | status = psa_hash_update( &op, params->MBEDTLS_PRIVATE(I_key_identifier), |
| 122 | MBEDTLS_LMOTS_I_KEY_ID_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 123 | ret = mbedtls_lms_error_from_psa( status ); |
| 124 | if ( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 125 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 126 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 127 | status = psa_hash_update( &op, params->MBEDTLS_PRIVATE(q_leaf_identifier), |
| 128 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 129 | ret = mbedtls_lms_error_from_psa( status ); |
| 130 | if ( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 131 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 132 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 133 | status = psa_hash_update( &op, D_MESSAGE_CONSTANT_BYTES, D_CONST_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 134 | ret = mbedtls_lms_error_from_psa( status ); |
| 135 | if ( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 136 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 137 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 138 | status = psa_hash_update( &op, C_random_value, MBEDTLS_LMOTS_C_RANDOM_VALUE_LEN ); |
| 139 | ret = mbedtls_lms_error_from_psa( status ); |
| 140 | if ( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 141 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 142 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 143 | status = psa_hash_update( &op, msg, msg_len ); |
| 144 | ret = mbedtls_lms_error_from_psa( status ); |
| 145 | if ( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 146 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 147 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 148 | status = psa_hash_finish( &op, out, MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT, |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 149 | &output_hash_len ); |
| 150 | ret = mbedtls_lms_error_from_psa( status ); |
| 151 | if ( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 152 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 153 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 154 | checksum = lmots_checksum_calculate( out ); |
| 155 | unsigned_int_to_network_bytes( checksum, CHECKSUM_LEN, out + MBEDTLS_LMOTS_N_HASH_LEN ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 156 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 157 | exit: |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 158 | psa_hash_abort( &op ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 159 | |
| 160 | return( ret ); |
| 161 | } |
| 162 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 163 | static int hash_digit_array( const mbedtls_lmots_parameters_t *params, |
| 164 | const unsigned char x_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN], |
| 165 | const unsigned char *hash_idx_min_values, |
| 166 | const unsigned char *hash_idx_max_values, |
| 167 | unsigned char output[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN] ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 168 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 169 | unsigned char i_digit_idx; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 170 | unsigned char j_hash_idx; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 171 | unsigned char i_digit_idx_bytes[I_DIGIT_IDX_LEN]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 172 | unsigned char j_hash_idx_bytes[1]; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 173 | /* These can't be unsigned chars, because they are sometimes set to |
| 174 | * #DIGIT_MAX_VALUE, which has a value of 256 |
| 175 | */ |
| 176 | unsigned int j_hash_idx_min; |
| 177 | unsigned int j_hash_idx_max; |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 178 | psa_hash_operation_t op; |
| 179 | psa_status_t status; |
| 180 | size_t output_hash_len; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 181 | unsigned char tmp_hash[MBEDTLS_LMOTS_N_HASH_LEN]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 182 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 183 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 184 | op = psa_hash_operation_init(); |
| 185 | |
| 186 | for ( i_digit_idx = 0; i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT; i_digit_idx++ ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 187 | { |
| 188 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 189 | memcpy( tmp_hash, &x_digit_array[i_digit_idx], MBEDTLS_LMOTS_N_HASH_LEN ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 190 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 191 | j_hash_idx_min = hash_idx_min_values != NULL ? hash_idx_min_values[i_digit_idx] : 0; |
| 192 | j_hash_idx_max = hash_idx_max_values != NULL ? hash_idx_max_values[i_digit_idx] : DIGIT_MAX_VALUE; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 193 | |
| 194 | for ( j_hash_idx = (unsigned char)j_hash_idx_min; j_hash_idx < j_hash_idx_max; j_hash_idx++ ) |
| 195 | { |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 196 | status = psa_hash_setup( &op, PSA_ALG_SHA_256 ); |
| 197 | ret = mbedtls_lms_error_from_psa( status ); |
| 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 | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 201 | status = psa_hash_update( &op, |
| 202 | params->MBEDTLS_PRIVATE(I_key_identifier), |
| 203 | MBEDTLS_LMOTS_I_KEY_ID_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 204 | ret = mbedtls_lms_error_from_psa( status ); |
| 205 | if ( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 206 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 207 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 208 | status = psa_hash_update( &op, |
| 209 | params->MBEDTLS_PRIVATE(q_leaf_identifier), |
| 210 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 211 | ret = mbedtls_lms_error_from_psa( status ); |
| 212 | if ( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 213 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 214 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 215 | unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN, i_digit_idx_bytes ); |
| 216 | 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] | 217 | ret = mbedtls_lms_error_from_psa( status ); |
| 218 | if ( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 219 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 220 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 221 | unsigned_int_to_network_bytes( j_hash_idx, J_HASH_IDX_LEN, j_hash_idx_bytes ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 222 | status = psa_hash_update( &op, j_hash_idx_bytes, J_HASH_IDX_LEN ); |
| 223 | ret = mbedtls_lms_error_from_psa( status ); |
| 224 | if ( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 225 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 226 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 227 | status = psa_hash_update( &op, tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN ); |
| 228 | ret = mbedtls_lms_error_from_psa( status ); |
| 229 | if ( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 230 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 231 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 232 | status = psa_hash_finish( &op, tmp_hash, sizeof( tmp_hash ), &output_hash_len ); |
| 233 | ret = mbedtls_lms_error_from_psa( status ); |
| 234 | if ( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 235 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 236 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 237 | psa_hash_abort( &op ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 238 | } |
| 239 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 240 | memcpy( &output[i_digit_idx], tmp_hash, MBEDTLS_LMOTS_N_HASH_LEN ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 241 | } |
| 242 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 243 | exit: |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 244 | if( ret ) |
| 245 | { |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 246 | psa_hash_abort( &op ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 247 | return( ret ); |
| 248 | } |
| 249 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 250 | mbedtls_platform_zeroize( tmp_hash, sizeof( tmp_hash ) ); |
| 251 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 252 | return ret; |
| 253 | } |
| 254 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 255 | static int public_key_from_hashed_digit_array( const mbedtls_lmots_parameters_t *params, |
| 256 | const unsigned char y_hashed_digits[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN], |
| 257 | unsigned char *pub_key ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 258 | { |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 259 | psa_hash_operation_t op; |
| 260 | psa_status_t status; |
| 261 | size_t output_hash_len; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 262 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 263 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 264 | op = psa_hash_operation_init( ); |
| 265 | status = psa_hash_setup( &op, PSA_ALG_SHA_256 ); |
| 266 | ret = mbedtls_lms_error_from_psa( status ); |
| 267 | if ( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 268 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 269 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 270 | status = psa_hash_update( &op, |
| 271 | params->MBEDTLS_PRIVATE(I_key_identifier), |
| 272 | MBEDTLS_LMOTS_I_KEY_ID_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 273 | ret = mbedtls_lms_error_from_psa( status ); |
| 274 | if ( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 275 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 276 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 277 | status = psa_hash_update( &op, params->MBEDTLS_PRIVATE(q_leaf_identifier), |
| 278 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 279 | ret = mbedtls_lms_error_from_psa( status ); |
| 280 | if ( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 281 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 282 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 283 | status = psa_hash_update( &op, D_PUBLIC_CONSTANT_BYTES, D_CONST_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 284 | ret = mbedtls_lms_error_from_psa( status ); |
| 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, ( unsigned char * )y_hashed_digits, |
| 289 | MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT * MBEDTLS_LMOTS_N_HASH_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 290 | ret = mbedtls_lms_error_from_psa( status ); |
| 291 | if ( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 292 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 293 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 294 | status = psa_hash_finish( &op, pub_key, 32, &output_hash_len ); |
| 295 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 296 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 297 | exit: |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 298 | psa_hash_abort( &op ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 299 | return( ret ); |
| 300 | } |
| 301 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 302 | int mbedtls_lms_error_from_psa(psa_status_t status) |
| 303 | { |
| 304 | switch( status ) { |
| 305 | case PSA_SUCCESS: |
| 306 | return( 0 ); |
| 307 | case PSA_ERROR_HARDWARE_FAILURE: |
| 308 | return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED ); |
| 309 | case PSA_ERROR_NOT_SUPPORTED: |
| 310 | return( MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED ); |
| 311 | case PSA_ERROR_BUFFER_TOO_SMALL: |
| 312 | return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL ); |
| 313 | case PSA_ERROR_INVALID_ARGUMENT: |
| 314 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 315 | default: |
| 316 | return( MBEDTLS_ERR_ERROR_GENERIC_ERROR ); |
| 317 | } |
| 318 | } |
| 319 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 320 | void mbedtls_lmots_init_public( mbedtls_lmots_public_t *ctx ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 321 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 322 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_public_t ) ) ; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 323 | } |
| 324 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 325 | void mbedtls_lmots_free_public( mbedtls_lmots_public_t *ctx ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 326 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 327 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_public_t ) ) ; |
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 | int mbedtls_lmots_import_public_key( mbedtls_lmots_public_t *ctx, |
| 331 | const unsigned char *key, size_t key_len ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 332 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 333 | if ( key_len < MBEDTLS_LMOTS_PUBLIC_KEY_LEN ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 334 | { |
Raef Coles | 7dce69a | 2022-08-24 14:07:06 +0100 | [diff] [blame] | 335 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 336 | } |
| 337 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 338 | ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(type) = |
| 339 | network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN, |
| 340 | key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ); |
| 341 | |
| 342 | memcpy( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier), |
| 343 | key + MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET, MBEDTLS_LMOTS_I_KEY_ID_LEN ); |
| 344 | |
| 345 | memcpy( ctx->MBEDTLS_PRIVATE(MBEDTLS_PRIVATE(params).q_leaf_identifier), |
| 346 | key + MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET, MBEDTLS_LMOTS_Q_LEAF_ID_LEN ); |
| 347 | |
| 348 | memcpy( ctx->MBEDTLS_PRIVATE(public_key), |
| 349 | key + MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET, |
| 350 | MBEDTLS_LMOTS_N_HASH_LEN ); |
| 351 | |
| 352 | ctx->MBEDTLS_PRIVATE(have_public_key) = 1; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 353 | |
| 354 | return( 0 ); |
| 355 | } |
| 356 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 357 | int mbedtls_lmots_calculate_public_key_candidate( const mbedtls_lmots_parameters_t *params, |
| 358 | const unsigned char *msg, |
| 359 | size_t msg_size, |
| 360 | const unsigned char *sig, |
| 361 | size_t sig_size, |
| 362 | unsigned char *out, |
| 363 | size_t out_size, |
| 364 | size_t *out_len) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 365 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 366 | unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT]; |
| 367 | unsigned char y_hashed_digits[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 368 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 369 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 370 | if ( msg == NULL && msg_size != 0 ) |
| 371 | { |
| 372 | return ( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 373 | } |
| 374 | |
| 375 | if ( sig_size != MBEDTLS_LMOTS_SIG_LEN || out_size < MBEDTLS_LMOTS_N_HASH_LEN ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 376 | { |
Raef Coles | 7dce69a | 2022-08-24 14:07:06 +0100 | [diff] [blame] | 377 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 378 | } |
| 379 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 380 | ret = create_digit_array_with_checksum( params, msg, msg_size, |
| 381 | sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, |
| 382 | tmp_digit_array ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 383 | if ( ret ) |
| 384 | { |
| 385 | return ( ret ); |
| 386 | } |
| 387 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 388 | ret = hash_digit_array( params, |
| 389 | ( const unsigned char( *)[MBEDTLS_LMOTS_N_HASH_LEN] )(sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET), |
| 390 | tmp_digit_array, NULL, y_hashed_digits ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 391 | if ( ret ) |
| 392 | { |
| 393 | return ( ret ); |
| 394 | } |
| 395 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 396 | ret = public_key_from_hashed_digit_array( params, |
| 397 | ( const unsigned char( *)[MBEDTLS_LMOTS_N_HASH_LEN] )y_hashed_digits, |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 398 | out ); |
| 399 | if ( ret ) |
| 400 | { |
| 401 | return ( ret ); |
| 402 | } |
| 403 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 404 | if ( out_len != NULL ) |
| 405 | { |
| 406 | *out_len = MBEDTLS_LMOTS_N_HASH_LEN; |
| 407 | } |
| 408 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 409 | return( 0 ); |
| 410 | } |
| 411 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 412 | int mbedtls_lmots_verify( mbedtls_lmots_public_t *ctx, const unsigned char *msg, |
| 413 | size_t msg_size, const unsigned char *sig, |
| 414 | size_t sig_size ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 415 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 416 | unsigned char Kc_public_key_candidate[MBEDTLS_LMOTS_N_HASH_LEN]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 417 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 418 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 419 | if ( msg == NULL && msg_size != 0 ) |
| 420 | { |
| 421 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 422 | } |
| 423 | |
| 424 | if ( !ctx->MBEDTLS_PRIVATE(have_public_key) ) |
| 425 | { |
| 426 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 427 | } |
| 428 | |
| 429 | if( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE( type ) |
| 430 | != MBEDTLS_LMOTS_SHA256_N32_W8 ) |
| 431 | { |
| 432 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 433 | } |
| 434 | |
| 435 | if ( network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN, |
| 436 | sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ) != MBEDTLS_LMOTS_SHA256_N32_W8 ) |
| 437 | { |
| 438 | return( MBEDTLS_ERR_LMS_VERIFY_FAILED ); |
| 439 | } |
| 440 | |
| 441 | ret = mbedtls_lmots_calculate_public_key_candidate( &ctx->MBEDTLS_PRIVATE(params), |
| 442 | msg, msg_size, sig, sig_size, |
| 443 | Kc_public_key_candidate, |
| 444 | MBEDTLS_LMOTS_N_HASH_LEN, |
| 445 | NULL); |
| 446 | if ( ret ) |
| 447 | { |
| 448 | return( ret ); |
| 449 | } |
| 450 | |
| 451 | if ( memcmp( &Kc_public_key_candidate, ctx->MBEDTLS_PRIVATE(public_key), |
| 452 | sizeof( ctx->MBEDTLS_PRIVATE(public_key) ) ) ) |
| 453 | { |
| 454 | return( MBEDTLS_ERR_LMS_VERIFY_FAILED ); |
| 455 | } |
| 456 | |
| 457 | return( 0 ); |
| 458 | } |
| 459 | |
| 460 | void mbedtls_lmots_init_private( mbedtls_lmots_private_t *ctx ) |
| 461 | { |
| 462 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ; |
| 463 | } |
| 464 | |
| 465 | void mbedtls_lmots_free_private( mbedtls_lmots_private_t *ctx ) |
| 466 | { |
| 467 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lmots_private_t ) ) ; |
| 468 | } |
| 469 | |
| 470 | int mbedtls_lmots_generate_private_key( mbedtls_lmots_private_t *ctx, |
| 471 | mbedtls_lmots_algorithm_type_t type, |
| 472 | const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN], |
| 473 | uint32_t q_leaf_identifier, |
| 474 | const unsigned char *seed, |
| 475 | size_t seed_size ) |
| 476 | { |
| 477 | psa_hash_operation_t op; |
| 478 | psa_status_t status; |
| 479 | size_t output_hash_len; |
| 480 | unsigned int i_digit_idx; |
| 481 | unsigned char i_digit_idx_bytes[2]; |
| 482 | unsigned char const_bytes[1]; |
| 483 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 484 | |
| 485 | if ( ctx->MBEDTLS_PRIVATE(have_private_key) ) |
| 486 | { |
| 487 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 488 | } |
| 489 | |
| 490 | if ( type != MBEDTLS_LMOTS_SHA256_N32_W8 ) { |
| 491 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 492 | } |
| 493 | |
| 494 | ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(type) = type; |
| 495 | |
| 496 | memcpy( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier), |
| 497 | I_key_identifier, |
| 498 | sizeof( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier) ) ); |
| 499 | |
| 500 | unsigned_int_to_network_bytes(q_leaf_identifier, |
| 501 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN, |
| 502 | ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(q_leaf_identifier) ); |
| 503 | |
| 504 | unsigned_int_to_network_bytes( 0xFF, sizeof( const_bytes ), const_bytes ); |
| 505 | |
| 506 | for ( i_digit_idx = 0; i_digit_idx < MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT; i_digit_idx++ ) |
| 507 | { |
| 508 | op = psa_hash_operation_init( ); |
| 509 | status = psa_hash_setup( &op, PSA_ALG_SHA_256 ); |
| 510 | ret = mbedtls_lms_error_from_psa( status ); |
| 511 | if ( ret != 0 ) |
| 512 | goto exit; |
| 513 | |
| 514 | ret = psa_hash_update( &op, |
| 515 | ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier), |
| 516 | sizeof( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier) ) ); |
| 517 | ret = mbedtls_lms_error_from_psa( status ); |
| 518 | if ( ret ) |
| 519 | goto exit; |
| 520 | |
| 521 | status = psa_hash_update( &op, |
| 522 | ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(q_leaf_identifier), |
| 523 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN ); |
| 524 | ret = mbedtls_lms_error_from_psa( status ); |
| 525 | if ( ret ) |
| 526 | goto exit; |
| 527 | |
| 528 | unsigned_int_to_network_bytes( i_digit_idx, I_DIGIT_IDX_LEN, i_digit_idx_bytes ); |
| 529 | status = psa_hash_update( &op, i_digit_idx_bytes, I_DIGIT_IDX_LEN ); |
| 530 | ret = mbedtls_lms_error_from_psa( status ); |
| 531 | if ( ret ) |
| 532 | goto exit; |
| 533 | |
| 534 | status = psa_hash_update( &op, const_bytes, sizeof( const_bytes) ); |
| 535 | ret = mbedtls_lms_error_from_psa( status ); |
| 536 | if ( ret ) |
| 537 | goto exit; |
| 538 | |
| 539 | status = psa_hash_update( &op, seed, seed_size ); |
| 540 | ret = mbedtls_lms_error_from_psa( status ); |
| 541 | if ( ret ) |
| 542 | goto exit; |
| 543 | |
| 544 | status = psa_hash_finish( &op, |
| 545 | ctx->MBEDTLS_PRIVATE(private_key)[i_digit_idx], |
| 546 | 32, &output_hash_len ); |
| 547 | ret = mbedtls_lms_error_from_psa( status ); |
| 548 | if ( ret ) |
| 549 | goto exit; |
| 550 | |
| 551 | psa_hash_abort( &op ); |
| 552 | } |
| 553 | |
| 554 | ctx->MBEDTLS_PRIVATE(have_private_key) = 1; |
| 555 | |
| 556 | exit: |
| 557 | if( ret ) |
| 558 | { |
| 559 | psa_hash_abort( &op ); |
| 560 | return( ret ); |
| 561 | } |
| 562 | |
| 563 | return ret; |
| 564 | } |
| 565 | |
| 566 | int mbedtls_lmots_calculate_public_key( mbedtls_lmots_public_t *ctx, |
| 567 | mbedtls_lmots_private_t *priv_ctx) |
| 568 | { |
| 569 | unsigned char y_hashed_digits[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN]; |
| 570 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 571 | |
| 572 | if( ctx == NULL ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 573 | { |
Raef Coles | 7dce69a | 2022-08-24 14:07:06 +0100 | [diff] [blame] | 574 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /* Check that a private key is loaded */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 578 | if ( !priv_ctx->MBEDTLS_PRIVATE(have_private_key) ) |
| 579 | { |
| 580 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 581 | } |
| 582 | |
| 583 | ret = hash_digit_array( &priv_ctx->MBEDTLS_PRIVATE(params), |
| 584 | ( const unsigned char( *)[MBEDTLS_LMOTS_N_HASH_LEN] )(priv_ctx->MBEDTLS_PRIVATE(private_key)), |
| 585 | NULL, NULL, y_hashed_digits ); |
| 586 | if ( ret ) |
| 587 | { |
| 588 | return( ret ); |
| 589 | } |
| 590 | |
| 591 | ret = public_key_from_hashed_digit_array( &priv_ctx->MBEDTLS_PRIVATE(params), |
| 592 | ( const unsigned char( *)[MBEDTLS_LMOTS_N_HASH_LEN] )y_hashed_digits, |
| 593 | ctx->MBEDTLS_PRIVATE(public_key) ); |
| 594 | if ( ret ) |
| 595 | { |
| 596 | return( ret ); |
| 597 | } |
| 598 | |
| 599 | memcpy( &ctx->MBEDTLS_PRIVATE(params), &priv_ctx->MBEDTLS_PRIVATE(params), |
| 600 | sizeof( ctx->MBEDTLS_PRIVATE(params) ) ); |
| 601 | |
| 602 | ctx->MBEDTLS_PRIVATE(have_public_key = 1); |
| 603 | |
| 604 | return( ret ); |
| 605 | } |
| 606 | |
| 607 | |
| 608 | int mbedtls_lmots_export_public_key( mbedtls_lmots_public_t *ctx, |
| 609 | unsigned char *key, size_t key_size, |
| 610 | size_t *key_len ) |
| 611 | { |
| 612 | if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN ) |
| 613 | { |
| 614 | return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL ); |
| 615 | } |
| 616 | |
| 617 | if( ! ctx->MBEDTLS_PRIVATE(have_public_key) ) |
| 618 | { |
| 619 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 620 | } |
| 621 | |
| 622 | unsigned_int_to_network_bytes( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(type), |
| 623 | MBEDTLS_LMOTS_TYPE_LEN, |
| 624 | key + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ); |
| 625 | |
| 626 | memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_I_KEY_ID_OFFSET, |
| 627 | ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(I_key_identifier), |
| 628 | MBEDTLS_LMOTS_I_KEY_ID_LEN ); |
| 629 | |
| 630 | memcpy(key + MBEDTLS_LMOTS_PUBLIC_KEY_Q_LEAF_ID_OFFSET, |
| 631 | ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(q_leaf_identifier), |
| 632 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN); |
| 633 | |
| 634 | memcpy( key + MBEDTLS_LMOTS_PUBLIC_KEY_KEY_HASH_OFFSET, ctx->MBEDTLS_PRIVATE(public_key), |
| 635 | MBEDTLS_LMOTS_N_HASH_LEN ); |
| 636 | |
| 637 | if( key_len != NULL ) |
| 638 | { |
| 639 | *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN; |
| 640 | } |
| 641 | |
| 642 | return( 0 ); |
| 643 | } |
| 644 | |
| 645 | int mbedtls_lmots_sign( mbedtls_lmots_private_t *ctx, |
| 646 | int (*f_rng)(void *, unsigned char *, size_t), |
| 647 | void *p_rng, const unsigned char *msg, size_t msg_size, |
| 648 | unsigned char *sig, size_t sig_size, size_t* sig_len ) |
| 649 | { |
| 650 | unsigned char tmp_digit_array[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT]; |
| 651 | unsigned char tmp_sig[MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT][MBEDTLS_LMOTS_N_HASH_LEN]; |
| 652 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 653 | |
| 654 | if ( msg == NULL && msg_size != 0 ) |
| 655 | { |
| 656 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 657 | } |
| 658 | |
| 659 | if( sig_size < MBEDTLS_LMOTS_SIG_LEN ) |
| 660 | { |
| 661 | return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL ); |
| 662 | } |
| 663 | |
| 664 | /* Check that a private key is loaded */ |
| 665 | if ( !ctx->MBEDTLS_PRIVATE(have_private_key) ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 666 | { |
Raef Coles | 7dce69a | 2022-08-24 14:07:06 +0100 | [diff] [blame] | 667 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | ret = f_rng( p_rng, sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, MBEDTLS_LMOTS_N_HASH_LEN ); |
| 671 | if ( ret ) |
| 672 | { |
| 673 | return( ret ); |
| 674 | } |
| 675 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 676 | ret = create_digit_array_with_checksum( &ctx->MBEDTLS_PRIVATE(params), |
| 677 | msg, msg_size, |
| 678 | sig + MBEDTLS_LMOTS_SIG_C_RANDOM_OFFSET, |
| 679 | tmp_digit_array ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 680 | if ( ret ) |
| 681 | { |
| 682 | return( ret ); |
| 683 | } |
| 684 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 685 | ret = hash_digit_array( &ctx->MBEDTLS_PRIVATE(params), |
| 686 | ( const unsigned char( *)[MBEDTLS_LMOTS_N_HASH_LEN] )(ctx->MBEDTLS_PRIVATE(private_key)), |
| 687 | NULL, tmp_digit_array, tmp_sig ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 688 | if ( ret ) |
| 689 | { |
| 690 | return( ret ); |
| 691 | } |
| 692 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 693 | unsigned_int_to_network_bytes( ctx->MBEDTLS_PRIVATE(params).MBEDTLS_PRIVATE(type), |
| 694 | MBEDTLS_LMOTS_TYPE_LEN, |
| 695 | sig + MBEDTLS_LMOTS_SIG_TYPE_OFFSET ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 696 | |
| 697 | /* We've got a valid signature now, so it's time to make sure the private |
| 698 | * key can't be reused. |
| 699 | */ |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 700 | ctx->MBEDTLS_PRIVATE(have_private_key) = 0; |
| 701 | mbedtls_platform_zeroize(ctx->MBEDTLS_PRIVATE(private_key), |
| 702 | sizeof(ctx->MBEDTLS_PRIVATE(private_key))); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 703 | |
| 704 | memcpy(sig + MBEDTLS_LMOTS_SIG_SIGNATURE_OFFSET, tmp_sig, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 705 | MBEDTLS_LMOTS_P_SIG_DIGIT_COUNT * MBEDTLS_LMOTS_N_HASH_LEN); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 706 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 707 | if( sig_len != NULL ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 708 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame^] | 709 | *sig_len = MBEDTLS_LMS_SIG_LEN; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | return( 0 ); |
| 713 | } |
| 714 | |
Raef Coles | 7dce69a | 2022-08-24 14:07:06 +0100 | [diff] [blame] | 715 | #endif /* MBEDTLS_LMS_C */ |