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 <string.h> 33 #include "sha256_mb.h" 34 35 #define TEST_LEN (1024 * 1024) 36 #define TEST_BUFS 100 37 #ifndef RANDOMS 38 #define RANDOMS 10 39 #endif 40 #ifndef TEST_SEED 41 #define TEST_SEED 0x1234 42 #endif 43 44 static uint32_t digest_ref[TEST_BUFS][SHA256_DIGEST_NWORDS]; 45 46 // Compare against reference function 47 extern void 48 sha256_ref(uint8_t *input_data, uint32_t *digest, uint32_t len); 49 50 // Generates pseudo-random data 51 void 52 rand_buffer(unsigned char *buf, const long buffer_size) 53 { 54 long i; 55 for (i = 0; i < buffer_size; i++) 56 buf[i] = rand(); 57 } 58 59 int 60 main(void) 61 { 62 SHA256_HASH_CTX_MGR *mgr = NULL; 63 SHA256_HASH_CTX ctxpool[TEST_BUFS]; 64 uint32_t i, j, fail = 0; 65 unsigned char *bufs[TEST_BUFS] = { 0 }; 66 uint32_t lens[TEST_BUFS]; 67 unsigned int jobs, t; 68 uint8_t *tmp_buf = NULL; 69 int ret; 70 71 printf("multibinary_sha256 test, %d sets of %dx%d max: ", RANDOMS, TEST_BUFS, TEST_LEN); 72 73 ret = posix_memalign((void *) &mgr, 16, sizeof(SHA256_HASH_CTX_MGR)); 74 if ((ret != 0) || (mgr == NULL)) { 75 printf("posix_memalign failed test aborted\n"); 76 return 1; 77 } 78 79 sha256_ctx_mgr_init(mgr); 80 81 srand(TEST_SEED); 82 83 for (i = 0; i < TEST_BUFS; i++) { 84 // Allocate and fill buffer 85 bufs[i] = (unsigned char *) malloc(TEST_LEN); 86 if (bufs[i] == NULL) { 87 printf("malloc failed test aborted\n"); 88 fail++; 89 goto end; 90 } 91 rand_buffer(bufs[i], TEST_LEN); 92 93 // Init ctx contexts 94 hash_ctx_init(&ctxpool[i]); 95 ctxpool[i].user_data = (void *) ((uint64_t) i); 96 97 // Run reference test 98 sha256_ref(bufs[i], digest_ref[i], TEST_LEN); 99 100 // Run sb_sha256 test 101 sha256_ctx_mgr_submit(mgr, &ctxpool[i], bufs[i], TEST_LEN, HASH_ENTIRE); 102 } 103 104 while (sha256_ctx_mgr_flush(mgr)) 105 ; 106 107 for (i = 0; i < TEST_BUFS; i++) { 108 for (j = 0; j < SHA256_DIGEST_NWORDS; j++) { 109 if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) { 110 fail++; 111 printf("Test%d fixed size, digest%d " 112 "fail 0x%08X <=> 0x%08X \n", 113 i, j, ctxpool[i].job.result_digest[j], digest_ref[i][j]); 114 } 115 } 116 } 117 118 if (fail) { 119 printf("Test failed function check %d\n", fail); 120 goto end; 121 } 122 // Run tests with random size and number of jobs 123 for (t = 0; t < RANDOMS; t++) { 124 jobs = rand() % (TEST_BUFS); 125 126 sha256_ctx_mgr_init(mgr); 127 128 for (i = 0; i < jobs; i++) { 129 // Use buffer with random len and contents 130 lens[i] = rand() % (TEST_LEN); 131 rand_buffer(bufs[i], lens[i]); 132 133 // Run reference test 134 sha256_ref(bufs[i], digest_ref[i], lens[i]); 135 136 // Run sha256_mb test 137 sha256_ctx_mgr_submit(mgr, &ctxpool[i], bufs[i], lens[i], HASH_ENTIRE); 138 } 139 140 while (sha256_ctx_mgr_flush(mgr)) 141 ; 142 143 for (i = 0; i < jobs; i++) { 144 for (j = 0; j < SHA256_DIGEST_NWORDS; j++) { 145 if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) { 146 fail++; 147 printf("Test%d, digest%d fail " 148 "0x%08X <=> 0x%08X\n", 149 i, j, ctxpool[i].job.result_digest[j], 150 digest_ref[i][j]); 151 } 152 } 153 } 154 if (fail) { 155 printf("Test failed function check %d\n", fail); 156 goto end; 157 } 158 159 putchar('.'); 160 fflush(0); 161 } // random test t 162 163 // Test at the end of buffer 164 jobs = rand() % TEST_BUFS; 165 tmp_buf = (uint8_t *) malloc(sizeof(uint8_t) * jobs); 166 if (!tmp_buf) { 167 printf("malloc failed, end test aborted.\n"); 168 goto end; 169 } 170 171 rand_buffer(tmp_buf, jobs); 172 173 sha256_ctx_mgr_init(mgr); 174 175 for (i = 0; i < TEST_BUFS; i++) 176 free(bufs[i]); 177 178 // Extend to the end of allocated buffer to construct jobs 179 for (i = 0; i < jobs; i++) { 180 bufs[i] = (uint8_t *) &tmp_buf[i]; 181 lens[i] = jobs - i; 182 183 // Reference test 184 sha256_ref(bufs[i], digest_ref[i], lens[i]); 185 186 // sb_sha256 test 187 sha256_ctx_mgr_submit(mgr, &ctxpool[i], bufs[i], lens[i], HASH_ENTIRE); 188 } 189 // Clear bufs 190 memset(bufs, 0, sizeof(bufs)); 191 192 while (sha256_ctx_mgr_flush(mgr)) 193 ; 194 195 for (i = 0; i < jobs; i++) { 196 for (j = 0; j < SHA256_DIGEST_NWORDS; j++) { 197 if (ctxpool[i].job.result_digest[j] != digest_ref[i][j]) { 198 fail++; 199 printf("End test failed at offset %d - result: 0x%08X" 200 ", ref: 0x%08X\n", 201 i, ctxpool[i].job.result_digest[j], digest_ref[i][j]); 202 } 203 } 204 } 205 putchar('.'); 206 207 end: 208 for (i = 0; i < TEST_BUFS; i++) 209 free(bufs[i]); 210 free(tmp_buf); 211 aligned_free(mgr); 212 213 if (fail) 214 printf("Test failed function check %d\n", fail); 215 else 216 printf(" multibinary_sha256 rand: Pass\n"); 217 218 return fail; 219 } 220