Debian Codegraph @

test/canlocktest_multi.c

raw
1 /* =============================================================================
2 * Copyright (c) 2021 Michael Baeuerle
3 *
4 * All rights reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, and/or sell copies of the Software, and to permit persons
11 * to whom the Software is furnished to do so, provided that the above
12 * copyright notice(s) and this permission notice appear in all copies of
13 * the Software and that both the above copyright notice(s) and this
14 * permission notice appear in supporting documentation.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
19 * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20 * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY
21 * SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
22 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
23 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
24 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
25 *
26 * Except as contained in this notice, the name of a copyright holder
27 * shall not be used in advertising or otherwise to promote the sale, use
28 * or other dealings in this Software without prior written authorization
29 * of the copyright holder.
30 *
31 * SPDX-License-Identifier: ICU
32 */
33
34 /* Test program for new cl_verify_multi() API available since version 3.3 */
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include "canlock.h"
40
41
42 /* ========================================================================== */
43 /* Test program for new cl_verify_multi() API from version 3.3 */
44
45 int main(void)
46 {
47 cl_hash_version reject_hash[3];
48 const char *c_keys, *c_locks;
49 int rv;
50 int failed = 0;
51
52 /* Example from RFC8315 Section 5.1 */
53 printf("Test 1\n\n");
54 c_keys = "sha256:qv1VXHYiCGjkX/N1nhfYKcAeUn8bCVhrWhoKuBSnpMA=";
55 c_locks = "sha256:s/pmK/3grrz++29ce2/mQydzJuc7iqHn1nqcJiQTPMc=";
56 printf("Deactivated hash algorithms: None\n");
57 printf("Keys : %s\n", c_keys);
58 printf("Locks: %s\n", c_locks);
59 rv = cl_verify_multi(NULL, c_keys, c_locks);
60 printf("\n");
61 printf("%s\n\n", "Expected result: GOOD");
62 if (rv)
63 {
64 printf("BAD: Result was not as expected\n");
65 failed = 1;
66 }
67 else
68 {
69 printf("GOOD\n");
70 }
71 printf("\n----------------------------------------"
72 "----------------------------------------\n\n");
73
74 /* Example from RFC8315 Section 5.1 */
75 printf("Test 2\n\n");
76 c_keys = "sha256:qv1VXHYiCGjkX/N1nhfYKcAeUn8bCVhrWhoKuBSnpMA=";
77 c_locks = "sha256:s/pmK/3grrz++29ce2/mQydzJuc7iqHn1nqcJiQTPMc=";
78 printf("Deactivated hash algorithms: None (with empty list)\n");
79 reject_hash[0] = CL_INVALID;
80 printf("Keys : %s\n", c_keys);
81 printf("Locks: %s\n", c_locks);
82 rv = cl_verify_multi(reject_hash, c_keys, c_locks);
83 printf("\n");
84 printf("%s\n\n", "Expected result: GOOD");
85 if (rv)
86 {
87 printf("BAD: Result was not as expected\n");
88 failed = 1;
89 }
90 else
91 {
92 printf("GOOD\n");
93 }
94 printf("\n----------------------------------------"
95 "----------------------------------------\n\n");
96
97 /* Example from RFC8315 Section 5.1 */
98 printf("Test 3\n\n");
99 c_keys = "sha256:qv1VXHYiCGjkX/N1nhfYKcAeUn8bCVhrWhoKuBSnpMA=";
100 c_locks = "sha256:s/pmK/3grrz++29ce2/mQydzJuc7iqHn1nqcJiQTPMc=";
101 printf("Deactivated hash algorithms: SHA256\n");
102 reject_hash[0] = CL_SHA256;
103 reject_hash[1] = CL_INVALID;
104 printf("Keys : %s\n", c_keys);
105 printf("Locks: %s\n", c_locks);
106 rv = cl_verify_multi(reject_hash, c_keys, c_locks);
107 printf("\n");
108 printf("%s\n\n", "Expected result: BAD");
109 if (!rv)
110 {
111 printf("GOOD: Result was not as expected\n");
112 failed = 1;
113 }
114 else
115 {
116 printf("BAD\n");
117 }
118 printf("\n----------------------------------------"
119 "----------------------------------------\n\n");
120
121 /* Example from RFC8315 Section 5.3 */
122 printf("Test 4\n\n");
123 c_keys = "sha256:qv1VXHYiCGjkX/N1nhfYKcAeUn8bCVhrWhoKuBSnpMA= "
124 "sha256:yM0ep490Fzt83CLYYAytm3S2HasHhYG4LAeAlmuSEys= "
125 "sha256:sSkDke97Dh78/d+Diu1i3dQ2Fp/EMK3xE2GfEqZlvK8= "
126 "ShA1:aaaBBBcccDDDeeeFFF";
127 c_locks = "sha256:s/pmK/3grrz++29ce2/mQydzJuc7iqHn1nqcJiQTPMc= "
128 "sha256:NSBTz7BfcQFTCen+U4lQ0VS8VIlZao2b8mxD/xJaaeE= "
129 "sha256:RrKLp7YCQc9T8HmgSbxwIDlnCDWsgy1awqtiDuhedRo= "
130 "sha1:bNXHc6ohSmeHaRHHW56BIWZJt+4=";
131 printf("Deactivated hash algorithms: None\n");
132 printf("Keys : %s\n", c_keys);
133 printf("Locks: %s\n", c_locks);
134 rv = cl_verify_multi(NULL, c_keys, c_locks);
135 printf("\n");
136 printf("%s\n\n", "Expected result: GOOD");
137 if (rv)
138 {
139 printf("BAD: Result was not as expected\n");
140 failed = 1;
141 }
142 else
143 {
144 printf("GOOD\n");
145 }
146 printf("\n----------------------------------------"
147 "----------------------------------------\n\n");
148
149 /* Example from RFC8315 Section 5.3 */
150 printf("Test 5\n\n");
151 c_keys = "sha256:qv1VXHYiCGjkX/N1nhfYKcAeUn8bCVhrWhoKuBSnpMA= "
152 "sha256:yM0ep490Fzt83CLYYAytm3S2HasHhYG4LAeAlmuSEys= "
153 "sha256:sSkDke97Dh78/d+Diu1i3dQ2Fp/EMK3xE2GfEqZlvK8= "
154 "ShA1:aaaBBBcccDDDeeeFFF";
155 c_locks = "sha256:s/pmK/3grrz++29ce2/mQydzJuc7iqHn1nqcJiQTPMc= "
156 "sha256:NSBTz7BfcQFTCen+U4lQ0VS8VIlZao2b8mxD/xJaaeE= "
157 "sha256:RrKLp7YCQc9T8HmgSbxwIDlnCDWsgy1awqtiDuhedRo= "
158 "sha1:bNXHc6ohSmeHaRHHW56BIWZJt+4=";
159 printf("Deactivated hash algorithms: SHA1, SHA256\n");
160 reject_hash[0] = CL_SHA1;
161 reject_hash[1] = CL_SHA256;
162 reject_hash[2] = CL_INVALID;
163 printf("Keys : %s\n", c_keys);
164 printf("Locks: %s\n", c_locks);
165 rv = cl_verify_multi(reject_hash, c_keys, c_locks);
166 printf("\n");
167 printf("%s\n\n", "Expected result: BAD");
168 if (!rv)
169 {
170 printf("GOOD: Result was not as expected\n");
171 failed = 1;
172 }
173 else
174 {
175 printf("BAD\n");
176 }
177 printf("\n----------------------------------------"
178 "----------------------------------------\n\n");
179
180 /* Example from RFC8315 Section 5.3 */
181 printf("Test 6\n\n");
182 c_keys = "sha256:qv1VXHYiCGjkX/N1nhfYKcAeUn8bCVhrWhoKuBSnpMA= "
183 "sha256:yM0ep490Fzt83CLYYAytm3S2HasHhYG4LAeAlmuSEys= "
184 "sha256:sSkDke97Dh78/d+Diu1i3dQ2Fp/EMK3xE2GfEqZlvK8= "
185 "ShA1:aaaBBBcccDDDeeeFFF";
186 c_locks = "sha256:s/pmK/3grrz++29ce2/mQydzJuc7iqHn1nqcJiQTPMc= "
187 "sha256:NSBTz7BfcQFTCen+U4lQ0VS8VIlZao2b8mxD/xJaaeE= "
188 "sha256:RrKLp7YCQc9T8HmgSbxwIDlnCDWsgy1awqtiDuhedRo= "
189 "sha1:bNXHc6ohSmeHaRHHW56BIWZJt+4=";
190 printf("Deactivated hash algorithms: SHA256\n");
191 reject_hash[0] = CL_SHA256;
192 reject_hash[1] = CL_INVALID;
193 printf("Keys : %s\n", c_keys);
194 printf("Locks: %s\n", c_locks);
195 rv = cl_verify_multi(reject_hash, c_keys, c_locks);
196 printf("\n");
197 printf("%s\n\n", "Expected result: GOOD");
198 if (rv)
199 {
200 printf("BAD: Result was not as expected\n");
201 failed = 1;
202 }
203 else
204 {
205 printf("GOOD\n");
206 }
207 printf("\n----------------------------------------"
208 "----------------------------------------\n\n");
209
210 /* Example from RFC8315 Section 5.3 */
211 printf("Test 7\n\n");
212 c_keys = "sha256:qv1VXHYiCGjkX/N1nhfYKcAeUn8bCVhrWhoKuBSnpMA= "
213 "sha256:yM0ep490Fzt83CLYYAytm3S2HasHhYG4LAeAlmuSEys= "
214 "sha256:sSkDke97Dh78/d+Diu1i3dQ2Fp/EMK3xE2GfEqZlvK8= "
215 "ShA1:aaaBBBcccDDDeeeFFF";
216 c_locks = "sha256:s/pmK/3grrz++29ce2/mQydzJuc7iqHn1nqcJiQTPMc= "
217 "sha256:NSBTz7BfcQFTCen+U4lQ0VS8VIlZao2b8mxD/xJaaeE= "
218 "sha256:RrKLp7YCQc9T8HmgSbxwIDlnCDWsgy1awqtiDuhedRo=";
219 printf("Deactivated hash algorithms: SHA256\n");
220 reject_hash[0] = CL_SHA256;
221 reject_hash[1] = CL_INVALID;
222 printf("Keys : %s\n", c_keys);
223 printf("Locks: %s\n", c_locks);
224 rv = cl_verify_multi(reject_hash, c_keys, c_locks);
225 printf("\n");
226 printf("%s\n\n", "Expected result: BAD");
227 if (!rv)
228 {
229 printf("GOOD: Result was not as expected\n");
230 failed = 1;
231 }
232 else
233 {
234 printf("BAD\n");
235 }
236 printf("\n----------------------------------------"
237 "----------------------------------------\n\n");
238
239 /*
240 * Example from RFC8315 Section 5.3
241 * Use some more whitespace between the elements to test the parser.
242 */
243 printf("Test 8\n\n");
244 c_keys = " sha256:qv1VXHYiCGjkX/N1nhfYKcAeUn8bCVhrWhoKuBSnpMA= "
245 " sha256:yM0ep490Fzt83CLYYAytm3S2HasHhYG4LAeAlmuSEys= "
246 " sha256:sSkDke97Dh78/d+Diu1i3dQ2Fp/EMK3xE2GfEqZlvK8= "
247 " ShA1:aaaBBBcccDDDeeeFFF ";
248 c_locks = " sha256:s/pmK/3grrz++29ce2/mQydzJuc7iqHn1nqcJiQTPMc= "
249 " sha1:bNXHc6ohSmeHaRHHW56BIWZJt+4= "
250 " sha256:NSBTz7BfcQFTCen+U4lQ0VS8VIlZao2b8mxD/xJaaeE= "
251 " sha256:RrKLp7YCQc9T8HmgSbxwIDlnCDWsgy1awqtiDuhedRo= ";
252 printf("Deactivated hash algorithms: SHA256\n");
253 reject_hash[0] = CL_SHA256;
254 reject_hash[1] = CL_INVALID;
255 printf("Keys : %s\n", c_keys);
256 printf("Locks: %s\n", c_locks);
257 rv = cl_verify_multi(reject_hash, c_keys, c_locks);
258 printf("\n");
259 printf("%s\n\n", "Expected result: GOOD");
260 if (rv)
261 {
262 printf("BAD: Result was not as expected\n");
263 failed = 1;
264 }
265 else
266 {
267 printf("GOOD\n");
268 }
269 printf("\n----------------------------------------"
270 "----------------------------------------\n\n");
271
272 /* Test with SHA512 */
273 printf("Test 9\n\n");
274 c_keys = "Sha512:ryoikFW3wKefmYr+zDzKn16ngNf1eYbZ0DN+3yqCbkid3HxU5K99G7RcNEx1UxiL3ZQfwg1+TDhH96D+tCcXGQ==";
275 c_locks = "shA512:Hq6MQ2JMzGf56agcqYPEMnoWHbQMSAG0eE0ABHgktP8cKL6/A4bvydjUAa0h7sHUU8vdfWXK7eUYG/pnDxgitg==";
276 printf("Deactivated hash algorithms: SHA1, SHA256\n");
277 reject_hash[0] = CL_SHA1;
278 reject_hash[1] = CL_SHA256;
279 reject_hash[2] = CL_INVALID;
280 printf("Keys : %s\n", c_keys);
281 printf("Locks: %s\n", c_locks);
282 rv = cl_verify_multi(reject_hash, c_keys, c_locks);
283 printf("\n");
284 printf("%s\n\n", "Expected result: GOOD");
285 if (rv)
286 {
287 printf("BAD: Result was not as expected\n");
288 failed = 1;
289 }
290 else
291 {
292 printf("GOOD\n");
293 }
294 printf("\n----------------------------------------"
295 "----------------------------------------\n\n");
296
297 /* Test with SHA384 and some unsupported hash algorithm */
298 printf("Test 10\n\n");
299 c_keys = "unsupported:xxx sha384:MhUl2H4gnJ6Wsji2Ybloz1mzvVX7zKqhfuzBOW+l8t86pVUlio4Vx7E+6KkGAfTZ";
300 c_locks = "unsupported:yyy sha384:CwL4/RWxnWroRrDuWoV/caCyg9zHNYthlxMU64V660K2VF6PUQN3eqruyPPQ1Gfh";
301 printf("Deactivated hash algorithms: SHA1, SHA224\n");
302 reject_hash[0] = CL_SHA1;
303 reject_hash[1] = CL_SHA224;
304 reject_hash[2] = CL_INVALID;
305 printf("Keys : %s\n", c_keys);
306 printf("Locks: %s\n", c_locks);
307 rv = cl_verify_multi(reject_hash, c_keys, c_locks);
308 printf("\n");
309 printf("%s\n\n", "Expected result: GOOD");
310 if (rv)
311 {
312 printf("BAD: Result was not as expected\n");
313 failed = 1;
314 }
315 else
316 {
317 printf("GOOD\n");
318 }
319 printf("\n----------------------------------------"
320 "----------------------------------------\n\n");
321
322 /* Check for success */
323 if (!failed) exit(EXIT_SUCCESS);
324 exit(EXIT_FAILURE);
325 }
326
327
328 /* EOF */