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	des_ct = des_n = 0;
9416Salm
957165Sjoerg	/* initialize the initialization vector */
9616Salm	MEMZERO(ivec, 8);
9716Salm
9846684Skris	/* initialize the padding vector */
99301233Spfg	arc4random_buf(pvec, sizeof(pvec));
10016Salm#endif
10116Salm}
10216Salm
10316Salm
1041057Salm/* get_des_char: return next char in an encrypted file */
1051057Salmint
10690109Simpget_des_char(FILE *fp)
10716Salm{
10816Salm#ifdef DES
10916Salm	if (des_n >= des_ct) {
11016Salm		des_n = 0;
1111057Salm		des_ct = cbc_decode(des_buf, fp);
11216Salm	}
11316Salm	return (des_ct > 0) ? des_buf[des_n++] : EOF;
1147165Sjoerg#else
1157165Sjoerg	return (getc(fp));
11616Salm#endif
11716Salm}
11816Salm
11916Salm
1201057Salm/* put_des_char: write a char to an encrypted file; return char written */
1211057Salmint
12290109Simpput_des_char(int c, FILE *fp)
12316Salm{
12416Salm#ifdef DES
12516Salm	if (des_n == sizeof des_buf) {
1261057Salm		des_ct = cbc_encode(des_buf, des_n, fp);
12716Salm		des_n = 0;
12816Salm	}
12916Salm	return (des_ct >= 0) ? (des_buf[des_n++] = c) : EOF;
1307165Sjoerg#else
1317165Sjoerg	return (fputc(c, fp));
13216Salm#endif
13316Salm}
13416Salm
13516Salm
1361057Salm/* flush_des_file: flush an encrypted file's output; return status */
1371057Salmint
13890109Simpflush_des_file(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	}
1451057Salm	return (des_ct >= 0 && cbc_encode(des_buf, des_n, fp) >= 0) ? 0 : EOF;
1467165Sjoerg#else
1477165Sjoerg	return (fflush(fp));
14816Salm#endif
14916Salm}
15016Salm
15116Salm#ifdef DES
15216Salm/*
15316Salm * get keyword from tty or stdin
15416Salm */
1551057Salmint
15690109Simpget_keyword(void)
15716Salm{
15881220Smike	char *p;			/* used to obtain the key */
159115717Smarkm	DES_cblock msgbuf;		/* I/O buffer */
16016Salm
16116Salm	/*
16216Salm	 * get the key
16316Salm	 */
164301233Spfg	if ((p = getpass("Enter key: ")) != NULL && *p != '\0') {
16516Salm
16616Salm		/*
16716Salm		 * copy it, nul-padded, into the key area
16816Salm		 */
169115717Smarkm		expand_des_key(msgbuf, p);
17016Salm		MEMZERO(p, _PASSWORD_LEN);
171115717Smarkm		set_des_key(&msgbuf);
17216Salm		MEMZERO(msgbuf, sizeof msgbuf);
17316Salm		return 1;
17416Salm	}
17516Salm	return 0;
17616Salm}
17716Salm
17816Salm
17916Salm/*
18016Salm * print a warning message and, possibly, terminate
18116Salm */
18216Salmvoid
18390109Simpdes_error(const char *s)
18416Salm{
18581220Smike	errmsg = s ? s : strerror(errno);
18616Salm}
18716Salm
18816Salm/*
18916Salm * map a hex character to an integer
19016Salm */
1911057Salmint
19290109Simphex_to_binary(int c, int radix)
19316Salm{
19416Salm	switch(c) {
19516Salm	case '0':		return(0x0);
19616Salm	case '1':		return(0x1);
19716Salm	case '2':		return(radix > 2 ? 0x2 : -1);
19816Salm	case '3':		return(radix > 3 ? 0x3 : -1);
19916Salm	case '4':		return(radix > 4 ? 0x4 : -1);
20016Salm	case '5':		return(radix > 5 ? 0x5 : -1);
20116Salm	case '6':		return(radix > 6 ? 0x6 : -1);
20216Salm	case '7':		return(radix > 7 ? 0x7 : -1);
20316Salm	case '8':		return(radix > 8 ? 0x8 : -1);
20416Salm	case '9':		return(radix > 9 ? 0x9 : -1);
20516Salm	case 'A': case 'a':	return(radix > 10 ? 0xa : -1);
20616Salm	case 'B': case 'b':	return(radix > 11 ? 0xb : -1);
20716Salm	case 'C': case 'c':	return(radix > 12 ? 0xc : -1);
20816Salm	case 'D': case 'd':	return(radix > 13 ? 0xd : -1);
20916Salm	case 'E': case 'e':	return(radix > 14 ? 0xe : -1);
21016Salm	case 'F': case 'f':	return(radix > 15 ? 0xf : -1);
21116Salm	}
21216Salm	/*
21316Salm	 * invalid character
21416Salm	 */
21516Salm	return(-1);
21616Salm}
21716Salm
21816Salm/*
21916Salm * convert the key to a bit pattern
22090109Simp *	obuf		bit pattern
22190109Simp *	kbuf		the key itself
22216Salm */
22316Salmvoid
22490109Simpexpand_des_key(char *obuf, char *kbuf)
22516Salm{
22681220Smike	int i, j;			/* counter in a for loop */
22716Salm	int nbuf[64];			/* used for hex/key translation */
22816Salm
22916Salm	/*
23016Salm	 * leading '0x' or '0X' == hex key
23116Salm	 */
23281220Smike	if (kbuf[0] == '0' && (kbuf[1] == 'x' || kbuf[1] == 'X')) {
23381220Smike		kbuf = &kbuf[2];
23416Salm		/*
23516Salm		 * now translate it, bombing on any illegal hex digit
23616Salm		 */
237270756Spfg		for (i = 0; i < 16 && kbuf[i]; i++)
23881220Smike			if ((nbuf[i] = hex_to_binary((int) kbuf[i], 16)) == -1)
2391057Salm				des_error("bad hex digit in key");
24016Salm		while (i < 16)
24116Salm			nbuf[i++] = 0;
24216Salm		for (i = 0; i < 8; i++)
24316Salm			obuf[i] =
24416Salm			    ((nbuf[2*i]&0xf)<<4) | (nbuf[2*i+1]&0xf);
24516Salm		/* preserve parity bits */
24616Salm		pflag = 1;
24716Salm		return;
24816Salm	}
24916Salm	/*
25016Salm	 * leading '0b' or '0B' == binary key
25116Salm	 */
25281220Smike	if (kbuf[0] == '0' && (kbuf[1] == 'b' || kbuf[1] == 'B')) {
25381220Smike		kbuf = &kbuf[2];
25416Salm		/*
25516Salm		 * now translate it, bombing on any illegal binary digit
25616Salm		 */
257298766Spfg		for (i = 0; i < 16 && kbuf[i]; i++)
25881220Smike			if ((nbuf[i] = hex_to_binary((int) kbuf[i], 2)) == -1)
2591057Salm				des_error("bad binary digit in key");
26016Salm		while (i < 64)
26116Salm			nbuf[i++] = 0;
26216Salm		for (i = 0; i < 8; i++)
26316Salm			for (j = 0; j < 8; j++)
26416Salm				obuf[i] = (obuf[i]<<1)|nbuf[8*i+j];
26516Salm		/* preserve parity bits */
26616Salm		pflag = 1;
26716Salm		return;
26816Salm	}
26916Salm	/*
27016Salm	 * no special leader -- ASCII
27116Salm	 */
27281220Smike	(void)strncpy(obuf, kbuf, 8);
27316Salm}
27416Salm
27516Salm/*****************
27616Salm * DES FUNCTIONS *
27716Salm *****************/
27816Salm/*
27916Salm * This sets the DES key and (if you're using the deszip version)
28016Salm * the direction of the transformation.  This uses the Sun
28116Salm * to map the 64-bit key onto the 56 bits that the key schedule
28216Salm * generation routines use: the old way, which just uses the user-
28316Salm * supplied 64 bits as is, and the new way, which resets the parity
28416Salm * bit to be the same as the low-order bit in each character.  The
28516Salm * new way generates a greater variety of key schedules, since many
28616Salm * systems set the parity (high) bit of each character to 0, and the
28716Salm * DES ignores the low order bit of each character.
28816Salm */
28916Salmvoid
290115717Smarkmset_des_key(DES_cblock *buf)			/* key block */
29116Salm{
29281220Smike	int i, j;				/* counter in a for loop */
29381220Smike	int par;				/* parity counter */
29416Salm
29516Salm	/*
29616Salm	 * if the parity is not preserved, flip it
29716Salm	 */
29816Salm	if (!pflag) {
29916Salm		for (i = 0; i < 8; i++) {
30016Salm			par = 0;
30116Salm			for (j = 1; j < 8; j++)
302115717Smarkm				if ((bits[j] & (*buf)[i]) != 0)
30316Salm					par++;
304115717Smarkm			if ((par & 0x01) == 0x01)
305115717Smarkm				(*buf)[i] &= 0x7f;
30616Salm			else
307115717Smarkm				(*buf)[i] = ((*buf)[i] & 0x7f) | 0x80;
30816Salm		}
30916Salm	}
31016Salm
311115717Smarkm	DES_set_odd_parity(buf);
312115717Smarkm	DES_set_key(buf, &schedule);
31316Salm}
31416Salm
31516Salm
31616Salm/*
31716Salm * This encrypts using the Cipher Block Chaining mode of DES
31816Salm */
3191057Salmint
320101093Smarkmcbc_encode(unsigned char *msgbuf, int n, FILE *fp)
32116Salm{
32216Salm	int inverse = 0;	/* 0 to encrypt, 1 to decrypt */
32316Salm
32416Salm	/*
32516Salm	 * do the transformation
32616Salm	 */
32716Salm	if (n == 8) {
32816Salm		for (n = 0; n < 8; n++)
329115717Smarkm			msgbuf[n] ^= ivec[n];
330115717Smarkm		DES_XFORM((DES_cblock *)msgbuf);
331115717Smarkm		MEMCPY(ivec, msgbuf, 8);
332115717Smarkm		return WRITE(msgbuf, 8, fp);
33316Salm	}
33416Salm	/*
33516Salm	 * at EOF or last block -- in either case, the last byte contains
33616Salm	 * the character representation of the number of bytes in it
33716Salm	 */
33816Salm/*
33916Salm	MEMZERO(msgbuf +  n, 8 - n);
34016Salm*/
34116Salm	/*
34216Salm	 *  Pad the last block randomly
34316Salm	 */
344115717Smarkm	(void)MEMCPY(msgbuf + n, pvec, 8 - n);
345115717Smarkm	msgbuf[7] = n;
34616Salm	for (n = 0; n < 8; n++)
347115717Smarkm		msgbuf[n] ^= ivec[n];
348115717Smarkm	DES_XFORM((DES_cblock *)msgbuf);
349115717Smarkm	return WRITE(msgbuf, 8, fp);
35016Salm}
35116Salm
35216Salm/*
35316Salm * This decrypts using the Cipher Block Chaining mode of DES
35490109Simp *	msgbuf	I/O buffer
35590109Simp *	fp	input file descriptor
35616Salm */
3571057Salmint
358101093Smarkmcbc_decode(unsigned char *msgbuf, FILE *fp)
35916Salm{
360115717Smarkm	DES_cblock tbuf;	/* temp buffer for initialization vector */
36181220Smike	int n;			/* number of bytes actually read */
36281220Smike	int c;			/* used to test for EOF */
36316Salm	int inverse = 1;	/* 0 to encrypt, 1 to decrypt */
36416Salm
365115717Smarkm	if ((n = READ(msgbuf, 8, fp)) == 8) {
36616Salm		/*
36716Salm		 * do the transformation
36816Salm		 */
369115717Smarkm		MEMCPY(tbuf, msgbuf, 8);
370115717Smarkm		DES_XFORM((DES_cblock *)msgbuf);
37116Salm		for (c = 0; c < 8; c++)
372115717Smarkm			msgbuf[c] ^= ivec[c];
373115717Smarkm		MEMCPY(ivec, tbuf, 8);
37416Salm		/*
37516Salm		 * if the last one, handle it specially
37616Salm		 */
37716Salm		if ((c = fgetc(fp)) == EOF) {
378115717Smarkm			n = msgbuf[7];
37916Salm			if (n < 0 || n > 7) {
3801057Salm				des_error("decryption failed (block corrupted)");
38116Salm				return EOF;
38216Salm			}
38316Salm		} else
38416Salm			(void)ungetc(c, fp);
38516Salm		return n;
38616Salm	}
38716Salm	if (n > 0)
3881057Salm		des_error("decryption failed (incomplete block)");
38916Salm	else if (n < 0)
3901057Salm		des_error("cannot read file");
39116Salm	return EOF;
39216Salm}
39316Salm#endif	/* DES */
394