xref: /isa-l_crypto/sha512_mb/sha512_mb_rand_ssl_test.c (revision e3f7d4fb1bd3dcbfb75b87e0c61870e751d613aa)
1 /**********************************************************************
2   Copyright(c) 2011-2016 Intel Corporation All rights reserved.
3 
4   Redistribution and use in source and binary forms, with or without
5   modification, are permitted provided that the following conditions
6   are met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above copyright
10       notice, this list of conditions and the following disclaimer in
11       the documentation and/or other materials provided with the
12       distribution.
13     * Neither the name of Intel Corporation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <openssl/sha.h>
33 #include "sha512_mb.h"
34 
35 #define TEST_LEN  (1024*1024)
36 #define TEST_BUFS 200
37 #ifndef RANDOMS
38 # define RANDOMS  10
39 #endif
40 #ifndef TEST_SEED
41 # define TEST_SEED 0x1234
42 #endif
43 
44 /* Reference digest global to reduce stack usage */
45 static uint8_t digest_ssl[TEST_BUFS][8 * SHA512_DIGEST_NWORDS];
46 
47 // Generates pseudo-random data
48 void rand_buffer(unsigned char *buf, const long buffer_size)
49 {
50 	long i;
51 	for (i = 0; i < buffer_size; i++)
52 		buf[i] = rand();
53 }
54 
55 int main(void)
56 {
57 	SHA512_HASH_CTX_MGR *mgr = NULL;
58 	SHA512_HASH_CTX ctxpool[TEST_BUFS];
59 	unsigned char *bufs[TEST_BUFS];
60 	uint32_t i, j, fail = 0;
61 	uint32_t lens[TEST_BUFS];
62 	unsigned int jobs, t;
63 
64 	printf("multibinary_sha512 test, %d sets of %dx%d max: ", RANDOMS, TEST_BUFS,
65 	       TEST_LEN);
66 
67 	srand(TEST_SEED);
68 
69 	posix_memalign((void *)&mgr, 16, sizeof(SHA512_HASH_CTX_MGR));
70 	sha512_ctx_mgr_init(mgr);
71 
72 	for (i = 0; i < TEST_BUFS; i++) {
73 		// Allocate and fill buffer
74 		bufs[i] = (unsigned char *)malloc(TEST_LEN);
75 		if (bufs[i] == NULL) {
76 			printf("malloc failed test aborted\n");
77 			return 1;
78 		}
79 		rand_buffer(bufs[i], TEST_LEN);
80 
81 		// Init ctx contents
82 		hash_ctx_init(&ctxpool[i]);
83 		ctxpool[i].user_data = (void *)((uint64_t) i);
84 
85 		// SSL test
86 		SHA512(bufs[i], TEST_LEN, digest_ssl[i]);
87 
88 		// sb_sha512 test
89 		sha512_ctx_mgr_submit(mgr, &ctxpool[i], bufs[i], TEST_LEN, HASH_ENTIRE);
90 	}
91 
92 	while (sha512_ctx_mgr_flush(mgr)) ;
93 
94 	for (i = 0; i < TEST_BUFS; i++) {
95 		for (j = 0; j < SHA512_DIGEST_NWORDS; j++) {
96 			if (ctxpool[i].job.result_digest[j] !=
97 			    to_be64(((uint64_t *) digest_ssl[i])[j])) {
98 				fail++;
99 				printf("Test%d, digest%d fail %016lX <=> %016lX\n",
100 				       i, j, ctxpool[i].job.result_digest[j],
101 				       to_be64(((uint64_t *) digest_ssl[i])[j]));
102 			}
103 		}
104 	}
105 	putchar('.');
106 
107 	// Run tests with random size and number of jobs
108 	for (t = 0; t < RANDOMS; t++) {
109 		jobs = rand() % (TEST_BUFS);
110 
111 		sha512_ctx_mgr_init(mgr);
112 
113 		for (i = 0; i < jobs; i++) {
114 			// Ramdom buffer with ramdom len and contents
115 			lens[i] = rand() % (TEST_LEN);
116 			rand_buffer(bufs[i], lens[i]);
117 
118 			// Run SSL test
119 			SHA512(bufs[i], lens[i], digest_ssl[i]);
120 
121 			// Run sb_sha512 test
122 			sha512_ctx_mgr_submit(mgr, &ctxpool[i], bufs[i], lens[i], HASH_ENTIRE);
123 		}
124 
125 		while (sha512_ctx_mgr_flush(mgr)) ;
126 
127 		for (i = 0; i < jobs; i++) {
128 			for (j = 0; j < SHA512_DIGEST_NWORDS; j++) {
129 				if (ctxpool[i].job.result_digest[j] !=
130 				    to_be64(((uint64_t *) digest_ssl[i])[j])) {
131 					fail++;
132 					printf("Test%d, digest%d fail %016lX <=> %016lX\n",
133 					       i, j, ctxpool[i].job.result_digest[j],
134 					       to_be64(((uint64_t *) digest_ssl[i])[j]));
135 				}
136 			}
137 		}
138 		if (fail) {
139 			printf("Test failed function check %d\n", fail);
140 			return fail;
141 		}
142 
143 		putchar('.');
144 		fflush(0);
145 	}			// random test t
146 
147 	if (fail)
148 		printf("Test failed function check %d\n", fail);
149 	else
150 		printf(" multibinary_sha512_ssl rand: Pass\n");
151 
152 	return fail;
153 }
154