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.8 2005/02/02 18:46:01 joerg 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 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 */
106 init_des_cipher(void)
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 */
125 get_des_char(FILE *fp
)
128 if (des_n
>= des_ct
) {
130 des_ct
= cbc_decode(des_buf
, fp
);
132 return (des_ct
> 0) ? (unsigned char)des_buf
[des_n
++] : EOF
;
139 /* put_des_char: write a char to an encrypted file; return char written */
141 put_des_char(int c
, FILE *fp
)
144 if (des_n
== sizeof des_buf
) {
145 des_ct
= cbc_encode(des_buf
, des_n
, fp
);
148 return (des_ct
>= 0) ? (des_buf
[des_n
++] = (char)c
) : EOF
;
150 return (fputc(c
, fp
));
155 /* flush_des_file: flush an encrypted file's output; return status */
157 flush_des_file(FILE *fp
)
160 if (des_n
== sizeof des_buf
) {
161 des_ct
= cbc_encode(des_buf
, des_n
, fp
);
164 return (des_ct
>= 0 && cbc_encode(des_buf
, des_n
, fp
) >= 0) ? 0 : EOF
;
172 * get keyword from tty or stdin
177 char *p
; /* used to obtain the key */
178 Desbuf msgbuf
; /* I/O buffer */
183 if (*(p
= getpass("Enter key: "))) {
186 * copy it, nul-padded, into the key area
188 expand_des_key(BUFFER(msgbuf
), p
);
189 MEMZERO(p
, _PASSWORD_LEN
);
191 MEMZERO(msgbuf
, sizeof msgbuf
);
199 * print a warning message and, possibly, terminate
202 des_error(const char *s
)
204 sprintf(errmsg
, "%s", s
? s
: strerror(errno
));
208 * map a hex character to an integer
211 hex_to_binary(int c
, int radix
)
214 case '0': return(0x0);
215 case '1': return(0x1);
216 case '2': return(radix
> 2 ? 0x2 : -1);
217 case '3': return(radix
> 3 ? 0x3 : -1);
218 case '4': return(radix
> 4 ? 0x4 : -1);
219 case '5': return(radix
> 5 ? 0x5 : -1);
220 case '6': return(radix
> 6 ? 0x6 : -1);
221 case '7': return(radix
> 7 ? 0x7 : -1);
222 case '8': return(radix
> 8 ? 0x8 : -1);
223 case '9': return(radix
> 9 ? 0x9 : -1);
224 case 'A': case 'a': return(radix
> 10 ? 0xa : -1);
225 case 'B': case 'b': return(radix
> 11 ? 0xb : -1);
226 case 'C': case 'c': return(radix
> 12 ? 0xc : -1);
227 case 'D': case 'd': return(radix
> 13 ? 0xd : -1);
228 case 'E': case 'e': return(radix
> 14 ? 0xe : -1);
229 case 'F': case 'f': return(radix
> 15 ? 0xf : -1);
238 * convert the key to a bit pattern
241 expand_des_key(char *obuf
, char *inbuf
)
243 int i
, j
; /* counter in a for loop */
244 int nbuf
[64]; /* used for hex/key translation */
247 * leading '0x' or '0X' == hex key
249 if (inbuf
[0] == '0' && (inbuf
[1] == 'x' || inbuf
[1] == 'X')) {
252 * now translate it, bombing on any illegal hex digit
254 for (i
= 0; inbuf
[i
] && i
< 16; i
++)
255 if ((nbuf
[i
] = hex_to_binary((int) inbuf
[i
], 16)) == -1)
256 des_error("bad hex digit in key");
259 for (i
= 0; i
< 8; i
++)
261 ((nbuf
[2*i
]&0xf)<<4) | (nbuf
[2*i
+1]&0xf);
262 /* preserve parity bits */
267 * leading '0b' or '0B' == binary key
269 if (inbuf
[0] == '0' && (inbuf
[1] == 'b' || inbuf
[1] == 'B')) {
272 * now translate it, bombing on any illegal binary digit
274 for (i
= 0; inbuf
[i
] && i
< 16; i
++)
275 if ((nbuf
[i
] = hex_to_binary((int) inbuf
[i
], 2)) == -1)
276 des_error("bad binary digit in key");
279 for (i
= 0; i
< 8; i
++)
280 for (j
= 0; j
< 8; j
++)
281 obuf
[i
] = (obuf
[i
]<<1)|nbuf
[8*i
+j
];
282 /* preserve parity bits */
287 * no special leader -- ASCII
289 strncpy(obuf
, inbuf
, 8);
296 * This sets the DES key and (if you're using the deszip version)
297 * the direction of the transformation. This uses the Sun
298 * to map the 64-bit key onto the 56 bits that the key schedule
299 * generation routines use: the old way, which just uses the user-
300 * supplied 64 bits as is, and the new way, which resets the parity
301 * bit to be the same as the low-order bit in each character. The
302 * new way generates a greater variety of key schedules, since many
303 * systems set the parity (high) bit of each character to 0, and the
304 * DES ignores the low order bit of each character.
307 set_des_key(Desbuf buf
)
309 int i
, j
; /* counter in a for loop */
310 int par
; /* parity counter */
313 * if the parity is not preserved, flip it
316 for (i
= 0; i
< 8; i
++) {
318 for (j
= 1; j
< 8; j
++)
319 if ((bits
[j
]&UCHAR(buf
, i
)) != 0)
322 UCHAR(buf
, i
) = UCHAR(buf
, i
)&0177;
324 UCHAR(buf
, i
) = (UCHAR(buf
, i
)&0177)|0200;
328 DES_KEY(UBUFFER(buf
));
333 * This encrypts using the Cipher Block Chaining mode of DES
336 cbc_encode(char *msgbuf
, int n
, FILE *fp
)
338 int inverse
= 0; /* 0 to encrypt, 1 to decrypt */
341 * do the transformation
344 for (n
= 0; n
< 8; n
++)
345 CHAR(msgbuf
, n
) ^= CHAR(ivec
, n
);
346 DES_XFORM(UBUFFER(msgbuf
));
347 MEMCPY(BUFFER(ivec
), BUFFER(msgbuf
), 8);
348 return WRITE(BUFFER(msgbuf
), 8, fp
);
351 * at EOF or last block -- in either case, the last byte contains
352 * the character representation of the number of bytes in it
355 MEMZERO(msgbuf + n, 8 - n);
358 * Pad the last block randomly
360 MEMCPY(BUFFER(msgbuf
+ n
), BUFFER(pvec
), 8 - n
);
362 for (n
= 0; n
< 8; n
++)
363 CHAR(msgbuf
, n
) ^= CHAR(ivec
, n
);
364 DES_XFORM(UBUFFER(msgbuf
));
365 return WRITE(BUFFER(msgbuf
), 8, fp
);
369 * This decrypts using the Cipher Block Chaining mode of DES
372 cbc_decode(char *msgbuf
, FILE *fp
)
374 Desbuf inbuf
; /* temp buffer for initialization vector */
375 int n
; /* number of bytes actually read */
376 int c
; /* used to test for EOF */
377 int inverse
= 1; /* 0 to encrypt, 1 to decrypt */
379 if ((n
= READ(BUFFER(msgbuf
), 8, fp
)) == 8) {
381 * do the transformation
383 MEMCPY(BUFFER(inbuf
), BUFFER(msgbuf
), 8);
384 DES_XFORM(UBUFFER(msgbuf
));
385 for (c
= 0; c
< 8; c
++)
386 UCHAR(msgbuf
, c
) ^= UCHAR(ivec
, c
);
387 MEMCPY(BUFFER(ivec
), BUFFER(inbuf
), 8);
389 * if the last one, handle it specially
391 if ((c
= fgetc(fp
)) == EOF
) {
393 if (n
< 0 || n
> 7) {
394 des_error("decryption failed (block corrupted)");
402 des_error("decryption failed (incomplete block)");
404 des_error("cannot read file");