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 |
37 | good_bye (void) |
38 | { |
39 | if(totfails == 0) { |
40 | printf(format: "Passed DES validation suite\n" ); |
41 | exit(status: 0); |
42 | } else { |
43 | printf(format: "%d failures during DES validation suite!!!\n" , totfails); |
44 | exit(status: 1); |
45 | } |
46 | } |
47 | |
48 | int |
49 | main (int argc, char *argv[]) |
50 | { |
51 | char key[64],plain[64],cipher[64],answer[64]; |
52 | int i; |
53 | int test; |
54 | int fail; |
55 | |
56 | for(test=0;!feof(stdin);test++){ |
57 | |
58 | get8(cp: key); |
59 | printf(format: " K: " ); put8(cp: key); |
60 | setkey(key); |
61 | |
62 | get8(cp: plain); |
63 | printf(format: " P: " ); put8(cp: plain); |
64 | |
65 | get8(cp: answer); |
66 | printf(format: " C: " ); put8(cp: answer); |
67 | |
68 | for(i=0;i<64;i++) |
69 | cipher[i] = plain[i]; |
70 | encrypt(cipher, 0); |
71 | |
72 | for(i=0;i<64;i++) |
73 | if(cipher[i] != answer[i]) |
74 | break; |
75 | fail = 0; |
76 | if(i != 64){ |
77 | printf(format: " Encrypt FAIL" ); |
78 | fail++; totfails++; |
79 | } |
80 | |
81 | encrypt(cipher, 1); |
82 | |
83 | for(i=0;i<64;i++) |
84 | if(cipher[i] != plain[i]) |
85 | break; |
86 | if(i != 64){ |
87 | printf(format: " Decrypt FAIL" ); |
88 | fail++; totfails++; |
89 | } |
90 | |
91 | if(fail == 0) |
92 | printf(format: " OK" ); |
93 | printf(format: "\n" ); |
94 | } |
95 | good_bye(); |
96 | } |
97 | void |
98 | get8 (char *cp) |
99 | { |
100 | int i,j,t; |
101 | |
102 | for(i=0;i<8;i++){ |
103 | scanf(format: "%2x" ,&t); |
104 | if(feof(stdin)) |
105 | good_bye(); |
106 | for(j=0; j<8 ; j++) { |
107 | *cp++ = (t & (0x01 << (7-j))) != 0; |
108 | } |
109 | } |
110 | } |
111 | void |
112 | put8 (char *cp) |
113 | { |
114 | int i,j,t; |
115 | |
116 | for(i=0;i<8;i++){ |
117 | t = 0; |
118 | for(j = 0; j<8; j++) |
119 | t = (t<<1) | *cp++; |
120 | printf(format: "%02x" , t); |
121 | } |
122 | } |
123 | |
124 | #else /* encrypt and setkey are not available. */ |
125 | |
126 | int |
127 | main (void) |
128 | { |
129 | return 77; /* UNSUPPORTED */ |
130 | } |
131 | |
132 | #endif |
133 | |