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