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