xref: /isa-l_crypto/sha256_mb/sha256_mb_flush_test.c (revision 2a268c597b745b55509ea38d00753e5caa85e9ac)
1 /**********************************************************************
2   Copyright(c) 2011-2017 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 "sha256_mb.h"
33 
34 #define TEST_LEN  (1024*1024)
35 #define TEST_BUFS (SHA256_MAX_LANES - 1)
36 #ifndef TEST_SEED
37 # define TEST_SEED 0x1234
38 #endif
39 
40 static uint32_t digest_ref[TEST_BUFS][SHA256_DIGEST_NWORDS];
41 
42 // Compare against reference function
43 extern void sha256_ref(uint8_t * input_data, uint32_t * digest, uint32_t len);
44 
45 // Generates pseudo-random data
46 void rand_buffer(unsigned char *buf, const long buffer_size)
47 {
48 	long i;
49 	for (i = 0; i < buffer_size; i++)
50 		buf[i] = rand();
51 }
52 
53 uint8_t lens_print_and_check(SHA256_HASH_CTX_MGR * mgr)
54 {
55 	static int32_t last_lens[SHA256_MAX_LANES] = { 0 };
56 	int32_t len;
57 	uint8_t num_unchanged = 0;
58 	int i;
59 	for (i = 0; i < SHA256_MAX_LANES; i++) {
60 		len = (int32_t) mgr->mgr.lens[i];
61 		// len[i] in mgr consists of byte_length<<4 | lane_index
62 		len = (len >= 16) ? (len >> 4 << 6) : 0;
63 		printf("\t%d", len);
64 		if (last_lens[i] > 0 && last_lens[i] == len)
65 			num_unchanged += 1;
66 		last_lens[i] = len;
67 	}
68 	printf("\n");
69 	return num_unchanged;
70 }
71 
72 int main(void)
73 {
74 	SHA256_HASH_CTX_MGR *mgr = NULL;
75 	SHA256_HASH_CTX ctxpool[TEST_BUFS];
76 	uint32_t i, j, fail = 0;
77 	unsigned char *bufs[TEST_BUFS];
78 	uint32_t lens[TEST_BUFS];
79 	uint8_t num_ret, num_unchanged = 0;
80 	int ret;
81 
82 	printf("sha256_mb flush test, %d buffers with %d length: \n", TEST_BUFS, TEST_LEN);
83 
84 	ret = posix_memalign((void *)&mgr, 16, sizeof(SHA256_HASH_CTX_MGR));
85 	if ((ret != 0) || (mgr == NULL)) {
86 		printf("posix_memalign failed test aborted\n");
87 		return 1;
88 	}
89 
90 	sha256_ctx_mgr_init(mgr);
91 
92 	srand(TEST_SEED);
93 
94 	for (i = 0; i < TEST_BUFS; i++) {
95 		// Allocate  and fill buffer
96 		lens[i] = TEST_LEN / SHA256_MAX_LANES * (i + 1);
97 		bufs[i] = (unsigned char *)malloc(lens[i]);
98 		if (bufs[i] == NULL) {
99 			printf("malloc failed test aborted\n");
100 			goto end;
101 		}
102 		rand_buffer(bufs[i], lens[i]);
103 	}
104 
105 	for (i = 0; i < TEST_BUFS; i++) {
106 		// Init ctx contexts
107 		hash_ctx_init(&ctxpool[i]);
108 		ctxpool[i].user_data = (void *)((uint64_t) i);
109 
110 		// Run reference test
111 		sha256_ref(bufs[i], digest_ref[i], lens[i]);
112 
113 		// Run sb_sha256 test
114 		sha256_ctx_mgr_submit(mgr, &ctxpool[i], bufs[i], lens[i], HASH_ENTIRE);
115 	}
116 
117 	printf("Changes of lens inside mgr:\n");
118 	lens_print_and_check(mgr);
119 	while (sha256_ctx_mgr_flush(mgr)) {
120 		num_ret = lens_print_and_check(mgr);
121 		num_unchanged = num_unchanged > num_ret ? num_unchanged : num_ret;
122 	}
123 	printf("Info of sha256_mb lens prints over\n");
124 
125 	for (i = 0; i < TEST_BUFS; i++) {
126 		for (j = 0; j < SHA256_DIGEST_NWORDS; j++) {
127 			if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) {
128 				fail++;
129 				printf("Test%d fixed size, digest%d "
130 				       "fail 0x%08X <=> 0x%08X \n",
131 				       i, j, ctxpool[i].job.result_digest[j],
132 				       digest_ref[i][j]);
133 			}
134 		}
135 	}
136 
137 	if (fail)
138 		printf("Test failed function check %d\n", fail);
139 	else if (num_unchanged)
140 		printf("SHA-NI is used when %d or %d jobs are uncompleted\n",
141 		       num_unchanged, num_unchanged + 1);
142 	else
143 		printf("SHA-NI is not used, or used for last job\n");
144 
145       end:
146 	for (i = 0; i < TEST_BUFS; i++)
147 		free(bufs[i]);
148 	aligned_free(mgr);
149 
150 	return fail;
151 }
152