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