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