1 /********************************************************************** 2 Copyright(c) 2024 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 <string.h> 33 #include "isal_crypto_api.h" 34 #include "mh_sha256.h" 35 #include "test.h" 36 37 #ifdef SAFE_PARAM 38 #define TEST_LEN 16 * 1024 39 40 static int 41 test_mh_sha256_init_api(void) 42 { 43 int ret, retval = 1; 44 struct mh_sha256_ctx *update_ctx = NULL; 45 const char *func_name = "isal_mh_sha256_init"; 46 47 update_ctx = malloc(sizeof(*update_ctx)); 48 if (update_ctx == NULL) { 49 printf("malloc failed test aborted\n"); 50 return retval; 51 } 52 53 ret = isal_mh_sha256_init(NULL); 54 CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_CTX, func_name, exit_init); 55 56 ret = isal_mh_sha256_init(update_ctx); 57 #ifdef FIPS_MODE 58 CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, func_name, exit_init); 59 #else 60 CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NONE, func_name, exit_init); 61 #endif 62 retval = 0; 63 64 exit_init: 65 free(update_ctx); 66 67 return retval; 68 } 69 70 static int 71 test_mh_sha256_update_api(void) 72 { 73 int ret, retval = 1; 74 struct mh_sha256_ctx *update_ctx = NULL; 75 uint8_t *buff = NULL; 76 const char *func_name = "isal_mh_sha256_update"; 77 78 update_ctx = malloc(sizeof(*update_ctx)); 79 buff = malloc(TEST_LEN); 80 if (update_ctx == NULL || buff == NULL) { 81 printf("malloc failed test aborted\n"); 82 goto exit_update; 83 } 84 85 ret = isal_mh_sha256_update(NULL, buff, TEST_LEN); 86 CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_CTX, func_name, exit_update); 87 88 ret = isal_mh_sha256_update(update_ctx, NULL, TEST_LEN); 89 CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_SRC, func_name, exit_update); 90 91 ret = isal_mh_sha256_update(update_ctx, buff, TEST_LEN); 92 #ifdef FIPS_MODE 93 CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, func_name, exit_update); 94 #else 95 CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NONE, func_name, exit_update); 96 #endif 97 98 retval = 0; 99 100 exit_update: 101 free(update_ctx); 102 free(buff); 103 104 return retval; 105 } 106 107 static int 108 test_mh_sha256_finalize_api(void) 109 { 110 int ret, retval = 1; 111 struct mh_sha256_ctx *update_ctx = NULL; 112 uint32_t hash_test[SHA256_DIGEST_WORDS] = { 0 }; 113 const char *func_name = "isal_mh_sha256_finalize"; 114 115 update_ctx = malloc(sizeof(*update_ctx)); 116 if (update_ctx == NULL) { 117 printf("malloc failed test aborted\n"); 118 return retval; 119 } 120 121 ret = isal_mh_sha256_finalize(NULL, hash_test); 122 CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_CTX, func_name, exit_finalize); 123 124 ret = isal_mh_sha256_finalize(update_ctx, NULL); 125 CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_AUTH, func_name, exit_finalize); 126 127 ret = isal_mh_sha256_finalize(update_ctx, hash_test); 128 #ifdef FIPS_MODE 129 CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, func_name, exit_finalize); 130 #else 131 CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NONE, func_name, exit_finalize); 132 #endif 133 retval = 0; 134 135 exit_finalize: 136 free(update_ctx); 137 138 return retval; 139 } 140 #endif /* SAFE_PARAM */ 141 142 int 143 main(void) 144 { 145 int fail = 0; 146 #ifdef SAFE_PARAM 147 fail |= test_mh_sha256_init_api(); 148 fail |= test_mh_sha256_update_api(); 149 fail |= test_mh_sha256_finalize_api(); 150 printf(fail ? "Fail\n" : "Pass\n"); 151 #else 152 printf("Not Executed\n"); 153 #endif 154 return fail; 155 } 156