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