xref: /isa-l_crypto/sm3_mb/sm3_mb_param_test.c (revision c31516b6ff51e80d7a8eb7bca4e45737b5684b2b)
1 /**********************************************************************
2   Copyright(c) 2024 Intel Corporation All rights reserved.
3   Redistribution and use in source and binary forms, with or without
4   modification, are permitted provided that the following conditions
5   are met:
6     * Redistributions of source code must retain the above copyright
7       notice, this list of conditions and the following disclaimer.
8     * Redistributions in binary form must reproduce the above copyright
9       notice, this list of conditions and the following disclaimer in
10       the documentation and/or other materials provided with the
11       distribution.
12     * Neither the name of Intel Corporation nor the names of its
13       contributors may be used to endorse or promote products derived
14       from this software without specific prior written permission.
15   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 **********************************************************************/
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include "isal_crypto_api.h"
31 #include "sm3_mb.h"
32 #include "multi_buffer.h"
33 #include "test.h"
34 
35 #ifdef SAFE_PARAM
36 
37 static int
38 test_sm3_mb_init_api(void)
39 {
40         SM3_HASH_CTX_MGR *mgr = NULL;
41         int rc, ret = -1;
42 
43         rc = posix_memalign((void *) &mgr, 16, sizeof(SM3_HASH_CTX_MGR));
44         if ((rc != 0) || (mgr == NULL)) {
45                 printf("posix_memalign failed test aborted\n");
46                 return 1;
47         }
48 #ifdef FIPS_MODE
49         // Check for invalid algorithm error
50         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_init(mgr), ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO,
51                           "isal_sm3_ctx_mgr_init", end_init);
52 #else
53         // check null mgr
54         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_init(NULL), ISAL_CRYPTO_ERR_NULL_MGR,
55                           "isal_sm3_ctx_mgr_init", end_init);
56 
57         // check valid args
58         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_init(mgr), ISAL_CRYPTO_ERR_NONE, "isal_sm3_ctx_mgr_init",
59                           end_init);
60 #endif
61         ret = 0;
62 
63 end_init:
64         aligned_free(mgr);
65 
66         return ret;
67 }
68 
69 static int
70 test_sm3_mb_submit_api(void)
71 {
72         SM3_HASH_CTX_MGR *mgr = NULL;
73         SM3_HASH_CTX ctx = { 0 }, *ctx_ptr = &ctx;
74         int rc, ret = -1;
75         const char *fn_name = "isal_sm3_ctx_mgr_submit";
76         static uint8_t msg[] = "Test message";
77 
78         rc = posix_memalign((void *) &mgr, 16, sizeof(SM3_HASH_CTX_MGR));
79         if ((rc != 0) || (mgr == NULL)) {
80                 printf("posix_memalign failed test aborted\n");
81                 return 1;
82         }
83 
84 #ifdef FIPS_MODE
85         // Check for invalid algorithm error
86         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
87                                                   (uint32_t) strlen((char *) msg), HASH_ENTIRE),
88                           ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, fn_name, end_submit);
89 #else
90         rc = isal_sm3_ctx_mgr_init(mgr);
91         if (rc != ISAL_CRYPTO_ERR_NONE)
92                 goto end_submit;
93 
94         // Init context before first use
95         hash_ctx_init(&ctx);
96 
97         // check null mgr
98         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(NULL, ctx_ptr, &ctx_ptr, msg,
99                                                   (uint32_t) strlen((char *) msg), HASH_ENTIRE),
100                           ISAL_CRYPTO_ERR_NULL_MGR, fn_name, end_submit);
101 
102         // check null input ctx
103         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, NULL, &ctx_ptr, msg,
104                                                   (uint32_t) strlen((char *) msg), HASH_ENTIRE),
105                           ISAL_CRYPTO_ERR_NULL_CTX, fn_name, end_submit);
106 
107         // check null output ctx
108         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, NULL, msg,
109                                                   (uint32_t) strlen((char *) msg), HASH_ENTIRE),
110                           ISAL_CRYPTO_ERR_NULL_CTX, fn_name, end_submit);
111 
112         // check null source ptr
113         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, NULL,
114                                                   (uint32_t) strlen((char *) msg), HASH_ENTIRE),
115                           ISAL_CRYPTO_ERR_NULL_SRC, fn_name, end_submit);
116 
117         // check invalid flag
118         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
119                                                   (uint32_t) strlen((char *) msg), 999),
120                           ISAL_CRYPTO_ERR_INVALID_FLAGS, fn_name, end_submit);
121 
122         // simulate internal error (submit in progress job)
123         ctx_ptr->status = HASH_CTX_STS_PROCESSING;
124 
125         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
126                                                   (uint32_t) strlen((char *) msg), HASH_ENTIRE),
127                           ISAL_CRYPTO_ERR_ALREADY_PROCESSING, fn_name, end_submit);
128 
129         CHECK_RETURN_GOTO(ctx_ptr->error, HASH_CTX_ERROR_ALREADY_PROCESSING, fn_name, end_submit);
130 
131         // simulate internal error (submit completed job)
132         ctx_ptr->error = HASH_CTX_ERROR_NONE;
133         ctx_ptr->status = HASH_CTX_STS_COMPLETE;
134 
135         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
136                                                   (uint32_t) strlen((char *) msg), HASH_UPDATE),
137                           ISAL_CRYPTO_ERR_ALREADY_COMPLETED, fn_name, end_submit);
138 
139         CHECK_RETURN_GOTO(ctx_ptr->error, HASH_CTX_ERROR_ALREADY_COMPLETED, fn_name, end_submit);
140 
141         // check valid args
142         hash_ctx_init(&ctx);
143         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
144                                                   (uint32_t) strlen((char *) msg), HASH_ENTIRE),
145                           ISAL_CRYPTO_ERR_NONE, fn_name, end_submit);
146 #endif
147         ret = 0;
148 
149 end_submit:
150         aligned_free(mgr);
151 
152         return ret;
153 }
154 
155 static int
156 test_sm3_mb_flush_api(void)
157 {
158         SM3_HASH_CTX_MGR *mgr = NULL;
159         SM3_HASH_CTX ctx = { 0 }, *ctx_ptr = &ctx;
160         int rc, ret = -1;
161         const char *fn_name = "isal_sm3_ctx_mgr_flush";
162 
163         rc = posix_memalign((void *) &mgr, 16, sizeof(SM3_HASH_CTX_MGR));
164         if ((rc != 0) || (mgr == NULL)) {
165                 printf("posix_memalign failed test aborted\n");
166                 return 1;
167         }
168 
169 #ifdef FIPS_MODE
170         // Check for invalid algorithm error
171         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_flush(mgr, &ctx_ptr), ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO,
172                           fn_name, end_flush);
173 #else
174         rc = isal_sm3_ctx_mgr_init(mgr);
175         if (rc != ISAL_CRYPTO_ERR_NONE)
176                 goto end_flush;
177 
178         // Init context before first use
179         hash_ctx_init(&ctx);
180 
181         // check null mgr
182         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_flush(NULL, &ctx_ptr), ISAL_CRYPTO_ERR_NULL_MGR, fn_name,
183                           end_flush);
184 
185         // check null ctx
186         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_flush(mgr, NULL), ISAL_CRYPTO_ERR_NULL_CTX, fn_name,
187                           end_flush);
188 
189         // check valid args
190         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_flush(mgr, &ctx_ptr), ISAL_CRYPTO_ERR_NONE, fn_name,
191                           end_flush);
192 
193         if (ctx_ptr != NULL) {
194                 printf("test: %s() - expected NULL job ptr\n", fn_name);
195                 goto end_flush;
196         }
197 #endif
198 
199         ret = 0;
200 
201 end_flush:
202         aligned_free(mgr);
203 
204         return ret;
205 }
206 #endif /* SAFE_PARAM */
207 
208 int
209 main(void)
210 {
211         int fail = 0;
212 
213 #ifdef SAFE_PARAM
214         fail |= test_sm3_mb_init_api();
215         fail |= test_sm3_mb_submit_api();
216         fail |= test_sm3_mb_flush_api();
217 
218         printf(fail ? "Fail\n" : "Pass\n");
219 #else
220         printf("Not Executed\n");
221 #endif
222         return fail;
223 }
224