xref: /dflybsd-src/usr.bin/bdes/bdes.c (revision 799ba435edf825f35aa8f08bc8353a878dca2116)
10fe46dc6SMatthew Dillon /*-
20fe46dc6SMatthew Dillon  * Copyright (c) 1991, 1993
30fe46dc6SMatthew Dillon  *	The Regents of the University of California.  All rights reserved.
40fe46dc6SMatthew Dillon  *
50fe46dc6SMatthew Dillon  * This code is derived from software contributed to Berkeley by
60fe46dc6SMatthew Dillon  * Matt Bishop of Dartmouth College.
70fe46dc6SMatthew Dillon  *
80fe46dc6SMatthew Dillon  * The United States Government has rights in this work pursuant
90fe46dc6SMatthew Dillon  * to contract no. NAG 2-680 between the National Aeronautics and
100fe46dc6SMatthew Dillon  * Space Administration and Dartmouth College.
110fe46dc6SMatthew Dillon  *
120fe46dc6SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
130fe46dc6SMatthew Dillon  * modification, are permitted provided that the following conditions
140fe46dc6SMatthew Dillon  * are met:
150fe46dc6SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
160fe46dc6SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
170fe46dc6SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
180fe46dc6SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
190fe46dc6SMatthew Dillon  *    documentation and/or other materials provided with the distribution.
200fe46dc6SMatthew Dillon  * 3. All advertising materials mentioning features or use of this software
210fe46dc6SMatthew Dillon  *    must display the following acknowledgement:
220fe46dc6SMatthew Dillon  *	This product includes software developed by the University of
230fe46dc6SMatthew Dillon  *	California, Berkeley and its contributors.
240fe46dc6SMatthew Dillon  * 4. Neither the name of the University nor the names of its contributors
250fe46dc6SMatthew Dillon  *    may be used to endorse or promote products derived from this software
260fe46dc6SMatthew Dillon  *    without specific prior written permission.
270fe46dc6SMatthew Dillon  *
280fe46dc6SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
290fe46dc6SMatthew Dillon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
300fe46dc6SMatthew Dillon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
310fe46dc6SMatthew Dillon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
320fe46dc6SMatthew Dillon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
330fe46dc6SMatthew Dillon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
340fe46dc6SMatthew Dillon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
350fe46dc6SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
360fe46dc6SMatthew Dillon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
370fe46dc6SMatthew Dillon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
380fe46dc6SMatthew Dillon  * SUCH DAMAGE.
390fe46dc6SMatthew Dillon  *
400fe46dc6SMatthew Dillon  * @(#) Copyright (c) 1991, 1993 The Regents of the University of California.  All rights reserved.
410fe46dc6SMatthew Dillon  * @(#)bdes.c	8.1 (Berkeley) 6/6/93
420fe46dc6SMatthew Dillon  * $FreeBSD: src/secure/usr.bin/bdes/bdes.c,v 1.11 2009/11/03 18:40:42 jhb Exp $
430fe46dc6SMatthew Dillon  */
440fe46dc6SMatthew Dillon 
450fe46dc6SMatthew Dillon /*
460fe46dc6SMatthew Dillon  * BDES -- DES encryption package for Berkeley Software Distribution 4.4
470fe46dc6SMatthew Dillon  * options:
480fe46dc6SMatthew Dillon  *	-a	key is in ASCII
490fe46dc6SMatthew Dillon  *	-b	use ECB (electronic code book) mode
500fe46dc6SMatthew Dillon  *	-d	invert (decrypt) input
510fe46dc6SMatthew Dillon  *	-f b	use b-bit CFB (cipher feedback) mode
520fe46dc6SMatthew Dillon  *	-F b	use b-bit CFB (cipher feedback) alternative mode
530fe46dc6SMatthew Dillon  *	-k key	use key as the cryptographic key
540fe46dc6SMatthew Dillon  *	-m b	generate a MAC of length b
550fe46dc6SMatthew Dillon  *	-o b	use b-bit OFB (output feedback) mode
560fe46dc6SMatthew Dillon  *	-p	don't reset the parity bit
570fe46dc6SMatthew Dillon  *	-v v	use v as the initialization vector (ignored for ECB)
580fe46dc6SMatthew Dillon  * note: the last character of the last block is the integer indicating
590fe46dc6SMatthew Dillon  * how many characters of that block are to be output
600fe46dc6SMatthew Dillon  *
610fe46dc6SMatthew Dillon  * Author: Matt Bishop
620fe46dc6SMatthew Dillon  *	   Department of Mathematics and Computer Science
630fe46dc6SMatthew Dillon  *	   Dartmouth College
640fe46dc6SMatthew Dillon  *	   Hanover, NH  03755
650fe46dc6SMatthew Dillon  * Email:  Matt.Bishop@dartmouth.edu
660fe46dc6SMatthew Dillon  *	   ...!decvax!dartvax!Matt.Bishop
670fe46dc6SMatthew Dillon  *
680fe46dc6SMatthew Dillon  * See Technical Report PCS-TR91-158, Department of Mathematics and Computer
690fe46dc6SMatthew Dillon  * Science, Dartmouth College, for a detailed description of the implemen-
700fe46dc6SMatthew Dillon  * tation and differences between it and Sun's.  The DES is described in
710fe46dc6SMatthew Dillon  * FIPS PUB 46, and the modes in FIPS PUB 81 (see either the manual page
720fe46dc6SMatthew Dillon  * or the technical report for a complete reference).
730fe46dc6SMatthew Dillon  */
740fe46dc6SMatthew Dillon 
750fe46dc6SMatthew Dillon #include <sys/types.h>
760fe46dc6SMatthew Dillon 
770fe46dc6SMatthew Dillon #include <ctype.h>
780fe46dc6SMatthew Dillon #include <err.h>
790fe46dc6SMatthew Dillon #include <errno.h>
800fe46dc6SMatthew Dillon #include <stdio.h>
810fe46dc6SMatthew Dillon #include <stdlib.h>
820fe46dc6SMatthew Dillon #include <string.h>
830fe46dc6SMatthew Dillon #include <unistd.h>
840fe46dc6SMatthew Dillon 
850fe46dc6SMatthew Dillon #include <openssl/des.h>
860fe46dc6SMatthew Dillon 
870fe46dc6SMatthew Dillon /*
880fe46dc6SMatthew Dillon  * BSD and System V systems offer special library calls that do
890fe46dc6SMatthew Dillon  * block moves and fills, so if possible we take advantage of them
900fe46dc6SMatthew Dillon  */
910fe46dc6SMatthew Dillon #define	MEMCPY(dest,src,len)	bcopy((src),(dest),(len))
920fe46dc6SMatthew Dillon #define	MEMZERO(dest,len)	bzero((dest),(len))
930fe46dc6SMatthew Dillon 
940fe46dc6SMatthew Dillon #define	DES_XFORM(buf)							\
950fe46dc6SMatthew Dillon 		DES_ecb_encrypt(buf, buf, &schedule, 			\
960fe46dc6SMatthew Dillon 		    mode == MODE_ENCRYPT ? DES_ENCRYPT : DES_DECRYPT);
970fe46dc6SMatthew Dillon 
980fe46dc6SMatthew Dillon /*
990fe46dc6SMatthew Dillon  * this does an error-checking write
1000fe46dc6SMatthew Dillon  */
1010fe46dc6SMatthew Dillon #define	READ(buf, n)	fread(buf, sizeof(char), n, stdin)
1020fe46dc6SMatthew Dillon #define WRITE(buf,n)						\
1030fe46dc6SMatthew Dillon 		if (fwrite(buf, sizeof(char), n, stdout) != n)	\
1040fe46dc6SMatthew Dillon 			warnx("fwrite error at %d", n);
1050fe46dc6SMatthew Dillon 
1060fe46dc6SMatthew Dillon /*
1070fe46dc6SMatthew Dillon  * global variables and related macros
1080fe46dc6SMatthew Dillon  */
1090fe46dc6SMatthew Dillon #define KEY_DEFAULT		0	/* interpret radix of key from key */
1100fe46dc6SMatthew Dillon #define KEY_ASCII		1	/* key is in ASCII characters */
1110fe46dc6SMatthew Dillon int keybase = KEY_DEFAULT;		/* how to interpret the key */
1120fe46dc6SMatthew Dillon 
1130fe46dc6SMatthew Dillon enum { 					/* encrypt, decrypt, authenticate */
1140fe46dc6SMatthew Dillon 	MODE_ENCRYPT, MODE_DECRYPT, MODE_AUTHENTICATE
1150fe46dc6SMatthew Dillon } mode = MODE_ENCRYPT;
1160fe46dc6SMatthew Dillon 
1170fe46dc6SMatthew Dillon enum {					/* ecb, cbc, cfb, cfba, ofb? */
1180fe46dc6SMatthew Dillon 	ALG_ECB, ALG_CBC, ALG_CFB, ALG_OFB, ALG_CFBA
1190fe46dc6SMatthew Dillon } alg = ALG_CBC;
1200fe46dc6SMatthew Dillon 
1210fe46dc6SMatthew Dillon DES_cblock ivec;				/* initialization vector */
1220fe46dc6SMatthew Dillon 
1230fe46dc6SMatthew Dillon char bits[] = {				/* used to extract bits from a char */
1240fe46dc6SMatthew Dillon 	'\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
1250fe46dc6SMatthew Dillon };
1260fe46dc6SMatthew Dillon 
1270fe46dc6SMatthew Dillon int inverse;				/* 0 to encrypt, 1 to decrypt */
1280fe46dc6SMatthew Dillon int macbits = -1;			/* number of bits in authentication */
1290fe46dc6SMatthew Dillon int fbbits = -1;			/* number of feedback bits */
1300fe46dc6SMatthew Dillon int pflag;				/* 1 to preserve parity bits */
1310fe46dc6SMatthew Dillon 
1320fe46dc6SMatthew Dillon DES_key_schedule schedule;		/* expanded DES key */
1330fe46dc6SMatthew Dillon 
1340fe46dc6SMatthew Dillon static void ecbenc(void);
1350fe46dc6SMatthew Dillon static void ecbdec(void);
1360fe46dc6SMatthew Dillon static void cbcenc(void);
1370fe46dc6SMatthew Dillon static void cbcdec(void);
1380fe46dc6SMatthew Dillon static void cfbenc(void);
1390fe46dc6SMatthew Dillon static void cfbdec(void);
1400fe46dc6SMatthew Dillon static void cfbaenc(void);
1410fe46dc6SMatthew Dillon static void cfbadec(void);
1420fe46dc6SMatthew Dillon static void ofbenc(void);
1430fe46dc6SMatthew Dillon static void ofbdec(void);
1440fe46dc6SMatthew Dillon 
1450fe46dc6SMatthew Dillon static void cbcauth(void);
1460fe46dc6SMatthew Dillon static void cfbauth(void);
1470fe46dc6SMatthew Dillon 
1480fe46dc6SMatthew Dillon static void cvtkey(DES_cblock, char *);
1490fe46dc6SMatthew Dillon static int setbits(char *, int);
1500fe46dc6SMatthew Dillon static void makekey(DES_cblock *);
1510fe46dc6SMatthew Dillon static int tobinhex(char, int);
1520fe46dc6SMatthew Dillon 
1530fe46dc6SMatthew Dillon static void usage(void);
1540fe46dc6SMatthew Dillon 
1550fe46dc6SMatthew Dillon int
main(int argc,char * argv[])1560fe46dc6SMatthew Dillon main(int argc, char *argv[])
1570fe46dc6SMatthew Dillon {
1580fe46dc6SMatthew Dillon 	extern char *optarg;		/* argument to option if any */
1590fe46dc6SMatthew Dillon 	int i;				/* counter in a for loop */
1600fe46dc6SMatthew Dillon 	char *p;			/* used to obtain the key */
1610fe46dc6SMatthew Dillon 	DES_cblock msgbuf;		/* I/O buffer */
1620fe46dc6SMatthew Dillon 	int kflag;			/* command-line encryption key */
1630fe46dc6SMatthew Dillon 
1640fe46dc6SMatthew Dillon 	setproctitle("-");		/* Hide command-line arguments */
1650fe46dc6SMatthew Dillon 
1660fe46dc6SMatthew Dillon 	/* initialize the initialization vector */
1670fe46dc6SMatthew Dillon 	MEMZERO(ivec, 8);
1680fe46dc6SMatthew Dillon 
1690fe46dc6SMatthew Dillon 	/* process the argument list */
1700fe46dc6SMatthew Dillon 	kflag = 0;
1710fe46dc6SMatthew Dillon 	while ((i = getopt(argc, argv, "abdF:f:k:m:o:pv:")) != -1)
1720fe46dc6SMatthew Dillon 		switch(i) {
1730fe46dc6SMatthew Dillon 		case 'a':		/* key is ASCII */
1740fe46dc6SMatthew Dillon 			keybase = KEY_ASCII;
1750fe46dc6SMatthew Dillon 			break;
1760fe46dc6SMatthew Dillon 		case 'b':		/* use ECB mode */
1770fe46dc6SMatthew Dillon 			alg = ALG_ECB;
1780fe46dc6SMatthew Dillon 			break;
1790fe46dc6SMatthew Dillon 		case 'd':		/* decrypt */
1800fe46dc6SMatthew Dillon 			mode = MODE_DECRYPT;
1810fe46dc6SMatthew Dillon 			break;
1820fe46dc6SMatthew Dillon 		case 'F':		/* use alternative CFB mode */
1830fe46dc6SMatthew Dillon 			alg = ALG_CFBA;
1840fe46dc6SMatthew Dillon 			if ((fbbits = setbits(optarg, 7)) > 56 || fbbits == 0)
1850fe46dc6SMatthew Dillon 				errx(1, "-F: number must be 1-56 inclusive");
1860fe46dc6SMatthew Dillon 			else if (fbbits == -1)
1870fe46dc6SMatthew Dillon 				errx(1, "-F: number must be a multiple of 7");
1880fe46dc6SMatthew Dillon 			break;
1890fe46dc6SMatthew Dillon 		case 'f':		/* use CFB mode */
1900fe46dc6SMatthew Dillon 			alg = ALG_CFB;
1910fe46dc6SMatthew Dillon 			if ((fbbits = setbits(optarg, 8)) > 64 || fbbits == 0)
1920fe46dc6SMatthew Dillon 				errx(1, "-f: number must be 1-64 inclusive");
1930fe46dc6SMatthew Dillon 			else if (fbbits == -1)
1940fe46dc6SMatthew Dillon 				errx(1, "-f: number must be a multiple of 8");
1950fe46dc6SMatthew Dillon 			break;
1960fe46dc6SMatthew Dillon 		case 'k':		/* encryption key */
1970fe46dc6SMatthew Dillon 			kflag = 1;
1980fe46dc6SMatthew Dillon 			cvtkey(msgbuf, optarg);
1990fe46dc6SMatthew Dillon 			break;
2000fe46dc6SMatthew Dillon 		case 'm':		/* number of bits for MACing */
2010fe46dc6SMatthew Dillon 			mode = MODE_AUTHENTICATE;
2020fe46dc6SMatthew Dillon 			if ((macbits = setbits(optarg, 1)) > 64)
2030fe46dc6SMatthew Dillon 				errx(1, "-m: number must be 0-64 inclusive");
2040fe46dc6SMatthew Dillon 			break;
2050fe46dc6SMatthew Dillon 		case 'o':		/* use OFB mode */
2060fe46dc6SMatthew Dillon 			alg = ALG_OFB;
2070fe46dc6SMatthew Dillon 			if ((fbbits = setbits(optarg, 8)) > 64 || fbbits == 0)
2080fe46dc6SMatthew Dillon 				errx(1, "-o: number must be 1-64 inclusive");
2090fe46dc6SMatthew Dillon 			else if (fbbits == -1)
2100fe46dc6SMatthew Dillon 				errx(1, "-o: number must be a multiple of 8");
2110fe46dc6SMatthew Dillon 			break;
2120fe46dc6SMatthew Dillon 		case 'p':		/* preserve parity bits */
2130fe46dc6SMatthew Dillon 			pflag = 1;
2140fe46dc6SMatthew Dillon 			break;
2150fe46dc6SMatthew Dillon 		case 'v':		/* set initialization vector */
2160fe46dc6SMatthew Dillon 			cvtkey(ivec, optarg);
2170fe46dc6SMatthew Dillon 			break;
2180fe46dc6SMatthew Dillon 		default:		/* error */
2190fe46dc6SMatthew Dillon 			usage();
2200fe46dc6SMatthew Dillon 		}
2210fe46dc6SMatthew Dillon 
2220fe46dc6SMatthew Dillon 	if (!kflag) {
2230fe46dc6SMatthew Dillon 		/*
2240fe46dc6SMatthew Dillon 		 * if the key's not ASCII, assume it is
2250fe46dc6SMatthew Dillon 		 */
2260fe46dc6SMatthew Dillon 		keybase = KEY_ASCII;
2270fe46dc6SMatthew Dillon 		/*
2280fe46dc6SMatthew Dillon 		 * get the key
2290fe46dc6SMatthew Dillon 		 */
2300fe46dc6SMatthew Dillon 		p = getpass("Enter key: ");
2310fe46dc6SMatthew Dillon 		/*
2320fe46dc6SMatthew Dillon 		 * copy it, nul-padded, into the key area
2330fe46dc6SMatthew Dillon 		 */
2340fe46dc6SMatthew Dillon 		cvtkey(msgbuf, p);
2350fe46dc6SMatthew Dillon 	}
2360fe46dc6SMatthew Dillon 
2370fe46dc6SMatthew Dillon 	makekey(&msgbuf);
2380fe46dc6SMatthew Dillon 	inverse = (alg == ALG_CBC || alg == ALG_ECB) && mode == MODE_DECRYPT;
2390fe46dc6SMatthew Dillon 
2400fe46dc6SMatthew Dillon 	switch(alg) {
2410fe46dc6SMatthew Dillon 	case ALG_CBC:
2420fe46dc6SMatthew Dillon 		switch(mode) {
2430fe46dc6SMatthew Dillon 		case MODE_AUTHENTICATE:	/* authenticate using CBC mode */
2440fe46dc6SMatthew Dillon 			cbcauth();
2450fe46dc6SMatthew Dillon 			break;
2460fe46dc6SMatthew Dillon 		case MODE_DECRYPT:	/* decrypt using CBC mode */
2470fe46dc6SMatthew Dillon 			cbcdec();
2480fe46dc6SMatthew Dillon 			break;
2490fe46dc6SMatthew Dillon 		case MODE_ENCRYPT:	/* encrypt using CBC mode */
2500fe46dc6SMatthew Dillon 			cbcenc();
2510fe46dc6SMatthew Dillon 			break;
2520fe46dc6SMatthew Dillon 		}
2530fe46dc6SMatthew Dillon 		break;
2540fe46dc6SMatthew Dillon 	case ALG_CFB:
2550fe46dc6SMatthew Dillon 		switch(mode) {
2560fe46dc6SMatthew Dillon 		case MODE_AUTHENTICATE:	/* authenticate using CFB mode */
2570fe46dc6SMatthew Dillon 			cfbauth();
2580fe46dc6SMatthew Dillon 			break;
2590fe46dc6SMatthew Dillon 		case MODE_DECRYPT:	/* decrypt using CFB mode */
2600fe46dc6SMatthew Dillon 			cfbdec();
2610fe46dc6SMatthew Dillon 			break;
2620fe46dc6SMatthew Dillon 		case MODE_ENCRYPT:	/* encrypt using CFB mode */
2630fe46dc6SMatthew Dillon 			cfbenc();
2640fe46dc6SMatthew Dillon 			break;
2650fe46dc6SMatthew Dillon 		}
2660fe46dc6SMatthew Dillon 		break;
2670fe46dc6SMatthew Dillon 	case ALG_CFBA:
2680fe46dc6SMatthew Dillon 		switch(mode) {
2690fe46dc6SMatthew Dillon 		case MODE_AUTHENTICATE:	/* authenticate using CFBA mode */
2700fe46dc6SMatthew Dillon 			errx(1, "can't authenticate with CFBA mode");
2710fe46dc6SMatthew Dillon 			break;
2720fe46dc6SMatthew Dillon 		case MODE_DECRYPT:	/* decrypt using CFBA mode */
2730fe46dc6SMatthew Dillon 			cfbadec();
2740fe46dc6SMatthew Dillon 			break;
2750fe46dc6SMatthew Dillon 		case MODE_ENCRYPT:	/* encrypt using CFBA mode */
2760fe46dc6SMatthew Dillon 			cfbaenc();
2770fe46dc6SMatthew Dillon 			break;
2780fe46dc6SMatthew Dillon 		}
2790fe46dc6SMatthew Dillon 		break;
2800fe46dc6SMatthew Dillon 	case ALG_ECB:
2810fe46dc6SMatthew Dillon 		switch(mode) {
2820fe46dc6SMatthew Dillon 		case MODE_AUTHENTICATE:	/* authenticate using ECB mode */
2830fe46dc6SMatthew Dillon 			errx(1, "can't authenticate with ECB mode");
2840fe46dc6SMatthew Dillon 			break;
2850fe46dc6SMatthew Dillon 		case MODE_DECRYPT:	/* decrypt using ECB mode */
2860fe46dc6SMatthew Dillon 			ecbdec();
2870fe46dc6SMatthew Dillon 			break;
2880fe46dc6SMatthew Dillon 		case MODE_ENCRYPT:	/* encrypt using ECB mode */
2890fe46dc6SMatthew Dillon 			ecbenc();
2900fe46dc6SMatthew Dillon 			break;
2910fe46dc6SMatthew Dillon 		}
2920fe46dc6SMatthew Dillon 		break;
2930fe46dc6SMatthew Dillon 	case ALG_OFB:
2940fe46dc6SMatthew Dillon 		switch(mode) {
2950fe46dc6SMatthew Dillon 		case MODE_AUTHENTICATE:	/* authenticate using OFB mode */
2960fe46dc6SMatthew Dillon 			errx(1, "can't authenticate with OFB mode");
2970fe46dc6SMatthew Dillon 			break;
2980fe46dc6SMatthew Dillon 		case MODE_DECRYPT:	/* decrypt using OFB mode */
2990fe46dc6SMatthew Dillon 			ofbdec();
3000fe46dc6SMatthew Dillon 			break;
3010fe46dc6SMatthew Dillon 		case MODE_ENCRYPT:	/* encrypt using OFB mode */
3020fe46dc6SMatthew Dillon 			ofbenc();
3030fe46dc6SMatthew Dillon 			break;
3040fe46dc6SMatthew Dillon 		}
3050fe46dc6SMatthew Dillon 		break;
3060fe46dc6SMatthew Dillon 	}
3070fe46dc6SMatthew Dillon 	return (0);
3080fe46dc6SMatthew Dillon }
3090fe46dc6SMatthew Dillon 
3100fe46dc6SMatthew Dillon /*
3110fe46dc6SMatthew Dillon  * map a hex character to an integer
3120fe46dc6SMatthew Dillon  */
3130fe46dc6SMatthew Dillon static int
tobinhex(char c,int radix)3140fe46dc6SMatthew Dillon tobinhex(char c, int radix)
3150fe46dc6SMatthew Dillon {
3160fe46dc6SMatthew Dillon 	switch(c) {
3170fe46dc6SMatthew Dillon 	case '0':		return(0x0);
3180fe46dc6SMatthew Dillon 	case '1':		return(0x1);
3190fe46dc6SMatthew Dillon 	case '2':		return(radix > 2 ? 0x2 : -1);
3200fe46dc6SMatthew Dillon 	case '3':		return(radix > 3 ? 0x3 : -1);
3210fe46dc6SMatthew Dillon 	case '4':		return(radix > 4 ? 0x4 : -1);
3220fe46dc6SMatthew Dillon 	case '5':		return(radix > 5 ? 0x5 : -1);
3230fe46dc6SMatthew Dillon 	case '6':		return(radix > 6 ? 0x6 : -1);
3240fe46dc6SMatthew Dillon 	case '7':		return(radix > 7 ? 0x7 : -1);
3250fe46dc6SMatthew Dillon 	case '8':		return(radix > 8 ? 0x8 : -1);
3260fe46dc6SMatthew Dillon 	case '9':		return(radix > 9 ? 0x9 : -1);
3270fe46dc6SMatthew Dillon 	case 'A': case 'a':	return(radix > 10 ? 0xa : -1);
3280fe46dc6SMatthew Dillon 	case 'B': case 'b':	return(radix > 11 ? 0xb : -1);
3290fe46dc6SMatthew Dillon 	case 'C': case 'c':	return(radix > 12 ? 0xc : -1);
3300fe46dc6SMatthew Dillon 	case 'D': case 'd':	return(radix > 13 ? 0xd : -1);
3310fe46dc6SMatthew Dillon 	case 'E': case 'e':	return(radix > 14 ? 0xe : -1);
3320fe46dc6SMatthew Dillon 	case 'F': case 'f':	return(radix > 15 ? 0xf : -1);
3330fe46dc6SMatthew Dillon 	}
3340fe46dc6SMatthew Dillon 	/*
3350fe46dc6SMatthew Dillon 	 * invalid character
3360fe46dc6SMatthew Dillon 	 */
3370fe46dc6SMatthew Dillon 	return(-1);
3380fe46dc6SMatthew Dillon }
3390fe46dc6SMatthew Dillon 
3400fe46dc6SMatthew Dillon /*
3410fe46dc6SMatthew Dillon  * convert the key to a bit pattern
3420fe46dc6SMatthew Dillon  */
3430fe46dc6SMatthew Dillon static void
cvtkey(DES_cblock obuf,char * ibuf)3440fe46dc6SMatthew Dillon cvtkey(DES_cblock obuf, char *ibuf)
3450fe46dc6SMatthew Dillon {
3460fe46dc6SMatthew Dillon 	int i, j;			/* counter in a for loop */
3470fe46dc6SMatthew Dillon 	int nbuf[64];			/* used for hex/key translation */
3480fe46dc6SMatthew Dillon 
3490fe46dc6SMatthew Dillon 	/*
3500fe46dc6SMatthew Dillon 	 * just switch on the key base
3510fe46dc6SMatthew Dillon 	 */
3520fe46dc6SMatthew Dillon 	switch(keybase) {
3530fe46dc6SMatthew Dillon 	case KEY_ASCII:			/* ascii to integer */
3540fe46dc6SMatthew Dillon 		(void)strncpy(obuf, ibuf, 8);
3550fe46dc6SMatthew Dillon 		return;
3560fe46dc6SMatthew Dillon 	case KEY_DEFAULT:		/* tell from context */
3570fe46dc6SMatthew Dillon 		/*
3580fe46dc6SMatthew Dillon 		 * leading '0x' or '0X' == hex key
3590fe46dc6SMatthew Dillon 		 */
3600fe46dc6SMatthew Dillon 		if (ibuf[0] == '0' && (ibuf[1] == 'x' || ibuf[1] == 'X')) {
3610fe46dc6SMatthew Dillon 			ibuf = &ibuf[2];
3620fe46dc6SMatthew Dillon 			/*
3630fe46dc6SMatthew Dillon 			 * now translate it, bombing on any illegal hex digit
3640fe46dc6SMatthew Dillon 			 */
365*799ba435SSascha Wildner 			for (i = 0; i < 16 && ibuf[i]; i++)
3660fe46dc6SMatthew Dillon 				if ((nbuf[i] = tobinhex(ibuf[i], 16)) == -1)
3670fe46dc6SMatthew Dillon 					warnx("bad hex digit in key");
3680fe46dc6SMatthew Dillon 			while (i < 16)
3690fe46dc6SMatthew Dillon 				nbuf[i++] = 0;
3700fe46dc6SMatthew Dillon 			for (i = 0; i < 8; i++)
3710fe46dc6SMatthew Dillon 				obuf[i] =
3720fe46dc6SMatthew Dillon 				    ((nbuf[2*i]&0xf)<<4) | (nbuf[2*i+1]&0xf);
3730fe46dc6SMatthew Dillon 			/* preserve parity bits */
3740fe46dc6SMatthew Dillon 			pflag = 1;
3750fe46dc6SMatthew Dillon 			return;
3760fe46dc6SMatthew Dillon 		}
3770fe46dc6SMatthew Dillon 		/*
3780fe46dc6SMatthew Dillon 		 * leading '0b' or '0B' == binary key
3790fe46dc6SMatthew Dillon 		 */
3800fe46dc6SMatthew Dillon 		if (ibuf[0] == '0' && (ibuf[1] == 'b' || ibuf[1] == 'B')) {
3810fe46dc6SMatthew Dillon 			ibuf = &ibuf[2];
3820fe46dc6SMatthew Dillon 			/*
3830fe46dc6SMatthew Dillon 			 * now translate it, bombing on any illegal binary digit
3840fe46dc6SMatthew Dillon 			 */
385*799ba435SSascha Wildner 			for (i = 0; i < 16 && ibuf[i]; i++)
3860fe46dc6SMatthew Dillon 				if ((nbuf[i] = tobinhex(ibuf[i], 2)) == -1)
3870fe46dc6SMatthew Dillon 					warnx("bad binary digit in key");
3880fe46dc6SMatthew Dillon 			while (i < 64)
3890fe46dc6SMatthew Dillon 				nbuf[i++] = 0;
3900fe46dc6SMatthew Dillon 			for (i = 0; i < 8; i++)
3910fe46dc6SMatthew Dillon 				for (j = 0; j < 8; j++)
3920fe46dc6SMatthew Dillon 					obuf[i] = (obuf[i]<<1)|nbuf[8*i+j];
3930fe46dc6SMatthew Dillon 			/* preserve parity bits */
3940fe46dc6SMatthew Dillon 			pflag = 1;
3950fe46dc6SMatthew Dillon 			return;
3960fe46dc6SMatthew Dillon 		}
3970fe46dc6SMatthew Dillon 		/*
3980fe46dc6SMatthew Dillon 		 * no special leader -- ASCII
3990fe46dc6SMatthew Dillon 		 */
4000fe46dc6SMatthew Dillon 		(void)strncpy(obuf, ibuf, 8);
4010fe46dc6SMatthew Dillon 	}
4020fe46dc6SMatthew Dillon }
4030fe46dc6SMatthew Dillon 
4040fe46dc6SMatthew Dillon /*
4050fe46dc6SMatthew Dillon  * convert an ASCII string into a decimal number:
4060fe46dc6SMatthew Dillon  * 1. must be between 0 and 64 inclusive
4070fe46dc6SMatthew Dillon  * 2. must be a valid decimal number
4080fe46dc6SMatthew Dillon  * 3. must be a multiple of mult
4090fe46dc6SMatthew Dillon  */
4100fe46dc6SMatthew Dillon static int
setbits(char * s,int mult)4110fe46dc6SMatthew Dillon setbits(char *s, int mult)
4120fe46dc6SMatthew Dillon {
4130fe46dc6SMatthew Dillon 	char *p;			/* pointer in a for loop */
4140fe46dc6SMatthew Dillon 	int n = 0;			/* the integer collected */
4150fe46dc6SMatthew Dillon 
4160fe46dc6SMatthew Dillon 	/*
4170fe46dc6SMatthew Dillon 	 * skip white space
4180fe46dc6SMatthew Dillon 	 */
4190fe46dc6SMatthew Dillon 	while (isspace(*s))
4200fe46dc6SMatthew Dillon 		s++;
4210fe46dc6SMatthew Dillon 	/*
4220fe46dc6SMatthew Dillon 	 * get the integer
4230fe46dc6SMatthew Dillon 	 */
4240fe46dc6SMatthew Dillon 	for (p = s; *p; p++) {
4250fe46dc6SMatthew Dillon 		if (isdigit(*p))
4260fe46dc6SMatthew Dillon 			n = n * 10 + *p - '0';
4270fe46dc6SMatthew Dillon 		else {
4280fe46dc6SMatthew Dillon 			warnx("bad decimal digit in MAC length");
4290fe46dc6SMatthew Dillon 		}
4300fe46dc6SMatthew Dillon 	}
4310fe46dc6SMatthew Dillon 	/*
4320fe46dc6SMatthew Dillon 	 * be sure it's a multiple of mult
4330fe46dc6SMatthew Dillon 	 */
4340fe46dc6SMatthew Dillon 	return((n % mult != 0) ? -1 : n);
4350fe46dc6SMatthew Dillon }
4360fe46dc6SMatthew Dillon 
4370fe46dc6SMatthew Dillon /*****************
4380fe46dc6SMatthew Dillon  * DES FUNCTIONS *
4390fe46dc6SMatthew Dillon  *****************/
4400fe46dc6SMatthew Dillon /*
4410fe46dc6SMatthew Dillon  * This sets the DES key and (if you're using the deszip version)
4420fe46dc6SMatthew Dillon  * the direction of the transformation.  This uses the Sun
4430fe46dc6SMatthew Dillon  * to map the 64-bit key onto the 56 bits that the key schedule
4440fe46dc6SMatthew Dillon  * generation routines use: the old way, which just uses the user-
4450fe46dc6SMatthew Dillon  * supplied 64 bits as is, and the new way, which resets the parity
4460fe46dc6SMatthew Dillon  * bit to be the same as the low-order bit in each character.  The
4470fe46dc6SMatthew Dillon  * new way generates a greater variety of key schedules, since many
4480fe46dc6SMatthew Dillon  * systems set the parity (high) bit of each character to 0, and the
4490fe46dc6SMatthew Dillon  * DES ignores the low order bit of each character.
4500fe46dc6SMatthew Dillon  */
4510fe46dc6SMatthew Dillon static void
makekey(DES_cblock * buf)4520fe46dc6SMatthew Dillon makekey(DES_cblock *buf)
4530fe46dc6SMatthew Dillon {
4540fe46dc6SMatthew Dillon 	int i, j;				/* counter in a for loop */
4550fe46dc6SMatthew Dillon 	int par;				/* parity counter */
4560fe46dc6SMatthew Dillon 
4570fe46dc6SMatthew Dillon 	/*
4580fe46dc6SMatthew Dillon 	 * if the parity is not preserved, flip it
4590fe46dc6SMatthew Dillon 	 */
4600fe46dc6SMatthew Dillon 	if (!pflag) {
4610fe46dc6SMatthew Dillon 		for (i = 0; i < 8; i++) {
4620fe46dc6SMatthew Dillon 			par = 0;
4630fe46dc6SMatthew Dillon 			for (j = 1; j < 8; j++)
4640fe46dc6SMatthew Dillon 				if ((bits[j] & (*buf)[i]) != 0)
4650fe46dc6SMatthew Dillon 					par++;
4660fe46dc6SMatthew Dillon 			if ((par & 0x01) == 0x01)
4670fe46dc6SMatthew Dillon 				(*buf)[i] &= 0x7f;
4680fe46dc6SMatthew Dillon 			else
4690fe46dc6SMatthew Dillon 				(*buf)[i] = ((*buf)[i] & 0x7f) | 0x80;
4700fe46dc6SMatthew Dillon 		}
4710fe46dc6SMatthew Dillon 	}
4720fe46dc6SMatthew Dillon 
4730fe46dc6SMatthew Dillon 	DES_set_odd_parity(buf);
4740fe46dc6SMatthew Dillon 	DES_set_key(buf, &schedule);
4750fe46dc6SMatthew Dillon }
4760fe46dc6SMatthew Dillon 
4770fe46dc6SMatthew Dillon /*
4780fe46dc6SMatthew Dillon  * This encrypts using the Electronic Code Book mode of DES
4790fe46dc6SMatthew Dillon  */
4800fe46dc6SMatthew Dillon static void
ecbenc(void)4810fe46dc6SMatthew Dillon ecbenc(void)
4820fe46dc6SMatthew Dillon {
4830fe46dc6SMatthew Dillon 	int n;				/* number of bytes actually read */
4840fe46dc6SMatthew Dillon 	int bn;				/* block number */
4850fe46dc6SMatthew Dillon 	DES_cblock msgbuf;		/* I/O buffer */
4860fe46dc6SMatthew Dillon 
4870fe46dc6SMatthew Dillon 	for (bn = 0; (n = READ(msgbuf,  8)) == 8; bn++) {
4880fe46dc6SMatthew Dillon 		/*
4890fe46dc6SMatthew Dillon 		 * do the transformation
4900fe46dc6SMatthew Dillon 		 */
4910fe46dc6SMatthew Dillon 		DES_XFORM(&msgbuf);
4920fe46dc6SMatthew Dillon 		WRITE(&msgbuf, 8);
4930fe46dc6SMatthew Dillon 	}
4940fe46dc6SMatthew Dillon 	/*
4950fe46dc6SMatthew Dillon 	 * at EOF or last block -- in either case, the last byte contains
4960fe46dc6SMatthew Dillon 	 * the character representation of the number of bytes in it
4970fe46dc6SMatthew Dillon 	 */
4980fe46dc6SMatthew Dillon 	bn++;
4990fe46dc6SMatthew Dillon 	MEMZERO(&msgbuf[n], 8 - n);
5000fe46dc6SMatthew Dillon 	msgbuf[7] = n;
5010fe46dc6SMatthew Dillon 	DES_XFORM(&msgbuf);
5020fe46dc6SMatthew Dillon 	WRITE(&msgbuf, 8);
5030fe46dc6SMatthew Dillon 
5040fe46dc6SMatthew Dillon }
5050fe46dc6SMatthew Dillon 
5060fe46dc6SMatthew Dillon /*
5070fe46dc6SMatthew Dillon  * This decrypts using the Electronic Code Book mode of DES
5080fe46dc6SMatthew Dillon  */
5090fe46dc6SMatthew Dillon static void
ecbdec(void)5100fe46dc6SMatthew Dillon ecbdec(void)
5110fe46dc6SMatthew Dillon {
5120fe46dc6SMatthew Dillon 	int n;			/* number of bytes actually read */
5130fe46dc6SMatthew Dillon 	int c;			/* used to test for EOF */
5140fe46dc6SMatthew Dillon 	int bn;			/* block number */
5150fe46dc6SMatthew Dillon 	DES_cblock msgbuf;		/* I/O buffer */
5160fe46dc6SMatthew Dillon 
5170fe46dc6SMatthew Dillon 	for (bn = 1; (n = READ(msgbuf, 8)) == 8; bn++) {
5180fe46dc6SMatthew Dillon 		/*
5190fe46dc6SMatthew Dillon 		 * do the transformation
5200fe46dc6SMatthew Dillon 		 */
5210fe46dc6SMatthew Dillon 		DES_XFORM(&msgbuf);
5220fe46dc6SMatthew Dillon 		/*
5230fe46dc6SMatthew Dillon 		 * if the last one, handle it specially
5240fe46dc6SMatthew Dillon 		 */
5250fe46dc6SMatthew Dillon 		if ((c = getchar()) == EOF) {
5260fe46dc6SMatthew Dillon 			n = msgbuf[7];
5270fe46dc6SMatthew Dillon 			if (n < 0 || n > 7)
5280fe46dc6SMatthew Dillon 				warnx("decryption failed (block corrupt) at %d",
5290fe46dc6SMatthew Dillon 				    bn);
5300fe46dc6SMatthew Dillon 		}
5310fe46dc6SMatthew Dillon 		else
5320fe46dc6SMatthew Dillon 			(void)ungetc(c, stdin);
5330fe46dc6SMatthew Dillon 		WRITE(msgbuf, n);
5340fe46dc6SMatthew Dillon 	}
5350fe46dc6SMatthew Dillon 	if (n > 0)
5360fe46dc6SMatthew Dillon 		warnx("decryption failed (incomplete block) at %d", bn);
5370fe46dc6SMatthew Dillon }
5380fe46dc6SMatthew Dillon 
5390fe46dc6SMatthew Dillon /*
5400fe46dc6SMatthew Dillon  * This encrypts using the Cipher Block Chaining mode of DES
5410fe46dc6SMatthew Dillon  */
5420fe46dc6SMatthew Dillon static void
cbcenc(void)5430fe46dc6SMatthew Dillon cbcenc(void)
5440fe46dc6SMatthew Dillon {
5450fe46dc6SMatthew Dillon 	int n;			/* number of bytes actually read */
5460fe46dc6SMatthew Dillon 	int bn;			/* block number */
5470fe46dc6SMatthew Dillon 	DES_cblock msgbuf;	/* I/O buffer */
5480fe46dc6SMatthew Dillon 
5490fe46dc6SMatthew Dillon 	/*
5500fe46dc6SMatthew Dillon 	 * do the transformation
5510fe46dc6SMatthew Dillon 	 */
5520fe46dc6SMatthew Dillon 	for (bn = 1; (n = READ(msgbuf, 8)) == 8; bn++) {
5530fe46dc6SMatthew Dillon 		for (n = 0; n < 8; n++)
5540fe46dc6SMatthew Dillon 			msgbuf[n] ^= ivec[n];
5550fe46dc6SMatthew Dillon 		DES_XFORM(&msgbuf);
5560fe46dc6SMatthew Dillon 		MEMCPY(ivec, msgbuf, 8);
5570fe46dc6SMatthew Dillon 		WRITE(msgbuf, 8);
5580fe46dc6SMatthew Dillon 	}
5590fe46dc6SMatthew Dillon 	/*
5600fe46dc6SMatthew Dillon 	 * at EOF or last block -- in either case, the last byte contains
5610fe46dc6SMatthew Dillon 	 * the character representation of the number of bytes in it
5620fe46dc6SMatthew Dillon 	 */
5630fe46dc6SMatthew Dillon 	bn++;
5640fe46dc6SMatthew Dillon 	MEMZERO(&msgbuf[n], 8 - n);
5650fe46dc6SMatthew Dillon 	msgbuf[7] = n;
5660fe46dc6SMatthew Dillon 	for (n = 0; n < 8; n++)
5670fe46dc6SMatthew Dillon 		msgbuf[n] ^= ivec[n];
5680fe46dc6SMatthew Dillon 	DES_XFORM(&msgbuf);
5690fe46dc6SMatthew Dillon 	WRITE(msgbuf, 8);
5700fe46dc6SMatthew Dillon 
5710fe46dc6SMatthew Dillon }
5720fe46dc6SMatthew Dillon 
5730fe46dc6SMatthew Dillon /*
5740fe46dc6SMatthew Dillon  * This decrypts using the Cipher Block Chaining mode of DES
5750fe46dc6SMatthew Dillon  */
5760fe46dc6SMatthew Dillon static void
cbcdec(void)5770fe46dc6SMatthew Dillon cbcdec(void)
5780fe46dc6SMatthew Dillon {
5790fe46dc6SMatthew Dillon 	int n;			/* number of bytes actually read */
5800fe46dc6SMatthew Dillon 	DES_cblock msgbuf;	/* I/O buffer */
5810fe46dc6SMatthew Dillon 	DES_cblock ibuf;	/* temp buffer for initialization vector */
5820fe46dc6SMatthew Dillon 	int c;			/* used to test for EOF */
5830fe46dc6SMatthew Dillon 	int bn;			/* block number */
5840fe46dc6SMatthew Dillon 
5850fe46dc6SMatthew Dillon 	for (bn = 0; (n = READ(msgbuf, 8)) == 8; bn++) {
5860fe46dc6SMatthew Dillon 		/*
5870fe46dc6SMatthew Dillon 		 * do the transformation
5880fe46dc6SMatthew Dillon 		 */
5890fe46dc6SMatthew Dillon 		MEMCPY(ibuf, msgbuf, 8);
5900fe46dc6SMatthew Dillon 		DES_XFORM(&msgbuf);
5910fe46dc6SMatthew Dillon 		for (c = 0; c < 8; c++)
5920fe46dc6SMatthew Dillon 			msgbuf[c] ^= ivec[c];
5930fe46dc6SMatthew Dillon 		MEMCPY(ivec, ibuf, 8);
5940fe46dc6SMatthew Dillon 		/*
5950fe46dc6SMatthew Dillon 		 * if the last one, handle it specially
5960fe46dc6SMatthew Dillon 		 */
5970fe46dc6SMatthew Dillon 		if ((c = getchar()) == EOF) {
5980fe46dc6SMatthew Dillon 			n = msgbuf[7];
5990fe46dc6SMatthew Dillon 			if (n < 0 || n > 7)
6000fe46dc6SMatthew Dillon 				warnx("decryption failed (block corrupt) at %d",
6010fe46dc6SMatthew Dillon 				    bn);
6020fe46dc6SMatthew Dillon 		}
6030fe46dc6SMatthew Dillon 		else
6040fe46dc6SMatthew Dillon 			(void)ungetc(c, stdin);
6050fe46dc6SMatthew Dillon 		WRITE(msgbuf, n);
6060fe46dc6SMatthew Dillon 	}
6070fe46dc6SMatthew Dillon 	if (n > 0)
6080fe46dc6SMatthew Dillon 		warnx("decryption failed (incomplete block) at %d", bn);
6090fe46dc6SMatthew Dillon }
6100fe46dc6SMatthew Dillon 
6110fe46dc6SMatthew Dillon /*
6120fe46dc6SMatthew Dillon  * This authenticates using the Cipher Block Chaining mode of DES
6130fe46dc6SMatthew Dillon  */
6140fe46dc6SMatthew Dillon static void
cbcauth(void)6150fe46dc6SMatthew Dillon cbcauth(void)
6160fe46dc6SMatthew Dillon {
6170fe46dc6SMatthew Dillon 	int n, j;		/* number of bytes actually read */
6180fe46dc6SMatthew Dillon 	DES_cblock msgbuf;		/* I/O buffer */
6190fe46dc6SMatthew Dillon 	DES_cblock encbuf;		/* encryption buffer */
6200fe46dc6SMatthew Dillon 
6210fe46dc6SMatthew Dillon 	/*
6220fe46dc6SMatthew Dillon 	 * do the transformation
6230fe46dc6SMatthew Dillon 	 * note we DISCARD the encrypted block;
6240fe46dc6SMatthew Dillon 	 * we only care about the last one
6250fe46dc6SMatthew Dillon 	 */
6260fe46dc6SMatthew Dillon 	while ((n = READ(msgbuf, 8)) == 8) {
6270fe46dc6SMatthew Dillon 		for (n = 0; n < 8; n++)
6280fe46dc6SMatthew Dillon 			encbuf[n] = msgbuf[n] ^ ivec[n];
6290fe46dc6SMatthew Dillon 		DES_XFORM(&encbuf);
6300fe46dc6SMatthew Dillon 		MEMCPY(ivec, encbuf, 8);
6310fe46dc6SMatthew Dillon 	}
6320fe46dc6SMatthew Dillon 	/*
6330fe46dc6SMatthew Dillon 	 * now compute the last one, right padding with '\0' if need be
6340fe46dc6SMatthew Dillon 	 */
6350fe46dc6SMatthew Dillon 	if (n > 0) {
6360fe46dc6SMatthew Dillon 		MEMZERO(&msgbuf[n], 8 - n);
6370fe46dc6SMatthew Dillon 		for (n = 0; n < 8; n++)
6380fe46dc6SMatthew Dillon 			encbuf[n] = msgbuf[n] ^ ivec[n];
6390fe46dc6SMatthew Dillon 		DES_XFORM(&encbuf);
6400fe46dc6SMatthew Dillon 	}
6410fe46dc6SMatthew Dillon 	/*
6420fe46dc6SMatthew Dillon 	 * drop the bits
6430fe46dc6SMatthew Dillon 	 * we write chars until fewer than 7 bits,
6440fe46dc6SMatthew Dillon 	 * and then pad the last one with 0 bits
6450fe46dc6SMatthew Dillon 	 */
6460fe46dc6SMatthew Dillon 	for (n = 0; macbits > 7; n++, macbits -= 8)
6470fe46dc6SMatthew Dillon 		(void)putchar(encbuf[n]);
6480fe46dc6SMatthew Dillon 	if (macbits > 0) {
6490fe46dc6SMatthew Dillon 		msgbuf[0] = 0x00;
6500fe46dc6SMatthew Dillon 		for (j = 0; j < macbits; j++)
6510fe46dc6SMatthew Dillon 			msgbuf[0] |= encbuf[n] & bits[j];
6520fe46dc6SMatthew Dillon 		(void)putchar(msgbuf[0]);
6530fe46dc6SMatthew Dillon 	}
6540fe46dc6SMatthew Dillon }
6550fe46dc6SMatthew Dillon 
6560fe46dc6SMatthew Dillon /*
6570fe46dc6SMatthew Dillon  * This encrypts using the Cipher FeedBack mode of DES
6580fe46dc6SMatthew Dillon  */
6590fe46dc6SMatthew Dillon static void
cfbenc(void)6600fe46dc6SMatthew Dillon cfbenc(void)
6610fe46dc6SMatthew Dillon {
6620fe46dc6SMatthew Dillon 	int n;			/* number of bytes actually read */
6630fe46dc6SMatthew Dillon 	int nbytes;		/* number of bytes to read */
6640fe46dc6SMatthew Dillon 	int bn;			/* block number */
6650fe46dc6SMatthew Dillon 	char ibuf[8];		/* input buffer */
6660fe46dc6SMatthew Dillon 	DES_cblock msgbuf;		/* encryption buffer */
6670fe46dc6SMatthew Dillon 
6680fe46dc6SMatthew Dillon 	/*
6690fe46dc6SMatthew Dillon 	 * do things in bytes, not bits
6700fe46dc6SMatthew Dillon 	 */
6710fe46dc6SMatthew Dillon 	nbytes = fbbits / 8;
6720fe46dc6SMatthew Dillon 	/*
6730fe46dc6SMatthew Dillon 	 * do the transformation
6740fe46dc6SMatthew Dillon 	 */
6750fe46dc6SMatthew Dillon 	for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
6760fe46dc6SMatthew Dillon 		MEMCPY(msgbuf, ivec, 8);
6770fe46dc6SMatthew Dillon 		DES_XFORM(&msgbuf);
6780fe46dc6SMatthew Dillon 		for (n = 0; n < 8 - nbytes; n++)
6790fe46dc6SMatthew Dillon 			ivec[n] = ivec[n+nbytes];
6800fe46dc6SMatthew Dillon 		for (n = 0; n < nbytes; n++)
6810fe46dc6SMatthew Dillon 			ivec[8 - nbytes + n] = ibuf[n] ^ msgbuf[n];
6820fe46dc6SMatthew Dillon 		WRITE(&ivec[8 - nbytes], nbytes);
6830fe46dc6SMatthew Dillon 	}
6840fe46dc6SMatthew Dillon 	/*
6850fe46dc6SMatthew Dillon 	 * at EOF or last block -- in either case, the last byte contains
6860fe46dc6SMatthew Dillon 	 * the character representation of the number of bytes in it
6870fe46dc6SMatthew Dillon 	 */
6880fe46dc6SMatthew Dillon 	bn++;
6890fe46dc6SMatthew Dillon 	MEMZERO(&ibuf[n], nbytes - n);
6900fe46dc6SMatthew Dillon 	ibuf[nbytes - 1] = n;
6910fe46dc6SMatthew Dillon 	MEMCPY(msgbuf, ivec, 8);
6920fe46dc6SMatthew Dillon 	DES_XFORM(&msgbuf);
6930fe46dc6SMatthew Dillon 	for (n = 0; n < nbytes; n++)
6940fe46dc6SMatthew Dillon 		ibuf[n] ^= msgbuf[n];
6950fe46dc6SMatthew Dillon 	WRITE(ibuf, nbytes);
6960fe46dc6SMatthew Dillon }
6970fe46dc6SMatthew Dillon 
6980fe46dc6SMatthew Dillon /*
6990fe46dc6SMatthew Dillon  * This decrypts using the Cipher Block Chaining mode of DES
7000fe46dc6SMatthew Dillon  */
7010fe46dc6SMatthew Dillon static void
cfbdec(void)7020fe46dc6SMatthew Dillon cfbdec(void)
7030fe46dc6SMatthew Dillon {
7040fe46dc6SMatthew Dillon 	int n;			/* number of bytes actually read */
7050fe46dc6SMatthew Dillon 	int c;			/* used to test for EOF */
7060fe46dc6SMatthew Dillon 	int nbytes;		/* number of bytes to read */
7070fe46dc6SMatthew Dillon 	int bn;			/* block number */
7080fe46dc6SMatthew Dillon 	char ibuf[8];		/* input buffer */
7090fe46dc6SMatthew Dillon 	char obuf[8];		/* output buffer */
7100fe46dc6SMatthew Dillon 	DES_cblock msgbuf;		/* encryption buffer */
7110fe46dc6SMatthew Dillon 
7120fe46dc6SMatthew Dillon 	/*
7130fe46dc6SMatthew Dillon 	 * do things in bytes, not bits
7140fe46dc6SMatthew Dillon 	 */
7150fe46dc6SMatthew Dillon 	nbytes = fbbits / 8;
7160fe46dc6SMatthew Dillon 	/*
7170fe46dc6SMatthew Dillon 	 * do the transformation
7180fe46dc6SMatthew Dillon 	 */
7190fe46dc6SMatthew Dillon 	for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
7200fe46dc6SMatthew Dillon 		MEMCPY(msgbuf, ivec, 8);
7210fe46dc6SMatthew Dillon 		DES_XFORM(&msgbuf);
7220fe46dc6SMatthew Dillon 		for (c = 0; c < 8 - nbytes; c++)
7230fe46dc6SMatthew Dillon 			ivec[c] = ivec[c + nbytes];
7240fe46dc6SMatthew Dillon 		for (c = 0; c < nbytes; c++) {
7250fe46dc6SMatthew Dillon 			ivec[8 - nbytes + c] = ibuf[c];
7260fe46dc6SMatthew Dillon 			obuf[c] = ibuf[c] ^ msgbuf[c];
7270fe46dc6SMatthew Dillon 		}
7280fe46dc6SMatthew Dillon 		/*
7290fe46dc6SMatthew Dillon 		 * if the last one, handle it specially
7300fe46dc6SMatthew Dillon 		 */
7310fe46dc6SMatthew Dillon 		if ((c = getchar()) == EOF) {
7320fe46dc6SMatthew Dillon 			n = obuf[nbytes-1];
7330fe46dc6SMatthew Dillon 			if (n < 0 || n > nbytes-1)
7340fe46dc6SMatthew Dillon 				warnx("decryption failed (block corrupt) at %d",
7350fe46dc6SMatthew Dillon 				    bn);
7360fe46dc6SMatthew Dillon 		}
7370fe46dc6SMatthew Dillon 		else
7380fe46dc6SMatthew Dillon 			(void)ungetc(c, stdin);
7390fe46dc6SMatthew Dillon 		WRITE(obuf, n);
7400fe46dc6SMatthew Dillon 	}
7410fe46dc6SMatthew Dillon 	if (n > 0)
7420fe46dc6SMatthew Dillon 		warnx("decryption failed (incomplete block) at %d", bn);
7430fe46dc6SMatthew Dillon }
7440fe46dc6SMatthew Dillon 
7450fe46dc6SMatthew Dillon /*
7460fe46dc6SMatthew Dillon  * This encrypts using the alternative Cipher FeedBack mode of DES
7470fe46dc6SMatthew Dillon  */
7480fe46dc6SMatthew Dillon static void
cfbaenc(void)7490fe46dc6SMatthew Dillon cfbaenc(void)
7500fe46dc6SMatthew Dillon {
7510fe46dc6SMatthew Dillon 	int n;			/* number of bytes actually read */
7520fe46dc6SMatthew Dillon 	int nbytes;		/* number of bytes to read */
7530fe46dc6SMatthew Dillon 	int bn;			/* block number */
7540fe46dc6SMatthew Dillon 	char ibuf[8];		/* input buffer */
7550fe46dc6SMatthew Dillon 	char obuf[8];		/* output buffer */
7560fe46dc6SMatthew Dillon 	DES_cblock msgbuf;		/* encryption buffer */
7570fe46dc6SMatthew Dillon 
7580fe46dc6SMatthew Dillon 	/*
7590fe46dc6SMatthew Dillon 	 * do things in bytes, not bits
7600fe46dc6SMatthew Dillon 	 */
7610fe46dc6SMatthew Dillon 	nbytes = fbbits / 7;
7620fe46dc6SMatthew Dillon 	/*
7630fe46dc6SMatthew Dillon 	 * do the transformation
7640fe46dc6SMatthew Dillon 	 */
7650fe46dc6SMatthew Dillon 	for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
7660fe46dc6SMatthew Dillon 		MEMCPY(msgbuf, ivec, 8);
7670fe46dc6SMatthew Dillon 		DES_XFORM(&msgbuf);
7680fe46dc6SMatthew Dillon 		for (n = 0; n < 8 - nbytes; n++)
7690fe46dc6SMatthew Dillon 			ivec[n] = ivec[n + nbytes];
7700fe46dc6SMatthew Dillon 		for (n = 0; n < nbytes; n++)
7710fe46dc6SMatthew Dillon 			ivec[8 - nbytes + n] = (ibuf[n] ^ msgbuf[n]) | 0x80;
7720fe46dc6SMatthew Dillon 		for (n = 0; n < nbytes; n++)
7730fe46dc6SMatthew Dillon 			obuf[n] = ivec[8 - nbytes + n] & 0x7f;
7740fe46dc6SMatthew Dillon 		WRITE(obuf, nbytes);
7750fe46dc6SMatthew Dillon 	}
7760fe46dc6SMatthew Dillon 	/*
7770fe46dc6SMatthew Dillon 	 * at EOF or last block -- in either case, the last byte contains
7780fe46dc6SMatthew Dillon 	 * the character representation of the number of bytes in it
7790fe46dc6SMatthew Dillon 	 */
7800fe46dc6SMatthew Dillon 	bn++;
7810fe46dc6SMatthew Dillon 	MEMZERO(&ibuf[n], nbytes - n);
7820fe46dc6SMatthew Dillon 	ibuf[nbytes - 1] = ('0' + n)|0200;
7830fe46dc6SMatthew Dillon 	MEMCPY(msgbuf, ivec, 8);
7840fe46dc6SMatthew Dillon 	DES_XFORM(&msgbuf);
7850fe46dc6SMatthew Dillon 	for (n = 0; n < nbytes; n++)
7860fe46dc6SMatthew Dillon 		ibuf[n] ^= msgbuf[n];
7870fe46dc6SMatthew Dillon 	WRITE(ibuf, nbytes);
7880fe46dc6SMatthew Dillon }
7890fe46dc6SMatthew Dillon 
7900fe46dc6SMatthew Dillon /*
7910fe46dc6SMatthew Dillon  * This decrypts using the alternative Cipher Block Chaining mode of DES
7920fe46dc6SMatthew Dillon  */
7930fe46dc6SMatthew Dillon static void
cfbadec(void)7940fe46dc6SMatthew Dillon cfbadec(void)
7950fe46dc6SMatthew Dillon {
7960fe46dc6SMatthew Dillon 	int n;			/* number of bytes actually read */
7970fe46dc6SMatthew Dillon 	int c;			/* used to test for EOF */
7980fe46dc6SMatthew Dillon 	int nbytes;		/* number of bytes to read */
7990fe46dc6SMatthew Dillon 	int bn;			/* block number */
8000fe46dc6SMatthew Dillon 	char ibuf[8];		/* input buffer */
8010fe46dc6SMatthew Dillon 	char obuf[8];		/* output buffer */
8020fe46dc6SMatthew Dillon 	DES_cblock msgbuf;		/* encryption buffer */
8030fe46dc6SMatthew Dillon 
8040fe46dc6SMatthew Dillon 	/*
8050fe46dc6SMatthew Dillon 	 * do things in bytes, not bits
8060fe46dc6SMatthew Dillon 	 */
8070fe46dc6SMatthew Dillon 	nbytes = fbbits / 7;
8080fe46dc6SMatthew Dillon 	/*
8090fe46dc6SMatthew Dillon 	 * do the transformation
8100fe46dc6SMatthew Dillon 	 */
8110fe46dc6SMatthew Dillon 	for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
8120fe46dc6SMatthew Dillon 		MEMCPY(msgbuf, ivec, 8);
8130fe46dc6SMatthew Dillon 		DES_XFORM(&msgbuf);
8140fe46dc6SMatthew Dillon 		for (c = 0; c < 8 - nbytes; c++)
8150fe46dc6SMatthew Dillon 			ivec[c] = ivec[c + nbytes];
8160fe46dc6SMatthew Dillon 		for (c = 0; c < nbytes; c++) {
8170fe46dc6SMatthew Dillon 			ivec[8 - nbytes + c] = ibuf[c] | 0x80;
8180fe46dc6SMatthew Dillon 			obuf[c] = (ibuf[c] ^ msgbuf[c]) & 0x7f;
8190fe46dc6SMatthew Dillon 		}
8200fe46dc6SMatthew Dillon 		/*
8210fe46dc6SMatthew Dillon 		 * if the last one, handle it specially
8220fe46dc6SMatthew Dillon 		 */
8230fe46dc6SMatthew Dillon 		if ((c = getchar()) == EOF) {
8240fe46dc6SMatthew Dillon 			if ((n = (obuf[nbytes-1] - '0')) < 0
8250fe46dc6SMatthew Dillon 						|| n > nbytes-1)
8260fe46dc6SMatthew Dillon 				warnx("decryption failed (block corrupt) at %d",
8270fe46dc6SMatthew Dillon 				    bn);
8280fe46dc6SMatthew Dillon 		}
8290fe46dc6SMatthew Dillon 		else
8300fe46dc6SMatthew Dillon 			(void)ungetc(c, stdin);
8310fe46dc6SMatthew Dillon 		WRITE(obuf, n);
8320fe46dc6SMatthew Dillon 	}
8330fe46dc6SMatthew Dillon 	if (n > 0)
8340fe46dc6SMatthew Dillon 		warnx("decryption failed (incomplete block) at %d", bn);
8350fe46dc6SMatthew Dillon }
8360fe46dc6SMatthew Dillon 
8370fe46dc6SMatthew Dillon 
8380fe46dc6SMatthew Dillon /*
8390fe46dc6SMatthew Dillon  * This encrypts using the Output FeedBack mode of DES
8400fe46dc6SMatthew Dillon  */
8410fe46dc6SMatthew Dillon static void
ofbenc(void)8420fe46dc6SMatthew Dillon ofbenc(void)
8430fe46dc6SMatthew Dillon {
8440fe46dc6SMatthew Dillon 	int n;			/* number of bytes actually read */
8450fe46dc6SMatthew Dillon 	int c;			/* used to test for EOF */
8460fe46dc6SMatthew Dillon 	int nbytes;		/* number of bytes to read */
8470fe46dc6SMatthew Dillon 	int bn;			/* block number */
8480fe46dc6SMatthew Dillon 	char ibuf[8];		/* input buffer */
8490fe46dc6SMatthew Dillon 	char obuf[8];		/* output buffer */
8500fe46dc6SMatthew Dillon 	DES_cblock msgbuf;		/* encryption buffer */
8510fe46dc6SMatthew Dillon 
8520fe46dc6SMatthew Dillon 	/*
8530fe46dc6SMatthew Dillon 	 * do things in bytes, not bits
8540fe46dc6SMatthew Dillon 	 */
8550fe46dc6SMatthew Dillon 	nbytes = fbbits / 8;
8560fe46dc6SMatthew Dillon 	/*
8570fe46dc6SMatthew Dillon 	 * do the transformation
8580fe46dc6SMatthew Dillon 	 */
8590fe46dc6SMatthew Dillon 	for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
8600fe46dc6SMatthew Dillon 		MEMCPY(msgbuf, ivec, 8);
8610fe46dc6SMatthew Dillon 		DES_XFORM(&msgbuf);
8620fe46dc6SMatthew Dillon 		for (n = 0; n < 8 - nbytes; n++)
8630fe46dc6SMatthew Dillon 			ivec[n] = ivec[n + nbytes];
8640fe46dc6SMatthew Dillon 		for (n = 0; n < nbytes; n++) {
8650fe46dc6SMatthew Dillon 			ivec[8 - nbytes + n] = msgbuf[n];
8660fe46dc6SMatthew Dillon 			obuf[n] = ibuf[n] ^ msgbuf[n];
8670fe46dc6SMatthew Dillon 		}
8680fe46dc6SMatthew Dillon 		WRITE(obuf, nbytes);
8690fe46dc6SMatthew Dillon 	}
8700fe46dc6SMatthew Dillon 	/*
8710fe46dc6SMatthew Dillon 	 * at EOF or last block -- in either case, the last byte contains
8720fe46dc6SMatthew Dillon 	 * the character representation of the number of bytes in it
8730fe46dc6SMatthew Dillon 	 */
8740fe46dc6SMatthew Dillon 	bn++;
8750fe46dc6SMatthew Dillon 	MEMZERO(&ibuf[n], nbytes - n);
8760fe46dc6SMatthew Dillon 	ibuf[nbytes - 1] = n;
8770fe46dc6SMatthew Dillon 	MEMCPY(msgbuf, ivec, 8);
8780fe46dc6SMatthew Dillon 	DES_XFORM(&msgbuf);
8790fe46dc6SMatthew Dillon 	for (c = 0; c < nbytes; c++)
8800fe46dc6SMatthew Dillon 		ibuf[c] ^= msgbuf[c];
8810fe46dc6SMatthew Dillon 	WRITE(ibuf, nbytes);
8820fe46dc6SMatthew Dillon }
8830fe46dc6SMatthew Dillon 
8840fe46dc6SMatthew Dillon /*
8850fe46dc6SMatthew Dillon  * This decrypts using the Output Block Chaining mode of DES
8860fe46dc6SMatthew Dillon  */
8870fe46dc6SMatthew Dillon static void
ofbdec(void)8880fe46dc6SMatthew Dillon ofbdec(void)
8890fe46dc6SMatthew Dillon {
8900fe46dc6SMatthew Dillon 	int n;			/* number of bytes actually read */
8910fe46dc6SMatthew Dillon 	int c;			/* used to test for EOF */
8920fe46dc6SMatthew Dillon 	int nbytes;		/* number of bytes to read */
8930fe46dc6SMatthew Dillon 	int bn;			/* block number */
8940fe46dc6SMatthew Dillon 	char ibuf[8];		/* input buffer */
8950fe46dc6SMatthew Dillon 	char obuf[8];		/* output buffer */
8960fe46dc6SMatthew Dillon 	DES_cblock msgbuf;		/* encryption buffer */
8970fe46dc6SMatthew Dillon 
8980fe46dc6SMatthew Dillon 	/*
8990fe46dc6SMatthew Dillon 	 * do things in bytes, not bits
9000fe46dc6SMatthew Dillon 	 */
9010fe46dc6SMatthew Dillon 	nbytes = fbbits / 8;
9020fe46dc6SMatthew Dillon 	/*
9030fe46dc6SMatthew Dillon 	 * do the transformation
9040fe46dc6SMatthew Dillon 	 */
9050fe46dc6SMatthew Dillon 	for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
9060fe46dc6SMatthew Dillon 		MEMCPY(msgbuf, ivec, 8);
9070fe46dc6SMatthew Dillon 		DES_XFORM(&msgbuf);
9080fe46dc6SMatthew Dillon 		for (c = 0; c < 8 - nbytes; c++)
9090fe46dc6SMatthew Dillon 			ivec[c] = ivec[c + nbytes];
9100fe46dc6SMatthew Dillon 		for (c = 0; c < nbytes; c++) {
9110fe46dc6SMatthew Dillon 			ivec[8 - nbytes + c] = msgbuf[c];
9120fe46dc6SMatthew Dillon 			obuf[c] = ibuf[c] ^ msgbuf[c];
9130fe46dc6SMatthew Dillon 		}
9140fe46dc6SMatthew Dillon 		/*
9150fe46dc6SMatthew Dillon 		 * if the last one, handle it specially
9160fe46dc6SMatthew Dillon 		 */
9170fe46dc6SMatthew Dillon 		if ((c = getchar()) == EOF) {
9180fe46dc6SMatthew Dillon 			n = obuf[nbytes-1];
9190fe46dc6SMatthew Dillon 			if (n < 0 || n > nbytes-1)
9200fe46dc6SMatthew Dillon 				warnx("decryption failed (block corrupt) at %d",
9210fe46dc6SMatthew Dillon 				    bn);
9220fe46dc6SMatthew Dillon 		}
9230fe46dc6SMatthew Dillon 		else
9240fe46dc6SMatthew Dillon 			(void)ungetc(c, stdin);
9250fe46dc6SMatthew Dillon 		/*
9260fe46dc6SMatthew Dillon 		 * dump it
9270fe46dc6SMatthew Dillon 		 */
9280fe46dc6SMatthew Dillon 		WRITE(obuf, n);
9290fe46dc6SMatthew Dillon 	}
9300fe46dc6SMatthew Dillon 	if (n > 0)
9310fe46dc6SMatthew Dillon 		warnx("decryption failed (incomplete block) at %d", bn);
9320fe46dc6SMatthew Dillon }
9330fe46dc6SMatthew Dillon 
9340fe46dc6SMatthew Dillon /*
9350fe46dc6SMatthew Dillon  * This authenticates using the Cipher FeedBack mode of DES
9360fe46dc6SMatthew Dillon  */
9370fe46dc6SMatthew Dillon static void
cfbauth(void)9380fe46dc6SMatthew Dillon cfbauth(void)
9390fe46dc6SMatthew Dillon {
9400fe46dc6SMatthew Dillon 	int n, j;		/* number of bytes actually read */
9410fe46dc6SMatthew Dillon 	int nbytes;		/* number of bytes to read */
9420fe46dc6SMatthew Dillon 	char ibuf[8];		/* input buffer */
9430fe46dc6SMatthew Dillon 	DES_cblock msgbuf;	/* encryption buffer */
9440fe46dc6SMatthew Dillon 
9450fe46dc6SMatthew Dillon 	/*
9460fe46dc6SMatthew Dillon 	 * do things in bytes, not bits
9470fe46dc6SMatthew Dillon 	 */
9480fe46dc6SMatthew Dillon 	nbytes = fbbits / 8;
9490fe46dc6SMatthew Dillon 	/*
9500fe46dc6SMatthew Dillon 	 * do the transformation
9510fe46dc6SMatthew Dillon 	 */
9520fe46dc6SMatthew Dillon 	while ((n = READ(ibuf, nbytes)) == nbytes) {
9530fe46dc6SMatthew Dillon 		MEMCPY(msgbuf, ivec, 8);
9540fe46dc6SMatthew Dillon 		DES_XFORM(&msgbuf);
9550fe46dc6SMatthew Dillon 		for (n = 0; n < 8 - nbytes; n++)
9560fe46dc6SMatthew Dillon 			ivec[n] = ivec[n + nbytes];
9570fe46dc6SMatthew Dillon 		for (n = 0; n < nbytes; n++)
9580fe46dc6SMatthew Dillon 			ivec[8 - nbytes + n] = ibuf[n] ^ msgbuf[n];
9590fe46dc6SMatthew Dillon 	}
9600fe46dc6SMatthew Dillon 	/*
9610fe46dc6SMatthew Dillon 	 * at EOF or last block -- in either case, the last byte contains
9620fe46dc6SMatthew Dillon 	 * the character representation of the number of bytes in it
9630fe46dc6SMatthew Dillon 	 */
9640fe46dc6SMatthew Dillon 	MEMZERO(&ibuf[n], nbytes - n);
9650fe46dc6SMatthew Dillon 	ibuf[nbytes - 1] = '0' + n;
9660fe46dc6SMatthew Dillon 	MEMCPY(msgbuf, ivec, 8);
9670fe46dc6SMatthew Dillon 	DES_XFORM(&msgbuf);
9680fe46dc6SMatthew Dillon 	for (n = 0; n < nbytes; n++)
9690fe46dc6SMatthew Dillon 		ibuf[n] ^= msgbuf[n];
9700fe46dc6SMatthew Dillon 	/*
9710fe46dc6SMatthew Dillon 	 * drop the bits
9720fe46dc6SMatthew Dillon 	 * we write chars until fewer than 7 bits,
9730fe46dc6SMatthew Dillon 	 * and then pad the last one with 0 bits
9740fe46dc6SMatthew Dillon 	 */
9750fe46dc6SMatthew Dillon 	for (n = 0; macbits > 7; n++, macbits -= 8)
9760fe46dc6SMatthew Dillon 		(void)putchar(msgbuf[n]);
9770fe46dc6SMatthew Dillon 	if (macbits > 0) {
9780fe46dc6SMatthew Dillon 		msgbuf[0] = 0x00;
9790fe46dc6SMatthew Dillon 		for (j = 0; j < macbits; j++)
9800fe46dc6SMatthew Dillon 			msgbuf[0] |= msgbuf[n] & bits[j];
9810fe46dc6SMatthew Dillon 		(void)putchar(msgbuf[0]);
9820fe46dc6SMatthew Dillon 	}
9830fe46dc6SMatthew Dillon }
9840fe46dc6SMatthew Dillon 
9850fe46dc6SMatthew Dillon /*
9860fe46dc6SMatthew Dillon  * message about usage
9870fe46dc6SMatthew Dillon  */
9880fe46dc6SMatthew Dillon static void
usage(void)9890fe46dc6SMatthew Dillon usage(void)
9900fe46dc6SMatthew Dillon {
9910fe46dc6SMatthew Dillon 	(void)fprintf(stderr, "%s\n",
9920fe46dc6SMatthew Dillon "usage: bdes [-abdp] [-F N] [-f N] [-k key] [-m N] [-o N] [-v vector]");
9930fe46dc6SMatthew Dillon 	exit(1);
9940fe46dc6SMatthew Dillon }
995