1 /**********************************************************************
2 Copyright(c) 2011-2016 Intel Corporation All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
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
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.
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 **********************************************************************/
33 #include "sha256_mb.h"
35 typedef uint32_t DigestSHA256
[SHA256_DIGEST_NWORDS
];
40 #define PSEUDO_RANDOM_NUM(seed) ((seed) * 5 + ((seed) * (seed)) / 64) % MSGS
42 static uint8_t msg1
[] = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
43 static uint8_t msg2
[] = "0123456789:;<=>?@ABCDEFGHIJKLMNO";
44 static uint8_t msg3
[] =
45 "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX" "0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX"
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
[] = "";
61 static DigestSHA256 expResultDigest1
= { 0x248D6A61, 0xD20638B8, 0xE5C02693, 0x0C3E6039,
62 0xA33CE459, 0x64FF2167, 0xF6ECEDD4, 0x19DB06C1
65 static DigestSHA256 expResultDigest2
= { 0xD9C2E699, 0x586B948F, 0x4022C799, 0x4FFE14C6,
66 0x3A4E8E31, 0x2EE2AEE1, 0xEBE51BED, 0x85705CFD
69 static DigestSHA256 expResultDigest3
= { 0xE3057651, 0x81295681, 0x7ECF1791, 0xFF9A1619,
70 0xB2BC5CAD, 0x2AC00018, 0x92AE489C, 0x48DD10B3
73 static DigestSHA256 expResultDigest4
= { 0x0307DAA3, 0x7130A140, 0x270790F9, 0x95B71407,
74 0x8EC752A6, 0x084EC1F3, 0xBD873D79, 0x3FF78383
77 static DigestSHA256 expResultDigest5
= { 0x679312F7, 0x2E18D599, 0x5F51BDC6, 0x4ED56AFD,
78 0x9B5704D3, 0x4387E11C, 0xC2331089, 0x2CD45DAA
81 static DigestSHA256 expResultDigest6
= { 0x8B1767E9, 0x7BA7BBE5, 0xF9A6E8D9, 0x9996904F,
82 0x3AF6562E, 0xA58AF438, 0x5D8D584B, 0x81C808CE
85 static DigestSHA256 expResultDigest7
= { 0xE3B0C442, 0x98FC1C14, 0x9AFBF4C8, 0x996FB924,
86 0x27AE41E4, 0x649B934C, 0xA495991B, 0x7852B855
89 static uint8_t *msgs
[MSGS
] = { msg1
, msg2
, msg3
, msg4
, msg5
, msg6
, msg7
};
91 static uint32_t *expResultDigest
[MSGS
] = {
92 expResultDigest1
, expResultDigest2
, expResultDigest3
,
93 expResultDigest4
, expResultDigest5
, expResultDigest6
,
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;
104 posix_memalign((void *)&mgr
, 16, sizeof(SHA256_HASH_CTX_MGR
));
105 sha256_ctx_mgr_init(mgr
);
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
);
113 for (i
= 0; i
< MSGS
; i
++) {
114 ctx
= sha256_ctx_mgr_submit(mgr
,
116 msgs
[i
], strlen((char *)msgs
[i
]), HASH_ENTIRE
);
119 t
= (unsigned long)(ctx
->user_data
);
120 good
= expResultDigest
[t
];
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
]);
131 printf("Something bad happened during the submit."
132 " Error code: %d", ctx
->error
);
140 ctx
= sha256_ctx_mgr_flush(mgr
);
143 t
= (unsigned long)(ctx
->user_data
);
144 good
= expResultDigest
[t
];
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
]);
155 printf("Something bad happened during the submit."
156 " Error code: %d", ctx
->error
);
164 // do larger test in pseudo-random order
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
);
173 for (i
= 0; i
< NUM_JOBS
; i
++) {
174 j
= PSEUDO_RANDOM_NUM(i
);
175 ctx
= sha256_ctx_mgr_submit(mgr
,
177 msgs
[j
], strlen((char *)msgs
[j
]), HASH_ENTIRE
);
179 t
= (unsigned long)(ctx
->user_data
);
180 k
= PSEUDO_RANDOM_NUM(t
);
181 good
= expResultDigest
[k
];
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
]);
192 printf("Something bad happened during the"
193 " submit. Error code: %d", ctx
->error
);
197 t
= (unsigned long)(ctx
->user_data
);
198 k
= PSEUDO_RANDOM_NUM(t
);
202 ctx
= sha256_ctx_mgr_flush(mgr
);
205 t
= (unsigned long)(ctx
->user_data
);
206 k
= PSEUDO_RANDOM_NUM(t
);
207 good
= expResultDigest
[k
];
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
]);
218 printf("Something bad happened during the submit."
219 " Error code: %d", ctx
->error
);
227 if (checked
!= NUM_JOBS
) {
228 printf("only tested %d rather than %d\n", checked
, NUM_JOBS
);
232 printf(" multibinary_sha256 test: Pass\n");