xref: /isa-l_crypto/sm3_mb/sm3_mb_rand_ssl_test.c (revision 6801b27bd9295090e36d2864110576f428efdf61)
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 #define ISAL_UNIT_TEST
30 #include <stdio.h>
31 #include <stdlib.h>
32 
33 #ifndef FIPS_MODE
34 #include "sm3_mb.h"
35 #include "endian_helper.h"
36 
37 #define TEST_LEN  (1024 * 1024)
38 #define TEST_BUFS 200
39 #ifndef RANDOMS
40 #define RANDOMS 10
41 #endif
42 #ifndef TEST_SEED
43 #define TEST_SEED 0x1234
44 #endif
45 
46 /* Reference digest global to reduce stack usage */
47 static uint8_t digest_ssl[TEST_BUFS][4 * ISAL_SM3_DIGEST_NWORDS];
48 
49 extern void
50 sm3_ossl(const unsigned char *buf, size_t length, unsigned char *digest);
51 
52 // Generates pseudo-random data
53 static void
rand_buffer(unsigned char * buf,const long buffer_size)54 rand_buffer(unsigned char *buf, const long buffer_size)
55 {
56         long i;
57         for (i = 0; i < buffer_size; i++)
58                 buf[i] = rand();
59 }
60 #endif /* !FIPS_MODE */
61 
62 int
main(void)63 main(void)
64 {
65 #ifndef FIPS_MODE
66         ISAL_SM3_HASH_CTX_MGR *mgr = NULL;
67         ISAL_SM3_HASH_CTX ctxpool[TEST_BUFS];
68         ISAL_SM3_HASH_CTX *ctx = NULL;
69         unsigned char *bufs[TEST_BUFS];
70         uint32_t i, j, fail = 0;
71         uint32_t lens[TEST_BUFS];
72         unsigned int jobs, t;
73         int ret;
74 
75         printf("multibinary_sm3 test, %d sets of %dx%d max: ", RANDOMS, TEST_BUFS, TEST_LEN);
76 
77         srand(TEST_SEED);
78 
79         ret = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SM3_HASH_CTX_MGR));
80         if ((ret != 0) || (mgr == NULL)) {
81                 printf("posix_memalign failed test aborted\n");
82                 return 1;
83         }
84 
85         isal_sm3_ctx_mgr_init(mgr);
86 
87         for (i = 0; i < TEST_BUFS; i++) {
88                 // Allocate and fill buffer
89                 bufs[i] = (unsigned char *) malloc(TEST_LEN);
90                 if (bufs[i] == NULL) {
91                         printf("malloc failed test aborted\n");
92                         return 1;
93                 }
94                 rand_buffer(bufs[i], TEST_LEN);
95 
96                 // Init ctx contents
97                 isal_hash_ctx_init(&ctxpool[i]);
98                 ctxpool[i].user_data = (void *) ((uint64_t) i);
99 
100                 // SSL test
101                 sm3_ossl(bufs[i], TEST_LEN, digest_ssl[i]);
102 
103                 // sb_sm3 test
104                 isal_sm3_ctx_mgr_submit(mgr, &ctxpool[i], &ctx, bufs[i], TEST_LEN,
105                                         ISAL_HASH_ENTIRE);
106         }
107 
108         while (isal_sm3_ctx_mgr_flush(mgr, &ctx) == 0)
109                 if (ctx == NULL)
110                         break;
111 
112         for (i = 0; i < TEST_BUFS; i++) {
113                 for (j = 0; j < ISAL_SM3_DIGEST_NWORDS; j++) {
114                         if (ctxpool[i].job.result_digest[j] !=
115                             to_le32(((uint32_t *) digest_ssl[i])[j])) {
116                                 fail++;
117                                 printf("Test%d, digest%d fail %08X <=> %08X\n", i, j,
118                                        ctxpool[i].job.result_digest[j],
119                                        to_le32(((uint32_t *) digest_ssl[i])[j]));
120                         }
121                 }
122         }
123         putchar('.');
124 
125         // Run tests with random size and number of jobs
126         for (t = 0; t < RANDOMS; t++) {
127                 jobs = rand() % (TEST_BUFS);
128 
129                 isal_sm3_ctx_mgr_init(mgr);
130 
131                 for (i = 0; i < jobs; i++) {
132                         // Random buffer with random len and contents
133                         lens[i] = rand() % (TEST_LEN);
134                         rand_buffer(bufs[i], lens[i]);
135 
136                         // Run SSL test
137                         sm3_ossl(bufs[i], lens[i], digest_ssl[i]);
138 
139                         // Run sb_sm3 test
140                         isal_sm3_ctx_mgr_submit(mgr, &ctxpool[i], &ctx, bufs[i], lens[i],
141                                                 ISAL_HASH_ENTIRE);
142                 }
143 
144                 while (isal_sm3_ctx_mgr_flush(mgr, &ctx) == 0)
145                         if (ctx == NULL)
146                                 break;
147 
148                 for (i = 0; i < jobs; i++) {
149                         for (j = 0; j < ISAL_SM3_DIGEST_NWORDS; j++) {
150                                 if (ctxpool[i].job.result_digest[j] !=
151                                     to_le32(((uint32_t *) digest_ssl[i])[j])) {
152                                         fail++;
153                                         printf("Test%d, digest%d fail %08X <=> %08X\n", i, j,
154                                                ctxpool[i].job.result_digest[j],
155                                                to_le32(((uint32_t *) digest_ssl[i])[j]));
156                                 }
157                         }
158                 }
159                 if (fail) {
160                         printf("Test failed function check %d\n", fail);
161                         return fail;
162                 }
163 
164                 putchar('.');
165                 fflush(0);
166         } // random test t
167 
168         if (fail)
169                 printf("Test failed function check %d\n", fail);
170         else
171                 printf(" multibinary_sm3_ssl rand: Pass\n");
172 
173         return fail;
174 #else
175         printf("Not Executed\n");
176         return 0;
177 #endif /* FIPS_MODE */
178 }
179