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 "mh_sha256.h" 33 #include "test.h" 34 35 #ifndef GT_L3_CACHE 36 #define GT_L3_CACHE 32 * 1024 * 1024 /* some number > last level cache */ 37 #endif 38 39 #if !defined(COLD_TEST) && !defined(TEST_CUSTOM) 40 // Cached test, loop many times over small dataset 41 #define TEST_LEN 16 * 1024 42 #define TEST_LOOPS 20000 43 #define TEST_TYPE_STR "_warm" 44 #elif defined(COLD_TEST) 45 // Uncached test. Pull from large mem base. 46 #define TEST_LEN (GT_L3_CACHE) 47 #define TEST_LOOPS 100 48 #define TEST_TYPE_STR "_cold" 49 #endif 50 51 #ifndef TEST_SEED 52 #define TEST_SEED 0x1234 53 #endif 54 #define TEST_MEM TEST_LEN 55 56 #define str(s) #s 57 #define xstr(s) str(s) 58 59 #define _FUNC_TOKEN(func, type) func##type 60 #define FUNC_TOKEN(func, type) _FUNC_TOKEN(func, type) 61 62 #ifndef MH_SHA256_FUNC_TYPE 63 #define MH_SHA256_FUNC_TYPE 64 #endif 65 66 #define TEST_UPDATE_FUNCTION FUNC_TOKEN(isal_mh_sha256_update, MH_SHA256_FUNC_TYPE) 67 #define TEST_FINAL_FUNCTION FUNC_TOKEN(isal_mh_sha256_finalize, MH_SHA256_FUNC_TYPE) 68 69 #define CHECK_RETURN(state) \ 70 do { \ 71 if ((state) != ISAL_MH_SHA256_CTX_ERROR_NONE) { \ 72 printf("The mh_sha256 function is failed.\n"); \ 73 goto exit; \ 74 } \ 75 } while (0) 76 77 // Generates pseudo-random data 78 void 79 rand_buffer(uint8_t *buf, long buffer_size) 80 { 81 long i; 82 for (i = 0; i < buffer_size; i++) 83 buf[i] = rand(); 84 } 85 86 void 87 dump(char *buf, int len) 88 { 89 int i; 90 for (i = 0; i < len;) { 91 printf(" %2x", 0xff & buf[i++]); 92 if (i % 32 == 0) 93 printf("\n"); 94 } 95 if (i % 32 != 0) 96 printf("\n"); 97 } 98 99 int 100 compare_digests(uint32_t hash_base[ISAL_SHA256_DIGEST_WORDS], 101 uint32_t hash_test[ISAL_SHA256_DIGEST_WORDS]) 102 { 103 int i; 104 int mh_sha256_fail = 0; 105 106 for (i = 0; i < ISAL_SHA256_DIGEST_WORDS; i++) { 107 if (hash_test[i] != hash_base[i]) 108 mh_sha256_fail++; 109 } 110 111 if (mh_sha256_fail) { 112 printf("mh_sha256 fail test\n"); 113 printf("base: "); 114 dump((char *) hash_base, 32); 115 printf("ref: "); 116 dump((char *) hash_test, 32); 117 } 118 119 return mh_sha256_fail; 120 } 121 122 int 123 main(int argc, char *argv[]) 124 { 125 #ifndef FIPS_MODE 126 int i, fail = -1; 127 uint32_t hash_test[ISAL_SHA256_DIGEST_WORDS], hash_base[ISAL_SHA256_DIGEST_WORDS]; 128 uint8_t *buff = NULL; 129 struct isal_mh_sha256_ctx *update_ctx_test = NULL, *update_ctx_base = NULL; 130 struct perf start, stop; 131 132 printf(xstr(TEST_UPDATE_FUNCTION) "_perf:\n"); 133 134 buff = malloc(TEST_LEN); 135 update_ctx_test = malloc(sizeof(*update_ctx_test)); 136 update_ctx_base = malloc(sizeof(*update_ctx_base)); 137 138 if (buff == NULL || update_ctx_base == NULL || update_ctx_test == NULL) { 139 printf("malloc failed test aborted\n"); 140 goto exit; 141 } 142 // Rand test1 143 rand_buffer(buff, TEST_LEN); 144 145 // mh_sha256 base version 146 CHECK_RETURN(isal_mh_sha256_init(update_ctx_base)); 147 CHECK_RETURN(mh_sha256_update_base(update_ctx_base, buff, TEST_LEN)); 148 CHECK_RETURN(mh_sha256_finalize_base(update_ctx_base, hash_base)); 149 150 perf_start(&start); 151 for (i = 0; i < TEST_LOOPS / 10; i++) { 152 isal_mh_sha256_init(update_ctx_base); 153 mh_sha256_update_base(update_ctx_base, buff, TEST_LEN); 154 mh_sha256_finalize_base(update_ctx_base, hash_base); 155 } 156 perf_stop(&stop); 157 printf("mh_sha256_update_base" TEST_TYPE_STR ": "); 158 perf_print(stop, start, (long long) TEST_MEM * i); 159 160 // Update feature test 161 CHECK_RETURN(isal_mh_sha256_init(update_ctx_test)); 162 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx_test, buff, TEST_LEN)); 163 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx_test, hash_test)); 164 165 perf_start(&start); 166 for (i = 0; i < TEST_LOOPS; i++) { 167 isal_mh_sha256_init(update_ctx_test); 168 TEST_UPDATE_FUNCTION(update_ctx_test, buff, TEST_LEN); 169 TEST_FINAL_FUNCTION(update_ctx_test, hash_test); 170 } 171 perf_stop(&stop); 172 printf(xstr(TEST_UPDATE_FUNCTION) TEST_TYPE_STR ": "); 173 perf_print(stop, start, (long long) TEST_MEM * i); 174 175 // Check results 176 fail = compare_digests(hash_base, hash_test); 177 178 if (fail) { 179 printf("Fail size=%d\n", TEST_LEN); 180 printf("Test failed function test%d\n", fail); 181 } else 182 printf("Pass func check\n"); 183 184 exit: 185 free(buff); 186 free(update_ctx_test); 187 free(update_ctx_base); 188 189 return fail; 190 #else 191 printf("Not Executed\n"); 192 return 0; 193 #endif /* FIPS_MODE */ 194 } 195