cbc.c revision 90109
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 90109 2002-02-02 06:36:49Z imp $"; 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 10590109Simpinit_des_cipher(void) 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 12490109Simpget_des_char(FILE *fp) 12516Salm{ 12616Salm#ifdef DES 12716Salm if (des_n >= des_ct) { 12816Salm des_n = 0; 1291057Salm des_ct = cbc_decode(des_buf, fp); 13016Salm } 13116Salm return (des_ct > 0) ? des_buf[des_n++] : EOF; 1327165Sjoerg#else 1337165Sjoerg return (getc(fp)); 13416Salm#endif 13516Salm} 13616Salm 13716Salm 1381057Salm/* put_des_char: write a char to an encrypted file; return char written */ 1391057Salmint 14090109Simpput_des_char(int c, FILE *fp) 14116Salm{ 14216Salm#ifdef DES 14316Salm if (des_n == sizeof des_buf) { 1441057Salm des_ct = cbc_encode(des_buf, des_n, fp); 14516Salm des_n = 0; 14616Salm } 14716Salm return (des_ct >= 0) ? (des_buf[des_n++] = c) : EOF; 1487165Sjoerg#else 1497165Sjoerg return (fputc(c, fp)); 15016Salm#endif 15116Salm} 15216Salm 15316Salm 1541057Salm/* flush_des_file: flush an encrypted file's output; return status */ 1551057Salmint 15690109Simpflush_des_file(FILE *fp) 15716Salm{ 15816Salm#ifdef DES 15916Salm if (des_n == sizeof des_buf) { 1601057Salm des_ct = cbc_encode(des_buf, des_n, fp); 16116Salm des_n = 0; 16216Salm } 1631057Salm return (des_ct >= 0 && cbc_encode(des_buf, des_n, fp) >= 0) ? 0 : EOF; 1647165Sjoerg#else 1657165Sjoerg return (fflush(fp)); 16616Salm#endif 16716Salm} 16816Salm 16916Salm#ifdef DES 17016Salm/* 17116Salm * get keyword from tty or stdin 17216Salm */ 1731057Salmint 17490109Simpget_keyword(void) 17516Salm{ 17681220Smike char *p; /* used to obtain the key */ 17716Salm Desbuf msgbuf; /* I/O buffer */ 17816Salm 17916Salm /* 18016Salm * get the key 18116Salm */ 18216Salm if (*(p = getpass("Enter key: "))) { 18316Salm 18416Salm /* 18516Salm * copy it, nul-padded, into the key area 18616Salm */ 1871057Salm expand_des_key(BUFFER(msgbuf), p); 18816Salm MEMZERO(p, _PASSWORD_LEN); 1891057Salm set_des_key(msgbuf); 19016Salm MEMZERO(msgbuf, sizeof msgbuf); 19116Salm return 1; 19216Salm } 19316Salm return 0; 19416Salm} 19516Salm 19616Salm 19716Salm/* 19816Salm * print a warning message and, possibly, terminate 19916Salm */ 20016Salmvoid 20190109Simpdes_error(const char *s) 20216Salm{ 20381220Smike errmsg = s ? s : strerror(errno); 20416Salm} 20516Salm 20616Salm/* 20716Salm * map a hex character to an integer 20816Salm */ 2091057Salmint 21090109Simphex_to_binary(int c, int radix) 21116Salm{ 21216Salm switch(c) { 21316Salm case '0': return(0x0); 21416Salm case '1': return(0x1); 21516Salm case '2': return(radix > 2 ? 0x2 : -1); 21616Salm case '3': return(radix > 3 ? 0x3 : -1); 21716Salm case '4': return(radix > 4 ? 0x4 : -1); 21816Salm case '5': return(radix > 5 ? 0x5 : -1); 21916Salm case '6': return(radix > 6 ? 0x6 : -1); 22016Salm case '7': return(radix > 7 ? 0x7 : -1); 22116Salm case '8': return(radix > 8 ? 0x8 : -1); 22216Salm case '9': return(radix > 9 ? 0x9 : -1); 22316Salm case 'A': case 'a': return(radix > 10 ? 0xa : -1); 22416Salm case 'B': case 'b': return(radix > 11 ? 0xb : -1); 22516Salm case 'C': case 'c': return(radix > 12 ? 0xc : -1); 22616Salm case 'D': case 'd': return(radix > 13 ? 0xd : -1); 22716Salm case 'E': case 'e': return(radix > 14 ? 0xe : -1); 22816Salm case 'F': case 'f': return(radix > 15 ? 0xf : -1); 22916Salm } 23016Salm /* 23116Salm * invalid character 23216Salm */ 23316Salm return(-1); 23416Salm} 23516Salm 23616Salm/* 23716Salm * convert the key to a bit pattern 23890109Simp * obuf bit pattern 23990109Simp * kbuf the key itself 24016Salm */ 24116Salmvoid 24290109Simpexpand_des_key(char *obuf, char *kbuf) 24316Salm{ 24481220Smike int i, j; /* counter in a for loop */ 24516Salm int nbuf[64]; /* used for hex/key translation */ 24616Salm 24716Salm /* 24816Salm * leading '0x' or '0X' == hex key 24916Salm */ 25081220Smike if (kbuf[0] == '0' && (kbuf[1] == 'x' || kbuf[1] == 'X')) { 25181220Smike kbuf = &kbuf[2]; 25216Salm /* 25316Salm * now translate it, bombing on any illegal hex digit 25416Salm */ 25581220Smike for (i = 0; kbuf[i] && i < 16; i++) 25681220Smike if ((nbuf[i] = hex_to_binary((int) kbuf[i], 16)) == -1) 2571057Salm des_error("bad hex digit in key"); 25816Salm while (i < 16) 25916Salm nbuf[i++] = 0; 26016Salm for (i = 0; i < 8; i++) 26116Salm obuf[i] = 26216Salm ((nbuf[2*i]&0xf)<<4) | (nbuf[2*i+1]&0xf); 26316Salm /* preserve parity bits */ 26416Salm pflag = 1; 26516Salm return; 26616Salm } 26716Salm /* 26816Salm * leading '0b' or '0B' == binary key 26916Salm */ 27081220Smike if (kbuf[0] == '0' && (kbuf[1] == 'b' || kbuf[1] == 'B')) { 27181220Smike kbuf = &kbuf[2]; 27216Salm /* 27316Salm * now translate it, bombing on any illegal binary digit 27416Salm */ 27581220Smike for (i = 0; kbuf[i] && i < 16; i++) 27681220Smike if ((nbuf[i] = hex_to_binary((int) kbuf[i], 2)) == -1) 2771057Salm des_error("bad binary digit in key"); 27816Salm while (i < 64) 27916Salm nbuf[i++] = 0; 28016Salm for (i = 0; i < 8; i++) 28116Salm for (j = 0; j < 8; j++) 28216Salm obuf[i] = (obuf[i]<<1)|nbuf[8*i+j]; 28316Salm /* preserve parity bits */ 28416Salm pflag = 1; 28516Salm return; 28616Salm } 28716Salm /* 28816Salm * no special leader -- ASCII 28916Salm */ 29081220Smike (void)strncpy(obuf, kbuf, 8); 29116Salm} 29216Salm 29316Salm/***************** 29416Salm * DES FUNCTIONS * 29516Salm *****************/ 29616Salm/* 29716Salm * This sets the DES key and (if you're using the deszip version) 29816Salm * the direction of the transformation. This uses the Sun 29916Salm * to map the 64-bit key onto the 56 bits that the key schedule 30016Salm * generation routines use: the old way, which just uses the user- 30116Salm * supplied 64 bits as is, and the new way, which resets the parity 30216Salm * bit to be the same as the low-order bit in each character. The 30316Salm * new way generates a greater variety of key schedules, since many 30416Salm * systems set the parity (high) bit of each character to 0, and the 30516Salm * DES ignores the low order bit of each character. 30616Salm */ 30716Salmvoid 30890109Simpset_des_key(Desbuf buf) /* key block */ 30916Salm{ 31081220Smike int i, j; /* counter in a for loop */ 31181220Smike int par; /* parity counter */ 31216Salm 31316Salm /* 31416Salm * if the parity is not preserved, flip it 31516Salm */ 31616Salm if (!pflag) { 31716Salm for (i = 0; i < 8; i++) { 31816Salm par = 0; 31916Salm for (j = 1; j < 8; j++) 32016Salm if ((bits[j]&UCHAR(buf, i)) != 0) 32116Salm par++; 32216Salm if ((par&01) == 01) 32316Salm UCHAR(buf, i) = UCHAR(buf, i)&0177; 32416Salm else 32516Salm UCHAR(buf, i) = (UCHAR(buf, i)&0177)|0200; 32616Salm } 32716Salm } 32816Salm 32916Salm DES_KEY(UBUFFER(buf)); 33016Salm} 33116Salm 33216Salm 33316Salm/* 33416Salm * This encrypts using the Cipher Block Chaining mode of DES 33516Salm */ 3361057Salmint 33790109Simpcbc_encode(char *msgbuf, int n, FILE *fp) 33816Salm{ 33916Salm int inverse = 0; /* 0 to encrypt, 1 to decrypt */ 34016Salm 34116Salm /* 34216Salm * do the transformation 34316Salm */ 34416Salm if (n == 8) { 34516Salm for (n = 0; n < 8; n++) 34616Salm CHAR(msgbuf, n) ^= CHAR(ivec, n); 34716Salm DES_XFORM(UBUFFER(msgbuf)); 34816Salm MEMCPY(BUFFER(ivec), BUFFER(msgbuf), 8); 34916Salm return WRITE(BUFFER(msgbuf), 8, fp); 35016Salm } 35116Salm /* 35216Salm * at EOF or last block -- in either case, the last byte contains 35316Salm * the character representation of the number of bytes in it 35416Salm */ 35516Salm/* 35616Salm MEMZERO(msgbuf + n, 8 - n); 35716Salm*/ 35816Salm /* 35916Salm * Pad the last block randomly 36016Salm */ 36116Salm (void)MEMCPY(BUFFER(msgbuf + n), BUFFER(pvec), 8 - n); 36216Salm CHAR(msgbuf, 7) = n; 36316Salm for (n = 0; n < 8; n++) 36416Salm CHAR(msgbuf, n) ^= CHAR(ivec, n); 36516Salm DES_XFORM(UBUFFER(msgbuf)); 36616Salm return WRITE(BUFFER(msgbuf), 8, fp); 36716Salm} 36816Salm 36916Salm/* 37016Salm * This decrypts using the Cipher Block Chaining mode of DES 37190109Simp * msgbuf I/O buffer 37290109Simp * fp input file descriptor 37316Salm */ 3741057Salmint 37590109Simpcbc_decode(char *msgbuf, FILE *fp) 37616Salm{ 37781220Smike Desbuf tbuf; /* temp buffer for initialization vector */ 37881220Smike int n; /* number of bytes actually read */ 37981220Smike int c; /* used to test for EOF */ 38016Salm int inverse = 1; /* 0 to encrypt, 1 to decrypt */ 38116Salm 38216Salm if ((n = READ(BUFFER(msgbuf), 8, fp)) == 8) { 38316Salm /* 38416Salm * do the transformation 38516Salm */ 38681220Smike MEMCPY(BUFFER(tbuf), BUFFER(msgbuf), 8); 38716Salm DES_XFORM(UBUFFER(msgbuf)); 38816Salm for (c = 0; c < 8; c++) 38916Salm UCHAR(msgbuf, c) ^= UCHAR(ivec, c); 39081220Smike MEMCPY(BUFFER(ivec), BUFFER(tbuf), 8); 39116Salm /* 39216Salm * if the last one, handle it specially 39316Salm */ 39416Salm if ((c = fgetc(fp)) == EOF) { 39516Salm n = CHAR(msgbuf, 7); 39616Salm if (n < 0 || n > 7) { 3971057Salm des_error("decryption failed (block corrupted)"); 39816Salm return EOF; 39916Salm } 40016Salm } else 40116Salm (void)ungetc(c, fp); 40216Salm return n; 40316Salm } 40416Salm if (n > 0) 4051057Salm des_error("decryption failed (incomplete block)"); 40616Salm else if (n < 0) 4071057Salm des_error("cannot read file"); 40816Salm return EOF; 40916Salm} 41016Salm#endif /* DES */ 411