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