cbc.c revision 101093
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.
3616Salm */
3716Salm
3899109Sobrien#include <sys/cdefs.h>
3999109Sobrien__FBSDID("$FreeBSD: head/bin/ed/cbc.c 101093 2002-07-31 16:52:16Z markm $");
4016Salm
411057Salm#include <sys/types.h>
4216Salm#include <errno.h>
4316Salm#include <pwd.h>
4427963Ssteve#ifdef DES
4527963Ssteve#include <time.h>
4627963Ssteve#endif
4716Salm
4816Salm#include "ed.h"
4916Salm
501057Salm
5116Salm/*
5216Salm * BSD and System V systems offer special library calls that do
531057Salm * block move_liness and fills, so if possible we take advantage of them
5416Salm */
5516Salm#define	MEMCPY(dest,src,len)	memcpy((dest),(src),(len))
5616Salm#define	MEMZERO(dest,len)	memset((dest), 0, (len))
5716Salm
5816Salm/* Hide the calls to the primitive encryption routines. */
59101093Smarkm#define	DES_KEY(buf)							\
60101093Smarkm	if (des_setkey(buf))						\
611057Salm		des_error("des_setkey");
62101093Smarkm#define	DES_XFORM(buf)							\
63101093Smarkm	if (des_cipher((char *)buf, (char *)buf, 0L, inverse ? -1 : 1))	\
641057Salm		des_error("des_cipher");
6516Salm
6616Salm/*
6716Salm * read/write - no error checking
6816Salm */
6916Salm#define	READ(buf, n, fp)	fread(buf, sizeof(char), n, fp)
7016Salm#define WRITE(buf, n, fp)	fwrite(buf, sizeof(char), n, fp)
7116Salm
7216Salm/*
7316Salm * some things to make references easier
7416Salm */
7516Salmtypedef char Desbuf[8];
7616Salm#define	CHAR(x,i)	(x[i])
7716Salm#define	UCHAR(x,i)	(x[i])
7816Salm#define	BUFFER(x)	(x)
7916Salm#define	UBUFFER(x)	(x)
8016Salm
8116Salm/*
8216Salm * global variables and related macros
8316Salm */
8416Salm
8516Salmenum { 					/* encrypt, decrypt, authenticate */
8616Salm	MODE_ENCRYPT, MODE_DECRYPT, MODE_AUTHENTICATE
8716Salm} mode = MODE_ENCRYPT;
8816Salm
8916SalmDesbuf ivec;				/* initialization vector */
9016SalmDesbuf pvec;				/* padding vector */
9116Salmchar bits[] = {				/* used to extract bits from a char */
9216Salm	'\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
9316Salm};
9416Salmint pflag;				/* 1 to preserve parity bits */
9516Salm
961057Salmunsigned char des_buf[8];	/* shared buffer for get_des_char/put_des_char */
971057Salmint des_ct = 0;			/* count for get_des_char/put_des_char */
981057Salmint des_n = 0;			/* index for put_des_char/get_des_char */
9916Salm
10016Salm
1011057Salm/* init_des_cipher: initialize DES */
10216Salmvoid
10390109Simpinit_des_cipher(void)
10416Salm{
10516Salm#ifdef DES
10616Salm	int i;
10716Salm
10816Salm	des_ct = des_n = 0;
10916Salm
1107165Sjoerg	/* initialize the initialization vector */
11116Salm	MEMZERO(ivec, 8);
11216Salm
11346684Skris	/* initialize the padding vector */
11416Salm	for (i = 0; i < 8; i++)
11573563Skris		CHAR(pvec, i) = (char) (arc4random() % 256);
11616Salm#endif
11716Salm}
11816Salm
11916Salm
1201057Salm/* get_des_char: return next char in an encrypted file */
1211057Salmint
12290109Simpget_des_char(FILE *fp)
12316Salm{
12416Salm#ifdef DES
12516Salm	if (des_n >= des_ct) {
12616Salm		des_n = 0;
1271057Salm		des_ct = cbc_decode(des_buf, fp);
12816Salm	}
12916Salm	return (des_ct > 0) ? des_buf[des_n++] : EOF;
1307165Sjoerg#else
1317165Sjoerg	return (getc(fp));
13216Salm#endif
13316Salm}
13416Salm
13516Salm
1361057Salm/* put_des_char: write a char to an encrypted file; return char written */
1371057Salmint
13890109Simpput_des_char(int c, FILE *fp)
13916Salm{
14016Salm#ifdef DES
14116Salm	if (des_n == sizeof des_buf) {
1421057Salm		des_ct = cbc_encode(des_buf, des_n, fp);
14316Salm		des_n = 0;
14416Salm	}
14516Salm	return (des_ct >= 0) ? (des_buf[des_n++] = c) : EOF;
1467165Sjoerg#else
1477165Sjoerg	return (fputc(c, fp));
14816Salm#endif
14916Salm}
15016Salm
15116Salm
1521057Salm/* flush_des_file: flush an encrypted file's output; return status */
1531057Salmint
15490109Simpflush_des_file(FILE *fp)
15516Salm{
15616Salm#ifdef DES
15716Salm	if (des_n == sizeof des_buf) {
1581057Salm		des_ct = cbc_encode(des_buf, des_n, fp);
15916Salm		des_n = 0;
16016Salm	}
1611057Salm	return (des_ct >= 0 && cbc_encode(des_buf, des_n, fp) >= 0) ? 0 : EOF;
1627165Sjoerg#else
1637165Sjoerg	return (fflush(fp));
16416Salm#endif
16516Salm}
16616Salm
16716Salm#ifdef DES
16816Salm/*
16916Salm * get keyword from tty or stdin
17016Salm */
1711057Salmint
17290109Simpget_keyword(void)
17316Salm{
17481220Smike	char *p;			/* used to obtain the key */
17516Salm	Desbuf msgbuf;			/* I/O buffer */
17616Salm
17716Salm	/*
17816Salm	 * get the key
17916Salm	 */
18016Salm	if (*(p = getpass("Enter key: "))) {
18116Salm
18216Salm		/*
18316Salm		 * copy it, nul-padded, into the key area
18416Salm		 */
1851057Salm		expand_des_key(BUFFER(msgbuf), p);
18616Salm		MEMZERO(p, _PASSWORD_LEN);
1871057Salm		set_des_key(msgbuf);
18816Salm		MEMZERO(msgbuf, sizeof msgbuf);
18916Salm		return 1;
19016Salm	}
19116Salm	return 0;
19216Salm}
19316Salm
19416Salm
19516Salm/*
19616Salm * print a warning message and, possibly, terminate
19716Salm */
19816Salmvoid
19990109Simpdes_error(const char *s)
20016Salm{
20181220Smike	errmsg = s ? s : strerror(errno);
20216Salm}
20316Salm
20416Salm/*
20516Salm * map a hex character to an integer
20616Salm */
2071057Salmint
20890109Simphex_to_binary(int c, int radix)
20916Salm{
21016Salm	switch(c) {
21116Salm	case '0':		return(0x0);
21216Salm	case '1':		return(0x1);
21316Salm	case '2':		return(radix > 2 ? 0x2 : -1);
21416Salm	case '3':		return(radix > 3 ? 0x3 : -1);
21516Salm	case '4':		return(radix > 4 ? 0x4 : -1);
21616Salm	case '5':		return(radix > 5 ? 0x5 : -1);
21716Salm	case '6':		return(radix > 6 ? 0x6 : -1);
21816Salm	case '7':		return(radix > 7 ? 0x7 : -1);
21916Salm	case '8':		return(radix > 8 ? 0x8 : -1);
22016Salm	case '9':		return(radix > 9 ? 0x9 : -1);
22116Salm	case 'A': case 'a':	return(radix > 10 ? 0xa : -1);
22216Salm	case 'B': case 'b':	return(radix > 11 ? 0xb : -1);
22316Salm	case 'C': case 'c':	return(radix > 12 ? 0xc : -1);
22416Salm	case 'D': case 'd':	return(radix > 13 ? 0xd : -1);
22516Salm	case 'E': case 'e':	return(radix > 14 ? 0xe : -1);
22616Salm	case 'F': case 'f':	return(radix > 15 ? 0xf : -1);
22716Salm	}
22816Salm	/*
22916Salm	 * invalid character
23016Salm	 */
23116Salm	return(-1);
23216Salm}
23316Salm
23416Salm/*
23516Salm * convert the key to a bit pattern
23690109Simp *	obuf		bit pattern
23790109Simp *	kbuf		the key itself
23816Salm */
23916Salmvoid
24090109Simpexpand_des_key(char *obuf, char *kbuf)
24116Salm{
24281220Smike	int i, j;			/* counter in a for loop */
24316Salm	int nbuf[64];			/* used for hex/key translation */
24416Salm
24516Salm	/*
24616Salm	 * leading '0x' or '0X' == hex key
24716Salm	 */
24881220Smike	if (kbuf[0] == '0' && (kbuf[1] == 'x' || kbuf[1] == 'X')) {
24981220Smike		kbuf = &kbuf[2];
25016Salm		/*
25116Salm		 * now translate it, bombing on any illegal hex digit
25216Salm		 */
25381220Smike		for (i = 0; kbuf[i] && i < 16; i++)
25481220Smike			if ((nbuf[i] = hex_to_binary((int) kbuf[i], 16)) == -1)
2551057Salm				des_error("bad hex digit in key");
25616Salm		while (i < 16)
25716Salm			nbuf[i++] = 0;
25816Salm		for (i = 0; i < 8; i++)
25916Salm			obuf[i] =
26016Salm			    ((nbuf[2*i]&0xf)<<4) | (nbuf[2*i+1]&0xf);
26116Salm		/* preserve parity bits */
26216Salm		pflag = 1;
26316Salm		return;
26416Salm	}
26516Salm	/*
26616Salm	 * leading '0b' or '0B' == binary key
26716Salm	 */
26881220Smike	if (kbuf[0] == '0' && (kbuf[1] == 'b' || kbuf[1] == 'B')) {
26981220Smike		kbuf = &kbuf[2];
27016Salm		/*
27116Salm		 * now translate it, bombing on any illegal binary digit
27216Salm		 */
27381220Smike		for (i = 0; kbuf[i] && i < 16; i++)
27481220Smike			if ((nbuf[i] = hex_to_binary((int) kbuf[i], 2)) == -1)
2751057Salm				des_error("bad binary digit in key");
27616Salm		while (i < 64)
27716Salm			nbuf[i++] = 0;
27816Salm		for (i = 0; i < 8; i++)
27916Salm			for (j = 0; j < 8; j++)
28016Salm				obuf[i] = (obuf[i]<<1)|nbuf[8*i+j];
28116Salm		/* preserve parity bits */
28216Salm		pflag = 1;
28316Salm		return;
28416Salm	}
28516Salm	/*
28616Salm	 * no special leader -- ASCII
28716Salm	 */
28881220Smike	(void)strncpy(obuf, kbuf, 8);
28916Salm}
29016Salm
29116Salm/*****************
29216Salm * DES FUNCTIONS *
29316Salm *****************/
29416Salm/*
29516Salm * This sets the DES key and (if you're using the deszip version)
29616Salm * the direction of the transformation.  This uses the Sun
29716Salm * to map the 64-bit key onto the 56 bits that the key schedule
29816Salm * generation routines use: the old way, which just uses the user-
29916Salm * supplied 64 bits as is, and the new way, which resets the parity
30016Salm * bit to be the same as the low-order bit in each character.  The
30116Salm * new way generates a greater variety of key schedules, since many
30216Salm * systems set the parity (high) bit of each character to 0, and the
30316Salm * DES ignores the low order bit of each character.
30416Salm */
30516Salmvoid
30690109Simpset_des_key(Desbuf buf)				/* key block */
30716Salm{
30881220Smike	int i, j;				/* counter in a for loop */
30981220Smike	int par;				/* parity counter */
31016Salm
31116Salm	/*
31216Salm	 * if the parity is not preserved, flip it
31316Salm	 */
31416Salm	if (!pflag) {
31516Salm		for (i = 0; i < 8; i++) {
31616Salm			par = 0;
31716Salm			for (j = 1; j < 8; j++)
31816Salm				if ((bits[j]&UCHAR(buf, i)) != 0)
31916Salm					par++;
32016Salm			if ((par&01) == 01)
32116Salm				UCHAR(buf, i) = UCHAR(buf, i)&0177;
32216Salm			else
32316Salm				UCHAR(buf, i) = (UCHAR(buf, i)&0177)|0200;
32416Salm		}
32516Salm	}
32616Salm
32716Salm	DES_KEY(UBUFFER(buf));
32816Salm}
32916Salm
33016Salm
33116Salm/*
33216Salm * This encrypts using the Cipher Block Chaining mode of DES
33316Salm */
3341057Salmint
335101093Smarkmcbc_encode(unsigned char *msgbuf, int n, FILE *fp)
33616Salm{
33716Salm	int inverse = 0;	/* 0 to encrypt, 1 to decrypt */
33816Salm
33916Salm	/*
34016Salm	 * do the transformation
34116Salm	 */
34216Salm	if (n == 8) {
34316Salm		for (n = 0; n < 8; n++)
34416Salm			CHAR(msgbuf, n) ^= CHAR(ivec, n);
34516Salm		DES_XFORM(UBUFFER(msgbuf));
34616Salm		MEMCPY(BUFFER(ivec), BUFFER(msgbuf), 8);
34716Salm		return WRITE(BUFFER(msgbuf), 8, fp);
34816Salm	}
34916Salm	/*
35016Salm	 * at EOF or last block -- in either case, the last byte contains
35116Salm	 * the character representation of the number of bytes in it
35216Salm	 */
35316Salm/*
35416Salm	MEMZERO(msgbuf +  n, 8 - n);
35516Salm*/
35616Salm	/*
35716Salm	 *  Pad the last block randomly
35816Salm	 */
35916Salm	(void)MEMCPY(BUFFER(msgbuf + n), BUFFER(pvec), 8 - n);
36016Salm	CHAR(msgbuf, 7) = n;
36116Salm	for (n = 0; n < 8; n++)
36216Salm		CHAR(msgbuf, n) ^= CHAR(ivec, n);
36316Salm	DES_XFORM(UBUFFER(msgbuf));
36416Salm	return WRITE(BUFFER(msgbuf), 8, fp);
36516Salm}
36616Salm
36716Salm/*
36816Salm * This decrypts using the Cipher Block Chaining mode of DES
36990109Simp *	msgbuf	I/O buffer
37090109Simp *	fp	input file descriptor
37116Salm */
3721057Salmint
373101093Smarkmcbc_decode(unsigned char *msgbuf, FILE *fp)
37416Salm{
37581220Smike	Desbuf tbuf;	/* temp buffer for initialization vector */
37681220Smike	int n;			/* number of bytes actually read */
37781220Smike	int c;			/* used to test for EOF */
37816Salm	int inverse = 1;	/* 0 to encrypt, 1 to decrypt */
37916Salm
38016Salm	if ((n = READ(BUFFER(msgbuf), 8, fp)) == 8) {
38116Salm		/*
38216Salm		 * do the transformation
38316Salm		 */
38481220Smike		MEMCPY(BUFFER(tbuf), BUFFER(msgbuf), 8);
38516Salm		DES_XFORM(UBUFFER(msgbuf));
38616Salm		for (c = 0; c < 8; c++)
38716Salm			UCHAR(msgbuf, c) ^= UCHAR(ivec, c);
38881220Smike		MEMCPY(BUFFER(ivec), BUFFER(tbuf), 8);
38916Salm		/*
39016Salm		 * if the last one, handle it specially
39116Salm		 */
39216Salm		if ((c = fgetc(fp)) == EOF) {
39316Salm			n = CHAR(msgbuf, 7);
39416Salm			if (n < 0 || n > 7) {
3951057Salm				des_error("decryption failed (block corrupted)");
39616Salm				return EOF;
39716Salm			}
39816Salm		} else
39916Salm			(void)ungetc(c, fp);
40016Salm		return n;
40116Salm	}
40216Salm	if (n > 0)
4031057Salm		des_error("decryption failed (incomplete block)");
40416Salm	else if (n < 0)
4051057Salm		des_error("cannot read file");
40616Salm	return EOF;
40716Salm}
40816Salm#endif	/* DES */
409