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