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