145818ee1SMatthew Ahrens /*
245818ee1SMatthew Ahrens  * CDDL HEADER START
345818ee1SMatthew Ahrens  *
445818ee1SMatthew Ahrens  * The contents of this file are subject to the terms of the
545818ee1SMatthew Ahrens  * Common Development and Distribution License (the "License").
645818ee1SMatthew Ahrens  * You may not use this file except in compliance with the License.
745818ee1SMatthew Ahrens  *
845818ee1SMatthew Ahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
945818ee1SMatthew Ahrens  * or http://opensource.org/licenses/CDDL-1.0.
1045818ee1SMatthew Ahrens  * See the License for the specific language governing permissions
1145818ee1SMatthew Ahrens  * and limitations under the License.
1245818ee1SMatthew Ahrens  *
1345818ee1SMatthew Ahrens  * When distributing Covered Code, include this CDDL HEADER in each
1445818ee1SMatthew Ahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1545818ee1SMatthew Ahrens  * If applicable, add the following below this CDDL HEADER, with the
1645818ee1SMatthew Ahrens  * fields enclosed by brackets "[]" replaced with your own identifying
1745818ee1SMatthew Ahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
1845818ee1SMatthew Ahrens  *
1945818ee1SMatthew Ahrens  * CDDL HEADER END
2045818ee1SMatthew Ahrens  */
2145818ee1SMatthew Ahrens 
2245818ee1SMatthew Ahrens /*
2345818ee1SMatthew Ahrens  * Copyright 2013 Saso Kiselkov. All rights reserved.
2445818ee1SMatthew Ahrens  */
2545818ee1SMatthew Ahrens 
2645818ee1SMatthew Ahrens /*
2745818ee1SMatthew Ahrens  * This is just to keep the compiler happy about sys/time.h not declaring
2845818ee1SMatthew Ahrens  * gettimeofday due to -D_KERNEL (we can do this since we're actually
2945818ee1SMatthew Ahrens  * running in userspace, but we need -D_KERNEL for the remaining Edon-R code).
3045818ee1SMatthew Ahrens  */
3145818ee1SMatthew Ahrens #ifdef	_KERNEL
3245818ee1SMatthew Ahrens #undef	_KERNEL
3345818ee1SMatthew Ahrens #endif
3445818ee1SMatthew Ahrens 
3545818ee1SMatthew Ahrens #include <sys/edonr.h>
3645818ee1SMatthew Ahrens #include <stdlib.h>
3745818ee1SMatthew Ahrens #include <strings.h>
3845818ee1SMatthew Ahrens #include <stdio.h>
3945818ee1SMatthew Ahrens #include <sys/time.h>
4045818ee1SMatthew Ahrens #include <note.h>
4145818ee1SMatthew Ahrens 
4245818ee1SMatthew Ahrens /*
4345818ee1SMatthew Ahrens  * Test messages from:
4445818ee1SMatthew Ahrens  * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA_All.pdf
4545818ee1SMatthew Ahrens  */
4645818ee1SMatthew Ahrens const char	*test_msg0 = "abc";
4745818ee1SMatthew Ahrens const char	*test_msg1 = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmn"
4845818ee1SMatthew Ahrens 	"lmnomnopnopq";
4945818ee1SMatthew Ahrens const char	*test_msg2 = "abcdefghbcdefghicdefghijdefghijkefghijklfghi"
5045818ee1SMatthew Ahrens 	"jklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
5145818ee1SMatthew Ahrens 
5245818ee1SMatthew Ahrens /*
5345818ee1SMatthew Ahrens  * Test digests computed by hand. There's no formal standard or spec for edonr.
5445818ee1SMatthew Ahrens  */
5545818ee1SMatthew Ahrens const uint8_t	edonr_224_test_digests[][28] = {
5645818ee1SMatthew Ahrens 	{
5745818ee1SMatthew Ahrens 		/* for test_msg0 */
5845818ee1SMatthew Ahrens 		0x56, 0x63, 0xc4, 0x93, 0x95, 0x20, 0xfa, 0xf6,
5945818ee1SMatthew Ahrens 		0x12, 0x31, 0x65, 0xa4, 0x66, 0xf2, 0x56, 0x01,
6045818ee1SMatthew Ahrens 		0x95, 0x2e, 0xa9, 0xe4, 0x24, 0xdd, 0xc9, 0x6b,
6145818ee1SMatthew Ahrens 		0xef, 0xd0, 0x40, 0x94
6245818ee1SMatthew Ahrens 	},
6345818ee1SMatthew Ahrens 	{
6445818ee1SMatthew Ahrens 		/* for test_msg1 */
6545818ee1SMatthew Ahrens 		0xd0, 0x13, 0xe4, 0x87, 0x4d, 0x06, 0x8d, 0xca,
6645818ee1SMatthew Ahrens 		0x4e, 0x14, 0xb9, 0x37, 0x2f, 0xce, 0x12, 0x20,
6745818ee1SMatthew Ahrens 		0x60, 0xf8, 0x5c, 0x0a, 0xfd, 0x7a, 0x7d, 0x97,
6845818ee1SMatthew Ahrens 		0x88, 0x2b, 0x05, 0x75
6945818ee1SMatthew Ahrens 	}
7045818ee1SMatthew Ahrens 	/* no test vector for test_msg2 */
7145818ee1SMatthew Ahrens };
7245818ee1SMatthew Ahrens 
7345818ee1SMatthew Ahrens const uint8_t	edonr_256_test_digests[][32] = {
7445818ee1SMatthew Ahrens 	{
7545818ee1SMatthew Ahrens 		/* for test_msg0 */
7645818ee1SMatthew Ahrens 		0x54, 0xd7, 0x8b, 0x13, 0xc7, 0x4e, 0xda, 0x5a,
7745818ee1SMatthew Ahrens 		0xed, 0xc2, 0x71, 0xcc, 0x88, 0x1f, 0xb2, 0x2f,
7845818ee1SMatthew Ahrens 		0x83, 0x99, 0xaf, 0xd3, 0x04, 0x0b, 0x6a, 0x39,
7945818ee1SMatthew Ahrens 		0x2d, 0x73, 0x94, 0x05, 0x50, 0x8d, 0xd8, 0x51
8045818ee1SMatthew Ahrens 	},
8145818ee1SMatthew Ahrens 	{
8245818ee1SMatthew Ahrens 		/* for test_msg1 */
8345818ee1SMatthew Ahrens 		0x49, 0x2d, 0x0b, 0x19, 0xab, 0x1e, 0xde, 0x3a,
8445818ee1SMatthew Ahrens 		0xea, 0x9b, 0xf2, 0x39, 0x3a, 0xb1, 0x21, 0xde,
8545818ee1SMatthew Ahrens 		0x21, 0xf6, 0x80, 0x1f, 0xad, 0xbe, 0x8b, 0x07,
8645818ee1SMatthew Ahrens 		0xc7, 0xfb, 0xe6, 0x99, 0x0e, 0x4d, 0x73, 0x63
8745818ee1SMatthew Ahrens 	}
8845818ee1SMatthew Ahrens 	/* no test vectorfor test_msg2 */
8945818ee1SMatthew Ahrens };
9045818ee1SMatthew Ahrens 
9145818ee1SMatthew Ahrens const uint8_t	edonr_384_test_digests[][48] = {
9245818ee1SMatthew Ahrens 	{
9345818ee1SMatthew Ahrens 		/* for test_msg0 */
9445818ee1SMatthew Ahrens 		0x0e, 0x7c, 0xd7, 0x85, 0x78, 0x77, 0xe0, 0x89,
9545818ee1SMatthew Ahrens 		0x5b, 0x1c, 0xdf, 0x49, 0xf4, 0x1d, 0x20, 0x9c,
9645818ee1SMatthew Ahrens 		0x72, 0x7d, 0x2e, 0x57, 0x9b, 0x9b, 0x9a, 0xdc,
9745818ee1SMatthew Ahrens 		0x60, 0x27, 0x97, 0x82, 0xb9, 0x90, 0x72, 0xec,
9845818ee1SMatthew Ahrens 		0x7e, 0xce, 0xd3, 0x16, 0x5f, 0x47, 0x75, 0x48,
9945818ee1SMatthew Ahrens 		0xfa, 0x60, 0x72, 0x7e, 0x01, 0xc7, 0x7c, 0xc6
10045818ee1SMatthew Ahrens 	},
10145818ee1SMatthew Ahrens 	{
10245818ee1SMatthew Ahrens 		/* no test vector for test_msg1 */
103*0c3cd038SMatthew Ahrens 		0
10445818ee1SMatthew Ahrens 	},
10545818ee1SMatthew Ahrens 	{
10645818ee1SMatthew Ahrens 		/* for test_msg2 */
10745818ee1SMatthew Ahrens 		0xe2, 0x34, 0xa1, 0x02, 0x83, 0x76, 0xae, 0xe6,
10845818ee1SMatthew Ahrens 		0x82, 0xd9, 0x38, 0x32, 0x0e, 0x00, 0x78, 0xd2,
10945818ee1SMatthew Ahrens 		0x34, 0xdb, 0xb9, 0xbd, 0xf0, 0x08, 0xa8, 0x0f,
11045818ee1SMatthew Ahrens 		0x63, 0x1c, 0x3d, 0x4a, 0xfd, 0x0a, 0xe9, 0x59,
11145818ee1SMatthew Ahrens 		0xdc, 0xd4, 0xce, 0xcd, 0x8d, 0x67, 0x6c, 0xea,
11245818ee1SMatthew Ahrens 		0xbb, 0x1a, 0x32, 0xed, 0x5c, 0x6b, 0xf1, 0x7f
11345818ee1SMatthew Ahrens 	}
11445818ee1SMatthew Ahrens };
11545818ee1SMatthew Ahrens 
11645818ee1SMatthew Ahrens const uint8_t	edonr_512_test_digests[][64] = {
11745818ee1SMatthew Ahrens 	{
11845818ee1SMatthew Ahrens 		/* for test_msg0 */
11945818ee1SMatthew Ahrens 		0x1b, 0x14, 0xdb, 0x15, 0x5f, 0x1d, 0x40, 0x65,
12045818ee1SMatthew Ahrens 		0x94, 0xb8, 0xce, 0xf7, 0x0a, 0x43, 0x62, 0xec,
12145818ee1SMatthew Ahrens 		0x6b, 0x5d, 0xe6, 0xa5, 0xda, 0xf5, 0x0e, 0xc9,
12245818ee1SMatthew Ahrens 		0x99, 0xe9, 0x87, 0xc1, 0x9d, 0x30, 0x49, 0xe2,
12345818ee1SMatthew Ahrens 		0xde, 0x59, 0x77, 0xbb, 0x05, 0xb1, 0xbb, 0x22,
12445818ee1SMatthew Ahrens 		0x00, 0x50, 0xa1, 0xea, 0x5b, 0x46, 0xa9, 0xf1,
12545818ee1SMatthew Ahrens 		0x74, 0x0a, 0xca, 0xfb, 0xf6, 0xb4, 0x50, 0x32,
12645818ee1SMatthew Ahrens 		0xad, 0xc9, 0x0c, 0x62, 0x83, 0x72, 0xc2, 0x2b
12745818ee1SMatthew Ahrens 	},
12845818ee1SMatthew Ahrens 	{
12945818ee1SMatthew Ahrens 		/* no test vector for test_msg1 */
130*0c3cd038SMatthew Ahrens 		0
13145818ee1SMatthew Ahrens 	},
13245818ee1SMatthew Ahrens 	{
13345818ee1SMatthew Ahrens 		/* for test_msg2 */
13445818ee1SMatthew Ahrens 		0x53, 0x51, 0x07, 0x0d, 0xc5, 0x1c, 0x3b, 0x2b,
13545818ee1SMatthew Ahrens 		0xac, 0xa5, 0xa6, 0x0d, 0x02, 0x52, 0xcc, 0xb4,
13645818ee1SMatthew Ahrens 		0xe4, 0x92, 0x1a, 0x96, 0xfe, 0x5a, 0x69, 0xe7,
13745818ee1SMatthew Ahrens 		0x6d, 0xad, 0x48, 0xfd, 0x21, 0xa0, 0x84, 0x5a,
13845818ee1SMatthew Ahrens 		0xd5, 0x7f, 0x88, 0x0b, 0x3e, 0x4a, 0x90, 0x7b,
13945818ee1SMatthew Ahrens 		0xc5, 0x03, 0x15, 0x18, 0x42, 0xbb, 0x94, 0x9e,
14045818ee1SMatthew Ahrens 		0x1c, 0xba, 0x74, 0x39, 0xa6, 0x40, 0x9a, 0x34,
14145818ee1SMatthew Ahrens 		0xb8, 0x43, 0x6c, 0xb4, 0x69, 0x21, 0x58, 0x3c
14245818ee1SMatthew Ahrens 	}
14345818ee1SMatthew Ahrens };
14445818ee1SMatthew Ahrens 
14545818ee1SMatthew Ahrens int
main(int argc,char * argv[])14645818ee1SMatthew Ahrens main(int argc, char *argv[])
14745818ee1SMatthew Ahrens {
14845818ee1SMatthew Ahrens 	boolean_t	failed = B_FALSE;
14945818ee1SMatthew Ahrens 	uint64_t	cpu_mhz = 0;
15045818ee1SMatthew Ahrens 
15145818ee1SMatthew Ahrens 	if (argc == 2)
15245818ee1SMatthew Ahrens 		cpu_mhz = atoi(argv[1]);
15345818ee1SMatthew Ahrens 
15445818ee1SMatthew Ahrens #define	EDONR_ALGO_TEST(_m, mode, testdigest)				\
15545818ee1SMatthew Ahrens 	do {								\
15645818ee1SMatthew Ahrens 		EdonRState	ctx;					\
15745818ee1SMatthew Ahrens 		uint8_t		digest[mode / 8];			\
15845818ee1SMatthew Ahrens 		EdonRInit(&ctx, mode);					\
15945818ee1SMatthew Ahrens 		EdonRUpdate(&ctx, (const uint8_t *) _m, strlen(_m) * 8);\
16045818ee1SMatthew Ahrens 		EdonRFinal(&ctx, digest);				\
16145818ee1SMatthew Ahrens 		(void) printf("Edon-R-%-6sMessage: " #_m		\
16245818ee1SMatthew Ahrens 		    "\tResult: ", #mode);				\
16345818ee1SMatthew Ahrens 		if (bcmp(digest, testdigest, mode / 8) == 0) {		\
16445818ee1SMatthew Ahrens 			(void) printf("OK\n");				\
16545818ee1SMatthew Ahrens 		} else {						\
16645818ee1SMatthew Ahrens 			(void) printf("FAILED!\n");			\
16745818ee1SMatthew Ahrens 			failed = B_TRUE;				\
16845818ee1SMatthew Ahrens 		}							\
16945818ee1SMatthew Ahrens 		NOTE(CONSTCOND)						\
17045818ee1SMatthew Ahrens 	} while (0)
17145818ee1SMatthew Ahrens 
17245818ee1SMatthew Ahrens #define	EDONR_PERF_TEST(mode)						\
17345818ee1SMatthew Ahrens 	do {								\
17445818ee1SMatthew Ahrens 		EdonRState	ctx;					\
17545818ee1SMatthew Ahrens 		uint8_t		digest[mode / 8];			\
17645818ee1SMatthew Ahrens 		uint8_t		block[131072];				\
17745818ee1SMatthew Ahrens 		uint64_t	delta;					\
17845818ee1SMatthew Ahrens 		double		cpb = 0;				\
17945818ee1SMatthew Ahrens 		int		i;					\
18045818ee1SMatthew Ahrens 		struct timeval	start, end;				\
18145818ee1SMatthew Ahrens 		bzero(block, sizeof (block));				\
18245818ee1SMatthew Ahrens 		(void) gettimeofday(&start, NULL);			\
18345818ee1SMatthew Ahrens 		EdonRInit(&ctx, mode);					\
18445818ee1SMatthew Ahrens 		for (i = 0; i < 8192; i++)				\
18545818ee1SMatthew Ahrens 			EdonRUpdate(&ctx, block, sizeof (block) * 8);	\
18645818ee1SMatthew Ahrens 		EdonRFinal(&ctx, digest);				\
18745818ee1SMatthew Ahrens 		(void) gettimeofday(&end, NULL);			\
18845818ee1SMatthew Ahrens 		delta = (end.tv_sec * 1000000llu + end.tv_usec) -	\
18945818ee1SMatthew Ahrens 		    (start.tv_sec * 1000000llu + start.tv_usec);	\
19045818ee1SMatthew Ahrens 		if (cpu_mhz != 0) {					\
19145818ee1SMatthew Ahrens 			cpb = (cpu_mhz * 1e6 * ((double)delta /		\
19245818ee1SMatthew Ahrens 			    1000000)) / (8192 * 128 * 1024);		\
19345818ee1SMatthew Ahrens 		}							\
19445818ee1SMatthew Ahrens 		(void) printf("Edon-R-%-6s%llu us (%.02f CPB)\n", #mode,\
19545818ee1SMatthew Ahrens 		    (u_longlong_t)delta, cpb);				\
19645818ee1SMatthew Ahrens 		NOTE(CONSTCOND)						\
19745818ee1SMatthew Ahrens 	} while (0)
19845818ee1SMatthew Ahrens 
19945818ee1SMatthew Ahrens 	(void) printf("Running algorithm correctness tests:\n");
20045818ee1SMatthew Ahrens 	EDONR_ALGO_TEST(test_msg0, 224, edonr_224_test_digests[0]);
20145818ee1SMatthew Ahrens 	EDONR_ALGO_TEST(test_msg1, 224, edonr_224_test_digests[1]);
20245818ee1SMatthew Ahrens 	EDONR_ALGO_TEST(test_msg0, 256, edonr_256_test_digests[0]);
20345818ee1SMatthew Ahrens 	EDONR_ALGO_TEST(test_msg1, 256, edonr_256_test_digests[1]);
20445818ee1SMatthew Ahrens 	EDONR_ALGO_TEST(test_msg0, 384, edonr_384_test_digests[0]);
20545818ee1SMatthew Ahrens 	EDONR_ALGO_TEST(test_msg2, 384, edonr_384_test_digests[2]);
20645818ee1SMatthew Ahrens 	EDONR_ALGO_TEST(test_msg0, 512, edonr_512_test_digests[0]);
20745818ee1SMatthew Ahrens 	EDONR_ALGO_TEST(test_msg2, 512, edonr_512_test_digests[2]);
20845818ee1SMatthew Ahrens 	if (failed)
20945818ee1SMatthew Ahrens 		return (1);
21045818ee1SMatthew Ahrens 
21145818ee1SMatthew Ahrens 	(void) printf("Running performance tests (hashing 1024 MiB of "
21245818ee1SMatthew Ahrens 	    "data):\n");
21345818ee1SMatthew Ahrens 	EDONR_PERF_TEST(256);
21445818ee1SMatthew Ahrens 	EDONR_PERF_TEST(512);
21545818ee1SMatthew Ahrens 
21645818ee1SMatthew Ahrens 	return (0);
21745818ee1SMatthew Ahrens }
218