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