Example #1
0
/**
 * Invokes an UPDATE message containing an anchor element as a hook to
 * next hash structure to be used when the active one depletes
 *
 * @param   entry the sadb entry for the outbound direction
 * @param   anchors the anchor elements to be sent
 * @param   hash_item_length length of the respective hash item
 * @param   soft_update indicates if HHL-based updates should be used
 * @param   anchor_offset the offset of the anchor element in the link tree
 * @param   link_trees the link trees for the anchor elements, in case of HHL
 * @return  0 on success, -1 on error
 */
int send_trigger_update_to_hipd(const struct hip_sa_entry *entry,
                                const unsigned char *anchors[MAX_NUM_PARALLEL_HCHAINS],
                                const int hash_item_length,
                                const int soft_update,
                                const int *anchor_offset,
                                struct hash_tree *link_trees[MAX_NUM_PARALLEL_HCHAINS])
{
    int                  err           = 0;
    int                  i             = 0;
    struct hip_common   *msg           = NULL;
    int                  hash_length   = 0;
    struct hash_chain   *hchain        = NULL;
    struct hash_tree    *htree         = NULL;
    struct hash_tree    *link_tree     = NULL;
    int                  secret_length = 0;
    int                  branch_length = 0;
    int                  root_length   = 0;
    const unsigned char *secret        = NULL;
    unsigned char       *branch_nodes  = NULL;
    const unsigned char *root          = NULL;

    HIP_ASSERT(entry != NULL);

    HIP_IFEL((hash_length = esp_prot_get_hash_length(entry->esp_prot_transform)) <= 0,
             -1, "error or tried to resolve UNUSED transform\n");

    HIP_IFEL(!(msg = malloc(HIP_MAX_PACKET)), -1,
             "failed to allocate memory\n");

    hip_msg_init(msg);

    HIP_IFEL(hip_build_user_hdr(msg, HIP_MSG_TRIGGER_UPDATE, 0), -1,
             "build hdr failed\n");

    HIP_DEBUG_HIT("src_hit", &entry->inner_src_addr);
    HIP_IFEL(hip_build_param_contents(msg, &entry->inner_src_addr,
                                      HIP_PARAM_HIT, sizeof(struct in6_addr)),
             -1, "build param contents failed\n");

    HIP_DEBUG_HIT("dst_hit", &entry->inner_dst_addr);
    HIP_IFEL(hip_build_param_contents(msg, &entry->inner_dst_addr,
                                      HIP_PARAM_HIT, sizeof(struct in6_addr)),
             -1, "build param contents failed\n");

    HIP_DEBUG("esp_prot_transform: %u\n", entry->esp_prot_transform);
    HIP_IFEL(hip_build_param_contents(msg, &entry->esp_prot_transform,
                                      HIP_PARAM_ESP_PROT_TFM, sizeof(uint8_t)),
             -1, "build param contents failed\n");

    // also send the hchain/htree length for all update items
    HIP_IFEL(hip_build_param_contents(msg, &hash_item_length, HIP_PARAM_INT,
                                      sizeof(int)), -1, "build param contents failed\n");
    HIP_DEBUG("added hash_item_length: %i\n", hash_item_length);

    HIP_DEBUG("num_parallel_hchains: %u\n", num_parallel_hchains);
    HIP_IFEL(hip_build_param_contents(msg, &num_parallel_hchains,
                                      HIP_PARAM_INT, sizeof(long)), -1,
             "build param contents failed\n");

    // add update anchors
    for (i = 0; i < num_parallel_hchains; i++) {
        HIP_HEXDUMP("anchor: ", anchors[i], hash_length);
        HIP_IFEL(hip_build_param_contents(msg, anchors[i],
                                          HIP_PARAM_HCHAIN_ANCHOR,
                                          hash_length), -1,
                 "build param contents failed\n");
    }

