cbc.c revision 1.7 1 /* cbc.c: This file contains the encryption routines for the ed line editor */
2 /*-
3 * Copyright (c) 1993 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * Copyright (c) 1993 Andrew Moore, Talke Studio.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 *
37 * from: @(#)bdes.c 5.5 (Berkeley) 6/27/91
38 */
39
40 #ifndef lint
41 /* static char sccsid[] = "@(#)cbc.c 5.5 (Talke Studio) 6/27/91"; */
42 static char rcsid[] = "$Id: cbc.c,v 1.7 1993/11/23 04:41:50 alm Exp $";
43 #endif /* not lint */
44
45 #include <errno.h>
46 #include <pwd.h>
47 #include <unistd.h>
48 #include <stdio.h>
49 #include <ctype.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <sys/types.h>
53
54 #include "ed.h"
55
56 /*
57 * Define a divisor for rand() that yields a uniform distribution in the
58 * range 0-255.
59 */
60 #define RAND_DIV (((unsigned) RAND_MAX + 1) >> 8)
61
62 /*
63 * BSD and System V systems offer special library calls that do
64 * block move_liness and fills, so if possible we take advantage of them
65 */
66 #define MEMCPY(dest,src,len) memcpy((dest),(src),(len))
67 #define MEMZERO(dest,len) memset((dest), 0, (len))
68
69 /* Hide the calls to the primitive encryption routines. */
70 #define DES_KEY(buf) \
71 if (des_setkey(buf)) \
72 des_error("des_setkey");
73 #define DES_XFORM(buf) \
74 if (des_cipher(buf, buf, 0L, (inverse ? -1 : 1))) \
75 des_error("des_cipher");
76
77 /*
78 * read/write - no error checking
79 */
80 #define READ(buf, n, fp) fread(buf, sizeof(char), n, fp)
81 #define WRITE(buf, n, fp) fwrite(buf, sizeof(char), n, fp)
82
83 /*
84 * some things to make references easier
85 */
86 typedef char Desbuf[8];
87 #define CHAR(x,i) (x[i])
88 #define UCHAR(x,i) (x[i])
89 #define BUFFER(x) (x)
90 #define UBUFFER(x) (x)
91
92 /*
93 * global variables and related macros
94 */
95
96 enum { /* encrypt, decrypt, authenticate */
97 MODE_ENCRYPT, MODE_DECRYPT, MODE_AUTHENTICATE
98 } mode = MODE_ENCRYPT;
99
100 Desbuf ivec; /* initialization vector */
101 Desbuf pvec; /* padding vector */
102 char bits[] = { /* used to extract bits from a char */
103 '\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
104 };
105 int pflag; /* 1 to preserve parity bits */
106
107 char des_buf[8]; /* shared buffer for get_des_char/put_des_char */
108 int des_ct = 0; /* count for get_des_char/put_des_char */
109 int des_n = 0; /* index for put_des_char/get_des_char */
110
111
112 /* init_des_cipher: initialize DES */
113 void
114 init_des_cipher()
115 {
116 #ifdef DES
117 int i;
118
119 des_ct = des_n = 0;
120
121 /* initialize the initialization vctor */
122 MEMZERO(ivec, 8);
123
124 /* intialize the padding vector */
125 srand((unsigned) time((time_t *) 0));
126 for (i = 0; i < 8; i++)
127 CHAR(pvec, i) = (char) (rand()/RAND_DIV);
128 #endif
129 }
130
131
132 /* get_des_char: return next char in an encrypted file */
133 int
134 get_des_char(fp)
135 FILE *fp;
136 {
137 #ifdef DES
138 if (des_n >= des_ct) {
139 des_n = 0;
140 des_ct = cbc_decode(des_buf, fp);
141 }
142 return (des_ct > 0) ? des_buf[des_n++] : EOF;
143 #endif
144 }
145
146
147 /* put_des_char: write a char to an encrypted file; return char written */
148 int
149 put_des_char(c, fp)
150 int c;
151 FILE *fp;
152 {
153 #ifdef DES
154 if (des_n == sizeof des_buf) {
155 des_ct = cbc_encode(des_buf, des_n, fp);
156 des_n = 0;
157 }
158 return (des_ct >= 0) ? (des_buf[des_n++] = c) : EOF;
159 #endif
160 }
161
162
163 /* flush_des_file: flush an encrypted file's output; return status */
164 int
165 flush_des_file(fp)
166 FILE *fp;
167 {
168 #ifdef DES
169 if (des_n == sizeof des_buf) {
170 des_ct = cbc_encode(des_buf, des_n, fp);
171 des_n = 0;
172 }
173 return (des_ct >= 0 && cbc_encode(des_buf, des_n, fp) >= 0) ? 0 : EOF;
174 #endif
175 }
176
177 #ifdef DES
178 /*
179 * get keyword from tty or stdin
180 */
181 int
182 get_keyword()
183 {
184 register char *p; /* used to obtain the key */
185 Desbuf msgbuf; /* I/O buffer */
186
187 /*
188 * get the key
189 */
190 if (*(p = getpass("Enter key: "))) {
191
192 /*
193 * copy it, nul-padded, into the key area
194 */
195 expand_des_key(BUFFER(msgbuf), p);
196 MEMZERO(p, _PASSWORD_LEN);
197 set_des_key(msgbuf);
198 MEMZERO(msgbuf, sizeof msgbuf);
199 return 1;
200 }
201 return 0;
202 }
203
204
205 extern char errmsg[];
206
207 /*
208 * print a warning message and, possibly, terminate
209 */
210 void
211 des_error(s)
212 char *s; /* the message */
213 {
214 (void)sprintf(errmsg, "%s", s ? s : strerror(errno));
215 }
216
217 /*
218 * map a hex character to an integer
219 */
220 int
221 hex_to_binary(c, radix)
222 int c; /* char to be converted */
223 int radix; /* base (2 to 16) */
224 {
225 switch(c) {
226 case '0': return(0x0);
227 case '1': return(0x1);
228 case '2': return(radix > 2 ? 0x2 : -1);
229 case '3': return(radix > 3 ? 0x3 : -1);
230 case '4': return(radix > 4 ? 0x4 : -1);
231 case '5': return(radix > 5 ? 0x5 : -1);
232 case '6': return(radix > 6 ? 0x6 : -1);
233 case '7': return(radix > 7 ? 0x7 : -1);
234 case '8': return(radix > 8 ? 0x8 : -1);
235 case '9': return(radix > 9 ? 0x9 : -1);
236 case 'A': case 'a': return(radix > 10 ? 0xa : -1);
237 case 'B': case 'b': return(radix > 11 ? 0xb : -1);
238 case 'C': case 'c': return(radix > 12 ? 0xc : -1);
239 case 'D': case 'd': return(radix > 13 ? 0xd : -1);
240 case 'E': case 'e': return(radix > 14 ? 0xe : -1);
241 case 'F': case 'f': return(radix > 15 ? 0xf : -1);
242 }
243 /*
244 * invalid character
245 */
246 return(-1);
247 }
248
249 /*
250 * convert the key to a bit pattern
251 */
252 void
253 expand_des_key(obuf, ibuf)
254 char *obuf; /* bit pattern */
255 char *ibuf; /* the key itself */
256 {
257 register int i, j; /* counter in a for loop */
258 int nbuf[64]; /* used for hex/key translation */
259
260 /*
261 * leading '0x' or '0X' == hex key
262 */
263 if (ibuf[0] == '0' && (ibuf[1] == 'x' || ibuf[1] == 'X')) {
264 ibuf = &ibuf[2];
265 /*
266 * now translate it, bombing on any illegal hex digit
267 */
268 for (i = 0; ibuf[i] && i < 16; i++)
269 if ((nbuf[i] = hex_to_binary((int) ibuf[i], 16)) == -1)
270 des_error("bad hex digit in key");
271 while (i < 16)
272 nbuf[i++] = 0;
273 for (i = 0; i < 8; i++)
274 obuf[i] =
275 ((nbuf[2*i]&0xf)<<4) | (nbuf[2*i+1]&0xf);
276 /* preserve parity bits */
277 pflag = 1;
278 return;
279 }
280 /*
281 * leading '0b' or '0B' == binary key
282 */
283 if (ibuf[0] == '0' && (ibuf[1] == 'b' || ibuf[1] == 'B')) {
284 ibuf = &ibuf[2];
285 /*
286 * now translate it, bombing on any illegal binary digit
287 */
288 for (i = 0; ibuf[i] && i < 16; i++)
289 if ((nbuf[i] = hex_to_binary((int) ibuf[i], 2)) == -1)
290 des_error("bad binary digit in key");
291 while (i < 64)
292 nbuf[i++] = 0;
293 for (i = 0; i < 8; i++)
294 for (j = 0; j < 8; j++)
295 obuf[i] = (obuf[i]<<1)|nbuf[8*i+j];
296 /* preserve parity bits */
297 pflag = 1;
298 return;
299 }
300 /*
301 * no special leader -- ASCII
302 */
303 (void)strncpy(obuf, ibuf, 8);
304 }
305
306 /*****************
307 * DES FUNCTIONS *
308 *****************/
309 /*
310 * This sets the DES key and (if you're using the deszip version)
311 * the direction of the transformation. This uses the Sun
312 * to map the 64-bit key onto the 56 bits that the key schedule
313 * generation routines use: the old way, which just uses the user-
314 * supplied 64 bits as is, and the new way, which resets the parity
315 * bit to be the same as the low-order bit in each character. The
316 * new way generates a greater variety of key schedules, since many
317 * systems set the parity (high) bit of each character to 0, and the
318 * DES ignores the low order bit of each character.
319 */
320 void
321 set_des_key(buf)
322 Desbuf buf; /* key block */
323 {
324 register int i, j; /* counter in a for loop */
325 register int par; /* parity counter */
326
327 /*
328 * if the parity is not preserved, flip it
329 */
330 if (!pflag) {
331 for (i = 0; i < 8; i++) {
332 par = 0;
333 for (j = 1; j < 8; j++)
334 if ((bits[j]&UCHAR(buf, i)) != 0)
335 par++;
336 if ((par&01) == 01)
337 UCHAR(buf, i) = UCHAR(buf, i)&0177;
338 else
339 UCHAR(buf, i) = (UCHAR(buf, i)&0177)|0200;
340 }
341 }
342
343 DES_KEY(UBUFFER(buf));
344 }
345
346
347 /*
348 * This encrypts using the Cipher Block Chaining mode of DES
349 */
350 int
351 cbc_encode(msgbuf, n, fp)
352 char *msgbuf;
353 int n;
354 FILE *fp;
355 {
356 int inverse = 0; /* 0 to encrypt, 1 to decrypt */
357
358 /*
359 * do the transformation
360 */
361 if (n == 8) {
362 for (n = 0; n < 8; n++)
363 CHAR(msgbuf, n) ^= CHAR(ivec, n);
364 DES_XFORM(UBUFFER(msgbuf));
365 MEMCPY(BUFFER(ivec), BUFFER(msgbuf), 8);
366 return WRITE(BUFFER(msgbuf), 8, fp);
367 }
368 /*
369 * at EOF or last block -- in either case, the last byte contains
370 * the character representation of the number of bytes in it
371 */
372 /*
373 MEMZERO(msgbuf + n, 8 - n);
374 */
375 /*
376 * Pad the last block randomly
377 */
378 (void)MEMCPY(BUFFER(msgbuf + n), BUFFER(pvec), 8 - n);
379 CHAR(msgbuf, 7) = n;
380 for (n = 0; n < 8; n++)
381 CHAR(msgbuf, n) ^= CHAR(ivec, n);
382 DES_XFORM(UBUFFER(msgbuf));
383 return WRITE(BUFFER(msgbuf), 8, fp);
384 }
385
386 /*
387 * This decrypts using the Cipher Block Chaining mode of DES
388 */
389 int
390 cbc_decode(msgbuf, fp)
391 char *msgbuf; /* I/O buffer */
392 FILE *fp; /* input file descriptor */
393 {
394 Desbuf ibuf; /* temp buffer for initialization vector */
395 register int n; /* number of bytes actually read */
396 register int c; /* used to test for EOF */
397 int inverse = 1; /* 0 to encrypt, 1 to decrypt */
398
399 if ((n = READ(BUFFER(msgbuf), 8, fp)) == 8) {
400 /*
401 * do the transformation
402 */
403 MEMCPY(BUFFER(ibuf), BUFFER(msgbuf), 8);
404 DES_XFORM(UBUFFER(msgbuf));
405 for (c = 0; c < 8; c++)
406 UCHAR(msgbuf, c) ^= UCHAR(ivec, c);
407 MEMCPY(BUFFER(ivec), BUFFER(ibuf), 8);
408 /*
409 * if the last one, handle it specially
410 */
411 if ((c = fgetc(fp)) == EOF) {
412 n = CHAR(msgbuf, 7);
413 if (n < 0 || n > 7) {
414 des_error("decryption failed (block corrupted)");
415 return EOF;
416 }
417 } else
418 (void)ungetc(c, fp);
419 return n;
420 }
421 if (n > 0)
422 des_error("decryption failed (incomplete block)");
423 else if (n < 0)
424 des_error("cannot read file");
425 return EOF;
426 }
427 #endif /* DES */
428