crypt.c 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. static int totfails = 0;
  11. static void good_bye (void) __attribute__ ((noreturn));
  12. static void good_bye (void)
  13. {
  14. if(totfails == 0) {
  15. printf("Passed DES validation suite\n");
  16. exit(0);
  17. } else {
  18. printf("%d failures during DES validation suite!!!\n", totfails);
  19. exit(1);
  20. }
  21. }
  22. static void get8(char *cp)
  23. {
  24. int i,j,t;
  25. for(i=0;i<8;i++){
  26. scanf("%2x",&t);
  27. if(feof(stdin))
  28. good_bye();
  29. for(j=0; j<8 ; j++) {
  30. *cp++ = (t & (0x01 << (7-j))) != 0;
  31. }
  32. }
  33. }
  34. static void put8(char *cp)
  35. {
  36. int i,j,t;
  37. for(i=0;i<8;i++){
  38. t = 0;
  39. for(j = 0; j<8; j++)
  40. t = (t<<1) | *cp++;
  41. printf("%02x", t);
  42. }
  43. }
  44. int main(void)
  45. {
  46. char key[64],plain[64],cipher[64],answer[64];
  47. int i;
  48. int test;
  49. int fail;
  50. for(test=0;!feof(stdin);test++){
  51. get8(key);
  52. printf(" K: "); put8(key);
  53. setkey(key);
  54. get8(plain);
  55. printf(" P: "); put8(plain);
  56. get8(answer);
  57. printf(" C: "); put8(answer);
  58. for(i=0;i<64;i++)
  59. cipher[i] = plain[i];
  60. encrypt(cipher, 0);
  61. for(i=0;i<64;i++) {
  62. if(cipher[i] != answer[i])
  63. break;
  64. }
  65. fail = 0;
  66. if(i != 64){
  67. printf(" Encrypt FAIL");
  68. fail++; totfails++;
  69. }
  70. encrypt(cipher, 1);
  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. if(fail == 0)
  79. printf(" OK");
  80. printf("\n");
  81. }
  82. good_bye();
  83. }