---------------------
PatchSet 134 
Date: 2003/06/02 11:06:28
Author: markm
Branch: HEAD
Tag: (none) 
Log:
Modernise. Use libcrypto for DES instead of libcipher.

Members: 
	Makefile:1.22->1.23 
	cbc.c:1.17->1.18 
	ed.h:1.18->1.19 

Index: src/bin/ed/Makefile
diff -u src/bin/ed/Makefile:1.22 src/bin/ed/Makefile:1.23
--- src/bin/ed/Makefile:1.22	Mon May 19 11:51:58 2003
+++ src/bin/ed/Makefile	Mon Jun  2 15:06:28 2003
@@ -1,4 +1,4 @@
-# $FreeBSD: src/bin/ed/Makefile,v 1.22 2003/05/19 15:51:58 des Exp $
+# $FreeBSD: src/bin/ed/Makefile,v 1.23 2003/06/02 19:06:28 markm Exp $
 
 PROG=	ed
 SRCS=	buf.c cbc.c glbl.c io.c main.c re.c sub.c undo.c
@@ -8,8 +8,8 @@
 .if exists(${.CURDIR}/../../secure) && !defined(NOCRYPT)
 DISTRIBUTION=crypto
 CFLAGS+=-DDES
-DPADD=	${LIBCIPHER}
-LDADD=	-lcipher
+DPADD=	${LIBCRYPTO}
+LDADD=	-lcrypto
 .endif
 
 .include <bsd.prog.mk>
Index: src/bin/ed/cbc.c
diff -u src/bin/ed/cbc.c:1.17 src/bin/ed/cbc.c:1.18
--- src/bin/ed/cbc.c:1.17	Wed Jul 31 12:49:01 2002
+++ src/bin/ed/cbc.c	Mon Jun  2 15:06:28 2003
@@ -36,13 +36,15 @@
  */
 
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/bin/ed/cbc.c,v 1.17 2002/07/31 16:49:01 markm Exp $");
+__FBSDID("$FreeBSD: src/bin/ed/cbc.c,v 1.18 2003/06/02 19:06:28 markm Exp $");
 
 #include <sys/types.h>
 #include <errno.h>
 #include <pwd.h>
 #ifdef DES
 #include <time.h>
+#include <openssl/des.h>
+#define ED_DES_INCLUDES
 #endif
 
 #include "ed.h"
@@ -56,12 +58,9 @@
 #define	MEMZERO(dest,len)	memset((dest), 0, (len))
 
 /* Hide the calls to the primitive encryption routines. */
-#define	DES_KEY(buf)							\
-	if (des_setkey(buf))						\
-		des_error("des_setkey");
 #define	DES_XFORM(buf)							\
-	if (des_cipher((char *)buf, (char *)buf, 0L, inverse ? -1 : 1))	\
-		des_error("des_cipher");
+		DES_ecb_encrypt(buf, buf, &schedule, 			\
+		    inverse ? DES_DECRYPT : DES_ENCRYPT);
 
 /*
  * read/write - no error checking
@@ -70,15 +69,6 @@
 #define WRITE(buf, n, fp)	fwrite(buf, sizeof(char), n, fp)
 
 /*
- * some things to make references easier
- */
-typedef char Desbuf[8];
-#define	CHAR(x,i)	(x[i])
-#define	UCHAR(x,i)	(x[i])
-#define	BUFFER(x)	(x)
-#define	UBUFFER(x)	(x)
-
-/*
  * global variables and related macros
  */
 
@@ -86,18 +76,21 @@
 	MODE_ENCRYPT, MODE_DECRYPT, MODE_AUTHENTICATE
 } mode = MODE_ENCRYPT;
 
-Desbuf ivec;				/* initialization vector */
-Desbuf pvec;				/* padding vector */
+DES_cblock ivec;			/* initialization vector */
+DES_cblock pvec;			/* padding vector */
+
 char bits[] = {				/* used to extract bits from a char */
 	'\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
 };
+
 int pflag;				/* 1 to preserve parity bits */
 
+DES_key_schedule schedule;		/* expanded DES key */
+
 unsigned char des_buf[8];	/* shared buffer for get_des_char/put_des_char */
 int des_ct = 0;			/* count for get_des_char/put_des_char */
 int des_n = 0;			/* index for put_des_char/get_des_char */
 
