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(isal_mh_sha256_update, MH_SHA256_FUNC_TYPE) 52 #define TEST_FINAL_FUNCTION FUNC_TOKEN(isal_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 #ifndef FIPS_MODE 116 uint32_t hash_test[ISAL_SHA256_DIGEST_WORDS], hash_ref[ISAL_SHA256_DIGEST_WORDS]; 117 uint8_t *buff = NULL; 118 int size, offset; 119 struct mh_sha256_ctx *update_ctx = NULL; 120 121 printf(xstr(TEST_UPDATE_FUNCTION) "_test:\n"); 122 123 srand(TEST_SEED); 124 125 buff = malloc(TEST_LEN); 126 update_ctx = malloc(sizeof(*update_ctx)); 127 128 if (buff == NULL || update_ctx == NULL) { 129 printf("malloc failed test aborted\n"); 130 fail++; 131 goto end; 132 } 133 // Rand test1 134 rand_buffer(buff, TEST_LEN); 135 136 MH_SHA256_REF(buff, TEST_LEN, hash_ref); 137 CHECK_RETURN(isal_mh_sha256_init(update_ctx)); 138 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff, TEST_LEN)); 139 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test)); 140 141 fail = compare_digests(hash_ref, hash_test); 142 143 if (fail) { 144 printf("fail rand1 test\n"); 145 goto end; 146 } else 147 putchar('.'); 148 149 // Test various size messages 150 for (size = TEST_LEN; size >= 0; size--) { 151 152 // Fill with rand data 153 rand_buffer(buff, size); 154 155 MH_SHA256_REF(buff, size, hash_ref); 156 CHECK_RETURN(isal_mh_sha256_init(update_ctx)); 157 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff, size)); 158 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test)); 159 160 fail = compare_digests(hash_ref, hash_test); 161 162 if (fail) { 163 printf("Fail size=%d\n", size); 164 goto end; 165 } 166 167 if ((size & 0xff) == 0) { 168 putchar('.'); 169 fflush(0); 170 } 171 } 172 173 // Test various buffer offsets and sizes 174 printf("offset tests"); 175 for (size = TEST_LEN - 256; size > 256; size -= 11) { 176 for (offset = 0; offset < 256; offset++) { 177 MH_SHA256_REF(buff + offset, size, hash_ref); 178 179 CHECK_RETURN(isal_mh_sha256_init(update_ctx)); 180 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff + offset, size)); 181 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test)); 182 183 fail = compare_digests(hash_ref, hash_test); 184 185 if (fail) { 186 printf("Fail size=%d\n", size); 187 goto end; 188 } 189 } 190 if ((size & 0xf) == 0) { 191 putchar('.'); 192 fflush(0); 193 } 194 } 195 196 // Run efence tests 197 printf("efence tests"); 198 for (size = TEST_SIZE; size > 0; size--) { 199 offset = TEST_LEN - size; 200 201 MH_SHA256_REF(buff + offset, size, hash_ref); 202 203 CHECK_RETURN(isal_mh_sha256_init(update_ctx)); 204 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff + offset, size)); 205 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test)); 206 207 fail = compare_digests(hash_ref, hash_test); 208 209 if (fail) { 210 printf("Fail size=%d\n", size); 211 goto end; 212 } 213 214 if ((size & 0xf) == 0) { 215 putchar('.'); 216 fflush(0); 217 } 218 } 219 220 end: 221 if (buff != NULL) 222 free(buff); 223 if (update_ctx != NULL) 224 free(update_ctx); 225 226 printf(xstr(TEST_UPDATE_FUNCTION) "_test:"); 227 printf(" %s\n", fail == 0 ? "Pass" : "Fail"); 228 #else 229 printf("Not Executed\n"); 230 #endif /* FIPS_MODE */ 231 return fail; 232 } 233