Home | History | Annotate | Line # | Download | only in ed
cbc.c revision 1.9
      1 /*	$NetBSD: cbc.c,v 1.9 1995/03/21 09:04:36 cgd 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 #ifndef lint
     43 #if 0
     44 static char *rcsid = "@(#)cbc.c,v 1.2 1994/02/01 00:34:36 alm Exp";
     45 #else
     46 static char rcsid[] = "$NetBSD: cbc.c,v 1.9 1995/03/21 09:04:36 cgd Exp $";
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/types.h>
     51 #include <ctype.h>
     52 #include <errno.h>
     53 #include <pwd.h>
     54 
     55 #include "ed.h"
     56 
     57 
     58 /*
     59  * Define a divisor for rand() that yields a uniform distribution in the
     60  * range 0-255.
     61  */
     62 #define	RAND_DIV (((unsigned) RAND_MAX + 1) >> 8)
     63 
     64 /*
     65  * BSD and System V systems offer special library calls that do
     66  * block move_liness and fills, so if possible we take advantage of them
     67  */
     68 #define	MEMCPY(dest,src,len)	memcpy((dest),(src),(len))
     69 #define	MEMZERO(dest,len)	memset((dest), 0, (len))
     70 
     71 /* Hide the calls to the primitive encryption routines. */
     72 #define	DES_KEY(buf) \
     73 	if (des_setkey(buf)) \
     74 		des_error("des_setkey");
     75 #define	DES_XFORM(buf) \
     76 	if (des_cipher(buf, buf, 0L, (inverse ? -1 : 1))) \
     77 		des_error("des_cipher");
     78 
     79 /*
     80  * read/write - no error checking
     81  */
     82 #define	READ(buf, n, fp)	fread(buf, sizeof(char), n, fp)
     83 #define WRITE(buf, n, fp)	fwrite(buf, sizeof(char), n, fp)
     84 
     85 /*
     86  * some things to make references easier
     87  */
     88 typedef char Desbuf[8];
     89 #define	CHAR(x,i)	(x[i])
     90 #define	UCHAR(x,i)	(x[i])
     91 #define	BUFFER(x)	(x)
     92 #define	UBUFFER(x)	(x)
     93 
     94 /*
     95  * global variables and related macros
     96  */
     97 
     98 enum { 					/* encrypt, decrypt, authenticate */
     99 	MODE_ENCRYPT, MODE_DECRYPT, MODE_AUTHENTICATE
    100 } mode = MODE_ENCRYPT;
    101 
    102 Desbuf ivec;				/* initialization vector */
    103 Desbuf pvec;				/* padding vector */
    104 char bits[] = {				/* used to extract bits from a char */
    105 	'\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
    106 };
    107 int pflag;				/* 1 to preserve parity bits */
    108 
    109 unsigned char des_buf[8];	/* shared buffer for get_des_char/put_des_char */
    110 int des_ct = 0;			/* count for get_des_char/put_des_char */
    111 int des_n = 0;			/* index for put_des_char/get_des_char */
    112 
    113 
    114 /* init_des_cipher: initialize DES */
    115 void
    116 init_des_cipher()
    117 {
    118 #ifdef DES
    119 	int i;
    120 
    121 	des_ct = des_n = 0;
    122 
    123 	/* initialize the initialization vctor */
    124 	MEMZERO(ivec, 8);
    125 
    126 	/* intialize the padding vector */
    127 	srand((unsigned) time((time_t *) 0));
    128 	for (i = 0; i < 8; i++)
    129 		CHAR(pvec, i) = (char) (rand()/RAND_DIV);
    130 #endif
    131 }
    132 
    133 
    134 /* get_des_char: return next char in an encrypted file */
    135 int
    136 get_des_char(fp)
    137 	FILE *fp;
    138 {
    139 #ifdef DES
    140 	if (des_n >= des_ct) {
    141 		des_n = 0;
    142 		des_ct = cbc_decode(des_buf, fp);
    143 	}
    144 	return (des_ct > 0) ? des_buf[des_n++] : EOF;
    145 #endif
    146 }
    147 
    148 
    149 /* put_des_char: write a char to an encrypted file; return char written */
    150 int
    151 put_des_char(c, fp)
    152 	int c;
    153 	FILE *fp;
    154 {
    155 #ifdef DES
    156 	if (des_n == sizeof des_buf) {
    157 		des_ct = cbc_encode(des_buf, des_n, fp);
    158 		des_n = 0;
    159 	}
    160 	return (des_ct >= 0) ? (des_buf[des_n++] = c) : EOF;
    161 #endif
    162 }
    163 
    164 
    165 /* flush_des_file: flush an encrypted file's output; return status */
    166 int
    167 flush_des_file(fp)
    168 	FILE *fp;
    169 {
    170 #ifdef DES
    171 	if (des_n == sizeof des_buf) {
    172 		des_ct = cbc_encode(des_buf, des_n, fp);
    173 		des_n = 0;
    174 	}
    175 	return (des_ct >= 0 && cbc_encode(des_buf, des_n, fp) >= 0) ? 0 : EOF;
    176 #endif
    177 }
    178 
    179 #ifdef DES
    180 /*
    181  * get keyword from tty or stdin
    182  */
    183 int
    184 get_keyword()
    185 {
    186 	register char *p;		/* used to obtain the key */
    187 	Desbuf msgbuf;			/* I/O buffer */
    188 
    189 	/*
    190 	 * get the key
    191 	 */
    192 	if (*(p = getpass("Enter key: "))) {
    193 
    194 		/*
    195 		 * copy it, nul-padded, into the key area
    196 		 */
    197 		expand_des_key(BUFFER(msgbuf), p);
    198 		MEMZERO(p, _PASSWORD_LEN);
    199 		set_des_key(msgbuf);
    200 		MEMZERO(msgbuf, sizeof msgbuf);
    201 		return 1;
    202 	}
    203 	return 0;
    204 }
    205 
    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