crypt.c 1.7 KB

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