-
 /* init_des_cipher: initialize DES */
 void
 init_des_cipher(void)
@@ -112,7 +105,7 @@
 
 	/* initialize the padding vector */
 	for (i = 0; i < 8; i++)
-		CHAR(pvec, i) = (char) (arc4random() % 256);
+		pvec[i] = (char) (arc4random() % 256);
 #endif
 }
 
@@ -172,7 +165,7 @@
 get_keyword(void)
 {
 	char *p;			/* used to obtain the key */
-	Desbuf msgbuf;			/* I/O buffer */
+	DES_cblock msgbuf;		/* I/O buffer */
 
 	/*
 	 * get the key
@@ -182,9 +175,9 @@
 		/*
 		 * copy it, nul-padded, into the key area
 		 */
-		expand_des_key(BUFFER(msgbuf), p);
+		expand_des_key(msgbuf, p);
 		MEMZERO(p, _PASSWORD_LEN);
-		set_des_key(msgbuf);
+		set_des_key(&msgbuf);
 		MEMZERO(msgbuf, sizeof msgbuf);
 		return 1;
 	}
@@ -303,7 +296,7 @@
  * DES ignores the low order bit of each character.
  */
 void
-set_des_key(Desbuf buf)				/* key block */
+set_des_key(DES_cblock *buf)			/* key block */
 {
 	int i, j;				/* counter in a for loop */
 	int par;				/* parity counter */
@@ -315,16 +308,17 @@
 		for (i = 0; i < 8; i++) {
 			par = 0;
 			for (j = 1; j < 8; j++)
-				if ((bits[j]&UCHAR(buf, i)) != 0)
+				if ((bits[j] & (*buf)[i]) != 0)
 					par++;
-			if ((par&01) == 01)
-				UCHAR(buf, i) = UCHAR(buf, i)&0177;
+			if ((par & 0x01) == 0x01)
+				(*buf)[i] &= 0x7f;
 			else
-				UCHAR(buf, i) = (UCHAR(buf, i)&0177)|0200;
+				(*buf)[i] = ((*buf)[i] & 0x7f) | 0x80;
 		}
 	}
 
-	DES_KEY(UBUFFER(buf));
+	DES_set_odd_parity(buf);
+	DES_set_key(buf, &schedule);
 }
 
 
@@ -341,10 +335,10 @@
 	 */
 	if (n == 8) {
 		for (n = 0; n < 8; n++)
-			CHAR(msgbuf, n) ^= CHAR(ivec, n);
-		DES_XFORM(UBUFFER(msgbuf));
-		MEMCPY(BUFFER(ivec), BUFFER(msgbuf), 8);
-		return WRITE(BUFFER(msgbuf), 8, fp);
+			msgbuf[n] ^= ivec[n];
+		DES_XFORM((DES_cblock *)msgbuf);
+		MEMCPY(ivec, msgbuf, 8);
+		return WRITE(msgbuf, 8, fp);
 	}
 	/*
 	 * at EOF or last block -- in either case, the last byte contains
@@ -356,12 +350,12 @@
 	/*
 	 *  Pad the last block randomly
 	 */
-	(void)MEMCPY(BUFFER(msgbuf + n), BUFFER(pvec), 8 - n);
-	CHAR(msgbuf, 7) = n;
+	(void)MEMCPY(msgbuf + n, pvec, 8 - n);
+	msgbuf[7] = n;
 	for (n = 0; n < 8; n++)
-		CHAR(msgbuf, n) ^= CHAR(ivec, n);
-	DES_XFORM(UBUFFER(msgbuf));
-	return WRITE(BUFFER(msgbuf), 8, fp);
+		msgbuf[n] ^= ivec[n];
+	DES_XFORM((DES_cblock *)msgbuf);
+	return WRITE(msgbuf, 8, fp);
 }
 
 /*
@@ -372,25 +366,25 @@
 int
 cbc_decode(unsigned char *msgbuf, FILE *fp)
 {
-	Desbuf tbuf;	/* temp buffer for initialization vector */
+	DES_cblock tbuf;	/* temp buffer for initialization vector */
 	int n;			/* number of bytes actually read */
 	int c;			/* used to test for EOF */
 	int inverse = 1;	/* 0 to encrypt, 1 to decrypt */
 
