xref: /isa-l_crypto/sm3_mb/sm3_mb_param_test.c (revision d030a6c7cca502ce96ccf696cc21d860125792cf)
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          // check invalid flag
106          CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
107                                                    (uint32_t) strlen((char *) msg), 999),
108                            ISAL_CRYPTO_ERR_INVALID_FLAGS, fn_name, end_submit);
109  
110          // simulate internal error (submit in progress job)
111          ctx_ptr->status = HASH_CTX_STS_PROCESSING;
112  
113          CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
114                                                    (uint32_t) strlen((char *) msg), HASH_ENTIRE),
115                            ISAL_CRYPTO_ERR_ALREADY_PROCESSING, fn_name, end_submit);
116  
117          CHECK_RETURN_GOTO(ctx_ptr->error, HASH_CTX_ERROR_ALREADY_PROCESSING, fn_name, end_submit);
118  
119          // simulate internal error (submit completed job)
120          ctx_ptr->error = HASH_CTX_ERROR_NONE;
121          ctx_ptr->status = HASH_CTX_STS_COMPLETE;
122  
123          CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
124                                                    (uint32_t) strlen((char *) msg), HASH_UPDATE),
125                            ISAL_CRYPTO_ERR_ALREADY_COMPLETED, fn_name, end_submit);
126  
127          CHECK_RETURN_GOTO(ctx_ptr->error, HASH_CTX_ERROR_ALREADY_COMPLETED, fn_name, end_submit);
128  
129          // check valid args
130          hash_ctx_init(&ctx);
131          CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_submit(mgr, ctx_ptr, &ctx_ptr, msg,
132                                                    (uint32_t) strlen((char *) msg), HASH_ENTIRE),
133                            ISAL_CRYPTO_ERR_NONE, fn_name, end_submit);
134          ret = 0;
135  
136  end_submit:
137          aligned_free(mgr);
138  
139          return ret;
140  }
141  
142  static int
143  test_sm3_mb_flush_api(void)
144  {
145          SM3_HASH_CTX_MGR *mgr = NULL;
146          SM3_HASH_CTX ctx = { 0 }, *ctx_ptr = &ctx;
147          int rc, ret = -1;
148          const char *fn_name = "isal_sm3_ctx_mgr_flush";
149  
150          rc = posix_memalign((void *) &mgr, 16, sizeof(SM3_HASH_CTX_MGR));
151          if ((rc != 0) || (mgr == NULL)) {
152                  printf("posix_memalign failed test aborted\n");
153                  return 1;
154          }
155  
156          rc = isal_sm3_ctx_mgr_init(mgr);
157          if (rc != ISAL_CRYPTO_ERR_NONE)
158                  goto end_flush;
159  
160          // Init context before first use
161          hash_ctx_init(&ctx);
162  
163          // check null mgr
164          CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_flush(NULL, &ctx_ptr), ISAL_CRYPTO_ERR_NULL_MGR, fn_name,
165                            end_flush);
166  
167          // check null ctx
168          CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_flush(mgr, NULL), ISAL_CRYPTO_ERR_NULL_CTX, fn_name,
169                            end_flush);
170  
171          // check valid args
172          CHECK_RETURN_GOTO(isal_sm3_ctx_mgr_flush(mgr, &ctx_ptr), ISAL_CRYPTO_ERR_NONE, fn_name,
173                            end_flush);
174  
175          if (ctx_ptr != NULL) {
176                  printf("test: %s() - expected NULL job ptr\n", fn_name);
177                  goto end_flush;
178          }
179  
180          ret = 0;
181  
182  end_flush:
183          aligned_free(mgr);
184  
185          return ret;
186  }
187  #endif /* SAFE_PARAM */
188  
189  int
190  main(void)
191  {
192          int fail = 0;
193  
194  #ifdef SAFE_PARAM
195          fail |= test_sm3_mb_init_api();
196          fail |= test_sm3_mb_submit_api();
197          fail |= test_sm3_mb_flush_api();
198  
199          printf(fail ? "Fail\n" : "Pass\n");
200  #else
201          printf("Not Executed\n");
202  #endif
203          return fail;
204  }
205