1b60da680SMarcel Cornu /**********************************************************************
2b60da680SMarcel Cornu Copyright(c) 2024 Intel Corporation All rights reserved.
3b60da680SMarcel Cornu Redistribution and use in source and binary forms, with or without
4b60da680SMarcel Cornu modification, are permitted provided that the following conditions
5b60da680SMarcel Cornu are met:
6b60da680SMarcel Cornu * Redistributions of source code must retain the above copyright
7b60da680SMarcel Cornu notice, this list of conditions and the following disclaimer.
8b60da680SMarcel Cornu * Redistributions in binary form must reproduce the above copyright
9b60da680SMarcel Cornu notice, this list of conditions and the following disclaimer in
10b60da680SMarcel Cornu the documentation and/or other materials provided with the
11b60da680SMarcel Cornu distribution.
12b60da680SMarcel Cornu * Neither the name of Intel Corporation nor the names of its
13b60da680SMarcel Cornu contributors may be used to endorse or promote products derived
14b60da680SMarcel Cornu from this software without specific prior written permission.
15b60da680SMarcel Cornu THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16b60da680SMarcel Cornu "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17b60da680SMarcel Cornu LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18b60da680SMarcel Cornu A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19b60da680SMarcel Cornu OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20b60da680SMarcel Cornu SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21b60da680SMarcel Cornu LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22b60da680SMarcel Cornu DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23b60da680SMarcel Cornu THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24b60da680SMarcel Cornu (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25b60da680SMarcel Cornu OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26b60da680SMarcel Cornu **********************************************************************/
27b60da680SMarcel Cornu
28b60da680SMarcel Cornu #include <stdio.h>
29b60da680SMarcel Cornu #include <stdlib.h>
30b60da680SMarcel Cornu #include "isal_crypto_api.h"
31b60da680SMarcel Cornu #include "sha1_mb.h"
32b60da680SMarcel Cornu #include "multi_buffer.h"
33b60da680SMarcel Cornu #include "test.h"
34b60da680SMarcel Cornu
35b60da680SMarcel Cornu #ifdef SAFE_PARAM
36b60da680SMarcel Cornu
37868f05eaSMarcel Cornu static int
test_sha1_mb_init_api(void)38868f05eaSMarcel Cornu test_sha1_mb_init_api(void)
39b60da680SMarcel Cornu {
400106da91SPablo de Lara ISAL_SHA1_HASH_CTX_MGR *mgr = NULL;
41b60da680SMarcel Cornu int rc, ret = -1;
42b60da680SMarcel Cornu
430106da91SPablo de Lara rc = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SHA1_HASH_CTX_MGR));
44b60da680SMarcel Cornu if ((rc != 0) || (mgr == NULL)) {
45b60da680SMarcel Cornu printf("posix_memalign failed test aborted\n");
46b60da680SMarcel Cornu return 1;
47b60da680SMarcel Cornu }
48b60da680SMarcel Cornu // check null mgr
49b60da680SMarcel Cornu CHECK_RETURN_GOTO(isal_sha1_ctx_mgr_init(NULL), ISAL_CRYPTO_ERR_NULL_MGR,
50b60da680SMarcel Cornu "isal_sha1_ctx_mgr_init", end_init);
51b60da680SMarcel Cornu
52b60da680SMarcel Cornu // check valid args
53b60da680SMarcel Cornu CHECK_RETURN_GOTO(isal_sha1_ctx_mgr_init(mgr), ISAL_CRYPTO_ERR_NONE,
54b60da680SMarcel Cornu "isal_sha1_ctx_mgr_init", end_init);
55b60da680SMarcel Cornu ret = 0;
56b60da680SMarcel Cornu
57b60da680SMarcel Cornu end_init:
58b60da680SMarcel Cornu aligned_free(mgr);
59b60da680SMarcel Cornu
60b60da680SMarcel Cornu return ret;
61b60da680SMarcel Cornu }
62b60da680SMarcel Cornu
63868f05eaSMarcel Cornu static int
test_sha1_mb_submit_api(void)64868f05eaSMarcel Cornu test_sha1_mb_submit_api(void)
65b60da680SMarcel Cornu {
660106da91SPablo de Lara ISAL_SHA1_HASH_CTX_MGR *mgr = NULL;
670106da91SPablo de Lara ISAL_SHA1_HASH_CTX ctx = { 0 }, *ctx_ptr = &ctx;
68b60da680SMarcel Cornu int rc, ret = -1;
69b60da680SMarcel Cornu const char *fn_name = "isal_sha1_ctx_mgr_submit";
70b60da680SMarcel Cornu static uint8_t msg[] = "Test message";
71b60da680SMarcel Cornu
720106da91SPablo de Lara rc = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SHA1_HASH_CTX_MGR));
73b60da680SMarcel Cornu if ((rc != 0) || (mgr == NULL)) {
74b60da680SMarcel Cornu printf("posix_memalign failed test aborted\n");
75b60da680SMarcel Cornu return 1;
76b60da680SMarcel Cornu }
77b60da680SMarcel Cornu
78b60da680SMarcel Cornu rc = isal_sha1_ctx_mgr_init(mgr);
79b60da680SMarcel Cornu if (rc != ISAL_CRYPTO_ERR_NONE)
80b60da680SMarcel Cornu goto end_submit;
81b60da680SMarcel Cornu
82b60da680SMarcel Cornu // Init context before first use
83*8cb7fe78SPablo de Lara isal_hash_ctx_init(&ctx);
84b60da680SMarcel Cornu
85b60da680SMarcel Cornu // check null mgr
86b60da680SMarcel Cornu CHECK_RETURN_GOTO(isal_sha1_ctx_mgr_submit(NULL, ctx_ptr, &ctx_ptr, msg,
87*8cb7fe78SPablo de Lara (uint32_t) strlen((char *) msg),
88*8cb7fe78SPablo de Lara ISAL_HASH_ENTIRE),
89b60da680SMarcel Cornu ISAL_CRYPTO_ERR_NULL_MGR, fn_name, end_submit);
90b60da680SMarcel Cornu
91b60da680SMarcel Cornu // check null input ctx
921ec19bc8SMarcel Cornu CHECK_RETURN_GOTO(isal_sha1_ctx_mgr_submit(mgr, NULL, &ctx_ptr, msg,
93*8cb7fe78SPablo de Lara (uint32_t) strlen((char *) msg),
94*8cb7fe78SPablo de Lara ISAL_HASH_ENTIRE),
95b60da680SMarcel Cornu ISAL_CRYPTO_ERR_NULL_CTX, fn_name, end_submit);
96b60da680SMarcel Cornu
97b60da680SMarcel Cornu // check null output ctx
981ec19bc8SMarcel Cornu CHECK_RETURN_GOTO(isal_sha1_ctx_mgr_submit(mgr, ctx_ptr, NULL, msg,
99*8cb7fe78SPablo de Lara (uint32_t) strlen((char *) msg),
100*8cb7fe78SPablo de Lara ISAL_HASH_ENTIRE),
101b60da680SMarcel Cornu ISAL_CRYPTO_ERR_NULL_CTX, fn_name, end_submit);
102b60da680SMarcel Cornu
103b60da680SMarcel Cornu // check null source ptr
104b60da680SMarcel Cornu CHECK_RETURN_GOTO(isal_sha1_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, NULL,
105*8cb7fe78SPablo de Lara (uint32_t) strlen((char *) msg),
106*8cb7fe78SPablo de Lara ISAL_HASH_ENTIRE),
107b60da680SMarcel Cornu ISAL_CRYPTO_ERR_NULL_SRC, fn_name, end_submit);
108b60da680SMarcel Cornu
109b60da680SMarcel Cornu // check invalid flag
1101ec19bc8SMarcel Cornu CHECK_RETURN_GOTO(isal_sha1_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
1111ec19bc8SMarcel Cornu (uint32_t) strlen((char *) msg), 999),
112b60da680SMarcel Cornu ISAL_CRYPTO_ERR_INVALID_FLAGS, fn_name, end_submit);
113b60da680SMarcel Cornu
114b60da680SMarcel Cornu // simulate internal error (submit in progress job)
115*8cb7fe78SPablo de Lara ctx_ptr->status = ISAL_HASH_CTX_STS_PROCESSING;
116b60da680SMarcel Cornu
117b60da680SMarcel Cornu CHECK_RETURN_GOTO(isal_sha1_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
118*8cb7fe78SPablo de Lara (uint32_t) strlen((char *) msg),
119*8cb7fe78SPablo de Lara ISAL_HASH_ENTIRE),
120b60da680SMarcel Cornu ISAL_CRYPTO_ERR_ALREADY_PROCESSING, fn_name, end_submit);
121b60da680SMarcel Cornu
122*8cb7fe78SPablo de Lara CHECK_RETURN_GOTO(ctx_ptr->error, ISAL_HASH_CTX_ERROR_ALREADY_PROCESSING, fn_name,
123*8cb7fe78SPablo de Lara end_submit);
124b60da680SMarcel Cornu
125b60da680SMarcel Cornu // simulate internal error (submit completed job)
126*8cb7fe78SPablo de Lara ctx_ptr->error = ISAL_HASH_CTX_ERROR_NONE;
127*8cb7fe78SPablo de Lara ctx_ptr->status = ISAL_HASH_CTX_STS_COMPLETE;
128b60da680SMarcel Cornu
129b60da680SMarcel Cornu CHECK_RETURN_GOTO(isal_sha1_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
130*8cb7fe78SPablo de Lara (uint32_t) strlen((char *) msg),
131*8cb7fe78SPablo de Lara ISAL_HASH_UPDATE),
132b60da680SMarcel Cornu ISAL_CRYPTO_ERR_ALREADY_COMPLETED, fn_name, end_submit);
133b60da680SMarcel Cornu
134*8cb7fe78SPablo de Lara CHECK_RETURN_GOTO(ctx_ptr->error, ISAL_HASH_CTX_ERROR_ALREADY_COMPLETED, fn_name,
135*8cb7fe78SPablo de Lara end_submit);
136b60da680SMarcel Cornu
137b60da680SMarcel Cornu // check valid args
138*8cb7fe78SPablo de Lara isal_hash_ctx_init(&ctx);
139b60da680SMarcel Cornu CHECK_RETURN_GOTO(isal_sha1_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
140*8cb7fe78SPablo de Lara (uint32_t) strlen((char *) msg),
141*8cb7fe78SPablo de Lara ISAL_HASH_ENTIRE),
142b60da680SMarcel Cornu ISAL_CRYPTO_ERR_NONE, fn_name, end_submit);
143b60da680SMarcel Cornu ret = 0;
144b60da680SMarcel Cornu
145b60da680SMarcel Cornu end_submit:
146b60da680SMarcel Cornu aligned_free(mgr);
147b60da680SMarcel Cornu
148b60da680SMarcel Cornu return ret;
149b60da680SMarcel Cornu }
150b60da680SMarcel Cornu
151868f05eaSMarcel Cornu static int
test_sha1_mb_flush_api(void)152868f05eaSMarcel Cornu test_sha1_mb_flush_api(void)
153b60da680SMarcel Cornu {
1540106da91SPablo de Lara ISAL_SHA1_HASH_CTX_MGR *mgr = NULL;
1550106da91SPablo de Lara ISAL_SHA1_HASH_CTX ctx = { 0 }, *ctx_ptr = &ctx;
156b60da680SMarcel Cornu int rc, ret = -1;
157b60da680SMarcel Cornu const char *fn_name = "isal_sha1_ctx_mgr_flush";
158b60da680SMarcel Cornu
1590106da91SPablo de Lara rc = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SHA1_HASH_CTX_MGR));
160b60da680SMarcel Cornu if ((rc != 0) || (mgr == NULL)) {
161b60da680SMarcel Cornu printf("posix_memalign failed test aborted\n");
162b60da680SMarcel Cornu return 1;
163b60da680SMarcel Cornu }
164b60da680SMarcel Cornu
165b60da680SMarcel Cornu rc = isal_sha1_ctx_mgr_init(mgr);
166b60da680SMarcel Cornu if (rc != ISAL_CRYPTO_ERR_NONE)
167b60da680SMarcel Cornu goto end_flush;
168b60da680SMarcel Cornu
169b60da680SMarcel Cornu // Init context before first use
170*8cb7fe78SPablo de Lara isal_hash_ctx_init(&ctx);
171b60da680SMarcel Cornu
172b60da680SMarcel Cornu // check null mgr
173868f05eaSMarcel Cornu CHECK_RETURN_GOTO(isal_sha1_ctx_mgr_flush(NULL, &ctx_ptr), ISAL_CRYPTO_ERR_NULL_MGR,
174868f05eaSMarcel Cornu fn_name, end_flush);
175b60da680SMarcel Cornu
176b60da680SMarcel Cornu // check null ctx
177868f05eaSMarcel Cornu CHECK_RETURN_GOTO(isal_sha1_ctx_mgr_flush(mgr, NULL), ISAL_CRYPTO_ERR_NULL_CTX, fn_name,
178868f05eaSMarcel Cornu end_flush);
179b60da680SMarcel Cornu
180b60da680SMarcel Cornu // check valid args
181868f05eaSMarcel Cornu CHECK_RETURN_GOTO(isal_sha1_ctx_mgr_flush(mgr, &ctx_ptr), ISAL_CRYPTO_ERR_NONE, fn_name,
182868f05eaSMarcel Cornu end_flush);
183b60da680SMarcel Cornu
184b60da680SMarcel Cornu if (ctx_ptr != NULL) {
185b60da680SMarcel Cornu printf("test: %s() - expected NULL job ptr\n", fn_name);
186b60da680SMarcel Cornu goto end_flush;
187b60da680SMarcel Cornu }
188b60da680SMarcel Cornu
189b60da680SMarcel Cornu ret = 0;
190b60da680SMarcel Cornu
191b60da680SMarcel Cornu end_flush:
192b60da680SMarcel Cornu aligned_free(mgr);
193b60da680SMarcel Cornu
194b60da680SMarcel Cornu return ret;
195b60da680SMarcel Cornu }
196b60da680SMarcel Cornu #endif /* SAFE_PARAM */
197b60da680SMarcel Cornu
198868f05eaSMarcel Cornu int
main(void)199868f05eaSMarcel Cornu main(void)
200b60da680SMarcel Cornu {
201b60da680SMarcel Cornu int fail = 0;
202b60da680SMarcel Cornu
203b60da680SMarcel Cornu #ifdef SAFE_PARAM
204b60da680SMarcel Cornu fail |= test_sha1_mb_init_api();
205b60da680SMarcel Cornu fail |= test_sha1_mb_submit_api();
206b60da680SMarcel Cornu fail |= test_sha1_mb_flush_api();
207b60da680SMarcel Cornu
208b60da680SMarcel Cornu printf(fail ? "Fail\n" : "Pass\n");
209b60da680SMarcel Cornu #else
210b60da680SMarcel Cornu printf("Not Executed\n");
211b60da680SMarcel Cornu #endif
212b60da680SMarcel Cornu return fail;
213b60da680SMarcel Cornu }
214