-	if ((n = READ(BUFFER(msgbuf), 8, fp)) == 8) {
+	if ((n = READ(msgbuf, 8, fp)) == 8) {
 		/*
 		 * do the transformation
 		 */
-		MEMCPY(BUFFER(tbuf), BUFFER(msgbuf), 8);
-		DES_XFORM(UBUFFER(msgbuf));
+		MEMCPY(tbuf, msgbuf, 8);
+		DES_XFORM((DES_cblock *)msgbuf);
 		for (c = 0; c < 8; c++)
-			UCHAR(msgbuf, c) ^= UCHAR(ivec, c);
-		MEMCPY(BUFFER(ivec), BUFFER(tbuf), 8);
+			msgbuf[c] ^= ivec[c];
+		MEMCPY(ivec, tbuf, 8);
 		/*
 		 * if the last one, handle it specially
 		 */
 		if ((c = fgetc(fp)) == EOF) {
-			n = CHAR(msgbuf, 7);
+			n = msgbuf[7];
 			if (n < 0 || n > 7) {
 				des_error("decryption failed (block corrupted)");
 				return EOF;
Index: src/bin/ed/ed.h
diff -u src/bin/ed/ed.h:1.18 src/bin/ed/ed.h:1.19
--- src/bin/ed/ed.h:1.18	Wed Jul 31 12:49:01 2002
+++ src/bin/ed/ed.h	Mon Jun  2 15:06:28 2003
@@ -25,7 +25,7 @@
  * SUCH DAMAGE.
  *
  *	@(#)ed.h,v 1.5 1994/02/01 00:34:39 alm Exp
- * $FreeBSD: src/bin/ed/ed.h,v 1.18 2002/07/31 16:49:01 markm Exp $
+ * $FreeBSD: src/bin/ed/ed.h,v 1.19 2003/06/02 19:06:28 markm Exp $
  */
 
 #include <sys/param.h>
@@ -175,6 +175,18 @@
 /* NEWLINE_TO_NUL: overwrite newlines with ASCII NULs */
 #define NEWLINE_TO_NUL(s, l) translit_text(s, l, '\n', '\0')
 
+#ifdef ED_DES_INCLUDES
+void des_error(const char *);
+void expand_des_key(char *, char *);
+void set_des_key(DES_cblock *);
+#endif
+
+/* Other DES support stuff */
+void init_des_cipher(void);
+int flush_des_file(FILE *);
+int get_des_char(FILE *);
+int put_des_char(int, FILE *);
+
 /* Local Function Declarations */
 void add_line_node(line_t *);
 int append_lines(long);
@@ -188,21 +200,17 @@
 int close_sbuf(void);
 int copy_lines(long);
 int delete_lines(long, long);
-void des_error(const char *);
 int display_lines(long, long, int);
 line_t *dup_line_node(line_t *);
 int exec_command(void);
 long exec_global(int, int);
-void expand_des_key(char *, char *);
 int extract_addr_range(void);
 char *extract_pattern(int);
 int extract_subst_tail(int *, long *);
 char *extract_subst_template(void);
 int filter_lines(long, long, char *);
-int flush_des_file(FILE *);
 line_t *get_addressed_line_node(long);
 pattern_t *get_compiled_pattern(void);
-int get_des_char(FILE *);
 char *get_extended_line(int *, int);
 char *get_filename(void);
 int get_keyword(void);
@@ -219,7 +227,6 @@
 int has_trailing_escape(char *, char *);
 int hex_to_binary(int, int);
 void init_buffers(void);
-void init_des_cipher(void);
 int is_legal_filename(char *);
 int join_lines(long, long);
 int mark_line_node(line_t *, int);
@@ -230,7 +237,6 @@
 char *parse_char_class(char *);
 int pop_undo_stack(void);
 undo_t *push_undo_stack(int, long, long);
-int put_des_char(int, FILE *);
 const char *put_sbuf_line(const char *);
 int put_stream_line(FILE *, const char *, int);
 int put_tty_line(const char *, int, long, int);
@@ -239,7 +245,6 @@
 long read_stream(FILE *, long);
 int search_and_replace(pattern_t *, int, int);
 int set_active_node(line_t *);
-void set_des_key(char *);
 void signal_hup(int);
 void signal_int(int);
 char *strip_escapes(char *);
