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 * 4. Neither the name of the University nor the names of its contributors
1816Salm *    may be used to endorse or promote products derived from this software
1916Salm *    without specific prior written permission.
2016Salm *
2116Salm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2216Salm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2316Salm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2416Salm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2516Salm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2616Salm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2716Salm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2816Salm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2916Salm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3016Salm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3116Salm * SUCH DAMAGE.
3216Salm */
3316Salm
3499109Sobrien#include <sys/cdefs.h>
3599109Sobrien__FBSDID("$FreeBSD$");
3616Salm
371057Salm#include <sys/types.h>
3816Salm#include <errno.h>
3916Salm#include <pwd.h>
4027963Ssteve#ifdef DES
4127963Ssteve#include <time.h>
42115717Smarkm#include <openssl/des.h>
43115717Smarkm#define ED_DES_INCLUDES
4427963Ssteve#endif
4516Salm
4616Salm#include "ed.h"
4716Salm
481057Salm
4916Salm/*
5016Salm * BSD and System V systems offer special library calls that do
511057Salm * block move_liness and fills, so if possible we take advantage of them
5216Salm */
5316Salm#define	MEMCPY(dest,src,len)	memcpy((dest),(src),(len))
5416Salm#define	MEMZERO(dest,len)	memset((dest), 0, (len))
5516Salm
5616Salm/* Hide the calls to the primitive encryption routines. */
57101093Smarkm#define	DES_XFORM(buf)							\
58115717Smarkm		DES_ecb_encrypt(buf, buf, &schedule, 			\
59115717Smarkm		    inverse ? DES_DECRYPT : DES_ENCRYPT);
6016Salm
6116Salm/*
6216Salm * read/write - no error checking
6316Salm */
6416Salm#define	READ(buf, n, fp)	fread(buf, sizeof(char), n, fp)
6516Salm#define WRITE(buf, n, fp)	fwrite(buf, sizeof(char), n, fp)
6616Salm
6716Salm/*
6816Salm * global variables and related macros
6916Salm */
7016Salm
71115777Sjhay#ifdef DES
72241720Sedstatic DES_cblock ivec;			/* initialization vector */
73241720Sedstatic DES_cblock pvec;			/* padding vector */
74115717Smarkm
75241720Sedstatic char bits[] = {			/* used to extract bits from a char */
7616Salm	'\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
7716Salm};
78115717Smarkm
79241720Sedstatic int pflag;			/* 1 to preserve parity bits */
8016Salm
81241720Sedstatic DES_key_schedule schedule;	/* expanded DES key */
82115717Smarkm
83241720Sedstatic unsigned char des_buf[8];/* shared buffer for get_des_char/put_des_char */
84241720Sedstatic int des_ct = 0;		/* count for get_des_char/put_des_char */
85241720Sedstatic int des_n = 0;		/* index for put_des_char/get_des_char */
86248656Sjmg#endif
8716Salm
881057Salm/* init_des_cipher: initialize DES */
8916Salmvoid
9090109Simpinit_des_cipher(void)
9116Salm{
9216Salm#ifdef DES
9316Salm	int i;
9416Salm
9516Salm	des_ct = des_n = 0;
9616Salm
977165Sjoerg	/* initialize the initialization vector */
9816Salm	MEMZERO(ivec, 8);
9916Salm
10046684Skris	/* initialize the padding vector */
10116Salm	for (i = 0; i < 8; i++)
102115717Smarkm		pvec[i] = (char) (arc4random() % 256);
10316Salm#endif
10416Salm}
10516Salm
10616Salm
1071057Salm/* get_des_char: return next char in an encrypted file */
1081057Salmint
10990109Simpget_des_char(FILE *fp)
11016Salm{
11116Salm#ifdef DES
11216Salm	if (des_n >= des_ct) {
11316Salm		des_n = 0;
1141057Salm		des_ct = cbc_decode(des_buf, fp);
11516Salm	}
11616Salm	return (des_ct > 0) ? des_buf[des_n++] : EOF;
1177165Sjoerg#else
1187165Sjoerg	return (getc(fp));
11916Salm#endif
12016Salm}
12116Salm
12216Salm
1231057Salm/* put_des_char: write a char to an encrypted file; return char written */
1241057Salmint
12590109Simpput_des_char(int c, FILE *fp)
12616Salm{
12716Salm#ifdef DES
12816Salm	if (des_n == sizeof des_buf) {
1291057Salm		des_ct = cbc_encode(des_buf, des_n, fp);
13016Salm		des_n = 0;
13116Salm	}
13216Salm	return (des_ct >= 0) ? (des_buf[des_n++] = c) : EOF;
1337165Sjoerg#else
1347165Sjoerg	return (fputc(c, fp));
13516Salm#endif
13616Salm}
13716Salm
13816Salm
1391057Salm/* flush_des_file: flush an encrypted file's output; return status */
1401057Salmint
14190109Simpflush_des_file(FILE *fp)
14216Salm{
14316Salm#ifdef DES
14416Salm	if (des_n == sizeof des_buf) {
1451057Salm		des_ct = cbc_encode(des_buf, des_n, fp);
14616Salm		des_n = 0;
14716Salm	}
1481057Salm	return (des_ct >= 0 && cbc_encode(des_buf, des_n, fp) >= 0) ? 0 : EOF;
1497165Sjoerg#else
1507165Sjoerg	return (fflush(fp));
15116Salm#endif
15216Salm}
15316Salm
15416Salm#ifdef DES
15516Salm/*
15616Salm * get keyword from tty or stdin
15716Salm */
1581057Salmint
15990109Simpget_keyword(void)
16016Salm{
16181220Smike	char *p;			/* used to obtain the key */
162115717Smarkm	DES_cblock msgbuf;		/* I/O buffer */
16316Salm
16416Salm	/*
16516Salm	 * get the key
16616Salm	 */
16716Salm	if (*(p = getpass("Enter key: "))) {
16816Salm
16916Salm		/*
17016Salm		 * copy it, nul-padded, into the key area
17116Salm		 */
172115717Smarkm		expand_des_key(msgbuf, p);
17316Salm		MEMZERO(p, _PASSWORD_LEN);
174115717Smarkm		set_des_key(&msgbuf);
17516Salm		MEMZERO(msgbuf, sizeof msgbuf);
17616Salm		return 1;
17716Salm	}
17816Salm	return 0;
17916Salm}
18016Salm
18116Salm
18216Salm/*
18316Salm * print a warning message and, possibly, terminate
18416Salm */
18516Salmvoid
18690109Simpdes_error(const char *s)
18716Salm{
18881220Smike	errmsg = s ? s : strerror(errno);
18916Salm}
19016Salm
19116Salm/*
19216Salm * map a hex character to an integer
19316Salm */
1941057Salmint
19590109Simphex_to_binary(int c, int radix)
19616Salm{
19716Salm	switch(c) {
19816Salm	case '0':		return(0x0);
19916Salm	case '1':		return(0x1);
20016Salm	case '2':		return(radix > 2 ? 0x2 : -1);
20116Salm	case '3':		return(radix > 3 ? 0x3 : -1);
20216Salm	case '4':		return(radix > 4 ? 0x4 : -1);
20316Salm	case '5':		return(radix > 5 ? 0x5 : -1);
20416Salm	case '6':		return(radix > 6 ? 0x6 : -1);
20516Salm	case '7':		return(radix > 7 ? 0x7 : -1);
20616Salm	case '8':		return(radix > 8 ? 0x8 : -1);
20716Salm	case '9':		return(radix > 9 ? 0x9 : -1);
20816Salm	case 'A': case 'a':	return(radix > 10 ? 0xa : -1);
20916Salm	case 'B': case 'b':	return(radix > 11 ? 0xb : -1);
21016Salm	case 'C': case 'c':	return(radix > 12 ? 0xc : -1);
21116Salm	case 'D': case 'd':	return(radix > 13 ? 0xd : -1);
21216Salm	case 'E': case 'e':	return(radix > 14 ? 0xe : -1);
21316Salm	case 'F': case 'f':	return(radix > 15 ? 0xf : -1);
21416Salm	}
21516Salm	/*
21616Salm	 * invalid character
21716Salm	 */
21816Salm	return(-1);
21916Salm}
22016Salm
22116Salm/*
22216Salm * convert the key to a bit pattern
22390109Simp *	obuf		bit pattern
22490109Simp *	kbuf		the key itself
22516Salm */
22616Salmvoid
22790109Simpexpand_des_key(char *obuf, char *kbuf)
22816Salm{
22981220Smike	int i, j;			/* counter in a for loop */
23016Salm	int nbuf[64];			/* used for hex/key translation */
23116Salm
23216Salm	/*
23316Salm	 * leading '0x' or '0X' == hex key
23416Salm	 */
23581220Smike	if (kbuf[0] == '0' && (kbuf[1] == 'x' || kbuf[1] == 'X')) {
23681220Smike		kbuf = &kbuf[2];
23716Salm		/*
23816Salm		 * now translate it, bombing on any illegal hex digit
23916Salm		 */
240270756Spfg		for (i = 0; i < 16 && kbuf[i]; i++)
24181220Smike			if ((nbuf[i] = hex_to_binary((int) kbuf[i], 16)) == -1)
2421057Salm				des_error("bad hex digit in key");
24316Salm		while (i < 16)
24416Salm			nbuf[i++] = 0;
24516Salm		for (i = 0; i < 8; i++)
24616Salm			obuf[i] =
24716Salm			    ((nbuf[2*i]&0xf)<<4) | (nbuf[2*i+1]&0xf);
24816Salm		/* preserve parity bits */
24916Salm		pflag = 1;
25016Salm		return;
25116Salm	}
25216Salm	/*
25316Salm	 * leading '0b' or '0B' == binary key
25416Salm	 */
25581220Smike	if (kbuf[0] == '0' && (kbuf[1] == 'b' || kbuf[1] == 'B')) {
25681220Smike		kbuf = &kbuf[2];
25716Salm		/*
25816Salm		 * now translate it, bombing on any illegal binary digit
25916Salm		 */
26081220Smike		for (i = 0; kbuf[i] && i < 16; i++)
26181220Smike			if ((nbuf[i] = hex_to_binary((int) kbuf[i], 2)) == -1)
2621057Salm				des_error("bad binary digit in key");
26316Salm		while (i < 64)
26416Salm			nbuf[i++] = 0;
26516Salm		for (i = 0; i < 8; i++)
26616Salm			for (j = 0; j < 8; j++)
26716Salm				obuf[i] = (obuf[i]<<1)|nbuf[8*i+j];
26816Salm		/* preserve parity bits */
26916Salm		pflag = 1;
27016Salm		return;
27116Salm	}
27216Salm	/*
27316Salm	 * no special leader -- ASCII
27416Salm	 */
27581220Smike	(void)strncpy(obuf, kbuf, 8);
27616Salm}
27716Salm
27816Salm/*****************
27916Salm * DES FUNCTIONS *
28016Salm *****************/
28116Salm/*
28216Salm * This sets the DES key and (if you're using the deszip version)
28316Salm * the direction of the transformation.  This uses the Sun
28416Salm * to map the 64-bit key onto the 56 bits that the key schedule
28516Salm * generation routines use: the old way, which just uses the user-
28616Salm * supplied 64 bits as is, and the new way, which resets the parity
28716Salm * bit to be the same as the low-order bit in each character.  The
28816Salm * new way generates a greater variety of key schedules, since many
28916Salm * systems set the parity (high) bit of each character to 0, and the
29016Salm * DES ignores the low order bit of each character.
29116Salm */
29216Salmvoid
293115717Smarkmset_des_key(DES_cblock *buf)			/* key block */
29416Salm{
29581220Smike	int i, j;				/* counter in a for loop */
29681220Smike	int par;				/* parity counter */
29716Salm
29816Salm	/*
29916Salm	 * if the parity is not preserved, flip it
30016Salm	 */
30116Salm	if (!pflag) {
30216Salm		for (i = 0; i < 8; i++) {
30316Salm			par = 0;
30416Salm			for (j = 1; j < 8; j++)
305115717Smarkm				if ((bits[j] & (*buf)[i]) != 0)
30616Salm					par++;
307115717Smarkm			if ((par & 0x01) == 0x01)
308115717Smarkm				(*buf)[i] &= 0x7f;
30916Salm			else
310115717Smarkm				(*buf)[i] = ((*buf)[i] & 0x7f) | 0x80;
31116Salm		}
31216Salm	}
31316Salm
314115717Smarkm	DES_set_odd_parity(buf);
315115717Smarkm	DES_set_key(buf, &schedule);
31616Salm}
31716Salm
31816Salm
31916Salm/*
32016Salm * This encrypts using the Cipher Block Chaining mode of DES
32116Salm */
3221057Salmint
323101093Smarkmcbc_encode(unsigned char *msgbuf, int n, FILE *fp)
32416Salm{
32516Salm	int inverse = 0;	/* 0 to encrypt, 1 to decrypt */
32616Salm
32716Salm	/*
32816Salm	 * do the transformation
32916Salm	 */
33016Salm	if (n == 8) {
33116Salm		for (n = 0; n < 8; n++)
332115717Smarkm			msgbuf[n] ^= ivec[n];
333115717Smarkm		DES_XFORM((DES_cblock *)msgbuf);
334115717Smarkm		MEMCPY(ivec, msgbuf, 8);
335115717Smarkm		return WRITE(msgbuf, 8, fp);
33616Salm	}
33716Salm	/*
33816Salm	 * at EOF or last block -- in either case, the last byte contains
33916Salm	 * the character representation of the number of bytes in it
34016Salm	 */
34116Salm/*
34216Salm	MEMZERO(msgbuf +  n, 8 - n);
34316Salm*/
34416Salm	/*
34516Salm	 *  Pad the last block randomly
34616Salm	 */
347115717Smarkm	(void)MEMCPY(msgbuf + n, pvec, 8 - n);
348115717Smarkm	msgbuf[7] = n;
34916Salm	for (n = 0; n < 8; n++)
350115717Smarkm		msgbuf[n] ^= ivec[n];
351115717Smarkm	DES_XFORM((DES_cblock *)msgbuf);
352115717Smarkm	return WRITE(msgbuf, 8, fp);
35316Salm}
35416Salm
35516Salm/*
35616Salm * This decrypts using the Cipher Block Chaining mode of DES
35790109Simp *	msgbuf	I/O buffer
35890109Simp *	fp	input file descriptor
35916Salm */
3601057Salmint
361101093Smarkmcbc_decode(unsigned char *msgbuf, FILE *fp)
36216Salm{
363115717Smarkm	DES_cblock tbuf;	/* temp buffer for initialization vector */
36481220Smike	int n;			/* number of bytes actually read */
36581220Smike	int c;			/* used to test for EOF */
36616Salm	int inverse = 1;	/* 0 to encrypt, 1 to decrypt */
36716Salm
368115717Smarkm	if ((n = READ(msgbuf, 8, fp)) == 8) {
36916Salm		/*
37016Salm		 * do the transformation
37116Salm		 */
372115717Smarkm		MEMCPY(tbuf, msgbuf, 8);
373115717Smarkm		DES_XFORM((DES_cblock *)msgbuf);
37416Salm		for (c = 0; c < 8; c++)
375115717Smarkm			msgbuf[c] ^= ivec[c];
376115717Smarkm		MEMCPY(ivec, tbuf, 8);
37716Salm		/*
37816Salm		 * if the last one, handle it specially
37916Salm		 */
38016Salm		if ((c = fgetc(fp)) == EOF) {
381115717Smarkm			n = msgbuf[7];
38216Salm			if (n < 0 || n > 7) {
3831057Salm				des_error("decryption failed (block corrupted)");
38416Salm				return EOF;
38516Salm			}
38616Salm		} else
38716Salm			(void)ungetc(c, fp);
38816Salm		return n;
38916Salm	}
39016Salm	if (n > 0)
3911057Salm		des_error("decryption failed (incomplete block)");
39216Salm	else if (n < 0)
3931057Salm		des_error("cannot read file");
39416Salm	return EOF;
39516Salm}
39616Salm#endif	/* DES */
397