"Fossies" - the Fresh Open Source Software Archive 
Member "tin-2.4.2/libcanlock/test/canlocktest.c" (8 May 2017, 8949 Bytes) of package /linux/misc/tin-2.4.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.
1 /* =============================================================================
2 * Copyright (c) 2017 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 char *rawkey, *rawlock;
84 char keytype[BUFFSIZE], locktype[BUFFSIZE];
85 const char *scheme;
86
87 printf("%s\n%s,%s\n", "Check Cancel-Key,Cancel-Lock:", key, lock);
88
89 rawkey = lock_strip_alpha(key, keytype);
90 rawlock = lock_strip_alpha(lock, locktype);
91
92 /* Check whether <scheme> matches */
93 if (!strcmp(keytype, locktype))
94 {
95 /* Check whether <scheme> is supported */
96 scheme = check_scheme(hash);
97 if (NULL != scheme && !strcmp(keytype, scheme))
98 {
99 if (!cl_verify(hash, rawkey, rawlock))
100 {
101 printf("\nGOOD\n");
102 res = 0;
103 }
104 else
105 printf("\nBAD\n");
106 }
107 else
108 printf("\nBAD: Scheme not supported\n");
109 }
110 else
111 printf("\nBAD: Scheme mismatch (%s/%s)\n", keytype, locktype);
112
113 return res;
114 }
115
116
117 /* ========================================================================== */
118 /* Test program for new API from version 3 */
119
120 int main(void)
121 {
122 char *c_key = NULL, *c_lock = NULL;
123 const char sec[] = "ExampleSecret";
124 const char sec2[] = "AnotherSecret";
125 const char mid[] = "<12345@mid.example>";
126 const char uid[] = "JaneDoe";
127 char *uid_mid = NULL;
128 int rv;
129 int failed = 0;
130
131 /* First test (SHA256 without UID) */
132 printf("Test 1 (SHA256 without UID)\n\n");
133 printf("Secret : %s\n", sec);
134 printf("Message-ID: %s\n", mid);
135 c_key = cl_get_key(CL_SHA256, (const unsigned char *) sec, strlen(sec),
136 (const unsigned char *) mid, strlen(mid));
137 c_lock = cl_get_lock(CL_SHA256, (const unsigned char *) sec, strlen(sec),
138 (const unsigned char *) mid, strlen(mid));
139 printf("\n");
140 printf("%s\n%s,%s\n\n", "Expected Cancel-Key,Cancel-Lock:",
141 "sha256:qv1VXHYiCGjkX/N1nhfYKcAeUn8bCVhrWhoKuBSnpMA=",
142 "sha256:s/pmK/3grrz++29ce2/mQydzJuc7iqHn1nqcJiQTPMc=");
143 if (strcmp(c_key, "sha256:qv1VXHYiCGjkX/N1nhfYKcAeUn8bCVhrWhoKuBSnpMA="))
144 {
145 printf("BAD: Cancel-Key not as expected (%s)\n", c_key);
146 failed = 1;
147 }
148 else
149 {
150 if (strcmp(c_lock, "sha256:s/pmK/3grrz++29ce2/mQydzJuc7iqHn1nqcJiQTPMc="))
151 {
152 printf("BAD: Cancel-Lock not as expected (%s)\n", c_lock);
153 failed = 1;
154 }
155 else
156 {
157 rv = checker(CL_SHA256, c_key, c_lock);
158 if (rv) failed = 1;
159 }
160 }
161 free((void *) c_lock);
162 free((void *) c_key);
163 printf("\n----------------------------------------"
164 "----------------------------------------\n\n");
165
166 /* Second test (SHA256 with UID) */
167 printf("Test 2 (SHA256 with UID)\n\n");
168 printf("Secret : %s\n", sec2);
169 printf("User-ID : %s\n", uid);
170 printf("Message-ID: %s\n", mid);
171 uid_mid = (char *) malloc(strlen(uid) + strlen(mid) + (size_t) 1);
172 strcpy(uid_mid, uid);
173 strcat(uid_mid, mid);
174 c_key = cl_get_key(CL_SHA256, (const unsigned char *) sec2, strlen(sec2),
175 (const unsigned char *) uid_mid, strlen(uid_mid));
176 c_lock = cl_get_lock(CL_SHA256, (const unsigned char *) sec2, strlen(sec2),
177 (const unsigned char *) uid_mid, strlen(uid_mid));
178 free((void *) uid_mid);
179 printf("\n");
180 printf("%s\n%s,%s\n\n", "Expected Cancel-Key,Cancel-Lock:",
181 "sha256:yM0ep490Fzt83CLYYAytm3S2HasHhYG4LAeAlmuSEys=",
182 "sha256:NSBTz7BfcQFTCen+U4lQ0VS8VIlZao2b8mxD/xJaaeE=");
183
184 if (strcmp(c_key, "sha256:yM0ep490Fzt83CLYYAytm3S2HasHhYG4LAeAlmuSEys="))
185 {
186 printf("BAD: Cancel-Key not as expected (%s)\n", c_key);
187 failed = 1;
188 }
189 else
190 {
191 if (strcmp(c_lock, "sha256:NSBTz7BfcQFTCen+U4lQ0VS8VIlZao2b8mxD/xJaaeE="))
192 {
193 printf("BAD: Cancel-Lock not as expected (%s)\n", c_lock);
194 failed = 1;
195 }
196 else
197 {
198 rv = checker(CL_SHA256, c_key, c_lock);
199 if (rv) failed = 1;
200 }
201 }
202 free((void *) c_lock);
203 free((void *) c_key);
204 printf("\n----------------------------------------"
205 "----------------------------------------\n\n");
206
207 /* Test 3 (Check SHA256) */
208 printf("Test 3 (Check SHA256)\n\n");
209 c_key = "shA256:sSkDke97Dh78/d+Diu1i3dQ2Fp/EMK3xE2GfEqZlvK8=";
210 c_lock = "sHa256:RrKLp7YCQc9T8HmgSbxwIDlnCDWsgy1awqtiDuhedRo=";
211 rv = checker(CL_SHA256, c_key, c_lock);
212 if (rv) failed = 1;
213 printf("\n----------------------------------------"
214 "----------------------------------------\n\n");
215
216 /* Test 4 (Check SHA1) */
217 printf("Test 4 (Check SHA1)\n\n");
218 c_key = "ShA1:aaaBBBcccDDDeeeFFF";
219 c_lock = "sha1:bNXHc6ohSmeHaRHHW56BIWZJt+4=";
220 rv = checker(CL_SHA1, c_key, c_lock);
221 if (rv) failed = 1;
222 printf("\n----------------------------------------"
223 "----------------------------------------\n\n");
224
225 /* Test 5 (Check SHA1 with <clue-string> element) */
226 printf("Test 5 (Check SHA1 with <clue-string> element)\n\n");
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 printf("\n----------------------------------------"
232 "----------------------------------------\n\n");
233
234 /* Test 6 (Check SHA512) */
235 printf("Test 6 (Check SHA512)\n\n");
236 c_key = "sha512:ryoikFW3wKefmYr+zDzKn16ngNf1eYbZ0DN+3yqCbkid3HxU5K99G7RcNEx1UxiL3ZQfwg1+TDhH96D+tCcXGQ==";
237 c_lock = "sha512:Hq6MQ2JMzGf56agcqYPEMnoWHbQMSAG0eE0ABHgktP8cKL6/A4bvydjUAa0h7sHUU8vdfWXK7eUYG/pnDxgitg==";
238 rv = checker(CL_SHA512, c_key, c_lock);
239 if (rv) failed = 1;
240 printf("\n----------------------------------------"
241 "----------------------------------------\n\n");
242
243 /* Test 7 (Check SHA256 with wrong key) */
244 printf("Test 7 (Check SHA256 with wrong key)\n\n");
245 c_key = "shA256:sSkDke97Dh78/d+Diu1i3dQ2Fp/EMK3xE2GfEqZlvK9=";
246 c_lock = "sHa256:RrKLp7YCQc9T8HmgSbxwIDlnCDWsgy1awqtiDuhedRo=";
247 rv = checker(CL_SHA256, c_key, c_lock);
248 /* BAD is expected and the correct result for this test */
249 printf("(Note: BAD is expected and the correct result for this test)\n");
250 if (!rv) failed = 1;
251 printf("\n----------------------------------------"
252 "----------------------------------------\n\n");
253
254 /* Check for success */
255 if (!failed) exit(EXIT_SUCCESS);
256 exit(EXIT_FAILURE);
257 }
258
259
260 /* EOF */