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