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