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); } } }
/** creates a new hash chain * * @param hash_func hash function to be used to generate the hash values * @param hash_length length of the hash values * @param hchain_length number of hash elements * @param hchain_hierarchy the hierarchy level this hash chain will belong to * @param link_tree the link tree, if HHL is used * @return pointer to the newly created hash chain, NULL on error */ struct hash_chain *hchain_create(const hash_function hash_func, const int hash_length, const int hchain_length, const int hchain_hierarchy, struct hash_tree *link_tree) { struct hash_chain *hchain = NULL; /* the hash function output might be longer than needed * allocate enough memory for the hash function output * * @note we also allow a concatenation with the link tree root and the jump chain element here */ unsigned char hash_value[3 * MAX_HASH_LENGTH]; int hash_data_length = 0; int i, err = 0; HIP_ASSERT(hash_func != NULL); // make sure that the hash we want to use is smaller than the max output HIP_ASSERT(hash_length > 0 && hash_length <= MAX_HASH_LENGTH); HIP_ASSERT(hchain_length > 0); HIP_ASSERT(!(hchain_hierarchy == 0 && link_tree)); // allocate memory for a new hash chain HIP_IFEL(!(hchain = calloc(1, sizeof(struct hash_chain))), -1, "failed to allocate memory\n"); // allocate memory for the hash chain elements HIP_IFEL(!(hchain->elements = calloc(1, hash_length * hchain_length)), -1, "failed to allocate memory\n"); // set the link tree if we are using different hierarchies if (link_tree) { hchain->link_tree = link_tree; hash_data_length = 2 * hash_length; } else { hchain->link_tree = NULL; hash_data_length = hash_length; } for (i = 0; i < hchain_length; i++) { if (i > 0) { // (input, input_length, output) -> output_length == 20 HIP_IFEL(!(hash_func(hash_value, hash_data_length, hash_value)), -1, "failed to calculate hash\n"); // only consider highest bytes of digest with length of actual element memcpy(&hchain->elements[i * hash_length], hash_value, hash_length); } else { // random bytes as seed -> need a copy in hash_value for further computations HIP_IFEL(RAND_bytes(hash_value, hash_length) <= 0, -1, "failed to get random bytes for source element\n"); memcpy(&hchain->elements[i * hash_length], hash_value, hash_length); } /* concatenate used part of the calculated hash with the link tree root */ if (link_tree) { memcpy(&hash_value[hash_length], link_tree->root, link_tree->node_length); } } hchain->hash_function = hash_func; hchain->hash_length = hash_length; hchain->hchain_length = hchain_length; hchain->current_index = hchain_length; hchain->hchain_hierarchy = hchain_hierarchy; HIP_DEBUG("Hash-chain with %i elements of length %i created!\n", hchain_length, hash_length); out_err: if (err) { // hchain was fully created hchain_free(hchain); hchain = NULL; } return hchain; }