cbc.c revision 7165
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
387165Sjoerg *	$Id: cbc.c,v 1.4 1994/09/24 02:55:24 davidg Exp $
3916Salm */
4016Salm
4116Salm#ifndef lint
421297Salmstatic char *rcsid = "@(#)cbc.c,v 1.2 1994/02/01 00:34:36 alm Exp";
4316Salm#endif /* not lint */
4416Salm
451057Salm#include <sys/types.h>
461057Salm#include <ctype.h>
4716Salm#include <errno.h>
4816Salm#include <pwd.h>
4916Salm
5016Salm#include "ed.h"
5116Salm
521057Salm
5316Salm/*
5416Salm * Define a divisor for rand() that yields a uniform distribution in the
5516Salm * range 0-255.
5616Salm */
5716Salm#define	RAND_DIV (((unsigned) RAND_MAX + 1) >> 8)
5816Salm
5916Salm/*
6016Salm * BSD and System V systems offer special library calls that do
611057Salm * block move_liness and fills, so if possible we take advantage of them
6216Salm */
6316Salm#define	MEMCPY(dest,src,len)	memcpy((dest),(src),(len))
6416Salm#define	MEMZERO(dest,len)	memset((dest), 0, (len))
6516Salm
6616Salm/* Hide the calls to the primitive encryption routines. */
6716Salm#define	DES_KEY(buf) \
6816Salm	if (des_setkey(buf)) \
691057Salm		des_error("des_setkey");
7016Salm#define	DES_XFORM(buf) \
7116Salm	if (des_cipher(buf, buf, 0L, (inverse ? -1 : 1))) \
721057Salm		des_error("des_cipher");
7316Salm
7416Salm/*
7516Salm * read/write - no error checking
7616Salm */
7716Salm#define	READ(buf, n, fp)	fread(buf, sizeof(char), n, fp)
7816Salm#define WRITE(buf, n, fp)	fwrite(buf, sizeof(char), n, fp)
7916Salm
8016Salm/*
8116Salm * some things to make references easier
8216Salm */
8316Salmtypedef char Desbuf[8];
8416Salm#define	CHAR(x,i)	(x[i])
8516Salm#define	UCHAR(x,i)	(x[i])
8616Salm#define	BUFFER(x)	(x)
8716Salm#define	UBUFFER(x)	(x)
8816Salm
8916Salm/*
9016Salm * global variables and related macros
9116Salm */
9216Salm
9316Salmenum { 					/* encrypt, decrypt, authenticate */
9416Salm	MODE_ENCRYPT, MODE_DECRYPT, MODE_AUTHENTICATE
9516Salm} mode = MODE_ENCRYPT;
9616Salm
9716SalmDesbuf ivec;				/* initialization vector */
9816SalmDesbuf pvec;				/* padding vector */
9916Salmchar bits[] = {				/* used to extract bits from a char */
10016Salm	'\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
10116Salm};
10216Salmint pflag;				/* 1 to preserve parity bits */
10316Salm
1041057Salmunsigned char des_buf[8];	/* shared buffer for get_des_char/put_des_char */
1051057Salmint des_ct = 0;			/* count for get_des_char/put_des_char */
1061057Salmint des_n = 0;			/* index for put_des_char/get_des_char */
10716Salm
10816Salm
1091057Salm/* init_des_cipher: initialize DES */
11016Salmvoid
1111057Salminit_des_cipher()
11216Salm{
11316Salm#ifdef DES
11416Salm	int i;
11516Salm
11616Salm	des_ct = des_n = 0;
11716Salm
1187165Sjoerg	/* initialize the initialization vector */
11916Salm	MEMZERO(ivec, 8);
12016Salm
12116Salm	/* intialize the padding vector */
12216Salm	srand((unsigned) time((time_t *) 0));
12316Salm	for (i = 0; i < 8; i++)
12416Salm		CHAR(pvec, i) = (char) (rand()/RAND_DIV);
12516Salm#endif
12616Salm}
12716Salm
12816Salm
1291057Salm/* get_des_char: return next char in an encrypted file */
1301057Salmint
1311057Salmget_des_char(fp)
13216Salm	FILE *fp;
13316Salm{
13416Salm#ifdef DES
13516Salm	if (des_n >= des_ct) {
13616Salm		des_n = 0;
1371057Salm		des_ct = cbc_decode(des_buf, fp);
13816Salm	}
13916Salm	return (des_ct > 0) ? des_buf[des_n++] : EOF;
1407165Sjoerg#else
1417165Sjoerg	return (getc(fp));
14216Salm#endif
14316Salm}
14416Salm
14516Salm
1461057Salm/* put_des_char: write a char to an encrypted file; return char written */
1471057Salmint
1481057Salmput_des_char(c, fp)
14916Salm	int c;
15016Salm	FILE *fp;
15116Salm{
15216Salm#ifdef DES
15316Salm	if (des_n == sizeof des_buf) {
1541057Salm		des_ct = cbc_encode(des_buf, des_n, fp);
15516Salm		des_n = 0;
15616Salm	}
15716Salm	return (des_ct >= 0) ? (des_buf[des_n++] = c) : EOF;
1587165Sjoerg#else
1597165Sjoerg	return (fputc(c, fp));
16016Salm#endif
16116Salm}
16216Salm
16316Salm
1641057Salm/* flush_des_file: flush an encrypted file's output; return status */
1651057Salmint
1661057Salmflush_des_file(fp)
16716Salm	FILE *fp;
16816Salm{
16916Salm#ifdef DES
17016Salm	if (des_n == sizeof des_buf) {
1711057Salm		des_ct = cbc_encode(des_buf, des_n, fp);
17216Salm		des_n = 0;
17316Salm	}
1741057Salm	return (des_ct >= 0 && cbc_encode(des_buf, des_n, fp) >= 0) ? 0 : EOF;
1757165Sjoerg#else
1767165Sjoerg	return (fflush(fp));
17716Salm#endif
17816Salm}
17916Salm
18016Salm#ifdef DES
18116Salm/*
18216Salm * get keyword from tty or stdin
18316Salm */
1841057Salmint
1851057Salmget_keyword()
18616Salm{
18716Salm	register char *p;		/* used to obtain the key */
18816Salm	Desbuf msgbuf;			/* I/O buffer */
18916Salm
19016Salm	/*
19116Salm	 * get the key
19216Salm	 */
19316Salm	if (*(p = getpass("Enter key: "))) {
19416Salm
19516Salm		/*
19616Salm		 * copy it, nul-padded, into the key area
19716Salm		 */
1981057Salm		expand_des_key(BUFFER(msgbuf), p);
19916Salm		MEMZERO(p, _PASSWORD_LEN);
2001057Salm		set_des_key(msgbuf);
20116Salm		MEMZERO(msgbuf, sizeof msgbuf);
20216Salm		return 1;
20316Salm	}
20416Salm	return 0;
20516Salm}
20616Salm
20716Salm
20816Salm/*
20916Salm * print a warning message and, possibly, terminate
21016Salm */
21116Salmvoid
2121057Salmdes_error(s)
21316Salm	char *s;		/* the message */
21416Salm{
21516Salm	(void)sprintf(errmsg, "%s", s ? s : strerror(errno));
21616Salm}
21716Salm
21816Salm/*
21916Salm * map a hex character to an integer
22016Salm */
2211057Salmint
2221057Salmhex_to_binary(c, radix)
22316Salm	int c;			/* char to be converted */
22416Salm	int radix;		/* base (2 to 16) */
22516Salm{
22616Salm	switch(c) {
22716Salm	case '0':		return(0x0);
22816Salm	case '1':		return(0x1);
22916Salm	case '2':		return(radix > 2 ? 0x2 : -1);
23016Salm	case '3':		return(radix > 3 ? 0x3 : -1);
23116Salm	case '4':		return(radix > 4 ? 0x4 : -1);
23216Salm	case '5':		return(radix > 5 ? 0x5 : -1);
23316Salm	case '6':		return(radix > 6 ? 0x6 : -1);
23416Salm	case '7':		return(radix > 7 ? 0x7 : -1);
23516Salm	case '8':		return(radix > 8 ? 0x8 : -1);
23616Salm	case '9':		return(radix > 9 ? 0x9 : -1);
23716Salm	case 'A': case 'a':	return(radix > 10 ? 0xa : -1);
23816Salm	case 'B': case 'b':	return(radix > 11 ? 0xb : -1);
23916Salm	case 'C': case 'c':	return(radix > 12 ? 0xc : -1);
24016Salm	case 'D': case 'd':	return(radix > 13 ? 0xd : -1);
24116Salm	case 'E': case 'e':	return(radix > 14 ? 0xe : -1);
24216Salm	case 'F': case 'f':	return(radix > 15 ? 0xf : -1);
24316Salm	}
24416Salm	/*
24516Salm	 * invalid character
24616Salm	 */
24716Salm	return(-1);
24816Salm}
24916Salm
25016Salm/*
25116Salm * convert the key to a bit pattern
25216Salm */
25316Salmvoid
2541057Salmexpand_des_key(obuf, ibuf)
25516Salm	char *obuf;			/* bit pattern */
25616Salm	char *ibuf;			/* the key itself */
25716Salm{
25816Salm	register int i, j;		/* counter in a for loop */
25916Salm	int nbuf[64];			/* used for hex/key translation */
26016Salm
26116Salm	/*
26216Salm	 * leading '0x' or '0X' == hex key
26316Salm	 */
26416Salm	if (ibuf[0] == '0' && (ibuf[1] == 'x' || ibuf[1] == 'X')) {
26516Salm		ibuf = &ibuf[2];
26616Salm		/*
26716Salm		 * now translate it, bombing on any illegal hex digit
26816Salm		 */
26916Salm		for (i = 0; ibuf[i] && i < 16; i++)
2701057Salm			if ((nbuf[i] = hex_to_binary((int) ibuf[i], 16)) == -1)
2711057Salm				des_error("bad hex digit in key");
27216Salm		while (i < 16)
27316Salm			nbuf[i++] = 0;
27416Salm		for (i = 0; i < 8; i++)
27516Salm			obuf[i] =
27616Salm			    ((nbuf[2*i]&0xf)<<4) | (nbuf[2*i+1]&0xf);
27716Salm		/* preserve parity bits */
27816Salm		pflag = 1;
27916Salm		return;
28016Salm	}
28116Salm	/*
28216Salm	 * leading '0b' or '0B' == binary key
28316Salm	 */
28416Salm	if (ibuf[0] == '0' && (ibuf[1] == 'b' || ibuf[1] == 'B')) {
28516Salm		ibuf = &ibuf[2];
28616Salm		/*
28716Salm		 * now translate it, bombing on any illegal binary digit
28816Salm		 */
28916Salm		for (i = 0; ibuf[i] && i < 16; i++)
2901057Salm			if ((nbuf[i] = hex_to_binary((int) ibuf[i], 2)) == -1)
2911057Salm				des_error("bad binary digit in key");
29216Salm		while (i < 64)
29316Salm			nbuf[i++] = 0;
29416Salm		for (i = 0; i < 8; i++)
29516Salm			for (j = 0; j < 8; j++)
29616Salm				obuf[i] = (obuf[i]<<1)|nbuf[8*i+j];
29716Salm		/* preserve parity bits */
29816Salm		pflag = 1;
29916Salm		return;
30016Salm	}
30116Salm	/*
30216Salm	 * no special leader -- ASCII
30316Salm	 */
30416Salm	(void)strncpy(obuf, ibuf, 8);
30516Salm}
30616Salm
30716Salm/*****************
30816Salm * DES FUNCTIONS *
30916Salm *****************/
31016Salm/*
31116Salm * This sets the DES key and (if you're using the deszip version)
31216Salm * the direction of the transformation.  This uses the Sun
31316Salm * to map the 64-bit key onto the 56 bits that the key schedule
31416Salm * generation routines use: the old way, which just uses the user-
31516Salm * supplied 64 bits as is, and the new way, which resets the parity
31616Salm * bit to be the same as the low-order bit in each character.  The
31716Salm * new way generates a greater variety of key schedules, since many
31816Salm * systems set the parity (high) bit of each character to 0, and the
31916Salm * DES ignores the low order bit of each character.
32016Salm */
32116Salmvoid
3221057Salmset_des_key(buf)
32316Salm	Desbuf buf;				/* key block */
32416Salm{
32516Salm	register int i, j;			/* counter in a for loop */
32616Salm	register int par;			/* parity counter */
32716Salm
32816Salm	/*
32916Salm	 * if the parity is not preserved, flip it
33016Salm	 */
33116Salm	if (!pflag) {
33216Salm		for (i = 0; i < 8; i++) {
33316Salm			par = 0;
33416Salm			for (j = 1; j < 8; j++)
33516Salm				if ((bits[j]&UCHAR(buf, i)) != 0)
33616Salm					par++;
33716Salm			if ((par&01) == 01)
33816Salm				UCHAR(buf, i) = UCHAR(buf, i)&0177;
33916Salm			else
34016Salm				UCHAR(buf, i) = (UCHAR(buf, i)&0177)|0200;
34116Salm		}
34216Salm	}
34316Salm
34416Salm	DES_KEY(UBUFFER(buf));
34516Salm}
34616Salm
34716Salm
34816Salm/*
34916Salm * This encrypts using the Cipher Block Chaining mode of DES
35016Salm */
3511057Salmint
3521057Salmcbc_encode(msgbuf, n, fp)
35316Salm	char *msgbuf;
35416Salm	int n;
35516Salm	FILE *fp;
35616Salm{
35716Salm	int inverse = 0;	/* 0 to encrypt, 1 to decrypt */
35816Salm
35916Salm	/*
36016Salm	 * do the transformation
36116Salm	 */
36216Salm	if (n == 8) {
36316Salm		for (n = 0; n < 8; n++)
36416Salm			CHAR(msgbuf, n) ^= CHAR(ivec, n);
36516Salm		DES_XFORM(UBUFFER(msgbuf));
36616Salm		MEMCPY(BUFFER(ivec), BUFFER(msgbuf), 8);
36716Salm		return WRITE(BUFFER(msgbuf), 8, fp);
36816Salm	}
36916Salm	/*
37016Salm	 * at EOF or last block -- in either case, the last byte contains
37116Salm	 * the character representation of the number of bytes in it
37216Salm	 */
37316Salm/*
37416Salm	MEMZERO(msgbuf +  n, 8 - n);
37516Salm*/
37616Salm	/*
37716Salm	 *  Pad the last block randomly
37816Salm	 */
37916Salm	(void)MEMCPY(BUFFER(msgbuf + n), BUFFER(pvec), 8 - n);
38016Salm	CHAR(msgbuf, 7) = n;
38116Salm	for (n = 0; n < 8; n++)
38216Salm		CHAR(msgbuf, n) ^= CHAR(ivec, n);
38316Salm	DES_XFORM(UBUFFER(msgbuf));
38416Salm	return WRITE(BUFFER(msgbuf), 8, fp);
38516Salm}
38616Salm
38716Salm/*
38816Salm * This decrypts using the Cipher Block Chaining mode of DES
38916Salm */
3901057Salmint
3911057Salmcbc_decode(msgbuf, fp)
39216Salm	char *msgbuf;		/* I/O buffer */
39316Salm	FILE *fp;			/* input file descriptor */
39416Salm{
39516Salm	Desbuf ibuf;	/* temp buffer for initialization vector */
39616Salm	register int n;		/* number of bytes actually read */
39716Salm	register int c;		/* used to test for EOF */
39816Salm	int inverse = 1;	/* 0 to encrypt, 1 to decrypt */
39916Salm
40016Salm	if ((n = READ(BUFFER(msgbuf), 8, fp)) == 8) {
40116Salm		/*
40216Salm		 * do the transformation
40316Salm		 */
40416Salm		MEMCPY(BUFFER(ibuf), BUFFER(msgbuf), 8);
40516Salm		DES_XFORM(UBUFFER(msgbuf));
40616Salm		for (c = 0; c < 8; c++)
40716Salm			UCHAR(msgbuf, c) ^= UCHAR(ivec, c);
40816Salm		MEMCPY(BUFFER(ivec), BUFFER(ibuf), 8);
40916Salm		/*
41016Salm		 * if the last one, handle it specially
41116Salm		 */
41216Salm		if ((c = fgetc(fp)) == EOF) {
41316Salm			n = CHAR(msgbuf, 7);
41416Salm			if (n < 0 || n > 7) {
4151057Salm				des_error("decryption failed (block corrupted)");
41616Salm				return EOF;
41716Salm			}
41816Salm		} else
41916Salm			(void)ungetc(c, fp);
42016Salm		return n;
42116Salm	}
42216Salm	if (n > 0)
4231057Salm		des_error("decryption failed (incomplete block)");
42416Salm	else if (n < 0)
4251057Salm		des_error("cannot read file");
42616Salm	return EOF;
42716Salm}
42816Salm#endif	/* DES */
429