cbc.c revision 270256
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 3416Salm#include <sys/cdefs.h> 3516Salm__FBSDID("$FreeBSD: head/bin/ed/cbc.c 270256 2014-08-21 02:40:33Z pfg $"); 361057Salm 371057Salm#include <sys/types.h> 3816Salm#include <errno.h> 3916Salm#include <pwd.h> 4016Salm#ifdef DES 4127963Ssteve#include <time.h> 4220420Ssteve#include <openssl/des.h> 4327963Ssteve#define ED_DES_INCLUDES 4427963Ssteve#endif 4546684Skris 4627963Ssteve#include "ed.h" 4716Salm 4816Salm 491057Salm/* 5016Salm * BSD and System V systems offer special library calls that do 5116Salm * block move_liness and fills, so if possible we take advantage of them 5227963Ssteve */ 5327963Ssteve#define MEMCPY(dest,src,len) memcpy((dest),(src),(len)) 5427963Ssteve#define MEMZERO(dest,len) memset((dest), 0, (len)) 5516Salm 5616Salm/* Hide the calls to the primitive encryption routines. */ 5716Salm#define DES_XFORM(buf) \ 581057Salm DES_ecb_encrypt(buf, buf, &schedule, \ 5916Salm 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 671057Salm/* 6816Salm * global variables and related macros 6916Salm */ 7016Salm 7116Salm#ifdef DES 7216Salmstatic DES_cblock ivec; /* initialization vector */ 7316Salmstatic DES_cblock pvec; /* padding vector */ 7416Salm 751057Salmstatic char bits[] = { /* used to extract bits from a char */ 7616Salm '\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001' 7716Salm}; 781057Salm 7916Salmstatic int pflag; /* 1 to preserve parity bits */ 8016Salm 8116Salmstatic DES_key_schedule schedule; /* expanded DES key */ 8216Salm 8316Salmstatic unsigned char des_buf[8];/* shared buffer for get_des_char/put_des_char */ 8416Salmstatic int des_ct = 0; /* count for get_des_char/put_des_char */ 8516Salmstatic int des_n = 0; /* index for put_des_char/get_des_char */ 8616Salm#endif 8716Salm 8816Salm/* init_des_cipher: initialize DES */ 8916Salmvoid 9016Salminit_des_cipher(void) 9116Salm{ 9216Salm#ifdef DES 9316Salm int i; 9416Salm 9516Salm des_ct = des_n = 0; 9616Salm 9716Salm /* initialize the initialization vector */ 9816Salm MEMZERO(ivec, 8); 9916Salm 10016Salm /* initialize the padding vector */ 10116Salm for (i = 0; i < 8; i++) 10216Salm pvec[i] = (char) (arc4random() % 256); 10316Salm#endif 10416Salm} 10516Salm 10616Salm 10716Salm/* get_des_char: return next char in an encrypted file */ 10816Salmint 10916Salmget_des_char(FILE *fp) 1101057Salm{ 1111057Salm#ifdef DES 1121057Salm if (des_n >= des_ct) { 11316Salm des_n = 0; 11416Salm des_ct = cbc_decode(des_buf, fp); 1151057Salm } 11616Salm return (des_ct > 0) ? des_buf[des_n++] : EOF; 1171057Salm#else 11816Salm return (getc(fp)); 11916Salm#endif 12016Salm} 12116Salm 12216Salm 12316Salm/* put_des_char: write a char to an encrypted file; return char written */ 1247165Sjoergint 12516Salmput_des_char(int c, FILE *fp) 12616Salm{ 12746684Skris#ifdef DES 12816Salm if (des_n == sizeof des_buf) { 12916Salm des_ct = cbc_encode(des_buf, des_n, fp); 13016Salm des_n = 0; 13116Salm } 13216Salm return (des_ct >= 0) ? (des_buf[des_n++] = c) : EOF; 13316Salm#else 13416Salm return (fputc(c, fp)); 1351057Salm#endif 1361057Salm} 1371057Salm 13816Salm 13916Salm/* flush_des_file: flush an encrypted file's output; return status */ 14016Salmint 14116Salmflush_des_file(FILE *fp) 14216Salm{ 1431057Salm#ifdef DES 14416Salm if (des_n == sizeof des_buf) { 14516Salm des_ct = cbc_encode(des_buf, des_n, fp); 1467165Sjoerg des_n = 0; 1477165Sjoerg } 14816Salm return (des_ct >= 0 && cbc_encode(des_buf, des_n, fp) >= 0) ? 0 : EOF; 14916Salm#else 15016Salm return (fflush(fp)); 15116Salm#endif 1521057Salm} 1531057Salm 1541057Salm#ifdef DES 15516Salm/* 15616Salm * get keyword from tty or stdin 15716Salm */ 15816Salmint 15916Salmget_keyword(void) 1601057Salm{ 16116Salm char *p; /* used to obtain the key */ 16216Salm DES_cblock msgbuf; /* I/O buffer */ 16316Salm 1647165Sjoerg /* 1657165Sjoerg * get the key 16616Salm */ 16716Salm if (*(p = getpass("Enter key: "))) { 16816Salm 16916Salm /* 1701057Salm * copy it, nul-padded, into the key area 1711057Salm */ 1721057Salm expand_des_key(msgbuf, p); 17316Salm MEMZERO(p, _PASSWORD_LEN); 17416Salm set_des_key(&msgbuf); 17516Salm MEMZERO(msgbuf, sizeof msgbuf); 17616Salm return 1; 1771057Salm } 17816Salm return 0; 17916Salm} 1801057Salm 1817165Sjoerg 1827165Sjoerg/* 18316Salm * print a warning message and, possibly, terminate 18416Salm */ 18516Salmvoid 18616Salmdes_error(const char *s) 18716Salm{ 18816Salm errmsg = s ? s : strerror(errno); 18916Salm} 1901057Salm 1911057Salm/* 19216Salm * map a hex character to an integer 19316Salm */ 19416Salmint 19516Salmhex_to_binary(int c, int radix) 19616Salm{ 19716Salm switch(c) { 19816Salm case '0': return(0x0); 19916Salm case '1': return(0x1); 20016Salm case '2': return(radix > 2 ? 0x2 : -1); 20116Salm case '3': return(radix > 3 ? 0x3 : -1); 20216Salm case '4': return(radix > 4 ? 0x4 : -1); 20316Salm case '5': return(radix > 5 ? 0x5 : -1); 2041057Salm case '6': return(radix > 6 ? 0x6 : -1); 20516Salm case '7': return(radix > 7 ? 0x7 : -1); 2061057Salm case '8': return(radix > 8 ? 0x8 : -1); 20716Salm case '9': return(radix > 9 ? 0x9 : -1); 20816Salm case 'A': case 'a': return(radix > 10 ? 0xa : -1); 20916Salm case 'B': case 'b': return(radix > 11 ? 0xb : -1); 21016Salm case 'C': case 'c': return(radix > 12 ? 0xc : -1); 21116Salm case 'D': case 'd': return(radix > 13 ? 0xd : -1); 21216Salm case 'E': case 'e': return(radix > 14 ? 0xe : -1); 21316Salm case 'F': case 'f': return(radix > 15 ? 0xf : -1); 21416Salm } 21516Salm /* 21616Salm * invalid character 21716Salm */ 2181057Salm return(-1); 21916Salm} 22016Salm 22116Salm/* 22216Salm * convert the key to a bit pattern 22316Salm * obuf bit pattern 22416Salm * kbuf the key itself 22516Salm */ 22616Salmvoid 2271057Salmexpand_des_key(char *obuf, char *kbuf) 2281057Salm{ 22916Salm int i, j; /* counter in a for loop */ 23016Salm int nbuf[64]; /* used for hex/key translation */ 23116Salm 23216Salm /* 23316Salm * leading '0x' or '0X' == hex key 23416Salm */ 23516Salm if (kbuf[0] == '0' && (kbuf[1] == 'x' || kbuf[1] == 'X')) { 23616Salm kbuf = &kbuf[2]; 23716Salm /* 23816Salm * now translate it, bombing on any illegal hex digit 23916Salm */ 24016Salm for (i = 0; i < 16 && kbuf[i]; i++) 24116Salm if ((nbuf[i] = hex_to_binary((int) kbuf[i], 16)) == -1) 24216Salm des_error("bad hex digit in key"); 24316Salm while (i < 16) 24416Salm nbuf[i++] = 0; 24516Salm for (i = 0; i < 8; i++) 24616Salm obuf[i] = 24716Salm ((nbuf[2*i]&0xf)<<4) | (nbuf[2*i+1]&0xf); 24816Salm /* preserve parity bits */ 24916Salm pflag = 1; 25016Salm return; 25116Salm } 25216Salm /* 25316Salm * leading '0b' or '0B' == binary key 25416Salm */ 25516Salm if (kbuf[0] == '0' && (kbuf[1] == 'b' || kbuf[1] == 'B')) { 25616Salm kbuf = &kbuf[2]; 25716Salm /* 25816Salm * now translate it, bombing on any illegal binary digit 25916Salm */ 2601057Salm for (i = 0; kbuf[i] && i < 16; i++) 26116Salm if ((nbuf[i] = hex_to_binary((int) kbuf[i], 2)) == -1) 26216Salm des_error("bad binary digit in key"); 26316Salm while (i < 64) 26416Salm nbuf[i++] = 0; 26516Salm for (i = 0; i < 8; i++) 26616Salm for (j = 0; j < 8; j++) 26716Salm obuf[i] = (obuf[i]<<1)|nbuf[8*i+j]; 26816Salm /* preserve parity bits */ 26916Salm pflag = 1; 27016Salm return; 27116Salm } 27216Salm /* 27316Salm * no special leader -- ASCII 27416Salm */ 27516Salm (void)strncpy(obuf, kbuf, 8); 2761057Salm} 2771057Salm 27816Salm/***************** 27916Salm * DES FUNCTIONS * 28016Salm *****************/ 28116Salm/* 28216Salm * This sets the DES key and (if you're using the deszip version) 28316Salm * the direction of the transformation. This uses the Sun 28416Salm * to map the 64-bit key onto the 56 bits that the key schedule 28516Salm * generation routines use: the old way, which just uses the user- 28616Salm * supplied 64 bits as is, and the new way, which resets the parity 28716Salm * bit to be the same as the low-order bit in each character. The 28816Salm * new way generates a greater variety of key schedules, since many 28916Salm * systems set the parity (high) bit of each character to 0, and the 29016Salm * DES ignores the low order bit of each character. 29116Salm */ 29216Salmvoid 29316Salmset_des_key(DES_cblock *buf) /* key block */ 29416Salm{ 29516Salm int i, j; /* counter in a for loop */ 2961057Salm int par; /* parity counter */ 2971057Salm 29816Salm /* 29916Salm * if the parity is not preserved, flip it 30016Salm */ 30116Salm if (!pflag) { 30216Salm for (i = 0; i < 8; i++) { 30316Salm par = 0; 30416Salm for (j = 1; j < 8; j++) 30516Salm if ((bits[j] & (*buf)[i]) != 0) 30616Salm par++; 30716Salm if ((par & 0x01) == 0x01) 30816Salm (*buf)[i] &= 0x7f; 30916Salm else 31016Salm (*buf)[i] = ((*buf)[i] & 0x7f) | 0x80; 31116Salm } 31216Salm } 31316Salm 31416Salm DES_set_odd_parity(buf); 31516Salm DES_set_key(buf, &schedule); 31616Salm} 31716Salm 31816Salm 31916Salm/* 32016Salm * This encrypts using the Cipher Block Chaining mode of DES 32116Salm */ 32216Salmint 32316Salmcbc_encode(unsigned char *msgbuf, int n, FILE *fp) 32416Salm{ 32516Salm int inverse = 0; /* 0 to encrypt, 1 to decrypt */ 32616Salm 32716Salm /* 3281057Salm * do the transformation 32916Salm */ 33016Salm if (n == 8) { 33116Salm for (n = 0; n < 8; n++) 33216Salm msgbuf[n] ^= ivec[n]; 33316Salm DES_XFORM((DES_cblock *)msgbuf); 33416Salm MEMCPY(ivec, msgbuf, 8); 33516Salm return WRITE(msgbuf, 8, fp); 33616Salm } 33716Salm /* 33816Salm * at EOF or last block -- in either case, the last byte contains 33916Salm * the character representation of the number of bytes in it 34016Salm */ 34116Salm/* 34216Salm MEMZERO(msgbuf + n, 8 - n); 34316Salm*/ 34416Salm /* 34516Salm * Pad the last block randomly 34616Salm */ 34716Salm (void)MEMCPY(msgbuf + n, pvec, 8 - n); 34816Salm msgbuf[7] = n; 34916Salm for (n = 0; n < 8; n++) 35016Salm msgbuf[n] ^= ivec[n]; 35116Salm DES_XFORM((DES_cblock *)msgbuf); 35216Salm return WRITE(msgbuf, 8, fp); 35316Salm} 35416Salm 35516Salm/* 35616Salm * This decrypts using the Cipher Block Chaining mode of DES 3571057Salm * msgbuf I/O buffer 3581057Salm * fp input file descriptor 35916Salm */ 36016Salmint 36116Salmcbc_decode(unsigned char *msgbuf, FILE *fp) 36216Salm{ 36316Salm DES_cblock tbuf; /* temp buffer for initialization vector */ 36416Salm int n; /* number of bytes actually read */ 36516Salm int c; /* used to test for EOF */ 36616Salm int inverse = 1; /* 0 to encrypt, 1 to decrypt */ 36716Salm 36816Salm if ((n = READ(msgbuf, 8, fp)) == 8) { 36916Salm /* 37016Salm * do the transformation 37116Salm */ 37216Salm MEMCPY(tbuf, msgbuf, 8); 37316Salm DES_XFORM((DES_cblock *)msgbuf); 37416Salm for (c = 0; c < 8; c++) 37516Salm msgbuf[c] ^= ivec[c]; 37616Salm MEMCPY(ivec, tbuf, 8); 37716Salm /* 37816Salm * if the last one, handle it specially 37916Salm */ 38016Salm if ((c = fgetc(fp)) == EOF) { 38116Salm n = msgbuf[7]; 38216Salm if (n < 0 || n > 7) { 38316Salm des_error("decryption failed (block corrupted)"); 38416Salm return EOF; 38516Salm } 38616Salm } else 38716Salm (void)ungetc(c, fp); 38816Salm return n; 38916Salm } 39016Salm if (n > 0) 39116Salm des_error("decryption failed (incomplete block)"); 39216Salm else if (n < 0) 39316Salm des_error("cannot read file"); 39416Salm return EOF; 39516Salm} 3961057Salm#endif /* DES */ 3971057Salm