xref: /isa-l_crypto/mh_sha256/mh_sha256_test.c (revision d28f1034f736e3eb791c3cf6bff3e2fa81fb5331)
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 "isal_crypto_api.h"
33 #include "mh_sha256.h"
34 
35 #define TEST_LEN  16 * 1024
36 #define TEST_SIZE 8 * 1024
37 #define TEST_MEM  TEST_LEN
38 #ifndef TEST_SEED
39 #define TEST_SEED 0x1234
40 #endif
41 
42 #define str(s)  #s
43 #define xstr(s) str(s)
44 
45 #define _FUNC_TOKEN(func, type) func##type
46 #define FUNC_TOKEN(func, type)  _FUNC_TOKEN(func, type)
47 
48 #ifndef MH_SHA256_FUNC_TYPE
49 #define MH_SHA256_FUNC_TYPE
50 #endif
51 
52 #define TEST_UPDATE_FUNCTION FUNC_TOKEN(isal_mh_sha256_update, MH_SHA256_FUNC_TYPE)
53 #define TEST_FINAL_FUNCTION  FUNC_TOKEN(isal_mh_sha256_finalize, MH_SHA256_FUNC_TYPE)
54 
55 #define CHECK_RETURN(state)                                                                        \
56         do {                                                                                       \
57                 if ((state) != ISAL_CRYPTO_ERR_NONE) {                                             \
58                         printf("The mh_sha256 function is failed.\n");                             \
59                         return 1;                                                                  \
60                 }                                                                                  \
61         } while (0)
62 
63 extern void
64 mh_sha256_ref(const void *buffer, uint32_t len, uint32_t *mh_sha256_digest);
65 #define MH_SHA256_REF mh_sha256_ref
66 
67 // Generates pseudo-random data
68 void
69 rand_buffer(uint8_t *buf, long buffer_size)
70 {
71         long i;
72         for (i = 0; i < buffer_size; i++)
73                 buf[i] = rand();
74 }
75 
76 void
77 dump(char *buf, int len)
78 {
79         int i;
80         for (i = 0; i < len;) {
81                 printf(" %2x", 0xff & buf[i++]);
82                 if (i % 32 == 0)
83                         printf("\n");
84         }
85         if (i % 32 != 0)
86                 printf("\n");
87 }
88 
89 int
90 compare_digests(uint32_t hash_ref[ISAL_SHA256_DIGEST_WORDS],
91                 uint32_t hash_test[ISAL_SHA256_DIGEST_WORDS])
92 {
93         int i;
94         int mh_sha256_fail = 0;
95 
96         for (i = 0; i < ISAL_SHA256_DIGEST_WORDS; i++) {
97                 if (hash_test[i] != hash_ref[i])
98                         mh_sha256_fail++;
99         }
100 
101         if (mh_sha256_fail) {
102                 printf("mh_sha256 fail test\n");
103                 printf("ref: ");
104                 dump((char *) hash_ref, 32);
105                 printf("test: ");
106                 dump((char *) hash_test, 32);
107         }
108 
109         return mh_sha256_fail;
110 }
111 
112 int
113 main(int argc, char *argv[])
114 {
115         int fail = 0;
116 #ifndef FIPS_MODE
117         uint32_t hash_test[ISAL_SHA256_DIGEST_WORDS], hash_ref[ISAL_SHA256_DIGEST_WORDS];
118         uint8_t *buff = NULL;
119         int size, offset;
120         struct isal_mh_sha256_ctx *update_ctx = NULL;
121 
122         printf(xstr(TEST_UPDATE_FUNCTION) "_test:\n");
123 
124         srand(TEST_SEED);
125 
126         buff = malloc(TEST_LEN);
127         update_ctx = malloc(sizeof(*update_ctx));
128 
129         if (buff == NULL || update_ctx == NULL) {
130                 printf("malloc failed test aborted\n");
131                 fail++;
132                 goto end;
133         }
134         // Rand test1
135         rand_buffer(buff, TEST_LEN);
136 
137         MH_SHA256_REF(buff, TEST_LEN, hash_ref);
138         CHECK_RETURN(isal_mh_sha256_init(update_ctx));
139         CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff, TEST_LEN));
140         CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test));
141 
142         fail = compare_digests(hash_ref, hash_test);
143 
144         if (fail) {
145                 printf("fail rand1 test\n");
146                 goto end;
147         } else
148                 putchar('.');
149 
150         // Test various size messages
151         for (size = TEST_LEN; size >= 0; size--) {
152 
153                 // Fill with rand data
154                 rand_buffer(buff, size);
155 
156                 MH_SHA256_REF(buff, size, hash_ref);
157                 CHECK_RETURN(isal_mh_sha256_init(update_ctx));
158                 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff, size));
159                 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test));
160 
161                 fail = compare_digests(hash_ref, hash_test);
162 
163                 if (fail) {
164                         printf("Fail size=%d\n", size);
165                         goto end;
166                 }
167 
168                 if ((size & 0xff) == 0) {
169                         putchar('.');
170                         fflush(0);
171                 }
172         }
173 
174         // Test various buffer offsets and sizes
175         printf("offset tests");
176         for (size = TEST_LEN - 256; size > 256; size -= 11) {
177                 for (offset = 0; offset < 256; offset++) {
178                         MH_SHA256_REF(buff + offset, size, hash_ref);
179 
180                         CHECK_RETURN(isal_mh_sha256_init(update_ctx));
181                         CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff + offset, size));
182                         CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test));
183 
184                         fail = compare_digests(hash_ref, hash_test);
185 
186                         if (fail) {
187                                 printf("Fail size=%d\n", size);
188                                 goto end;
189                         }
190                 }
191                 if ((size & 0xf) == 0) {
192                         putchar('.');
193                         fflush(0);
194                 }
195         }
196 
197         // Run efence tests
198         printf("efence tests");
199         for (size = TEST_SIZE; size > 0; size--) {
200                 offset = TEST_LEN - size;
201 
202                 MH_SHA256_REF(buff + offset, size, hash_ref);
203 
204                 CHECK_RETURN(isal_mh_sha256_init(update_ctx));
205                 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff + offset, size));
206                 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test));
207 
208                 fail = compare_digests(hash_ref, hash_test);
209 
210                 if (fail) {
211                         printf("Fail size=%d\n", size);
212                         goto end;
213                 }
214 
215                 if ((size & 0xf) == 0) {
216                         putchar('.');
217                         fflush(0);
218                 }
219         }
220 
221 end:
222         if (buff != NULL)
223                 free(buff);
224         if (update_ctx != NULL)
225                 free(update_ctx);
226 
227         printf(xstr(TEST_UPDATE_FUNCTION) "_test:");
228         printf(" %s\n", fail == 0 ? "Pass" : "Fail");
229 #else
230         printf("Not Executed\n");
231 #endif /* FIPS_MODE */
232         return fail;
233 }
234