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