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