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