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