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