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(mh_sha256_update, MH_SHA256_FUNC_TYPE) 67 #define TEST_FINAL_FUNCTION FUNC_TOKEN(mh_sha256_finalize, MH_SHA256_FUNC_TYPE) 68 69 #define CHECK_RETURN(state) do{ \ 70 if((state) != MH_SHA256_CTX_ERROR_NONE){ \ 71 printf("The mh_sha256 function is failed.\n"); \ 72 return 1; \ 73 } \ 74 }while(0) 75 76 // Generates pseudo-random data 77 void rand_buffer(uint8_t * buf, long buffer_size) 78 { 79 long i; 80 for (i = 0; i < buffer_size; i++) 81 buf[i] = rand(); 82 } 83 84 void dump(char *buf, int len) 85 { 86 int i; 87 for (i = 0; i < len;) { 88 printf(" %2x", 0xff & buf[i++]); 89 if (i % 32 == 0) 90 printf("\n"); 91 } 92 if (i % 32 != 0) 93 printf("\n"); 94 } 95 96 int compare_digests(uint32_t hash_base[SHA256_DIGEST_WORDS], 97 uint32_t hash_test[SHA256_DIGEST_WORDS]) 98 { 99 int i; 100 int mh_sha256_fail = 0; 101 102 for (i = 0; i < SHA256_DIGEST_WORDS; i++) { 103 if (hash_test[i] != hash_base[i]) 104 mh_sha256_fail++; 105 } 106 107 if (mh_sha256_fail) { 108 printf("mh_sha256 fail test\n"); 109 printf("base: "); 110 dump((char *)hash_base, 32); 111 printf("ref: "); 112 dump((char *)hash_test, 32); 113 } 114 115 return mh_sha256_fail; 116 } 117 118 int main(int argc, char *argv[]) 119 { 120 int i, fail = 0; 121 uint32_t hash_test[SHA256_DIGEST_WORDS], hash_base[SHA256_DIGEST_WORDS]; 122 uint8_t *buff = NULL; 123 struct mh_sha256_ctx *update_ctx_test = NULL, *update_ctx_base = NULL; 124 struct perf start, stop; 125 126 printf(xstr(TEST_UPDATE_FUNCTION) "_perf:\n"); 127 128 buff = malloc(TEST_LEN); 129 update_ctx_test = malloc(sizeof(*update_ctx_test)); 130 update_ctx_base = malloc(sizeof(*update_ctx_base)); 131 132 if (buff == NULL || update_ctx_base == NULL || update_ctx_test == NULL) { 133 printf("malloc failed test aborted\n"); 134 return -1; 135 } 136 // Rand test1 137 rand_buffer(buff, TEST_LEN); 138 139 // mh_sha256 base version 140 mh_sha256_init(update_ctx_base); 141 mh_sha256_update_base(update_ctx_base, buff, TEST_LEN); 142 mh_sha256_finalize_base(update_ctx_base, hash_base); 143 144 perf_start(&start); 145 for (i = 0; i < TEST_LOOPS / 10; i++) { 146 mh_sha256_init(update_ctx_base); 147 mh_sha256_update_base(update_ctx_base, buff, TEST_LEN); 148 mh_sha256_finalize_base(update_ctx_base, hash_base); 149 } 150 perf_stop(&stop); 151 printf("mh_sha256_update_base" TEST_TYPE_STR ": "); 152 perf_print(stop, start, (long long)TEST_MEM * i); 153 154 //Update feature test 155 CHECK_RETURN(mh_sha256_init(update_ctx_test)); 156 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx_test, buff, TEST_LEN)); 157 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx_test, hash_test)); 158 159 perf_start(&start); 160 for (i = 0; i < TEST_LOOPS; i++) { 161 CHECK_RETURN(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_stop(&stop); 166 printf(xstr(TEST_UPDATE_FUNCTION) TEST_TYPE_STR ": "); 167 perf_print(stop, start, (long long)TEST_MEM * i); 168 169 // Check results 170 fail = compare_digests(hash_base, hash_test); 171 172 if (fail) { 173 printf("Fail size=%d\n", TEST_LEN); 174 printf("Test failed function test%d\n", fail); 175 } else 176 printf("Pass func check\n"); 177 178 return fail; 179 } 180