xref: /isa-l_crypto/sm3_mb/sm3_mb_param_test.c (revision 37c1320fef459b6ac01a1fae6f17aa16f54f55fb)
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         // check null mgr
49         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_init(NULL), ISAL_CRYPTO_ERR_NULL_MGR,
50                           "isal_sm3_ctx_mgr_init", end_init);
51 
52         // check valid args
53         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_init(mgr), ISAL_CRYPTO_ERR_NONE, "isal_sm3_ctx_mgr_init",
54                           end_init);
55         ret = 0;
56 
57 end_init:
58         aligned_free(mgr);
59 
60         return ret;
61 }
62 
63 static int
64 test_sm3_mb_submit_api(void)
65 {
66         SM3_HASH_CTX_MGR *mgr = NULL;
67         SM3_HASH_CTX ctx = { 0 }, *ctx_ptr = &ctx;
68         int rc, ret = -1;
69         const char *fn_name = "isal_sm3_ctx_mgr_submit";
70         static uint8_t msg[] = "Test message";
71 
72         rc = posix_memalign((void *) &mgr, 16, sizeof(SM3_HASH_CTX_MGR));
73         if ((rc != 0) || (mgr == NULL)) {
74                 printf("posix_memalign failed test aborted\n");
75                 return 1;
76         }
77 
78         rc = isal_sm3_ctx_mgr_init(mgr);
79         if (rc != ISAL_CRYPTO_ERR_NONE)
80                 goto end_submit;
81 
82         // Init context before first use
83         hash_ctx_init(&ctx);
84 
85         // check null mgr
86         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(NULL, ctx_ptr, &ctx_ptr, msg,
87                                                   (uint32_t) strlen((char *) msg), HASH_ENTIRE),
88                           ISAL_CRYPTO_ERR_NULL_MGR, fn_name, end_submit);
89 
90         // check null input ctx
91         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, NULL, &ctx_ptr, msg,
92                                                   (uint32_t) strlen((char *) msg), HASH_ENTIRE),
93                           ISAL_CRYPTO_ERR_NULL_CTX, fn_name, end_submit);
94 
95         // check null output ctx
96         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, NULL, msg,
97                                                   (uint32_t) strlen((char *) msg), HASH_ENTIRE),
98                           ISAL_CRYPTO_ERR_NULL_CTX, fn_name, end_submit);
99 
100         // check null source ptr
101         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, NULL,
102                                                   (uint32_t) strlen((char *) msg), HASH_ENTIRE),
103                           ISAL_CRYPTO_ERR_NULL_SRC, fn_name, end_submit);
104 
105 #ifdef FIPS_MODE
106         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
107                                                   (uint32_t) strlen((char *) msg), HASH_ENTIRE),
108                           ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, fn_name, end_submit);
109 #else
110         // check invalid flag
111         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
112                                                   (uint32_t) strlen((char *) msg), 999),
113                           ISAL_CRYPTO_ERR_INVALID_FLAGS, fn_name, end_submit);
114 
115         // simulate internal error (submit in progress job)
116         ctx_ptr->status = HASH_CTX_STS_PROCESSING;
117 
118         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
119                                                   (uint32_t) strlen((char *) msg), HASH_ENTIRE),
120                           ISAL_CRYPTO_ERR_ALREADY_PROCESSING, fn_name, end_submit);
121 
122         CHECK_RETURN_GOTO(ctx_ptr->error, HASH_CTX_ERROR_ALREADY_PROCESSING, fn_name, end_submit);
123 
124         // simulate internal error (submit completed job)
125         ctx_ptr->error = HASH_CTX_ERROR_NONE;
126         ctx_ptr->status = HASH_CTX_STS_COMPLETE;
127 
128         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
129                                                   (uint32_t) strlen((char *) msg), HASH_UPDATE),
130                           ISAL_CRYPTO_ERR_ALREADY_COMPLETED, fn_name, end_submit);
131 
132         CHECK_RETURN_GOTO(ctx_ptr->error, HASH_CTX_ERROR_ALREADY_COMPLETED, fn_name, end_submit);
133 
134         // check valid args
135         hash_ctx_init(&ctx);
136         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
137                                                   (uint32_t) strlen((char *) msg), HASH_ENTIRE),
138                           ISAL_CRYPTO_ERR_NONE, fn_name, end_submit);
139 #endif
140         ret = 0;
141 
142 end_submit:
143         aligned_free(mgr);
144 
145         return ret;
146 }
147 
148 static int
149 test_sm3_mb_flush_api(void)
150 {
151         SM3_HASH_CTX_MGR *mgr = NULL;
152         SM3_HASH_CTX ctx = { 0 }, *ctx_ptr = &ctx;
153         int rc, ret = -1;
154         const char *fn_name = "isal_sm3_ctx_mgr_flush";
155 
156         rc = posix_memalign((void *) &mgr, 16, sizeof(SM3_HASH_CTX_MGR));
157         if ((rc != 0) || (mgr == NULL)) {
158                 printf("posix_memalign failed test aborted\n");
159                 return 1;
160         }
161 
162         rc = isal_sm3_ctx_mgr_init(mgr);
163         if (rc != ISAL_CRYPTO_ERR_NONE)
164                 goto end_flush;
165 
166         // Init context before first use
167         hash_ctx_init(&ctx);
168 
169         // check null mgr
170         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_flush(NULL, &ctx_ptr), ISAL_CRYPTO_ERR_NULL_MGR, fn_name,
171                           end_flush);
172 
173         // check null ctx
174         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_flush(mgr, NULL), ISAL_CRYPTO_ERR_NULL_CTX, fn_name,
175                           end_flush);
176 
177         // check valid args
178 #ifdef FIPS_MODE
179         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_flush(mgr, &ctx_ptr), ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO,
180                           fn_name, end_flush);
181 #else
182         CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_flush(mgr, &ctx_ptr), ISAL_CRYPTO_ERR_NONE, fn_name,
183                           end_flush);
184 
185         if (ctx_ptr != NULL) {
186                 printf("test: %s() - expected NULL job ptr\n", fn_name);
187                 goto end_flush;
188         }
189 #endif
190 
191         ret = 0;
192 
193 end_flush:
194         aligned_free(mgr);
195 
196         return ret;
197 }
198 #endif /* SAFE_PARAM */
199 
200 int
201 main(void)
202 {
203         int fail = 0;
204 
205 #ifdef SAFE_PARAM
206         fail |= test_sm3_mb_init_api();
207         fail |= test_sm3_mb_submit_api();
208         fail |= test_sm3_mb_flush_api();
209 
210         printf(fail ? "Fail\n" : "Pass\n");
211 #else
212         printf("Not Executed\n");
213 #endif
214         return fail;
215 }
216