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