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