cbc.c revision 81220
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 3816Salm#ifndef lint 3981220Smikestatic const char rcsid[] = 4050471Speter "$FreeBSD: head/bin/ed/cbc.c 81220 2001-08-06 22:01:31Z mike $"; 4116Salm#endif /* not lint */ 4216Salm 431057Salm#include <sys/types.h> 4416Salm#include <errno.h> 4516Salm#include <pwd.h> 4627963Ssteve#ifdef DES 4727963Ssteve#include <time.h> 4827963Ssteve#endif 4916Salm 5016Salm#include "ed.h" 5116Salm 521057Salm 5316Salm/* 5416Salm * BSD and System V systems offer special library calls that do 551057Salm * block move_liness and fills, so if possible we take advantage of them 5616Salm */ 5716Salm#define MEMCPY(dest,src,len) memcpy((dest),(src),(len)) 5816Salm#define MEMZERO(dest,len) memset((dest), 0, (len)) 5916Salm 6016Salm/* Hide the calls to the primitive encryption routines. */ 6116Salm#define DES_KEY(buf) \ 6216Salm if (des_setkey(buf)) \ 631057Salm des_error("des_setkey"); 6416Salm#define DES_XFORM(buf) \ 6516Salm if (des_cipher(buf, buf, 0L, (inverse ? -1 : 1))) \ 661057Salm des_error("des_cipher"); 6716Salm 6816Salm/* 6916Salm * read/write - no error checking 7016Salm */ 7116Salm#define READ(buf, n, fp) fread(buf, sizeof(char), n, fp) 7216Salm#define WRITE(buf, n, fp) fwrite(buf, sizeof(char), n, fp) 7316Salm 7416Salm/* 7516Salm * some things to make references easier 7616Salm */ 7716Salmtypedef char Desbuf[8]; 7816Salm#define CHAR(x,i) (x[i]) 7916Salm#define UCHAR(x,i) (x[i]) 8016Salm#define BUFFER(x) (x) 8116Salm#define UBUFFER(x) (x) 8216Salm 8316Salm/* 8416Salm * global variables and related macros 8516Salm */ 8616Salm 8716Salmenum { /* encrypt, decrypt, authenticate */ 8816Salm MODE_ENCRYPT, MODE_DECRYPT, MODE_AUTHENTICATE 8916Salm} mode = MODE_ENCRYPT; 9016Salm 9116SalmDesbuf ivec; /* initialization vector */ 9216SalmDesbuf pvec; /* padding vector */ 9316Salmchar bits[] = { /* used to extract bits from a char */ 9416Salm '\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001' 9516Salm}; 9616Salmint pflag; /* 1 to preserve parity bits */ 9716Salm 981057Salmunsigned char des_buf[8]; /* shared buffer for get_des_char/put_des_char */ 991057Salmint des_ct = 0; /* count for get_des_char/put_des_char */ 1001057Salmint des_n = 0; /* index for put_des_char/get_des_char */ 10116Salm 10216Salm 1031057Salm/* init_des_cipher: initialize DES */ 10416Salmvoid 1051057Salminit_des_cipher() 10616Salm{ 10716Salm#ifdef DES 10816Salm int i; 10916Salm 11016Salm des_ct = des_n = 0; 11116Salm 1127165Sjoerg /* initialize the initialization vector */ 11316Salm MEMZERO(ivec, 8); 11416Salm 11546684Skris /* initialize the padding vector */ 11616Salm for (i = 0; i < 8; i++) 11773563Skris CHAR(pvec, i) = (char) (arc4random() % 256); 11816Salm#endif 11916Salm} 12016Salm 12116Salm 1221057Salm/* get_des_char: return next char in an encrypted file */ 1231057Salmint 1241057Salmget_des_char(fp) 12516Salm FILE *fp; 12616Salm{ 12716Salm#ifdef DES 12816Salm if (des_n >= des_ct) { 12916Salm des_n = 0; 1301057Salm des_ct = cbc_decode(des_buf, fp); 13116Salm } 13216Salm return (des_ct > 0) ? des_buf[des_n++] : EOF; 1337165Sjoerg#else 1347165Sjoerg return (getc(fp)); 13516Salm#endif 13616Salm} 13716Salm 13816Salm 1391057Salm/* put_des_char: write a char to an encrypted file; return char written */ 1401057Salmint 1411057Salmput_des_char(c, fp) 14216Salm int c; 14316Salm FILE *fp; 14416Salm{ 14516Salm#ifdef DES 14616Salm if (des_n == sizeof des_buf) { 1471057Salm des_ct = cbc_encode(des_buf, des_n, fp); 14816Salm des_n = 0; 14916Salm } 15016Salm return (des_ct >= 0) ? (des_buf[des_n++] = c) : EOF; 1517165Sjoerg#else 1527165Sjoerg return (fputc(c, fp)); 15316Salm#endif 15416Salm} 15516Salm 15616Salm 1571057Salm/* flush_des_file: flush an encrypted file's output; return status */ 1581057Salmint 1591057Salmflush_des_file(fp) 16016Salm FILE *fp; 16116Salm{ 16216Salm#ifdef DES 16316Salm if (des_n == sizeof des_buf) { 1641057Salm des_ct = cbc_encode(des_buf, des_n, fp); 16516Salm des_n = 0; 16616Salm } 1671057Salm return (des_ct >= 0 && cbc_encode(des_buf, des_n, fp) >= 0) ? 0 : EOF; 1687165Sjoerg#else 1697165Sjoerg return (fflush(fp)); 17016Salm#endif 17116Salm} 17216Salm 17316Salm#ifdef DES 17416Salm/* 17516Salm * get keyword from tty or stdin 17616Salm */ 1771057Salmint 1781057Salmget_keyword() 17916Salm{ 18081220Smike char *p; /* used to obtain the key */ 18116Salm Desbuf msgbuf; /* I/O buffer */ 18216Salm 18316Salm /* 18416Salm * get the key 18516Salm */ 18616Salm if (*(p = getpass("Enter key: "))) { 18716Salm 18816Salm /* 18916Salm * copy it, nul-padded, into the key area 19016Salm */ 1911057Salm expand_des_key(BUFFER(msgbuf), p); 19216Salm MEMZERO(p, _PASSWORD_LEN); 1931057Salm set_des_key(msgbuf); 19416Salm MEMZERO(msgbuf, sizeof msgbuf); 19516Salm return 1; 19616Salm } 19716Salm return 0; 19816Salm} 19916Salm 20016Salm 20116Salm/* 20216Salm * print a warning message and, possibly, terminate 20316Salm */ 20416Salmvoid 2051057Salmdes_error(s) 20681220Smike const char *s; /* the message */ 20716Salm{ 20881220Smike errmsg = s ? s : strerror(errno); 20916Salm} 21016Salm 21116Salm/* 21216Salm * map a hex character to an integer 21316Salm */ 2141057Salmint 2151057Salmhex_to_binary(c, radix) 21616Salm int c; /* char to be converted */ 21716Salm int radix; /* base (2 to 16) */ 21816Salm{ 21916Salm switch(c) { 22016Salm case '0': return(0x0); 22116Salm case '1': return(0x1); 22216Salm case '2': return(radix > 2 ? 0x2 : -1); 22316Salm case '3': return(radix > 3 ? 0x3 : -1); 22416Salm case '4': return(radix > 4 ? 0x4 : -1); 22516Salm case '5': return(radix > 5 ? 0x5 : -1); 22616Salm case '6': return(radix > 6 ? 0x6 : -1); 22716Salm case '7': return(radix > 7 ? 0x7 : -1); 22816Salm case '8': return(radix > 8 ? 0x8 : -1); 22916Salm case '9': return(radix > 9 ? 0x9 : -1); 23016Salm case 'A': case 'a': return(radix > 10 ? 0xa : -1); 23116Salm case 'B': case 'b': return(radix > 11 ? 0xb : -1); 23216Salm case 'C': case 'c': return(radix > 12 ? 0xc : -1); 23316Salm case 'D': case 'd': return(radix > 13 ? 0xd : -1); 23416Salm case 'E': case 'e': return(radix > 14 ? 0xe : -1); 23516Salm case 'F': case 'f': return(radix > 15 ? 0xf : -1); 23616Salm } 23716Salm /* 23816Salm * invalid character 23916Salm */ 24016Salm return(-1); 24116Salm} 24216Salm 24316Salm/* 24416Salm * convert the key to a bit pattern 24516Salm */ 24616Salmvoid 24781220Smikeexpand_des_key(obuf, kbuf) 24816Salm char *obuf; /* bit pattern */ 24981220Smike char *kbuf; /* the key itself */ 25016Salm{ 25181220Smike int i, j; /* counter in a for loop */ 25216Salm int nbuf[64]; /* used for hex/key translation */ 25316Salm 25416Salm /* 25516Salm * leading '0x' or '0X' == hex key 25616Salm */ 25781220Smike if (kbuf[0] == '0' && (kbuf[1] == 'x' || kbuf[1] == 'X')) { 25881220Smike kbuf = &kbuf[2]; 25916Salm /* 26016Salm * now translate it, bombing on any illegal hex digit 26116Salm */ 26281220Smike for (i = 0; kbuf[i] && i < 16; i++) 26381220Smike if ((nbuf[i] = hex_to_binary((int) kbuf[i], 16)) == -1) 2641057Salm des_error("bad hex digit in key"); 26516Salm while (i < 16) 26616Salm nbuf[i++] = 0; 26716Salm for (i = 0; i < 8; i++) 26816Salm obuf[i] = 26916Salm ((nbuf[2*i]&0xf)<<4) | (nbuf[2*i+1]&0xf); 27016Salm /* preserve parity bits */ 27116Salm pflag = 1; 27216Salm return; 27316Salm } 27416Salm /* 27516Salm * leading '0b' or '0B' == binary key 27616Salm */ 27781220Smike if (kbuf[0] == '0' && (kbuf[1] == 'b' || kbuf[1] == 'B')) { 27881220Smike kbuf = &kbuf[2]; 27916Salm /* 28016Salm * now translate it, bombing on any illegal binary digit 28116Salm */ 28281220Smike for (i = 0; kbuf[i] && i < 16; i++) 28381220Smike if ((nbuf[i] = hex_to_binary((int) kbuf[i], 2)) == -1) 2841057Salm des_error("bad binary digit in key"); 28516Salm while (i < 64) 28616Salm nbuf[i++] = 0; 28716Salm for (i = 0; i < 8; i++) 28816Salm for (j = 0; j < 8; j++) 28916Salm obuf[i] = (obuf[i]<<1)|nbuf[8*i+j]; 29016Salm /* preserve parity bits */ 29116Salm pflag = 1; 29216Salm return; 29316Salm } 29416Salm /* 29516Salm * no special leader -- ASCII 29616Salm */ 29781220Smike (void)strncpy(obuf, kbuf, 8); 29816Salm} 29916Salm 30016Salm/***************** 30116Salm * DES FUNCTIONS * 30216Salm *****************/ 30316Salm/* 30416Salm * This sets the DES key and (if you're using the deszip version) 30516Salm * the direction of the transformation. This uses the Sun 30616Salm * to map the 64-bit key onto the 56 bits that the key schedule 30716Salm * generation routines use: the old way, which just uses the user- 30816Salm * supplied 64 bits as is, and the new way, which resets the parity 30916Salm * bit to be the same as the low-order bit in each character. The 31016Salm * new way generates a greater variety of key schedules, since many 31116Salm * systems set the parity (high) bit of each character to 0, and the 31216Salm * DES ignores the low order bit of each character. 31316Salm */ 31416Salmvoid 3151057Salmset_des_key(buf) 31616Salm Desbuf buf; /* key block */ 31716Salm{ 31881220Smike int i, j; /* counter in a for loop */ 31981220Smike int par; /* parity counter */ 32016Salm 32116Salm /* 32216Salm * if the parity is not preserved, flip it 32316Salm */ 32416Salm if (!pflag) { 32516Salm for (i = 0; i < 8; i++) { 32616Salm par = 0; 32716Salm for (j = 1; j < 8; j++) 32816Salm if ((bits[j]&UCHAR(buf, i)) != 0) 32916Salm par++; 33016Salm if ((par&01) == 01) 33116Salm UCHAR(buf, i) = UCHAR(buf, i)&0177; 33216Salm else 33316Salm UCHAR(buf, i) = (UCHAR(buf, i)&0177)|0200; 33416Salm } 33516Salm } 33616Salm 33716Salm DES_KEY(UBUFFER(buf)); 33816Salm} 33916Salm 34016Salm 34116Salm/* 34216Salm * This encrypts using the Cipher Block Chaining mode of DES 34316Salm */ 3441057Salmint 3451057Salmcbc_encode(msgbuf, n, fp) 34616Salm char *msgbuf; 34716Salm int n; 34816Salm FILE *fp; 34916Salm{ 35016Salm int inverse = 0; /* 0 to encrypt, 1 to decrypt */ 35116Salm 35216Salm /* 35316Salm * do the transformation 35416Salm */ 35516Salm if (n == 8) { 35616Salm for (n = 0; n < 8; n++) 35716Salm CHAR(msgbuf, n) ^= CHAR(ivec, n); 35816Salm DES_XFORM(UBUFFER(msgbuf)); 35916Salm MEMCPY(BUFFER(ivec), BUFFER(msgbuf), 8); 36016Salm return WRITE(BUFFER(msgbuf), 8, fp); 36116Salm } 36216Salm /* 36316Salm * at EOF or last block -- in either case, the last byte contains 36416Salm * the character representation of the number of bytes in it 36516Salm */ 36616Salm/* 36716Salm MEMZERO(msgbuf + n, 8 - n); 36816Salm*/ 36916Salm /* 37016Salm * Pad the last block randomly 37116Salm */ 37216Salm (void)MEMCPY(BUFFER(msgbuf + n), BUFFER(pvec), 8 - n); 37316Salm CHAR(msgbuf, 7) = n; 37416Salm for (n = 0; n < 8; n++) 37516Salm CHAR(msgbuf, n) ^= CHAR(ivec, n); 37616Salm DES_XFORM(UBUFFER(msgbuf)); 37716Salm return WRITE(BUFFER(msgbuf), 8, fp); 37816Salm} 37916Salm 38016Salm/* 38116Salm * This decrypts using the Cipher Block Chaining mode of DES 38216Salm */ 3831057Salmint 3841057Salmcbc_decode(msgbuf, fp) 38516Salm char *msgbuf; /* I/O buffer */ 38616Salm FILE *fp; /* input file descriptor */ 38716Salm{ 38881220Smike Desbuf tbuf; /* temp buffer for initialization vector */ 38981220Smike int n; /* number of bytes actually read */ 39081220Smike int c; /* used to test for EOF */ 39116Salm int inverse = 1; /* 0 to encrypt, 1 to decrypt */ 39216Salm 39316Salm if ((n = READ(BUFFER(msgbuf), 8, fp)) == 8) { 39416Salm /* 39516Salm * do the transformation 39616Salm */ 39781220Smike MEMCPY(BUFFER(tbuf), BUFFER(msgbuf), 8); 39816Salm DES_XFORM(UBUFFER(msgbuf)); 39916Salm for (c = 0; c < 8; c++) 40016Salm UCHAR(msgbuf, c) ^= UCHAR(ivec, c); 40181220Smike MEMCPY(BUFFER(ivec), BUFFER(tbuf), 8); 40216Salm /* 40316Salm * if the last one, handle it specially 40416Salm */ 40516Salm if ((c = fgetc(fp)) == EOF) { 40616Salm n = CHAR(msgbuf, 7); 40716Salm if (n < 0 || n > 7) { 4081057Salm des_error("decryption failed (block corrupted)"); 40916Salm return EOF; 41016Salm } 41116Salm } else 41216Salm (void)ungetc(c, fp); 41316Salm return n; 41416Salm } 41516Salm if (n > 0) 4161057Salm des_error("decryption failed (incomplete block)"); 41716Salm else if (n < 0) 4181057Salm des_error("cannot read file"); 41916Salm return EOF; 42016Salm} 42116Salm#endif /* DES */ 422