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