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.
6 * $FreeBSD: src/secure/lib/libcipher/test/cert.c,v 1.5 1999/08/28 01:30:22 peter Exp $
16 #endif /* HAVE_CRYPT16 */
19 static struct crypt_test {
20 char *key, *setting, *answer;
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",
36 static struct crypt_test crypt16_tests[] = {
37 "foob", "ar", "arxo23jZDD5AYbHbqoy9Dalg",
38 "holyhooplasbatman!", "ar", "arU5FRLJ3kxIoedlmyrOelEw",
46 printf(" Passed validation\n");
49 printf(" %d failures during validation!!!\n", totfails);
60 for(i = 0; i < 8; i++){
62 for(j = 0; j < 8; j++)
74 for (i = 0; i < 8; i++) {
75 printf("%02x", bits[i]);
80 int parse_line(buff, salt, key, plain, answer)
83 char *key, *plain, *answer;
92 if (sscanf(buff, "%lu", salt) != 1)
94 for (ptr2 = buff; *ptr2 && !isspace(*ptr2); ptr2++)
100 for (ptr1 = ptr2; *ptr1 && isspace(*ptr1); ptr1++)
102 for (ptr2 = ptr1; *ptr2 && !isspace(*ptr2); ptr2++)
104 if (ptr2 - ptr1 != 16)
106 for (i = 0; i < 8; i++){
107 if (sscanf(ptr1 + 2*i, "%2x", &t) != 1)
109 for (j = 0; j < 8; j++)
110 *key++ = (t & 1 << (7 - j)) != 0;
116 for (ptr1 = ptr2; *ptr1 && isspace(*ptr1); ptr1++)
118 for (ptr2 = ptr1; *ptr2 && !isspace(*ptr2); ptr2++)
120 if (ptr2 - ptr1 != 16)
122 for (i = 0; i < 8; i++){
123 if (sscanf(ptr1 + 2*i, "%2x", &t) != 1)
125 for (j = 0; j < 8; j++)
126 *plain++ = (t & 1 << (7 - j)) != 0;
132 for (ptr1 = ptr2; *ptr1 && isspace(*ptr1); ptr1++)
134 for (ptr2 = ptr1; *ptr2 && !isspace(*ptr2); ptr2++)
136 if (ptr2 - ptr1 != 16)
138 for (i = 0; i < 8; i++){
139 if (sscanf(ptr1 + 2*i, "%2x", &t) != 1)
141 for (j = 0; j < 8; j++)
142 *answer++ = (t & 1 << (7 - j)) != 0;
148 * Test the setkey and encrypt functions
152 char key[64],plain[64],cipher[64],answer[64];
159 printf("Testing setkey/encrypt\n");
161 for(test=0;fgets(buff, BUFSIZ, stdin);test++){
169 if ((fail = parse_line(buff, &salt, key, plain, answer)) < 0){
170 printf("test %d garbled (%d)\n", test, fail);
175 continue; /* encrypt has no salt support */
177 printf(" K: "); put8(key);
178 printf(" P: "); put8(plain);
179 printf(" C: "); put8(answer);
182 for(i = 0; i < 64; i++)
183 cipher[i] = plain[i];
187 if(cipher[i] != answer[i])
191 printf(" Enc FAIL ");
199 if(cipher[i] != plain[i])
213 void bytes_to_bits(bytes, bits)
219 for (i = 0; i < 8; i++) {
221 for (j = 0; j < 8; j++) {
222 bits[i] |= (bytes[i*8+j] & 1) << (7 - j);
229 * Test the des_setkey and des_cipher functions
233 char ckey[64], cplain[64], canswer[64];
234 unsigned char key[8], plain[8], cipher[8], answer[8];
241 printf("Testing des_setkey/des_cipher\n");
243 for(test=0;fgets(buff, BUFSIZ, stdin);test++){
251 if ((fail = parse_line(buff, &salt, ckey, cplain, canswer)) <0){
252 printf("test %d garbled (%d)\n", test, fail);
256 printf(" S: %06x", salt);
257 printf(" K: "); put8(ckey);
258 printf(" P: "); put8(cplain);
259 printf(" C: "); put8(canswer);
261 bytes_to_bits(ckey, key);
262 bytes_to_bits(cplain, plain);
263 bytes_to_bits(canswer, answer);
265 des_cipher(plain, cipher, salt, 1);
267 for(i = 0; i < 8; i++)
268 if(cipher[i] != answer[i])
272 printf(" Enc FAIL ");
277 des_cipher(cipher, cipher, salt, -1);
279 for(i = 0; i < 8; i++)
280 if(cipher[i] != plain[i])
295 * Test the old-style crypt(), the new-style crypt(), and crypt16().
300 struct crypt_test *p;
302 printf("Testing crypt() family\n");
304 for (p = crypt_tests; p->key; p++) {
305 printf(" crypt(\"%s\", \"%s\"), \"%s\" expected",
306 p->key, p->setting, p->answer);
308 result = crypt(p->key, p->setting);
309 if(!strcmp(result, p->answer)) {
312 printf("\n failed (\"%s\")\n", result);
318 for (p = crypt16_tests; p->key; p++) {
319 printf(" crypt16(\"%s\", \"%s\"), \"%s\" expected",
320 p->key, p->setting, p->answer);
322 result = crypt16(p->key, p->setting);
323 if(!strcmp(result, p->answer)) {
326 printf("\n failed (\"%s\")\n", result);
330 #endif /* HAVE_CRYPT16 */
337 if(argc < 1 || !strcmp(argv[1], "-e"))
339 else if(!strcmp(argv[1], "-d"))
341 else if(!strcmp(argv[1], "-c"))