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