xref: /isa-l_crypto/mh_sha256/mh_sha256_param_test.c (revision 9de7f4ddfbfd4e6544d0f8176131b27150111e81)
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 
36 #define TEST_LEN   16*1024
37 
38 #define CHECK_RETURN(state, expected, func, label)	do{ \
39 	if((state) != (expected)){ \
40 		printf("test: %s() - expected return " \
41 		       "value %d, got %d\n", func, expected, state); \
42 		goto label; \
43 	} \
44 }while(0)
45 
46 #ifdef SAFE_PARAM
47 static int test_mh_sha256_init_api(void)
48 {
49 	int ret, retval = 1;
50 	struct mh_sha256_ctx *update_ctx = NULL;
51 	ISAL_CRYPTO_ERROR expected = ISAL_CRYPTO_ERR_NULL_CTX;
52 	const char *func_name = "isal_mh_sha256_init";
53 
54 	update_ctx = malloc(sizeof(*update_ctx));
55 	if (update_ctx == NULL) {
56 		printf("malloc failed test aborted\n");
57 		return retval;
58 	}
59 
60 	ret = isal_mh_sha256_init(NULL);
61 	CHECK_RETURN(ret, expected, func_name, exit_init);
62 
63 	expected = ISAL_CRYPTO_ERR_NONE;
64 	ret = isal_mh_sha256_init(update_ctx);
65 	CHECK_RETURN(ret, expected, func_name, exit_init);
66 
67 	retval = 0;
68 
69       exit_init:
70 	free(update_ctx);
71 
72 	return retval;
73 }
74 
75 static int test_mh_sha256_update_api(void)
76 {
77 	int ret, retval = 1;
78 	struct mh_sha256_ctx *update_ctx = NULL;
79 	uint8_t *buff = NULL;
80 	ISAL_CRYPTO_ERROR expected = ISAL_CRYPTO_ERR_NULL_CTX;
81 	const char *func_name = "isal_mh_sha256_update";
82 
83 	update_ctx = malloc(sizeof(*update_ctx));
84 	buff = malloc(TEST_LEN);
85 	if (update_ctx == NULL || buff == NULL) {
86 		printf("malloc failed test aborted\n");
87 		goto exit_update;
88 	}
89 
90 	ret = isal_mh_sha256_update(NULL, buff, TEST_LEN);
91 	CHECK_RETURN(ret, expected, func_name, exit_update);
92 
93 	expected = ISAL_CRYPTO_ERR_NULL_SRC;
94 	ret = isal_mh_sha256_update(update_ctx, NULL, TEST_LEN);
95 	CHECK_RETURN(ret, expected, func_name, exit_update);
96 
97 	expected = ISAL_CRYPTO_ERR_NONE;
98 	ret = isal_mh_sha256_update(update_ctx, buff, TEST_LEN);
99 	CHECK_RETURN(ret, expected, func_name, exit_update);
100 
101 	retval = 0;
102 
103       exit_update:
104 	free(update_ctx);
105 	free(buff);
106 
107 	return retval;
108 }
109 
110 static int test_mh_sha256_finalize_api(void)
111 {
112 	int ret, retval = 1;
113 	struct mh_sha256_ctx *update_ctx = NULL;
114 	uint32_t hash_test[SHA256_DIGEST_WORDS] = { 0 };
115 	ISAL_CRYPTO_ERROR expected = ISAL_CRYPTO_ERR_NULL_CTX;
116 	const char *func_name = "isal_mh_sha256_finalize";
117 
118 	update_ctx = malloc(sizeof(*update_ctx));
119 	if (update_ctx == NULL) {
120 		printf("malloc failed test aborted\n");
121 		return retval;
122 	}
123 
124 	ret = isal_mh_sha256_finalize(NULL, hash_test);
125 	CHECK_RETURN(ret, expected, func_name, exit_finalize);
126 
127 	expected = ISAL_CRYPTO_ERR_NULL_AUTH;
128 	ret = isal_mh_sha256_finalize(update_ctx, NULL);
129 	CHECK_RETURN(ret, expected, func_name, exit_finalize);
130 
131 	expected = ISAL_CRYPTO_ERR_NONE;
132 	ret = isal_mh_sha256_finalize(update_ctx, hash_test);
133 	CHECK_RETURN(ret, expected, func_name, exit_finalize);
134 	retval = 0;
135 
136       exit_finalize:
137 	free(update_ctx);
138 
139 	return retval;
140 }
141 #endif /* SAFE_PARAM */
142 
143 int 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