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 $
7 * $DragonFly: src/secure/lib/libcipher/test/cert.c,v 1.2 2003/06/17 04:27:48 dillon Exp $
17 #endif /* HAVE_CRYPT16 */
20 static struct crypt_test {
21 char *key, *setting, *answer;
23 "foob", "ar", "arlEKn0OzVJn.",
24 "holyhooplasbatman!", "_X.......", "_X.......N89y2Z.e4WU",
25 "holyhooplasbatman!", "_X...X...", "_X...X...rSUDQ5Na/QM",
26 "holyhooplasbatman!", "_XX..X...", "_XX..X...P8vb9xU4JAk",
27 "holyhooplasbatman!", "_XX..XX..", "_XX..XX..JDs5IlGLqT2",
28 "holyhooplasbatman!", "_XX..XXa.", "_XX..XXa.bFVsOnCNh8Y",
29 "holyhooplasbatman!", "_XXa.X...", "_XXa.X...Ghsb3QKNaps",
30 #ifdef TAKES_TOO_LONG_ON_SOME_CRYPTS
31 "holyhooplasbatman!", "_arararar", "_ararararNGMzvpNjeCc",
37 static struct crypt_test crypt16_tests[] = {
38 "foob", "ar", "arxo23jZDD5AYbHbqoy9Dalg",
39 "holyhooplasbatman!", "ar", "arU5FRLJ3kxIoedlmyrOelEw",
47 printf(" Passed validation\n");
50 printf(" %d failures during validation!!!\n", totfails);
61 for(i = 0; i < 8; i++){
63 for(j = 0; j < 8; j++)
75 for (i = 0; i < 8; i++) {
76 printf("%02x", bits[i]);
81 int parse_line(buff, salt, key, plain, answer)
84 char *key, *plain, *answer;
93 if (sscanf(buff, "%lu", salt) != 1)
95 for (ptr2 = buff; *ptr2 && !isspace(*ptr2); ptr2++)
101 for (ptr1 = ptr2; *ptr1 && isspace(*ptr1); ptr1++)
103 for (ptr2 = ptr1; *ptr2 && !isspace(*ptr2); ptr2++)
105 if (ptr2 - ptr1 != 16)
107 for (i = 0; i < 8; i++){
108 if (sscanf(ptr1 + 2*i, "%2x", &t) != 1)
110 for (j = 0; j < 8; j++)
111 *key++ = (t & 1 << (7 - j)) != 0;
117 for (ptr1 = ptr2; *ptr1 && isspace(*ptr1); ptr1++)
119 for (ptr2 = ptr1; *ptr2 && !isspace(*ptr2); ptr2++)
121 if (ptr2 - ptr1 != 16)
123 for (i = 0; i < 8; i++){
124 if (sscanf(ptr1 + 2*i, "%2x", &t) != 1)
126 for (j = 0; j < 8; j++)
127 *plain++ = (t & 1 << (7 - j)) != 0;
133 for (ptr1 = ptr2; *ptr1 && isspace(*ptr1); ptr1++)
135 for (ptr2 = ptr1; *ptr2 && !isspace(*ptr2); ptr2++)
137 if (ptr2 - ptr1 != 16)
139 for (i = 0; i < 8; i++){
140 if (sscanf(ptr1 + 2*i, "%2x", &t) != 1)
142 for (j = 0; j < 8; j++)
143 *answer++ = (t & 1 << (7 - j)) != 0;
149 * Test the setkey and encrypt functions
153 char key[64],plain[64],cipher[64],answer[64];
160 printf("Testing setkey/encrypt\n");
162 for(test=0;fgets(buff, BUFSIZ, stdin);test++){
170 if ((fail = parse_line(buff, &salt, key, plain, answer)) < 0){
171 printf("test %d garbled (%d)\n", test, fail);
176 continue; /* encrypt has no salt support */
178 printf(" K: "); put8(key);
179 printf(" P: "); put8(plain);
180 printf(" C: "); put8(answer);
183 for(i = 0; i < 64; i++)
184 cipher[i] = plain[i];
188 if(cipher[i] != answer[i])
192 printf(" Enc FAIL ");
200 if(cipher[i] != plain[i])
214 void bytes_to_bits(bytes, bits)
220 for (i = 0; i < 8; i++) {
222 for (j = 0; j < 8; j++) {
223 bits[i] |= (bytes[i*8+j] & 1) << (7 - j);
230 * Test the des_setkey and des_cipher functions
234 char ckey[64], cplain[64], canswer[64];
235 unsigned char key[8], plain[8], cipher[8], answer[8];
242 printf("Testing des_setkey/des_cipher\n");
244 for(test=0;fgets(buff, BUFSIZ, stdin);test++){
252 if ((fail = parse_line(buff, &salt, ckey, cplain, canswer)) <0){
253 printf("test %d garbled (%d)\n", test, fail);
257 printf(" S: %06x", salt);
258 printf(" K: "); put8(ckey);
259 printf(" P: "); put8(cplain);
260 printf(" C: "); put8(canswer);
262 bytes_to_bits(ckey, key);
263 bytes_to_bits(cplain, plain);
264 bytes_to_bits(canswer, answer);
266 des_cipher(plain, cipher, salt, 1);
268 for(i = 0; i < 8; i++)
269 if(cipher[i] != answer[i])
273 printf(" Enc FAIL ");
278 des_cipher(cipher, cipher, salt, -1);
280 for(i = 0; i < 8; i++)
281 if(cipher[i] != plain[i])
296 * Test the old-style crypt(), the new-style crypt(), and crypt16().
301 struct crypt_test *p;
303 printf("Testing crypt() family\n");
305 for (p = crypt_tests; p->key; p++) {
306 printf(" crypt(\"%s\", \"%s\"), \"%s\" expected",
307 p->key, p->setting, p->answer);
309 result = crypt(p->key, p->setting);
310 if(!strcmp(result, p->answer)) {
313 printf("\n failed (\"%s\")\n", result);
319 for (p = crypt16_tests; p->key; p++) {
320 printf(" crypt16(\"%s\", \"%s\"), \"%s\" expected",
321 p->key, p->setting, p->answer);
323 result = crypt16(p->key, p->setting);
324 if(!strcmp(result, p->answer)) {
327 printf("\n failed (\"%s\")\n", result);
331 #endif /* HAVE_CRYPT16 */
338 if(argc < 1 || !strcmp(argv[1], "-e"))
340 else if(!strcmp(argv[1], "-d"))
342 else if(!strcmp(argv[1], "-c"))