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 "sha512_mb.h" 32 #include "multi_buffer.h" 33 #include "test.h" 34 35 #ifdef SAFE_PARAM 36 37 static int 38 test_sha512_mb_init_api(void) 39 { 40 ISAL_SHA512_HASH_CTX_MGR *mgr = NULL; 41 int rc, ret = -1; 42 43 rc = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SHA512_HASH_CTX_MGR)); 44 if ((rc != 0) || (mgr == NULL)) { 45 printf("posix_memalign failed test aborted\n"); 46 return 1; 47 } 48 // check null mgr 49 CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_init(NULL), ISAL_CRYPTO_ERR_NULL_MGR, 50 "isal_sha512_ctx_mgr_init", end_init); 51 52 // check valid args 53 CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_init(mgr), ISAL_CRYPTO_ERR_NONE, 54 "isal_sha512_ctx_mgr_init", end_init); 55 ret = 0; 56 57 end_init: 58 aligned_free(mgr); 59 60 return ret; 61 } 62 63 static int 64 test_sha512_mb_submit_api(void) 65 { 66 ISAL_SHA512_HASH_CTX_MGR *mgr = NULL; 67 ISAL_SHA512_HASH_CTX ctx = { 0 }, *ctx_ptr = &ctx; 68 int rc, ret = -1; 69 const char *fn_name = "isal_sha512_ctx_mgr_submit"; 70 static uint8_t msg[] = "Test message"; 71 72 rc = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SHA512_HASH_CTX_MGR)); 73 if ((rc != 0) || (mgr == NULL)) { 74 printf("posix_memalign failed test aborted\n"); 75 return 1; 76 } 77 78 rc = isal_sha512_ctx_mgr_init(mgr); 79 if (rc != ISAL_CRYPTO_ERR_NONE) 80 goto end_submit; 81 82 // Init context before first use 83 isal_hash_ctx_init(&ctx); 84 85 // check null mgr 86 CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_submit(NULL, ctx_ptr, &ctx_ptr, msg, 87 (uint32_t) strlen((char *) msg), 88 ISAL_HASH_ENTIRE), 89 ISAL_CRYPTO_ERR_NULL_MGR, fn_name, end_submit); 90 91 // check null input ctx 92 CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_submit(mgr, NULL, &ctx_ptr, msg, 93 (uint32_t) strlen((char *) msg), 94 ISAL_HASH_ENTIRE), 95 ISAL_CRYPTO_ERR_NULL_CTX, fn_name, end_submit); 96 97 // check null output ctx 98 CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_submit(mgr, ctx_ptr, NULL, msg, 99 (uint32_t) strlen((char *) msg), 100 ISAL_HASH_ENTIRE), 101 ISAL_CRYPTO_ERR_NULL_CTX, fn_name, end_submit); 102 103 // check null source ptr 104 CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, NULL, 105 (uint32_t) strlen((char *) msg), 106 ISAL_HASH_ENTIRE), 107 ISAL_CRYPTO_ERR_NULL_SRC, fn_name, end_submit); 108 109 // check invalid flag 110 CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg, 111 (uint32_t) strlen((char *) msg), 999), 112 ISAL_CRYPTO_ERR_INVALID_FLAGS, fn_name, end_submit); 113 114 // simulate internal error (submit in progress job) 115 ctx_ptr->status = ISAL_HASH_CTX_STS_PROCESSING; 116 117 CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg, 118 (uint32_t) strlen((char *) msg), 119 ISAL_HASH_ENTIRE), 120 ISAL_CRYPTO_ERR_ALREADY_PROCESSING, fn_name, end_submit); 121 122 CHECK_RETURN_GOTO(ctx_ptr->error, ISAL_HASH_CTX_ERROR_ALREADY_PROCESSING, fn_name, 123 end_submit); 124 125 // simulate internal error (submit completed job) 126 ctx_ptr->error = ISAL_HASH_CTX_ERROR_NONE; 127 ctx_ptr->status = ISAL_HASH_CTX_STS_COMPLETE; 128 129 CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg, 130 (uint32_t) strlen((char *) msg), 131 ISAL_HASH_UPDATE), 132 ISAL_CRYPTO_ERR_ALREADY_COMPLETED, fn_name, end_submit); 133 134 CHECK_RETURN_GOTO(ctx_ptr->error, ISAL_HASH_CTX_ERROR_ALREADY_COMPLETED, fn_name, 135 end_submit); 136 137 // check valid args 138 isal_hash_ctx_init(&ctx); 139 CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg, 140 (uint32_t) strlen((char *) msg), 141 ISAL_HASH_ENTIRE), 142 ISAL_CRYPTO_ERR_NONE, fn_name, end_submit); 143 ret = 0; 144 145 end_submit: 146 aligned_free(mgr); 147 148 return ret; 149 } 150 151 static int 152 test_sha512_mb_flush_api(void) 153 { 154 ISAL_SHA512_HASH_CTX_MGR *mgr = NULL; 155 ISAL_SHA512_HASH_CTX ctx = { 0 }, *ctx_ptr = &ctx; 156 int rc, ret = -1; 157 const char *fn_name = "isal_sha512_ctx_mgr_flush"; 158 159 rc = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SHA512_HASH_CTX_MGR)); 160 if ((rc != 0) || (mgr == NULL)) { 161 printf("posix_memalign failed test aborted\n"); 162 return 1; 163 } 164 165 rc = isal_sha512_ctx_mgr_init(mgr); 166 if (rc != ISAL_CRYPTO_ERR_NONE) 167 goto end_flush; 168 169 // Init context before first use 170 isal_hash_ctx_init(&ctx); 171 172 // check null mgr 173 CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_flush(NULL, &ctx_ptr), ISAL_CRYPTO_ERR_NULL_MGR, 174 fn_name, end_flush); 175 176 // check null ctx 177 CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_flush(mgr, NULL), ISAL_CRYPTO_ERR_NULL_CTX, fn_name, 178 end_flush); 179 180 // check valid args 181 CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_flush(mgr, &ctx_ptr), ISAL_CRYPTO_ERR_NONE, fn_name, 182 end_flush); 183 184 if (ctx_ptr != NULL) { 185 printf("test: %s() - expected NULL job ptr\n", fn_name); 186 goto end_flush; 187 } 188 189 ret = 0; 190 191 end_flush: 192 aligned_free(mgr); 193 194 return ret; 195 } 196 #endif /* SAFE_PARAM */ 197 198 int 199 main(void) 200 { 201 int fail = 0; 202 203 #ifdef SAFE_PARAM 204 fail |= test_sha512_mb_init_api(); 205 fail |= test_sha512_mb_submit_api(); 206 fail |= test_sha512_mb_flush_api(); 207 208 printf(fail ? "Fail\n" : "Pass\n"); 209 #else 210 printf("Not Executed\n"); 211 #endif 212 return fail; 213 } 214