Lines Matching refs:ctx

225 void mbedtls_lms_public_init(mbedtls_lms_public_t *ctx)  in mbedtls_lms_public_init()  argument
227 memset(ctx, 0, sizeof(*ctx)); in mbedtls_lms_public_init()
230 void mbedtls_lms_public_free(mbedtls_lms_public_t *ctx) in mbedtls_lms_public_free() argument
232 if (ctx == NULL) { in mbedtls_lms_public_free()
236 mbedtls_platform_zeroize(ctx, sizeof(*ctx)); in mbedtls_lms_public_free()
239 int mbedtls_lms_import_public_key(mbedtls_lms_public_t *ctx, in mbedtls_lms_import_public_key() argument
249 ctx->params.type = type; in mbedtls_lms_import_public_key()
251 if (key_size != MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) { in mbedtls_lms_import_public_key()
260 ctx->params.otstype = otstype; in mbedtls_lms_import_public_key()
262 memcpy(ctx->params.I_key_identifier, in mbedtls_lms_import_public_key()
265 memcpy(ctx->T_1_pub_key, key + PUBLIC_KEY_ROOT_NODE_OFFSET, in mbedtls_lms_import_public_key()
266 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)); in mbedtls_lms_import_public_key()
268 ctx->have_public_key = 1; in mbedtls_lms_import_public_key()
273 int mbedtls_lms_export_public_key(const mbedtls_lms_public_t *ctx, in mbedtls_lms_export_public_key() argument
277 if (key_size < MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type)) { in mbedtls_lms_export_public_key()
281 if (!ctx->have_public_key) { in mbedtls_lms_export_public_key()
285 MBEDTLS_PUT_UINT32_BE(ctx->params.type, key, PUBLIC_KEY_TYPE_OFFSET); in mbedtls_lms_export_public_key()
286 MBEDTLS_PUT_UINT32_BE(ctx->params.otstype, key, PUBLIC_KEY_OTSTYPE_OFFSET); in mbedtls_lms_export_public_key()
288 ctx->params.I_key_identifier, in mbedtls_lms_export_public_key()
291 ctx->T_1_pub_key, in mbedtls_lms_export_public_key()
292 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)); in mbedtls_lms_export_public_key()
295 *key_len = MBEDTLS_LMS_PUBLIC_KEY_LEN(ctx->params.type); in mbedtls_lms_export_public_key()
301 int mbedtls_lms_verify(const mbedtls_lms_public_t *ctx, in mbedtls_lms_verify() argument
316 if (!ctx->have_public_key) { in mbedtls_lms_verify()
320 if (ctx->params.type in mbedtls_lms_verify()
325 if (ctx->params.otstype in mbedtls_lms_verify()
330 if (sig_size != MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) { in mbedtls_lms_verify()
343 if (sig_size < SIG_TYPE_OFFSET(ctx->params.otstype) + MBEDTLS_LMS_TYPE_LEN) { in mbedtls_lms_verify()
347 if (MBEDTLS_GET_UINT32_BE(sig, SIG_TYPE_OFFSET(ctx->params.otstype)) in mbedtls_lms_verify()
355 if (q_leaf_identifier >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) { in mbedtls_lms_verify()
360 ctx->params.I_key_identifier, in mbedtls_lms_verify()
363 ots_params.type = ctx->params.otstype; in mbedtls_lms_verify()
369 MBEDTLS_LMOTS_SIG_LEN(ctx->params.otstype), in mbedtls_lms_verify()
378 &ctx->params, in mbedtls_lms_verify()
380 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier, in mbedtls_lms_verify()
383 curr_node_id = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + in mbedtls_lms_verify()
386 for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type); in mbedtls_lms_verify()
392 left_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) + in mbedtls_lms_verify()
393 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type); in mbedtls_lms_verify()
397 right_node = sig + SIG_PATH_OFFSET(ctx->params.otstype) + in mbedtls_lms_verify()
398 height * MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type); in mbedtls_lms_verify()
401 create_merkle_internal_value(&ctx->params, left_node, right_node, in mbedtls_lms_verify()
407 if (memcmp(Tc_candidate_root_node, ctx->T_1_pub_key, in mbedtls_lms_verify()
408 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type))) { in mbedtls_lms_verify()
431 static int calculate_merkle_tree(const mbedtls_lms_private_t *ctx, in calculate_merkle_tree() argument
440 priv_key_idx < MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type); in calculate_merkle_tree()
442 r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + priv_key_idx; in calculate_merkle_tree()
444 ret = create_merkle_leaf_value(&ctx->params, in calculate_merkle_tree()
445 ctx->ots_public_keys[priv_key_idx].public_key, in calculate_merkle_tree()
448 ctx->params.type)]); in calculate_merkle_tree()
456 for (r_node_idx = MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) - 1; in calculate_merkle_tree()
459 ret = create_merkle_internal_value(&ctx->params, in calculate_merkle_tree()
461 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)], in calculate_merkle_tree()
463 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)], in calculate_merkle_tree()
466 MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type)]); in calculate_merkle_tree()
487 static int get_merkle_path(mbedtls_lms_private_t *ctx, in get_merkle_path() argument
491 const size_t node_bytes = MBEDTLS_LMS_M_NODE_BYTES(ctx->params.type); in get_merkle_path()
498 tree = mbedtls_calloc((size_t) MERKLE_TREE_NODE_AM(ctx->params.type), in get_merkle_path()
504 ret = calculate_merkle_tree(ctx, tree); in get_merkle_path()
509 for (height = 0; height < MBEDTLS_LMS_H_TREE_HEIGHT(ctx->params.type); in get_merkle_path()
523 (size_t) MERKLE_TREE_NODE_AM(ctx->params.type)); in get_merkle_path()
528 void mbedtls_lms_private_init(mbedtls_lms_private_t *ctx) in mbedtls_lms_private_init() argument
530 memset(ctx, 0, sizeof(*ctx)); in mbedtls_lms_private_init()
533 void mbedtls_lms_private_free(mbedtls_lms_private_t *ctx) in mbedtls_lms_private_free() argument
535 if (ctx == NULL) { in mbedtls_lms_private_free()
541 if (ctx->have_private_key) { in mbedtls_lms_private_free()
542 if (ctx->ots_private_keys != NULL) { in mbedtls_lms_private_free()
543 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) { in mbedtls_lms_private_free()
544 mbedtls_lmots_private_free(&ctx->ots_private_keys[idx]); in mbedtls_lms_private_free()
548 if (ctx->ots_public_keys != NULL) { in mbedtls_lms_private_free()
549 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) { in mbedtls_lms_private_free()
550 mbedtls_lmots_public_free(&ctx->ots_public_keys[idx]); in mbedtls_lms_private_free()
554 mbedtls_free(ctx->ots_private_keys); in mbedtls_lms_private_free()
555 mbedtls_free(ctx->ots_public_keys); in mbedtls_lms_private_free()
558 mbedtls_platform_zeroize(ctx, sizeof(*ctx)); in mbedtls_lms_private_free()
562 int mbedtls_lms_generate_private_key(mbedtls_lms_private_t *ctx, in mbedtls_lms_generate_private_key() argument
580 if (ctx->have_private_key) { in mbedtls_lms_generate_private_key()
584 ctx->params.type = type; in mbedtls_lms_generate_private_key()
585 ctx->params.otstype = otstype; in mbedtls_lms_generate_private_key()
586 ctx->have_private_key = 1; in mbedtls_lms_generate_private_key()
589 ctx->params.I_key_identifier, in mbedtls_lms_generate_private_key()
597 ctx->ots_private_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type), in mbedtls_lms_generate_private_key()
598 sizeof(*ctx->ots_private_keys)); in mbedtls_lms_generate_private_key()
599 if (ctx->ots_private_keys == NULL) { in mbedtls_lms_generate_private_key()
606 ctx->ots_public_keys = mbedtls_calloc((size_t) MERKLE_TREE_LEAF_NODE_AM(ctx->params.type), in mbedtls_lms_generate_private_key()
607 sizeof(*ctx->ots_public_keys)); in mbedtls_lms_generate_private_key()
608 if (ctx->ots_public_keys == NULL) { in mbedtls_lms_generate_private_key()
613 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) { in mbedtls_lms_generate_private_key()
614 mbedtls_lmots_private_init(&ctx->ots_private_keys[idx]); in mbedtls_lms_generate_private_key()
615 mbedtls_lmots_public_init(&ctx->ots_public_keys[idx]); in mbedtls_lms_generate_private_key()
619 for (idx = 0; idx < MERKLE_TREE_LEAF_NODE_AM(ctx->params.type); idx++) { in mbedtls_lms_generate_private_key()
620 ret = mbedtls_lmots_generate_private_key(&ctx->ots_private_keys[idx], in mbedtls_lms_generate_private_key()
622 ctx->params.I_key_identifier, in mbedtls_lms_generate_private_key()
628 ret = mbedtls_lmots_calculate_public_key(&ctx->ots_public_keys[idx], in mbedtls_lms_generate_private_key()
629 &ctx->ots_private_keys[idx]); in mbedtls_lms_generate_private_key()
635 ctx->q_next_usable_key = 0; in mbedtls_lms_generate_private_key()
639 mbedtls_lms_private_free(ctx); in mbedtls_lms_generate_private_key()
645 int mbedtls_lms_calculate_public_key(mbedtls_lms_public_t *ctx, in mbedtls_lms_calculate_public_key() argument
672 memcpy(&ctx->params, &priv_ctx->params, in mbedtls_lms_calculate_public_key()
681 memcpy(ctx->T_1_pub_key, &tree[node_bytes], node_bytes); in mbedtls_lms_calculate_public_key()
683 ctx->have_public_key = 1; in mbedtls_lms_calculate_public_key()
695 int mbedtls_lms_sign(mbedtls_lms_private_t *ctx, in mbedtls_lms_sign() argument
704 if (!ctx->have_private_key) { in mbedtls_lms_sign()
708 if (sig_size < MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype)) { in mbedtls_lms_sign()
712 if (ctx->params.type != MBEDTLS_LMS_SHA256_M32_H10) { in mbedtls_lms_sign()
716 if (ctx->params.otstype in mbedtls_lms_sign()
721 if (ctx->q_next_usable_key >= MERKLE_TREE_LEAF_NODE_AM(ctx->params.type)) { in mbedtls_lms_sign()
726 q_leaf_identifier = ctx->q_next_usable_key; in mbedtls_lms_sign()
730 ctx->q_next_usable_key += 1; in mbedtls_lms_sign()
732 if (MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype) in mbedtls_lms_sign()
737 ret = mbedtls_lmots_sign(&ctx->ots_private_keys[q_leaf_identifier], in mbedtls_lms_sign()
743 MBEDTLS_LMS_SIG_LEN(ctx->params.type, in mbedtls_lms_sign()
744 ctx->params.otstype) - SIG_OTS_SIG_OFFSET, in mbedtls_lms_sign()
750 MBEDTLS_PUT_UINT32_BE(ctx->params.type, sig, SIG_TYPE_OFFSET(ctx->params.otstype)); in mbedtls_lms_sign()
753 ret = get_merkle_path(ctx, in mbedtls_lms_sign()
754 MERKLE_TREE_INTERNAL_NODE_AM(ctx->params.type) + q_leaf_identifier, in mbedtls_lms_sign()
755 sig + SIG_PATH_OFFSET(ctx->params.otstype)); in mbedtls_lms_sign()
761 *sig_len = MBEDTLS_LMS_SIG_LEN(ctx->params.type, ctx->params.otstype); in mbedtls_lms_sign()