diff options
-rw-r--r-- | bin/ed/Makefile | 4 | ||||
-rw-r--r-- | bin/ed/cbc.c | 80 | ||||
-rw-r--r-- | bin/ed/ed.h | 19 |
3 files changed, 51 insertions, 52 deletions
diff --git a/bin/ed/Makefile b/bin/ed/Makefile index 2b612b9..e721cfa 100644 --- a/bin/ed/Makefile +++ b/bin/ed/Makefile @@ -8,8 +8,8 @@ MLINKS= ed.1 red.1 .if exists(${.CURDIR}/../../secure) && !defined(NOCRYPT) DISTRIBUTION=crypto CFLAGS+=-DDES -DPADD= ${LIBCIPHER} -LDADD= -lcipher +DPADD= ${LIBCRYPTO} +LDADD= -lcrypto .endif .include <bsd.prog.mk> diff --git a/bin/ed/cbc.c b/bin/ed/cbc.c index bb96022..24307c9 100644 --- a/bin/ed/cbc.c +++ b/bin/ed/cbc.c @@ -43,6 +43,8 @@ __FBSDID("$FreeBSD$"); #include <pwd.h> #ifdef DES #include <time.h> +#include <openssl/des.h> +#define ED_DES_INCLUDES #endif #include "ed.h" @@ -56,12 +58,9 @@ __FBSDID("$FreeBSD$"); #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 @@ __FBSDID("$FreeBSD$"); #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 @@ enum { /* encrypt, decrypt, authenticate */ 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 @@ init_des_cipher(void) /* 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 @@ int 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 @@ get_keyword(void) /* * 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 @@ expand_des_key(char *obuf, char *kbuf) * 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 @@ set_des_key(Desbuf buf) /* key block */ 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 @@ cbc_encode(unsigned char *msgbuf, int n, FILE *fp) */ 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 @@ cbc_encode(unsigned char *msgbuf, int n, FILE *fp) /* * 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 @@ cbc_encode(unsigned char *msgbuf, int n, FILE *fp) 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; diff --git a/bin/ed/ed.h b/bin/ed/ed.h index 9233cd3..c40f573 100644 --- a/bin/ed/ed.h +++ b/bin/ed/ed.h @@ -175,6 +175,18 @@ if ((i) > (n)) { \ /* 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 @@ void clear_undo_stack(void); 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 @@ void handle_winch(int); 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 @@ int open_sbuf(void); 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_file(char *, long); 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 *); |