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