cbc.c (81220) | cbc.c (90109) |
---|---|
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 * --- 23 unchanged lines hidden (view full) --- 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#ifndef lint 39static const char rcsid[] = | 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 * --- 23 unchanged lines hidden (view full) --- 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#ifndef lint 39static const char rcsid[] = |
40 "$FreeBSD: head/bin/ed/cbc.c 81220 2001-08-06 22:01:31Z mike $"; | 40 "$FreeBSD: head/bin/ed/cbc.c 90109 2002-02-02 06:36:49Z imp $"; |
41#endif /* not lint */ 42 43#include <sys/types.h> 44#include <errno.h> 45#include <pwd.h> 46#ifdef DES 47#include <time.h> 48#endif --- 48 unchanged lines hidden (view full) --- 97 98unsigned char des_buf[8]; /* shared buffer for get_des_char/put_des_char */ 99int des_ct = 0; /* count for get_des_char/put_des_char */ 100int des_n = 0; /* index for put_des_char/get_des_char */ 101 102 103/* init_des_cipher: initialize DES */ 104void | 41#endif /* not lint */ 42 43#include <sys/types.h> 44#include <errno.h> 45#include <pwd.h> 46#ifdef DES 47#include <time.h> 48#endif --- 48 unchanged lines hidden (view full) --- 97 98unsigned char des_buf[8]; /* shared buffer for get_des_char/put_des_char */ 99int des_ct = 0; /* count for get_des_char/put_des_char */ 100int des_n = 0; /* index for put_des_char/get_des_char */ 101 102 103/* init_des_cipher: initialize DES */ 104void |
105init_des_cipher() | 105init_des_cipher(void) |
106{ 107#ifdef DES 108 int i; 109 110 des_ct = des_n = 0; 111 112 /* initialize the initialization vector */ 113 MEMZERO(ivec, 8); 114 115 /* initialize the padding vector */ 116 for (i = 0; i < 8; i++) 117 CHAR(pvec, i) = (char) (arc4random() % 256); 118#endif 119} 120 121 122/* get_des_char: return next char in an encrypted file */ 123int | 106{ 107#ifdef DES 108 int i; 109 110 des_ct = des_n = 0; 111 112 /* initialize the initialization vector */ 113 MEMZERO(ivec, 8); 114 115 /* initialize the padding vector */ 116 for (i = 0; i < 8; i++) 117 CHAR(pvec, i) = (char) (arc4random() % 256); 118#endif 119} 120 121 122/* get_des_char: return next char in an encrypted file */ 123int |
124get_des_char(fp) 125 FILE *fp; | 124get_des_char(FILE *fp) |
126{ 127#ifdef DES 128 if (des_n >= des_ct) { 129 des_n = 0; 130 des_ct = cbc_decode(des_buf, fp); 131 } 132 return (des_ct > 0) ? des_buf[des_n++] : EOF; 133#else 134 return (getc(fp)); 135#endif 136} 137 138 139/* put_des_char: write a char to an encrypted file; return char written */ 140int | 125{ 126#ifdef DES 127 if (des_n >= des_ct) { 128 des_n = 0; 129 des_ct = cbc_decode(des_buf, fp); 130 } 131 return (des_ct > 0) ? des_buf[des_n++] : EOF; 132#else 133 return (getc(fp)); 134#endif 135} 136 137 138/* put_des_char: write a char to an encrypted file; return char written */ 139int |
141put_des_char(c, fp) 142 int c; 143 FILE *fp; | 140put_des_char(int c, FILE *fp) |
144{ 145#ifdef DES 146 if (des_n == sizeof des_buf) { 147 des_ct = cbc_encode(des_buf, des_n, fp); 148 des_n = 0; 149 } 150 return (des_ct >= 0) ? (des_buf[des_n++] = c) : EOF; 151#else 152 return (fputc(c, fp)); 153#endif 154} 155 156 157/* flush_des_file: flush an encrypted file's output; return status */ 158int | 141{ 142#ifdef DES 143 if (des_n == sizeof des_buf) { 144 des_ct = cbc_encode(des_buf, des_n, fp); 145 des_n = 0; 146 } 147 return (des_ct >= 0) ? (des_buf[des_n++] = c) : EOF; 148#else 149 return (fputc(c, fp)); 150#endif 151} 152 153 154/* flush_des_file: flush an encrypted file's output; return status */ 155int |
159flush_des_file(fp) 160 FILE *fp; | 156flush_des_file(FILE *fp) |
161{ 162#ifdef DES 163 if (des_n == sizeof des_buf) { 164 des_ct = cbc_encode(des_buf, des_n, fp); 165 des_n = 0; 166 } 167 return (des_ct >= 0 && cbc_encode(des_buf, des_n, fp) >= 0) ? 0 : EOF; 168#else 169 return (fflush(fp)); 170#endif 171} 172 173#ifdef DES 174/* 175 * get keyword from tty or stdin 176 */ 177int | 157{ 158#ifdef DES 159 if (des_n == sizeof des_buf) { 160 des_ct = cbc_encode(des_buf, des_n, fp); 161 des_n = 0; 162 } 163 return (des_ct >= 0 && cbc_encode(des_buf, des_n, fp) >= 0) ? 0 : EOF; 164#else 165 return (fflush(fp)); 166#endif 167} 168 169#ifdef DES 170/* 171 * get keyword from tty or stdin 172 */ 173int |
178get_keyword() | 174get_keyword(void) |
179{ 180 char *p; /* used to obtain the key */ 181 Desbuf msgbuf; /* I/O buffer */ 182 183 /* 184 * get the key 185 */ 186 if (*(p = getpass("Enter key: "))) { --- 10 unchanged lines hidden (view full) --- 197 return 0; 198} 199 200 201/* 202 * print a warning message and, possibly, terminate 203 */ 204void | 175{ 176 char *p; /* used to obtain the key */ 177 Desbuf msgbuf; /* I/O buffer */ 178 179 /* 180 * get the key 181 */ 182 if (*(p = getpass("Enter key: "))) { --- 10 unchanged lines hidden (view full) --- 193 return 0; 194} 195 196 197/* 198 * print a warning message and, possibly, terminate 199 */ 200void |
205des_error(s) 206 const char *s; /* the message */ | 201des_error(const char *s) |
207{ 208 errmsg = s ? s : strerror(errno); 209} 210 211/* 212 * map a hex character to an integer 213 */ 214int | 202{ 203 errmsg = s ? s : strerror(errno); 204} 205 206/* 207 * map a hex character to an integer 208 */ 209int |
215hex_to_binary(c, radix) 216 int c; /* char to be converted */ 217 int radix; /* base (2 to 16) */ | 210hex_to_binary(int c, int radix) |
218{ 219 switch(c) { 220 case '0': return(0x0); 221 case '1': return(0x1); 222 case '2': return(radix > 2 ? 0x2 : -1); 223 case '3': return(radix > 3 ? 0x3 : -1); 224 case '4': return(radix > 4 ? 0x4 : -1); 225 case '5': return(radix > 5 ? 0x5 : -1); --- 11 unchanged lines hidden (view full) --- 237 /* 238 * invalid character 239 */ 240 return(-1); 241} 242 243/* 244 * convert the key to a bit pattern | 211{ 212 switch(c) { 213 case '0': return(0x0); 214 case '1': return(0x1); 215 case '2': return(radix > 2 ? 0x2 : -1); 216 case '3': return(radix > 3 ? 0x3 : -1); 217 case '4': return(radix > 4 ? 0x4 : -1); 218 case '5': return(radix > 5 ? 0x5 : -1); --- 11 unchanged lines hidden (view full) --- 230 /* 231 * invalid character 232 */ 233 return(-1); 234} 235 236/* 237 * convert the key to a bit pattern |
238 * obuf bit pattern 239 * kbuf the key itself |
|
245 */ 246void | 240 */ 241void |
247expand_des_key(obuf, kbuf) 248 char *obuf; /* bit pattern */ 249 char *kbuf; /* the key itself */ | 242expand_des_key(char *obuf, char *kbuf) |
250{ 251 int i, j; /* counter in a for loop */ 252 int nbuf[64]; /* used for hex/key translation */ 253 254 /* 255 * leading '0x' or '0X' == hex key 256 */ 257 if (kbuf[0] == '0' && (kbuf[1] == 'x' || kbuf[1] == 'X')) { --- 49 unchanged lines hidden (view full) --- 307 * generation routines use: the old way, which just uses the user- 308 * supplied 64 bits as is, and the new way, which resets the parity 309 * bit to be the same as the low-order bit in each character. The 310 * new way generates a greater variety of key schedules, since many 311 * systems set the parity (high) bit of each character to 0, and the 312 * DES ignores the low order bit of each character. 313 */ 314void | 243{ 244 int i, j; /* counter in a for loop */ 245 int nbuf[64]; /* used for hex/key translation */ 246 247 /* 248 * leading '0x' or '0X' == hex key 249 */ 250 if (kbuf[0] == '0' && (kbuf[1] == 'x' || kbuf[1] == 'X')) { --- 49 unchanged lines hidden (view full) --- 300 * generation routines use: the old way, which just uses the user- 301 * supplied 64 bits as is, and the new way, which resets the parity 302 * bit to be the same as the low-order bit in each character. The 303 * new way generates a greater variety of key schedules, since many 304 * systems set the parity (high) bit of each character to 0, and the 305 * DES ignores the low order bit of each character. 306 */ 307void |
315set_des_key(buf) 316 Desbuf buf; /* key block */ | 308set_des_key(Desbuf buf) /* key block */ |
317{ 318 int i, j; /* counter in a for loop */ 319 int par; /* parity counter */ 320 321 /* 322 * if the parity is not preserved, flip it 323 */ 324 if (!pflag) { --- 12 unchanged lines hidden (view full) --- 337 DES_KEY(UBUFFER(buf)); 338} 339 340 341/* 342 * This encrypts using the Cipher Block Chaining mode of DES 343 */ 344int | 309{ 310 int i, j; /* counter in a for loop */ 311 int par; /* parity counter */ 312 313 /* 314 * if the parity is not preserved, flip it 315 */ 316 if (!pflag) { --- 12 unchanged lines hidden (view full) --- 329 DES_KEY(UBUFFER(buf)); 330} 331 332 333/* 334 * This encrypts using the Cipher Block Chaining mode of DES 335 */ 336int |
345cbc_encode(msgbuf, n, fp) 346 char *msgbuf; 347 int n; 348 FILE *fp; | 337cbc_encode(char *msgbuf, int n, FILE *fp) |
349{ 350 int inverse = 0; /* 0 to encrypt, 1 to decrypt */ 351 352 /* 353 * do the transformation 354 */ 355 if (n == 8) { 356 for (n = 0; n < 8; n++) --- 17 unchanged lines hidden (view full) --- 374 for (n = 0; n < 8; n++) 375 CHAR(msgbuf, n) ^= CHAR(ivec, n); 376 DES_XFORM(UBUFFER(msgbuf)); 377 return WRITE(BUFFER(msgbuf), 8, fp); 378} 379 380/* 381 * This decrypts using the Cipher Block Chaining mode of DES | 338{ 339 int inverse = 0; /* 0 to encrypt, 1 to decrypt */ 340 341 /* 342 * do the transformation 343 */ 344 if (n == 8) { 345 for (n = 0; n < 8; n++) --- 17 unchanged lines hidden (view full) --- 363 for (n = 0; n < 8; n++) 364 CHAR(msgbuf, n) ^= CHAR(ivec, n); 365 DES_XFORM(UBUFFER(msgbuf)); 366 return WRITE(BUFFER(msgbuf), 8, fp); 367} 368 369/* 370 * This decrypts using the Cipher Block Chaining mode of DES |
371 * msgbuf I/O buffer 372 * fp input file descriptor |
|
382 */ 383int | 373 */ 374int |
384cbc_decode(msgbuf, fp) 385 char *msgbuf; /* I/O buffer */ 386 FILE *fp; /* input file descriptor */ | 375cbc_decode(char *msgbuf, FILE *fp) |
387{ 388 Desbuf tbuf; /* temp buffer for initialization vector */ 389 int n; /* number of bytes actually read */ 390 int c; /* used to test for EOF */ 391 int inverse = 1; /* 0 to encrypt, 1 to decrypt */ 392 393 if ((n = READ(BUFFER(msgbuf), 8, fp)) == 8) { 394 /* --- 27 unchanged lines hidden --- | 376{ 377 Desbuf tbuf; /* temp buffer for initialization vector */ 378 int n; /* number of bytes actually read */ 379 int c; /* used to test for EOF */ 380 int inverse = 1; /* 0 to encrypt, 1 to decrypt */ 381 382 if ((n = READ(BUFFER(msgbuf), 8, fp)) == 8) { 383 /* --- 27 unchanged lines hidden --- |