Initial checkin of glibc-crypt-2.1.tar.gz to the CVS archive.
[kopensolaris-gnu/glibc.git] / crypt / cert.c
1
2 /*
3  * This crypt(3) validation program shipped with UFC-crypt
4  * is derived from one distributed with Phil Karns PD DES package.
5  *
6  * @(#)cert.c   1.8 11 Aug 1996
7  */
8
9 #include <stdio.h>
10 #include "crypt.h"
11
12 int totfails = 0;
13
14 #if __STDC__ - 0
15 int main (int argc, char *argv[]);
16 void get8 (char *cp);
17 void put8 (char *cp);
18 void good_bye (void) __attribute__ ((noreturn));
19 #else
20 void get8(), put8();
21 #endif
22
23 void good_bye ()
24 {
25   if(totfails == 0) {
26     printf("Passed DES validation suite\n");
27     exit(0);
28   } else {
29     printf("%d failures during DES validation suite!!!\n", totfails);
30     exit(1);
31   }
32 }
33
34 int
35 main(argc, argv)
36      int argc;
37      char *argv[];
38 {
39         char key[64],plain[64],cipher[64],answer[64];
40         int i;
41         int test;
42         int fail;
43
44         for(test=0;!feof(stdin);test++){
45
46                 get8(key);
47                 printf(" K: "); put8(key);
48                 setkey(key);
49
50                 get8(plain);
51                 printf(" P: "); put8(plain);
52
53                 get8(answer);
54                 printf(" C: "); put8(answer);
55
56                 for(i=0;i<64;i++)
57                         cipher[i] = plain[i];
58                 encrypt(cipher, 0);
59
60                 for(i=0;i<64;i++)
61                         if(cipher[i] != answer[i])
62                                 break;
63                 fail = 0;
64                 if(i != 64){
65                         printf(" Encrypt FAIL");
66                         fail++; totfails++;
67                 }
68
69                 encrypt(cipher, 1);
70
71                 for(i=0;i<64;i++)
72                         if(cipher[i] != plain[i])
73                                 break;
74                 if(i != 64){
75                         printf(" Decrypt FAIL");
76                         fail++; totfails++;
77                 }
78
79                 if(fail == 0)
80                         printf(" OK");
81                 printf("\n");
82         }
83         good_bye();
84 }
85 void
86 get8(cp)
87 char *cp;
88 {
89         int i,j,t;
90
91         for(i=0;i<8;i++){
92                 scanf("%2x",&t);
93                 if(feof(stdin))
94                   good_bye();
95                 for(j=0; j<8 ; j++) {
96                   *cp++ = (t & (0x01 << (7-j))) != 0;
97                 }
98         }
99 }
100 void
101 put8(cp)
102 char *cp;
103 {
104         int i,j,t;
105
106         for(i=0;i<8;i++){
107           t = 0;
108           for(j = 0; j<8; j++)
109             t = (t<<1) | *cp++;
110           printf("%02x", t);
111         }
112 }