Initial import from FreeBSD RELENG_4:
[dragonfly.git] / secure / lib / libcipher / test / cert.c
1 /*
2  * This DES validation program shipped with FreeSec is derived from that
3  * shipped with UFC-crypt which is apparently derived from one distributed
4  * with Phil Karns PD DES package.
5  *
6  * $FreeBSD: src/secure/lib/libcipher/test/cert.c,v 1.5 1999/08/28 01:30:22 peter Exp $
7  */
8
9 #include <stdio.h>
10
11 int totfails = 0;
12
13 char *crypt();
14 #ifdef HAVE_CRYPT16
15 char *crypt16();
16 #endif /* HAVE_CRYPT16 */
17
18
19 static struct crypt_test {
20         char    *key, *setting, *answer;
21 } crypt_tests[] = {
22         "foob",                 "ar",           "arlEKn0OzVJn.",
23         "holyhooplasbatman!",   "_X.......",    "_X.......N89y2Z.e4WU",
24         "holyhooplasbatman!",   "_X...X...",    "_X...X...rSUDQ5Na/QM",
25         "holyhooplasbatman!",   "_XX..X...",    "_XX..X...P8vb9xU4JAk",
26         "holyhooplasbatman!",   "_XX..XX..",    "_XX..XX..JDs5IlGLqT2",
27         "holyhooplasbatman!",   "_XX..XXa.",    "_XX..XXa.bFVsOnCNh8Y",
28         "holyhooplasbatman!",   "_XXa.X...",    "_XXa.X...Ghsb3QKNaps",
29 #ifdef TAKES_TOO_LONG_ON_SOME_CRYPTS
30         "holyhooplasbatman!",   "_arararar",    "_ararararNGMzvpNjeCc",
31 #endif
32         NULL, NULL, NULL,
33 };
34
35
36 static struct crypt_test crypt16_tests[] = {
37         "foob",                 "ar",           "arxo23jZDD5AYbHbqoy9Dalg",
38         "holyhooplasbatman!",   "ar",           "arU5FRLJ3kxIoedlmyrOelEw",
39         NULL, NULL, NULL
40 };
41
42
43 void good_bye()
44 {
45   if(totfails == 0) {
46     printf(" Passed validation\n");
47     exit(0);
48   } else {
49     printf(" %d failures during validation!!!\n", totfails);
50     exit(1);
51   }
52 }
53
54
55 void put8(cp)
56 char *cp;
57 {
58         int i,j,t;
59
60         for(i = 0; i < 8; i++){
61                 t = 0;
62                 for(j = 0; j < 8; j++)
63                         t = t << 1 | *cp++;
64                 printf("%02x", t);
65         }
66 }
67
68
69 void print_bits(bits)
70 unsigned char *bits;
71 {
72         int     i;
73
74         for (i = 0; i < 8; i++) {
75                 printf("%02x", bits[i]);
76         }
77 }
78
79
80 int parse_line(buff, salt, key, plain, answer)
81 char *buff;
82 long *salt;
83 char *key, *plain, *answer;
84 {
85         char *ptr1, *ptr2;
86         int val;
87         int i,j,t;
88
89         /*
90          * Extract salt
91          */
92         if (sscanf(buff, "%lu", salt) != 1)
93                 return(-1);
94         for (ptr2 = buff; *ptr2 && !isspace(*ptr2); ptr2++)
95                 ;
96
97         /*
98          * Extract key
99          */
100         for (ptr1 = ptr2; *ptr1 && isspace(*ptr1); ptr1++)
101                 ;
102         for (ptr2 = ptr1; *ptr2 && !isspace(*ptr2); ptr2++)
103                 ;
104         if (ptr2 - ptr1 != 16)
105                 return(-1);
106         for (i = 0; i < 8; i++){
107                 if (sscanf(ptr1 + 2*i, "%2x", &t) != 1)
108                         return(-2);
109                 for (j = 0; j < 8; j++)
110                         *key++ = (t & 1 << (7 - j)) != 0;
111         }
112
113         /*
114          * Extract plain
115          */
116         for (ptr1 = ptr2; *ptr1 && isspace(*ptr1); ptr1++)
117                 ;
118         for (ptr2 = ptr1; *ptr2 && !isspace(*ptr2); ptr2++)
119                 ;
120         if (ptr2 - ptr1 != 16)
121                 return(-1);
122         for (i = 0; i < 8; i++){
123                 if (sscanf(ptr1 + 2*i, "%2x", &t) != 1)
124                         return(-2);
125                 for (j = 0; j < 8; j++)
126                         *plain++ = (t & 1 << (7 - j)) != 0;
127         }
128
129         /*
130          * Extract answer
131          */
132         for (ptr1 = ptr2; *ptr1 && isspace(*ptr1); ptr1++)
133                 ;
134         for (ptr2 = ptr1; *ptr2 && !isspace(*ptr2); ptr2++)
135                 ;
136         if (ptr2 - ptr1 != 16)
137                 return(-1);
138         for (i = 0; i < 8; i++){
139                 if (sscanf(ptr1 + 2*i, "%2x", &t) != 1)
140                         return(-2);
141                 for (j = 0; j < 8; j++)
142                         *answer++ = (t & 1 << (7 - j)) != 0;
143         }
144         return(0);
145 }
146
147 /*
148  * Test the setkey and encrypt functions
149  */
150 void test_encrypt()
151 {
152         char key[64],plain[64],cipher[64],answer[64];
153         char buff[BUFSIZ];
154         unsigned long salt;
155         int i;
156         int test;
157         int fail;
158
159         printf("Testing setkey/encrypt\n");
160
161         for(test=0;fgets(buff, BUFSIZ, stdin);test++){
162
163                 /*
164                  * Allow comments.
165                  */
166                 if (*buff == '#')
167                         continue;
168
169                 if ((fail = parse_line(buff, &salt, key, plain, answer)) < 0){
170                         printf("test %d garbled (%d)\n", test, fail);
171                         continue;
172                 }
173
174                 if (salt)
175                         continue;       /* encrypt has no salt support */
176
177                 printf(" K: "); put8(key);
178                 printf(" P: "); put8(plain);
179                 printf(" C: "); put8(answer);
180
181                 setkey(key);
182                 for(i = 0; i < 64; i++)
183                         cipher[i] = plain[i];
184                 encrypt(cipher, 0);
185
186                 for(i=0;i<64;i++)
187                         if(cipher[i] != answer[i])
188                                 break;
189                 fail = 0;
190                 if(i != 64){
191                         printf(" Enc FAIL ");
192                         put8(cipher);
193                         fail++; totfails++;
194                 }
195
196                 encrypt(cipher, 1);
197
198                 for(i=0;i<64;i++)
199                         if(cipher[i] != plain[i])
200                                 break;
201                 if(i != 64){
202                         printf(" Dec FAIL");
203                         fail++; totfails++;
204                 }
205
206                 if(fail == 0)
207                         printf(" OK");
208                 printf("\n");
209         }
210 }
211
212
213 void bytes_to_bits(bytes, bits)
214 char *bytes;
215 unsigned char *bits;
216 {
217         int     i, j;
218
219         for (i = 0; i < 8; i++) {
220                 bits[i] = 0;
221                 for (j = 0; j < 8; j++) {
222                         bits[i] |= (bytes[i*8+j] & 1) << (7 - j);
223                 }
224         }
225 }
226
227
228 /*
229  * Test the des_setkey and des_cipher functions
230  */
231 void test_des()
232 {
233         char ckey[64], cplain[64], canswer[64];
234         unsigned char key[8], plain[8], cipher[8], answer[8];
235         char buff[BUFSIZ];
236         unsigned long salt;
237         int i;
238         int test;
239         int fail;
240
241         printf("Testing des_setkey/des_cipher\n");
242
243         for(test=0;fgets(buff, BUFSIZ, stdin);test++){
244
245                 /*
246                  * Allow comments.
247                  */
248                 if (*buff == '#')
249                         continue;
250
251                 if ((fail = parse_line(buff, &salt, ckey, cplain, canswer)) <0){
252                         printf("test %d garbled (%d)\n", test, fail);
253                         continue;
254                 }
255
256                 printf(" S: %06x", salt);
257                 printf(" K: "); put8(ckey);
258                 printf(" P: "); put8(cplain);
259                 printf(" C: "); put8(canswer);
260
261                 bytes_to_bits(ckey, key);
262                 bytes_to_bits(cplain, plain);
263                 bytes_to_bits(canswer, answer);
264                 des_setkey(key);
265                 des_cipher(plain, cipher, salt, 1);
266
267                 for(i = 0; i < 8; i++)
268                         if(cipher[i] != answer[i])
269                                 break;
270                 fail = 0;
271                 if(i != 8){
272                         printf(" Enc FAIL ");
273                         print_bits(cipher);
274                         fail++; totfails++;
275                 }
276
277                 des_cipher(cipher, cipher, salt, -1);
278
279                 for(i = 0; i < 8; i++)
280                         if(cipher[i] != plain[i])
281                                 break;
282                 if(i != 8){
283                         printf(" Dec FAIL");
284                         fail++; totfails++;
285                 }
286
287                 if(fail == 0)
288                         printf(" OK");
289                 printf("\n");
290         }
291 }
292
293
294 /*
295  *      Test the old-style crypt(), the new-style crypt(), and crypt16().
296  */
297 void test_crypt()
298 {
299         char    *result;
300         struct crypt_test       *p;
301
302         printf("Testing crypt() family\n");
303
304         for (p = crypt_tests; p->key; p++) {
305                 printf(" crypt(\"%s\", \"%s\"), \"%s\" expected",
306                         p->key, p->setting, p->answer);
307                 fflush(stdout);
308                 result = crypt(p->key, p->setting);
309                 if(!strcmp(result, p->answer)) {
310                         printf(", OK\n");
311                 } else {
312                         printf("\n  failed (\"%s\")\n", result);
313                         totfails++;
314                 }
315         }
316
317 #ifdef HAVE_CRYPT16
318         for (p = crypt16_tests; p->key; p++) {
319                 printf(" crypt16(\"%s\", \"%s\"), \"%s\" expected",
320                         p->key, p->setting, p->answer);
321                 fflush(stdout);
322                 result = crypt16(p->key, p->setting);
323                 if(!strcmp(result, p->answer)) {
324                         printf(", OK\n");
325                 } else {
326                         printf("\n  failed (\"%s\")\n", result);
327                         totfails++;
328                 }
329         }
330 #endif /* HAVE_CRYPT16 */
331 }
332
333 main(argc, argv)
334 int argc;
335 char *argv[];
336 {
337         if(argc < 1 || !strcmp(argv[1], "-e"))
338                 test_encrypt();
339         else if(!strcmp(argv[1], "-d"))
340                 test_des();
341         else if(!strcmp(argv[1], "-c"))
342                 test_crypt();
343         good_bye();
344 }