1 /********************************************************************** 2 Copyright(c) 2011-2019 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 #define ISAL_UNIT_TEST 30 #include <stdio.h> 31 #include <stdlib.h> 32 33 #ifndef FIPS_MODE 34 #include "sm3_mb.h" 35 #include "endian_helper.h" 36 37 #define TEST_LEN (1024 * 1024) 38 #define TEST_BUFS (ISAL_SM3_MAX_LANES - 1) 39 #ifndef TEST_SEED 40 #define TEST_SEED 0x1234 41 #endif 42 43 static uint8_t digest_ref[TEST_BUFS][4 * ISAL_SM3_DIGEST_NWORDS]; 44 45 // Compare against reference function 46 extern void 47 sm3_ossl(const unsigned char *buf, size_t length, unsigned char *digest); 48 49 // Generates pseudo-random data 50 void 51 rand_buffer(unsigned char *buf, const long buffer_size) 52 { 53 long i; 54 for (i = 0; i < buffer_size; i++) 55 buf[i] = rand(); 56 } 57 58 uint8_t 59 lens_print_and_check(ISAL_SM3_HASH_CTX_MGR *mgr) 60 { 61 static int32_t last_lens[ISAL_SM3_MAX_LANES] = { 0 }; 62 int32_t len; 63 uint8_t num_unchanged = 0; 64 int i; 65 for (i = 0; i < ISAL_SM3_MAX_LANES; i++) { 66 len = (int32_t) mgr->mgr.lens[i]; 67 // len[i] in mgr consists of byte_length<<4 | lane_index 68 len = (len >= 16) ? (len >> 4 << 6) : 0; 69 printf("\t%d", len); 70 if (last_lens[i] > 0 && last_lens[i] == len) 71 num_unchanged += 1; 72 last_lens[i] = len; 73 } 74 printf("\n"); 75 return num_unchanged; 76 } 77 #endif /* !FIPS_MODE */ 78 79 int 80 main(void) 81 { 82 #ifndef FIPS_MODE 83 ISAL_SM3_HASH_CTX_MGR *mgr = NULL; 84 ISAL_SM3_HASH_CTX ctxpool[TEST_BUFS]; 85 ISAL_SM3_HASH_CTX *ctx = NULL; 86 uint32_t i, j, fail = 0; 87 unsigned char *bufs[TEST_BUFS]; 88 uint32_t lens[TEST_BUFS]; 89 uint8_t num_ret, num_unchanged = 0; 90 int ret; 91 92 printf("sm3_mb flush test, %d buffers with %d length: \n", TEST_BUFS, TEST_LEN); 93 94 ret = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SM3_HASH_CTX_MGR)); 95 if ((ret != 0) || (mgr == NULL)) { 96 printf("posix_memalign failed test aborted\n"); 97 return 1; 98 } 99 100 if (isal_sm3_ctx_mgr_init(mgr) != 0) { 101 printf("init failed test aborted\n"); 102 fail++; 103 goto end; 104 } 105 106 srand(TEST_SEED); 107 108 for (i = 0; i < TEST_BUFS; i++) { 109 // Allocate and fill buffer 110 lens[i] = TEST_LEN / ISAL_SM3_MAX_LANES * (i + 1); 111 bufs[i] = (unsigned char *) malloc(lens[i]); 112 if (bufs[i] == NULL) { 113 printf("malloc failed test aborted\n"); 114 fail++; 115 goto end; 116 } 117 rand_buffer(bufs[i], lens[i]); 118 } 119 120 for (i = 0; i < TEST_BUFS; i++) { 121 // Init ctx contexts 122 isal_hash_ctx_init(&ctxpool[i]); 123 ctxpool[i].user_data = (void *) ((uint64_t) i); 124 125 // Run reference test 126 sm3_ossl(bufs[i], lens[i], digest_ref[i]); 127 128 // Run sb_sm3 test 129 if (isal_sm3_ctx_mgr_submit(mgr, &ctxpool[i], &ctx, bufs[i], lens[i], 130 ISAL_HASH_ENTIRE) != 0) { 131 printf("submit failed test aborted\n"); 132 fail++; 133 goto end; 134 } 135 } 136 137 printf("Changes of lens inside mgr:\n"); 138 lens_print_and_check(mgr); 139 while (isal_sm3_ctx_mgr_flush(mgr, &ctx) == 0) { 140 if (ctx == NULL) 141 break; 142 num_ret = lens_print_and_check(mgr); 143 num_unchanged = num_unchanged > num_ret ? num_unchanged : num_ret; 144 } 145 printf("Info of sm3_mb lens prints over\n"); 146 147 for (i = 0; i < TEST_BUFS; i++) { 148 for (j = 0; j < ISAL_SM3_DIGEST_NWORDS; j++) { 149 if (ctxpool[i].job.result_digest[j] != 150 to_le32(((uint32_t *) digest_ref[i])[j])) { 151 fail++; 152 printf("Test%d fixed size, digest%d " 153 "fail 0x%08X <=> 0x%08X \n", 154 i, j, ctxpool[i].job.result_digest[j], 155 to_le32(((uint32_t *) digest_ref[i])[j])); 156 } 157 } 158 } 159 160 end: 161 if (fail) 162 printf("Test failed function check %d\n", fail); 163 else 164 printf("Pass\n"); 165 166 for (i = 0; i < TEST_BUFS; i++) 167 free(bufs[i]); 168 aligned_free(mgr); 169 170 return fail; 171 #else 172 printf("Not Executed\n"); 173 return 0; 174 #endif /* FIPS_MODE */ 175 } 176