Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 1 | /* |
| 2 | * The LMS stateful-hash 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 LMS 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 | |
| 35 | #ifdef MBEDTLS_LMS_C |
| 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 "psa/crypto.h" |
| 42 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 43 | #include "mbedtls/lms.h" |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 44 | #include "mbedtls/error.h" |
| 45 | #include "mbedtls/platform_util.h" |
| 46 | |
| 47 | #if defined(MBEDTLS_PLATFORM_C) |
| 48 | #include "mbedtls/platform.h" |
| 49 | #else |
| 50 | #include <stdlib.h> |
| 51 | #include <stdio.h> |
| 52 | #define mbedtls_printf printf |
| 53 | #define mbedtls_calloc calloc |
| 54 | #define mbedtls_free free |
| 55 | #endif |
| 56 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 57 | #define MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET (0) |
| 58 | #define MBEDTLS_LMS_SIG_OTS_SIG_OFFSET (MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET + MBEDTLS_LMOTS_Q_LEAF_ID_LEN) |
| 59 | #define MBEDTLS_LMS_SIG_TYPE_OFFSET (MBEDTLS_LMS_SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_LEN) |
| 60 | #define MBEDTLS_LMS_SIG_PATH_OFFSET (MBEDTLS_LMS_SIG_TYPE_OFFSET + MBEDTLS_LMS_TYPE_LEN) |
| 61 | |
| 62 | #define MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET (0) |
| 63 | #define MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET (MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET + MBEDTLS_LMS_TYPE_LEN) |
| 64 | #define MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET (MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) |
| 65 | #define MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET (MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET + MBEDTLS_LMOTS_I_KEY_ID_LEN) |
| 66 | |
| 67 | |
| 68 | #define MERKLE_TREE_NODE_AM (1u << (MBEDTLS_LMS_H_TREE_HEIGHT + 1u)) |
| 69 | #define MERKLE_TREE_LEAF_NODE_AM (1u << MBEDTLS_LMS_H_TREE_HEIGHT) |
| 70 | #define MERKLE_TREE_INTERNAL_NODE_AM (1u << MBEDTLS_LMS_H_TREE_HEIGHT) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 71 | |
| 72 | #define D_CONST_LEN (2) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 73 | static const unsigned char D_LEAF_CONSTANT_BYTES[D_CONST_LEN] = {0x82, 0x82}; |
| 74 | static const unsigned char D_INTERNAL_CONSTANT_BYTES[D_CONST_LEN] = {0x83, 0x83}; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 75 | |
Raef Coles | ebd35b5 | 2022-09-01 11:52:17 +0100 | [diff] [blame] | 76 | static int create_merkle_leaf_value( const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN], |
| 77 | unsigned char pub_key[MBEDTLS_LMOTS_N_HASH_LEN], |
| 78 | unsigned int r_node_idx, |
| 79 | unsigned char out[MBEDTLS_LMS_M_NODE_BYTES] ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 80 | { |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 81 | psa_hash_operation_t op; |
| 82 | psa_status_t status; |
| 83 | size_t output_hash_len; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 84 | unsigned char r_node_idx_bytes[4]; |
| 85 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 86 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 87 | op = psa_hash_operation_init( ); |
| 88 | status = psa_hash_setup( &op, PSA_ALG_SHA_256 ); |
| 89 | ret = mbedtls_lms_error_from_psa( status ); |
| 90 | if ( ret != 0 ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 91 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 92 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 93 | status = psa_hash_update( &op, I_key_identifier, MBEDTLS_LMOTS_I_KEY_ID_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 94 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 95 | if( ret ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 96 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 97 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 98 | unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 99 | status = psa_hash_update( &op, r_node_idx_bytes, 4 ); |
| 100 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 101 | if( ret ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 102 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 103 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 104 | status = psa_hash_update( &op, D_LEAF_CONSTANT_BYTES, D_CONST_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 105 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 106 | if( ret ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 107 | goto exit; |
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 | status = psa_hash_update( &op, pub_key, MBEDTLS_LMOTS_N_HASH_LEN ); |
| 110 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 111 | if( ret ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 112 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 113 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 114 | status = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES, &output_hash_len); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 115 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 116 | if( ret ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 117 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 118 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 119 | exit: |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 120 | psa_hash_abort( &op ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 121 | |
| 122 | return( ret ); |
| 123 | } |
| 124 | |
Raef Coles | ebd35b5 | 2022-09-01 11:52:17 +0100 | [diff] [blame] | 125 | static int create_merkle_internal_value( const unsigned char I_key_identifier[MBEDTLS_LMOTS_I_KEY_ID_LEN], |
| 126 | const unsigned char left_node[MBEDTLS_LMS_M_NODE_BYTES], |
| 127 | const unsigned char right_node[MBEDTLS_LMS_M_NODE_BYTES], |
| 128 | unsigned int r_node_idx, |
| 129 | unsigned char out[MBEDTLS_LMS_M_NODE_BYTES] ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 130 | { |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 131 | psa_hash_operation_t op; |
| 132 | psa_status_t status; |
| 133 | size_t output_hash_len; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 134 | unsigned char r_node_idx_bytes[4]; |
| 135 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 136 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 137 | op = psa_hash_operation_init( ); |
| 138 | status = psa_hash_setup( &op, PSA_ALG_SHA_256 ); |
| 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 | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 143 | status = psa_hash_update( &op, I_key_identifier, MBEDTLS_LMOTS_I_KEY_ID_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 144 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 145 | if( ret ) |
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 | unsigned_int_to_network_bytes( r_node_idx, 4, r_node_idx_bytes ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 149 | status = psa_hash_update( &op, r_node_idx_bytes, 4 ); |
| 150 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 151 | if( ret ) |
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 | status = psa_hash_update( &op, D_INTERNAL_CONSTANT_BYTES, D_CONST_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 155 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 156 | if( ret ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 157 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 158 | |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 159 | status = psa_hash_update( &op, left_node, MBEDTLS_LMOTS_N_HASH_LEN ); |
| 160 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 161 | if( ret ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 162 | goto exit; |
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 | status = psa_hash_update( &op, right_node, MBEDTLS_LMOTS_N_HASH_LEN ); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 165 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 166 | if( ret ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 167 | goto exit; |
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 | ret = psa_hash_finish( &op, out, MBEDTLS_LMS_M_NODE_BYTES, &output_hash_len); |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 170 | ret = mbedtls_lms_error_from_psa( status ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 171 | if( ret ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 172 | goto exit; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 173 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 174 | exit: |
Raef Coles | c8f9604 | 2022-08-25 13:49:54 +0100 | [diff] [blame] | 175 | psa_hash_abort( &op ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 176 | |
| 177 | return ret; |
| 178 | } |
| 179 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 180 | void mbedtls_lms_init_public( mbedtls_lms_public_t *ctx ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 181 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 182 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) ) ; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 183 | } |
| 184 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 185 | void mbedtls_lms_free_public( mbedtls_lms_public_t *ctx ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 186 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 187 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 188 | } |
| 189 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 190 | int mbedtls_lms_import_public_key( mbedtls_lms_public_t *ctx, |
| 191 | const unsigned char *key, size_t key_size ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 192 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 193 | mbedtls_lms_algorithm_type_t type; |
| 194 | mbedtls_lmots_algorithm_type_t otstype; |
| 195 | |
| 196 | if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN ) |
| 197 | { |
| 198 | return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL ); |
| 199 | } |
| 200 | |
| 201 | type = network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN, key + MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET ); |
| 202 | if( type != MBEDTLS_LMS_SHA256_M32_H10 ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 203 | { |
| 204 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 205 | } |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 206 | ctx->params.type = type; |
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 | otstype = network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN, |
| 209 | key + MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET ); |
| 210 | if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 ) |
| 211 | { |
| 212 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 213 | } |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 214 | ctx->params.otstype = otstype; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 215 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 216 | memcpy( ctx->params.I_key_identifier, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 217 | key + MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET, |
| 218 | MBEDTLS_LMOTS_I_KEY_ID_LEN ); |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 219 | memcpy( ctx->T_1_pub_key, key + MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 220 | MBEDTLS_LMOTS_N_HASH_LEN ); |
| 221 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 222 | ctx->have_public_key = 1; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 223 | |
| 224 | return( 0 ); |
| 225 | } |
| 226 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 227 | int mbedtls_lms_verify( const mbedtls_lms_public_t *ctx, |
| 228 | const unsigned char *msg, size_t msg_size, |
| 229 | const unsigned char *sig, size_t sig_size ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 230 | { |
| 231 | unsigned int q_leaf_identifier; |
| 232 | unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN]; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 233 | unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 234 | unsigned int height; |
| 235 | unsigned int curr_node_id; |
| 236 | unsigned int parent_node_id; |
| 237 | const unsigned char* left_node; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 238 | const unsigned char* right_node; |
| 239 | mbedtls_lmots_parameters_t ots_params; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 240 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 241 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 242 | if( ! ctx->have_public_key ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 243 | { |
| 244 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 245 | } |
| 246 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 247 | if( sig_size != MBEDTLS_LMS_SIG_LEN ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 248 | { |
| 249 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 250 | } |
| 251 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 252 | if( ctx->params.type |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 253 | != MBEDTLS_LMS_SHA256_M32_H10 ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 254 | { |
| 255 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 256 | } |
| 257 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 258 | if( ctx->params.otstype |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 259 | != MBEDTLS_LMOTS_SHA256_N32_W8 ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 260 | { |
| 261 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 262 | } |
| 263 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 264 | if( network_bytes_to_unsigned_int( MBEDTLS_LMS_TYPE_LEN, |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 265 | sig + MBEDTLS_LMS_SIG_TYPE_OFFSET) != MBEDTLS_LMS_SHA256_M32_H10 ) |
| 266 | { |
| 267 | return( MBEDTLS_ERR_LMS_VERIFY_FAILED ); |
| 268 | } |
| 269 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 270 | if( network_bytes_to_unsigned_int( MBEDTLS_LMOTS_TYPE_LEN, |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 271 | sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_SIG_TYPE_OFFSET) |
| 272 | != MBEDTLS_LMOTS_SHA256_N32_W8 ) |
| 273 | { |
| 274 | return( MBEDTLS_ERR_LMS_VERIFY_FAILED ); |
| 275 | } |
| 276 | |
| 277 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 278 | q_leaf_identifier = network_bytes_to_unsigned_int( MBEDTLS_LMOTS_Q_LEAF_ID_LEN, |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 279 | sig + MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET ); |
| 280 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 281 | if( q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 282 | { |
| 283 | return( MBEDTLS_ERR_LMS_VERIFY_FAILED ); |
| 284 | } |
| 285 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 286 | memcpy(ots_params.I_key_identifier, |
| 287 | ctx->params.I_key_identifier, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 288 | MBEDTLS_LMOTS_I_KEY_ID_LEN); |
| 289 | unsigned_int_to_network_bytes( q_leaf_identifier, |
| 290 | MBEDTLS_LMOTS_Q_LEAF_ID_LEN, |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 291 | ots_params.q_leaf_identifier ); |
| 292 | ots_params.type = ctx->params.otstype; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 293 | |
| 294 | ret = mbedtls_lmots_calculate_public_key_candidate( &ots_params, msg, msg_size, |
| 295 | sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET, |
| 296 | MBEDTLS_LMOTS_SIG_LEN, |
| 297 | Kc_candidate_ots_pub_key, |
| 298 | sizeof(Kc_candidate_ots_pub_key), |
| 299 | NULL ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 300 | if( ret ) |
| 301 | { |
| 302 | return( ret ); |
| 303 | } |
| 304 | |
Raef Coles | ebd35b5 | 2022-09-01 11:52:17 +0100 | [diff] [blame] | 305 | create_merkle_leaf_value( |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 306 | ctx->params.I_key_identifier, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 307 | Kc_candidate_ots_pub_key, MERKLE_TREE_INTERNAL_NODE_AM + q_leaf_identifier, |
| 308 | Tc_candidate_root_node ); |
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 | curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM + q_leaf_identifier; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 311 | |
| 312 | for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT; height++ ) |
| 313 | { |
| 314 | parent_node_id = curr_node_id / 2; |
| 315 | |
| 316 | /* Left/right node ordering matters for the hash */ |
| 317 | if( curr_node_id & 1 ) |
| 318 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 319 | left_node = ( ( const unsigned char( * )[MBEDTLS_LMS_M_NODE_BYTES] )( sig + MBEDTLS_LMS_SIG_PATH_OFFSET ) )[height]; |
| 320 | right_node = Tc_candidate_root_node; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 321 | } |
| 322 | else |
| 323 | { |
| 324 | left_node = Tc_candidate_root_node; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 325 | right_node = ( ( const unsigned char( * )[MBEDTLS_LMS_M_NODE_BYTES] )( sig + MBEDTLS_LMS_SIG_PATH_OFFSET ) )[height]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 326 | } |
| 327 | |
Raef Coles | ebd35b5 | 2022-09-01 11:52:17 +0100 | [diff] [blame] | 328 | create_merkle_internal_value( |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 329 | ctx->params.I_key_identifier, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 330 | left_node, right_node, parent_node_id, Tc_candidate_root_node); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 331 | |
| 332 | curr_node_id /= 2; |
| 333 | } |
| 334 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 335 | if( memcmp( Tc_candidate_root_node, ctx->T_1_pub_key, |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 336 | MBEDTLS_LMOTS_N_HASH_LEN) ) |
| 337 | { |
| 338 | return( MBEDTLS_ERR_LMS_VERIFY_FAILED ); |
| 339 | } |
| 340 | |
| 341 | return( 0 ); |
| 342 | } |
| 343 | |
Raef Coles | ab4f874 | 2022-09-01 12:24:31 +0100 | [diff] [blame^] | 344 | #ifdef MBEDTLS_LMS_PRIVATE |
| 345 | |
| 346 | static int calculate_merkle_tree( mbedtls_lms_private_t *ctx, |
| 347 | unsigned char tree[MERKLE_TREE_NODE_AM][MBEDTLS_LMS_M_NODE_BYTES] ) |
| 348 | { |
| 349 | unsigned int priv_key_idx; |
| 350 | unsigned int r_node_idx; |
| 351 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 352 | |
| 353 | /* First create the leaf nodes, in ascending order */ |
| 354 | for( priv_key_idx = 0; priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM; |
| 355 | priv_key_idx++ ) |
| 356 | { |
| 357 | r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM + priv_key_idx; |
| 358 | |
| 359 | ret = create_merkle_leaf_value( |
| 360 | ctx->params.I_key_identifier, |
| 361 | ctx->ots_public_keys[priv_key_idx].public_key, |
| 362 | r_node_idx, tree[r_node_idx] ); |
| 363 | if( ret ) |
| 364 | { |
| 365 | return( ret ); |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | /* Then the internal nodes, in reverse order so that we can guarantee the |
| 370 | * parent has been created */ |
| 371 | for( r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM - 1; r_node_idx > 0; |
| 372 | r_node_idx-- ) |
| 373 | { |
| 374 | ret = create_merkle_internal_value( |
| 375 | ctx->params.I_key_identifier, |
| 376 | tree[(r_node_idx * 2)], tree[(r_node_idx * 2 + 1)], r_node_idx, tree[r_node_idx] ); |
| 377 | if( ret ) |
| 378 | { |
| 379 | return( ret ); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | return( 0 ); |
| 384 | } |
| 385 | |
| 386 | static int get_merkle_path( mbedtls_lms_private_t *ctx, |
| 387 | unsigned int leaf_node_id, |
| 388 | unsigned char path[MBEDTLS_LMS_H_TREE_HEIGHT][MBEDTLS_LMS_M_NODE_BYTES] ) |
| 389 | { |
| 390 | unsigned char tree[MERKLE_TREE_NODE_AM][MBEDTLS_LMS_M_NODE_BYTES]; |
| 391 | unsigned int curr_node_id = leaf_node_id; |
| 392 | unsigned int adjacent_node_id; |
| 393 | unsigned int height; |
| 394 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 395 | |
| 396 | ret = calculate_merkle_tree( ctx, tree); |
| 397 | if( ret ) |
| 398 | { |
| 399 | return( ret ); |
| 400 | } |
| 401 | |
| 402 | for( height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT; height++ ) |
| 403 | { |
| 404 | adjacent_node_id = curr_node_id ^ 1; |
| 405 | |
| 406 | memcpy( &path[height], &tree[adjacent_node_id], MBEDTLS_LMOTS_N_HASH_LEN ); |
| 407 | |
| 408 | curr_node_id >>=1; |
| 409 | } |
| 410 | |
| 411 | return( 0 ); |
| 412 | } |
| 413 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 414 | void mbedtls_lms_init_private( mbedtls_lms_private_t *ctx ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 415 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 416 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) ) ; |
| 417 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 418 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 419 | void mbedtls_lms_free_private( mbedtls_lms_private_t *ctx ) |
| 420 | { |
| 421 | unsigned int idx; |
| 422 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 423 | if( ctx->have_private_key ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 424 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 425 | for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM; idx++ ) |
| 426 | { |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 427 | mbedtls_lmots_free_private( &ctx->ots_private_keys[idx] ); |
| 428 | mbedtls_lmots_free_public( &ctx->ots_public_keys[idx] ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 429 | } |
| 430 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 431 | mbedtls_free( ctx->ots_private_keys ); |
| 432 | mbedtls_free( ctx->ots_public_keys ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 433 | } |
| 434 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 435 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_lms_public_t ) ); |
| 436 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 437 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 438 | |
| 439 | int mbedtls_lms_generate_private_key( mbedtls_lms_private_t *ctx, |
| 440 | mbedtls_lms_algorithm_type_t type, |
| 441 | mbedtls_lmots_algorithm_type_t otstype, |
| 442 | int (*f_rng)(void *, unsigned char *, size_t), |
| 443 | void* p_rng, unsigned char *seed, |
| 444 | size_t seed_size ) |
| 445 | { |
| 446 | unsigned int idx = 0; |
| 447 | unsigned int free_idx = 0; |
| 448 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 449 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 450 | if( type != MBEDTLS_LMS_SHA256_M32_H10 ) |
| 451 | { |
| 452 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 453 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 454 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 455 | if( otstype != MBEDTLS_LMOTS_SHA256_N32_W8 ) |
| 456 | { |
| 457 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 458 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 459 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 460 | if( ctx->have_private_key ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 461 | { |
| 462 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 463 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 464 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 465 | ctx->params.type = type; |
| 466 | ctx->params.otstype = otstype; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 467 | |
| 468 | f_rng( p_rng, |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 469 | ctx->params.I_key_identifier, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 470 | MBEDTLS_LMOTS_I_KEY_ID_LEN ); |
| 471 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 472 | ctx->ots_private_keys = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 473 | sizeof( mbedtls_lmots_private_t)); |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 474 | if( ctx->ots_private_keys == NULL ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 475 | { |
| 476 | ret = MBEDTLS_ERR_LMS_ALLOC_FAILED; |
| 477 | goto exit; |
| 478 | } |
| 479 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 480 | ctx->ots_public_keys = mbedtls_calloc( MERKLE_TREE_LEAF_NODE_AM, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 481 | sizeof( mbedtls_lmots_public_t)); |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 482 | if( ctx->ots_public_keys == NULL ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 483 | { |
| 484 | ret = MBEDTLS_ERR_LMS_ALLOC_FAILED; |
| 485 | goto exit; |
| 486 | } |
| 487 | |
| 488 | for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM; idx++ ) |
| 489 | { |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 490 | mbedtls_lmots_init_private( &ctx->ots_private_keys[idx] ); |
| 491 | mbedtls_lmots_init_public( &ctx->ots_public_keys[idx] ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | |
| 495 | for( idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM; idx++ ) |
| 496 | { |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 497 | ret = mbedtls_lmots_generate_private_key( &ctx->ots_private_keys[idx], |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 498 | otstype, |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 499 | ctx->params.I_key_identifier, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 500 | idx, seed, seed_size ); |
| 501 | if( ret) |
| 502 | goto exit; |
| 503 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 504 | ret = mbedtls_lmots_calculate_public_key( &ctx->ots_public_keys[idx], |
| 505 | &ctx->ots_private_keys[idx] ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 506 | if( ret) |
| 507 | goto exit; |
| 508 | } |
| 509 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 510 | ctx->q_next_usable_key = 0; |
| 511 | ctx->have_private_key = 1; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 512 | |
| 513 | exit: |
| 514 | if( ret ) |
| 515 | { |
| 516 | for ( free_idx = 0; free_idx < idx; free_idx++ ) { |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 517 | mbedtls_lmots_free_private( &ctx->ots_private_keys[free_idx] ); |
| 518 | mbedtls_lmots_free_public( &ctx->ots_public_keys[free_idx] ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 519 | } |
| 520 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 521 | mbedtls_free( ctx->ots_private_keys ); |
| 522 | mbedtls_free( ctx->ots_public_keys ); |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 523 | return( ret ); |
| 524 | } |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 525 | |
| 526 | return( 0 ); |
| 527 | } |
| 528 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 529 | int mbedtls_lms_calculate_public_key( mbedtls_lms_public_t *ctx, |
| 530 | mbedtls_lms_private_t *priv_ctx ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 531 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 532 | unsigned char tree[MERKLE_TREE_NODE_AM][MBEDTLS_LMS_M_NODE_BYTES]; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 533 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 534 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 535 | if( ! priv_ctx->MBEDTLS_PRIVATE( have_private_key ) ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 536 | { |
| 537 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 538 | } |
| 539 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 540 | if( priv_ctx->params.type |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 541 | != MBEDTLS_LMS_SHA256_M32_H10 ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 542 | { |
| 543 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 544 | } |
| 545 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 546 | if( priv_ctx->params.otstype |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 547 | != MBEDTLS_LMOTS_SHA256_N32_W8 ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 548 | { |
| 549 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 550 | } |
| 551 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 552 | memcpy( &ctx->params, &priv_ctx->params, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 553 | sizeof(mbedtls_lmots_parameters_t) ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 554 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 555 | ret = calculate_merkle_tree( priv_ctx, tree); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 556 | if( ret ) |
| 557 | { |
| 558 | return( ret ); |
| 559 | } |
| 560 | |
| 561 | /* Root node is always at position 1, due to 1-based indexing */ |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 562 | memcpy( ctx->T_1_pub_key, &tree[1], MBEDTLS_LMOTS_N_HASH_LEN ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 563 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 564 | ctx->have_public_key = 1; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 565 | |
| 566 | return( 0 ); |
| 567 | } |
| 568 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 569 | |
| 570 | int mbedtls_lms_export_public_key( mbedtls_lms_public_t *ctx, unsigned char *key, |
| 571 | size_t key_size, size_t *key_len ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 572 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 573 | if( key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN ) { |
| 574 | return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL ); |
| 575 | } |
| 576 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 577 | if( ! ctx->have_public_key ) |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 578 | { |
| 579 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 580 | } |
| 581 | |
| 582 | unsigned_int_to_network_bytes( |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 583 | ctx->params.type, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 584 | MBEDTLS_LMS_TYPE_LEN, key + MBEDTLS_LMS_PUBLIC_KEY_TYPE_OFFSET ); |
| 585 | unsigned_int_to_network_bytes( |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 586 | ctx->params.otstype, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 587 | MBEDTLS_LMOTS_TYPE_LEN, key + MBEDTLS_LMS_PUBLIC_KEY_OTSTYPE_OFFSET ); |
| 588 | memcpy( key + MBEDTLS_LMS_PUBLIC_KEY_I_KEY_ID_OFFSET, |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 589 | ctx->params.I_key_identifier, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 590 | MBEDTLS_LMOTS_I_KEY_ID_LEN ); |
| 591 | memcpy( key + MBEDTLS_LMS_PUBLIC_KEY_ROOT_NODE_OFFSET, |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 592 | ctx->T_1_pub_key, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 593 | MBEDTLS_LMOTS_N_HASH_LEN ); |
| 594 | |
| 595 | if( key_len != NULL ) { |
| 596 | *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN; |
| 597 | } |
| 598 | |
| 599 | return( 0 ); |
| 600 | } |
| 601 | |
| 602 | |
| 603 | int mbedtls_lms_sign( mbedtls_lms_private_t *ctx, |
| 604 | int (*f_rng)(void *, unsigned char *, size_t), |
| 605 | void* p_rng, unsigned char *msg, unsigned int msg_size, |
| 606 | unsigned char *sig, size_t sig_size, size_t *sig_len) |
| 607 | { |
| 608 | uint32_t q_leaf_identifier; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 609 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
| 610 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 611 | if( ! ctx->have_private_key ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 612 | { |
| 613 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 614 | } |
| 615 | |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 616 | if( sig_size < MBEDTLS_LMS_SIG_LEN ) |
| 617 | { |
| 618 | return( MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL ); |
| 619 | } |
| 620 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 621 | if( ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10 ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 622 | { |
| 623 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 624 | } |
| 625 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 626 | if( ctx->params.otstype |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 627 | != MBEDTLS_LMOTS_SHA256_N32_W8 ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 628 | { |
| 629 | return( MBEDTLS_ERR_LMS_BAD_INPUT_DATA ); |
| 630 | } |
| 631 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 632 | if( ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM ) |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 633 | { |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 634 | return( MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 635 | } |
| 636 | |
| 637 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 638 | q_leaf_identifier = ctx->q_next_usable_key; |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 639 | /* This new value must _always_ be written back to the disk before the |
| 640 | * signature is returned. |
| 641 | */ |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 642 | ctx->q_next_usable_key += 1; |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 643 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 644 | ret = mbedtls_lmots_sign( &ctx->ots_private_keys[q_leaf_identifier], |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 645 | f_rng, p_rng, msg, msg_size, |
| 646 | sig + MBEDTLS_LMS_SIG_OTS_SIG_OFFSET, |
| 647 | MBEDTLS_LMS_SIG_LEN, NULL ); |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 648 | if( ret ) |
| 649 | { |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 650 | return( ret ); |
| 651 | } |
| 652 | |
Raef Coles | f5632d3 | 2022-09-01 09:56:52 +0100 | [diff] [blame] | 653 | unsigned_int_to_network_bytes( ctx->params.type, |
Raef Coles | 01c71a1 | 2022-08-31 15:55:00 +0100 | [diff] [blame] | 654 | MBEDTLS_LMS_TYPE_LEN, sig + MBEDTLS_LMS_SIG_TYPE_OFFSET ); |
| 655 | unsigned_int_to_network_bytes( q_leaf_identifier, MBEDTLS_LMOTS_Q_LEAF_ID_LEN, |
| 656 | sig + MBEDTLS_LMS_SIG_Q_LEAF_ID_OFFSET); |
| 657 | |
| 658 | ret = get_merkle_path( ctx, MERKLE_TREE_INTERNAL_NODE_AM + q_leaf_identifier, |
| 659 | ( unsigned char( * )[MBEDTLS_LMS_M_NODE_BYTES] )( sig + MBEDTLS_LMS_SIG_PATH_OFFSET ) ); |
| 660 | if( ret ) |
| 661 | { |
| 662 | return( ret ); |
| 663 | } |
| 664 | |
| 665 | if( sig_len != NULL ) { |
| 666 | *sig_len = MBEDTLS_LMS_SIG_LEN; |
| 667 | } |
| 668 | |
| 669 | |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 670 | return( 0 ); |
| 671 | } |
| 672 | |
Raef Coles | ab4f874 | 2022-09-01 12:24:31 +0100 | [diff] [blame^] | 673 | #endif /* MBEDTLS_LMS_PRIVATE */ |
Raef Coles | 8ff6df5 | 2021-07-21 12:42:15 +0100 | [diff] [blame] | 674 | #endif /* MBEDTLS_LMS_C */ |