cbc.c revision 27963
116Salm/* cbc.c: This file contains the encryption routines for the ed line editor */
216Salm/*-
31057Salm * Copyright (c) 1993 The Regents of the University of California.
416Salm * All rights reserved.
516Salm *
61057Salm * Copyright (c) 1993 Andrew Moore, Talke Studio.
71057Salm * All rights reserved.
816Salm *
916Salm * Redistribution and use in source and binary forms, with or without
1016Salm * modification, are permitted provided that the following conditions
1116Salm * are met:
1216Salm * 1. Redistributions of source code must retain the above copyright
1316Salm *    notice, this list of conditions and the following disclaimer.
1416Salm * 2. Redistributions in binary form must reproduce the above copyright
1516Salm *    notice, this list of conditions and the following disclaimer in the
1616Salm *    documentation and/or other materials provided with the distribution.
1716Salm * 3. All advertising materials mentioning features or use of this software
1816Salm *    must display the following acknowledgement:
1916Salm *	This product includes software developed by the University of
2016Salm *	California, Berkeley and its contributors.
2116Salm * 4. Neither the name of the University nor the names of its contributors
2216Salm *    may be used to endorse or promote products derived from this software
2316Salm *    without specific prior written permission.
2416Salm *
2516Salm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2616Salm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2716Salm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2816Salm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2916Salm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3016Salm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3116Salm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3216Salm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3316Salm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3416Salm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3516Salm * SUCH DAMAGE.
361057Salm *
371057Salm *	from: @(#)bdes.c	5.5 (Berkeley) 6/27/91
3816Salm */
3916Salm
4016Salm#ifndef lint
4127963Ssteve#if 0
4220420Sstevestatic char * const rcsid = "@(#)cbc.c,v 1.2 1994/02/01 00:34:36 alm Exp";
4327963Ssteve#else
4427963Sstevestatic char * const rcsid =
4527963Ssteve	"$Id: cbc.c,v 1.9 1997/02/22 14:03:12 peter Exp $";
4627963Ssteve#endif
4716Salm#endif /* not lint */
4816Salm
491057Salm#include <sys/types.h>
5016Salm#include <errno.h>
5116Salm#include <pwd.h>
5227963Ssteve#ifdef DES
5327963Ssteve#include <time.h>
5427963Ssteve#endif
5516Salm
5616Salm#include "ed.h"
5716Salm
581057Salm
5916Salm/*
6016Salm * Define a divisor for rand() that yields a uniform distribution in the
6116Salm * range 0-255.
6216Salm */
6316Salm#define	RAND_DIV (((unsigned) RAND_MAX + 1) >> 8)
6416Salm
6516Salm/*
6616Salm * BSD and System V systems offer special library calls that do
671057Salm * block move_liness and fills, so if possible we take advantage of them
6816Salm */
6916Salm#define	MEMCPY(dest,src,len)	memcpy((dest),(src),(len))
7016Salm#define	MEMZERO(dest,len)	memset((dest), 0, (len))
7116Salm
7216Salm/* Hide the calls to the primitive encryption routines. */
7316Salm#define	DES_KEY(buf) \
7416Salm	if (des_setkey(buf)) \
751057Salm		des_error("des_setkey");
7616Salm#define	DES_XFORM(buf) \
7716Salm	if (des_cipher(buf, buf, 0L, (inverse ? -1 : 1))) \
781057Salm		des_error("des_cipher");
7916Salm
8016Salm/*
8116Salm * read/write - no error checking
8216Salm */
8316Salm#define	READ(buf, n, fp)	fread(buf, sizeof(char), n, fp)
8416Salm#define WRITE(buf, n, fp)	fwrite(buf, sizeof(char), n, fp)
8516Salm
8616Salm/*
8716Salm * some things to make references easier
8816Salm */
8916Salmtypedef char Desbuf[8];
9016Salm#define	CHAR(x,i)	(x[i])
9116Salm#define	UCHAR(x,i)	(x[i])
9216Salm#define	BUFFER(x)	(x)
9316Salm#define	UBUFFER(x)	(x)
9416Salm
9516Salm/*
9616Salm * global variables and related macros
9716Salm */
9816Salm
9916Salmenum { 					/* encrypt, decrypt, authenticate */
10016Salm	MODE_ENCRYPT, MODE_DECRYPT, MODE_AUTHENTICATE
10116Salm} mode = MODE_ENCRYPT;
10216Salm
10316SalmDesbuf ivec;				/* initialization vector */
10416SalmDesbuf pvec;				/* padding vector */
10516Salmchar bits[] = {				/* used to extract bits from a char */
10616Salm	'\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
10716Salm};
10816Salmint pflag;				/* 1 to preserve parity bits */
10916Salm
1101057Salmunsigned char des_buf[8];	/* shared buffer for get_des_char/put_des_char */
1111057Salmint des_ct = 0;			/* count for get_des_char/put_des_char */
1121057Salmint des_n = 0;			/* index for put_des_char/get_des_char */
11316Salm
11416Salm
1151057Salm/* init_des_cipher: initialize DES */
11616Salmvoid
1171057Salminit_des_cipher()
11816Salm{
11916Salm#ifdef DES
12016Salm	int i;
12116Salm
12216Salm	des_ct = des_n = 0;
12316Salm
1247165Sjoerg	/* initialize the initialization vector */
12516Salm	MEMZERO(ivec, 8);
12616Salm
12716Salm	/* intialize the padding vector */
12816Salm	srand((unsigned) time((time_t *) 0));
12916Salm	for (i = 0; i < 8; i++)
13016Salm		CHAR(pvec, i) = (char) (rand()/RAND_DIV);
13116Salm#endif
13216Salm}
13316Salm
13416Salm
1351057Salm/* get_des_char: return next char in an encrypted file */
1361057Salmint
1371057Salmget_des_char(fp)
13816Salm	FILE *fp;
13916Salm{
14016Salm#ifdef DES
14116Salm	if (des_n >= des_ct) {
14216Salm		des_n = 0;
1431057Salm		des_ct = cbc_decode(des_buf, fp);
14416Salm	}
14516Salm	return (des_ct > 0) ? des_buf[des_n++] : EOF;
1467165Sjoerg#else
1477165Sjoerg	return (getc(fp));
14816Salm#endif
14916Salm}
15016Salm
15116Salm
1521057Salm/* put_des_char: write a char to an encrypted file; return char written */
1531057Salmint
1541057Salmput_des_char(c, fp)
15516Salm	int c;
15616Salm	FILE *fp;
15716Salm{
15816Salm#ifdef DES
15916Salm	if (des_n == sizeof des_buf) {
1601057Salm		des_ct = cbc_encode(des_buf, des_n, fp);
16116Salm		des_n = 0;
16216Salm	}
16316Salm	return (des_ct >= 0) ? (des_buf[des_n++] = c) : EOF;
1647165Sjoerg#else
1657165Sjoerg	return (fputc(c, fp));
16616Salm#endif
16716Salm}
16816Salm
16916Salm
1701057Salm/* flush_des_file: flush an encrypted file's output; return status */
1711057Salmint
1721057Salmflush_des_file(fp)
17316Salm	FILE *fp;
17416Salm{
17516Salm#ifdef DES
17616Salm	if (des_n == sizeof des_buf) {
1771057Salm		des_ct = cbc_encode(des_buf, des_n, fp);
17816Salm		des_n = 0;
17916Salm	}
1801057Salm	return (des_ct >= 0 && cbc_encode(des_buf, des_n, fp) >= 0) ? 0 : EOF;
1817165Sjoerg#else
1827165Sjoerg	return (fflush(fp));
18316Salm#endif
18416Salm}
18516Salm
18616Salm#ifdef DES
18716Salm/*
18816Salm * get keyword from tty or stdin
18916Salm */
1901057Salmint
1911057Salmget_keyword()
19216Salm{
19316Salm	register char *p;		/* used to obtain the key */
19416Salm	Desbuf msgbuf;			/* I/O buffer */
19516Salm
19616Salm	/*
19716Salm	 * get the key
19816Salm	 */
19916Salm	if (*(p = getpass("Enter key: "))) {
20016Salm
20116Salm		/*
20216Salm		 * copy it, nul-padded, into the key area
20316Salm		 */
2041057Salm		expand_des_key(BUFFER(msgbuf), p);
20516Salm		MEMZERO(p, _PASSWORD_LEN);
2061057Salm		set_des_key(msgbuf);
20716Salm		MEMZERO(msgbuf, sizeof msgbuf);
20816Salm		return 1;
20916Salm	}
21016Salm	return 0;
21116Salm}
21216Salm
21316Salm
21416Salm/*
21516Salm * print a warning message and, possibly, terminate
21616Salm */
21716Salmvoid
2181057Salmdes_error(s)
21916Salm	char *s;		/* the message */
22016Salm{
22116Salm	(void)sprintf(errmsg, "%s", s ? s : strerror(errno));
22216Salm}
22316Salm
22416Salm/*
22516Salm * map a hex character to an integer
22616Salm */
2271057Salmint
2281057Salmhex_to_binary(c, radix)
22916Salm	int c;			/* char to be converted */
23016Salm	int radix;		/* base (2 to 16) */
23116Salm{
23216Salm	switch(c) {
23316Salm	case '0':		return(0x0);
23416Salm	case '1':		return(0x1);
23516Salm	case '2':		return(radix > 2 ? 0x2 : -1);
23616Salm	case '3':		return(radix > 3 ? 0x3 : -1);
23716Salm	case '4':		return(radix > 4 ? 0x4 : -1);
23816Salm	case '5':		return(radix > 5 ? 0x5 : -1);
23916Salm	case '6':		return(radix > 6 ? 0x6 : -1);
24016Salm	case '7':		return(radix > 7 ? 0x7 : -1);
24116Salm	case '8':		return(radix > 8 ? 0x8 : -1);
24216Salm	case '9':		return(radix > 9 ? 0x9 : -1);
24316Salm	case 'A': case 'a':	return(radix > 10 ? 0xa : -1);
24416Salm	case 'B': case 'b':	return(radix > 11 ? 0xb : -1);
24516Salm	case 'C': case 'c':	return(radix > 12 ? 0xc : -1);
24616Salm	case 'D': case 'd':	return(radix > 13 ? 0xd : -1);
24716Salm	case 'E': case 'e':	return(radix > 14 ? 0xe : -1);
24816Salm	case 'F': case 'f':	return(radix > 15 ? 0xf : -1);
24916Salm	}
25016Salm	/*
25116Salm	 * invalid character
25216Salm	 */
25316Salm	return(-1);
25416Salm}
25516Salm
25616Salm/*
25716Salm * convert the key to a bit pattern
25816Salm */
25916Salmvoid
2601057Salmexpand_des_key(obuf, ibuf)
26116Salm	char *obuf;			/* bit pattern */
26216Salm	char *ibuf;			/* the key itself */
26316Salm{
26416Salm	register int i, j;		/* counter in a for loop */
26516Salm	int nbuf[64];			/* used for hex/key translation */
26616Salm
26716Salm	/*
26816Salm	 * leading '0x' or '0X' == hex key
26916Salm	 */
27016Salm	if (ibuf[0] == '0' && (ibuf[1] == 'x' || ibuf[1] == 'X')) {
27116Salm		ibuf = &ibuf[2];
27216Salm		/*
27316Salm		 * now translate it, bombing on any illegal hex digit
27416Salm		 */
27516Salm		for (i = 0; ibuf[i] && i < 16; i++)
2761057Salm			if ((nbuf[i] = hex_to_binary((int) ibuf[i], 16)) == -1)
2771057Salm				des_error("bad hex digit in key");
27816Salm		while (i < 16)
27916Salm			nbuf[i++] = 0;
28016Salm		for (i = 0; i < 8; i++)
28116Salm			obuf[i] =
28216Salm			    ((nbuf[2*i]&0xf)<<4) | (nbuf[2*i+1]&0xf);
28316Salm		/* preserve parity bits */
28416Salm		pflag = 1;
28516Salm		return;
28616Salm	}
28716Salm	/*
28816Salm	 * leading '0b' or '0B' == binary key
28916Salm	 */
29016Salm	if (ibuf[0] == '0' && (ibuf[1] == 'b' || ibuf[1] == 'B')) {
29116Salm		ibuf = &ibuf[2];
29216Salm		/*
29316Salm		 * now translate it, bombing on any illegal binary digit
29416Salm		 */
29516Salm		for (i = 0; ibuf[i] && i < 16; i++)
2961057Salm			if ((nbuf[i] = hex_to_binary((int) ibuf[i], 2)) == -1)
2971057Salm				des_error("bad binary digit in key");
29816Salm		while (i < 64)
29916Salm			nbuf[i++] = 0;
30016Salm		for (i = 0; i < 8; i++)
30116Salm			for (j = 0; j < 8; j++)
30216Salm				obuf[i] = (obuf[i]<<1)|nbuf[8*i+j];
30316Salm		/* preserve parity bits */
30416Salm		pflag = 1;
30516Salm		return;
30616Salm	}
30716Salm	/*
30816Salm	 * no special leader -- ASCII
30916Salm	 */
31016Salm	(void)strncpy(obuf, ibuf, 8);
31116Salm}
31216Salm
31316Salm/*****************
31416Salm * DES FUNCTIONS *
31516Salm *****************/
31616Salm/*
31716Salm * This sets the DES key and (if you're using the deszip version)
31816Salm * the direction of the transformation.  This uses the Sun
31916Salm * to map the 64-bit key onto the 56 bits that the key schedule
32016Salm * generation routines use: the old way, which just uses the user-
32116Salm * supplied 64 bits as is, and the new way, which resets the parity
32216Salm * bit to be the same as the low-order bit in each character.  The
32316Salm * new way generates a greater variety of key schedules, since many
32416Salm * systems set the parity (high) bit of each character to 0, and the
32516Salm * DES ignores the low order bit of each character.
32616Salm */
32716Salmvoid
3281057Salmset_des_key(buf)
32916Salm	Desbuf buf;				/* key block */
33016Salm{
33116Salm	register int i, j;			/* counter in a for loop */
33216Salm	register int par;			/* parity counter */
33316Salm
33416Salm	/*
33516Salm	 * if the parity is not preserved, flip it
33616Salm	 */
33716Salm	if (!pflag) {
33816Salm		for (i = 0; i < 8; i++) {
33916Salm			par = 0;
34016Salm			for (j = 1; j < 8; j++)
34116Salm				if ((bits[j]&UCHAR(buf, i)) != 0)
34216Salm					par++;
34316Salm			if ((par&01) == 01)
34416Salm				UCHAR(buf, i) = UCHAR(buf, i)&0177;
34516Salm			else
34616Salm				UCHAR(buf, i) = (UCHAR(buf, i)&0177)|0200;
34716Salm		}
34816Salm	}
34916Salm
35016Salm	DES_KEY(UBUFFER(buf));
35116Salm}
35216Salm
35316Salm
35416Salm/*
35516Salm * This encrypts using the Cipher Block Chaining mode of DES
35616Salm */
3571057Salmint
3581057Salmcbc_encode(msgbuf, n, fp)
35916Salm	char *msgbuf;
36016Salm	int n;
36116Salm	FILE *fp;
36216Salm{
36316Salm	int inverse = 0;	/* 0 to encrypt, 1 to decrypt */
36416Salm
36516Salm	/*
36616Salm	 * do the transformation
36716Salm	 */
36816Salm	if (n == 8) {
36916Salm		for (n = 0; n < 8; n++)
37016Salm			CHAR(msgbuf, n) ^= CHAR(ivec, n);
37116Salm		DES_XFORM(UBUFFER(msgbuf));
37216Salm		MEMCPY(BUFFER(ivec), BUFFER(msgbuf), 8);
37316Salm		return WRITE(BUFFER(msgbuf), 8, fp);
37416Salm	}
37516Salm	/*
37616Salm	 * at EOF or last block -- in either case, the last byte contains
37716Salm	 * the character representation of the number of bytes in it
37816Salm	 */
37916Salm/*
38016Salm	MEMZERO(msgbuf +  n, 8 - n);
38116Salm*/
38216Salm	/*
38316Salm	 *  Pad the last block randomly
38416Salm	 */
38516Salm	(void)MEMCPY(BUFFER(msgbuf + n), BUFFER(pvec), 8 - n);
38616Salm	CHAR(msgbuf, 7) = n;
38716Salm	for (n = 0; n < 8; n++)
38816Salm		CHAR(msgbuf, n) ^= CHAR(ivec, n);
38916Salm	DES_XFORM(UBUFFER(msgbuf));
39016Salm	return WRITE(BUFFER(msgbuf), 8, fp);
39116Salm}
39216Salm
39316Salm/*
39416Salm * This decrypts using the Cipher Block Chaining mode of DES
39516Salm */
3961057Salmint
3971057Salmcbc_decode(msgbuf, fp)
39816Salm	char *msgbuf;		/* I/O buffer */
39916Salm	FILE *fp;			/* input file descriptor */
40016Salm{
40116Salm	Desbuf ibuf;	/* temp buffer for initialization vector */
40216Salm	register int n;		/* number of bytes actually read */
40316Salm	register int c;		/* used to test for EOF */
40416Salm	int inverse = 1;	/* 0 to encrypt, 1 to decrypt */
40516Salm
40616Salm	if ((n = READ(BUFFER(msgbuf), 8, fp)) == 8) {
40716Salm		/*
40816Salm		 * do the transformation
40916Salm		 */
41016Salm		MEMCPY(BUFFER(ibuf), BUFFER(msgbuf), 8);
41116Salm		DES_XFORM(UBUFFER(msgbuf));
41216Salm		for (c = 0; c < 8; c++)
41316Salm			UCHAR(msgbuf, c) ^= UCHAR(ivec, c);
41416Salm		MEMCPY(BUFFER(ivec), BUFFER(ibuf), 8);
41516Salm		/*
41616Salm		 * if the last one, handle it specially
41716Salm		 */
41816Salm		if ((c = fgetc(fp)) == EOF) {
41916Salm			n = CHAR(msgbuf, 7);
42016Salm			if (n < 0 || n > 7) {
4211057Salm				des_error("decryption failed (block corrupted)");
42216Salm				return EOF;
42316Salm			}
42416Salm		} else
42516Salm			(void)ungetc(c, fp);
42616Salm		return n;
42716Salm	}
42816Salm	if (n > 0)
4291057Salm		des_error("decryption failed (incomplete block)");
43016Salm	else if (n < 0)
4311057Salm		des_error("cannot read file");
43216Salm	return EOF;
43316Salm}
43416Salm#endif	/* DES */
435