Back to index

glibc  2.9
cert.c
Go to the documentation of this file.
00001 
00002 /*
00003  * This crypt(3) validation program shipped with UFC-crypt
00004  * is derived from one distributed with Phil Karns PD DES package.
00005  *
00006  * @(#)cert.c 1.8 11 Aug 1996
00007  */
00008 
00009 #include <stdio.h>
00010 #include <stdlib.h>
00011 #include "crypt.h"
00012 
00013 int totfails = 0;
00014 
00015 #if __STDC__ - 0
00016 int main (int argc, char *argv[]);
00017 void get8 (char *cp);
00018 void put8 (char *cp);
00019 void good_bye (void) __attribute__ ((noreturn));
00020 #else
00021 void get8(), put8();
00022 #endif
00023 
00024 void good_bye ()
00025 {
00026   if(totfails == 0) {
00027     printf("Passed DES validation suite\n");
00028     exit(0);
00029   } else {
00030     printf("%d failures during DES validation suite!!!\n", totfails);
00031     exit(1);
00032   }
00033 }
00034 
00035 int
00036 main(argc, argv)
00037      int argc;
00038      char *argv[];
00039 {
00040        char key[64],plain[64],cipher[64],answer[64];
00041        int i;
00042        int test;
00043        int fail;
00044 
00045        for(test=0;!feof(stdin);test++){
00046 
00047               get8(key);
00048               printf(" K: "); put8(key);
00049               setkey(key);
00050 
00051               get8(plain);
00052               printf(" P: "); put8(plain);
00053 
00054               get8(answer);
00055               printf(" C: "); put8(answer);
00056 
00057               for(i=0;i<64;i++)
00058                      cipher[i] = plain[i];
00059               encrypt(cipher, 0);
00060 
00061               for(i=0;i<64;i++)
00062                      if(cipher[i] != answer[i])
00063                             break;
00064               fail = 0;
00065               if(i != 64){
00066                      printf(" Encrypt FAIL");
00067                      fail++; totfails++;
00068               }
00069 
00070               encrypt(cipher, 1);
00071 
00072               for(i=0;i<64;i++)
00073                      if(cipher[i] != plain[i])
00074                             break;
00075               if(i != 64){
00076                      printf(" Decrypt FAIL");
00077                      fail++; totfails++;
00078               }
00079 
00080               if(fail == 0)
00081                      printf(" OK");
00082               printf("\n");
00083        }
00084        good_bye();
00085 }
00086 void
00087 get8(cp)
00088 char *cp;
00089 {
00090        int i,j,t;
00091 
00092        for(i=0;i<8;i++){
00093               scanf("%2x",&t);
00094               if(feof(stdin))
00095                 good_bye();
00096               for(j=0; j<8 ; j++) {
00097                 *cp++ = (t & (0x01 << (7-j))) != 0;
00098               }
00099        }
00100 }
00101 void
00102 put8(cp)
00103 char *cp;
00104 {
00105        int i,j,t;
00106 
00107        for(i=0;i<8;i++){
00108          t = 0;
00109          for(j = 0; j<8; j++)
00110            t = (t<<1) | *cp++;
00111          printf("%02x", t);
00112        }
00113 }