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