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