inline void message_digest_context::sign_update(const buffer& buf)
		{
			sign_update(buffer_cast<uint8_t>(buf), buffer_size(buf));
		}
예제 #2
0
파일: benchmark.c 프로젝트: robgjansen/pbs
void pbs_test(char *data) {
	pbs_parameters pbs;
	pbs_pk pk;
	pbs_sk sk;
	pbs_bank_state bstate;
	pbs_client_state cstate;
	pbs_signature signature;
	pbs_workspace workspace;
	struct timespec bank_start, bank_end, verify_start, verify_end;
	long bank_nanos = 0;
	long verify_nanos = 0;
	int success, i = 0;

	load_parameters(&pbs);
	/* use this to generate keys */
	/*
	 gen_keys(&pbs, &sk, &pk);
	 printf("skx:%s\n", BN_bn2hex(sk.x));
	 printf("pky:%s\n", BN_bn2hex(pk.y));
	 */
	load_keys(&sk, &pk);

	int date1 = time(NULL) / 86400;
	int date2 = date1 + 7;
	int date3 = date1 + 28;
	int infolen = 3 * sizeof(date1) + 6; /* 3 ints, 2 commas, null byte */
	char info[infolen];
	snprintf(info, infolen, "%d,%d,%d", date1, date2, date3);
	/* printf("%s\n", info); */

	/* do a bunch of signatures and verifies */
	/* FIXME this can be made much more efficient */
	char *pos = data;
	for (i = 0; i < NUM_LOOPS_PER_RUN; ++i) {
		BIGNUM *a = BN_new();
		BIGNUM *b = BN_new();
		BIGNUM *e = BN_new();
		BIGNUM *r = BN_new();
		BIGNUM *c = BN_new();
		BIGNUM *s = BN_new();
		BIGNUM *d = BN_new();

		/* client sends bank request, bank sends back a,b,info */
		clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &bank_start);
		bank_sign_init(a, b, &bstate, info, &pbs);
		clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &bank_end);

		bank_nanos = get_timer_nanos(&bank_start, &bank_end);

		/* client initialization */
		sign_init(&cstate, &signature, &workspace);
		/* client uses a,b,info and its message to produce e for bank */
		sign_update(e, &cstate, &pbs, &pk, data, info, a, b);

		/* bank uses e to produce r,c,s,d for client */
		clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &bank_start);
		bank_sign_update(r, c, s, d, &bstate, &pbs, &sk, e);
		clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &bank_end);

		bank_nanos += get_timer_nanos(&bank_start, &bank_end);

		/* client finishes signature */
		sign_final(&signature, r, c, s, d, &cstate, &pbs);

		/* now verify */
		clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &verify_start);
		success = verify(&signature, &pk, &pbs, info, data, &workspace);
		clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &verify_end);

		verify_nanos = get_timer_nanos(&verify_start, &verify_end);

		if (success != 0) {
			printf("Signature incorrect\n");
		}
		pos += CELL_NETWORK_SIZE + 1;

		BN_free(a);
		BN_free(b);
		BN_free(e);
		BN_free(r);
		BN_free(c);
		BN_free(s);
		BN_free(d);
		free_bank_state(&bstate);
		free_client_state(&cstate);
		free_signature(&signature);
		free_workspace(&workspace);

		printf("pbs Bank effort: 1 signature %ld nanoseconds (%d/%d)\n", bank_nanos, i+1, NUM_LOOPS_PER_RUN);
		printf("pbs Signature verification: 1 verify %ld nanoseconds (%d/%d)\n", verify_nanos, i+1, NUM_LOOPS_PER_RUN);
	}

	free_keys(&sk, &pk);
	free_parameters(&pbs);


#ifdef DEBUG
	/* signature results */
	printBN(signature.delta, "delta:");
	printBN(signature.sigma, "sigma:");
	printBN(signature.omega, "omega:");
	printBN(signature.rho, "rho:");
	printf("info:%s\n", info);
	printf("message:%s\n", message);

	printBN(a, "a:");
	printBN(b, "b:");
	printBN(e, "e:");
	printBN(r, "r:");
	printBN(c, "c:");
	printBN(s, "s:");
	printBN(d, "d:");
	printBN(cstate.t1, "t1:");
	printBN(cstate.t2, "t2:");
	printBN(cstate.t3, "t3:");
	printBN(cstate.t4, "t4:");
	printBN(cstate.epsilon, "epsilon:");
	printBN(bstate.d, "d:");
	printBN(bstate.s, "s:");
	printBN(bstate.u, "u:");
	printBN(pbs.g, "g:");
	printBN(pbs.p, "p:");
	printBN(pbs.q, "q:");
	printBN(pk.y, "y:");
	printBN(sk.x, "x:");

	/* sanity checks */
	BIGNUM *temp1 = BN_new();
	BIGNUM *temp2 = BN_new();
	BN_CTX *ctx = BN_CTX_new();

	/* q |? p-1 */
	BN_sub(temp1, pbs.p, BN_value_one());
	BN_mod(temp2, temp1, pbs.q, ctx);
	printf("q|p-1 remainder: %s\n", BN_bn2hex(temp2));

	/* g^q =? 1 mod p */
	BN_mod_exp(temp1, pbs.g, pbs.q, pbs.p, ctx);
	printf("g^q =? 1 mod p result: %s\n", BN_bn2hex(temp1));
#endif

	/*
	 BN_free(a);
	 BN_free(b);
	 BN_free(e);
	 BN_free(r);
	 BN_free(c);
	 BN_free(s);
	 BN_free(d);
	 free_bank_state(&bstate);
	 free_client_state(&cstate);
	 free_signature(&signature);
	 free_keys(&sk, &pk);
	 free_parameters(&pbs);
	 */
}