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