Home | History | Annotate | Line # | Download | only in bdes
bdes.c revision 1.5
      1  1.5      agc /*	$NetBSD: bdes.c,v 1.5 2003/08/07 11:13:11 agc Exp $	*/
      2  1.1  thorpej 
      3  1.1  thorpej /*-
      4  1.1  thorpej  * Copyright (c) 1991, 1993
      5  1.1  thorpej  *	The Regents of the University of California.  All rights reserved.
      6  1.1  thorpej  *
      7  1.1  thorpej  * This code is derived from software contributed to Berkeley by
      8  1.1  thorpej  * Matt Bishop of Dartmouth College.
      9  1.1  thorpej  *
     10  1.1  thorpej  * The United States Government has rights in this work pursuant
     11  1.1  thorpej  * to contract no. NAG 2-680 between the National Aeronautics and
     12  1.1  thorpej  * Space Administration and Dartmouth College.
     13  1.1  thorpej  *
     14  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     15  1.1  thorpej  * modification, are permitted provided that the following conditions
     16  1.1  thorpej  * are met:
     17  1.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     18  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     19  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     20  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     21  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     22  1.5      agc  * 3. Neither the name of the University nor the names of its contributors
     23  1.1  thorpej  *    may be used to endorse or promote products derived from this software
     24  1.1  thorpej  *    without specific prior written permission.
     25  1.1  thorpej  *
     26  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1  thorpej  * SUCH DAMAGE.
     37  1.1  thorpej  */
     38  1.1  thorpej 
     39  1.2  thorpej #include <sys/cdefs.h>
     40  1.1  thorpej #ifndef lint
     41  1.2  thorpej __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
     42  1.2  thorpej 	The Regents of the University of California.  All rights reserved.\n");
     43  1.1  thorpej #endif /* not lint */
     44  1.1  thorpej 
     45  1.1  thorpej #ifndef lint
     46  1.1  thorpej #if 0
     47  1.1  thorpej static char sccsid[] = "@(#)bdes.c	8.1 (Berkeley) 6/6/93";
     48  1.1  thorpej #else
     49  1.5      agc __RCSID("$NetBSD: bdes.c,v 1.5 2003/08/07 11:13:11 agc Exp $");
     50  1.1  thorpej #endif
     51  1.1  thorpej #endif /* not lint */
     52  1.1  thorpej 
     53  1.1  thorpej /*
     54  1.1  thorpej  * BDES -- DES encryption package for Berkeley Software Distribution 4.4
     55  1.1  thorpej  * options:
     56  1.1  thorpej  *	-a	key is in ASCII
     57  1.1  thorpej  *	-b	use ECB (electronic code book) mode
     58  1.1  thorpej  *	-d	invert (decrypt) input
     59  1.1  thorpej  *	-f b	use b-bit CFB (cipher feedback) mode
     60  1.1  thorpej  *	-F b	use b-bit CFB (cipher feedback) alternative mode
     61  1.1  thorpej  *	-k key	use key as the cryptographic key
     62  1.1  thorpej  *	-m b	generate a MAC of length b
     63  1.1  thorpej  *	-o b	use b-bit OFB (output feedback) mode
     64  1.1  thorpej  *	-p	don't reset the parity bit
     65  1.1  thorpej  *	-v v	use v as the initialization vector (ignored for ECB)
     66  1.1  thorpej  * note: the last character of the last block is the integer indicating
     67  1.1  thorpej  * how many characters of that block are to be output
     68  1.1  thorpej  *
     69  1.1  thorpej  * Author: Matt Bishop
     70  1.1  thorpej  *	   Department of Mathematics and Computer Science
     71  1.1  thorpej  *	   Dartmouth College
     72  1.1  thorpej  *	   Hanover, NH  03755
     73  1.1  thorpej  * Email:  Matt.Bishop (at) dartmouth.edu
     74  1.1  thorpej  *	   ...!decvax!dartvax!Matt.Bishop
     75  1.1  thorpej  *
     76  1.1  thorpej  * See Technical Report PCS-TR91-158, Department of Mathematics and Computer
     77  1.1  thorpej  * Science, Dartmouth College, for a detailed description of the implemen-
     78  1.1  thorpej  * tation and differences between it and Sun's.  The DES is described in
     79  1.1  thorpej  * FIPS PUB 46, and the modes in FIPS PUB 81 (see either the manual page
     80  1.1  thorpej  * or the technical report for a complete reference).
     81  1.1  thorpej  */
     82  1.1  thorpej 
     83  1.1  thorpej #include <errno.h>
     84  1.1  thorpej #include <unistd.h>
     85  1.1  thorpej #include <stdio.h>
     86  1.1  thorpej #include <ctype.h>
     87  1.1  thorpej #include <stdlib.h>
     88  1.1  thorpej #include <string.h>
     89  1.1  thorpej 
     90  1.1  thorpej /*
     91  1.1  thorpej  * BSD and System V systems offer special library calls that do
     92  1.1  thorpej  * block moves and fills, so if possible we take advantage of them
     93  1.1  thorpej  */
     94  1.1  thorpej #define	MEMCPY(dest,src,len)	bcopy((src),(dest),(len))
     95  1.1  thorpej #define	MEMZERO(dest,len)	bzero((dest),(len))
     96  1.1  thorpej 
     97  1.1  thorpej /* Hide the calls to the primitive encryption routines. */
     98  1.1  thorpej #define	FASTWAY
     99  1.1  thorpej #ifdef	FASTWAY
    100  1.1  thorpej #define	DES_KEY(buf) \
    101  1.1  thorpej 	if (des_setkey(buf)) \
    102  1.2  thorpej 		bdes_err(0, "des_setkey");
    103  1.1  thorpej #define	DES_XFORM(buf) \
    104  1.1  thorpej 	if (des_cipher(buf, buf, 0L, (inverse ? -1 : 1))) \
    105  1.2  thorpej 		bdes_err(0, "des_cipher");
    106  1.1  thorpej #else
    107  1.1  thorpej #define	DES_KEY(buf)	{						\
    108  1.1  thorpej 				char bits1[64];	/* bits of key */	\
    109  1.1  thorpej 				expand(buf, bits1);			\
    110  1.1  thorpej 				if (setkey(bits1))			\
    111  1.2  thorpej 					bdes_err(0, "setkey");		\
    112  1.1  thorpej 			}
    113  1.1  thorpej #define	DES_XFORM(buf)	{						\
    114  1.1  thorpej 				char bits1[64];	/* bits of message */	\
    115  1.1  thorpej 				expand(buf, bits1);			\
    116  1.1  thorpej 				if (encrypt(bits1, inverse))		\
    117  1.2  thorpej 					bdes_err(0, "encrypt");		\
    118  1.1  thorpej 				compress(bits1, buf);			\
    119  1.1  thorpej 			}
    120  1.1  thorpej #endif
    121  1.1  thorpej 
    122  1.1  thorpej /*
    123  1.1  thorpej  * this does an error-checking write
    124  1.1  thorpej  */
    125  1.1  thorpej #define	READ(buf, n)	fread(buf, sizeof(char), n, stdin)
    126  1.1  thorpej #define WRITE(buf,n)						\
    127  1.1  thorpej 		if (fwrite(buf, sizeof(char), n, stdout) != n)	\
    128  1.2  thorpej 			bdes_err(bn, NULL);
    129  1.1  thorpej 
    130  1.1  thorpej /*
    131  1.1  thorpej  * some things to make references easier
    132  1.1  thorpej  */
    133  1.1  thorpej typedef char Desbuf[8];
    134  1.1  thorpej #define	CHAR(x,i)	(x[i])
    135  1.1  thorpej #define	UCHAR(x,i)	(x[i])
    136  1.1  thorpej #define	BUFFER(x)	(x)
    137  1.1  thorpej #define	UBUFFER(x)	(x)
    138  1.1  thorpej 
    139  1.1  thorpej /*
    140  1.1  thorpej  * global variables and related macros
    141  1.1  thorpej  */
    142  1.1  thorpej #define KEY_DEFAULT		0	/* interpret radix of key from key */
    143  1.1  thorpej #define KEY_ASCII		1	/* key is in ASCII characters */
    144  1.1  thorpej int keybase = KEY_DEFAULT;		/* how to interpret the key */
    145  1.1  thorpej 
    146  1.1  thorpej enum { 					/* encrypt, decrypt, authenticate */
    147  1.1  thorpej 	MODE_ENCRYPT, MODE_DECRYPT, MODE_AUTHENTICATE
    148  1.1  thorpej } mode = MODE_ENCRYPT;
    149  1.1  thorpej enum {					/* ecb, cbc, cfb, cfba, ofb? */
    150  1.1  thorpej 	ALG_ECB, ALG_CBC, ALG_CFB, ALG_OFB, ALG_CFBA
    151  1.1  thorpej } alg = ALG_CBC;
    152  1.1  thorpej 
    153  1.1  thorpej Desbuf ivec;				/* initialization vector */
    154  1.1  thorpej char bits[] = {				/* used to extract bits from a char */
    155  1.1  thorpej 	'\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
    156  1.1  thorpej };
    157  1.1  thorpej int inverse;				/* 0 to encrypt, 1 to decrypt */
    158  1.1  thorpej int macbits = -1;			/* number of bits in authentication */
    159  1.1  thorpej int fbbits = -1;			/* number of feedback bits */
    160  1.1  thorpej int pflag;				/* 1 to preserve parity bits */
    161  1.1  thorpej 
    162  1.2  thorpej int	setbits(char *, int);
    163  1.2  thorpej void	bdes_err(int, const char *);
    164  1.2  thorpej int	tobinhex(char, int);
    165  1.2  thorpej void	cvtkey(char *, char *);
    166  1.2  thorpej void	makekey(Desbuf);
    167  1.2  thorpej void	ecbenc(void);
    168  1.2  thorpej void	ecbdec(void);
    169  1.2  thorpej void	cbcenc(void);
    170  1.2  thorpej void	cbcdec(void);
    171  1.2  thorpej void	cbcauth(void);
    172  1.2  thorpej void	cfbenc(void);
    173  1.2  thorpej void	cfbdec(void);
    174  1.2  thorpej void	cfbaenc(void);
    175  1.2  thorpej void	cfbadec(void);
    176  1.2  thorpej void	ofbenc(void);
    177  1.2  thorpej void	ofbdec(void);
    178  1.2  thorpej void	cfbauth(void);
    179  1.2  thorpej void	expand(Desbuf, char *);
    180  1.2  thorpej void	compress(char *, Desbuf);
    181  1.2  thorpej void	usage(void);
    182  1.2  thorpej 
    183  1.2  thorpej int
    184  1.2  thorpej main(int ac, char *av[])
    185  1.1  thorpej {
    186  1.1  thorpej 	register int i;			/* counter in a for loop */
    187  1.1  thorpej 	register char *p;		/* used to obtain the key */
    188  1.1  thorpej 	Desbuf msgbuf;			/* I/O buffer */
    189  1.1  thorpej 	int kflag;			/* command-line encryptiooon key */
    190  1.1  thorpej 	int argc;			/* the real arg count */
    191  1.1  thorpej 	char **argv;			/* the real argument vector */
    192  1.1  thorpej 
    193  1.1  thorpej 	/*
    194  1.1  thorpej 	 * Hide the arguments from ps(1) by making private copies of them
    195  1.1  thorpej 	 * and clobbering the global (visible to ps(1)) ones.
    196  1.1  thorpej 	 */
    197  1.1  thorpej 	argc = ac;
    198  1.1  thorpej 	ac = 1;
    199  1.1  thorpej 	argv = malloc((argc + 1) * sizeof(char *));
    200  1.1  thorpej 	for (i = 0; i < argc; ++i) {
    201  1.1  thorpej 		argv[i] = strdup(av[i]);
    202  1.1  thorpej 		MEMZERO(av[i], strlen(av[i]));
    203  1.1  thorpej 	}
    204  1.1  thorpej 	argv[argc] = NULL;
    205  1.1  thorpej 
    206  1.1  thorpej 	/* initialize the initialization vctor */
    207  1.1  thorpej 	MEMZERO(ivec, 8);
    208  1.1  thorpej 
    209  1.1  thorpej 	/* process the argument list */
    210  1.1  thorpej 	kflag = 0;
    211  1.3      mjl 	while ((i = getopt(argc, argv, "abdF:f:k:m:o:pv:")) != -1)
    212  1.1  thorpej 		switch(i) {
    213  1.1  thorpej 		case 'a':		/* key is ASCII */
    214  1.1  thorpej 			keybase = KEY_ASCII;
    215  1.1  thorpej 			break;
    216  1.1  thorpej 		case 'b':		/* use ECB mode */
    217  1.1  thorpej 			alg = ALG_ECB;
    218  1.1  thorpej 			break;
    219  1.1  thorpej 		case 'd':		/* decrypt */
    220  1.1  thorpej 			mode = MODE_DECRYPT;
    221  1.1  thorpej 			break;
    222  1.1  thorpej 		case 'F':		/* use alternative CFB mode */
    223  1.1  thorpej 			alg = ALG_CFBA;
    224  1.1  thorpej 			if ((fbbits = setbits(optarg, 7)) > 56 || fbbits == 0)
    225  1.2  thorpej 				bdes_err(-1,
    226  1.2  thorpej 				    "-F: number must be 1-56 inclusive");
    227  1.1  thorpej 			else if (fbbits == -1)
    228  1.2  thorpej 				bdes_err(-1,
    229  1.2  thorpej 				    "-F: number must be a multiple of 7");
    230  1.1  thorpej 			break;
    231  1.1  thorpej 		case 'f':		/* use CFB mode */
    232  1.1  thorpej 			alg = ALG_CFB;
    233  1.1  thorpej 			if ((fbbits = setbits(optarg, 8)) > 64 || fbbits == 0)
    234  1.2  thorpej 				bdes_err(-1,
    235  1.2  thorpej 				    "-f: number must be 1-64 inclusive");
    236  1.1  thorpej 			else if (fbbits == -1)
    237  1.2  thorpej 				bdes_err(-1,
    238  1.2  thorpej 				    "-f: number must be a multiple of 8");
    239  1.1  thorpej 			break;
    240  1.1  thorpej 		case 'k':		/* encryption key */
    241  1.1  thorpej 			kflag = 1;
    242  1.1  thorpej 			cvtkey(BUFFER(msgbuf), optarg);
    243  1.1  thorpej 			break;
    244  1.1  thorpej 		case 'm':		/* number of bits for MACing */
    245  1.1  thorpej 			mode = MODE_AUTHENTICATE;
    246  1.1  thorpej 			if ((macbits = setbits(optarg, 1)) > 64)
    247  1.2  thorpej 				bdes_err(-1,
    248  1.2  thorpej 				    "-m: number must be 0-64 inclusive");
    249  1.1  thorpej 			break;
    250  1.1  thorpej 		case 'o':		/* use OFB mode */
    251  1.1  thorpej 			alg = ALG_OFB;
    252  1.1  thorpej 			if ((fbbits = setbits(optarg, 8)) > 64 || fbbits == 0)
    253  1.2  thorpej 				bdes_err(-1,
    254  1.2  thorpej 				    "-o: number must be 1-64 inclusive");
    255  1.1  thorpej 			else if (fbbits == -1)
    256  1.2  thorpej 				bdes_err(-1,
    257  1.2  thorpej 				    "-o: number must be a multiple of 8");
    258  1.1  thorpej 			break;
    259  1.1  thorpej 		case 'p':		/* preserve parity bits */
    260  1.1  thorpej 			pflag = 1;
    261  1.1  thorpej 			break;
    262  1.1  thorpej 		case 'v':		/* set initialization vector */
    263  1.1  thorpej 			cvtkey(BUFFER(ivec), optarg);
    264  1.1  thorpej 			break;
    265  1.1  thorpej 		default:		/* error */
    266  1.1  thorpej 			usage();
    267  1.1  thorpej 		}
    268  1.1  thorpej 
    269  1.1  thorpej 	if (!kflag) {
    270  1.1  thorpej 		/*
    271  1.1  thorpej 		 * if the key's not ASCII, assume it is
    272  1.1  thorpej 		 */
    273  1.1  thorpej 		keybase = KEY_ASCII;
    274  1.1  thorpej 		/*
    275  1.1  thorpej 		 * get the key
    276  1.1  thorpej 		 */
    277  1.1  thorpej 		p = getpass("Enter key: ");
    278  1.1  thorpej 		/*
    279  1.1  thorpej 		 * copy it, nul-padded, into the key area
    280  1.1  thorpej 		 */
    281  1.1  thorpej 		cvtkey(BUFFER(msgbuf), p);
    282  1.1  thorpej 	}
    283  1.1  thorpej 
    284  1.1  thorpej 	makekey(msgbuf);
    285  1.1  thorpej 	inverse = (alg == ALG_CBC || alg == ALG_ECB) && mode == MODE_DECRYPT;
    286  1.1  thorpej 
    287  1.1  thorpej 	switch(alg) {
    288  1.1  thorpej 	case ALG_CBC:
    289  1.1  thorpej 		switch(mode) {
    290  1.1  thorpej 		case MODE_AUTHENTICATE:	/* authenticate using CBC mode */
    291  1.1  thorpej 			cbcauth();
    292  1.1  thorpej 			break;
    293  1.1  thorpej 		case MODE_DECRYPT:	/* decrypt using CBC mode */
    294  1.1  thorpej 			cbcdec();
    295  1.1  thorpej 			break;
    296  1.1  thorpej 		case MODE_ENCRYPT:	/* encrypt using CBC mode */
    297  1.1  thorpej 			cbcenc();
    298  1.1  thorpej 			break;
    299  1.1  thorpej 		}
    300  1.1  thorpej 		break;
    301  1.1  thorpej 	case ALG_CFB:
    302  1.1  thorpej 		switch(mode) {
    303  1.1  thorpej 		case MODE_AUTHENTICATE:	/* authenticate using CFB mode */
    304  1.1  thorpej 			cfbauth();
    305  1.1  thorpej 			break;
    306  1.1  thorpej 		case MODE_DECRYPT:	/* decrypt using CFB mode */
    307  1.1  thorpej 			cfbdec();
    308  1.1  thorpej 			break;
    309  1.1  thorpej 		case MODE_ENCRYPT:	/* encrypt using CFB mode */
    310  1.1  thorpej 			cfbenc();
    311  1.1  thorpej 			break;
    312  1.1  thorpej 		}
    313  1.1  thorpej 		break;
    314  1.1  thorpej 	case ALG_CFBA:
    315  1.1  thorpej 		switch(mode) {
    316  1.1  thorpej 		case MODE_AUTHENTICATE:	/* authenticate using CFBA mode */
    317  1.2  thorpej 			bdes_err(-1, "can't authenticate with CFBA mode");
    318  1.1  thorpej 			break;
    319  1.1  thorpej 		case MODE_DECRYPT:	/* decrypt using CFBA mode */
    320  1.1  thorpej 			cfbadec();
    321  1.1  thorpej 			break;
    322  1.1  thorpej 		case MODE_ENCRYPT:	/* encrypt using CFBA mode */
    323  1.1  thorpej 			cfbaenc();
    324  1.1  thorpej 			break;
    325  1.1  thorpej 		}
    326  1.1  thorpej 		break;
    327  1.1  thorpej 	case ALG_ECB:
    328  1.1  thorpej 		switch(mode) {
    329  1.1  thorpej 		case MODE_AUTHENTICATE:	/* authenticate using ECB mode */
    330  1.2  thorpej 			bdes_err(-1, "can't authenticate with ECB mode");
    331  1.1  thorpej 			break;
    332  1.1  thorpej 		case MODE_DECRYPT:	/* decrypt using ECB mode */
    333  1.1  thorpej 			ecbdec();
    334  1.1  thorpej 			break;
    335  1.1  thorpej 		case MODE_ENCRYPT:	/* encrypt using ECB mode */
    336  1.1  thorpej 			ecbenc();
    337  1.1  thorpej 			break;
    338  1.1  thorpej 		}
    339  1.1  thorpej 		break;
    340  1.1  thorpej 	case ALG_OFB:
    341  1.1  thorpej 		switch(mode) {
    342  1.1  thorpej 		case MODE_AUTHENTICATE:	/* authenticate using OFB mode */
    343  1.2  thorpej 			bdes_err(-1, "can't authenticate with OFB mode");
    344  1.1  thorpej 			break;
    345  1.1  thorpej 		case MODE_DECRYPT:	/* decrypt using OFB mode */
    346  1.1  thorpej 			ofbdec();
    347  1.1  thorpej 			break;
    348  1.1  thorpej 		case MODE_ENCRYPT:	/* encrypt using OFB mode */
    349  1.1  thorpej 			ofbenc();
    350  1.1  thorpej 			break;
    351  1.1  thorpej 		}
    352  1.1  thorpej 		break;
    353  1.1  thorpej 	}
    354  1.1  thorpej 	exit(0);
    355  1.1  thorpej }
    356  1.1  thorpej 
    357  1.1  thorpej /*
    358  1.1  thorpej  * print a warning message and, possibly, terminate
    359  1.1  thorpej  */
    360  1.2  thorpej void
    361  1.2  thorpej bdes_err(int n, const char *s)
    362  1.1  thorpej {
    363  1.1  thorpej 	if (n > 0)
    364  1.1  thorpej 		(void)fprintf(stderr, "bdes (block %d): ", n);
    365  1.1  thorpej 	else
    366  1.1  thorpej 		(void)fprintf(stderr, "bdes: ");
    367  1.1  thorpej 	(void)fprintf(stderr, "%s\n", s ? s : strerror(errno));
    368  1.1  thorpej 	exit(1);
    369  1.1  thorpej }
    370  1.1  thorpej 
    371  1.1  thorpej /*
    372  1.1  thorpej  * map a hex character to an integer
    373  1.1  thorpej  */
    374  1.2  thorpej int
    375  1.2  thorpej tobinhex(char c, int radix)
    376  1.1  thorpej {
    377  1.1  thorpej 	switch(c) {
    378  1.1  thorpej 	case '0':		return(0x0);
    379  1.1  thorpej 	case '1':		return(0x1);
    380  1.1  thorpej 	case '2':		return(radix > 2 ? 0x2 : -1);
    381  1.1  thorpej 	case '3':		return(radix > 3 ? 0x3 : -1);
    382  1.1  thorpej 	case '4':		return(radix > 4 ? 0x4 : -1);
    383  1.1  thorpej 	case '5':		return(radix > 5 ? 0x5 : -1);
    384  1.1  thorpej 	case '6':		return(radix > 6 ? 0x6 : -1);
    385  1.1  thorpej 	case '7':		return(radix > 7 ? 0x7 : -1);
    386  1.1  thorpej 	case '8':		return(radix > 8 ? 0x8 : -1);
    387  1.1  thorpej 	case '9':		return(radix > 9 ? 0x9 : -1);
    388  1.1  thorpej 	case 'A': case 'a':	return(radix > 10 ? 0xa : -1);
    389  1.1  thorpej 	case 'B': case 'b':	return(radix > 11 ? 0xb : -1);
    390  1.1  thorpej 	case 'C': case 'c':	return(radix > 12 ? 0xc : -1);
    391  1.1  thorpej 	case 'D': case 'd':	return(radix > 13 ? 0xd : -1);
    392  1.1  thorpej 	case 'E': case 'e':	return(radix > 14 ? 0xe : -1);
    393  1.1  thorpej 	case 'F': case 'f':	return(radix > 15 ? 0xf : -1);
    394  1.1  thorpej 	}
    395  1.1  thorpej 	/*
    396  1.1  thorpej 	 * invalid character
    397  1.1  thorpej 	 */
    398  1.1  thorpej 	return(-1);
    399  1.1  thorpej }
    400  1.1  thorpej 
    401  1.1  thorpej /*
    402  1.1  thorpej  * convert the key to a bit pattern
    403  1.1  thorpej  */
    404  1.2  thorpej void
    405  1.2  thorpej cvtkey(char *obuf, char *ibuf)
    406  1.1  thorpej {
    407  1.1  thorpej 	register int i, j;		/* counter in a for loop */
    408  1.1  thorpej 	int nbuf[64];			/* used for hex/key translation */
    409  1.1  thorpej 
    410  1.1  thorpej 	/*
    411  1.1  thorpej 	 * just switch on the key base
    412  1.1  thorpej 	 */
    413  1.1  thorpej 	switch(keybase) {
    414  1.1  thorpej 	case KEY_ASCII:			/* ascii to integer */
    415  1.1  thorpej 		(void)strncpy(obuf, ibuf, 8);
    416  1.1  thorpej 		return;
    417  1.1  thorpej 	case KEY_DEFAULT:		/* tell from context */
    418  1.1  thorpej 		/*
    419  1.1  thorpej 		 * leading '0x' or '0X' == hex key
    420  1.1  thorpej 		 */
    421  1.1  thorpej 		if (ibuf[0] == '0' && (ibuf[1] == 'x' || ibuf[1] == 'X')) {
    422  1.1  thorpej 			ibuf = &ibuf[2];
    423  1.1  thorpej 			/*
    424  1.1  thorpej 			 * now translate it, bombing on any illegal hex digit
    425  1.1  thorpej 			 */
    426  1.1  thorpej 			for (i = 0; ibuf[i] && i < 16; i++)
    427  1.1  thorpej 				if ((nbuf[i] = tobinhex(ibuf[i], 16)) == -1)
    428  1.2  thorpej 					bdes_err(-1, "bad hex digit in key");
    429  1.1  thorpej 			while (i < 16)
    430  1.1  thorpej 				nbuf[i++] = 0;
    431  1.1  thorpej 			for (i = 0; i < 8; i++)
    432  1.1  thorpej 				obuf[i] =
    433  1.1  thorpej 				    ((nbuf[2*i]&0xf)<<4) | (nbuf[2*i+1]&0xf);
    434  1.1  thorpej 			/* preserve parity bits */
    435  1.1  thorpej 			pflag = 1;
    436  1.1  thorpej 			return;
    437  1.1  thorpej 		}
    438  1.1  thorpej 		/*
    439  1.1  thorpej 		 * leading '0b' or '0B' == binary key
    440  1.1  thorpej 		 */
    441  1.1  thorpej 		if (ibuf[0] == '0' && (ibuf[1] == 'b' || ibuf[1] == 'B')) {
    442  1.1  thorpej 			ibuf = &ibuf[2];
    443  1.1  thorpej 			/*
    444  1.1  thorpej 			 * now translate it, bombing on any illegal binary digit
    445  1.1  thorpej 			 */
    446  1.1  thorpej 			for (i = 0; ibuf[i] && i < 16; i++)
    447  1.1  thorpej 				if ((nbuf[i] = tobinhex(ibuf[i], 2)) == -1)
    448  1.2  thorpej 					bdes_err(-1, "bad binary digit in key");
    449  1.1  thorpej 			while (i < 64)
    450  1.1  thorpej 				nbuf[i++] = 0;
    451  1.1  thorpej 			for (i = 0; i < 8; i++)
    452  1.1  thorpej 				for (j = 0; j < 8; j++)
    453  1.1  thorpej 					obuf[i] = (obuf[i]<<1)|nbuf[8*i+j];
    454  1.1  thorpej 			/* preserve parity bits */
    455  1.1  thorpej 			pflag = 1;
    456  1.1  thorpej 			return;
    457  1.1  thorpej 		}
    458  1.1  thorpej 		/*
    459  1.1  thorpej 		 * no special leader -- ASCII
    460  1.1  thorpej 		 */
    461  1.1  thorpej 		(void)strncpy(obuf, ibuf, 8);
    462  1.1  thorpej 	}
    463  1.1  thorpej }
    464  1.1  thorpej 
    465  1.1  thorpej /*
    466  1.1  thorpej  * convert an ASCII string into a decimal number:
    467  1.1  thorpej  * 1. must be between 0 and 64 inclusive
    468  1.1  thorpej  * 2. must be a valid decimal number
    469  1.1  thorpej  * 3. must be a multiple of mult
    470  1.1  thorpej  */
    471  1.2  thorpej int
    472  1.2  thorpej setbits(char *s, int mult)
    473  1.1  thorpej {
    474  1.1  thorpej 	register char *p;		/* pointer in a for loop */
    475  1.1  thorpej 	register int n = 0;		/* the integer collected */
    476  1.1  thorpej 
    477  1.1  thorpej 	/*
    478  1.1  thorpej 	 * skip white space
    479  1.1  thorpej 	 */
    480  1.1  thorpej 	while (isspace(*s))
    481  1.1  thorpej 		s++;
    482  1.1  thorpej 	/*
    483  1.1  thorpej 	 * get the integer
    484  1.1  thorpej 	 */
    485  1.1  thorpej 	for (p = s; *p; p++) {
    486  1.1  thorpej 		if (isdigit(*p))
    487  1.1  thorpej 			n = n * 10 + *p - '0';
    488  1.1  thorpej 		else {
    489  1.2  thorpej 			bdes_err(-1, "bad decimal digit in MAC length");
    490  1.1  thorpej 		}
    491  1.1  thorpej 	}
    492  1.1  thorpej 	/*
    493  1.1  thorpej 	 * be sure it's a multiple of mult
    494  1.1  thorpej 	 */
    495  1.1  thorpej 	return((n % mult != 0) ? -1 : n);
    496  1.1  thorpej }
    497  1.1  thorpej 
    498  1.1  thorpej /*****************
    499  1.1  thorpej  * DES FUNCTIONS *
    500  1.1  thorpej  *****************/
    501  1.1  thorpej /*
    502  1.1  thorpej  * This sets the DES key and (if you're using the deszip version)
    503  1.1  thorpej  * the direction of the transformation.  This uses the Sun
    504  1.1  thorpej  * to map the 64-bit key onto the 56 bits that the key schedule
    505  1.1  thorpej  * generation routines use: the old way, which just uses the user-
    506  1.1  thorpej  * supplied 64 bits as is, and the new way, which resets the parity
    507  1.1  thorpej  * bit to be the same as the low-order bit in each character.  The
    508  1.1  thorpej  * new way generates a greater variety of key schedules, since many
    509  1.1  thorpej  * systems set the parity (high) bit of each character to 0, and the
    510  1.1  thorpej  * DES ignores the low order bit of each character.
    511  1.1  thorpej  */
    512  1.2  thorpej void
    513  1.2  thorpej makekey(Desbuf buf)
    514  1.1  thorpej {
    515  1.1  thorpej 	register int i, j;			/* counter in a for loop */
    516  1.1  thorpej 	register int par;			/* parity counter */
    517  1.1  thorpej 
    518  1.1  thorpej 	/*
    519  1.1  thorpej 	 * if the parity is not preserved, flip it
    520  1.1  thorpej 	 */
    521  1.1  thorpej 	if (!pflag) {
    522  1.1  thorpej 		for (i = 0; i < 8; i++) {
    523  1.1  thorpej 			par = 0;
    524  1.1  thorpej 			for (j = 1; j < 8; j++)
    525  1.1  thorpej 				if ((bits[j]&UCHAR(buf, i)) != 0)
    526  1.1  thorpej 					par++;
    527  1.1  thorpej 			if ((par&01) == 01)
    528  1.1  thorpej 				UCHAR(buf, i) = UCHAR(buf, i)&0177;
    529  1.1  thorpej 			else
    530  1.1  thorpej 				UCHAR(buf, i) = (UCHAR(buf, i)&0177)|0200;
    531  1.1  thorpej 		}
    532  1.1  thorpej 	}
    533  1.1  thorpej 
    534  1.1  thorpej 	DES_KEY(UBUFFER(buf));
    535  1.1  thorpej }
    536  1.1  thorpej 
    537  1.1  thorpej /*
    538  1.1  thorpej  * This encrypts using the Electronic Code Book mode of DES
    539  1.1  thorpej  */
    540  1.2  thorpej void
    541  1.2  thorpej ecbenc(void)
    542  1.1  thorpej {
    543  1.1  thorpej 	register int n;		/* number of bytes actually read */
    544  1.1  thorpej 	register int bn;	/* block number */
    545  1.1  thorpej 	Desbuf msgbuf;		/* I/O buffer */
    546  1.1  thorpej 
    547  1.1  thorpej 	for (bn = 0; (n = READ(BUFFER(msgbuf),  8)) == 8; bn++) {
    548  1.1  thorpej 		/*
    549  1.1  thorpej 		 * do the transformation
    550  1.1  thorpej 		 */
    551  1.1  thorpej 		DES_XFORM(UBUFFER(msgbuf));
    552  1.1  thorpej 		WRITE(BUFFER(msgbuf), 8);
    553  1.1  thorpej 	}
    554  1.1  thorpej 	/*
    555  1.1  thorpej 	 * at EOF or last block -- in either ase, the last byte contains
    556  1.1  thorpej 	 * the character representation of the number of bytes in it
    557  1.1  thorpej 	 */
    558  1.1  thorpej 	bn++;
    559  1.1  thorpej 	MEMZERO(&CHAR(msgbuf, n), 8 - n);
    560  1.1  thorpej 	CHAR(msgbuf, 7) = n;
    561  1.1  thorpej 	DES_XFORM(UBUFFER(msgbuf));
    562  1.1  thorpej 	WRITE(BUFFER(msgbuf), 8);
    563  1.1  thorpej 
    564  1.1  thorpej }
    565  1.1  thorpej 
    566  1.1  thorpej /*
    567  1.1  thorpej  * This decrypts using the Electronic Code Book mode of DES
    568  1.1  thorpej  */
    569  1.2  thorpej void
    570  1.2  thorpej ecbdec(void)
    571  1.1  thorpej {
    572  1.1  thorpej 	register int n;		/* number of bytes actually read */
    573  1.1  thorpej 	register int c;		/* used to test for EOF */
    574  1.1  thorpej 	register int bn;	/* block number */
    575  1.1  thorpej 	Desbuf msgbuf;		/* I/O buffer */
    576  1.1  thorpej 
    577  1.1  thorpej 	for (bn = 1; (n = READ(BUFFER(msgbuf), 8)) == 8; bn++) {
    578  1.1  thorpej 		/*
    579  1.1  thorpej 		 * do the transformation
    580  1.1  thorpej 		 */
    581  1.1  thorpej 		DES_XFORM(UBUFFER(msgbuf));
    582  1.1  thorpej 		/*
    583  1.1  thorpej 		 * if the last one, handle it specially
    584  1.1  thorpej 		 */
    585  1.1  thorpej 		if ((c = getchar()) == EOF) {
    586  1.1  thorpej 			n = CHAR(msgbuf, 7);
    587  1.1  thorpej 			if (n < 0 || n > 7)
    588  1.2  thorpej 				bdes_err(bn,
    589  1.2  thorpej 				    "decryption failed (block corrupted)");
    590  1.1  thorpej 		}
    591  1.1  thorpej 		else
    592  1.1  thorpej 			(void)ungetc(c, stdin);
    593  1.1  thorpej 		WRITE(BUFFER(msgbuf), n);
    594  1.1  thorpej 	}
    595  1.1  thorpej 	if (n > 0)
    596  1.2  thorpej 		bdes_err(bn, "decryption failed (incomplete block)");
    597  1.1  thorpej }
    598  1.1  thorpej 
    599  1.1  thorpej /*
    600  1.1  thorpej  * This encrypts using the Cipher Block Chaining mode of DES
    601  1.1  thorpej  */
    602  1.2  thorpej void
    603  1.2  thorpej cbcenc(void)
    604  1.1  thorpej {
    605  1.1  thorpej 	register int n;		/* number of bytes actually read */
    606  1.1  thorpej 	register int bn;	/* block number */
    607  1.1  thorpej 	Desbuf msgbuf;		/* I/O buffer */
    608  1.1  thorpej 
    609  1.1  thorpej 	/*
    610  1.1  thorpej 	 * do the transformation
    611  1.1  thorpej 	 */
    612  1.1  thorpej 	for (bn = 1; (n = READ(BUFFER(msgbuf), 8)) == 8; bn++) {
    613  1.1  thorpej 		for (n = 0; n < 8; n++)
    614  1.1  thorpej 			CHAR(msgbuf, n) ^= CHAR(ivec, n);
    615  1.1  thorpej 		DES_XFORM(UBUFFER(msgbuf));
    616  1.1  thorpej 		MEMCPY(BUFFER(ivec), BUFFER(msgbuf), 8);
    617  1.1  thorpej 		WRITE(BUFFER(msgbuf), 8);
    618  1.1  thorpej 	}
    619  1.1  thorpej 	/*
    620  1.1  thorpej 	 * at EOF or last block -- in either case, the last byte contains
    621  1.1  thorpej 	 * the character representation of the number of bytes in it
    622  1.1  thorpej 	 */
    623  1.1  thorpej 	bn++;
    624  1.1  thorpej 	MEMZERO(&CHAR(msgbuf, n), 8 - n);
    625  1.1  thorpej 	CHAR(msgbuf, 7) = n;
    626  1.1  thorpej 	for (n = 0; n < 8; n++)
    627  1.1  thorpej 		CHAR(msgbuf, n) ^= CHAR(ivec, n);
    628  1.1  thorpej 	DES_XFORM(UBUFFER(msgbuf));
    629  1.1  thorpej 	WRITE(BUFFER(msgbuf), 8);
    630  1.1  thorpej 
    631  1.1  thorpej }
    632  1.1  thorpej 
    633  1.1  thorpej /*
    634  1.1  thorpej  * This decrypts using the Cipher Block Chaining mode of DES
    635  1.1  thorpej  */
    636  1.2  thorpej void
    637  1.2  thorpej cbcdec(void)
    638  1.1  thorpej {
    639  1.1  thorpej 	register int n;		/* number of bytes actually read */
    640  1.1  thorpej 	Desbuf msgbuf;		/* I/O buffer */
    641  1.1  thorpej 	Desbuf ibuf;		/* temp buffer for initialization vector */
    642  1.1  thorpej 	register int c;		/* used to test for EOF */
    643  1.1  thorpej 	register int bn;	/* block number */
    644  1.1  thorpej 
    645  1.1  thorpej 	for (bn = 0; (n = READ(BUFFER(msgbuf), 8)) == 8; bn++) {
    646  1.1  thorpej 		/*
    647  1.1  thorpej 		 * do the transformation
    648  1.1  thorpej 		 */
    649  1.1  thorpej 		MEMCPY(BUFFER(ibuf), BUFFER(msgbuf), 8);
    650  1.1  thorpej 		DES_XFORM(UBUFFER(msgbuf));
    651  1.1  thorpej 		for (c = 0; c < 8; c++)
    652  1.1  thorpej 			UCHAR(msgbuf, c) ^= UCHAR(ivec, c);
    653  1.1  thorpej 		MEMCPY(BUFFER(ivec), BUFFER(ibuf), 8);
    654  1.1  thorpej 		/*
    655  1.1  thorpej 		 * if the last one, handle it specially
    656  1.1  thorpej 		 */
    657  1.1  thorpej 		if ((c = getchar()) == EOF) {
    658  1.1  thorpej 			n = CHAR(msgbuf, 7);
    659  1.1  thorpej 			if (n < 0 || n > 7)
    660  1.2  thorpej 				bdes_err(bn,
    661  1.2  thorpej 				    "decryption failed (block corrupted)");
    662  1.1  thorpej 		}
    663  1.1  thorpej 		else
    664  1.1  thorpej 			(void)ungetc(c, stdin);
    665  1.1  thorpej 		WRITE(BUFFER(msgbuf), n);
    666  1.1  thorpej 	}
    667  1.1  thorpej 	if (n > 0)
    668  1.2  thorpej 		bdes_err(bn, "decryption failed (incomplete block)");
    669  1.1  thorpej }
    670  1.1  thorpej 
    671  1.1  thorpej /*
    672  1.1  thorpej  * This authenticates using the Cipher Block Chaining mode of DES
    673  1.1  thorpej  */
    674  1.2  thorpej void
    675  1.2  thorpej cbcauth(void)
    676  1.1  thorpej {
    677  1.1  thorpej 	register int n, j;		/* number of bytes actually read */
    678  1.1  thorpej 	Desbuf msgbuf;		/* I/O buffer */
    679  1.1  thorpej 	Desbuf encbuf;		/* encryption buffer */
    680  1.1  thorpej 
    681  1.1  thorpej 	/*
    682  1.1  thorpej 	 * do the transformation
    683  1.1  thorpej 	 * note we DISCARD the encrypted block;
    684  1.1  thorpej 	 * we only care about the last one
    685  1.1  thorpej 	 */
    686  1.1  thorpej 	while ((n = READ(BUFFER(msgbuf), 8)) == 8) {
    687  1.1  thorpej 		for (n = 0; n < 8; n++)
    688  1.1  thorpej 			CHAR(encbuf, n) = CHAR(msgbuf, n) ^ CHAR(ivec, n);
    689  1.1  thorpej 		DES_XFORM(UBUFFER(encbuf));
    690  1.1  thorpej 		MEMCPY(BUFFER(ivec), BUFFER(encbuf), 8);
    691  1.1  thorpej 	}
    692  1.1  thorpej 	/*
    693  1.1  thorpej 	 * now compute the last one, right padding with '\0' if need be
    694  1.1  thorpej 	 */
    695  1.1  thorpej 	if (n > 0) {
    696  1.1  thorpej 		MEMZERO(&CHAR(msgbuf, n), 8 - n);
    697  1.1  thorpej 		for (n = 0; n < 8; n++)
    698  1.1  thorpej 			CHAR(encbuf, n) = CHAR(msgbuf, n) ^ CHAR(ivec, n);
    699  1.1  thorpej 		DES_XFORM(UBUFFER(encbuf));
    700  1.1  thorpej 	}
    701  1.1  thorpej 	/*
    702  1.1  thorpej 	 * drop the bits
    703  1.1  thorpej 	 * we write chars until fewer than 7 bits,
    704  1.1  thorpej 	 * and then pad the last one with 0 bits
    705  1.1  thorpej 	 */
    706  1.1  thorpej 	for (n = 0; macbits > 7; n++, macbits -= 8)
    707  1.1  thorpej 		(void)putchar(CHAR(encbuf, n));
    708  1.1  thorpej 	if (macbits > 0) {
    709  1.1  thorpej 		CHAR(msgbuf, 0) = 0x00;
    710  1.1  thorpej 		for (j = 0; j < macbits; j++)
    711  1.1  thorpej 			CHAR(msgbuf, 0) |= (CHAR(encbuf, n)&bits[j]);
    712  1.1  thorpej 		(void)putchar(CHAR(msgbuf, 0));
    713  1.1  thorpej 	}
    714  1.1  thorpej }
    715  1.1  thorpej 
    716  1.1  thorpej /*
    717  1.1  thorpej  * This encrypts using the Cipher FeedBack mode of DES
    718  1.1  thorpej  */
    719  1.2  thorpej void
    720  1.2  thorpej cfbenc(void)
    721  1.1  thorpej {
    722  1.1  thorpej 	register int n;		/* number of bytes actually read */
    723  1.1  thorpej 	register int nbytes;	/* number of bytes to read */
    724  1.1  thorpej 	register int bn;	/* block number */
    725  1.1  thorpej 	char ibuf[8];		/* input buffer */
    726  1.1  thorpej 	Desbuf msgbuf;		/* encryption buffer */
    727  1.1  thorpej 
    728  1.1  thorpej 	/*
    729  1.1  thorpej 	 * do things in bytes, not bits
    730  1.1  thorpej 	 */
    731  1.1  thorpej 	nbytes = fbbits / 8;
    732  1.1  thorpej 	/*
    733  1.1  thorpej 	 * do the transformation
    734  1.1  thorpej 	 */
    735  1.1  thorpej 	for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
    736  1.1  thorpej 		MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
    737  1.1  thorpej 		DES_XFORM(UBUFFER(msgbuf));
    738  1.1  thorpej 		for (n = 0; n < 8 - nbytes; n++)
    739  1.1  thorpej 			UCHAR(ivec, n) = UCHAR(ivec, n+nbytes);
    740  1.1  thorpej 		for (n = 0; n < nbytes; n++)
    741  1.1  thorpej 			UCHAR(ivec, 8-nbytes+n) = ibuf[n] ^ UCHAR(msgbuf, n);
    742  1.1  thorpej 		WRITE(&CHAR(ivec, 8-nbytes), nbytes);
    743  1.1  thorpej 	}
    744  1.1  thorpej 	/*
    745  1.1  thorpej 	 * at EOF or last block -- in either case, the last byte contains
    746  1.1  thorpej 	 * the character representation of the number of bytes in it
    747  1.1  thorpej 	 */
    748  1.1  thorpej 	bn++;
    749  1.1  thorpej 	MEMZERO(&ibuf[n], nbytes - n);
    750  1.1  thorpej 	ibuf[nbytes - 1] = n;
    751  1.1  thorpej 	MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
    752  1.1  thorpej 	DES_XFORM(UBUFFER(msgbuf));
    753  1.1  thorpej 	for (n = 0; n < nbytes; n++)
    754  1.1  thorpej 		ibuf[n] ^= UCHAR(msgbuf, n);
    755  1.1  thorpej 	WRITE(ibuf, nbytes);
    756  1.1  thorpej }
    757  1.1  thorpej 
    758  1.1  thorpej /*
    759  1.1  thorpej  * This decrypts using the Cipher Block Chaining mode of DES
    760  1.1  thorpej  */
    761  1.2  thorpej void
    762  1.2  thorpej cfbdec(void)
    763  1.1  thorpej {
    764  1.1  thorpej 	register int n;		/* number of bytes actually read */
    765  1.1  thorpej 	register int c;		/* used to test for EOF */
    766  1.1  thorpej 	register int nbytes;	/* number of bytes to read */
    767  1.1  thorpej 	register int bn;	/* block number */
    768  1.1  thorpej 	char ibuf[8];		/* input buffer */
    769  1.1  thorpej 	char obuf[8];		/* output buffer */
    770  1.1  thorpej 	Desbuf msgbuf;		/* encryption buffer */
    771  1.1  thorpej 
    772  1.1  thorpej 	/*
    773  1.1  thorpej 	 * do things in bytes, not bits
    774  1.1  thorpej 	 */
    775  1.1  thorpej 	nbytes = fbbits / 8;
    776  1.1  thorpej 	/*
    777  1.1  thorpej 	 * do the transformation
    778  1.1  thorpej 	 */
    779  1.1  thorpej 	for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
    780  1.1  thorpej 		MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
    781  1.1  thorpej 		DES_XFORM(UBUFFER(msgbuf));
    782  1.1  thorpej 		for (c = 0; c < 8 - nbytes; c++)
    783  1.1  thorpej 			CHAR(ivec, c) = CHAR(ivec, c+nbytes);
    784  1.1  thorpej 		for (c = 0; c < nbytes; c++) {
    785  1.1  thorpej 			CHAR(ivec, 8-nbytes+c) = ibuf[c];
    786  1.1  thorpej 			obuf[c] = ibuf[c] ^ UCHAR(msgbuf, c);
    787  1.1  thorpej 		}
    788  1.1  thorpej 		/*
    789  1.1  thorpej 		 * if the last one, handle it specially
    790  1.1  thorpej 		 */
    791  1.1  thorpej 		if ((c = getchar()) == EOF) {
    792  1.1  thorpej 			n = obuf[nbytes-1];
    793  1.1  thorpej 			if (n < 0 || n > nbytes-1)
    794  1.2  thorpej 				bdes_err(bn,
    795  1.2  thorpej 				    "decryption failed (block corrupted)");
    796  1.1  thorpej 		}
    797  1.1  thorpej 		else
    798  1.1  thorpej 			(void)ungetc(c, stdin);
    799  1.1  thorpej 		WRITE(obuf, n);
    800  1.1  thorpej 	}
    801  1.1  thorpej 	if (n > 0)
    802  1.2  thorpej 		bdes_err(bn, "decryption failed (incomplete block)");
    803  1.1  thorpej }
    804  1.1  thorpej 
    805  1.1  thorpej /*
    806  1.1  thorpej  * This encrypts using the alternative Cipher FeedBack mode of DES
    807  1.1  thorpej  */
    808  1.2  thorpej void
    809  1.2  thorpej cfbaenc(void)
    810  1.1  thorpej {
    811  1.1  thorpej 	register int n;		/* number of bytes actually read */
    812  1.1  thorpej 	register int nbytes;	/* number of bytes to read */
    813  1.1  thorpej 	register int bn;	/* block number */
    814  1.1  thorpej 	char ibuf[8];		/* input buffer */
    815  1.1  thorpej 	char obuf[8];		/* output buffer */
    816  1.1  thorpej 	Desbuf msgbuf;		/* encryption buffer */
    817  1.1  thorpej 
    818  1.1  thorpej 	/*
    819  1.1  thorpej 	 * do things in bytes, not bits
    820  1.1  thorpej 	 */
    821  1.1  thorpej 	nbytes = fbbits / 7;
    822  1.1  thorpej 	/*
    823  1.1  thorpej 	 * do the transformation
    824  1.1  thorpej 	 */
    825  1.1  thorpej 	for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
    826  1.1  thorpej 		MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
    827  1.1  thorpej 		DES_XFORM(UBUFFER(msgbuf));
    828  1.1  thorpej 		for (n = 0; n < 8 - nbytes; n++)
    829  1.1  thorpej 			UCHAR(ivec, n) = UCHAR(ivec, n+nbytes);
    830  1.1  thorpej 		for (n = 0; n < nbytes; n++)
    831  1.1  thorpej 			UCHAR(ivec, 8-nbytes+n) = (ibuf[n] ^ UCHAR(msgbuf, n))
    832  1.1  thorpej 							|0200;
    833  1.1  thorpej 		for (n = 0; n < nbytes; n++)
    834  1.1  thorpej 			obuf[n] = CHAR(ivec, 8-nbytes+n)&0177;
    835  1.1  thorpej 		WRITE(obuf, nbytes);
    836  1.1  thorpej 	}
    837  1.1  thorpej 	/*
    838  1.1  thorpej 	 * at EOF or last block -- in either case, the last byte contains
    839  1.1  thorpej 	 * the character representation of the number of bytes in it
    840  1.1  thorpej 	 */
    841  1.1  thorpej 	bn++;
    842  1.1  thorpej 	MEMZERO(&ibuf[n], nbytes - n);
    843  1.1  thorpej 	ibuf[nbytes - 1] = ('0' + n)|0200;
    844  1.1  thorpej 	MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
    845  1.1  thorpej 	DES_XFORM(UBUFFER(msgbuf));
    846  1.1  thorpej 	for (n = 0; n < nbytes; n++)
    847  1.1  thorpej 		ibuf[n] ^= UCHAR(msgbuf, n);
    848  1.1  thorpej 	WRITE(ibuf, nbytes);
    849  1.1  thorpej }
    850  1.1  thorpej 
    851  1.1  thorpej /*
    852  1.1  thorpej  * This decrypts using the alternative Cipher Block Chaining mode of DES
    853  1.1  thorpej  */
    854  1.2  thorpej void
    855  1.2  thorpej cfbadec(void)
    856  1.1  thorpej {
    857  1.1  thorpej 	register int n;		/* number of bytes actually read */
    858  1.1  thorpej 	register int c;		/* used to test for EOF */
    859  1.1  thorpej 	register int nbytes;	/* number of bytes to read */
    860  1.1  thorpej 	register int bn;	/* block number */
    861  1.1  thorpej 	char ibuf[8];		/* input buffer */
    862  1.1  thorpej 	char obuf[8];		/* output buffer */
    863  1.1  thorpej 	Desbuf msgbuf;		/* encryption buffer */
    864  1.1  thorpej 
    865  1.1  thorpej 	/*
    866  1.1  thorpej 	 * do things in bytes, not bits
    867  1.1  thorpej 	 */
    868  1.1  thorpej 	nbytes = fbbits / 7;
    869  1.1  thorpej 	/*
    870  1.1  thorpej 	 * do the transformation
    871  1.1  thorpej 	 */
    872  1.1  thorpej 	for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
    873  1.1  thorpej 		MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
    874  1.1  thorpej 		DES_XFORM(UBUFFER(msgbuf));
    875  1.1  thorpej 		for (c = 0; c < 8 - nbytes; c++)
    876  1.1  thorpej 			CHAR(ivec, c) = CHAR(ivec, c+nbytes);
    877  1.1  thorpej 		for (c = 0; c < nbytes; c++) {
    878  1.1  thorpej 			CHAR(ivec, 8-nbytes+c) = ibuf[c]|0200;
    879  1.1  thorpej 			obuf[c] = (ibuf[c] ^ UCHAR(msgbuf, c))&0177;
    880  1.1  thorpej 		}
    881  1.1  thorpej 		/*
    882  1.1  thorpej 		 * if the last one, handle it specially
    883  1.1  thorpej 		 */
    884  1.1  thorpej 		if ((c = getchar()) == EOF) {
    885  1.1  thorpej 			if ((n = (obuf[nbytes-1] - '0')) < 0
    886  1.1  thorpej 						|| n > nbytes-1)
    887  1.2  thorpej 				bdes_err(bn,
    888  1.2  thorpej 				    "decryption failed (block corrupted)");
    889  1.1  thorpej 		}
    890  1.1  thorpej 		else
    891  1.1  thorpej 			(void)ungetc(c, stdin);
    892  1.1  thorpej 		WRITE(obuf, n);
    893  1.1  thorpej 	}
    894  1.1  thorpej 	if (n > 0)
    895  1.2  thorpej 		bdes_err(bn, "decryption failed (incomplete block)");
    896  1.1  thorpej }
    897  1.1  thorpej 
    898  1.1  thorpej 
    899  1.1  thorpej /*
    900  1.1  thorpej  * This encrypts using the Output FeedBack mode of DES
    901  1.1  thorpej  */
    902  1.2  thorpej void
    903  1.2  thorpej ofbenc(void)
    904  1.1  thorpej {
    905  1.1  thorpej 	register int n;		/* number of bytes actually read */
    906  1.1  thorpej 	register int c;		/* used to test for EOF */
    907  1.1  thorpej 	register int nbytes;	/* number of bytes to read */
    908  1.1  thorpej 	register int bn;	/* block number */
    909  1.1  thorpej 	char ibuf[8];		/* input buffer */
    910  1.1  thorpej 	char obuf[8];		/* output buffer */
    911  1.1  thorpej 	Desbuf msgbuf;		/* encryption buffer */
    912  1.1  thorpej 
    913  1.1  thorpej 	/*
    914  1.1  thorpej 	 * do things in bytes, not bits
    915  1.1  thorpej 	 */
    916  1.1  thorpej 	nbytes = fbbits / 8;
    917  1.1  thorpej 	/*
    918  1.1  thorpej 	 * do the transformation
    919  1.1  thorpej 	 */
    920  1.1  thorpej 	for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
    921  1.1  thorpej 		MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
    922  1.1  thorpej 		DES_XFORM(UBUFFER(msgbuf));
    923  1.1  thorpej 		for (n = 0; n < 8 - nbytes; n++)
    924  1.1  thorpej 			UCHAR(ivec, n) = UCHAR(ivec, n+nbytes);
    925  1.1  thorpej 		for (n = 0; n < nbytes; n++) {
    926  1.1  thorpej 			UCHAR(ivec, 8-nbytes+n) = UCHAR(msgbuf, n);
    927  1.1  thorpej 			obuf[n] = ibuf[n] ^ UCHAR(msgbuf, n);
    928  1.1  thorpej 		}
    929  1.1  thorpej 		WRITE(obuf, nbytes);
    930  1.1  thorpej 	}
    931  1.1  thorpej 	/*
    932  1.1  thorpej 	 * at EOF or last block -- in either case, the last byte contains
    933  1.1  thorpej 	 * the character representation of the number of bytes in it
    934  1.1  thorpej 	 */
    935  1.1  thorpej 	bn++;
    936  1.1  thorpej 	MEMZERO(&ibuf[n], nbytes - n);
    937  1.1  thorpej 	ibuf[nbytes - 1] = n;
    938  1.1  thorpej 	MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
    939  1.1  thorpej 	DES_XFORM(UBUFFER(msgbuf));
    940  1.1  thorpej 	for (c = 0; c < nbytes; c++)
    941  1.1  thorpej 		ibuf[c] ^= UCHAR(msgbuf, c);
    942  1.1  thorpej 	WRITE(ibuf, nbytes);
    943  1.1  thorpej }
    944  1.1  thorpej 
    945  1.1  thorpej /*
    946  1.1  thorpej  * This decrypts using the Output Block Chaining mode of DES
    947  1.1  thorpej  */
    948  1.2  thorpej void
    949  1.2  thorpej ofbdec(void)
    950  1.1  thorpej {
    951  1.1  thorpej 	register int n;		/* number of bytes actually read */
    952  1.1  thorpej 	register int c;		/* used to test for EOF */
    953  1.1  thorpej 	register int nbytes;	/* number of bytes to read */
    954  1.1  thorpej 	register int bn;	/* block number */
    955  1.1  thorpej 	char ibuf[8];		/* input buffer */
    956  1.1  thorpej 	char obuf[8];		/* output buffer */
    957  1.1  thorpej 	Desbuf msgbuf;		/* encryption buffer */
    958  1.1  thorpej 
    959  1.1  thorpej 	/*
    960  1.1  thorpej 	 * do things in bytes, not bits
    961  1.1  thorpej 	 */
    962  1.1  thorpej 	nbytes = fbbits / 8;
    963  1.1  thorpej 	/*
    964  1.1  thorpej 	 * do the transformation
    965  1.1  thorpej 	 */
    966  1.1  thorpej 	for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
    967  1.1  thorpej 		MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
    968  1.1  thorpej 		DES_XFORM(UBUFFER(msgbuf));
    969  1.1  thorpej 		for (c = 0; c < 8 - nbytes; c++)
    970  1.1  thorpej 			CHAR(ivec, c) = CHAR(ivec, c+nbytes);
    971  1.1  thorpej 		for (c = 0; c < nbytes; c++) {
    972  1.1  thorpej 			CHAR(ivec, 8-nbytes+c) = UCHAR(msgbuf, c);
    973  1.1  thorpej 			obuf[c] = ibuf[c] ^ UCHAR(msgbuf, c);
    974  1.1  thorpej 		}
    975  1.1  thorpej 		/*
    976  1.1  thorpej 		 * if the last one, handle it specially
    977  1.1  thorpej 		 */
    978  1.1  thorpej 		if ((c = getchar()) == EOF) {
    979  1.1  thorpej 			n = obuf[nbytes-1];
    980  1.1  thorpej 			if (n < 0 || n > nbytes-1)
    981  1.2  thorpej 				bdes_err(bn,
    982  1.2  thorpej 				    "decryption failed (block corrupted)");
    983  1.1  thorpej 		}
    984  1.1  thorpej 		else
    985  1.1  thorpej 			(void)ungetc(c, stdin);
    986  1.1  thorpej 		/*
    987  1.1  thorpej 		 * dump it
    988  1.1  thorpej 		 */
    989  1.1  thorpej 		WRITE(obuf, n);
    990  1.1  thorpej 	}
    991  1.1  thorpej 	if (n > 0)
    992  1.2  thorpej 		bdes_err(bn, "decryption failed (incomplete block)");
    993  1.1  thorpej }
    994  1.1  thorpej 
    995  1.1  thorpej /*
    996  1.1  thorpej  * This authenticates using the Cipher FeedBack mode of DES
    997  1.1  thorpej  */
    998  1.2  thorpej void
    999  1.2  thorpej cfbauth(void)
   1000  1.1  thorpej {
   1001  1.1  thorpej 	register int n, j;	/* number of bytes actually read */
   1002  1.1  thorpej 	register int nbytes;	/* number of bytes to read */
   1003  1.1  thorpej 	char ibuf[8];		/* input buffer */
   1004  1.1  thorpej 	Desbuf msgbuf;		/* encryption buffer */
   1005  1.1  thorpej 
   1006  1.1  thorpej 	/*
   1007  1.1  thorpej 	 * do things in bytes, not bits
   1008  1.1  thorpej 	 */
   1009  1.1  thorpej 	nbytes = fbbits / 8;
   1010  1.1  thorpej 	/*
   1011  1.1  thorpej 	 * do the transformation
   1012  1.1  thorpej 	 */
   1013  1.1  thorpej 	while ((n = READ(ibuf, nbytes)) == nbytes) {
   1014  1.1  thorpej 		MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
   1015  1.1  thorpej 		DES_XFORM(UBUFFER(msgbuf));
   1016  1.1  thorpej 		for (n = 0; n < 8 - nbytes; n++)
   1017  1.1  thorpej 			UCHAR(ivec, n) = UCHAR(ivec, n+nbytes);
   1018  1.1  thorpej 		for (n = 0; n < nbytes; n++)
   1019  1.1  thorpej 			UCHAR(ivec, 8-nbytes+n) = ibuf[n] ^ UCHAR(msgbuf, n);
   1020  1.1  thorpej 	}
   1021  1.1  thorpej 	/*
   1022  1.1  thorpej 	 * at EOF or last block -- in either case, the last byte contains
   1023  1.1  thorpej 	 * the character representation of the number of bytes in it
   1024  1.1  thorpej 	 */
   1025  1.1  thorpej 	MEMZERO(&ibuf[n], nbytes - n);
   1026  1.1  thorpej 	ibuf[nbytes - 1] = '0' + n;
   1027  1.1  thorpej 	MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
   1028  1.1  thorpej 	DES_XFORM(UBUFFER(msgbuf));
   1029  1.1  thorpej 	for (n = 0; n < nbytes; n++)
   1030  1.1  thorpej 		ibuf[n] ^= UCHAR(msgbuf, n);
   1031  1.1  thorpej 	/*
   1032  1.1  thorpej 	 * drop the bits
   1033  1.1  thorpej 	 * we write chars until fewer than 7 bits,
   1034  1.1  thorpej 	 * and then pad the last one with 0 bits
   1035  1.1  thorpej 	 */
   1036  1.1  thorpej 	for (n = 0; macbits > 7; n++, macbits -= 8)
   1037  1.1  thorpej 		(void)putchar(CHAR(msgbuf, n));
   1038  1.1  thorpej 	if (macbits > 0) {
   1039  1.1  thorpej 		CHAR(msgbuf, 0) = 0x00;
   1040  1.1  thorpej 		for (j = 0; j < macbits; j++)
   1041  1.1  thorpej 			CHAR(msgbuf, 0) |= (CHAR(msgbuf, n)&bits[j]);
   1042  1.1  thorpej 		(void)putchar(CHAR(msgbuf, 0));
   1043  1.1  thorpej 	}
   1044  1.1  thorpej }
   1045  1.1  thorpej 
   1046  1.1  thorpej #ifndef FASTWAY
   1047  1.1  thorpej /*
   1048  1.1  thorpej  * change from 8 bits/Uchar to 1 bit/Uchar
   1049  1.1  thorpej  */
   1050  1.2  thorpej void
   1051  1.2  thorpej expand(Desbuf from, char *to)
   1052  1.1  thorpej {
   1053  1.1  thorpej 	register int i, j;		/* counters in for loop */
   1054  1.1  thorpej 
   1055  1.1  thorpej 	for (i = 0; i < 8; i++)
   1056  1.1  thorpej 		for (j = 0; j < 8; j++)
   1057  1.1  thorpej 			*to++ = (CHAR(from, i)>>(7-j))&01;
   1058  1.1  thorpej }
   1059  1.1  thorpej 
   1060  1.1  thorpej /*
   1061  1.1  thorpej  * change from 1 bit/char to 8 bits/Uchar
   1062  1.1  thorpej  */
   1063  1.2  thorpej void
   1064  1.2  thorpej compress(char *from, Desbuf to)
   1065  1.1  thorpej {
   1066  1.1  thorpej 	register int i, j;		/* counters in for loop */
   1067  1.1  thorpej 
   1068  1.1  thorpej 	for (i = 0; i < 8; i++) {
   1069  1.1  thorpej 	 	CHAR(to, i) = 0;
   1070  1.1  thorpej 		for (j = 0; j < 8; j++)
   1071  1.1  thorpej 			CHAR(to, i) = ((*from++)<<(7-j))|CHAR(to, i);
   1072  1.1  thorpej 	}
   1073  1.1  thorpej }
   1074  1.1  thorpej #endif
   1075  1.1  thorpej 
   1076  1.1  thorpej /*
   1077  1.1  thorpej  * message about usage
   1078  1.1  thorpej  */
   1079  1.2  thorpej void
   1080  1.2  thorpej usage(void)
   1081  1.1  thorpej {
   1082  1.2  thorpej 
   1083  1.4      cgd 	(void) fprintf(stderr, "usage: %s %s\n", getprogname(),
   1084  1.2  thorpej 	    "[-abdp] [-F bit] [-f bit] [-k key] [-m bit] [-o bit] [-v vector]");
   1085  1.1  thorpej 	exit(1);
   1086  1.1  thorpej }
   1087