2021-07-21 13:42:15 +02:00
|
|
|
/*
|
|
|
|
* The LMS stateful-hash public-key signature scheme
|
|
|
|
*
|
|
|
|
* Copyright The Mbed TLS Contributors
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The following sources were referenced in the design of this implementation
|
|
|
|
* of the LMS algorithm:
|
|
|
|
*
|
|
|
|
* [1] IETF RFC8554
|
|
|
|
* D. McGrew, M. Curcio, S.Fluhrer
|
|
|
|
* https://datatracker.ietf.org/doc/html/rfc8554
|
|
|
|
*
|
|
|
|
* [2] NIST Special Publication 800-208
|
|
|
|
* David A. Cooper et. al.
|
|
|
|
* https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-208.pdf
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
|
2022-10-07 11:35:56 +02:00
|
|
|
#if defined(MBEDTLS_LMS_C)
|
2021-07-21 13:42:15 +02:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2022-08-24 15:07:06 +02:00
|
|
|
#include "lmots.h"
|
|
|
|
|
2022-08-25 14:49:54 +02:00
|
|
|
#include "psa/crypto.h"
|
2022-12-23 17:00:06 +01:00
|
|
|
#include "mbedtls/psa_util.h"
|
2021-07-21 13:42:15 +02:00
|
|
|
#include "mbedtls/lms.h"
|
|
|
|
#include "mbedtls/error.h"
|
|
|
|
#include "mbedtls/platform_util.h"
|
|
|
|
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
2022-12-23 17:00:06 +01:00
|
|
|
#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
|
|
|
|
psa_to_lms_errors, \
|
|
|
|
psa_generic_status_to_mbedtls)
|
|
|
|
|
2022-09-27 12:30:51 +02:00
|
|
|
#define SIG_Q_LEAF_ID_OFFSET (0)
|
|
|
|
#define SIG_OTS_SIG_OFFSET (SIG_Q_LEAF_ID_OFFSET + \
|
|
|
|
MBEDTLS_LMOTS_Q_LEAF_ID_LEN)
|
|
|
|
#define SIG_TYPE_OFFSET(otstype) (SIG_OTS_SIG_OFFSET + \
|
|
|
|
MBEDTLS_LMOTS_SIG_LEN(otstype))
|
|
|
|
#define SIG_PATH_OFFSET(otstype) (SIG_TYPE_OFFSET(otstype) + \
|
|
|
|
MBEDTLS_LMS_TYPE_LEN)
|
|
|
|
|
|
|
|
#define PUBLIC_KEY_TYPE_OFFSET (0)
|
|
|
|
#define PUBLIC_KEY_OTSTYPE_OFFSET (PUBLIC_KEY_TYPE_OFFSET + \
|
|
|
|
MBEDTLS_LMS_TYPE_LEN)
|
|
|
|
#define PUBLIC_KEY_I_KEY_ID_OFFSET (PUBLIC_KEY_OTSTYPE_OFFSET + \
|
|
|
|
MBEDTLS_LMOTS_TYPE_LEN)
|
|
|
|
#define PUBLIC_KEY_ROOT_NODE_OFFSET (PUBLIC_KEY_I_KEY_ID_OFFSET + \
|
|
|
|
MBEDTLS_LMOTS_I_KEY_ID_LEN)
|
2021-07-21 13:42:15 +02:00
|
|
|
|
|
|
|
|
2022-09-01 17:06:35 +02:00
|
|
|
/* Currently only support H=10 */
|
2022-09-27 12:30:51 +02:00
|
|
|
#define H_TREE_HEIGHT_MAX 10
|
2022-11-12 17:28:04 +01:00
|
|
|
#define MERKLE_TREE_NODE_AM(type) ((size_t) 1 << (MBEDTLS_LMS_H_TREE_HEIGHT(type) + 1u))
|
|
|
|
#define MERKLE_TREE_LEAF_NODE_AM(type) ((size_t) 1 << MBEDTLS_LMS_H_TREE_HEIGHT(type))
|
|
|
|
#define MERKLE_TREE_INTERNAL_NODE_AM(type) ((size_t) 1 << MBEDTLS_LMS_H_TREE_HEIGHT(type))
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
#define D_CONST_LEN (2)
|
2023-01-11 14:50:10 +01:00
|
|
|
static const unsigned char D_LEAF_CONSTANT_BYTES[D_CONST_LEN] = { 0x82, 0x82 };
|
|
|
|
static const unsigned char D_INTR_CONSTANT_BYTES[D_CONST_LEN] = { 0x83, 0x83 };
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-02 18:46:15 +02:00
|
|
|
|
2022-10-10 16:44:17 +02:00
|
|
|
/* Calculate the value of a leaf node of the Merkle tree (which is a hash of a
|
2022-09-02 18:46:15 +02:00
|
|
|
* public key and some other parameters like the leaf index). This function
|
|
|
|
* implements RFC8554 section 5.3, in the case where r >= 2^h.
|
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* params The LMS parameter set, the underlying LMOTS
|
|
|
|
* parameter set, and I value which describe the key
|
|
|
|
* being used.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* pub_key The public key of the private whose index
|
|
|
|
* corresponds to the index of this leaf node. This
|
|
|
|
* is a hash output.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-10-10 16:44:17 +02:00
|
|
|
* r_node_idx The index of this node in the Merkle tree. Note
|
|
|
|
* that the root node of the Merkle tree is
|
2022-09-23 10:04:04 +02:00
|
|
|
* 1-indexed.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* out The output node value, which is a hash output.
|
2022-09-02 18:46:15 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int create_merkle_leaf_value(const mbedtls_lms_parameters_t *params,
|
|
|
|
unsigned char *pub_key,
|
|
|
|
unsigned int r_node_idx,
|
|
|
|
unsigned char *out)
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-08-25 14:49:54 +02:00
|
|
|
psa_hash_operation_t op;
|
2022-10-07 12:27:35 +02:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-08-25 14:49:54 +02:00
|
|
|
size_t output_hash_len;
|
2021-07-21 13:42:15 +02:00
|
|
|
unsigned char r_node_idx_bytes[4];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
op = psa_hash_operation_init();
|
|
|
|
status = psa_hash_setup(&op, PSA_ALG_SHA_256);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_hash_update(&op, params->I_key_identifier,
|
|
|
|
MBEDTLS_LMOTS_I_KEY_ID_LEN);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_lms_unsigned_int_to_network_bytes(r_node_idx, 4, r_node_idx_bytes);
|
|
|
|
status = psa_hash_update(&op, r_node_idx_bytes, 4);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_hash_update(&op, D_LEAF_CONSTANT_BYTES, D_CONST_LEN);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_hash_update(&op, pub_key,
|
|
|
|
MBEDTLS_LMOTS_N_HASH_LEN(params->otstype));
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_hash_finish(&op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
|
|
|
|
&output_hash_len);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_hash_abort(&op);
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-12-23 17:00:06 +01:00
|
|
|
return PSA_TO_MBEDTLS_ERR(status);
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-10-10 16:44:17 +02:00
|
|
|
/* Calculate the value of an internal node of the Merkle tree (which is a hash
|
2022-09-02 18:46:15 +02:00
|
|
|
* of a public key and some other parameters like the node index). This function
|
|
|
|
* implements RFC8554 section 5.3, in the case where r < 2^h.
|
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* params The LMS parameter set, the underlying LMOTS
|
|
|
|
* parameter set, and I value which describe the key
|
|
|
|
* being used.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* left_node The value of the child of this node which is on
|
|
|
|
* the left-hand side. As with all nodes on the
|
2022-10-10 16:44:17 +02:00
|
|
|
* Merkle tree, this is a hash output.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* right_node The value of the child of this node which is on
|
|
|
|
* the right-hand side. As with all nodes on the
|
2022-10-10 16:44:17 +02:00
|
|
|
* Merkle tree, this is a hash output.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-10-10 16:44:17 +02:00
|
|
|
* r_node_idx The index of this node in the Merkle tree. Note
|
|
|
|
* that the root node of the Merkle tree is
|
2022-09-23 10:04:04 +02:00
|
|
|
* 1-indexed.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* out The output node value, which is a hash output.
|
2022-09-02 18:46:15 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int create_merkle_internal_value(const mbedtls_lms_parameters_t *params,
|
|
|
|
const unsigned char *left_node,
|
|
|
|
const unsigned char *right_node,
|
|
|
|
unsigned int r_node_idx,
|
|
|
|
unsigned char *out)
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-08-25 14:49:54 +02:00
|
|
|
psa_hash_operation_t op;
|
2022-10-07 12:27:35 +02:00
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
2022-08-25 14:49:54 +02:00
|
|
|
size_t output_hash_len;
|
2021-07-21 13:42:15 +02:00
|
|
|
unsigned char r_node_idx_bytes[4];
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
op = psa_hash_operation_init();
|
|
|
|
status = psa_hash_setup(&op, PSA_ALG_SHA_256);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_hash_update(&op, params->I_key_identifier,
|
|
|
|
MBEDTLS_LMOTS_I_KEY_ID_LEN);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_lms_unsigned_int_to_network_bytes(r_node_idx, 4, r_node_idx_bytes);
|
|
|
|
status = psa_hash_update(&op, r_node_idx_bytes, 4);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_hash_update(&op, D_INTR_CONSTANT_BYTES, D_CONST_LEN);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_hash_update(&op, left_node,
|
|
|
|
MBEDTLS_LMS_M_NODE_BYTES(params->type));
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_hash_update(&op, right_node,
|
|
|
|
MBEDTLS_LMS_M_NODE_BYTES(params->type));
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
status = psa_hash_finish(&op, out, MBEDTLS_LMS_M_NODE_BYTES(params->type),
|
|
|
|
&output_hash_len);
|
|
|
|
if (status != PSA_SUCCESS) {
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
psa_hash_abort(&op);
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-12-23 17:00:06 +01:00
|
|
|
return PSA_TO_MBEDTLS_ERR(status);
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_lms_public_init(mbedtls_lms_public_t *ctx)
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(ctx, 0, sizeof(*ctx));
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_lms_public_free(mbedtls_lms_public_t *ctx)
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(ctx, sizeof(*ctx));
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_lms_import_public_key(mbedtls_lms_public_t *ctx,
|
|
|
|
const unsigned char *key, size_t key_size)
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lms_algorithm_type_t type;
|
|
|
|
mbedtls_lmots_algorithm_type_t otstype;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
type = mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMS_TYPE_LEN,
|
|
|
|
key + PUBLIC_KEY_TYPE_OFFSET);
|
|
|
|
if (type != MBEDTLS_LMS_SHA256_M32_H10) {
|
|
|
|
return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
2022-09-01 10:56:52 +02:00
|
|
|
ctx->params.type = type;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (key_size != MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) {
|
|
|
|
return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
|
2022-10-07 17:06:35 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
otstype = mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMOTS_TYPE_LEN,
|
|
|
|
key + PUBLIC_KEY_OTSTYPE_OFFSET);
|
|
|
|
if (otstype != MBEDTLS_LMOTS_SHA256_N32_W8) {
|
|
|
|
return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
2022-09-01 10:56:52 +02:00
|
|
|
ctx->params.otstype = otstype;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(ctx->params.I_key_identifier,
|
|
|
|
key + PUBLIC_KEY_I_KEY_ID_OFFSET,
|
|
|
|
MBEDTLS_LMOTS_I_KEY_ID_LEN);
|
|
|
|
memcpy(ctx->T_1_pub_key, key + PUBLIC_KEY_ROOT_NODE_OFFSET,
|
|
|
|
MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
ctx->have_public_key = 1;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_lms_export_public_key(const mbedtls_lms_public_t *ctx,
|
|
|
|
unsigned char *key,
|
|
|
|
size_t key_size, size_t *key_len)
|
2022-10-07 17:07:33 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) {
|
|
|
|
return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
|
2022-10-07 17:07:33 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!ctx->have_public_key) {
|
|
|
|
return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
|
2022-10-07 17:07:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_lms_unsigned_int_to_network_bytes(
|
2023-01-11 14:50:10 +01:00
|
|
|
ctx->params.type,
|
|
|
|
MBEDTLS_LMS_TYPE_LEN, key + PUBLIC_KEY_TYPE_OFFSET);
|
|
|
|
mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.otstype,
|
|
|
|
MBEDTLS_LMOTS_TYPE_LEN,
|
|
|
|
key + PUBLIC_KEY_OTSTYPE_OFFSET);
|
|
|
|
memcpy(key + PUBLIC_KEY_I_KEY_ID_OFFSET,
|
|
|
|
ctx->params.I_key_identifier,
|
|
|
|
MBEDTLS_LMOTS_I_KEY_ID_LEN);
|
|
|
|
memcpy(key +PUBLIC_KEY_ROOT_NODE_OFFSET,
|
|
|
|
ctx->T_1_pub_key,
|
|
|
|
MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type));
|
|
|
|
|
|
|
|
if (key_len != NULL) {
|
2022-10-07 17:07:33 +02:00
|
|
|
*key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type);
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-10-07 17:07:33 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_lms_verify(const mbedtls_lms_public_t *ctx,
|
|
|
|
const unsigned char *msg, size_t msg_size,
|
|
|
|
const unsigned char *sig, size_t sig_size)
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
|
|
|
unsigned int q_leaf_identifier;
|
2022-09-01 17:06:35 +02:00
|
|
|
unsigned char Kc_candidate_ots_pub_key[MBEDTLS_LMOTS_N_HASH_LEN_MAX];
|
|
|
|
unsigned char Tc_candidate_root_node[MBEDTLS_LMS_M_NODE_BYTES_MAX];
|
2021-07-21 13:42:15 +02:00
|
|
|
unsigned int height;
|
|
|
|
unsigned int curr_node_id;
|
|
|
|
unsigned int parent_node_id;
|
2023-01-11 14:50:10 +01:00
|
|
|
const unsigned char *left_node;
|
|
|
|
const unsigned char *right_node;
|
2022-08-31 16:55:00 +02:00
|
|
|
mbedtls_lmots_parameters_t ots_params;
|
2021-07-21 13:42:15 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!ctx->have_public_key) {
|
|
|
|
return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx->params.type
|
|
|
|
!= MBEDTLS_LMS_SHA256_M32_H10) {
|
|
|
|
return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx->params.otstype
|
|
|
|
!= MBEDTLS_LMOTS_SHA256_N32_W8) {
|
|
|
|
return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) {
|
|
|
|
return MBEDTLS_ERR_LMS_VERIFY_FAILED;
|
2022-10-10 16:40:56 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (sig_size < SIG_OTS_SIG_OFFSET + MBEDTLS_LMOTS_TYPE_LEN) {
|
|
|
|
return MBEDTLS_ERR_LMS_VERIFY_FAILED;
|
2022-10-10 16:40:56 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMOTS_TYPE_LEN,
|
|
|
|
sig + SIG_OTS_SIG_OFFSET +
|
|
|
|
MBEDTLS_LMOTS_SIG_TYPE_OFFSET)
|
|
|
|
!= MBEDTLS_LMOTS_SHA256_N32_W8) {
|
|
|
|
return MBEDTLS_ERR_LMS_VERIFY_FAILED;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (sig_size < SIG_TYPE_OFFSET(ctx->params.otstype) + MBEDTLS_LMS_TYPE_LEN) {
|
|
|
|
return MBEDTLS_ERR_LMS_VERIFY_FAILED;
|
2022-10-10 16:40:56 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_lms_network_bytes_to_unsigned_int(MBEDTLS_LMS_TYPE_LEN,
|
|
|
|
sig + SIG_TYPE_OFFSET(ctx->params.otstype))
|
|
|
|
!= MBEDTLS_LMS_SHA256_M32_H10) {
|
|
|
|
return MBEDTLS_ERR_LMS_VERIFY_FAILED;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-27 11:59:16 +02:00
|
|
|
q_leaf_identifier = mbedtls_lms_network_bytes_to_unsigned_int(
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_LMOTS_Q_LEAF_ID_LEN, sig + SIG_Q_LEAF_ID_OFFSET);
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
|
|
|
|
return MBEDTLS_ERR_LMS_VERIFY_FAILED;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(ots_params.I_key_identifier,
|
|
|
|
ctx->params.I_key_identifier,
|
|
|
|
MBEDTLS_LMOTS_I_KEY_ID_LEN);
|
|
|
|
mbedtls_lms_unsigned_int_to_network_bytes(q_leaf_identifier,
|
2022-09-27 11:59:16 +02:00
|
|
|
MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
|
2023-01-11 14:50:10 +01:00
|
|
|
ots_params.q_leaf_identifier);
|
2022-09-01 10:56:52 +02:00
|
|
|
ots_params.type = ctx->params.otstype;
|
2022-08-31 16:55:00 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_lmots_calculate_public_key_candidate(&ots_params,
|
|
|
|
msg,
|
|
|
|
msg_size,
|
|
|
|
sig + SIG_OTS_SIG_OFFSET,
|
|
|
|
MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype),
|
|
|
|
Kc_candidate_ots_pub_key,
|
|
|
|
sizeof(Kc_candidate_ots_pub_key),
|
|
|
|
NULL);
|
|
|
|
if (ret != 0) {
|
|
|
|
return MBEDTLS_ERR_LMS_VERIFY_FAILED;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 12:52:17 +02:00
|
|
|
create_merkle_leaf_value(
|
2023-01-11 14:50:10 +01:00
|
|
|
&ctx->params,
|
|
|
|
Kc_candidate_ots_pub_key,
|
|
|
|
MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
|
|
|
|
Tc_candidate_root_node);
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-01 18:23:12 +02:00
|
|
|
curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) +
|
|
|
|
q_leaf_identifier;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
|
|
|
|
height++) {
|
2021-07-21 13:42:15 +02:00
|
|
|
parent_node_id = curr_node_id / 2;
|
|
|
|
|
|
|
|
/* Left/right node ordering matters for the hash */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (curr_node_id & 1) {
|
2022-09-27 12:30:51 +02:00
|
|
|
left_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
|
2022-09-01 17:06:35 +02:00
|
|
|
height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
|
2022-08-31 16:55:00 +02:00
|
|
|
right_node = Tc_candidate_root_node;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
2021-07-21 13:42:15 +02:00
|
|
|
left_node = Tc_candidate_root_node;
|
2022-09-27 12:30:51 +02:00
|
|
|
right_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) +
|
2022-09-01 17:06:35 +02:00
|
|
|
height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
create_merkle_internal_value(&ctx->params, left_node, right_node,
|
|
|
|
parent_node_id, Tc_candidate_root_node);
|
2021-07-21 13:42:15 +02:00
|
|
|
|
|
|
|
curr_node_id /= 2;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (memcmp(Tc_candidate_root_node, ctx->T_1_pub_key,
|
|
|
|
MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type))) {
|
|
|
|
return MBEDTLS_ERR_LMS_VERIFY_FAILED;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-10-07 11:35:56 +02:00
|
|
|
#if defined(MBEDTLS_LMS_PRIVATE)
|
2022-09-01 13:24:31 +02:00
|
|
|
|
2022-10-10 16:44:17 +02:00
|
|
|
/* Calculate a full Merkle tree based on a private key. This function
|
2022-09-02 18:46:15 +02:00
|
|
|
* implements RFC8554 section 5.3, and is used to generate a public key (as the
|
2022-10-10 16:44:17 +02:00
|
|
|
* public key is the root node of the Merkle tree).
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* ctx The LMS private context, containing a parameter
|
|
|
|
* set and private key material consisting of both
|
|
|
|
* public and private OTS.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* tree The output tree, which is 2^(H + 1) hash outputs.
|
|
|
|
* In the case of H=10 we have 2048 tree nodes (of
|
|
|
|
* which 1024 of them are leaf nodes). Note that
|
2022-10-10 16:44:17 +02:00
|
|
|
* because the Merkle tree root is 1-indexed, the 0
|
2022-09-23 10:04:04 +02:00
|
|
|
* index tree node is never used.
|
2022-09-02 18:46:15 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int calculate_merkle_tree(const mbedtls_lms_private_t *ctx,
|
|
|
|
unsigned char *tree)
|
2022-09-01 13:24:31 +02:00
|
|
|
{
|
|
|
|
unsigned int priv_key_idx;
|
|
|
|
unsigned int r_node_idx;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
|
|
|
/* First create the leaf nodes, in ascending order */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (priv_key_idx = 0;
|
2022-09-01 17:06:35 +02:00
|
|
|
priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type);
|
2023-01-11 14:50:10 +01:00
|
|
|
priv_key_idx++) {
|
2022-09-01 17:06:35 +02:00
|
|
|
r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx;
|
2022-09-01 13:24:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = create_merkle_leaf_value(&ctx->params,
|
|
|
|
ctx->ots_public_keys[priv_key_idx].public_key,
|
|
|
|
r_node_idx,
|
|
|
|
&tree[r_node_idx * MBEDTLS_LMS_M_NODE_BYTES(
|
|
|
|
ctx->params.type)]);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
2022-09-01 13:24:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then the internal nodes, in reverse order so that we can guarantee the
|
|
|
|
* parent has been created */
|
2023-01-11 14:50:10 +01:00
|
|
|
for (r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1;
|
2022-09-01 17:06:35 +02:00
|
|
|
r_node_idx > 0;
|
2023-01-11 14:50:10 +01:00
|
|
|
r_node_idx--) {
|
|
|
|
ret = create_merkle_internal_value(&ctx->params,
|
|
|
|
&tree[(r_node_idx * 2) *
|
|
|
|
MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
|
|
|
|
&tree[(r_node_idx * 2 + 1) *
|
|
|
|
MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)],
|
|
|
|
r_node_idx,
|
|
|
|
&tree[r_node_idx *
|
|
|
|
MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)]);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
2022-09-01 13:24:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2022-09-01 13:24:31 +02:00
|
|
|
}
|
|
|
|
|
2022-10-10 16:44:17 +02:00
|
|
|
/* Calculate a path from a leaf node of the Merkle tree to the root of the tree,
|
2022-09-02 18:46:15 +02:00
|
|
|
* and return the full path. This function implements RFC8554 section 5.4.1, as
|
2022-10-10 16:44:17 +02:00
|
|
|
* the Merkle path is the main component of an LMS signature.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* ctx The LMS private context, containing a parameter
|
|
|
|
* set and private key material consisting of both
|
|
|
|
* public and private OTS.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-09-23 10:04:04 +02:00
|
|
|
* leaf_node_id Which leaf node to calculate the path from.
|
2022-09-02 18:46:15 +02:00
|
|
|
*
|
2022-10-10 14:58:28 +02:00
|
|
|
* path The output path, which is H hash outputs.
|
2022-09-02 18:46:15 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int get_merkle_path(mbedtls_lms_private_t *ctx,
|
|
|
|
unsigned int leaf_node_id,
|
|
|
|
unsigned char *path)
|
2022-09-01 13:24:31 +02:00
|
|
|
{
|
2022-11-12 17:28:04 +01:00
|
|
|
const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type);
|
2022-09-01 13:24:31 +02:00
|
|
|
unsigned int curr_node_id = leaf_node_id;
|
|
|
|
unsigned int adjacent_node_id;
|
2022-11-12 17:28:04 +01:00
|
|
|
unsigned char *tree = NULL;
|
2022-09-01 13:24:31 +02:00
|
|
|
unsigned int height;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
tree = mbedtls_calloc(MERKLE_TREE_NODE_AM(ctx->params.type),
|
|
|
|
node_bytes);
|
|
|
|
if (tree == NULL) {
|
2022-11-12 17:28:04 +01:00
|
|
|
return MBEDTLS_ERR_LMS_ALLOC_FAILED;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = calculate_merkle_tree(ctx, tree);
|
|
|
|
if (ret != 0) {
|
2022-10-12 11:47:27 +02:00
|
|
|
goto exit;
|
2022-09-01 13:24:31 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type);
|
|
|
|
height++) {
|
2022-09-01 13:24:31 +02:00
|
|
|
adjacent_node_id = curr_node_id ^ 1;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(&path[height * node_bytes],
|
|
|
|
&tree[adjacent_node_id * node_bytes], node_bytes);
|
2022-09-01 13:24:31 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
curr_node_id >>= 1;
|
2022-09-01 13:24:31 +02:00
|
|
|
}
|
|
|
|
|
2022-10-12 11:47:27 +02:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(tree, node_bytes *
|
|
|
|
MERKLE_TREE_NODE_AM(ctx->params.type));
|
|
|
|
mbedtls_free(tree);
|
2022-10-12 11:47:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2022-09-01 13:24:31 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_lms_private_init(mbedtls_lms_private_t *ctx)
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(ctx, 0, sizeof(*ctx));
|
2022-08-31 16:55:00 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_lms_private_free(mbedtls_lms_private_t *ctx)
|
2022-08-31 16:55:00 +02:00
|
|
|
{
|
|
|
|
unsigned int idx;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx->have_private_key) {
|
|
|
|
if (ctx->ots_private_keys != NULL) {
|
|
|
|
for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
|
|
|
|
mbedtls_lmots_private_free(&ctx->ots_private_keys[idx]);
|
2022-10-13 15:11:49 +02:00
|
|
|
}
|
2022-08-31 16:55:00 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx->ots_public_keys != NULL) {
|
|
|
|
for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
|
|
|
|
mbedtls_lmots_public_free(&ctx->ots_public_keys[idx]);
|
2022-10-13 15:11:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(ctx->ots_private_keys);
|
|
|
|
mbedtls_free(ctx->ots_public_keys);
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(ctx, sizeof(*ctx));
|
2022-08-31 16:55:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_lms_generate_private_key(mbedtls_lms_private_t *ctx,
|
|
|
|
mbedtls_lms_algorithm_type_t type,
|
|
|
|
mbedtls_lmots_algorithm_type_t otstype,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng, const unsigned char *seed,
|
|
|
|
size_t seed_size)
|
2022-08-31 16:55:00 +02:00
|
|
|
{
|
|
|
|
unsigned int idx = 0;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (type != MBEDTLS_LMS_SHA256_M32_H10) {
|
|
|
|
return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (otstype != MBEDTLS_LMOTS_SHA256_N32_W8) {
|
|
|
|
return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx->have_private_key) {
|
|
|
|
return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
ctx->params.type = type;
|
|
|
|
ctx->params.otstype = otstype;
|
2022-10-13 15:11:49 +02:00
|
|
|
ctx->have_private_key = 1;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = f_rng(p_rng,
|
|
|
|
ctx->params.I_key_identifier,
|
|
|
|
MBEDTLS_LMOTS_I_KEY_ID_LEN);
|
|
|
|
if (ret != 0) {
|
2022-10-07 15:07:59 +02:00
|
|
|
goto exit;
|
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-10-12 16:22:48 +02:00
|
|
|
/* Requires a cast to size_t to avoid an implicit cast warning on certain
|
|
|
|
* platforms (particularly Windows) */
|
2023-01-11 14:50:10 +01:00
|
|
|
ctx->ots_private_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
|
|
|
|
sizeof(*ctx->ots_private_keys));
|
|
|
|
if (ctx->ots_private_keys == NULL) {
|
2022-08-31 16:55:00 +02:00
|
|
|
ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
|
|
|
|
goto exit;
|
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-10-12 16:22:48 +02:00
|
|
|
/* Requires a cast to size_t to avoid an implicit cast warning on certain
|
|
|
|
* platforms (particularly Windows) */
|
2023-01-11 14:50:10 +01:00
|
|
|
ctx->ots_public_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type),
|
|
|
|
sizeof(*ctx->ots_public_keys));
|
|
|
|
if (ctx->ots_public_keys == NULL) {
|
2022-08-31 16:55:00 +02:00
|
|
|
ret = MBEDTLS_ERR_LMS_ALLOC_FAILED;
|
|
|
|
goto exit;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
|
|
|
|
mbedtls_lmots_private_init(&ctx->ots_private_keys[idx]);
|
|
|
|
mbedtls_lmots_public_init(&ctx->ots_public_keys[idx]);
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) {
|
|
|
|
ret = mbedtls_lmots_generate_private_key(&ctx->ots_private_keys[idx],
|
|
|
|
otstype,
|
|
|
|
ctx->params.I_key_identifier,
|
|
|
|
idx, seed, seed_size);
|
|
|
|
if (ret != 0) {
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-08-31 16:55:00 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_lmots_calculate_public_key(&ctx->ots_public_keys[idx],
|
|
|
|
&ctx->ots_private_keys[idx]);
|
|
|
|
if (ret != 0) {
|
2022-08-31 16:55:00 +02:00
|
|
|
goto exit;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
ctx->q_next_usable_key = 0;
|
2022-08-31 16:55:00 +02:00
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
2022-10-07 14:27:54 +02:00
|
|
|
mbedtls_lms_private_free(ctx);
|
2022-08-31 16:55:00 +02:00
|
|
|
}
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_lms_calculate_public_key(mbedtls_lms_public_t *ctx,
|
|
|
|
const mbedtls_lms_private_t *priv_ctx)
|
2021-07-21 13:42:15 +02:00
|
|
|
{
|
2022-11-12 17:28:04 +01:00
|
|
|
const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(priv_ctx->params.type);
|
2021-07-21 13:42:15 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-11-12 17:28:04 +01:00
|
|
|
unsigned char *tree = NULL;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!priv_ctx->have_private_key) {
|
|
|
|
return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (priv_ctx->params.type
|
|
|
|
!= MBEDTLS_LMS_SHA256_M32_H10) {
|
|
|
|
return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (priv_ctx->params.otstype
|
|
|
|
!= MBEDTLS_LMOTS_SHA256_N32_W8) {
|
|
|
|
return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
tree = mbedtls_calloc(MERKLE_TREE_NODE_AM(priv_ctx->params.type),
|
|
|
|
node_bytes);
|
|
|
|
if (tree == NULL) {
|
2022-11-12 17:28:04 +01:00
|
|
|
return MBEDTLS_ERR_LMS_ALLOC_FAILED;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(&ctx->params, &priv_ctx->params,
|
|
|
|
sizeof(mbedtls_lmots_parameters_t));
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = calculate_merkle_tree(priv_ctx, tree);
|
|
|
|
if (ret != 0) {
|
2022-10-12 11:47:27 +02:00
|
|
|
goto exit;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Root node is always at position 1, due to 1-based indexing */
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(ctx->T_1_pub_key, &tree[node_bytes], node_bytes);
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
ctx->have_public_key = 1;
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2022-10-12 11:47:27 +02:00
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
exit:
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(tree, node_bytes *
|
|
|
|
MERKLE_TREE_NODE_AM(priv_ctx->params.type));
|
|
|
|
mbedtls_free(tree);
|
2022-10-12 11:47:27 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-08-31 16:55:00 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_lms_sign(mbedtls_lms_private_t *ctx,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng, const unsigned char *msg,
|
|
|
|
unsigned int msg_size, unsigned char *sig, size_t sig_size,
|
|
|
|
size_t *sig_len)
|
2022-08-31 16:55:00 +02:00
|
|
|
{
|
|
|
|
uint32_t q_leaf_identifier;
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (!ctx->have_private_key) {
|
|
|
|
return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) {
|
|
|
|
return MBEDTLS_ERR_LMS_BUFFER_TOO_SMALL;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10) {
|
|
|
|
return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx->params.otstype
|
|
|
|
!= MBEDTLS_LMOTS_SHA256_N32_W8) {
|
|
|
|
return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) {
|
|
|
|
return MBEDTLS_ERR_LMS_OUT_OF_PRIVATE_KEYS;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-01 10:56:52 +02:00
|
|
|
q_leaf_identifier = ctx->q_next_usable_key;
|
2022-08-31 16:55:00 +02:00
|
|
|
/* This new value must _always_ be written back to the disk before the
|
|
|
|
* signature is returned.
|
|
|
|
*/
|
2022-09-01 10:56:52 +02:00
|
|
|
ctx->q_next_usable_key += 1;
|
2022-08-31 16:55:00 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)
|
|
|
|
< SIG_OTS_SIG_OFFSET) {
|
|
|
|
return MBEDTLS_ERR_LMS_BAD_INPUT_DATA;
|
2022-10-10 12:23:07 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_lmots_sign(&ctx->ots_private_keys[q_leaf_identifier],
|
|
|
|
f_rng,
|
|
|
|
p_rng,
|
|
|
|
msg,
|
|
|
|
msg_size,
|
|
|
|
sig + SIG_OTS_SIG_OFFSET,
|
|
|
|
MBEDTLS_LMS_SIG_LEN(ctx->params.type,
|
|
|
|
ctx->params.otstype) - SIG_OTS_SIG_OFFSET,
|
|
|
|
NULL);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_lms_unsigned_int_to_network_bytes(ctx->params.type,
|
|
|
|
MBEDTLS_LMS_TYPE_LEN,
|
|
|
|
sig + SIG_TYPE_OFFSET(ctx->params.otstype));
|
|
|
|
mbedtls_lms_unsigned_int_to_network_bytes(q_leaf_identifier,
|
|
|
|
MBEDTLS_LMOTS_Q_LEAF_ID_LEN,
|
|
|
|
sig + SIG_Q_LEAF_ID_OFFSET);
|
2021-07-21 13:42:15 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = get_merkle_path(ctx,
|
|
|
|
MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier,
|
|
|
|
sig + SIG_PATH_OFFSET(ctx->params.otstype));
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (sig_len != NULL) {
|
2022-09-01 17:06:35 +02:00
|
|
|
*sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype);
|
2022-08-31 16:55:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2021-07-21 13:42:15 +02:00
|
|
|
}
|
|
|
|
|
2022-10-07 11:35:56 +02:00
|
|
|
#endif /* defined(MBEDTLS_LMS_PRIVATE) */
|
|
|
|
#endif /* defined(MBEDTLS_LMS_C) */
|