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) != 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[SHA256_DIGEST_WORDS], uint32_t hash_test[SHA256_DIGEST_WORDS]) 90 { 91 int i; 92 int mh_sha256_fail = 0; 93 94 for (i = 0; i < SHA256_DIGEST_WORDS; i++) { 95 if (hash_test[i] != hash_ref[i]) 96 mh_sha256_fail++; 97 } 98 99 if (mh_sha256_fail) { 100 printf("mh_sha256 fail test\n"); 101 printf("ref: "); 102 dump((char *) hash_ref, 32); 103 printf("test: "); 104 dump((char *) hash_test, 32); 105 } 106 107 return mh_sha256_fail; 108 } 109 110 int 111 main(int argc, char *argv[]) 112 { 113 int fail = 0; 114 uint32_t hash_test[SHA256_DIGEST_WORDS], hash_ref[SHA256_DIGEST_WORDS]; 115 uint8_t *buff = NULL; 116 int size, offset; 117 struct mh_sha256_ctx *update_ctx = NULL; 118 119 printf(xstr(TEST_UPDATE_FUNCTION) "_test:\n"); 120 121 srand(TEST_SEED); 122 123 buff = malloc(TEST_LEN); 124 update_ctx = malloc(sizeof(*update_ctx)); 125 126 if (buff == NULL || update_ctx == NULL) { 127 printf("malloc failed test aborted\n"); 128 fail++; 129 goto end; 130 } 131 // Rand test1 132 rand_buffer(buff, TEST_LEN); 133 134 MH_SHA256_REF(buff, TEST_LEN, hash_ref); 135 CHECK_RETURN(mh_sha256_init(update_ctx)); 136 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff, TEST_LEN)); 137 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test)); 138 139 fail = compare_digests(hash_ref, hash_test); 140 141 if (fail) { 142 printf("fail rand1 test\n"); 143 goto end; 144 } else 145 putchar('.'); 146 147 // Test various size messages 148 for (size = TEST_LEN; size >= 0; size--) { 149 150 // Fill with rand data 151 rand_buffer(buff, size); 152 153 MH_SHA256_REF(buff, size, hash_ref); 154 CHECK_RETURN(mh_sha256_init(update_ctx)); 155 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff, size)); 156 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test)); 157 158 fail = compare_digests(hash_ref, hash_test); 159 160 if (fail) { 161 printf("Fail size=%d\n", size); 162 goto end; 163 } 164 165 if ((size & 0xff) == 0) { 166 putchar('.'); 167 fflush(0); 168 } 169 } 170 171 // Test various buffer offsets and sizes 172 printf("offset tests"); 173 for (size = TEST_LEN - 256; size > 256; size -= 11) { 174 for (offset = 0; offset < 256; offset++) { 175 MH_SHA256_REF(buff + offset, size, hash_ref); 176 177 CHECK_RETURN(mh_sha256_init(update_ctx)); 178 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff + offset, size)); 179 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test)); 180 181 fail = compare_digests(hash_ref, hash_test); 182 183 if (fail) { 184 printf("Fail size=%d\n", size); 185 goto end; 186 } 187 } 188 if ((size & 0xf) == 0) { 189 putchar('.'); 190 fflush(0); 191 } 192 } 193 194 // Run efence tests 195 printf("efence tests"); 196 for (size = TEST_SIZE; size > 0; size--) { 197 offset = TEST_LEN - size; 198 199 MH_SHA256_REF(buff + offset, size, hash_ref); 200 201 CHECK_RETURN(mh_sha256_init(update_ctx)); 202 CHECK_RETURN(TEST_UPDATE_FUNCTION(update_ctx, buff + offset, size)); 203 CHECK_RETURN(TEST_FINAL_FUNCTION(update_ctx, hash_test)); 204 205 fail = compare_digests(hash_ref, hash_test); 206 207 if (fail) { 208 printf("Fail size=%d\n", size); 209 goto end; 210 } 211 212 if ((size & 0xf) == 0) { 213 putchar('.'); 214 fflush(0); 215 } 216 } 217 218 end: 219 if (buff != NULL) 220 free(buff); 221 if (update_ctx != NULL) 222 free(update_ctx); 223 224 printf(xstr(TEST_UPDATE_FUNCTION) "_test:"); 225 printf(" %s\n", fail == 0 ? "Pass" : "Fail"); 226 227 return fail; 228 } 229