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