Commit | Line | Data |
---|---|---|
05a0983d GR |
1 | #include <stdio.h> |
2 | #include <stdlib.h> | |
3 | #include <string.h> | |
4 | #ifdef __MSDOS__ | |
5 | #include <dos.h> | |
6 | #endif | |
7 | #ifdef unix /* Assume POSIX */ | |
8 | #include <fcntl.h> | |
9 | #include <termios.h> | |
10 | #endif | |
11 | #include "md4.h" | |
12 | #include "skey.h" | |
13 | ||
14 | #if (defined(__MSDOS__) || defined(MPU8086) || defined(MPU8080) \ | |
15 | || defined(vax) || defined (MIPSEL)) | |
16 | #define LITTLE_ENDIAN /* Low order bytes are first in memory */ | |
17 | #endif /* Almost all other machines are big-endian */ | |
18 | ||
19 | /* Crunch a key: | |
20 | * concatenate the seed and the password, run through MD4 and | |
21 | * collapse to 64 bits. This is defined as the user's starting key. | |
22 | */ | |
23 | int | |
24 | keycrunch(result,seed,passwd) | |
25 | char *result; /* 8-byte result */ | |
26 | char *seed; /* Seed, any length */ | |
27 | char *passwd; /* Password, any length */ | |
28 | { | |
29 | char *buf; | |
30 | MDstruct md; | |
31 | unsigned int buflen; | |
32 | #ifndef LITTLE_ENDIAN | |
33 | int i; | |
34 | register long tmp; | |
35 | #endif | |
36 | ||
37 | buflen = strlen(seed) + strlen(passwd); | |
38 | if((buf = malloc(buflen+1)) == NULL) | |
39 | return -1; | |
40 | strcpy(buf,seed); | |
41 | strcat(buf,passwd); | |
42 | ||
43 | /* Crunch the key through MD4 */ | |
44 | sevenbit(buf); | |
45 | MDbegin(&md); | |
46 | MDupdate(&md,(unsigned char *)buf,8*buflen); | |
47 | ||
48 | free(buf); | |
49 | ||
50 | /* Fold result from 128 to 64 bits */ | |
51 | md.buffer[0] ^= md.buffer[2]; | |
52 | md.buffer[1] ^= md.buffer[3]; | |
53 | ||
54 | #ifdef LITTLE_ENDIAN | |
55 | /* Only works on byte-addressed little-endian machines!! */ | |
56 | memcpy(result,(char *)md.buffer,8); | |
57 | #else | |
58 | /* Default (but slow) code that will convert to | |
59 | * little-endian byte ordering on any machine | |
60 | */ | |
61 | for(i=0;i<2;i++){ | |
62 | tmp = md.buffer[i]; | |
63 | *result++ = tmp; | |
64 | tmp >>= 8; | |
65 | *result++ = tmp; | |
66 | tmp >>= 8; | |
67 | *result++ = tmp; | |
68 | tmp >>= 8; | |
69 | *result++ = tmp; | |
70 | } | |
71 | #endif | |
72 | ||
73 | return 0; | |
74 | } | |
75 | ||
76 | /* The one-way function f(). Takes 8 bytes and returns 8 bytes in place */ | |
77 | void | |
78 | f(x) | |
79 | char *x; | |
80 | { | |
81 | MDstruct md; | |
82 | #ifndef LITTLE_ENDIAN | |
83 | register long tmp; | |
84 | #endif | |
85 | ||
86 | MDbegin(&md); | |
87 | MDupdate(&md,(unsigned char *)x,64); | |
88 | ||
89 | /* Fold 128 to 64 bits */ | |
90 | md.buffer[0] ^= md.buffer[2]; | |
91 | md.buffer[1] ^= md.buffer[3]; | |
92 | ||
93 | #ifdef LITTLE_ENDIAN | |
94 | /* Only works on byte-addressed little-endian machines!! */ | |
95 | memcpy(x,(char *)md.buffer,8); | |
96 | ||
97 | #else | |
98 | /* Default (but slow) code that will convert to | |
99 | * little-endian byte ordering on any machine | |
100 | */ | |
101 | tmp = md.buffer[0]; | |
102 | *x++ = tmp; | |
103 | tmp >>= 8; | |
104 | *x++ = tmp; | |
105 | tmp >>= 8; | |
106 | *x++ = tmp; | |
107 | tmp >>= 8; | |
108 | *x++ = tmp; | |
109 | ||
110 | tmp = md.buffer[1]; | |
111 | *x++ = tmp; | |
112 | tmp >>= 8; | |
113 | *x++ = tmp; | |
114 | tmp >>= 8; | |
115 | *x++ = tmp; | |
116 | tmp >>= 8; | |
117 | *x = tmp; | |
118 | #endif | |
119 | } | |
120 | ||
121 | /* Strip trailing cr/lf from a line of text */ | |
122 | void | |
123 | rip(buf) | |
124 | char *buf; | |
125 | { | |
126 | char *cp; | |
127 | ||
128 | if((cp = strchr(buf,'\r')) != NULL) | |
129 | *cp = '\0'; | |
130 | ||
131 | if((cp = strchr(buf,'\n')) != NULL) | |
132 | *cp = '\0'; | |
133 | } | |
134 | /************************/ | |
135 | #ifdef __MSDOS__ | |
136 | char * | |
137 | readpass(buf,n) | |
138 | char *buf; | |
139 | int n; | |
140 | { | |
141 | int i; | |
142 | char *cp; | |
143 | ||
144 | for(cp=buf,i = 0; i < n ; i++) | |
145 | if ((*cp++ = bdos(7,0,0)) == '\r') | |
146 | break; | |
147 | *cp = '\0'; | |
148 | printf("\n"); | |
149 | rip(buf); | |
150 | return buf; | |
151 | } | |
152 | #else | |
153 | char * | |
154 | readpass(buf,n) | |
155 | char *buf; | |
156 | int n; | |
157 | { | |
158 | struct termios saved_ttymode; | |
159 | struct termios noecho_ttymode; | |
160 | ||
161 | /* Save normal line editing modes */ | |
162 | tcgetattr(0, &saved_ttymode); | |
163 | ||
164 | /* Turn off echoing */ | |
165 | tcgetattr(0, &noecho_ttymode); | |
166 | noecho_ttymode.c_lflag &= ~ECHO; | |
167 | tcsetattr(0, TCSANOW, &noecho_ttymode); | |
168 | fgets(buf,n,stdin); | |
169 | rip(buf); | |
170 | ||
171 | /* Restore previous tty modes */ | |
172 | tcsetattr(0, TCSANOW, &saved_ttymode); | |
173 | ||
174 | /* | |
175 | after the secret key is taken from the keyboard, the line feed is | |
176 | written to standard error instead of standard output. That means that | |
177 | anyone using the program from a terminal won't notice, but capturing | |
178 | standard output will get the key words without a newline in front of | |
179 | them. | |
180 | */ | |
181 | fprintf(stderr, "\n"); | |
182 | fflush(stderr); | |
183 | sevenbit(buf); | |
184 | ||
185 | return buf; | |
186 | } | |
187 | ||
188 | #endif | |
189 | ||
190 | /* removebackspaced over charaters from the string*/ | |
191 | backspace(buf) | |
192 | char *buf; | |
193 | { | |
194 | char bs = 0x8; | |
195 | char *cp = buf; | |
196 | char *out = buf; | |
197 | ||
198 | while(*cp){ | |
199 | if( *cp == bs ) { | |
200 | if(out == buf){ | |
201 | cp++; | |
202 | continue; | |
203 | } | |
204 | else { | |
205 | cp++; | |
206 | out--; | |
207 | } | |
208 | } | |
209 | else { | |
210 | *out++ = *cp++; | |
211 | } | |
212 | ||
213 | } | |
214 | *out = '\0'; | |
215 | ||
216 | } | |
217 | sevenbit(s) | |
218 | char *s; | |
219 | { | |
220 | /* make sure there are only 7 bit code in the line*/ | |
221 | while(*s){ | |
222 | *s = 0x7f & ( *s); | |
223 | s++; | |
224 | } | |
225 | } |