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