]> git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/sha256_mb/sha256_mb_test.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / crypto / isa-l / isa-l_crypto / sha256_mb / sha256_mb_test.c
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 <string.h>
33 #include "sha256_mb.h"
34
35 typedef uint32_t DigestSHA256[SHA256_DIGEST_NWORDS];
36
37 #define MSGS 7
38 #define NUM_JOBS 1000
39
40 #define PSEUDO_RANDOM_NUM(seed) ((seed) * 5 + ((seed) * (seed)) / 64) % MSGS
41
42 static uint8_t msg1[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
43 static uint8_t msg2[] = "0123456789:;<=>?@ABCDEFGHIJKLMNO";
44 static uint8_t msg3[] =
45 "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
46 "0123456789:;<";
47 static uint8_t msg4[] =
48 "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
49 "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" "0123456789:;<=>?@ABCDEFGHIJKLMNOPQR";
50 static uint8_t msg5[] =
51 "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
52 "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
53 "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" "0123456789:;<=>?";
54 static uint8_t msg6[] =
55 "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
56 "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
57 "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
58 "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTU";
59 static uint8_t msg7[] = "";
60
61 static DigestSHA256 expResultDigest1 = { 0x248D6A61, 0xD20638B8, 0xE5C02693, 0x0C3E6039,
62 0xA33CE459, 0x64FF2167, 0xF6ECEDD4, 0x19DB06C1
63 };
64
65 static DigestSHA256 expResultDigest2 = { 0xD9C2E699, 0x586B948F, 0x4022C799, 0x4FFE14C6,
66 0x3A4E8E31, 0x2EE2AEE1, 0xEBE51BED, 0x85705CFD
67 };
68
69 static DigestSHA256 expResultDigest3 = { 0xE3057651, 0x81295681, 0x7ECF1791, 0xFF9A1619,
70 0xB2BC5CAD, 0x2AC00018, 0x92AE489C, 0x48DD10B3
71 };
72
73 static DigestSHA256 expResultDigest4 = { 0x0307DAA3, 0x7130A140, 0x270790F9, 0x95B71407,
74 0x8EC752A6, 0x084EC1F3, 0xBD873D79, 0x3FF78383
75 };
76
77 static DigestSHA256 expResultDigest5 = { 0x679312F7, 0x2E18D599, 0x5F51BDC6, 0x4ED56AFD,
78 0x9B5704D3, 0x4387E11C, 0xC2331089, 0x2CD45DAA
79 };
80
81 static DigestSHA256 expResultDigest6 = { 0x8B1767E9, 0x7BA7BBE5, 0xF9A6E8D9, 0x9996904F,
82 0x3AF6562E, 0xA58AF438, 0x5D8D584B, 0x81C808CE
83 };
84
85 static DigestSHA256 expResultDigest7 = { 0xE3B0C442, 0x98FC1C14, 0x9AFBF4C8, 0x996FB924,
86 0x27AE41E4, 0x649B934C, 0xA495991B, 0x7852B855
87 };
88
89 static uint8_t *msgs[MSGS] = { msg1, msg2, msg3, msg4, msg5, msg6, msg7 };
90
91 static uint32_t *expResultDigest[MSGS] = {
92 expResultDigest1, expResultDigest2, expResultDigest3,
93 expResultDigest4, expResultDigest5, expResultDigest6,
94 expResultDigest7
95 };
96
97 int main(void)
98 {
99 SHA256_HASH_CTX_MGR *mgr = NULL;
100 SHA256_HASH_CTX ctxpool[NUM_JOBS], *ctx = NULL;
101 uint32_t i, j, k, t, checked = 0;
102 uint32_t *good;
103
104 posix_memalign((void *)&mgr, 16, sizeof(SHA256_HASH_CTX_MGR));
105 sha256_ctx_mgr_init(mgr);
106
107 // Init contexts before first use
108 for (i = 0; i < MSGS; i++) {
109 hash_ctx_init(&ctxpool[i]);
110 ctxpool[i].user_data = (void *)((uint64_t) i);
111 }
112
113 for (i = 0; i < MSGS; i++) {
114 ctx = sha256_ctx_mgr_submit(mgr,
115 &ctxpool[i],
116 msgs[i], strlen((char *)msgs[i]), HASH_ENTIRE);
117
118 if (ctx) {
119 t = (unsigned long)(ctx->user_data);
120 good = expResultDigest[t];
121 checked++;
122 for (j = 0; j < SHA256_DIGEST_NWORDS; j++) {
123 if (good[j] != ctxpool[t].job.result_digest[j]) {
124 printf("Test %d, digest %d is %08X, should be %08X\n",
125 t, j, ctxpool[t].job.result_digest[j], good[j]);
126 return -1;
127 }
128 }
129
130 if (ctx->error) {
131 printf("Something bad happened during the submit."
132 " Error code: %d", ctx->error);
133 return -1;
134 }
135
136 }
137 }
138
139 while (1) {
140 ctx = sha256_ctx_mgr_flush(mgr);
141
142 if (ctx) {
143 t = (unsigned long)(ctx->user_data);
144 good = expResultDigest[t];
145 checked++;
146 for (j = 0; j < SHA256_DIGEST_NWORDS; j++) {
147 if (good[j] != ctxpool[t].job.result_digest[j]) {
148 printf("Test %d, digest %d is %08X, should be %08X\n",
149 t, j, ctxpool[t].job.result_digest[j], good[j]);
150 return -1;
151 }
152 }
153
154 if (ctx->error) {
155 printf("Something bad happened during the submit."
156 " Error code: %d", ctx->error);
157 return -1;
158 }
159 } else {
160 break;
161 }
162 }
163
164 // do larger test in pseudo-random order
165
166 // Init contexts before first use
167 for (i = 0; i < NUM_JOBS; i++) {
168 hash_ctx_init(&ctxpool[i]);
169 ctxpool[i].user_data = (void *)((uint64_t) i);
170 }
171
172 checked = 0;
173 for (i = 0; i < NUM_JOBS; i++) {
174 j = PSEUDO_RANDOM_NUM(i);
175 ctx = sha256_ctx_mgr_submit(mgr,
176 &ctxpool[i],
177 msgs[j], strlen((char *)msgs[j]), HASH_ENTIRE);
178 if (ctx) {
179 t = (unsigned long)(ctx->user_data);
180 k = PSEUDO_RANDOM_NUM(t);
181 good = expResultDigest[k];
182 checked++;
183 for (j = 0; j < SHA256_DIGEST_NWORDS; j++) {
184 if (good[j] != ctxpool[t].job.result_digest[j]) {
185 printf("Test %d, digest %d is %08X, should be %08X\n",
186 t, j, ctxpool[t].job.result_digest[j], good[j]);
187 return -1;
188 }
189 }
190
191 if (ctx->error) {
192 printf("Something bad happened during the"
193 " submit. Error code: %d", ctx->error);
194 return -1;
195 }
196
197 t = (unsigned long)(ctx->user_data);
198 k = PSEUDO_RANDOM_NUM(t);
199 }
200 }
201 while (1) {
202 ctx = sha256_ctx_mgr_flush(mgr);
203
204 if (ctx) {
205 t = (unsigned long)(ctx->user_data);
206 k = PSEUDO_RANDOM_NUM(t);
207 good = expResultDigest[k];
208 checked++;
209 for (j = 0; j < SHA256_DIGEST_NWORDS; j++) {
210 if (good[j] != ctxpool[t].job.result_digest[j]) {
211 printf("Test %d, digest %d is %08X, should be %08X\n",
212 t, j, ctxpool[t].job.result_digest[j], good[j]);
213 return -1;
214 }
215 }
216
217 if (ctx->error) {
218 printf("Something bad happened during the submit."
219 " Error code: %d", ctx->error);
220 return -1;
221 }
222 } else {
223 break;
224 }
225 }
226
227 if (checked != NUM_JOBS) {
228 printf("only tested %d rather than %d\n", checked, NUM_JOBS);
229 return -1;
230 }
231
232 printf(" multibinary_sha256 test: Pass\n");
233
234 return 0;
235 }