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