cbc.c revision 99109
1/* cbc.c: This file contains the encryption routines for the ed line editor */ 2/*- 3 * Copyright (c) 1993 The Regents of the University of California. 4 * All rights reserved. 5 * 6 * Copyright (c) 1993 Andrew Moore, Talke Studio. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 */ 37 38#include <sys/cdefs.h> 39__FBSDID("$FreeBSD: head/bin/ed/cbc.c 99109 2002-06-30 05:13:54Z obrien $"); 40 41#include <sys/types.h> 42#include <errno.h> 43#include <pwd.h> 44#ifdef DES 45#include <time.h> 46#endif 47 48#include "ed.h" 49 50 51/* 52 * BSD and System V systems offer special library calls that do 53 * block move_liness and fills, so if possible we take advantage of them 54 */ 55#define MEMCPY(dest,src,len) memcpy((dest),(src),(len)) 56#define MEMZERO(dest,len) memset((dest), 0, (len)) 57 58/* Hide the calls to the primitive encryption routines. */ 59#define DES_KEY(buf) \ 60 if (des_setkey(buf)) \ 61 des_error("des_setkey"); 62#define DES_XFORM(buf) \ 63 if (des_cipher(buf, buf, 0L, (inverse ? -1 : 1))) \ 64 des_error("des_cipher"); 65 66/* 67 * read/write - no error checking 68 */ 69#define READ(buf, n, fp) fread(buf, sizeof(char), n, fp) 70#define WRITE(buf, n, fp) fwrite(buf, sizeof(char), n, fp) 71 72/* 73 * some things to make references easier 74 */ 75typedef char Desbuf[8]; 76#define CHAR(x,i) (x[i]) 77#define UCHAR(x,i) (x[i]) 78#define BUFFER(x) (x) 79#define UBUFFER(x) (x) 80 81/* 82 * global variables and related macros 83 */ 84 85enum { /* encrypt, decrypt, authenticate */ 86 MODE_ENCRYPT, MODE_DECRYPT, MODE_AUTHENTICATE 87} mode = MODE_ENCRYPT; 88 89Desbuf ivec; /* initialization vector */ 90Desbuf pvec; /* padding vector */ 91char bits[] = { /* used to extract bits from a char */ 92 '\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001' 93}; 94int pflag; /* 1 to preserve parity bits */ 95 96unsigned char des_buf[8]; /* shared buffer for get_des_char/put_des_char */ 97int des_ct = 0; /* count for get_des_char/put_des_char */ 98int des_n = 0; /* index for put_des_char/get_des_char */ 99 100 101/* init_des_cipher: initialize DES */ 102void 103init_des_cipher(void) 104{ 105#ifdef DES 106 int i; 107 108 des_ct = des_n = 0; 109 110 /* initialize the initialization vector */ 111 MEMZERO(ivec, 8); 112 113 /* initialize the padding vector */ 114 for (i = 0; i < 8; i++) 115 CHAR(pvec, i) = (char) (arc4random() % 256); 116#endif 117} 118 119 120/* get_des_char: return next char in an encrypted file */ 121int 122get_des_char(FILE *fp) 123{ 124#ifdef DES 125 if (des_n >= des_ct) { 126 des_n = 0; 127 des_ct = cbc_decode(des_buf, fp); 128 } 129 return (des_ct > 0) ? des_buf[des_n++] : EOF; 130#else 131 return (getc(fp)); 132#endif 133} 134 135 136/* put_des_char: write a char to an encrypted file; return char written */ 137int 138put_des_char(int c, FILE *fp) 139{ 140#ifdef DES 141 if (des_n == sizeof des_buf) { 142 des_ct = cbc_encode(des_buf, des_n, fp); 143 des_n = 0; 144 } 145 return (des_ct >= 0) ? (des_buf[des_n++] = c) : EOF; 146#else 147 return (fputc(c, fp)); 148#endif 149} 150 151 152/* flush_des_file: flush an encrypted file's output; return status */ 153int 154flush_des_file(FILE *fp) 155{ 156#ifdef DES 157 if (des_n == sizeof des_buf) { 158 des_ct = cbc_encode(des_buf, des_n, fp); 159 des_n = 0; 160 } 161 return (des_ct >= 0 && cbc_encode(des_buf, des_n, fp) >= 0) ? 0 : EOF; 162#else 163 return (fflush(fp)); 164#endif 165} 166 167#ifdef DES 168/* 169 * get keyword from tty or stdin 170 */ 171int 172get_keyword(void) 173{ 174 char *p; /* used to obtain the key */ 175 Desbuf msgbuf; /* I/O buffer */ 176 177 /* 178 * get the key 179 */ 180 if (*(p = getpass("Enter key: "))) { 181 182 /* 183 * copy it, nul-padded, into the key area 184 */ 185 expand_des_key(BUFFER(msgbuf), p); 186 MEMZERO(p, _PASSWORD_LEN); 187 set_des_key(msgbuf); 188 MEMZERO(msgbuf, sizeof msgbuf); 189 return 1; 190 } 191 return 0; 192} 193 194 195/* 196 * print a warning message and, possibly, terminate 197 */ 198void 199des_error(const char *s) 200{ 201 errmsg = s ? s : strerror(errno); 202} 203 204/* 205 * map a hex character to an integer 206 */ 207int 208hex_to_binary(int c, int radix) 209{ 210 switch(c) { 211 case '0': return(0x0); 212 case '1': return(0x1); 213 case '2': return(radix > 2 ? 0x2 : -1); 214 case '3': return(radix > 3 ? 0x3 : -1); 215 case '4': return(radix > 4 ? 0x4 : -1); 216 case '5': return(radix > 5 ? 0x5 : -1); 217 case '6': return(radix > 6 ? 0x6 : -1); 218 case '7': return(radix > 7 ? 0x7 : -1); 219 case '8': return(radix > 8 ? 0x8 : -1); 220 case '9': return(radix > 9 ? 0x9 : -1); 221 case 'A': case 'a': return(radix > 10 ? 0xa : -1); 222 case 'B': case 'b': return(radix > 11 ? 0xb : -1); 223 case 'C': case 'c': return(radix > 12 ? 0xc : -1); 224 case 'D': case 'd': return(radix > 13 ? 0xd : -1); 225 case 'E': case 'e': return(radix > 14 ? 0xe : -1); 226 case 'F': case 'f': return(radix > 15 ? 0xf : -1); 227 } 228 /* 229 * invalid character 230 */ 231 return(-1); 232} 233 234/* 235 * convert the key to a bit pattern 236 * obuf bit pattern 237 * kbuf the key itself 238 */ 239void 240expand_des_key(char *obuf, char *kbuf) 241{ 242 int i, j; /* counter in a for loop */ 243 int nbuf[64]; /* used for hex/key translation */ 244 245 /* 246 * leading '0x' or '0X' == hex key 247 */ 248 if (kbuf[0] == '0' && (kbuf[1] == 'x' || kbuf[1] == 'X')) { 249 kbuf = &kbuf[2]; 250 /* 251 * now translate it, bombing on any illegal hex digit 252 */ 253 for (i = 0; kbuf[i] && i < 16; i++) 254 if ((nbuf[i] = hex_to_binary((int) kbuf[i], 16)) == -1) 255 des_error("bad hex digit in key"); 256 while (i < 16) 257 nbuf[i++] = 0; 258 for (i = 0; i < 8; i++) 259 obuf[i] = 260 ((nbuf[2*i]&0xf)<<4) | (nbuf[2*i+1]&0xf); 261 /* preserve parity bits */ 262 pflag = 1; 263 return; 264 } 265 /* 266 * leading '0b' or '0B' == binary key 267 */ 268 if (kbuf[0] == '0' && (kbuf[1] == 'b' || kbuf[1] == 'B')) { 269 kbuf = &kbuf[2]; 270 /* 271 * now translate it, bombing on any illegal binary digit 272 */ 273 for (i = 0; kbuf[i] && i < 16; i++) 274 if ((nbuf[i] = hex_to_binary((int) kbuf[i], 2)) == -1) 275 des_error("bad binary digit in key"); 276 while (i < 64) 277 nbuf[i++] = 0; 278 for (i = 0; i < 8; i++) 279 for (j = 0; j < 8; j++) 280 obuf[i] = (obuf[i]<<1)|nbuf[8*i+j]; 281 /* preserve parity bits */ 282 pflag = 1; 283 return; 284 } 285 /* 286 * no special leader -- ASCII 287 */ 288 (void)strncpy(obuf, kbuf, 8); 289} 290 291/***************** 292 * DES FUNCTIONS * 293 *****************/ 294/* 295 * This sets the DES key and (if you're using the deszip version) 296 * the direction of the transformation. This uses the Sun 297 * to map the 64-bit key onto the 56 bits that the key schedule 298 * generation routines use: the old way, which just uses the user- 299 * supplied 64 bits as is, and the new way, which resets the parity 300 * bit to be the same as the low-order bit in each character. The 301 * new way generates a greater variety of key schedules, since many 302 * systems set the parity (high) bit of each character to 0, and the 303 * DES ignores the low order bit of each character. 304 */ 305void 306set_des_key(Desbuf buf) /* key block */ 307{ 308 int i, j; /* counter in a for loop */ 309 int par; /* parity counter */ 310 311 /* 312 * if the parity is not preserved, flip it 313 */ 314 if (!pflag) { 315 for (i = 0; i < 8; i++) { 316 par = 0; 317 for (j = 1; j < 8; j++) 318 if ((bits[j]&UCHAR(buf, i)) != 0) 319 par++; 320 if ((par&01) == 01) 321 UCHAR(buf, i) = UCHAR(buf, i)&0177; 322 else 323 UCHAR(buf, i) = (UCHAR(buf, i)&0177)|0200; 324 } 325 } 326 327 DES_KEY(UBUFFER(buf)); 328} 329 330 331/* 332 * This encrypts using the Cipher Block Chaining mode of DES 333 */ 334int 335cbc_encode(char *msgbuf, int n, FILE *fp) 336{ 337 int inverse = 0; /* 0 to encrypt, 1 to decrypt */ 338 339 /* 340 * do the transformation 341 */ 342 if (n == 8) { 343 for (n = 0; n < 8; n++) 344 CHAR(msgbuf, n) ^= CHAR(ivec, n); 345 DES_XFORM(UBUFFER(msgbuf)); 346 MEMCPY(BUFFER(ivec), BUFFER(msgbuf), 8); 347 return WRITE(BUFFER(msgbuf), 8, fp); 348 } 349 /* 350 * at EOF or last block -- in either case, the last byte contains 351 * the character representation of the number of bytes in it 352 */ 353/* 354 MEMZERO(msgbuf + n, 8 - n); 355*/ 356 /* 357 * Pad the last block randomly 358 */ 359 (void)MEMCPY(BUFFER(msgbuf + n), BUFFER(pvec), 8 - n); 360 CHAR(msgbuf, 7) = n; 361 for (n = 0; n < 8; n++) 362 CHAR(msgbuf, n) ^= CHAR(ivec, n); 363 DES_XFORM(UBUFFER(msgbuf)); 364 return WRITE(BUFFER(msgbuf), 8, fp); 365} 366 367/* 368 * This decrypts using the Cipher Block Chaining mode of DES 369 * msgbuf I/O buffer 370 * fp input file descriptor 371 */ 372int 373cbc_decode(char *msgbuf, FILE *fp) 374{ 375 Desbuf tbuf; /* temp buffer for initialization vector */ 376 int n; /* number of bytes actually read */ 377 int c; /* used to test for EOF */ 378 int inverse = 1; /* 0 to encrypt, 1 to decrypt */ 379 380 if ((n = READ(BUFFER(msgbuf), 8, fp)) == 8) { 381 /* 382 * do the transformation 383 */ 384 MEMCPY(BUFFER(tbuf), BUFFER(msgbuf), 8); 385 DES_XFORM(UBUFFER(msgbuf)); 386 for (c = 0; c < 8; c++) 387 UCHAR(msgbuf, c) ^= UCHAR(ivec, c); 388 MEMCPY(BUFFER(ivec), BUFFER(tbuf), 8); 389 /* 390 * if the last one, handle it specially 391 */ 392 if ((c = fgetc(fp)) == EOF) { 393 n = CHAR(msgbuf, 7); 394 if (n < 0 || n > 7) { 395 des_error("decryption failed (block corrupted)"); 396 return EOF; 397 } 398 } else 399 (void)ungetc(c, fp); 400 return n; 401 } 402 if (n > 0) 403 des_error("decryption failed (incomplete block)"); 404 else if (n < 0) 405 des_error("cannot read file"); 406 return EOF; 407} 408#endif /* DES */ 409