xref: /isa-l_crypto/sm3_mb/sm3_mb_param_test.c (revision f6da0bf48af4a49143c2bd4ecaa976ae453273d6)
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         ISAL_SM3_HASH_CTX_MGR *mgr = NULL;
41         int rc, ret = -1;
42 
43         rc = posix_memalign((void *) &mgr, 16, sizeof(ISAL_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         ISAL_SM3_HASH_CTX_MGR *mgr = NULL;
73         ISAL_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(ISAL_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),
88                                                   ISAL_HASH_ENTIRE),
89                           ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, fn_name, end_submit);
90 #else
91         rc = isal_sm3_ctx_mgr_init(mgr);
92         if (rc != ISAL_CRYPTO_ERR_NONE)
93                 goto end_submit;
94 
95         // Init context before first use
96         isal_hash_ctx_init(&ctx);
97 
98         // check null mgr
99         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(NULL, ctx_ptr, &ctx_ptr, msg,
100                                                   (uint32_t) strlen((char *) msg),
101                                                   ISAL_HASH_ENTIRE),
102                           ISAL_CRYPTO_ERR_NULL_MGR, fn_name, end_submit);
103 
104         // check null input ctx
105         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, NULL, &ctx_ptr, msg,
106                                                   (uint32_t) strlen((char *) msg),
107                                                   ISAL_HASH_ENTIRE),
108                           ISAL_CRYPTO_ERR_NULL_CTX, fn_name, end_submit);
109 
110         // check null output ctx
111         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, NULL, msg,
112                                                   (uint32_t) strlen((char *) msg),
113                                                   ISAL_HASH_ENTIRE),
114                           ISAL_CRYPTO_ERR_NULL_CTX, fn_name, end_submit);
115 
116         // check null source ptr
117         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, NULL,
118                                                   (uint32_t) strlen((char *) msg),
119                                                   ISAL_HASH_ENTIRE),
120                           ISAL_CRYPTO_ERR_NULL_SRC, fn_name, end_submit);
121 
122         // check invalid flag
123         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
124                                                   (uint32_t) strlen((char *) msg), 999),
125                           ISAL_CRYPTO_ERR_INVALID_FLAGS, fn_name, end_submit);
126 
127         // simulate internal error (submit in progress job)
128         ctx_ptr->status = ISAL_HASH_CTX_STS_PROCESSING;
129 
130         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
131                                                   (uint32_t) strlen((char *) msg),
132                                                   ISAL_HASH_ENTIRE),
133                           ISAL_CRYPTO_ERR_ALREADY_PROCESSING, fn_name, end_submit);
134 
135         CHECK_RETURN_GOTO(ctx_ptr->error, ISAL_HASH_CTX_ERROR_ALREADY_PROCESSING, fn_name,
136                           end_submit);
137 
138         // simulate internal error (submit completed job)
139         ctx_ptr->error = ISAL_HASH_CTX_ERROR_NONE;
140         ctx_ptr->status = ISAL_HASH_CTX_STS_COMPLETE;
141 
142         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
143                                                   (uint32_t) strlen((char *) msg),
144                                                   ISAL_HASH_UPDATE),
145                           ISAL_CRYPTO_ERR_ALREADY_COMPLETED, fn_name, end_submit);
146 
147         CHECK_RETURN_GOTO(ctx_ptr->error, ISAL_HASH_CTX_ERROR_ALREADY_COMPLETED, fn_name,
148                           end_submit);
149 
150         // check valid args
151         isal_hash_ctx_init(&ctx);
152         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
153                                                   (uint32_t) strlen((char *) msg),
154                                                   ISAL_HASH_ENTIRE),
155                           ISAL_CRYPTO_ERR_NONE, fn_name, end_submit);
156 #endif
157         ret = 0;
158 
159 end_submit:
160         aligned_free(mgr);
161 
162         return ret;
163 }
164 
165 static int
166 test_sm3_mb_flush_api(void)
167 {
168         ISAL_SM3_HASH_CTX_MGR *mgr = NULL;
169         ISAL_SM3_HASH_CTX ctx = { 0 }, *ctx_ptr = &ctx;
170         int rc, ret = -1;
171         const char *fn_name = "isal_sm3_ctx_mgr_flush";
172 
173         rc = posix_memalign((void *) &mgr, 16, sizeof(ISAL_SM3_HASH_CTX_MGR));
174         if ((rc != 0) || (mgr == NULL)) {
175                 printf("posix_memalign failed test aborted\n");
176                 return 1;
177         }
178 
179 #ifdef FIPS_MODE
180         // Check for invalid algorithm error
181         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_flush(mgr, &ctx_ptr), ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO,
182                           fn_name, end_flush);
183 #else
184         rc = isal_sm3_ctx_mgr_init(mgr);
185         if (rc != ISAL_CRYPTO_ERR_NONE)
186                 goto end_flush;
187 
188         // Init context before first use
189         isal_hash_ctx_init(&ctx);
190 
191         // check null mgr
192         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_flush(NULL, &ctx_ptr), ISAL_CRYPTO_ERR_NULL_MGR, fn_name,
193                           end_flush);
194 
195         // check null ctx
196         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_flush(mgr, NULL), ISAL_CRYPTO_ERR_NULL_CTX, fn_name,
197                           end_flush);
198 
199         // check valid args
200         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_flush(mgr, &ctx_ptr), ISAL_CRYPTO_ERR_NONE, fn_name,
201                           end_flush);
202 
203         if (ctx_ptr != NULL) {
204                 printf("test: %s() - expected NULL job ptr\n", fn_name);
205                 goto end_flush;
206         }
207 #endif
208 
209         ret = 0;
210 
211 end_flush:
212         aligned_free(mgr);
213 
214         return ret;
215 }
216 #endif /* SAFE_PARAM */
217 
218 int
219 main(void)
220 {
221         int fail = 0;
222 
223 #ifdef SAFE_PARAM
224         fail |= test_sm3_mb_init_api();
225         fail |= test_sm3_mb_submit_api();
226         fail |= test_sm3_mb_flush_api();
227 
228         printf(fail ? "Fail\n" : "Pass\n");
229 #else
230         printf("Not Executed\n");
231 #endif
232         return fail;
233 }
234