19de7f4ddSMarcel Cornu /**********************************************************************
29de7f4ddSMarcel Cornu Copyright(c) 2024 Intel Corporation All rights reserved.
39de7f4ddSMarcel Cornu
49de7f4ddSMarcel Cornu Redistribution and use in source and binary forms, with or without
59de7f4ddSMarcel Cornu modification, are permitted provided that the following conditions
69de7f4ddSMarcel Cornu are met:
79de7f4ddSMarcel Cornu * Redistributions of source code must retain the above copyright
89de7f4ddSMarcel Cornu notice, this list of conditions and the following disclaimer.
99de7f4ddSMarcel Cornu * Redistributions in binary form must reproduce the above copyright
109de7f4ddSMarcel Cornu notice, this list of conditions and the following disclaimer in
119de7f4ddSMarcel Cornu the documentation and/or other materials provided with the
129de7f4ddSMarcel Cornu distribution.
139de7f4ddSMarcel Cornu * Neither the name of Intel Corporation nor the names of its
149de7f4ddSMarcel Cornu contributors may be used to endorse or promote products derived
159de7f4ddSMarcel Cornu from this software without specific prior written permission.
169de7f4ddSMarcel Cornu
179de7f4ddSMarcel Cornu THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
189de7f4ddSMarcel Cornu "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
199de7f4ddSMarcel Cornu LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
209de7f4ddSMarcel Cornu A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
219de7f4ddSMarcel Cornu OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
229de7f4ddSMarcel Cornu SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
239de7f4ddSMarcel Cornu LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
249de7f4ddSMarcel Cornu DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
259de7f4ddSMarcel Cornu THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
269de7f4ddSMarcel Cornu (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
279de7f4ddSMarcel Cornu OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
289de7f4ddSMarcel Cornu **********************************************************************/
299de7f4ddSMarcel Cornu
309de7f4ddSMarcel Cornu #include <stdio.h>
319de7f4ddSMarcel Cornu #include <stdlib.h>
329de7f4ddSMarcel Cornu #include <string.h>
339de7f4ddSMarcel Cornu #include "isal_crypto_api.h"
349de7f4ddSMarcel Cornu #include "mh_sha256.h"
3578d8d8a4SMarcel Cornu #include "test.h"
369de7f4ddSMarcel Cornu
379de7f4ddSMarcel Cornu #ifdef SAFE_PARAM
3878d8d8a4SMarcel Cornu #define TEST_LEN 16 * 1024
3978d8d8a4SMarcel Cornu
4038e16e11SMarcel Cornu static int
test_mh_sha256_init_api(void)4138e16e11SMarcel Cornu test_mh_sha256_init_api(void)
429de7f4ddSMarcel Cornu {
439de7f4ddSMarcel Cornu int ret, retval = 1;
44*f6da0bf4SMarcel Cornu struct isal_mh_sha256_ctx *update_ctx = NULL;
459de7f4ddSMarcel Cornu const char *func_name = "isal_mh_sha256_init";
469de7f4ddSMarcel Cornu
479de7f4ddSMarcel Cornu update_ctx = malloc(sizeof(*update_ctx));
489de7f4ddSMarcel Cornu if (update_ctx == NULL) {
499de7f4ddSMarcel Cornu printf("malloc failed test aborted\n");
509de7f4ddSMarcel Cornu return retval;
519de7f4ddSMarcel Cornu }
529de7f4ddSMarcel Cornu
533e138b34SPablo de Lara #ifdef FIPS_MODE
543e138b34SPablo de Lara // Check for invalid algorithm error
553e138b34SPablo de Lara ret = isal_mh_sha256_init(update_ctx);
563e138b34SPablo de Lara CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, func_name, exit_init);
573e138b34SPablo de Lara #else
589de7f4ddSMarcel Cornu ret = isal_mh_sha256_init(NULL);
59a5dbb126SMarcel Cornu CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_CTX, func_name, exit_init);
609de7f4ddSMarcel Cornu
619de7f4ddSMarcel Cornu ret = isal_mh_sha256_init(update_ctx);
62a5dbb126SMarcel Cornu CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NONE, func_name, exit_init);
636a8a917cSPablo de Lara #endif
649de7f4ddSMarcel Cornu retval = 0;
659de7f4ddSMarcel Cornu
669de7f4ddSMarcel Cornu exit_init:
679de7f4ddSMarcel Cornu free(update_ctx);
689de7f4ddSMarcel Cornu
699de7f4ddSMarcel Cornu return retval;
709de7f4ddSMarcel Cornu }
719de7f4ddSMarcel Cornu
7238e16e11SMarcel Cornu static int
test_mh_sha256_update_api(void)7338e16e11SMarcel Cornu test_mh_sha256_update_api(void)
749de7f4ddSMarcel Cornu {
759de7f4ddSMarcel Cornu int ret, retval = 1;
76*f6da0bf4SMarcel Cornu struct isal_mh_sha256_ctx *update_ctx = NULL;
779de7f4ddSMarcel Cornu uint8_t *buff = NULL;
789de7f4ddSMarcel Cornu const char *func_name = "isal_mh_sha256_update";
799de7f4ddSMarcel Cornu
809de7f4ddSMarcel Cornu update_ctx = malloc(sizeof(*update_ctx));
819de7f4ddSMarcel Cornu buff = malloc(TEST_LEN);
829de7f4ddSMarcel Cornu if (update_ctx == NULL || buff == NULL) {
839de7f4ddSMarcel Cornu printf("malloc failed test aborted\n");
849de7f4ddSMarcel Cornu goto exit_update;
859de7f4ddSMarcel Cornu }
869de7f4ddSMarcel Cornu
873e138b34SPablo de Lara #ifdef FIPS_MODE
883e138b34SPablo de Lara // Check for invalid algorithm error
893e138b34SPablo de Lara ret = isal_mh_sha256_update(update_ctx, buff, TEST_LEN);
903e138b34SPablo de Lara CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, func_name, exit_update);
913e138b34SPablo de Lara #else
929de7f4ddSMarcel Cornu ret = isal_mh_sha256_update(NULL, buff, TEST_LEN);
93a5dbb126SMarcel Cornu CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_CTX, func_name, exit_update);
949de7f4ddSMarcel Cornu
959de7f4ddSMarcel Cornu ret = isal_mh_sha256_update(update_ctx, NULL, TEST_LEN);
96a5dbb126SMarcel Cornu CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_SRC, func_name, exit_update);
979de7f4ddSMarcel Cornu
989de7f4ddSMarcel Cornu ret = isal_mh_sha256_update(update_ctx, buff, TEST_LEN);
99a5dbb126SMarcel Cornu CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NONE, func_name, exit_update);
1006a8a917cSPablo de Lara #endif
1019de7f4ddSMarcel Cornu
1029de7f4ddSMarcel Cornu retval = 0;
1039de7f4ddSMarcel Cornu
1049de7f4ddSMarcel Cornu exit_update:
1059de7f4ddSMarcel Cornu free(update_ctx);
1069de7f4ddSMarcel Cornu free(buff);
1079de7f4ddSMarcel Cornu
1089de7f4ddSMarcel Cornu return retval;
1099de7f4ddSMarcel Cornu }
1109de7f4ddSMarcel Cornu
11138e16e11SMarcel Cornu static int
test_mh_sha256_finalize_api(void)11238e16e11SMarcel Cornu test_mh_sha256_finalize_api(void)
1139de7f4ddSMarcel Cornu {
1149de7f4ddSMarcel Cornu int ret, retval = 1;
115*f6da0bf4SMarcel Cornu struct isal_mh_sha256_ctx *update_ctx = NULL;
11615f45959SMarcel Cornu uint32_t hash_test[ISAL_SHA256_DIGEST_WORDS] = { 0 };
1179de7f4ddSMarcel Cornu const char *func_name = "isal_mh_sha256_finalize";
1189de7f4ddSMarcel Cornu
1199de7f4ddSMarcel Cornu update_ctx = malloc(sizeof(*update_ctx));
1209de7f4ddSMarcel Cornu if (update_ctx == NULL) {
1219de7f4ddSMarcel Cornu printf("malloc failed test aborted\n");
1229de7f4ddSMarcel Cornu return retval;
1239de7f4ddSMarcel Cornu }
1249de7f4ddSMarcel Cornu
1253e138b34SPablo de Lara #ifdef FIPS_MODE
1263e138b34SPablo de Lara // Check for invalid algorithm error
1273e138b34SPablo de Lara ret = isal_mh_sha256_finalize(update_ctx, hash_test);
1283e138b34SPablo de Lara CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, func_name, exit_finalize);
1293e138b34SPablo de Lara #else
1309de7f4ddSMarcel Cornu ret = isal_mh_sha256_finalize(NULL, hash_test);
131a5dbb126SMarcel Cornu CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_CTX, func_name, exit_finalize);
1329de7f4ddSMarcel Cornu
1339de7f4ddSMarcel Cornu ret = isal_mh_sha256_finalize(update_ctx, NULL);
134a5dbb126SMarcel Cornu CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NULL_AUTH, func_name, exit_finalize);
1359de7f4ddSMarcel Cornu
1369de7f4ddSMarcel Cornu ret = isal_mh_sha256_finalize(update_ctx, hash_test);
137a5dbb126SMarcel Cornu CHECK_RETURN_GOTO(ret, ISAL_CRYPTO_ERR_NONE, func_name, exit_finalize);
1386a8a917cSPablo de Lara #endif
1399de7f4ddSMarcel Cornu retval = 0;
1409de7f4ddSMarcel Cornu
1419de7f4ddSMarcel Cornu exit_finalize:
1429de7f4ddSMarcel Cornu free(update_ctx);
1439de7f4ddSMarcel Cornu
1449de7f4ddSMarcel Cornu return retval;
1459de7f4ddSMarcel Cornu }
1469de7f4ddSMarcel Cornu #endif /* SAFE_PARAM */
1479de7f4ddSMarcel Cornu
14838e16e11SMarcel Cornu int
main(void)14938e16e11SMarcel Cornu main(void)
1509de7f4ddSMarcel Cornu {
1519de7f4ddSMarcel Cornu int fail = 0;
1529de7f4ddSMarcel Cornu #ifdef SAFE_PARAM
1539de7f4ddSMarcel Cornu fail |= test_mh_sha256_init_api();
1549de7f4ddSMarcel Cornu fail |= test_mh_sha256_update_api();
1559de7f4ddSMarcel Cornu fail |= test_mh_sha256_finalize_api();
1569de7f4ddSMarcel Cornu printf(fail ? "Fail\n" : "Pass\n");
1579de7f4ddSMarcel Cornu #else
1589de7f4ddSMarcel Cornu printf("Not Executed\n");
1599de7f4ddSMarcel Cornu #endif
1609de7f4ddSMarcel Cornu return fail;
1619de7f4ddSMarcel Cornu }
162