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.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 $
44 #include <sys/types.h>
49 #include <openssl/des.h>
50 #define ED_DES_INCLUDES
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
60 #define MEMCPY(dest,src,len) memcpy((dest),(src),(len))
61 #define MEMZERO(dest,len) memset((dest), 0, (len))
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);
68 * read/write - no error checking
70 #define READ(buf, n, fp) fread(buf, sizeof(char), n, fp)
71 #define WRITE(buf, n, fp) fwrite(buf, sizeof(char), n, fp)
74 * global variables and related macros
77 enum { /* encrypt, decrypt, authenticate */
78 MODE_ENCRYPT
, MODE_DECRYPT
, MODE_AUTHENTICATE
79 } mode
= MODE_ENCRYPT
;
82 DES_cblock ivec
; /* initialization vector */
83 DES_cblock pvec
; /* padding vector */
86 char bits
[] = { /* used to extract bits from a char */
87 '\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
90 int pflag
; /* 1 to preserve parity bits */
93 DES_key_schedule schedule
; /* expanded DES key */
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 */
101 /* init_des_cipher: initialize DES */
103 init_des_cipher(void)
110 /* initialize the initialization vector */
113 /* initialize the padding vector */
114 for (i
= 0; i
< 8; i
++)
115 pvec
[i
] = (char) (arc4random() % 256);
120 /* get_des_char: return next char in an encrypted file */
122 get_des_char(FILE *fp
)
125 if (des_n
>= des_ct
) {
127 des_ct
= cbc_decode(des_buf
, fp
);
129 return (des_ct
> 0) ? (unsigned char)des_buf
[des_n
++] : EOF
;
136 /* put_des_char: write a char to an encrypted file; return char written */
138 put_des_char(int c
, FILE *fp
)
141 if (des_n
== sizeof des_buf
) {
142 des_ct
= cbc_encode(des_buf
, des_n
, fp
);
145 return (des_ct
>= 0) ? (des_buf
[des_n
++] = (char)c
) : EOF
;
147 return (fputc(c
, fp
));
152 /* flush_des_file: flush an encrypted file's output; return status */
154 flush_des_file(FILE *fp
)
157 if (des_n
== sizeof des_buf
) {
158 des_ct
= cbc_encode(des_buf
, des_n
, fp
);
161 return (des_ct
>= 0 && cbc_encode(des_buf
, des_n
, fp
) >= 0) ? 0 : EOF
;
169 * get keyword from tty or stdin
174 char *p
; /* used to obtain the key */
175 DES_cblock msgbuf
; /* I/O buffer */
180 if (*(p
= getpass("Enter key: "))) {
183 * copy it, nul-padded, into the key area
185 expand_des_key(msgbuf
, p
);
186 MEMZERO(p
, _PASSWORD_LEN
);
187 set_des_key(&msgbuf
);
188 MEMZERO(msgbuf
, sizeof msgbuf
);
196 * print a warning message and, possibly, terminate
199 des_error(const char *s
)
201 errmsg
= s
? s
: strerror(errno
);
205 * map a hex character to an integer
208 hex_to_binary(int c
, int radix
)
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);
235 * convert the key to a bit pattern
237 * inbuf the key itself
240 expand_des_key(char *obuf
, char *inbuf
)
242 int i
, j
; /* counter in a for loop */
243 int nbuf
[64]; /* used for hex/key translation */
246 * leading '0x' or '0X' == hex key
248 if (inbuf
[0] == '0' && (inbuf
[1] == 'x' || inbuf
[1] == 'X')) {
251 * now translate it, bombing on any illegal hex digit
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");
258 for (i
= 0; i
< 8; i
++)
260 ((nbuf
[2*i
]&0xf)<<4) | (nbuf
[2*i
+1]&0xf);
261 /* preserve parity bits */
266 * leading '0b' or '0B' == binary key
268 if (inbuf
[0] == '0' && (inbuf
[1] == 'b' || inbuf
[1] == 'B')) {
271 * now translate it, bombing on any illegal binary digit
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");
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 */
286 * no special leader -- ASCII
288 strncpy(obuf
, inbuf
, 8);
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.
306 set_des_key(DES_cblock
*buf
) /* key block */
308 int i
, j
; /* counter in a for loop */
309 int par
; /* parity counter */
312 * if the parity is not preserved, flip it
315 for (i
= 0; i
< 8; i
++) {
317 for (j
= 1; j
< 8; j
++)
318 if ((bits
[j
] & (*buf
)[i
]) != 0)
320 if ((par
& 0x01) == 0x01)
323 (*buf
)[i
] = ((*buf
)[i
] & 0x7f) | 0x80;
327 DES_set_odd_parity(buf
);
328 DES_set_key(buf
, &schedule
);
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 msgbuf
[n
] ^= ivec
[n
];
346 DES_XFORM((DES_cblock
*)msgbuf
);
347 MEMCPY(ivec
, msgbuf
, 8);
348 return WRITE(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(msgbuf
+ n
, pvec
, 8 - 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
);
369 * This decrypts using the Cipher Block Chaining mode of DES
371 * fp input file descriptor
374 cbc_decode(char *msgbuf
, FILE *fp
)
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 */
381 if ((n
= READ(msgbuf
, 8, fp
)) == 8) {
383 * do the transformation
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);
391 * if the last one, handle it specially
393 if ((c
= fgetc(fp
)) == EOF
) {
395 if (n
< 0 || n
> 7) {
396 des_error("decryption failed (block corrupted)");
404 des_error("decryption failed (incomplete block)");
406 des_error("cannot read file");