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