cbc.c revision 115777
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 3899109Sobrien#include <sys/cdefs.h> 3999109Sobrien__FBSDID("$FreeBSD: head/bin/ed/cbc.c 115777 2003-06-03 17:03:48Z jhay $"); 4016Salm 411057Salm#include <sys/types.h> 4216Salm#include <errno.h> 4316Salm#include <pwd.h> 4427963Ssteve#ifdef DES 4527963Ssteve#include <time.h> 46115717Smarkm#include <openssl/des.h> 47115717Smarkm#define ED_DES_INCLUDES 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. */ 61101093Smarkm#define DES_XFORM(buf) \ 62115717Smarkm DES_ecb_encrypt(buf, buf, &schedule, \ 63115717Smarkm inverse ? DES_DECRYPT : DES_ENCRYPT); 6416Salm 6516Salm/* 6616Salm * read/write - no error checking 6716Salm */ 6816Salm#define READ(buf, n, fp) fread(buf, sizeof(char), n, fp) 6916Salm#define WRITE(buf, n, fp) fwrite(buf, sizeof(char), n, fp) 7016Salm 7116Salm/* 7216Salm * global variables and related macros 7316Salm */ 7416Salm 7516Salmenum { /* encrypt, decrypt, authenticate */ 7616Salm MODE_ENCRYPT, MODE_DECRYPT, MODE_AUTHENTICATE 7716Salm} mode = MODE_ENCRYPT; 7816Salm 79115777Sjhay#ifdef DES 80115717SmarkmDES_cblock ivec; /* initialization vector */ 81115717SmarkmDES_cblock pvec; /* padding vector */ 82115777Sjhay#endif 83115717Smarkm 8416Salmchar bits[] = { /* used to extract bits from a char */ 8516Salm '\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001' 8616Salm}; 87115717Smarkm 8816Salmint pflag; /* 1 to preserve parity bits */ 8916Salm 90115777Sjhay#ifdef DES 91115717SmarkmDES_key_schedule schedule; /* expanded DES key */ 92115777Sjhay#endif 93115717Smarkm 941057Salmunsigned char des_buf[8]; /* shared buffer for get_des_char/put_des_char */ 951057Salmint des_ct = 0; /* count for get_des_char/put_des_char */ 961057Salmint des_n = 0; /* index for put_des_char/get_des_char */ 9716Salm 981057Salm/* init_des_cipher: initialize DES */ 9916Salmvoid 10090109Simpinit_des_cipher(void) 10116Salm{ 10216Salm#ifdef DES 10316Salm int i; 10416Salm 10516Salm des_ct = des_n = 0; 10616Salm 1077165Sjoerg /* initialize the initialization vector */ 10816Salm MEMZERO(ivec, 8); 10916Salm 11046684Skris /* initialize the padding vector */ 11116Salm for (i = 0; i < 8; i++) 112115717Smarkm pvec[i] = (char) (arc4random() % 256); 11316Salm#endif 11416Salm} 11516Salm 11616Salm 1171057Salm/* get_des_char: return next char in an encrypted file */ 1181057Salmint 11990109Simpget_des_char(FILE *fp) 12016Salm{ 12116Salm#ifdef DES 12216Salm if (des_n >= des_ct) { 12316Salm des_n = 0; 1241057Salm des_ct = cbc_decode(des_buf, fp); 12516Salm } 12616Salm return (des_ct > 0) ? des_buf[des_n++] : EOF; 1277165Sjoerg#else 1287165Sjoerg return (getc(fp)); 12916Salm#endif 13016Salm} 13116Salm 13216Salm 1331057Salm/* put_des_char: write a char to an encrypted file; return char written */ 1341057Salmint 13590109Simpput_des_char(int c, FILE *fp) 13616Salm{ 13716Salm#ifdef DES 13816Salm if (des_n == sizeof des_buf) { 1391057Salm des_ct = cbc_encode(des_buf, des_n, fp); 14016Salm des_n = 0; 14116Salm } 14216Salm return (des_ct >= 0) ? (des_buf[des_n++] = c) : EOF; 1437165Sjoerg#else 1447165Sjoerg return (fputc(c, fp)); 14516Salm#endif 14616Salm} 14716Salm 14816Salm 1491057Salm/* flush_des_file: flush an encrypted file's output; return status */ 1501057Salmint 15190109Simpflush_des_file(FILE *fp) 15216Salm{ 15316Salm#ifdef DES 15416Salm if (des_n == sizeof des_buf) { 1551057Salm des_ct = cbc_encode(des_buf, des_n, fp); 15616Salm des_n = 0; 15716Salm } 1581057Salm return (des_ct >= 0 && cbc_encode(des_buf, des_n, fp) >= 0) ? 0 : EOF; 1597165Sjoerg#else 1607165Sjoerg return (fflush(fp)); 16116Salm#endif 16216Salm} 16316Salm 16416Salm#ifdef DES 16516Salm/* 16616Salm * get keyword from tty or stdin 16716Salm */ 1681057Salmint 16990109Simpget_keyword(void) 17016Salm{ 17181220Smike char *p; /* used to obtain the key */ 172115717Smarkm DES_cblock msgbuf; /* I/O buffer */ 17316Salm 17416Salm /* 17516Salm * get the key 17616Salm */ 17716Salm if (*(p = getpass("Enter key: "))) { 17816Salm 17916Salm /* 18016Salm * copy it, nul-padded, into the key area 18116Salm */ 182115717Smarkm expand_des_key(msgbuf, p); 18316Salm MEMZERO(p, _PASSWORD_LEN); 184115717Smarkm set_des_key(&msgbuf); 18516Salm MEMZERO(msgbuf, sizeof msgbuf); 18616Salm return 1; 18716Salm } 18816Salm return 0; 18916Salm} 19016Salm 19116Salm 19216Salm/* 19316Salm * print a warning message and, possibly, terminate 19416Salm */ 19516Salmvoid 19690109Simpdes_error(const char *s) 19716Salm{ 19881220Smike errmsg = s ? s : strerror(errno); 19916Salm} 20016Salm 20116Salm/* 20216Salm * map a hex character to an integer 20316Salm */ 2041057Salmint 20590109Simphex_to_binary(int c, int radix) 20616Salm{ 20716Salm switch(c) { 20816Salm case '0': return(0x0); 20916Salm case '1': return(0x1); 21016Salm case '2': return(radix > 2 ? 0x2 : -1); 21116Salm case '3': return(radix > 3 ? 0x3 : -1); 21216Salm case '4': return(radix > 4 ? 0x4 : -1); 21316Salm case '5': return(radix > 5 ? 0x5 : -1); 21416Salm case '6': return(radix > 6 ? 0x6 : -1); 21516Salm case '7': return(radix > 7 ? 0x7 : -1); 21616Salm case '8': return(radix > 8 ? 0x8 : -1); 21716Salm case '9': return(radix > 9 ? 0x9 : -1); 21816Salm case 'A': case 'a': return(radix > 10 ? 0xa : -1); 21916Salm case 'B': case 'b': return(radix > 11 ? 0xb : -1); 22016Salm case 'C': case 'c': return(radix > 12 ? 0xc : -1); 22116Salm case 'D': case 'd': return(radix > 13 ? 0xd : -1); 22216Salm case 'E': case 'e': return(radix > 14 ? 0xe : -1); 22316Salm case 'F': case 'f': return(radix > 15 ? 0xf : -1); 22416Salm } 22516Salm /* 22616Salm * invalid character 22716Salm */ 22816Salm return(-1); 22916Salm} 23016Salm 23116Salm/* 23216Salm * convert the key to a bit pattern 23390109Simp * obuf bit pattern 23490109Simp * kbuf the key itself 23516Salm */ 23616Salmvoid 23790109Simpexpand_des_key(char *obuf, char *kbuf) 23816Salm{ 23981220Smike int i, j; /* counter in a for loop */ 24016Salm int nbuf[64]; /* used for hex/key translation */ 24116Salm 24216Salm /* 24316Salm * leading '0x' or '0X' == hex key 24416Salm */ 24581220Smike if (kbuf[0] == '0' && (kbuf[1] == 'x' || kbuf[1] == 'X')) { 24681220Smike kbuf = &kbuf[2]; 24716Salm /* 24816Salm * now translate it, bombing on any illegal hex digit 24916Salm */ 25081220Smike for (i = 0; kbuf[i] && i < 16; i++) 25181220Smike if ((nbuf[i] = hex_to_binary((int) kbuf[i], 16)) == -1) 2521057Salm des_error("bad hex digit in key"); 25316Salm while (i < 16) 25416Salm nbuf[i++] = 0; 25516Salm for (i = 0; i < 8; i++) 25616Salm obuf[i] = 25716Salm ((nbuf[2*i]&0xf)<<4) | (nbuf[2*i+1]&0xf); 25816Salm /* preserve parity bits */ 25916Salm pflag = 1; 26016Salm return; 26116Salm } 26216Salm /* 26316Salm * leading '0b' or '0B' == binary key 26416Salm */ 26581220Smike if (kbuf[0] == '0' && (kbuf[1] == 'b' || kbuf[1] == 'B')) { 26681220Smike kbuf = &kbuf[2]; 26716Salm /* 26816Salm * now translate it, bombing on any illegal binary digit 26916Salm */ 27081220Smike for (i = 0; kbuf[i] && i < 16; i++) 27181220Smike if ((nbuf[i] = hex_to_binary((int) kbuf[i], 2)) == -1) 2721057Salm des_error("bad binary digit in key"); 27316Salm while (i < 64) 27416Salm nbuf[i++] = 0; 27516Salm for (i = 0; i < 8; i++) 27616Salm for (j = 0; j < 8; j++) 27716Salm obuf[i] = (obuf[i]<<1)|nbuf[8*i+j]; 27816Salm /* preserve parity bits */ 27916Salm pflag = 1; 28016Salm return; 28116Salm } 28216Salm /* 28316Salm * no special leader -- ASCII 28416Salm */ 28581220Smike (void)strncpy(obuf, kbuf, 8); 28616Salm} 28716Salm 28816Salm/***************** 28916Salm * DES FUNCTIONS * 29016Salm *****************/ 29116Salm/* 29216Salm * This sets the DES key and (if you're using the deszip version) 29316Salm * the direction of the transformation. This uses the Sun 29416Salm * to map the 64-bit key onto the 56 bits that the key schedule 29516Salm * generation routines use: the old way, which just uses the user- 29616Salm * supplied 64 bits as is, and the new way, which resets the parity 29716Salm * bit to be the same as the low-order bit in each character. The 29816Salm * new way generates a greater variety of key schedules, since many 29916Salm * systems set the parity (high) bit of each character to 0, and the 30016Salm * DES ignores the low order bit of each character. 30116Salm */ 30216Salmvoid 303115717Smarkmset_des_key(DES_cblock *buf) /* key block */ 30416Salm{ 30581220Smike int i, j; /* counter in a for loop */ 30681220Smike int par; /* parity counter */ 30716Salm 30816Salm /* 30916Salm * if the parity is not preserved, flip it 31016Salm */ 31116Salm if (!pflag) { 31216Salm for (i = 0; i < 8; i++) { 31316Salm par = 0; 31416Salm for (j = 1; j < 8; j++) 315115717Smarkm if ((bits[j] & (*buf)[i]) != 0) 31616Salm par++; 317115717Smarkm if ((par & 0x01) == 0x01) 318115717Smarkm (*buf)[i] &= 0x7f; 31916Salm else 320115717Smarkm (*buf)[i] = ((*buf)[i] & 0x7f) | 0x80; 32116Salm } 32216Salm } 32316Salm 324115717Smarkm DES_set_odd_parity(buf); 325115717Smarkm DES_set_key(buf, &schedule); 32616Salm} 32716Salm 32816Salm 32916Salm/* 33016Salm * This encrypts using the Cipher Block Chaining mode of DES 33116Salm */ 3321057Salmint 333101093Smarkmcbc_encode(unsigned char *msgbuf, int n, FILE *fp) 33416Salm{ 33516Salm int inverse = 0; /* 0 to encrypt, 1 to decrypt */ 33616Salm 33716Salm /* 33816Salm * do the transformation 33916Salm */ 34016Salm if (n == 8) { 34116Salm for (n = 0; n < 8; n++) 342115717Smarkm msgbuf[n] ^= ivec[n]; 343115717Smarkm DES_XFORM((DES_cblock *)msgbuf); 344115717Smarkm MEMCPY(ivec, msgbuf, 8); 345115717Smarkm return WRITE(msgbuf, 8, fp); 34616Salm } 34716Salm /* 34816Salm * at EOF or last block -- in either case, the last byte contains 34916Salm * the character representation of the number of bytes in it 35016Salm */ 35116Salm/* 35216Salm MEMZERO(msgbuf + n, 8 - n); 35316Salm*/ 35416Salm /* 35516Salm * Pad the last block randomly 35616Salm */ 357115717Smarkm (void)MEMCPY(msgbuf + n, pvec, 8 - n); 358115717Smarkm msgbuf[7] = n; 35916Salm for (n = 0; n < 8; n++) 360115717Smarkm msgbuf[n] ^= ivec[n]; 361115717Smarkm DES_XFORM((DES_cblock *)msgbuf); 362115717Smarkm return WRITE(msgbuf, 8, fp); 36316Salm} 36416Salm 36516Salm/* 36616Salm * This decrypts using the Cipher Block Chaining mode of DES 36790109Simp * msgbuf I/O buffer 36890109Simp * fp input file descriptor 36916Salm */ 3701057Salmint 371101093Smarkmcbc_decode(unsigned char *msgbuf, FILE *fp) 37216Salm{ 373115717Smarkm DES_cblock tbuf; /* temp buffer for initialization vector */ 37481220Smike int n; /* number of bytes actually read */ 37581220Smike int c; /* used to test for EOF */ 37616Salm int inverse = 1; /* 0 to encrypt, 1 to decrypt */ 37716Salm 378115717Smarkm if ((n = READ(msgbuf, 8, fp)) == 8) { 37916Salm /* 38016Salm * do the transformation 38116Salm */ 382115717Smarkm MEMCPY(tbuf, msgbuf, 8); 383115717Smarkm DES_XFORM((DES_cblock *)msgbuf); 38416Salm for (c = 0; c < 8; c++) 385115717Smarkm msgbuf[c] ^= ivec[c]; 386115717Smarkm MEMCPY(ivec, tbuf, 8); 38716Salm /* 38816Salm * if the last one, handle it specially 38916Salm */ 39016Salm if ((c = fgetc(fp)) == EOF) { 391115717Smarkm n = msgbuf[7]; 39216Salm if (n < 0 || n > 7) { 3931057Salm des_error("decryption failed (block corrupted)"); 39416Salm return EOF; 39516Salm } 39616Salm } else 39716Salm (void)ungetc(c, fp); 39816Salm return n; 39916Salm } 40016Salm if (n > 0) 4011057Salm des_error("decryption failed (incomplete block)"); 40216Salm else if (n < 0) 4031057Salm des_error("cannot read file"); 40416Salm return EOF; 40516Salm} 40616Salm#endif /* DES */ 407