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