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