xref: /isa-l_crypto/rolling_hash/rolling_hash2_param_test.c (revision 1e0b122e090c8ad3a8d8c56e182f754ea26fe70d)
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 "rolling_hashx.h"
32 #include "multi_buffer.h"
33 #include "test.h"
34 
35 #ifdef SAFE_PARAM
36 
37 static int
test_rolling_hash2_init_api(void)38 test_rolling_hash2_init_api(void)
39 {
40         int ret = -1;
41         const char *fn_name = "isal_rolling_hash2_init";
42         struct isal_rh_state2 state = { 0 };
43 
44 #ifdef FIPS_MODE
45         // check for invalid algorithm
46         CHECK_RETURN_GOTO(isal_rolling_hash2_init(&state, 5), ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO,
47                           fn_name, end_init);
48 #else
49         // check NULL state
50         CHECK_RETURN_GOTO(isal_rolling_hash2_init(NULL, 32), ISAL_CRYPTO_ERR_NULL_CTX, fn_name,
51                           end_init);
52         // check invalid window size
53         CHECK_RETURN_GOTO(isal_rolling_hash2_init(&state, 500), ISAL_CRYPTO_ERR_WINDOW_SIZE,
54                           fn_name, end_init);
55 
56         // check valid args
57         CHECK_RETURN_GOTO(isal_rolling_hash2_init(&state, 5), ISAL_CRYPTO_ERR_NONE, fn_name,
58                           end_init);
59 #endif
60 
61         ret = 0;
62 end_init:
63         return ret;
64 }
65 
66 static int
test_rolling_hash2_reset_api(void)67 test_rolling_hash2_reset_api(void)
68 {
69         int ret = -1;
70         const char *fn_name = "isal_rolling_hash2_reset";
71         struct isal_rh_state2 state = { 0 };
72         uint8_t init_bytes[64] = { 0 };
73 
74 #ifdef FIPS_MODE
75         // check for invalid algorithm
76         CHECK_RETURN_GOTO(isal_rolling_hash2_reset(&state, init_bytes),
77                           ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, fn_name, end_reset);
78 #else
79         // check NULL state
80         CHECK_RETURN_GOTO(isal_rolling_hash2_reset(NULL, init_bytes), ISAL_CRYPTO_ERR_NULL_CTX,
81                           fn_name, end_reset);
82 
83         // check NULL init bytes
84         CHECK_RETURN_GOTO(isal_rolling_hash2_reset(&state, NULL), ISAL_CRYPTO_ERR_NULL_INIT_VAL,
85                           fn_name, end_reset);
86 
87         // check valid args
88         CHECK_RETURN_GOTO(isal_rolling_hash2_reset(&state, init_bytes), ISAL_CRYPTO_ERR_NONE,
89                           fn_name, end_reset);
90 #endif
91 
92         ret = 0;
93 end_reset:
94         return ret;
95 }
96 
97 static int
test_rolling_hash2_run_api(void)98 test_rolling_hash2_run_api(void)
99 {
100         int ret = -1;
101         const char *fn_name = "isal_rolling_hash2_run";
102         struct isal_rh_state2 state = { 0 };
103         uint8_t buffer[64] = { 0 };
104         uint32_t len = (uint32_t) sizeof(buffer);
105         uint32_t mask = 0xffff0;
106         uint32_t trigger = 0x3df0;
107         uint32_t offset = 0;
108         int match = -1;
109 
110 #ifdef FIPS_MODE
111         // check for invalid algorithm
112         CHECK_RETURN_GOTO(
113                 isal_rolling_hash2_run(&state, buffer, len, mask, trigger, &offset, &match),
114                 ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, fn_name, end_run);
115 #else
116         // check NULL state
117         CHECK_RETURN_GOTO(isal_rolling_hash2_run(NULL, buffer, len, mask, trigger, &offset, &match),
118                           ISAL_CRYPTO_ERR_NULL_CTX, fn_name, end_run);
119 
120         // check NULL source buffer
121         CHECK_RETURN_GOTO(isal_rolling_hash2_run(&state, NULL, len, mask, trigger, &offset, &match),
122                           ISAL_CRYPTO_ERR_NULL_SRC, fn_name, end_run);
123 
124         // check NULL offset
125         CHECK_RETURN_GOTO(isal_rolling_hash2_run(&state, buffer, len, mask, trigger, NULL, &match),
126                           ISAL_CRYPTO_ERR_NULL_OFFSET, fn_name, end_run);
127 
128         // check NULL match
129         CHECK_RETURN_GOTO(isal_rolling_hash2_run(&state, buffer, len, mask, trigger, &offset, NULL),
130                           ISAL_CRYPTO_ERR_NULL_MATCH, fn_name, end_run);
131 
132         // check valid args
133         CHECK_RETURN_GOTO(
134                 isal_rolling_hash2_run(&state, buffer, len, mask, trigger, &offset, &match),
135                 ISAL_CRYPTO_ERR_NONE, fn_name, end_run);
136 #endif
137 
138         ret = 0;
139 end_run:
140         return ret;
141 }
142 
143 static int
test_rolling_hashx_mask_gen_api(void)144 test_rolling_hashx_mask_gen_api(void)
145 {
146         int ret = -1;
147         const char *fn_name = "isal_rolling_hashx_mask_gen";
148         uint32_t mean = 0;
149         uint32_t shift = 0;
150         uint32_t mask = ISAL_FINGERPRINT_RET_OTHER + 1;
151 
152 #ifdef FIPS_MODE
153         // check for invalid algorithm
154         CHECK_RETURN_GOTO(isal_rolling_hashx_mask_gen(mean, shift, &mask),
155                           ISAL_CRYPTO_ERR_FIPS_INVALID_ALGO, fn_name, end_mask_gen);
156 #else
157         // check NULL mask
158         CHECK_RETURN_GOTO(isal_rolling_hashx_mask_gen(mean, shift, NULL), ISAL_CRYPTO_ERR_NULL_MASK,
159                           fn_name, end_mask_gen);
160 
161         // check valid args
162         CHECK_RETURN_GOTO(isal_rolling_hashx_mask_gen(mean, shift, &mask), ISAL_CRYPTO_ERR_NONE,
163                           fn_name, end_mask_gen);
164 
165         // check mask was set to valid value
166         if (mask >= ISAL_FINGERPRINT_RET_OTHER) {
167                 printf("test: %s() - unexpected mask set\n", fn_name);
168                 goto end_mask_gen;
169         }
170 #endif
171 
172         ret = 0;
173 end_mask_gen:
174         return ret;
175 }
176 
177 #endif /* SAFE_PARAM */
178 
179 int
main(void)180 main(void)
181 {
182         int fail = 0;
183 
184 #ifdef SAFE_PARAM
185         fail |= test_rolling_hash2_init_api();
186         fail |= test_rolling_hash2_reset_api();
187         fail |= test_rolling_hash2_run_api();
188         fail |= test_rolling_hashx_mask_gen_api();
189 
190         printf(fail ? "Fail\n" : "Pass\n");
191 #else
192         printf("Not Executed\n");
193 #endif
194         return fail;
195 }
196