]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
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 | #include "endian_helper.h" | |
34 | #include <openssl/sha.h> | |
35 | #define TEST_LEN (1024*1024ull) //1M | |
36 | #define TEST_BUFS SHA256_MIN_LANES | |
37 | #define ROTATION_TIMES 10000 //total length processing = TEST_LEN * ROTATION_TIMES | |
38 | #define UPDATE_SIZE (13*SHA256_BLOCK_SIZE) | |
39 | #define LEN_TOTAL (TEST_LEN * ROTATION_TIMES) | |
40 | ||
41 | /* Reference digest global to reduce stack usage */ | |
42 | static uint8_t digest_ref_upd[4 * SHA256_DIGEST_NWORDS]; | |
43 | ||
44 | struct user_data { | |
45 | int idx; | |
46 | uint64_t processed; | |
47 | }; | |
48 | ||
49 | int main(void) | |
50 | { | |
51 | SHA256_CTX o_ctx; //openSSL | |
52 | SHA256_HASH_CTX_MGR *mgr = NULL; | |
53 | SHA256_HASH_CTX ctxpool[TEST_BUFS], *ctx = NULL; | |
54 | uint32_t i, j, k, fail = 0; | |
55 | unsigned char *bufs[TEST_BUFS]; | |
56 | struct user_data udata[TEST_BUFS]; | |
57 | int ret; | |
58 | ||
59 | ret = posix_memalign((void *)&mgr, 16, sizeof(SHA256_HASH_CTX_MGR)); | |
60 | if ((ret != 0) || (mgr == NULL)) { | |
61 | printf("posix_memalign failed test aborted\n"); | |
62 | return 1; | |
63 | } | |
64 | ||
65 | sha256_ctx_mgr_init(mgr); | |
66 | ||
67 | printf("sha256_large_test\n"); | |
68 | ||
69 | // Init ctx contents | |
70 | for (i = 0; i < TEST_BUFS; i++) { | |
71 | bufs[i] = (unsigned char *)calloc((size_t)TEST_LEN, 1); | |
72 | if (bufs[i] == NULL) { | |
73 | printf("malloc failed test aborted\n"); | |
74 | return 1; | |
75 | } | |
76 | hash_ctx_init(&ctxpool[i]); | |
77 | ctxpool[i].user_data = (void *)&udata[i]; | |
78 | } | |
79 | ||
80 | //Openssl SHA256 update test | |
81 | SHA256_Init(&o_ctx); | |
82 | for (k = 0; k < ROTATION_TIMES; k++) { | |
83 | SHA256_Update(&o_ctx, bufs[k % TEST_BUFS], TEST_LEN); | |
84 | } | |
85 | SHA256_Final(digest_ref_upd, &o_ctx); | |
86 | ||
87 | // Initialize pool | |
88 | for (i = 0; i < TEST_BUFS; i++) { | |
89 | struct user_data *u = (struct user_data *)ctxpool[i].user_data; | |
90 | u->idx = i; | |
91 | u->processed = 0; | |
92 | } | |
93 | ||
94 | printf("Starting updates\n"); | |
95 | int highest_pool_idx = 0; | |
96 | ctx = &ctxpool[highest_pool_idx++]; | |
97 | while (ctx) { | |
98 | int len = UPDATE_SIZE; | |
99 | int update_type = HASH_UPDATE; | |
100 | struct user_data *u = (struct user_data *)ctx->user_data; | |
101 | int idx = u->idx; | |
102 | ||
103 | if (u->processed == 0) | |
104 | update_type = HASH_FIRST; | |
105 | ||
106 | else if (hash_ctx_complete(ctx)) { | |
107 | if (highest_pool_idx < TEST_BUFS) | |
108 | ctx = &ctxpool[highest_pool_idx++]; | |
109 | else | |
110 | ctx = sha256_ctx_mgr_flush(mgr); | |
111 | continue; | |
112 | } else if (u->processed >= (LEN_TOTAL - UPDATE_SIZE)) { | |
113 | len = (LEN_TOTAL - u->processed); | |
114 | update_type = HASH_LAST; | |
115 | } | |
116 | u->processed += len; | |
117 | ctx = sha256_ctx_mgr_submit(mgr, ctx, bufs[idx], len, update_type); | |
118 | ||
119 | if (NULL == ctx) { | |
120 | if (highest_pool_idx < TEST_BUFS) | |
121 | ctx = &ctxpool[highest_pool_idx++]; | |
122 | else | |
123 | ctx = sha256_ctx_mgr_flush(mgr); | |
124 | } | |
125 | } | |
126 | ||
127 | printf("multibuffer SHA256 digest: \n"); | |
128 | for (i = 0; i < TEST_BUFS; i++) { | |
129 | printf("Total processing size of buf[%d] is %ld \n", i, | |
130 | ctxpool[i].total_length); | |
131 | for (j = 0; j < SHA256_DIGEST_NWORDS; j++) { | |
132 | printf("digest%d : %08X\n", j, ctxpool[i].job.result_digest[j]); | |
133 | } | |
134 | } | |
135 | printf("\n"); | |
136 | ||
137 | printf("openssl SHA256 update digest: \n"); | |
138 | for (i = 0; i < SHA256_DIGEST_NWORDS; i++) | |
139 | printf("%08X - ", to_be32(((uint32_t *) digest_ref_upd)[i])); | |
140 | printf("\n"); | |
141 | ||
142 | for (i = 0; i < TEST_BUFS; i++) { | |
143 | for (j = 0; j < SHA256_DIGEST_NWORDS; j++) { | |
144 | if (ctxpool[i].job.result_digest[j] != | |
145 | to_be32(((uint32_t *) digest_ref_upd)[j])) { | |
146 | fail++; | |
147 | } | |
148 | } | |
149 | } | |
150 | ||
151 | if (fail) | |
152 | printf("Test failed SHA256 hash large file check %d\n", fail); | |
153 | else | |
154 | printf(" SHA256_hash_large_test: Pass\n"); | |
155 | return fail; | |
156 | } |