    // now transmit root for each next hash item for tree-based updates, if available
    for (i = 0; i < num_parallel_hchains; i++) {
        if (entry->esp_prot_transform == ESP_PROT_TFM_TREE) {
            htree     = entry->next_hash_items[i];
            link_tree = htree->link_tree;
        } else {
            hchain    = entry->next_hash_items[i];
            link_tree = hchain->link_tree;
        }

        if (link_tree) {
            /* if the next_hchain has got a link_tree, we need its root for
             * the verification of the next_hchain's elements */
            root = htree_get_root(link_tree, &root_length);
        }

        // only transmit root length once
        if (i == 0) {
            HIP_DEBUG("root_length: %i\n", root_length);
            HIP_IFEL(hip_build_param_contents(msg, &root_length,
                                              HIP_PARAM_INT,
                                              sizeof(int)), -1,
                     "build param contents failed\n");
        }

        if (root) {
            HIP_HEXDUMP("root: ", root, root_length);
            HIP_IFEL(hip_build_param_contents(msg, root,
                                              HIP_PARAM_ROOT, root_length), -1,
                     "build param contents failed\n");
        }
    }

    HIP_DEBUG("soft_update: %i\n", soft_update);
    HIP_IFEL(hip_build_param_contents(msg, &soft_update, HIP_PARAM_INT,
                                      sizeof(int)), -1,
             "build param contents failed\n");

    if (soft_update) {
        for (i = 0; i < num_parallel_hchains; i++) {
            secret = htree_get_secret(link_trees[i],
                                      anchor_offset[i], &secret_length);
            HIP_IFEL(!(branch_nodes = htree_get_branch(link_trees[i],
                                                       anchor_offset[i], NULL,
                                                       &branch_length)), -1,
                     "failed to get branch nodes\n");

            HIP_DEBUG("anchor_offset: %i\n", anchor_offset[i]);
            HIP_IFEL(hip_build_param_contents(msg, &anchor_offset[i],
                                              HIP_PARAM_INT,
                                              sizeof(int)), -1,
                     "build param contents failed\n");

            HIP_DEBUG("secret_length: %i\n", secret_length);
            HIP_IFEL(hip_build_param_contents(msg, &secret_length,
                                              HIP_PARAM_INT,
                                              sizeof(int)), -1,
                     "build param contents failed\n");

            HIP_DEBUG("branch_length: %i\n", branch_length);
            HIP_IFEL(hip_build_param_contents(msg, &branch_length,
                                              HIP_PARAM_INT,
                                              sizeof(int)), -1,
                     "build param contents failed\n");

            HIP_HEXDUMP("secret: ", secret, secret_length);
            HIP_IFEL(hip_build_param_contents(msg, secret,
                                              HIP_PARAM_SECRET,
                                              secret_length), -1,
                     "build param contents failed\n");

            HIP_HEXDUMP("branch_nodes: ", branch_nodes, branch_length);
            HIP_IFEL(hip_build_param_contents(msg, branch_nodes,
                                              HIP_PARAM_BRANCH_NODES,
                                              branch_length), -1,
                     "build param contents failed\n");
        }
    }

    HIP_DUMP_MSG(msg);

    /* send msg to hipd and receive corresponding reply */
    HIP_IFEL(hip_send_recv_daemon_info(msg, 1, hip_fw_sock), -1,
             "send_recv msg failed\n");

    /* check error value */
    HIP_IFEL(hip_get_msg_err(msg), -1, "hipd returned error message!\n");

