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