xref: /isa-l_crypto/sha512_mb/sha512_mb_param_test.c (revision 592e639e5cd0e9fa1a927dd7459a23176ec36070)
1f69ff2b8SMarcel Cornu /**********************************************************************
2f69ff2b8SMarcel Cornu   Copyright(c) 2024 Intel Corporation All rights reserved.
3f69ff2b8SMarcel Cornu   Redistribution and use in source and binary forms, with or without
4f69ff2b8SMarcel Cornu   modification, are permitted provided that the following conditions
5f69ff2b8SMarcel Cornu   are met:
6f69ff2b8SMarcel Cornu     * Redistributions of source code must retain the above copyright
7f69ff2b8SMarcel Cornu       notice, this list of conditions and the following disclaimer.
8f69ff2b8SMarcel Cornu     * Redistributions in binary form must reproduce the above copyright
9f69ff2b8SMarcel Cornu       notice, this list of conditions and the following disclaimer in
10f69ff2b8SMarcel Cornu       the documentation and/or other materials provided with the
11f69ff2b8SMarcel Cornu       distribution.
12f69ff2b8SMarcel Cornu     * Neither the name of Intel Corporation nor the names of its
13f69ff2b8SMarcel Cornu       contributors may be used to endorse or promote products derived
14f69ff2b8SMarcel Cornu       from this software without specific prior written permission.
15f69ff2b8SMarcel Cornu   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16f69ff2b8SMarcel Cornu   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17f69ff2b8SMarcel Cornu   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18f69ff2b8SMarcel Cornu   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19f69ff2b8SMarcel Cornu   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20f69ff2b8SMarcel Cornu   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21f69ff2b8SMarcel Cornu   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22f69ff2b8SMarcel Cornu   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23f69ff2b8SMarcel Cornu   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24f69ff2b8SMarcel Cornu   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25f69ff2b8SMarcel Cornu   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26f69ff2b8SMarcel Cornu **********************************************************************/
27f69ff2b8SMarcel Cornu 
28f69ff2b8SMarcel Cornu #include <stdio.h>
29f69ff2b8SMarcel Cornu #include <stdlib.h>
30f69ff2b8SMarcel Cornu #include "isal_crypto_api.h"
31f69ff2b8SMarcel Cornu #include "sha512_mb.h"
32f69ff2b8SMarcel Cornu #include "multi_buffer.h"
33f69ff2b8SMarcel Cornu #include "test.h"
34f69ff2b8SMarcel Cornu 
35f69ff2b8SMarcel Cornu #ifdef SAFE_PARAM
36f69ff2b8SMarcel Cornu 
373fb7b5f1SMarcel Cornu static int
test_sha512_mb_init_api(void)383fb7b5f1SMarcel Cornu test_sha512_mb_init_api(void)
39f69ff2b8SMarcel Cornu {
40*592e639eSPablo de Lara         ISAL_SHA512_HASH_CTX_MGR *mgr = NULL;
41f69ff2b8SMarcel Cornu         int rc, ret = -1;
42f69ff2b8SMarcel Cornu 
43*592e639eSPablo de Lara         rc = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SHA512_HASH_CTX_MGR));
44f69ff2b8SMarcel Cornu         if ((rc != 0) || (mgr == NULL)) {
45f69ff2b8SMarcel Cornu                 printf("posix_memalign failed test aborted\n");
46f69ff2b8SMarcel Cornu                 return 1;
47f69ff2b8SMarcel Cornu         }
48f69ff2b8SMarcel Cornu         // check null mgr
49f69ff2b8SMarcel Cornu         CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_init(NULL), ISAL_CRYPTO_ERR_NULL_MGR,
50f69ff2b8SMarcel Cornu                           "isal_sha512_ctx_mgr_init", end_init);
51f69ff2b8SMarcel Cornu 
52f69ff2b8SMarcel Cornu         // check valid args
53f69ff2b8SMarcel Cornu         CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_init(mgr), ISAL_CRYPTO_ERR_NONE,
54f69ff2b8SMarcel Cornu                           "isal_sha512_ctx_mgr_init", end_init);
55f69ff2b8SMarcel Cornu         ret = 0;
56f69ff2b8SMarcel Cornu 
57f69ff2b8SMarcel Cornu end_init:
58f69ff2b8SMarcel Cornu         aligned_free(mgr);
59f69ff2b8SMarcel Cornu 
60f69ff2b8SMarcel Cornu         return ret;
61f69ff2b8SMarcel Cornu }
62f69ff2b8SMarcel Cornu 
633fb7b5f1SMarcel Cornu static int
test_sha512_mb_submit_api(void)643fb7b5f1SMarcel Cornu test_sha512_mb_submit_api(void)
65f69ff2b8SMarcel Cornu {
66*592e639eSPablo de Lara         ISAL_SHA512_HASH_CTX_MGR *mgr = NULL;
67*592e639eSPablo de Lara         ISAL_SHA512_HASH_CTX ctx = { 0 }, *ctx_ptr = &ctx;
68f69ff2b8SMarcel Cornu         int rc, ret = -1;
69f69ff2b8SMarcel Cornu         const char *fn_name = "isal_sha512_ctx_mgr_submit";
70f69ff2b8SMarcel Cornu         static uint8_t msg[] = "Test message";
71f69ff2b8SMarcel Cornu 
72*592e639eSPablo de Lara         rc = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SHA512_HASH_CTX_MGR));
73f69ff2b8SMarcel Cornu         if ((rc != 0) || (mgr == NULL)) {
74f69ff2b8SMarcel Cornu                 printf("posix_memalign failed test aborted\n");
75f69ff2b8SMarcel Cornu                 return 1;
76f69ff2b8SMarcel Cornu         }
77f69ff2b8SMarcel Cornu 
78f69ff2b8SMarcel Cornu         rc = isal_sha512_ctx_mgr_init(mgr);
79f69ff2b8SMarcel Cornu         if (rc != ISAL_CRYPTO_ERR_NONE)
80f69ff2b8SMarcel Cornu                 goto end_submit;
81f69ff2b8SMarcel Cornu 
82f69ff2b8SMarcel Cornu         // Init context before first use
838cb7fe78SPablo de Lara         isal_hash_ctx_init(&ctx);
84f69ff2b8SMarcel Cornu 
85f69ff2b8SMarcel Cornu         // check null mgr
86f69ff2b8SMarcel Cornu         CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_submit(NULL, ctx_ptr, &ctx_ptr, msg,
878cb7fe78SPablo de Lara                                                      (uint32_t) strlen((char *) msg),
888cb7fe78SPablo de Lara                                                      ISAL_HASH_ENTIRE),
89f69ff2b8SMarcel Cornu                           ISAL_CRYPTO_ERR_NULL_MGR, fn_name, end_submit);
90f69ff2b8SMarcel Cornu 
91f69ff2b8SMarcel Cornu         // check null input ctx
92122c1779SMarcel Cornu         CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_submit(mgr, NULL, &ctx_ptr, msg,
938cb7fe78SPablo de Lara                                                      (uint32_t) strlen((char *) msg),
948cb7fe78SPablo de Lara                                                      ISAL_HASH_ENTIRE),
95f69ff2b8SMarcel Cornu                           ISAL_CRYPTO_ERR_NULL_CTX, fn_name, end_submit);
96f69ff2b8SMarcel Cornu 
97f69ff2b8SMarcel Cornu         // check null output ctx
98122c1779SMarcel Cornu         CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_submit(mgr, ctx_ptr, NULL, msg,
998cb7fe78SPablo de Lara                                                      (uint32_t) strlen((char *) msg),
1008cb7fe78SPablo de Lara                                                      ISAL_HASH_ENTIRE),
101f69ff2b8SMarcel Cornu                           ISAL_CRYPTO_ERR_NULL_CTX, fn_name, end_submit);
102f69ff2b8SMarcel Cornu 
103f69ff2b8SMarcel Cornu         // check null source ptr
104f69ff2b8SMarcel Cornu         CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, NULL,
1058cb7fe78SPablo de Lara                                                      (uint32_t) strlen((char *) msg),
1068cb7fe78SPablo de Lara                                                      ISAL_HASH_ENTIRE),
107f69ff2b8SMarcel Cornu                           ISAL_CRYPTO_ERR_NULL_SRC, fn_name, end_submit);
108f69ff2b8SMarcel Cornu 
109f69ff2b8SMarcel Cornu         // check invalid flag
110122c1779SMarcel Cornu         CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
111122c1779SMarcel Cornu                                                      (uint32_t) strlen((char *) msg), 999),
112f69ff2b8SMarcel Cornu                           ISAL_CRYPTO_ERR_INVALID_FLAGS, fn_name, end_submit);
113f69ff2b8SMarcel Cornu 
114f69ff2b8SMarcel Cornu         // simulate internal error (submit in progress job)
1158cb7fe78SPablo de Lara         ctx_ptr->status = ISAL_HASH_CTX_STS_PROCESSING;
116f69ff2b8SMarcel Cornu 
117f69ff2b8SMarcel Cornu         CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
1188cb7fe78SPablo de Lara                                                      (uint32_t) strlen((char *) msg),
1198cb7fe78SPablo de Lara                                                      ISAL_HASH_ENTIRE),
120f69ff2b8SMarcel Cornu                           ISAL_CRYPTO_ERR_ALREADY_PROCESSING, fn_name, end_submit);
121f69ff2b8SMarcel Cornu 
1228cb7fe78SPablo de Lara         CHECK_RETURN_GOTO(ctx_ptr->error, ISAL_HASH_CTX_ERROR_ALREADY_PROCESSING, fn_name,
1238cb7fe78SPablo de Lara                           end_submit);
124f69ff2b8SMarcel Cornu 
125f69ff2b8SMarcel Cornu         // simulate internal error (submit completed job)
1268cb7fe78SPablo de Lara         ctx_ptr->error = ISAL_HASH_CTX_ERROR_NONE;
1278cb7fe78SPablo de Lara         ctx_ptr->status = ISAL_HASH_CTX_STS_COMPLETE;
128f69ff2b8SMarcel Cornu 
129f69ff2b8SMarcel Cornu         CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
1308cb7fe78SPablo de Lara                                                      (uint32_t) strlen((char *) msg),
1318cb7fe78SPablo de Lara                                                      ISAL_HASH_UPDATE),
132f69ff2b8SMarcel Cornu                           ISAL_CRYPTO_ERR_ALREADY_COMPLETED, fn_name, end_submit);
133f69ff2b8SMarcel Cornu 
1348cb7fe78SPablo de Lara         CHECK_RETURN_GOTO(ctx_ptr->error, ISAL_HASH_CTX_ERROR_ALREADY_COMPLETED, fn_name,
1358cb7fe78SPablo de Lara                           end_submit);
136f69ff2b8SMarcel Cornu 
137f69ff2b8SMarcel Cornu         // check valid args
1388cb7fe78SPablo de Lara         isal_hash_ctx_init(&ctx);
139f69ff2b8SMarcel Cornu         CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
1408cb7fe78SPablo de Lara                                                      (uint32_t) strlen((char *) msg),
1418cb7fe78SPablo de Lara                                                      ISAL_HASH_ENTIRE),
142f69ff2b8SMarcel Cornu                           ISAL_CRYPTO_ERR_NONE, fn_name, end_submit);
143f69ff2b8SMarcel Cornu         ret = 0;
144f69ff2b8SMarcel Cornu 
145f69ff2b8SMarcel Cornu end_submit:
146f69ff2b8SMarcel Cornu         aligned_free(mgr);
147f69ff2b8SMarcel Cornu 
148f69ff2b8SMarcel Cornu         return ret;
149f69ff2b8SMarcel Cornu }
150f69ff2b8SMarcel Cornu 
1513fb7b5f1SMarcel Cornu static int
test_sha512_mb_flush_api(void)1523fb7b5f1SMarcel Cornu test_sha512_mb_flush_api(void)
153f69ff2b8SMarcel Cornu {
154*592e639eSPablo de Lara         ISAL_SHA512_HASH_CTX_MGR *mgr = NULL;
155*592e639eSPablo de Lara         ISAL_SHA512_HASH_CTX ctx = { 0 }, *ctx_ptr = &ctx;
156f69ff2b8SMarcel Cornu         int rc, ret = -1;
157f69ff2b8SMarcel Cornu         const char *fn_name = "isal_sha512_ctx_mgr_flush";
158f69ff2b8SMarcel Cornu 
159*592e639eSPablo de Lara         rc = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SHA512_HASH_CTX_MGR));
160f69ff2b8SMarcel Cornu         if ((rc != 0) || (mgr == NULL)) {
161f69ff2b8SMarcel Cornu                 printf("posix_memalign failed test aborted\n");
162f69ff2b8SMarcel Cornu                 return 1;
163f69ff2b8SMarcel Cornu         }
164f69ff2b8SMarcel Cornu 
165f69ff2b8SMarcel Cornu         rc = isal_sha512_ctx_mgr_init(mgr);
166f69ff2b8SMarcel Cornu         if (rc != ISAL_CRYPTO_ERR_NONE)
167f69ff2b8SMarcel Cornu                 goto end_flush;
168f69ff2b8SMarcel Cornu 
169f69ff2b8SMarcel Cornu         // Init context before first use
1708cb7fe78SPablo de Lara         isal_hash_ctx_init(&ctx);
171f69ff2b8SMarcel Cornu 
172f69ff2b8SMarcel Cornu         // check null mgr
1733fb7b5f1SMarcel Cornu         CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_flush(NULL, &ctx_ptr), ISAL_CRYPTO_ERR_NULL_MGR,
1743fb7b5f1SMarcel Cornu                           fn_name, end_flush);
175f69ff2b8SMarcel Cornu 
176f69ff2b8SMarcel Cornu         // check null ctx
1773fb7b5f1SMarcel Cornu         CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_flush(mgr, NULL), ISAL_CRYPTO_ERR_NULL_CTX, fn_name,
1783fb7b5f1SMarcel Cornu                           end_flush);
179f69ff2b8SMarcel Cornu 
180f69ff2b8SMarcel Cornu         // check valid args
1813fb7b5f1SMarcel Cornu         CHECK_RETURN_GOTO(isal_sha512_ctx_mgr_flush(mgr, &ctx_ptr), ISAL_CRYPTO_ERR_NONE, fn_name,
1823fb7b5f1SMarcel Cornu                           end_flush);
183f69ff2b8SMarcel Cornu 
184f69ff2b8SMarcel Cornu         if (ctx_ptr != NULL) {
185f69ff2b8SMarcel Cornu                 printf("test: %s() - expected NULL job ptr\n", fn_name);
186f69ff2b8SMarcel Cornu                 goto end_flush;
187f69ff2b8SMarcel Cornu         }
188f69ff2b8SMarcel Cornu 
189f69ff2b8SMarcel Cornu         ret = 0;
190f69ff2b8SMarcel Cornu 
191f69ff2b8SMarcel Cornu end_flush:
192f69ff2b8SMarcel Cornu         aligned_free(mgr);
193f69ff2b8SMarcel Cornu 
194f69ff2b8SMarcel Cornu         return ret;
195f69ff2b8SMarcel Cornu }
196f69ff2b8SMarcel Cornu #endif /* SAFE_PARAM */
197f69ff2b8SMarcel Cornu 
1983fb7b5f1SMarcel Cornu int
main(void)1993fb7b5f1SMarcel Cornu main(void)
200f69ff2b8SMarcel Cornu {
201f69ff2b8SMarcel Cornu         int fail = 0;
202f69ff2b8SMarcel Cornu 
203f69ff2b8SMarcel Cornu #ifdef SAFE_PARAM
204f69ff2b8SMarcel Cornu         fail |= test_sha512_mb_init_api();
205f69ff2b8SMarcel Cornu         fail |= test_sha512_mb_submit_api();
206f69ff2b8SMarcel Cornu         fail |= test_sha512_mb_flush_api();
207f69ff2b8SMarcel Cornu 
208f69ff2b8SMarcel Cornu         printf(fail ? "Fail\n" : "Pass\n");
209f69ff2b8SMarcel Cornu #else
210f69ff2b8SMarcel Cornu         printf("Not Executed\n");
211f69ff2b8SMarcel Cornu #endif
212f69ff2b8SMarcel Cornu         return fail;
213f69ff2b8SMarcel Cornu }
214