]>
git.proxmox.com Git - ceph.git/blob - 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.
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 "endian_helper.h"
34 #include <openssl/evp.h>
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)
42 /* Reference digest global to reduce stack usage */
43 static uint8_t digest_ref_upd
[4 * SM3_DIGEST_NWORDS
];
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
];
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");
68 sm3_ctx_mgr_init(mgr
);
70 printf("sm3_large_test\n");
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");
79 hash_ctx_init(&ctxpool
[i
]);
80 ctxpool
[i
].user_data
= (void *)&udata
[i
];
83 //Openssl SM3 update test
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
);
90 EVP_DigestFinal_ex(md_ctx
, digest_ref_upd
, &md_len
);
91 EVP_MD_CTX_free(md_ctx
);
94 for (i
= 0; i
< TEST_BUFS
; i
++) {
95 struct user_data
*u
= (struct user_data
*)ctxpool
[i
].user_data
;
100 printf("Starting updates\n");
101 int highest_pool_idx
= 0;
102 ctx
= &ctxpool
[highest_pool_idx
++];
104 int len
= UPDATE_SIZE
;
105 int update_type
= HASH_UPDATE
;
106 struct user_data
*u
= (struct user_data
*)ctx
->user_data
;
109 if (u
->processed
== 0)
110 update_type
= HASH_FIRST
;
112 else if (hash_ctx_complete(ctx
)) {
113 if (highest_pool_idx
< TEST_BUFS
)
114 ctx
= &ctxpool
[highest_pool_idx
++];
116 ctx
= sm3_ctx_mgr_flush(mgr
);
118 } else if (u
->processed
>= (LEN_TOTAL
- UPDATE_SIZE
)) {
119 len
= (LEN_TOTAL
- u
->processed
);
120 update_type
= HASH_LAST
;
123 ctx
= sm3_ctx_mgr_submit(mgr
, ctx
, bufs
[idx
], len
, update_type
);
126 if (highest_pool_idx
< TEST_BUFS
)
127 ctx
= &ctxpool
[highest_pool_idx
++];
129 ctx
= sm3_ctx_mgr_flush(mgr
);
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
]);
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
]));
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
])) {
158 printf("Test failed SM3_hash_large check %d\n", fail
);
160 printf(" SM3_hash_large_test: Pass\n");