Lines Matching refs:ctx
228 void mbedtls_lms_public_init(mbedtls_lms_public_t *ctx) in mbedtls_lms_public_init() argument
230 memset(ctx, 0, sizeof(*ctx)); in mbedtls_lms_public_init()
233 void mbedtls_lms_public_free(mbedtls_lms_public_t *ctx) in mbedtls_lms_public_free() argument
235 if (ctx == NULL) { in mbedtls_lms_public_free()
239 mbedtls_platform_zeroize(ctx, sizeof(*ctx)); in mbedtls_lms_public_free()
242 int mbedtls_lms_import_public_key(mbedtls_lms_public_t *ctx, in mbedtls_lms_import_public_key() argument
253 ctx->params.type = (mbedtls_lms_algorithm_type_t) type; in mbedtls_lms_import_public_key()
255 if (key_size != MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) { in mbedtls_lms_import_public_key()
263 ctx->params.otstype = (mbedtls_lmots_algorithm_type_t) otstype; in mbedtls_lms_import_public_key()
265 memcpy(ctx->params.I_key_identifier, in mbedtls_lms_import_public_key()
268 memcpy(ctx->T_1_pub_key, key + PUBLIC_KEY_ROOT_NODE_OFFSET, in mbedtls_lms_import_public_key()
269 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)); in mbedtls_lms_import_public_key()
271 ctx->have_public_key = 1; in mbedtls_lms_import_public_key()
276 int mbedtls_lms_export_public_key(const mbedtls_lms_public_t *ctx, in mbedtls_lms_export_public_key() argument
280 if (key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) { in mbedtls_lms_export_public_key()
284 if (!ctx->have_public_key) { in mbedtls_lms_export_public_key()
288 MBEDTLS_PUT_UINT32_BE(ctx->params.type, key, PUBLIC_KEY_TYPE_OFFSET); in mbedtls_lms_export_public_key()
289 MBEDTLS_PUT_UINT32_BE(ctx->params.otstype, key, PUBLIC_KEY_OTSTYPE_OFFSET); in mbedtls_lms_export_public_key()
291 ctx->params.I_key_identifier, in mbedtls_lms_export_public_key()
294 ctx->T_1_pub_key, in mbedtls_lms_export_public_key()
295 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)); in mbedtls_lms_export_public_key()
298 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type); in mbedtls_lms_export_public_key()
304 int mbedtls_lms_verify(const mbedtls_lms_public_t *ctx, in mbedtls_lms_verify() argument
319 if (!ctx->have_public_key) { in mbedtls_lms_verify()
323 if (ctx->params.type in mbedtls_lms_verify()
328 if (ctx->params.otstype in mbedtls_lms_verify()
333 if (sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) { in mbedtls_lms_verify()
346 if (sig_size < SIG_TYPE_OFFSET(ctx->params.otstype) + MBEDTLS_LMS_TYPE_LEN) { in mbedtls_lms_verify()
350 if (MBEDTLS_GET_UINT32_BE(sig, SIG_TYPE_OFFSET(ctx->params.otstype)) in mbedtls_lms_verify()
358 if (q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) { in mbedtls_lms_verify()
363 ctx->params.I_key_identifier, in mbedtls_lms_verify()
366 ots_params.type = ctx->params.otstype; in mbedtls_lms_verify()
372 MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype), in mbedtls_lms_verify()
381 &ctx->params, in mbedtls_lms_verify()
383 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier, in mbedtls_lms_verify()
390 curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + in mbedtls_lms_verify()
393 for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type); in mbedtls_lms_verify()
399 left_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) + in mbedtls_lms_verify()
400 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type); in mbedtls_lms_verify()
404 right_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) + in mbedtls_lms_verify()
405 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type); in mbedtls_lms_verify()
408 ret = create_merkle_internal_value(&ctx->params, left_node, right_node, in mbedtls_lms_verify()
416 if (memcmp(Tc_candidate_root_node, ctx->T_1_pub_key, in mbedtls_lms_verify()
417 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type))) { in mbedtls_lms_verify()
440 static int calculate_merkle_tree(const mbedtls_lms_private_t *ctx, in calculate_merkle_tree() argument
449 priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type); in calculate_merkle_tree()
451 r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx; in calculate_merkle_tree()
453 ret = create_merkle_leaf_value(&ctx->params, in calculate_merkle_tree()
454 ctx->ots_public_keys[priv_key_idx].public_key, in calculate_merkle_tree()
457 ctx->params.type)]); in calculate_merkle_tree()
465 for (r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1; in calculate_merkle_tree()
468 ret = create_merkle_internal_value(&ctx->params, in calculate_merkle_tree()
470 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)], in calculate_merkle_tree()
472 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)], in calculate_merkle_tree()
475 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)]); in calculate_merkle_tree()
496 static int get_merkle_path(mbedtls_lms_private_t *ctx, in get_merkle_path() argument
500 const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type); in get_merkle_path()
507 tree = mbedtls_calloc((size_t) MERKLE_TREE_NODE_AM(ctx->params.type), in get_merkle_path()
513 ret = calculate_merkle_tree(ctx, tree); in get_merkle_path()
518 for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type); in get_merkle_path()
532 (size_t) MERKLE_TREE_NODE_AM(ctx->params.type)); in get_merkle_path()
537 void mbedtls_lms_private_init(mbedtls_lms_private_t *ctx) in mbedtls_lms_private_init() argument
539 memset(ctx, 0, sizeof(*ctx)); in mbedtls_lms_private_init()
542 void mbedtls_lms_private_free(mbedtls_lms_private_t *ctx) in mbedtls_lms_private_free() argument
544 if (ctx == NULL) { in mbedtls_lms_private_free()
550 if (ctx->have_private_key) { in mbedtls_lms_private_free()
551 if (ctx->ots_private_keys != NULL) { in mbedtls_lms_private_free()
552 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) { in mbedtls_lms_private_free()
553 mbedtls_lmots_private_free(&ctx->ots_private_keys[idx]); in mbedtls_lms_private_free()
557 if (ctx->ots_public_keys != NULL) { in mbedtls_lms_private_free()
558 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) { in mbedtls_lms_private_free()
559 mbedtls_lmots_public_free(&ctx->ots_public_keys[idx]); in mbedtls_lms_private_free()
563 mbedtls_free(ctx->ots_private_keys); in mbedtls_lms_private_free()
564 mbedtls_free(ctx->ots_public_keys); in mbedtls_lms_private_free()
567 mbedtls_platform_zeroize(ctx, sizeof(*ctx)); in mbedtls_lms_private_free()
571 int mbedtls_lms_generate_private_key(mbedtls_lms_private_t *ctx, in mbedtls_lms_generate_private_key() argument
589 if (ctx->have_private_key) { in mbedtls_lms_generate_private_key()
593 ctx->params.type = type; in mbedtls_lms_generate_private_key()
594 ctx->params.otstype = otstype; in mbedtls_lms_generate_private_key()
595 ctx->have_private_key = 1; in mbedtls_lms_generate_private_key()
598 ctx->params.I_key_identifier, in mbedtls_lms_generate_private_key()
606 ctx->ots_private_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type), in mbedtls_lms_generate_private_key()
607 sizeof(*ctx->ots_private_keys)); in mbedtls_lms_generate_private_key()
608 if (ctx->ots_private_keys == NULL) { in mbedtls_lms_generate_private_key()
615 ctx->ots_public_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type), in mbedtls_lms_generate_private_key()
616 sizeof(*ctx->ots_public_keys)); in mbedtls_lms_generate_private_key()
617 if (ctx->ots_public_keys == NULL) { in mbedtls_lms_generate_private_key()
622 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) { in mbedtls_lms_generate_private_key()
623 mbedtls_lmots_private_init(&ctx->ots_private_keys[idx]); in mbedtls_lms_generate_private_key()
624 mbedtls_lmots_public_init(&ctx->ots_public_keys[idx]); in mbedtls_lms_generate_private_key()
628 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) { in mbedtls_lms_generate_private_key()
629 ret = mbedtls_lmots_generate_private_key(&ctx->ots_private_keys[idx], in mbedtls_lms_generate_private_key()
631 ctx->params.I_key_identifier, in mbedtls_lms_generate_private_key()
637 ret = mbedtls_lmots_calculate_public_key(&ctx->ots_public_keys[idx], in mbedtls_lms_generate_private_key()
638 &ctx->ots_private_keys[idx]); in mbedtls_lms_generate_private_key()
644 ctx->q_next_usable_key = 0; in mbedtls_lms_generate_private_key()
648 mbedtls_lms_private_free(ctx); in mbedtls_lms_generate_private_key()
654 int mbedtls_lms_calculate_public_key(mbedtls_lms_public_t *ctx, in mbedtls_lms_calculate_public_key() argument
681 memcpy(&ctx->params, &priv_ctx->params, in mbedtls_lms_calculate_public_key()
690 memcpy(ctx->T_1_pub_key, &tree[node_bytes], node_bytes); in mbedtls_lms_calculate_public_key()
692 ctx->have_public_key = 1; in mbedtls_lms_calculate_public_key()
704 int mbedtls_lms_sign(mbedtls_lms_private_t *ctx, in mbedtls_lms_sign() argument
713 if (!ctx->have_private_key) { in mbedtls_lms_sign()
717 if (sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) { in mbedtls_lms_sign()
721 if (ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10) { in mbedtls_lms_sign()
725 if (ctx->params.otstype in mbedtls_lms_sign()
730 if (ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) { in mbedtls_lms_sign()
735 q_leaf_identifier = ctx->q_next_usable_key; in mbedtls_lms_sign()
739 ctx->q_next_usable_key += 1; in mbedtls_lms_sign()
741 if (MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) in mbedtls_lms_sign()
746 ret = mbedtls_lmots_sign(&ctx->ots_private_keys[q_leaf_identifier], in mbedtls_lms_sign()
752 MBEDTLS_LMS_SIG_LEN(ctx->params.type, in mbedtls_lms_sign()
753 ctx->params.otstype) - SIG_OTS_SIG_OFFSET, in mbedtls_lms_sign()
759 MBEDTLS_PUT_UINT32_BE(ctx->params.type, sig, SIG_TYPE_OFFSET(ctx->params.otstype)); in mbedtls_lms_sign()
762 ret = get_merkle_path(ctx, in mbedtls_lms_sign()
763 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier, in mbedtls_lms_sign()
764 sig + SIG_PATH_OFFSET(ctx->params.otstype)); in mbedtls_lms_sign()
770 *sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype); in mbedtls_lms_sign()