    HIP_DEBUG("send_recv msg succeeded\n");

out_err:
    free(msg);
    free(branch_nodes);
    return err;
}
Example #2
0
int main(int argc, char ** argv)
{
	int i;
	char c;
	int err = 0;
	struct timeval start_time;
	struct timeval stop_time;
	hash_chain_t *hchain = NULL;
	hash_tree_t *htree = NULL;
	statistics_data_t creation_stats;
	statistics_data_t verify_stats;
	uint64_t timediff = 0;
	uint32_t num_items = 0;
	double min = 0.0, max = 0.0, avg = 0.0;
	double std_dev = 0.0;
	unsigned char *branch_nodes = NULL;
	int branch_length = 0;
	unsigned char *secret = NULL;
	int secret_length = 0;
	hash_chain_t *hchains[8];
	unsigned char *data = NULL;
	int data_length = 0;
	unsigned char *root = NULL;
	int root_length = 0;

	hash_function = NULL;


	while ((c=getopt(argc, argv, "ctsml:h:v:n:")) != -1)
	{
		switch (c)
		{
			case 'c':
				test_hc = 1;
				break;
			case 't':
				test_ht = 1;
				break;
			case 's':
				hash_function = hash_functions[0];
				break;
			case 'm':
				hash_function = hash_functions[1];
				break;
			case 'l':
				hchain_length = atoi(optarg);
				break;
			case 'h':
				hash_length = atoi(optarg);
				break;
			case 'v':
				verify_length = atoi(optarg);
				break;
			case 'n':
				count = atoi(optarg);
				break;
			case ':':
				printf("Missing argument %c\n", optopt);
				print_usage();
				exit(1);
			case '?':
				printf("Unknown option %c\n", optopt);
				print_usage();
				exit(1);
		}
	}

	if (hash_function == NULL)
	{
		printf("no hash function selected!\n");
		print_usage();
		exit(1);
	}

	hip_set_logdebug(LOGDEBUG_NONE);

	memset(&creation_stats, 0, sizeof(statistics_data_t));
	memset(&verify_stats, 0, sizeof(statistics_data_t));

	print_timeres();

	if (test_hc)
	{
		printf(	"-------------------------------\n"
			"Hash chain performance test\n"
			"-------------------------------\n\n");

		printf("Creating %d hash chains of length %d with element length %d\n",
				count, hchain_length, hash_length);

		for(i = 0; i < count; i++)
		{
			gettimeofday(&start_time, NULL);
			if (hchain = hchain_create(hash_function, hash_length, hchain_length, 0,
					NULL))
			{
				gettimeofday(&stop_time, NULL);
				timediff = calc_timeval_diff(&start_time, &stop_time);
				add_statistics_item(&creation_stats, timediff);
				hchain_free(hchain);
			} else
			{
				printf("ERROR creating hchain!\n");
				exit(1);
			}
		}

		calc_statistics(&creation_stats, &num_items, &min, &max, &avg, &std_dev,
				STATS_IN_MSECS);
		printf("creation statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n",
					num_items, min, max, avg, std_dev);

		printf("\n");

		printf("Verifying %d hash chains of length %d with element length %d\n",
				count, verify_length, hash_length);

		for(i = 0; i < count; i++)
		{
			if (!(hchain = hchain_create(hash_function, hash_length, verify_length, 0,
					NULL)))
			{
				printf("ERROR creating hchain!");
				exit(1);
			}

			gettimeofday(&start_time, NULL);
			if(hchain_verify(hchain_get_seed(hchain), hchain_get_anchor(hchain),
					hash_function, hash_length, verify_length, NULL, 0))
			{
				gettimeofday(&stop_time, NULL);
				timediff = calc_timeval_diff(&start_time, &stop_time);
				add_statistics_item(&verify_stats, timediff);
				hchain_free(hchain);
			} else
			{
				printf("ERROR verifying hchain!\n");
				exit(1);
			}
		}

		calc_statistics(&verify_stats, &num_items, &min, &max, &avg, &std_dev,
				STATS_IN_MSECS);
		printf("verification statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n",
					num_items, min, max, avg, std_dev);
	}

	if (test_ht)
	{
		printf(	"\n-------------------------------\n"
			"Hash tree performance test\n"
			"-------------------------------\n\n");

		memset(&creation_stats, 0, sizeof(statistics_data_t));
		memset(&verify_stats, 0, sizeof(statistics_data_t));

		printf("Creating %d hash trees of length %d with element length %d\n",
				count, hchain_length, hash_length);

		for(i = 0; i < count; i++)
		{
			HIP_DEBUG("number of leaves: %i\n", hchain_length);
			HIP_DEBUG("hash_length: %i\n", hash_length);
			HIP_DEBUG("data_length: %i\n", hash_length);

			gettimeofday(&start_time, NULL);
			htree = htree_init(hchain_length, hash_length, hash_length, 0, NULL, 0);
			htree_add_random_data(htree, hchain_length);
			htree_calc_nodes(htree, htree_leaf_generator, htree_node_generator, NULL);
			gettimeofday(&stop_time, NULL);
			timediff = calc_timeval_diff(&start_time, &stop_time);
			add_statistics_item(&creation_stats, timediff);

			htree_free(htree);
		}

		calc_statistics(&creation_stats, &num_items, &min, &max, &avg, &std_dev,
				STATS_IN_MSECS);
		printf("creation statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n",
					num_items, min, max, avg, std_dev);

		for(i = 0; i < count; i++)
		{
			htree = htree_init(hchain_length, hash_length, hash_length, hash_length, NULL, 0);
			htree_add_random_data(htree, hchain_length);
			htree_add_random_secrets(htree);
			htree_calc_nodes(htree, htree_leaf_generator, htree_node_generator, NULL);

			root = htree_get_root(htree, &root_length);
			branch_nodes = htree_get_branch(htree, i, NULL, &branch_length);
			data = htree_get_data(htree, i, &data_length);
			secret = htree_get_secret(htree, i, &secret_length);

			gettimeofday(&start_time, NULL);
			if (!htree_verify_branch(root, root_length,
					branch_nodes, branch_length,
					data, data_length, i,
					secret, secret_length,
					htree_leaf_generator, htree_node_generator, NULL))
			{
				gettimeofday(&stop_time, NULL);
				timediff = calc_timeval_diff(&start_time, &stop_time);
				add_statistics_item(&verify_stats, timediff);

				HIP_DEBUG("branch verified\n");

			} else
			{
				printf("ERROR verifying htree!\n");
				exit(1);
			}

			htree_free(htree);
		}

		calc_statistics(&verify_stats, &num_items, &min, &max, &avg, &std_dev,
				STATS_IN_MSECS);
		printf("verification statistics - num_data_items: %u, min: %.3fms, max: %.3fms, avg: %.3fms, std_dev: %.3fms\n",
					num_items, min, max, avg, std_dev);



		printf("\n\ntrying out hchain linking...\n");

		// simulate level 0 creation
		htree = htree_init(8, hash_length, hash_length, hash_length, NULL, 0);
		htree_add_random_secrets(htree);

		for (i = 0; i < 8; i++)
		{
			hchains[i] = hchain_create(hash_function, hash_length, hchain_length, 0,
								NULL);
			htree_add_data(htree, hchain_get_anchor(hchains[i]), hash_length);
		}

		htree_calc_nodes(htree, htree_leaf_generator, htree_node_generator, NULL);

		// simulate level 1 creation
		hchain = hchain_create(hash_function, hash_length, hchain_length, 1,
				htree);

		// simulate BEX
		// get hchain anchor
		root = htree_get_root(htree, &root_length);

		// simulate level 1 hchain verification
		if(!hchain_verify(hchain_get_seed(hchain), hchain_get_anchor(hchain),
				hash_function, hash_length, verify_length, root, root_length))
		{
			printf("hchain level 1 verfied\n");

		} else
		{
			printf("ERROR verifying hchain level 1!\n");
			exit(1);
		}

		// simulate update
		branch_nodes = htree_get_branch(htree, 0, NULL, &branch_length);
		secret = htree_get_secret(htree, 0, &secret_length);
		data = htree_get_data(htree, 0, &data_length);

		if (!htree_verify_branch(root, root_length,
				branch_nodes, branch_length,
				data, data_length, i,
				secret, secret_length,
				htree_leaf_generator, htree_node_generator, NULL))
		{
			printf("anchor verified\n");

		} else
		{
			printf("ERROR verifying anchor!\n");
			exit(1);
		}

		if (!memcmp(data, hchain_get_anchor(hchains[0]), hash_length))
		{
			printf("yes, this is the anchor we verified!\n");
		} else
		{
			printf("ERROR no this is not the anchor we verified!\n");
			exit(1);
		}
		hchain_free(hchain);

		// simulate level 0 hchain verification
		if(!hchain_verify(hchain_get_seed(hchains[0]), data,
				hash_function, hash_length, verify_length, NULL, 0))
		{
			printf("hchain level 0 verfied\n");

		} else
		{
			printf("ERROR verifying hchain level 0!\n");
			exit(1);
		}
	}
}