cbc.c revision 1057
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
411057Salmstatic char *rcsid = "@(#)$Id: cbc.c,v 1.3 1993/12/14 18:01:10 alm Exp $";
4216Salm#endif /* not lint */
4316Salm
441057Salm#include <sys/types.h>
451057Salm#include <ctype.h>
4616Salm#include <errno.h>
4716Salm#include <pwd.h>
4816Salm
4916Salm#include "ed.h"
5016Salm
511057Salm
5216Salm/*
5316Salm * Define a divisor for rand() that yields a uniform distribution in the
5416Salm * range 0-255.
5516Salm */
5616Salm#define	RAND_DIV (((unsigned) RAND_MAX + 1) >> 8)
5716Salm
5816Salm/*
5916Salm * BSD and System V systems offer special library calls that do
601057Salm * block move_liness and fills, so if possible we take advantage of them
6116Salm */
6216Salm#define	MEMCPY(dest,src,len)	memcpy((dest),(src),(len))
6316Salm#define	MEMZERO(dest,len)	memset((dest), 0, (len))
6416Salm
6516Salm/* Hide the calls to the primitive encryption routines. */
6616Salm#define	DES_KEY(buf) \
6716Salm	if (des_setkey(buf)) \
681057Salm		des_error("des_setkey");
6916Salm#define	DES_XFORM(buf) \
7016Salm	if (des_cipher(buf, buf, 0L, (inverse ? -1 : 1))) \
711057Salm		des_error("des_cipher");
7216Salm
7316Salm/*
7416Salm * read/write - no error checking
7516Salm */
7616Salm#define	READ(buf, n, fp)	fread(buf, sizeof(char), n, fp)
7716Salm#define WRITE(buf, n, fp)	fwrite(buf, sizeof(char), n, fp)
7816Salm
7916Salm/*
8016Salm * some things to make references easier
8116Salm */
8216Salmtypedef char Desbuf[8];
8316Salm#define	CHAR(x,i)	(x[i])
8416Salm#define	UCHAR(x,i)	(x[i])
8516Salm#define	BUFFER(x)	(x)
8616Salm#define	UBUFFER(x)	(x)
8716Salm
8816Salm/*
8916Salm * global variables and related macros
9016Salm */
9116Salm
9216Salmenum { 					/* encrypt, decrypt, authenticate */
9316Salm	MODE_ENCRYPT, MODE_DECRYPT, MODE_AUTHENTICATE
9416Salm} mode = MODE_ENCRYPT;
9516Salm
9616SalmDesbuf ivec;				/* initialization vector */
9716SalmDesbuf pvec;				/* padding vector */
9816Salmchar bits[] = {				/* used to extract bits from a char */
9916Salm	'\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
10016Salm};
10116Salmint pflag;				/* 1 to preserve parity bits */
10216Salm
1031057Salmunsigned char des_buf[8];	/* shared buffer for get_des_char/put_des_char */
1041057Salmint des_ct = 0;			/* count for get_des_char/put_des_char */
1051057Salmint des_n = 0;			/* index for put_des_char/get_des_char */
10616Salm
10716Salm
1081057Salm/* init_des_cipher: initialize DES */
10916Salmvoid
1101057Salminit_des_cipher()
11116Salm{
11216Salm#ifdef DES
11316Salm	int i;
11416Salm
11516Salm	des_ct = des_n = 0;
11616Salm
11716Salm	/* initialize the initialization vctor */
11816Salm	MEMZERO(ivec, 8);
11916Salm
12016Salm	/* intialize the padding vector */
12116Salm	srand((unsigned) time((time_t *) 0));
12216Salm	for (i = 0; i < 8; i++)
12316Salm		CHAR(pvec, i) = (char) (rand()/RAND_DIV);
12416Salm#endif
12516Salm}
12616Salm
12716Salm
1281057Salm/* get_des_char: return next char in an encrypted file */
1291057Salmint
1301057Salmget_des_char(fp)
13116Salm	FILE *fp;
13216Salm{
13316Salm#ifdef DES
13416Salm	if (des_n >= des_ct) {
13516Salm		des_n = 0;
1361057Salm		des_ct = cbc_decode(des_buf, fp);
13716Salm	}
13816Salm	return (des_ct > 0) ? des_buf[des_n++] : EOF;
13916Salm#endif
14016Salm}
14116Salm
14216Salm
1431057Salm/* put_des_char: write a char to an encrypted file; return char written */
1441057Salmint
1451057Salmput_des_char(c, fp)
14616Salm	int c;
14716Salm	FILE *fp;
14816Salm{
14916Salm#ifdef DES
15016Salm	if (des_n == sizeof des_buf) {
1511057Salm		des_ct = cbc_encode(des_buf, des_n, fp);
15216Salm		des_n = 0;
15316Salm	}
15416Salm	return (des_ct >= 0) ? (des_buf[des_n++] = c) : EOF;
15516Salm#endif
15616Salm}
15716Salm
15816Salm
1591057Salm/* flush_des_file: flush an encrypted file's output; return status */
1601057Salmint
1611057Salmflush_des_file(fp)
16216Salm	FILE *fp;
16316Salm{
16416Salm#ifdef DES
16516Salm	if (des_n == sizeof des_buf) {
1661057Salm		des_ct = cbc_encode(des_buf, des_n, fp);
16716Salm		des_n = 0;
16816Salm	}
1691057Salm	return (des_ct >= 0 && cbc_encode(des_buf, des_n, fp) >= 0) ? 0 : EOF;
17016Salm#endif
17116Salm}
17216Salm
17316Salm#ifdef DES
17416Salm/*
17516Salm * get keyword from tty or stdin
17616Salm */
1771057Salmint
1781057Salmget_keyword()
17916Salm{
18016Salm	register char *p;		/* used to obtain the key */
18116Salm	Desbuf msgbuf;			/* I/O buffer */
18216Salm
18316Salm	/*
18416Salm	 * get the key
18516Salm	 */
18616Salm	if (*(p = getpass("Enter key: "))) {
18716Salm
18816Salm		/*
18916Salm		 * copy it, nul-padded, into the key area
19016Salm		 */
1911057Salm		expand_des_key(BUFFER(msgbuf), p);
19216Salm		MEMZERO(p, _PASSWORD_LEN);
1931057Salm		set_des_key(msgbuf);
19416Salm		MEMZERO(msgbuf, sizeof msgbuf);
19516Salm		return 1;
19616Salm	}
19716Salm	return 0;
19816Salm}
19916Salm
20016Salm
20116Salm/*
20216Salm * print a warning message and, possibly, terminate
20316Salm */
20416Salmvoid
2051057Salmdes_error(s)
20616Salm	char *s;		/* the message */
20716Salm{
20816Salm	(void)sprintf(errmsg, "%s", s ? s : strerror(errno));
20916Salm}
21016Salm
21116Salm/*
21216Salm * map a hex character to an integer
21316Salm */
2141057Salmint
2151057Salmhex_to_binary(c, radix)
21616Salm	int c;			/* char to be converted */
21716Salm	int radix;		/* base (2 to 16) */
21816Salm{
21916Salm	switch(c) {
22016Salm	case '0':		return(0x0);
22116Salm	case '1':		return(0x1);
22216Salm	case '2':		return(radix > 2 ? 0x2 : -1);
22316Salm	case '3':		return(radix > 3 ? 0x3 : -1);
22416Salm	case '4':		return(radix > 4 ? 0x4 : -1);
22516Salm	case '5':		return(radix > 5 ? 0x5 : -1);
22616Salm	case '6':		return(radix > 6 ? 0x6 : -1);
22716Salm	case '7':		return(radix > 7 ? 0x7 : -1);
22816Salm	case '8':		return(radix > 8 ? 0x8 : -1);
22916Salm	case '9':		return(radix > 9 ? 0x9 : -1);
23016Salm	case 'A': case 'a':	return(radix > 10 ? 0xa : -1);
23116Salm	case 'B': case 'b':	return(radix > 11 ? 0xb : -1);
23216Salm	case 'C': case 'c':	return(radix > 12 ? 0xc : -1);
23316Salm	case 'D': case 'd':	return(radix > 13 ? 0xd : -1);
23416Salm	case 'E': case 'e':	return(radix > 14 ? 0xe : -1);
23516Salm	case 'F': case 'f':	return(radix > 15 ? 0xf : -1);
23616Salm	}
23716Salm	/*
23816Salm	 * invalid character
23916Salm	 */
24016Salm	return(-1);
24116Salm}
24216Salm
24316Salm/*
24416Salm * convert the key to a bit pattern
24516Salm */
24616Salmvoid
2471057Salmexpand_des_key(obuf, ibuf)
24816Salm	char *obuf;			/* bit pattern */
24916Salm	char *ibuf;			/* the key itself */
25016Salm{
25116Salm	register int i, j;		/* counter in a for loop */
25216Salm	int nbuf[64];			/* used for hex/key translation */
25316Salm
25416Salm	/*
25516Salm	 * leading '0x' or '0X' == hex key
25616Salm	 */
25716Salm	if (ibuf[0] == '0' && (ibuf[1] == 'x' || ibuf[1] == 'X')) {
25816Salm		ibuf = &ibuf[2];
25916Salm		/*
26016Salm		 * now translate it, bombing on any illegal hex digit
26116Salm		 */
26216Salm		for (i = 0; ibuf[i] && i < 16; i++)
2631057Salm			if ((nbuf[i] = hex_to_binary((int) ibuf[i], 16)) == -1)
2641057Salm				des_error("bad hex digit in key");
26516Salm		while (i < 16)
26616Salm			nbuf[i++] = 0;
26716Salm		for (i = 0; i < 8; i++)
26816Salm			obuf[i] =
26916Salm			    ((nbuf[2*i]&0xf)<<4) | (nbuf[2*i+1]&0xf);
27016Salm		/* preserve parity bits */
27116Salm		pflag = 1;
27216Salm		return;
27316Salm	}
27416Salm	/*
27516Salm	 * leading '0b' or '0B' == binary key
27616Salm	 */
27716Salm	if (ibuf[0] == '0' && (ibuf[1] == 'b' || ibuf[1] == 'B')) {
27816Salm		ibuf = &ibuf[2];
27916Salm		/*
28016Salm		 * now translate it, bombing on any illegal binary digit
28116Salm		 */
28216Salm		for (i = 0; ibuf[i] && i < 16; i++)
2831057Salm			if ((nbuf[i] = hex_to_binary((int) ibuf[i], 2)) == -1)
2841057Salm				des_error("bad binary digit in key");
28516Salm		while (i < 64)
28616Salm			nbuf[i++] = 0;
28716Salm		for (i = 0; i < 8; i++)
28816Salm			for (j = 0; j < 8; j++)
28916Salm				obuf[i] = (obuf[i]<<1)|nbuf[8*i+j];
29016Salm		/* preserve parity bits */
29116Salm		pflag = 1;
29216Salm		return;
29316Salm	}
29416Salm	/*
29516Salm	 * no special leader -- ASCII
29616Salm	 */
29716Salm	(void)strncpy(obuf, ibuf, 8);
29816Salm}
29916Salm
30016Salm/*****************
30116Salm * DES FUNCTIONS *
30216Salm *****************/
30316Salm/*
30416Salm * This sets the DES key and (if you're using the deszip version)
30516Salm * the direction of the transformation.  This uses the Sun
30616Salm * to map the 64-bit key onto the 56 bits that the key schedule
30716Salm * generation routines use: the old way, which just uses the user-
30816Salm * supplied 64 bits as is, and the new way, which resets the parity
30916Salm * bit to be the same as the low-order bit in each character.  The
31016Salm * new way generates a greater variety of key schedules, since many
31116Salm * systems set the parity (high) bit of each character to 0, and the
31216Salm * DES ignores the low order bit of each character.
31316Salm */
31416Salmvoid
3151057Salmset_des_key(buf)
31616Salm	Desbuf buf;				/* key block */
31716Salm{
31816Salm	register int i, j;			/* counter in a for loop */
31916Salm	register int par;			/* parity counter */
32016Salm
32116Salm	/*
32216Salm	 * if the parity is not preserved, flip it
32316Salm	 */
32416Salm	if (!pflag) {
32516Salm		for (i = 0; i < 8; i++) {
32616Salm			par = 0;
32716Salm			for (j = 1; j < 8; j++)
32816Salm				if ((bits[j]&UCHAR(buf, i)) != 0)
32916Salm					par++;
33016Salm			if ((par&01) == 01)
33116Salm				UCHAR(buf, i) = UCHAR(buf, i)&0177;
33216Salm			else
33316Salm				UCHAR(buf, i) = (UCHAR(buf, i)&0177)|0200;
33416Salm		}
33516Salm	}
33616Salm
33716Salm	DES_KEY(UBUFFER(buf));
33816Salm}
33916Salm
34016Salm
34116Salm/*
34216Salm * This encrypts using the Cipher Block Chaining mode of DES
34316Salm */
3441057Salmint
3451057Salmcbc_encode(msgbuf, n, fp)
34616Salm	char *msgbuf;
34716Salm	int n;
34816Salm	FILE *fp;
34916Salm{
35016Salm	int inverse = 0;	/* 0 to encrypt, 1 to decrypt */
35116Salm
35216Salm	/*
35316Salm	 * do the transformation
35416Salm	 */
35516Salm	if (n == 8) {
35616Salm		for (n = 0; n < 8; n++)
35716Salm			CHAR(msgbuf, n) ^= CHAR(ivec, n);
35816Salm		DES_XFORM(UBUFFER(msgbuf));
35916Salm		MEMCPY(BUFFER(ivec), BUFFER(msgbuf), 8);
36016Salm		return WRITE(BUFFER(msgbuf), 8, fp);
36116Salm	}
36216Salm	/*
36316Salm	 * at EOF or last block -- in either case, the last byte contains
36416Salm	 * the character representation of the number of bytes in it
36516Salm	 */
36616Salm/*
36716Salm	MEMZERO(msgbuf +  n, 8 - n);
36816Salm*/
36916Salm	/*
37016Salm	 *  Pad the last block randomly
37116Salm	 */
37216Salm	(void)MEMCPY(BUFFER(msgbuf + n), BUFFER(pvec), 8 - n);
37316Salm	CHAR(msgbuf, 7) = n;
37416Salm	for (n = 0; n < 8; n++)
37516Salm		CHAR(msgbuf, n) ^= CHAR(ivec, n);
37616Salm	DES_XFORM(UBUFFER(msgbuf));
37716Salm	return WRITE(BUFFER(msgbuf), 8, fp);
37816Salm}
37916Salm
38016Salm/*
38116Salm * This decrypts using the Cipher Block Chaining mode of DES
38216Salm */
3831057Salmint
3841057Salmcbc_decode(msgbuf, fp)
38516Salm	char *msgbuf;		/* I/O buffer */
38616Salm	FILE *fp;			/* input file descriptor */
38716Salm{
38816Salm	Desbuf ibuf;	/* temp buffer for initialization vector */
38916Salm	register int n;		/* number of bytes actually read */
39016Salm	register int c;		/* used to test for EOF */
39116Salm	int inverse = 1;	/* 0 to encrypt, 1 to decrypt */
39216Salm
39316Salm	if ((n = READ(BUFFER(msgbuf), 8, fp)) == 8) {
39416Salm		/*
39516Salm		 * do the transformation
39616Salm		 */
39716Salm		MEMCPY(BUFFER(ibuf), BUFFER(msgbuf), 8);
39816Salm		DES_XFORM(UBUFFER(msgbuf));
39916Salm		for (c = 0; c < 8; c++)
40016Salm			UCHAR(msgbuf, c) ^= UCHAR(ivec, c);
40116Salm		MEMCPY(BUFFER(ivec), BUFFER(ibuf), 8);
40216Salm		/*
40316Salm		 * if the last one, handle it specially
40416Salm		 */
40516Salm		if ((c = fgetc(fp)) == EOF) {
40616Salm			n = CHAR(msgbuf, 7);
40716Salm			if (n < 0 || n > 7) {
4081057Salm				des_error("decryption failed (block corrupted)");
40916Salm				return EOF;
41016Salm			}
41116Salm		} else
41216Salm			(void)ungetc(c, fp);
41316Salm		return n;
41416Salm	}
41516Salm	if (n > 0)
4161057Salm		des_error("decryption failed (incomplete block)");
41716Salm	else if (n < 0)
4181057Salm		des_error("cannot read file");
41916Salm	return EOF;
42016Salm}
42116Salm#endif	/* DES */
422