]> git.proxmox.com Git - ceph.git/blob - ceph/src/crypto/isa-l/isa-l_crypto/tests/extended/sm3_mb_over_4GB_test.c
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / crypto / isa-l / isa-l_crypto / tests / extended / sm3_mb_over_4GB_test.c
1 /**********************************************************************
2 Copyright(c) 2011-2019 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 "sm3_mb.h"
33 #include "endian_helper.h"
34 #include <openssl/evp.h>
35
36 #define TEST_LEN (1024*1024ull) //1M
37 #define TEST_BUFS SM3_MAX_LANES
38 #define ROTATION_TIMES 10000 //total length processing = TEST_LEN * ROTATION_TIMES
39 #define UPDATE_SIZE (13*SM3_BLOCK_SIZE)
40 #define LEN_TOTAL (TEST_LEN * ROTATION_TIMES)
41
42 /* Reference digest global to reduce stack usage */
43 static uint8_t digest_ref_upd[4 * SM3_DIGEST_NWORDS];
44
45 struct user_data {
46 int idx;
47 uint64_t processed;
48 };
49
50 int main(void)
51 {
52 SM3_HASH_CTX_MGR *mgr = NULL;
53 SM3_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 EVP_MD_CTX *md_ctx;
58 const EVP_MD *md;
59 unsigned int md_len;
60 int ret;
61
62 ret = posix_memalign((void *)&mgr, 16, sizeof(SM3_HASH_CTX_MGR));
63 if ((ret != 0) || (mgr == NULL)) {
64 printf("posix_memalign failed test aborted\n");
65 return 1;
66 }
67
68 sm3_ctx_mgr_init(mgr);
69
70 printf("sm3_large_test\n");
71
72 // Init ctx contents
73 for (i = 0; i < TEST_BUFS; i++) {
74 bufs[i] = (unsigned char *)calloc((size_t)TEST_LEN, 1);
75 if (bufs[i] == NULL) {
76 printf("malloc failed test aborted\n");
77 return 1;
78 }
79 hash_ctx_init(&ctxpool[i]);
80 ctxpool[i].user_data = (void *)&udata[i];
81 }
82
83 //Openssl SM3 update test
84 md = EVP_sm3();
85 md_ctx = EVP_MD_CTX_new();
86 EVP_DigestInit_ex(md_ctx, md, NULL);
87 for (k = 0; k < ROTATION_TIMES; k++) {
88 EVP_DigestUpdate(md_ctx, bufs[k % TEST_BUFS], TEST_LEN);
89 }
90 EVP_DigestFinal_ex(md_ctx, digest_ref_upd, &md_len);
91 EVP_MD_CTX_free(md_ctx);
92
93 // Initialize pool
94 for (i = 0; i < TEST_BUFS; i++) {
95 struct user_data *u = (struct user_data *)ctxpool[i].user_data;
96 u->idx = i;
97 u->processed = 0;
98 }
99
100 printf("Starting updates\n");
101 int highest_pool_idx = 0;
102 ctx = &ctxpool[highest_pool_idx++];
103 while (ctx) {
104 int len = UPDATE_SIZE;
105 int update_type = HASH_UPDATE;
106 struct user_data *u = (struct user_data *)ctx->user_data;
107 int idx = u->idx;
108
109 if (u->processed == 0)
110 update_type = HASH_FIRST;
111
112 else if (hash_ctx_complete(ctx)) {
113 if (highest_pool_idx < TEST_BUFS)
114 ctx = &ctxpool[highest_pool_idx++];
115 else
116 ctx = sm3_ctx_mgr_flush(mgr);
117 continue;
118 } else if (u->processed >= (LEN_TOTAL - UPDATE_SIZE)) {
119 len = (LEN_TOTAL - u->processed);
120 update_type = HASH_LAST;
121 }
122 u->processed += len;
123 ctx = sm3_ctx_mgr_submit(mgr, ctx, bufs[idx], len, update_type);
124
125 if (NULL == ctx) {
126 if (highest_pool_idx < TEST_BUFS)
127 ctx = &ctxpool[highest_pool_idx++];
128 else
129 ctx = sm3_ctx_mgr_flush(mgr);
130 }
131 }
132
133 printf("multibuffer SM3 digest: \n");
134 for (i = 0; i < TEST_BUFS; i++) {
135 printf("Total processing size of buf[%d] is %ld \n", i,
136 ctxpool[i].total_length);
137 for (j = 0; j < SM3_DIGEST_NWORDS; j++) {
138 printf("digest%d : %08X\n", j, ctxpool[i].job.result_digest[j]);
139 }
140 }
141 printf("\n");
142
143 printf("openssl SM3 update digest: \n");
144 for (i = 0; i < SM3_DIGEST_NWORDS; i++)
145 printf("%08X - ", to_le32(((uint32_t *) digest_ref_upd)[i]));
146 printf("\n");
147
148 for (i = 0; i < TEST_BUFS; i++) {
149 for (j = 0; j < SM3_DIGEST_NWORDS; j++) {
150 if (ctxpool[i].job.result_digest[j] !=
151 to_le32(((uint32_t *) digest_ref_upd)[j])) {
152 fail++;
153 }
154 }
155 }
156
157 if (fail)
158 printf("Test failed SM3_hash_large check %d\n", fail);
159 else
160 printf(" SM3_hash_large_test: Pass\n");
161 return fail;
162 }