Example #1
0
int main(void)
{
	SHA512_HASH_CTX_MGR *mgr = NULL;
	SHA512_HASH_CTX ctxpool[NUM_JOBS], *ctx = NULL;
	uint32_t i, j, k, t, checked = 0;
	uint64_t *good;

	posix_memalign((void *)&mgr, 16, sizeof(SHA512_HASH_CTX_MGR));
	sha512_ctx_mgr_init(mgr);

	// Init contexts before first use
	for (i = 0; i < MSGS; i++) {
		hash_ctx_init(&ctxpool[i]);
		ctxpool[i].user_data = (void *)((uint64_t) i);
	}

	for (i = 0; i < MSGS; i++) {
		ctx = sha512_ctx_mgr_submit(mgr,
					    &ctxpool[i],
					    msgs[i], strlen((char *)msgs[i]), HASH_ENTIRE);

		if (ctx) {
			t = (unsigned long)(ctx->user_data);
			good = expResultDigest[t];
			checked++;
			for (j = 0; j < SHA512_DIGEST_NWORDS; j++) {
				if (good[j] != ctxpool[t].job.result_digest[j]) {
					printf("Test %d, digest %d is %016lX, "
					       "should be %016lX\n", t, j,
					       ctxpool[t].job.result_digest[j], good[j]);
					return -1;
				}
			}

			if (ctx->error) {
				printf("Something bad happened during the"
				       " submit. Error code: %d", ctx->error);
				return -1;
			}
		}
	}

	while (1) {
		ctx = sha512_ctx_mgr_flush(mgr);

		if (ctx) {
			t = (unsigned long)(ctx->user_data);
			good = expResultDigest[t];
			checked++;
			for (j = 0; j < SHA512_DIGEST_NWORDS; j++) {
				if (good[j] != ctxpool[t].job.result_digest[j]) {
					printf("Test %d, digest %d is %016lX, "
					       "should be %016lX\n", t, j,
					       ctxpool[t].job.result_digest[j], good[j]);
					return -1;
				}
			}

			if (ctx->error) {
				printf("Something bad happened during the "
				       "submit. Error code: %d", ctx->error);
				return -1;
			}
		} else {
			break;
		}
	}

	// do larger test in pseudo-random order

	// Init contexts before first use
	for (i = 0; i < NUM_JOBS; i++) {
		hash_ctx_init(&ctxpool[i]);
		ctxpool[i].user_data = (void *)((uint64_t) i);
	}

	checked = 0;
	for (i = 0; i < NUM_JOBS; i++) {
		j = PSEUDO_RANDOM_NUM(i);

		ctx = sha512_ctx_mgr_submit(mgr,
					    &ctxpool[i],
					    msgs[j], strlen((char *)msgs[j]), HASH_ENTIRE);

		if (ctx) {
			t = (unsigned long)(ctx->user_data);
			k = PSEUDO_RANDOM_NUM(t);
			good = expResultDigest[k];
			checked++;
			for (j = 0; j < SHA512_DIGEST_NWORDS; j++) {
				if (good[j] != ctxpool[t].job.result_digest[j]) {
					printf("Test %d, digest %d is %016lX, "
					       "should be %016lX\n", t, j,
					       ctxpool[t].job.result_digest[j], good[j]);
					return -1;
				}
			}

			if (ctx->error) {
				printf("Something bad happened during the"
				       " submit. Error code: %d", ctx->error);
				return -1;
			}

			t = (unsigned long)(ctx->user_data);
			k = PSEUDO_RANDOM_NUM(t);
		}
	}
	while (1) {
		ctx = sha512_ctx_mgr_flush(mgr);

		if (ctx) {
			t = (unsigned long)(ctx->user_data);
			k = PSEUDO_RANDOM_NUM(t);
			good = expResultDigest[k];
			checked++;
			for (j = 0; j < SHA512_DIGEST_NWORDS; j++) {
				if (good[j] != ctxpool[t].job.result_digest[j]) {
					printf("Test %d, digest %d is %016lX, "
					       "should be %016lX\n", t, j,
					       ctxpool[t].job.result_digest[j], good[j]);
					return -1;
				}
			}

			if (ctx->error) {
				printf("Something bad happened during the"
				       " submit. Error code: %d", ctx->error);
				return -1;
			}
		} else {
			break;
		}
	}

	if (checked != NUM_JOBS) {
		printf("only tested %d rather than %d\n", checked, NUM_JOBS);
		return -1;
	}

	printf(" multibinary_sha512 test: Pass\n");

	return 0;
}
int main(void)
{
	SHA512_HASH_CTX_MGR *mgr = NULL;
	SHA512_HASH_CTX ctxpool[TEST_BUFS];
	uint32_t i, j, fail = 0;
	unsigned char *bufs[TEST_BUFS];
	uint32_t lens[TEST_BUFS];
	unsigned int jobs, t;
	uint8_t *tmp_buf;

	printf("multibinary_sha512 test, %d sets of %dx%d max: ", RANDOMS, TEST_BUFS,
	       TEST_LEN);

	posix_memalign((void *)&mgr, 16, sizeof(SHA512_HASH_CTX_MGR));
	sha512_ctx_mgr_init(mgr);

	srand(TEST_SEED);

	for (i = 0; i < TEST_BUFS; i++) {
		// Allocate  and fill buffer
		bufs[i] = (unsigned char *)malloc(TEST_LEN);
		if (bufs[i] == NULL) {
			printf("malloc failed test aborted\n");
			return 1;
		}
		rand_buffer(bufs[i], TEST_LEN);

		// Init ctx contexts
		hash_ctx_init(&ctxpool[i]);
		ctxpool[i].user_data = (void *)((uint64_t) i);

		// Run reference test
		sha512_ref(bufs[i], digest_ref[i], TEST_LEN);

		// Run sb_sha512 test
		sha512_ctx_mgr_submit(mgr, &ctxpool[i], bufs[i], TEST_LEN, HASH_ENTIRE);
	}

	while (sha512_ctx_mgr_flush(mgr)) ;

	for (i = 0; i < TEST_BUFS; i++) {
		for (j = 0; j < SHA512_DIGEST_NWORDS; j++) {
			if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) {
				fail++;
				printf("Test%d fixed size, digest%d "
				       "fail 0x%016lX <=> 0x%016lX \n",
				       i, j, ctxpool[i].job.result_digest[j],
				       digest_ref[i][j]);
			}
		}
	}

	if (fail) {
		printf("Test failed function check %d\n", fail);
		return fail;
	}
	// Run tests with random size and number of jobs
	for (t = 0; t < RANDOMS; t++) {
		jobs = rand() % (TEST_BUFS);

		sha512_ctx_mgr_init(mgr);

		for (i = 0; i < jobs; i++) {
			// Use buffer with random len and contents
			lens[i] = rand() % (TEST_LEN);
			rand_buffer(bufs[i], lens[i]);

			// Run reference test
			sha512_ref(bufs[i], digest_ref[i], lens[i]);

			// Run sha512_mb test
			sha512_ctx_mgr_submit(mgr, &ctxpool[i], bufs[i], lens[i], HASH_ENTIRE);
		}

		while (sha512_ctx_mgr_flush(mgr)) ;

		for (i = 0; i < jobs; i++) {
			for (j = 0; j < SHA512_DIGEST_NWORDS; j++) {
				if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) {
					fail++;
					printf("Test%d, digest%d fail "
					       "0x%016lX <=> 0x%016lX\n",
					       i, j, ctxpool[i].job.result_digest[j],
					       digest_ref[i][j]);
				}
			}
		}
		if (fail) {
			printf("Test failed function check %d\n", fail);
			return fail;
		}

		putchar('.');
		fflush(0);
	}			// random test t

	// Test at the end of buffer
	jobs = rand() % TEST_BUFS;
	tmp_buf = (uint8_t *) malloc(sizeof(uint8_t) * jobs);
	if (!tmp_buf) {
		printf("malloc failed, end test aborted.\n");
		return 1;
	}

	rand_buffer(tmp_buf, jobs);

	sha512_ctx_mgr_init(mgr);

	// Extend to the end of allocated buffer to construct jobs
	for (i = 0; i < jobs; i++) {
		bufs[i] = (uint8_t *) & tmp_buf[i];
		lens[i] = jobs - i;

		// Reference test
		sha512_ref(bufs[i], digest_ref[i], lens[i]);

		// sb_sha512 test
		sha512_ctx_mgr_submit(mgr, &ctxpool[i], bufs[i], lens[i], HASH_ENTIRE);
	}

	while (sha512_ctx_mgr_flush(mgr)) ;

	for (i = 0; i < jobs; i++) {
		for (j = 0; j < SHA512_DIGEST_NWORDS; j++) {
			if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) {
				fail++;
				printf("End test failed at offset %d - result: 0x%016lX"
				       ", ref: 0x%016lX\n", i, ctxpool[i].job.result_digest[j],
				       digest_ref[i][j]);
			}
		}
	}

	putchar('.');

	if (fail)
		printf("Test failed function check %d\n", fail);
	else
		printf(" multibinary_sha512 rand: Pass\n");

	return fail;
}