1 /********************************************************************** 2 Copyright(c) 2024 Intel Corporation All rights reserved. 3 Redistribution and use in source and binary forms, with or without 4 modification, are permitted provided that the following conditions 5 are met: 6 * Redistributions of source code must retain the above copyright 7 notice, this list of conditions and the following disclaimer. 8 * Redistributions in binary form must reproduce the above copyright 9 notice, this list of conditions and the following disclaimer in 10 the documentation and/or other materials provided with the 11 distribution. 12 * Neither the name of Intel Corporation nor the names of its 13 contributors may be used to endorse or promote products derived 14 from this software without specific prior written permission. 15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 **********************************************************************/ 27 28 #include <stdio.h> 29 #include <stdlib.h> 30 #include "isal_crypto_api.h" 31 #include "sha256_mb.h" 32 #include "multi_buffer.h" 33 #include "test.h" 34 35 #ifdef SAFE_PARAM 36 37 static int test_sha256_mb_init_api(void) 38 { 39 SHA256_HASH_CTX_MGR *mgr = NULL; 40 int rc, ret = -1; 41 42 rc = posix_memalign((void *)&mgr, 16, sizeof(SHA256_HASH_CTX_MGR)); 43 if ((rc != 0) || (mgr == NULL)) { 44 printf("posix_memalign failed test aborted\n"); 45 return 1; 46 } 47 // check null mgr 48 CHECK_RETURN_GOTO(isal_sha256_ctx_mgr_init(NULL), ISAL_CRYPTO_ERR_NULL_MGR, 49 "isal_sha256_ctx_mgr_init", end_init); 50 51 // check valid args 52 CHECK_RETURN_GOTO(isal_sha256_ctx_mgr_init(mgr), ISAL_CRYPTO_ERR_NONE, 53 "isal_sha256_ctx_mgr_init", end_init); 54 ret = 0; 55 56 end_init: 57 aligned_free(mgr); 58 59 return ret; 60 } 61 62 static int test_sha256_mb_submit_api(void) 63 { 64 SHA256_HASH_CTX_MGR *mgr = NULL; 65 SHA256_HASH_CTX ctx = { 0 }, *ctx_ptr = &ctx; 66 int rc, ret = -1; 67 const char *fn_name = "isal_sha256_ctx_mgr_submit"; 68 static uint8_t msg[] = "Test message"; 69 70 rc = posix_memalign((void *)&mgr, 16, sizeof(SHA256_HASH_CTX_MGR)); 71 if ((rc != 0) || (mgr == NULL)) { 72 printf("posix_memalign failed test aborted\n"); 73 return 1; 74 } 75 76 rc = isal_sha256_ctx_mgr_init(mgr); 77 if (rc != ISAL_CRYPTO_ERR_NONE) 78 goto end_submit; 79 80 // Init context before first use 81 hash_ctx_init(&ctx); 82 83 // check null mgr 84 CHECK_RETURN_GOTO(isal_sha256_ctx_mgr_submit(NULL, ctx_ptr, &ctx_ptr, msg, 85 strlen((char *)msg), 86 HASH_ENTIRE), 87 ISAL_CRYPTO_ERR_NULL_MGR, fn_name, end_submit); 88 89 // check null input ctx 90 CHECK_RETURN_GOTO(isal_sha256_ctx_mgr_submit 91 (mgr, NULL, &ctx_ptr, msg, strlen((char *)msg), HASH_ENTIRE), 92 ISAL_CRYPTO_ERR_NULL_CTX, fn_name, end_submit); 93 94 // check null output ctx 95 CHECK_RETURN_GOTO(isal_sha256_ctx_mgr_submit 96 (mgr, ctx_ptr, NULL, msg, strlen((char *)msg), HASH_ENTIRE), 97 ISAL_CRYPTO_ERR_NULL_CTX, fn_name, end_submit); 98 99 // check null source ptr 100 CHECK_RETURN_GOTO(isal_sha256_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, NULL, 101 strlen((char *)msg), 102 HASH_ENTIRE), 103 ISAL_CRYPTO_ERR_NULL_SRC, fn_name, end_submit); 104 105 // check invalid flag 106 CHECK_RETURN_GOTO(isal_sha256_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg, 107 strlen((char *)msg), 999), 108 ISAL_CRYPTO_ERR_INVALID_FLAGS, fn_name, end_submit); 109 110 // simulate internal error (submit in progress job) 111 ctx_ptr->status = HASH_CTX_STS_PROCESSING; 112 113 CHECK_RETURN_GOTO(isal_sha256_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg, 114 strlen((char *)msg), 115 HASH_ENTIRE), 116 ISAL_CRYPTO_ERR_ALREADY_PROCESSING, fn_name, end_submit); 117 118 CHECK_RETURN_GOTO(ctx_ptr->error, HASH_CTX_ERROR_ALREADY_PROCESSING, 119 fn_name, end_submit); 120 121 // simulate internal error (submit completed job) 122 ctx_ptr->error = HASH_CTX_ERROR_NONE; 123 ctx_ptr->status = HASH_CTX_STS_COMPLETE; 124 125 CHECK_RETURN_GOTO(isal_sha256_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg, 126 strlen((char *)msg), 127 HASH_UPDATE), 128 ISAL_CRYPTO_ERR_ALREADY_COMPLETED, fn_name, end_submit); 129 130 CHECK_RETURN_GOTO(ctx_ptr->error, HASH_CTX_ERROR_ALREADY_COMPLETED, 131 fn_name, end_submit); 132 133 // check valid args 134 hash_ctx_init(&ctx); 135 CHECK_RETURN_GOTO(isal_sha256_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg, 136 strlen((char *)msg), 137 HASH_ENTIRE), 138 ISAL_CRYPTO_ERR_NONE, fn_name, end_submit); 139 ret = 0; 140 141 end_submit: 142 aligned_free(mgr); 143 144 return ret; 145 } 146 147 static int test_sha256_mb_flush_api(void) 148 { 149 SHA256_HASH_CTX_MGR *mgr = NULL; 150 SHA256_HASH_CTX ctx = { 0 }, *ctx_ptr = &ctx; 151 int rc, ret = -1; 152 const char *fn_name = "isal_sha256_ctx_mgr_flush"; 153 154 rc = posix_memalign((void *)&mgr, 16, sizeof(SHA256_HASH_CTX_MGR)); 155 if ((rc != 0) || (mgr == NULL)) { 156 printf("posix_memalign failed test aborted\n"); 157 return 1; 158 } 159 160 rc = isal_sha256_ctx_mgr_init(mgr); 161 if (rc != ISAL_CRYPTO_ERR_NONE) 162 goto end_flush; 163 164 // Init context before first use 165 hash_ctx_init(&ctx); 166 167 // check null mgr 168 CHECK_RETURN_GOTO(isal_sha256_ctx_mgr_flush(NULL, &ctx_ptr), 169 ISAL_CRYPTO_ERR_NULL_MGR, fn_name, end_flush); 170 171 // check null ctx 172 CHECK_RETURN_GOTO(isal_sha256_ctx_mgr_flush(mgr, NULL), 173 ISAL_CRYPTO_ERR_NULL_CTX, fn_name, end_flush); 174 175 // check valid args 176 CHECK_RETURN_GOTO(isal_sha256_ctx_mgr_flush(mgr, &ctx_ptr), 177 ISAL_CRYPTO_ERR_NONE, fn_name, end_flush); 178 179 if (ctx_ptr != NULL) { 180 printf("test: %s() - expected NULL job ptr\n", fn_name); 181 goto end_flush; 182 } 183 184 ret = 0; 185 186 end_flush: 187 aligned_free(mgr); 188 189 return ret; 190 } 191 #endif /* SAFE_PARAM */ 192 193 int main(void) 194 { 195 int fail = 0; 196 197 #ifdef SAFE_PARAM 198 fail |= test_sha256_mb_init_api(); 199 fail |= test_sha256_mb_submit_api(); 200 fail |= test_sha256_mb_flush_api(); 201 202 printf(fail ? "Fail\n" : "Pass\n"); 203 #else 204 printf("Not Executed\n"); 205 #endif 206 return fail; 207 } 208