1 /* cbc.c: This file contains the encryption routines for the ed line editor */
3 * Copyright (c) 1991 The Regents of the University of California.
6 * This code is derived from software contributed to Berkeley by
7 * Matt Bishop of Dartmouth College.
9 * The United States Government has rights in this work pursuant
10 * to contract no. NAG 2-680 between the National Aeronautics and
11 * Space Administration and Dartmouth College.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 static char sccsid[] = "@(#)cbc.c 5.5 (Berkeley) 6/27/91";
46 /* Author: Matt Bishop
47 * Department of Mathematics and Computer Science
50 * Email: Matt.Bishop@dartmouth.edu
51 * ...!decvax!dartvax!Matt.Bishop
53 * See Technical Report PCS-TR91-158, Department of Mathematics and Computer
54 * Science, Dartmouth College, for a detailed description of the implemen-
55 * tation and differences between it and Sun's. The DES is described in
56 * FIPS PUB 46, and the modes in FIPS PUB 81 (see either the manual page
57 * or the technical report for a complete reference).
67 #include <sys/types.h>
72 * Define a divisor for rand() that yields a uniform distribution in the
75 #define RAND_DIV (((unsigned) RAND_MAX + 1) >> 8)
78 * BSD and System V systems offer special library calls that do
79 * block moves and fills, so if possible we take advantage of them
81 #define MEMCPY(dest,src,len) memcpy((dest),(src),(len))
82 #define MEMZERO(dest,len) memset((dest), 0, (len))
84 /* Hide the calls to the primitive encryption routines. */
85 #define DES_KEY(buf) \
86 if (des_setkey(buf)) \
88 #define DES_XFORM(buf) \
89 if (des_cipher(buf, buf, 0L, (inverse ? -1 : 1))) \
93 * read/write - no error checking
95 #define READ(buf, n, fp) fread(buf, sizeof(char), n, fp)
96 #define WRITE(buf, n, fp) fwrite(buf, sizeof(char), n, fp)
99 * some things to make references easier
101 typedef char Desbuf[8];
102 #define CHAR(x,i) (x[i])
103 #define UCHAR(x,i) (x[i])
104 #define BUFFER(x) (x)
105 #define UBUFFER(x) (x)
108 * global variables and related macros
111 enum { /* encrypt, decrypt, authenticate */
112 MODE_ENCRYPT, MODE_DECRYPT, MODE_AUTHENTICATE
113 } mode = MODE_ENCRYPT;
115 Desbuf ivec; /* initialization vector */
116 Desbuf pvec; /* padding vector */
117 char bits[] = { /* used to extract bits from a char */
118 '\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
120 int pflag; /* 1 to preserve parity bits */
122 char des_buf[8]; /* shared buffer for desgetc/desputc */
123 int des_ct = 0; /* count for desgetc/desputc */
124 int des_n = 0; /* index for desputc/desgetc */
127 /* desinit: initialize DES */
136 /* initialize the initialization vctor */
139 /* intialize the padding vector */
140 srand((unsigned) time((time_t *) 0));
141 for (i = 0; i < 8; i++)
142 CHAR(pvec, i) = (char) (rand()/RAND_DIV);
147 /* desgetc: return next char in an encrypted file */
152 if (des_n >= des_ct) {
154 des_ct = cbcdec(des_buf, fp);
156 return (des_ct > 0) ? des_buf[des_n++] : EOF;
161 /* desputc: write a char to an encrypted file; return char written */
167 if (des_n == sizeof des_buf) {
168 des_ct = cbcenc(des_buf, des_n, fp);
171 return (des_ct >= 0) ? (des_buf[des_n++] = c) : EOF;
176 /* desflush: flush an encrypted file's output; return status */
181 if (des_n == sizeof des_buf) {
182 des_ct = cbcenc(des_buf, des_n, fp);
185 return (des_ct >= 0 && cbcenc(des_buf, des_n, fp) >= 0) ? 0 : EOF;
191 * get keyword from tty or stdin
195 register char *p; /* used to obtain the key */
196 Desbuf msgbuf; /* I/O buffer */
201 if (*(p = getpass("Enter key: "))) {
204 * copy it, nul-padded, into the key area
206 cvtkey(BUFFER(msgbuf), p);
207 MEMZERO(p, _PASSWORD_LEN);
209 MEMZERO(msgbuf, sizeof msgbuf);
216 extern char errmsg[];
219 * print a warning message and, possibly, terminate
223 char *s; /* the message */
225 (void)sprintf(errmsg, "%s", s ? s : strerror(errno));
229 * map a hex character to an integer
232 int c; /* char to be converted */
233 int radix; /* base (2 to 16) */
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);
260 * convert the key to a bit pattern
264 char *obuf; /* bit pattern */
265 char *ibuf; /* the key itself */
267 register int i, j; /* counter in a for loop */
268 int nbuf[64]; /* used for hex/key translation */
271 * leading '0x' or '0X' == hex key
273 if (ibuf[0] == '0' && (ibuf[1] == 'x' || ibuf[1] == 'X')) {
276 * now translate it, bombing on any illegal hex digit
278 for (i = 0; ibuf[i] && i < 16; i++)
279 if ((nbuf[i] = tobinhex((int) ibuf[i], 16)) == -1)
280 err("bad hex digit in key");
283 for (i = 0; i < 8; i++)
285 ((nbuf[2*i]&0xf)<<4) | (nbuf[2*i+1]&0xf);
286 /* preserve parity bits */
291 * leading '0b' or '0B' == binary key
293 if (ibuf[0] == '0' && (ibuf[1] == 'b' || ibuf[1] == 'B')) {
296 * now translate it, bombing on any illegal binary digit
298 for (i = 0; ibuf[i] && i < 16; i++)
299 if ((nbuf[i] = tobinhex((int) ibuf[i], 2)) == -1)
300 err("bad binary digit in key");
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 */
311 * no special leader -- ASCII
313 (void)strncpy(obuf, ibuf, 8);
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.
332 Desbuf buf; /* key block */
334 register int i, j; /* counter in a for loop */
335 register int par; /* parity counter */
338 * if the parity is not preserved, flip it
341 for (i = 0; i < 8; i++) {
343 for (j = 1; j < 8; j++)
344 if ((bits[j]&UCHAR(buf, i)) != 0)
347 UCHAR(buf, i) = UCHAR(buf, i)&0177;
349 UCHAR(buf, i) = (UCHAR(buf, i)&0177)|0200;
353 DES_KEY(UBUFFER(buf));
358 * This encrypts using the Cipher Block Chaining mode of DES
360 cbcenc(msgbuf, n, fp)
365 int inverse = 0; /* 0 to encrypt, 1 to decrypt */
368 * do the transformation
371 for (n = 0; n < 8; n++)
372 CHAR(msgbuf, n) ^= CHAR(ivec, n);
373 DES_XFORM(UBUFFER(msgbuf));
374 MEMCPY(BUFFER(ivec), BUFFER(msgbuf), 8);
375 return WRITE(BUFFER(msgbuf), 8, fp);
378 * at EOF or last block -- in either case, the last byte contains
379 * the character representation of the number of bytes in it
382 MEMZERO(msgbuf + n, 8 - n);
385 * Pad the last block randomly
387 (void)MEMCPY(BUFFER(msgbuf + n), BUFFER(pvec), 8 - n);
389 for (n = 0; n < 8; n++)
390 CHAR(msgbuf, n) ^= CHAR(ivec, n);
391 DES_XFORM(UBUFFER(msgbuf));
392 return WRITE(BUFFER(msgbuf), 8, fp);
396 * This decrypts using the Cipher Block Chaining mode of DES
399 char *msgbuf; /* I/O buffer */
400 FILE *fp; /* input file descriptor */
402 Desbuf ibuf; /* temp buffer for initialization vector */
403 register int n; /* number of bytes actually read */
404 register int c; /* used to test for EOF */
405 int inverse = 1; /* 0 to encrypt, 1 to decrypt */
407 if ((n = READ(BUFFER(msgbuf), 8, fp)) == 8) {
409 * do the transformation
411 MEMCPY(BUFFER(ibuf), BUFFER(msgbuf), 8);
412 DES_XFORM(UBUFFER(msgbuf));
413 for (c = 0; c < 8; c++)
414 UCHAR(msgbuf, c) ^= UCHAR(ivec, c);
415 MEMCPY(BUFFER(ivec), BUFFER(ibuf), 8);
417 * if the last one, handle it specially
419 if ((c = fgetc(fp)) == EOF) {
421 if (n < 0 || n > 7) {
422 err("decryption failed (block corrupted)");
430 err("decryption failed (incomplete block)");
432 err("cannot read file");