"Fossies" - the Fresh Open Source Software Archive 
Member "tin-2.6.2/libcanlock/test/canlocktest.c" (23 Aug 2021, 10118 Bytes) of package /linux/misc/tin-2.6.2.tar.xz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the last
Fossies "Diffs" side-by-side code changes report for "canlocktest.c":
2.4.5_vs_2.6.0.
1 /* =============================================================================
2 * Copyright (c) 2017-2018 Michael Baeuerle
3 * Copyright (c) 2003 G.J. Andruk
4 *
5 * All rights reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, and/or sell copies of the Software, and to permit persons
12 * to whom the Software is furnished to do so, provided that the above
13 * copyright notice(s) and this permission notice appear in all copies of
14 * the Software and that both the above copyright notice(s) and this
15 * permission notice appear in supporting documentation.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
20 * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
21 * HOLDERS INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY
22 * SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER
23 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
24 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
25 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 *
27 * Except as contained in this notice, the name of a copyright holder
28 * shall not be used in advertising or otherwise to promote the sale, use
29 * or other dealings in this Software without prior written authorization
30 * of the copyright holder.
31 */
32
33 /* Test program for new API available since version 3 */
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include "canlock.h"
39
40
41 #define BUFFSIZE 128
42
43
44 /* ========================================================================== */
45 /* Get <scheme> element */
46
47 static const char *check_scheme(cl_hash_version hash)
48 {
49 const char *scheme;
50
51 switch (hash)
52 {
53 case CL_SHA1:
54 scheme = "sha1";
55 break;
56 case CL_SHA224:
57 scheme = "sha224";
58 break;
59 case CL_SHA256:
60 scheme = "sha256";
61 break;
62 case CL_SHA384:
63 scheme = "sha384";
64 break;
65 case CL_SHA512:
66 scheme = "sha512";
67 break;
68 default:
69 /* Not supported */
70 return NULL;
71 }
72
73 return scheme;
74 }
75
76
77 /* ========================================================================== */
78 /* Check whether Cancel-Key matches Cancel-Lock */
79
80 static int checker(cl_hash_version hash, char *key, char *lock)
81 {
82 int res = -1;
83 cl_hash_version hash_key, hash_lock;
84 char *string_key, *string_lock;
85 const char *scheme;
86
87 printf("%s\n%s,%s\n", "Check Cancel-Key,Cancel-Lock:", key, lock);
88
89 hash_key = cl_split(key, &string_key);
90 hash_lock = cl_split(lock, &string_lock);
91
92 if (hash_key && hash_lock)
93 {
94 /* Check whether <scheme> matches */
95 if (hash_key == hash_lock)
96 {
97 if (!cl_verify(hash, string_key, string_lock))
98 {
99 printf("\nGOOD\n");
100 res = 0;
101 }
102 else
103 printf("\nBAD\n");
104 }
105 else
106 printf("\nBAD: Scheme mismatch (%s/%s)\n",
107 check_scheme(hash_key), check_scheme(hash_lock));
108 }
109 else
110 printf("\nBAD: Scheme not supported\n");
111
112 return res;
113 }
114
115
116 /* ========================================================================== */
117 /* Test program for new API from version 3 */
118
119 int main(void)
120 {
121 char *c_key = NULL, *c_lock = NULL;
122 const char sec[] = "ExampleSecret";
123 const char sec2[] = "AnotherSecret";
124 const char mid[] = "<12345@mid.example>";
125 const char uid[] = "JaneDoe";
126 char *uid_mid = NULL;
127 int rv;
128 int failed = 0;
129
130 /* First test (SHA256 without UID) */
131 printf("Test 1 (SHA256 without UID)\n\n");
132 printf("Secret : %s\n", sec);
133 printf("Message-ID: %s\n", mid);
134 c_key = cl_get_key(CL_SHA256, (const unsigned char *) sec, strlen(sec),
135 (const unsigned char *) mid, strlen(mid));
136 c_lock = cl_get_lock(CL_SHA256, (const unsigned char *) sec, strlen(sec),
137 (const unsigned char *) mid, strlen(mid));
138 printf("\n");
139 printf("%s\n%s,%s\n\n", "Expected Cancel-Key,Cancel-Lock:",
140 "sha256:qv1VXHYiCGjkX/N1nhfYKcAeUn8bCVhrWhoKuBSnpMA=",
141 "sha256:s/pmK/3grrz++29ce2/mQydzJuc7iqHn1nqcJiQTPMc=");
142 if (strcmp(c_key, "sha256:qv1VXHYiCGjkX/N1nhfYKcAeUn8bCVhrWhoKuBSnpMA="))
143 {
144 printf("BAD: Cancel-Key not as expected (%s)\n", c_key);
145 failed = 1;
146 }
147 else
148 {
149 if (strcmp(c_lock, "sha256:s/pmK/3grrz++29ce2/mQydzJuc7iqHn1nqcJiQTPMc="))
150 {
151 printf("BAD: Cancel-Lock not as expected (%s)\n", c_lock);
152 failed = 1;
153 }
154 else
155 {
156 rv = checker(CL_SHA256, c_key, c_lock);
157 if (rv) failed = 1;
158 }
159 }
160 free((void *) c_lock);
161 free((void *) c_key);
162 printf("\n----------------------------------------"
163 "----------------------------------------\n\n");
164
165 /* Second test (SHA256 with UID) */
166 printf("Test 2 (SHA256 with UID)\n\n");
167 printf("Secret : %s\n", sec2);
168 printf("User-ID : %s\n", uid);
169 printf("Message-ID: %s\n", mid);
170 uid_mid = (char *) malloc(strlen(uid) + strlen(mid) + (size_t) 1);
171 strcpy(uid_mid, uid);
172 strcat(uid_mid, mid);
173 c_key = cl_get_key(CL_SHA256, (const unsigned char *) sec2, strlen(sec2),
174 (const unsigned char *) uid_mid, strlen(uid_mid));
175 c_lock = cl_get_lock(CL_SHA256, (const unsigned char *) sec2, strlen(sec2),
176 (const unsigned char *) uid_mid, strlen(uid_mid));
177 free((void *) uid_mid);
178 printf("\n");
179 printf("%s\n%s,%s\n\n", "Expected Cancel-Key,Cancel-Lock:",
180 "sha256:yM0ep490Fzt83CLYYAytm3S2HasHhYG4LAeAlmuSEys=",
181 "sha256:NSBTz7BfcQFTCen+U4lQ0VS8VIlZao2b8mxD/xJaaeE=");
182
183 if (strcmp(c_key, "sha256:yM0ep490Fzt83CLYYAytm3S2HasHhYG4LAeAlmuSEys="))
184 {
185 printf("BAD: Cancel-Key not as expected (%s)\n", c_key);
186 failed = 1;
187 }
188 else
189 {
190 if (strcmp(c_lock, "sha256:NSBTz7BfcQFTCen+U4lQ0VS8VIlZao2b8mxD/xJaaeE="))
191 {
192 printf("BAD: Cancel-Lock not as expected (%s)\n", c_lock);
193 failed = 1;
194 }
195 else
196 {
197 rv = checker(CL_SHA256, c_key, c_lock);
198 if (rv) failed = 1;
199 }
200 }
201 free((void *) c_lock);
202 free((void *) c_key);
203 printf("\n----------------------------------------"
204 "----------------------------------------\n\n");
205
206 /* Test 3 (Check SHA256) */
207 printf("Test 3 (Check SHA256)\n\n");
208 c_key = "shA256:sSkDke97Dh78/d+Diu1i3dQ2Fp/EMK3xE2GfEqZlvK8=";
209 c_lock = "sHa256:RrKLp7YCQc9T8HmgSbxwIDlnCDWsgy1awqtiDuhedRo=";
210 rv = checker(CL_SHA256, c_key, c_lock);
211 if (rv) failed = 1;
212 printf("\n----------------------------------------"
213 "----------------------------------------\n\n");
214
215 /* Test 4 (Check SHA1) */
216 printf("Test 4 (Check SHA1)\n\n");
217 c_key = "ShA1:aaaBBBcccDDDeeeFFF";
218 c_lock = "sha1:bNXHc6ohSmeHaRHHW56BIWZJt+4=";
219 rv = checker(CL_SHA1, c_key, c_lock);
220 if (rv) failed = 1;
221 printf("\n----------------------------------------"
222 "----------------------------------------\n\n");
223
224 /* Test 5 (Check SHA1 with <clue-string> element) */
225 printf("Test 5 (Check SHA1 with <clue-string> element)\n\n");
226 #if 0 /* Skip because this requires V2 legacy API */
227 c_key = "ShA1:aaaBBBcccDDDeeeFFF:bN";
228 c_lock = "sha1:bNXHc6ohSmeHaRHHW56BIWZJt+4=";
229 rv = checker(CL_SHA1, c_key, c_lock);
230 if (rv) failed = 1;
231 #endif
232 printf("SKIP: <clue-string> is obsolete since >20 years\n");
233 printf("\n----------------------------------------"
234 "----------------------------------------\n\n");
235
236 /* Test 6 (Check SHA512) */
237 printf("Test 6 (Check SHA512)\n\n");
238 c_key = "sha512:ryoikFW3wKefmYr+zDzKn16ngNf1eYbZ0DN+3yqCbkid3HxU5K99G7RcNEx1UxiL3ZQfwg1+TDhH96D+tCcXGQ==";
239 c_lock = "sha512:Hq6MQ2JMzGf56agcqYPEMnoWHbQMSAG0eE0ABHgktP8cKL6/A4bvydjUAa0h7sHUU8vdfWXK7eUYG/pnDxgitg==";
240 rv = checker(CL_SHA512, c_key, c_lock);
241 if (rv) failed = 1;
242 printf("\n----------------------------------------"
243 "----------------------------------------\n\n");
244
245 /* Test 7 (Check SHA256 with wrong key) */
246 printf("Test 7 (Check SHA256 with wrong key)\n\n");
247 c_key = "shA256:sSkDke97Dh78/d+Diu1i3dQ2Fp/EMK3xE2GfEqZlvK9=";
248 c_lock = "sHa256:RrKLp7YCQc9T8HmgSbxwIDlnCDWsgy1awqtiDuhedRo=";
249 rv = checker(CL_SHA256, c_key, c_lock);
250 /* BAD is expected and the correct result for this test */
251 printf("(Note: BAD is expected and the correct result for this test)\n");
252 if (!rv) failed = 1;
253 printf("\n----------------------------------------"
254 "----------------------------------------\n\n");
255
256 /* Test 8 (Check with unknown <scheme>) */
257 printf("Test 8 (Check with unknown <scheme>)\n\n");
258 c_key = "shA257:sSkDke97Dh78/d+Diu1i3dQ2Fp/EMK3xE2GfEqZlvK8=";
259 c_lock = "sHa256:RrKLp7YCQc9T8HmgSbxwIDlnCDWsgy1awqtiDuhedRo=";
260 rv = checker(CL_SHA256, c_key, c_lock);
261 /* BAD is expected and the correct result for this test */
262 printf("(Note: BAD is expected and the correct result for this test)\n");
263 if (!rv) failed = 1;
264 printf("\n----------------------------------------"
265 "----------------------------------------\n\n");
266
267 /* Test 9 (Check with <scheme> mismatch) */
268 printf("Test 9 (Check with <scheme> mismatch)\n\n");
269 c_key = "shA256:sSkDke97Dh78/d+Diu1i3dQ2Fp/EMK3xE2GfEqZlvK8=";
270 c_lock = "sha1:bNXHc6ohSmeHaRHHW56BIWZJt+4=";
271 rv = checker(CL_SHA256, c_key, c_lock);
272 /* BAD is expected and the correct result for this test */
273 printf("(Note: BAD is expected and the correct result for this test)\n");
274 if (!rv) failed = 1;
275 printf("\n----------------------------------------"
276 "----------------------------------------\n\n");
277
278 /* Check for success */
279 if (!failed) exit(EXIT_SUCCESS);
280 exit(EXIT_FAILURE);
281 }
282
283
284 /* EOF */