1 /* cbc.c: This file contains the encryption routines for the ed line editor */
3 * Copyright (c) 1993 The Regents of the University of California.
6 * Copyright (c) 1993 Andrew Moore, Talke Studio.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
37 * from: @(#)bdes.c 5.5 (Berkeley) 6/27/91
39 * @(#)cbc.c,v 1.2 1994/02/01 00:34:36 alm Exp
40 * $FreeBSD: src/bin/ed/cbc.c,v 1.12.2.1 2001/07/04 22:32:18 kris Exp $
41 * $DragonFly: src/bin/ed/cbc.c,v 1.2 2003/06/17 04:22:49 dillon Exp $
44 #include <sys/types.h>
55 * BSD and System V systems offer special library calls that do
56 * block move_liness and fills, so if possible we take advantage of them
58 #define MEMCPY(dest,src,len) memcpy((dest),(src),(len))
59 #define MEMZERO(dest,len) memset((dest), 0, (len))
61 /* Hide the calls to the primitive encryption routines. */
62 #define DES_KEY(buf) \
63 if (des_setkey(buf)) \
64 des_error("des_setkey");
65 #define DES_XFORM(buf) \
66 if (des_cipher(buf, buf, 0L, (inverse ? -1 : 1))) \
67 des_error("des_cipher");
70 * read/write - no error checking
72 #define READ(buf, n, fp) fread(buf, sizeof(char), n, fp)
73 #define WRITE(buf, n, fp) fwrite(buf, sizeof(char), n, fp)
76 * some things to make references easier
78 typedef char Desbuf[8];
79 #define CHAR(x,i) (x[i])
80 #define UCHAR(x,i) (x[i])
82 #define UBUFFER(x) (x)
85 * global variables and related macros
88 enum { /* encrypt, decrypt, authenticate */
89 MODE_ENCRYPT, MODE_DECRYPT, MODE_AUTHENTICATE
90 } mode = MODE_ENCRYPT;
92 Desbuf ivec; /* initialization vector */
93 Desbuf pvec; /* padding vector */
94 char bits[] = { /* used to extract bits from a char */
95 '\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
97 int pflag; /* 1 to preserve parity bits */
99 unsigned char des_buf[8]; /* shared buffer for get_des_char/put_des_char */
100 int des_ct = 0; /* count for get_des_char/put_des_char */
101 int des_n = 0; /* index for put_des_char/get_des_char */
104 /* init_des_cipher: initialize DES */
113 /* initialize the initialization vector */
116 /* initialize the padding vector */
117 for (i = 0; i < 8; i++)
118 CHAR(pvec, i) = (char) (arc4random() % 256);
123 /* get_des_char: return next char in an encrypted file */
129 if (des_n >= des_ct) {
131 des_ct = cbc_decode(des_buf, fp);
133 return (des_ct > 0) ? des_buf[des_n++] : EOF;
140 /* put_des_char: write a char to an encrypted file; return char written */
147 if (des_n == sizeof des_buf) {
148 des_ct = cbc_encode(des_buf, des_n, fp);
151 return (des_ct >= 0) ? (des_buf[des_n++] = c) : EOF;
153 return (fputc(c, fp));
158 /* flush_des_file: flush an encrypted file's output; return status */
164 if (des_n == sizeof des_buf) {
165 des_ct = cbc_encode(des_buf, des_n, fp);
168 return (des_ct >= 0 && cbc_encode(des_buf, des_n, fp) >= 0) ? 0 : EOF;
176 * get keyword from tty or stdin
181 register char *p; /* used to obtain the key */
182 Desbuf msgbuf; /* I/O buffer */
187 if (*(p = getpass("Enter key: "))) {
190 * copy it, nul-padded, into the key area
192 expand_des_key(BUFFER(msgbuf), p);
193 MEMZERO(p, _PASSWORD_LEN);
195 MEMZERO(msgbuf, sizeof msgbuf);
203 * print a warning message and, possibly, terminate
207 char *s; /* the message */
209 (void)sprintf(errmsg, "%s", s ? s : strerror(errno));
213 * map a hex character to an integer
216 hex_to_binary(c, radix)
217 int c; /* char to be converted */
218 int radix; /* base (2 to 16) */
221 case '0': return(0x0);
222 case '1': return(0x1);
223 case '2': return(radix > 2 ? 0x2 : -1);
224 case '3': return(radix > 3 ? 0x3 : -1);
225 case '4': return(radix > 4 ? 0x4 : -1);
226 case '5': return(radix > 5 ? 0x5 : -1);
227 case '6': return(radix > 6 ? 0x6 : -1);
228 case '7': return(radix > 7 ? 0x7 : -1);
229 case '8': return(radix > 8 ? 0x8 : -1);
230 case '9': return(radix > 9 ? 0x9 : -1);
231 case 'A': case 'a': return(radix > 10 ? 0xa : -1);
232 case 'B': case 'b': return(radix > 11 ? 0xb : -1);
233 case 'C': case 'c': return(radix > 12 ? 0xc : -1);
234 case 'D': case 'd': return(radix > 13 ? 0xd : -1);
235 case 'E': case 'e': return(radix > 14 ? 0xe : -1);
236 case 'F': case 'f': return(radix > 15 ? 0xf : -1);
245 * convert the key to a bit pattern
248 expand_des_key(obuf, ibuf)
249 char *obuf; /* bit pattern */
250 char *ibuf; /* the key itself */
252 register int i, j; /* counter in a for loop */
253 int nbuf[64]; /* used for hex/key translation */
256 * leading '0x' or '0X' == hex key
258 if (ibuf[0] == '0' && (ibuf[1] == 'x' || ibuf[1] == 'X')) {
261 * now translate it, bombing on any illegal hex digit
263 for (i = 0; ibuf[i] && i < 16; i++)
264 if ((nbuf[i] = hex_to_binary((int) ibuf[i], 16)) == -1)
265 des_error("bad hex digit in key");
268 for (i = 0; i < 8; i++)
270 ((nbuf[2*i]&0xf)<<4) | (nbuf[2*i+1]&0xf);
271 /* preserve parity bits */
276 * leading '0b' or '0B' == binary key
278 if (ibuf[0] == '0' && (ibuf[1] == 'b' || ibuf[1] == 'B')) {
281 * now translate it, bombing on any illegal binary digit
283 for (i = 0; ibuf[i] && i < 16; i++)
284 if ((nbuf[i] = hex_to_binary((int) ibuf[i], 2)) == -1)
285 des_error("bad binary digit in key");
288 for (i = 0; i < 8; i++)
289 for (j = 0; j < 8; j++)
290 obuf[i] = (obuf[i]<<1)|nbuf[8*i+j];
291 /* preserve parity bits */
296 * no special leader -- ASCII
298 (void)strncpy(obuf, ibuf, 8);
305 * This sets the DES key and (if you're using the deszip version)
306 * the direction of the transformation. This uses the Sun
307 * to map the 64-bit key onto the 56 bits that the key schedule
308 * generation routines use: the old way, which just uses the user-
309 * supplied 64 bits as is, and the new way, which resets the parity
310 * bit to be the same as the low-order bit in each character. The
311 * new way generates a greater variety of key schedules, since many
312 * systems set the parity (high) bit of each character to 0, and the
313 * DES ignores the low order bit of each character.
317 Desbuf buf; /* key block */
319 register int i, j; /* counter in a for loop */
320 register int par; /* parity counter */
323 * if the parity is not preserved, flip it
326 for (i = 0; i < 8; i++) {
328 for (j = 1; j < 8; j++)
329 if ((bits[j]&UCHAR(buf, i)) != 0)
332 UCHAR(buf, i) = UCHAR(buf, i)&0177;
334 UCHAR(buf, i) = (UCHAR(buf, i)&0177)|0200;
338 DES_KEY(UBUFFER(buf));
343 * This encrypts using the Cipher Block Chaining mode of DES
346 cbc_encode(msgbuf, n, fp)
351 int inverse = 0; /* 0 to encrypt, 1 to decrypt */
354 * do the transformation
357 for (n = 0; n < 8; n++)
358 CHAR(msgbuf, n) ^= CHAR(ivec, n);
359 DES_XFORM(UBUFFER(msgbuf));
360 MEMCPY(BUFFER(ivec), BUFFER(msgbuf), 8);
361 return WRITE(BUFFER(msgbuf), 8, fp);
364 * at EOF or last block -- in either case, the last byte contains
365 * the character representation of the number of bytes in it
368 MEMZERO(msgbuf + n, 8 - n);
371 * Pad the last block randomly
373 (void)MEMCPY(BUFFER(msgbuf + n), BUFFER(pvec), 8 - n);
375 for (n = 0; n < 8; n++)
376 CHAR(msgbuf, n) ^= CHAR(ivec, n);
377 DES_XFORM(UBUFFER(msgbuf));
378 return WRITE(BUFFER(msgbuf), 8, fp);
382 * This decrypts using the Cipher Block Chaining mode of DES
385 cbc_decode(msgbuf, fp)
386 char *msgbuf; /* I/O buffer */
387 FILE *fp; /* input file descriptor */
389 Desbuf ibuf; /* temp buffer for initialization vector */
390 register int n; /* number of bytes actually read */
391 register int c; /* used to test for EOF */
392 int inverse = 1; /* 0 to encrypt, 1 to decrypt */
394 if ((n = READ(BUFFER(msgbuf), 8, fp)) == 8) {
396 * do the transformation
398 MEMCPY(BUFFER(ibuf), BUFFER(msgbuf), 8);
399 DES_XFORM(UBUFFER(msgbuf));
400 for (c = 0; c < 8; c++)
401 UCHAR(msgbuf, c) ^= UCHAR(ivec, c);
402 MEMCPY(BUFFER(ivec), BUFFER(ibuf), 8);
404 * if the last one, handle it specially
406 if ((c = fgetc(fp)) == EOF) {
408 if (n < 0 || n > 7) {
409 des_error("decryption failed (block corrupted)");
417 des_error("decryption failed (incomplete block)");
419 des_error("cannot read file");