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 * from: @(#)bdes.c 5.5 (Berkeley) 6/27/91 38 * 39 * @(#)cbc.c,v 1.2 1994/02/01 00:34:36 alm Exp 40 * $FreeBSD: src/bin/ed/cbc.c,v 1.20 2004/04/06 20:06:47 markm Exp $ 41 * $DragonFly: src/bin/ed/cbc.c,v 1.10 2007/04/06 23:36:54 pavalos Exp $ 42 */ 43 44 #include <sys/types.h> 45 #include <errno.h> 46 #include <pwd.h> 47 #ifdef DES 48 #include <time.h> 49 #include <openssl/des.h> 50 #define ED_DES_INCLUDES 51 #endif 52 53 #include "ed.h" 54 55 56 /* 57 * BSD and System V systems offer special library calls that do 58 * block move_liness and fills, so if possible we take advantage of them 59 */ 60 #define MEMCPY(dest,src,len) memcpy((dest),(src),(len)) 61 #define MEMZERO(dest,len) memset((dest), 0, (len)) 62 63 /* Hide the calls to the primitive encryption routines. */ 64 #define DES_XFORM(buf) \ 65 DES_ecb_encrypt(buf, buf, &schedule, \ 66 inverse ? DES_DECRYPT : DES_ENCRYPT); 67 /* 68 * read/write - no error checking 69 */ 70 #define READ(buf, n, fp) fread(buf, sizeof(char), n, fp) 71 #define WRITE(buf, n, fp) fwrite(buf, sizeof(char), n, fp) 72 73 /* 74 * global variables and related macros 75 */ 76 77 enum { /* encrypt, decrypt, authenticate */ 78 MODE_ENCRYPT, MODE_DECRYPT, MODE_AUTHENTICATE 79 } mode = MODE_ENCRYPT; 80 81 #ifdef DES 82 DES_cblock ivec; /* initialization vector */ 83 DES_cblock pvec; /* padding vector */ 84 #endif 85 86 char bits[] = { /* used to extract bits from a char */ 87 '\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001' 88 }; 89 90 int pflag; /* 1 to preserve parity bits */ 91 92 #ifdef DES 93 DES_key_schedule schedule; /* expanded DES key */ 94 #endif 95 96 char des_buf[8]; /* shared buffer for get_des_char/put_des_char */ 97 int des_ct = 0; /* count for get_des_char/put_des_char */ 98 int des_n = 0; /* index for put_des_char/get_des_char */ 99 100 101 /* init_des_cipher: initialize DES */ 102 void 103 init_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 pvec[i] = (char) (arc4random() % 256); 116 #endif 117 } 118 119 120 /* get_des_char: return next char in an encrypted file */ 121 int 122 get_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) ? (unsigned char)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 */ 137 int 138 put_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++] = (char)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 */ 153 int 154 flush_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 */ 171 int 172 get_keyword(void) 173 { 174 char *p; /* used to obtain the key */ 175 DES_cblock 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(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 */ 198 void 199 des_error(const char *s) 200 { 201 errmsg = s ? s : strerror(errno); 202 } 203 204 /* 205 * map a hex character to an integer 206 */ 207 int 208 hex_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 * inbuf the key itself 238 */ 239 void 240 expand_des_key(char *obuf, char *inbuf) 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 (inbuf[0] == '0' && (inbuf[1] == 'x' || inbuf[1] == 'X')) { 249 inbuf = &inbuf[2]; 250 /* 251 * now translate it, bombing on any illegal hex digit 252 */ 253 for (i = 0; inbuf[i] && i < 16; i++) 254 if ((nbuf[i] = hex_to_binary((int) inbuf[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 (inbuf[0] == '0' && (inbuf[1] == 'b' || inbuf[1] == 'B')) { 269 inbuf = &inbuf[2]; 270 /* 271 * now translate it, bombing on any illegal binary digit 272 */ 273 for (i = 0; inbuf[i] && i < 16; i++) 274 if ((nbuf[i] = hex_to_binary((int) inbuf[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 strncpy(obuf, inbuf, 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 */ 305 void 306 set_des_key(DES_cblock *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] & (*buf)[i]) != 0) 319 par++; 320 if ((par & 0x01) == 0x01) 321 (*buf)[i] &= 0x7f; 322 else 323 (*buf)[i] = ((*buf)[i] & 0x7f) | 0x80; 324 } 325 } 326 327 DES_set_odd_parity(buf); 328 DES_set_key(buf, &schedule); 329 } 330 331 332 /* 333 * This encrypts using the Cipher Block Chaining mode of DES 334 */ 335 int 336 cbc_encode(char *msgbuf, int n, FILE *fp) 337 { 338 int inverse = 0; /* 0 to encrypt, 1 to decrypt */ 339 340 /* 341 * do the transformation 342 */ 343 if (n == 8) { 344 for (n = 0; n < 8; n++) 345 msgbuf[n] ^= ivec[n]; 346 DES_XFORM((DES_cblock *)msgbuf); 347 MEMCPY(ivec, msgbuf, 8); 348 return WRITE(msgbuf, 8, fp); 349 } 350 /* 351 * at EOF or last block -- in either case, the last byte contains 352 * the character representation of the number of bytes in it 353 */ 354 /* 355 MEMZERO(msgbuf + n, 8 - n); 356 */ 357 /* 358 * Pad the last block randomly 359 */ 360 MEMCPY(msgbuf + n, pvec, 8 - n); 361 msgbuf[7] = n; 362 for (n = 0; n < 8; n++) 363 msgbuf[n] ^= ivec[n]; 364 DES_XFORM((DES_cblock *)msgbuf); 365 return WRITE(msgbuf, 8, fp); 366 } 367 368 /* 369 * This decrypts using the Cipher Block Chaining mode of DES 370 * msgbuf I/O buffer 371 * fp input file descriptor 372 */ 373 int 374 cbc_decode(char *msgbuf, FILE *fp) 375 { 376 DES_cblock inbuf; /* temp buffer for initialization vector */ 377 int n; /* number of bytes actually read */ 378 int c; /* used to test for EOF */ 379 int inverse = 1; /* 0 to encrypt, 1 to decrypt */ 380 381 if ((n = READ(msgbuf, 8, fp)) == 8) { 382 /* 383 * do the transformation 384 */ 385 MEMCPY(inbuf, msgbuf, 8); 386 DES_XFORM((DES_cblock *)msgbuf); 387 for (c = 0; c < 8; c++) 388 msgbuf[c] ^= ivec[c]; 389 MEMCPY(ivec, inbuf, 8); 390 /* 391 * if the last one, handle it specially 392 */ 393 if ((c = fgetc(fp)) == EOF) { 394 n = msgbuf[7]; 395 if (n < 0 || n > 7) { 396 des_error("decryption failed (block corrupted)"); 397 return EOF; 398 } 399 } else 400 ungetc(c, fp); 401 return n; 402 } 403 if (n > 0) 404 des_error("decryption failed (incomplete block)"); 405 else if (n < 0) 406 des_error("cannot read file"); 407 return EOF; 408 } 409 #endif /* DES */ 410