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 /* This file tests the deprecated setkey/encrypt interface. */
14 #include <shlib-compat.h>
15 #if TEST_COMPAT (libcrypt, GLIBC_2_0, GLIBC_2_28)
16
17 #define libcrypt_version_reference(symbol, version) \
18 _libcrypt_version_reference (symbol, VERSION_libcrypt_##version)
19 #define _libcrypt_version_reference(symbol, version) \
20 __libcrypt_version_reference (symbol, version)
21 #define __libcrypt_version_reference(symbol, version) \
22 __asm__ (".symver " #symbol ", " #symbol "@" #version)
23
24 extern void setkey (const char *);
25 extern void encrypt (const char *, int);
26 libcrypt_version_reference (setkey, GLIBC_2_0);
27 libcrypt_version_reference (encrypt, GLIBC_2_0);
28
29 int totfails = 0;
30
31 int main (int argc, char *argv[]);
32 void get8 (char *cp);
33 void put8 (char *cp);
34 void good_bye (void) __attribute__ ((noreturn));
35
36 void
good_bye(void)37 good_bye (void)
38 {
39 if(totfails == 0) {
40 printf("Passed DES validation suite\n");
41 exit(0);
42 } else {
43 printf("%d failures during DES validation suite!!!\n", totfails);
44 exit(1);
45 }
46 }
47
48 int
main(int argc,char * argv[])49 main (int argc, char *argv[])
50 {
51 char key[64],plain[64],cipher[64],answer[64];
52 int i;
53 int fail;
54
55 for(;!feof(stdin);){
56
57 get8(key);
58 printf(" K: "); put8(key);
59 setkey(key);
60
61 get8(plain);
62 printf(" P: "); put8(plain);
63
64 get8(answer);
65 printf(" C: "); put8(answer);
66
67 for(i=0;i<64;i++)
68 cipher[i] = plain[i];
69 encrypt(cipher, 0);
70
71 for(i=0;i<64;i++)
72 if(cipher[i] != answer[i])
73 break;
74 fail = 0;
75 if(i != 64){
76 printf(" Encrypt FAIL");
77 fail++; totfails++;
78 }
79
80 encrypt(cipher, 1);
81
82 for(i=0;i<64;i++)
83 if(cipher[i] != plain[i])
84 break;
85 if(i != 64){
86 printf(" Decrypt FAIL");
87 fail++; totfails++;
88 }
89
90 if(fail == 0)
91 printf(" OK");
92 printf("\n");
93 }
94 good_bye();
95 }
96 void
get8(char * cp)97 get8 (char *cp)
98 {
99 int i,j,t;
100
101 for(i=0;i<8;i++){
102 scanf("%2x",&t);
103 if(feof(stdin))
104 good_bye();
105 for(j=0; j<8 ; j++) {
106 *cp++ = (t & (0x01 << (7-j))) != 0;
107 }
108 }
109 }
110 void
put8(char * cp)111 put8 (char *cp)
112 {
113 int i,j,t;
114
115 for(i=0;i<8;i++){
116 t = 0;
117 for(j = 0; j<8; j++)
118 t = (t<<1) | *cp++;
119 printf("%02x", t);
120 }
121 }
122
123 #else /* encrypt and setkey are not available. */
124
125 int
main(void)126 main (void)
127 {
128 return 77; /* UNSUPPORTED */
129 }
130
131 #endif
132