lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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 <stdlib.h> |
| 11 | #include "crypt.h" |
| 12 | |
| 13 | int totfails = 0; |
| 14 | |
| 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 | |
| 20 | void good_bye () |
| 21 | { |
| 22 | if(totfails == 0) { |
| 23 | printf("Passed DES validation suite\n"); |
| 24 | exit(0); |
| 25 | } else { |
| 26 | printf("%d failures during DES validation suite!!!\n", totfails); |
| 27 | exit(1); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | int |
| 32 | main(argc, argv) |
| 33 | int argc; |
| 34 | char *argv[]; |
| 35 | { |
| 36 | char key[64],plain[64],cipher[64],answer[64]; |
| 37 | int i; |
| 38 | int test; |
| 39 | int fail; |
| 40 | |
| 41 | for(test=0;!feof(stdin);test++){ |
| 42 | |
| 43 | get8(key); |
| 44 | printf(" K: "); put8(key); |
| 45 | setkey(key); |
| 46 | |
| 47 | get8(plain); |
| 48 | printf(" P: "); put8(plain); |
| 49 | |
| 50 | get8(answer); |
| 51 | printf(" C: "); put8(answer); |
| 52 | |
| 53 | for(i=0;i<64;i++) |
| 54 | cipher[i] = plain[i]; |
| 55 | encrypt(cipher, 0); |
| 56 | |
| 57 | for(i=0;i<64;i++) |
| 58 | if(cipher[i] != answer[i]) |
| 59 | break; |
| 60 | fail = 0; |
| 61 | if(i != 64){ |
| 62 | printf(" Encrypt FAIL"); |
| 63 | fail++; totfails++; |
| 64 | } |
| 65 | |
| 66 | encrypt(cipher, 1); |
| 67 | |
| 68 | for(i=0;i<64;i++) |
| 69 | if(cipher[i] != plain[i]) |
| 70 | break; |
| 71 | if(i != 64){ |
| 72 | printf(" Decrypt FAIL"); |
| 73 | fail++; totfails++; |
| 74 | } |
| 75 | |
| 76 | if(fail == 0) |
| 77 | printf(" OK"); |
| 78 | printf("\n"); |
| 79 | } |
| 80 | good_bye(); |
| 81 | } |
| 82 | void |
| 83 | get8(cp) |
| 84 | char *cp; |
| 85 | { |
| 86 | int i,j,t; |
| 87 | |
| 88 | for(i=0;i<8;i++){ |
| 89 | scanf("%2x",&t); |
| 90 | if(feof(stdin)) |
| 91 | good_bye(); |
| 92 | for(j=0; j<8 ; j++) { |
| 93 | *cp++ = (t & (0x01 << (7-j))) != 0; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | void |
| 98 | put8(cp) |
| 99 | char *cp; |
| 100 | { |
| 101 | int i,j,t; |
| 102 | |
| 103 | for(i=0;i<8;i++){ |
| 104 | t = 0; |
| 105 | for(j = 0; j<8; j++) |
| 106 | t = (t<<1) | *cp++; |
| 107 | printf("%02x", t); |
| 108 | } |
| 109 | } |