No empty .Rs/.Re
[netbsd-mini2440.git] / bin / ed / cbc.c
blob0f37c98ca34d30da2d4646a7bcf0fdd5aa3fa5e2
1 /* $NetBSD: cbc.c,v 1.20 2009/07/26 02:06:37 dholland Exp $ */
3 /* cbc.c: This file contains the encryption routines for the ed line editor */
4 /*-
5 * Copyright (c) 1993 The Regents of the University of California.
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
32 * from: @(#)bdes.c 5.5 (Berkeley) 6/27/91
35 /*-
36 * Copyright (c) 1993 Andrew Moore, Talke Studio.
37 * All rights reserved.
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. All advertising materials mentioning features or use of this software
48 * must display the following acknowledgement:
49 * This product includes software developed by the University of
50 * California, Berkeley and its contributors.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
65 * SUCH DAMAGE.
67 * from: @(#)bdes.c 5.5 (Berkeley) 6/27/91
70 #include <sys/cdefs.h>
71 #ifndef lint
72 #if 0
73 static char *rcsid = "@(#)cbc.c,v 1.2 1994/02/01 00:34:36 alm Exp";
74 #else
75 __RCSID("$NetBSD: cbc.c,v 1.20 2009/07/26 02:06:37 dholland Exp $");
76 #endif
77 #endif /* not lint */
79 #include <sys/types.h>
80 #include <ctype.h>
81 #include <errno.h>
82 #include <pwd.h>
83 #ifdef DES
84 #include <time.h>
85 #endif
87 #include "ed.h"
91 * Define a divisor for rand() that yields a uniform distribution in the
92 * range 0-255.
94 #define RAND_DIV (((unsigned) RAND_MAX + 1) >> 8)
97 * BSD and System V systems offer special library calls that do
98 * block move_liness and fills, so if possible we take advantage of them
100 #define MEMCPY(dest,src,len) memcpy((dest),(src),(len))
101 #define MEMZERO(dest,len) memset((dest), 0, (len))
103 /* Hide the calls to the primitive encryption routines. */
104 #define DES_KEY(buf) \
105 if (des_setkey(buf)) \
106 des_error("des_setkey");
107 #define DES_XFORM(buf) \
108 if (des_cipher(buf, buf, 0L, (inverse ? -1 : 1))) \
109 des_error("des_cipher");
112 * read/write - no error checking
114 #define READ(buf, n, fp) fread(buf, sizeof(char), n, fp)
115 #define WRITE(buf, n, fp) fwrite(buf, sizeof(char), n, fp)
118 * some things to make references easier
120 typedef char Desbuf[8];
121 #define CHAR(x,i) (x[i])
122 #define UCHAR(x,i) (x[i])
123 #define BUFFER(x) (x)
124 #define UBUFFER(x) (x)
126 #ifdef DES
128 * global variables and related macros
131 static Desbuf ivec; /* initialization vector */
132 static Desbuf pvec; /* padding vector */
133 static char bits[] = { /* used to extract bits from a char */
134 '\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
136 static int pflag; /* 1 to preserve parity bits */
138 static char des_buf[8]; /* shared buffer for get_des_char/put_des_char */
139 static int des_ct = 0; /* count for get_des_char/put_des_char */
140 static int des_n = 0; /* index for put_des_char/get_des_char */
141 #endif
144 static void des_error(const char *);
145 static int hex_to_binary(int, int);
146 static void expand_des_key(char *, char *);
147 static void set_des_key(char *);
148 static int cbc_decode(char *, FILE *);
149 static int cbc_encode(char *, int, FILE *);
152 /* init_des_cipher: initialize DES */
153 void
154 init_des_cipher(void)
156 #ifdef DES
157 int i;
159 des_ct = des_n = 0;
161 /* initialize the initialization vector */
162 MEMZERO(ivec, 8);
164 /* intialize the padding vector */
165 srand((unsigned) time((time_t *) 0));
166 for (i = 0; i < 8; i++)
167 CHAR(pvec, i) = (char) (rand()/RAND_DIV);
168 #endif
172 /* get_des_char: return next char in an encrypted file */
174 get_des_char(FILE *fp)
176 #ifdef DES
177 if (des_n >= des_ct) {
178 des_n = 0;
179 des_ct = cbc_decode(des_buf, fp);
181 return (des_ct > 0) ? (unsigned char) des_buf[des_n++] : EOF;
182 #else
183 return EOF;
184 #endif
188 /* put_des_char: write a char to an encrypted file; return char written */
190 put_des_char(int c, FILE *fp)
192 #ifdef DES
193 if (des_n == sizeof des_buf) {
194 des_ct = cbc_encode(des_buf, des_n, fp);
195 des_n = 0;
197 return (des_ct >= 0) ? (unsigned char) (des_buf[des_n++] = c) : EOF;
198 #else
199 return EOF;
200 #endif
204 /* flush_des_file: flush an encrypted file's output; return status */
206 flush_des_file(FILE *fp)
208 #ifdef DES
209 if (des_n == sizeof des_buf) {
210 des_ct = cbc_encode(des_buf, des_n, fp);
211 des_n = 0;
213 return (des_ct >= 0 && cbc_encode(des_buf, des_n, fp) >= 0) ? 0 : EOF;
214 #else
215 return EOF;
216 #endif
219 #ifdef DES
221 * get keyword from tty or stdin
224 get_keyword(void)
226 char *p; /* used to obtain the key */
227 Desbuf msgbuf; /* I/O buffer */
230 * get the key
232 if (*(p = getpass("Enter key: "))) {
235 * copy it, nul-padded, into the key area
237 expand_des_key(BUFFER(msgbuf), p);
238 MEMZERO(p, _PASSWORD_LEN);
239 set_des_key(msgbuf);
240 MEMZERO(msgbuf, sizeof msgbuf);
241 return 1;
243 return 0;
248 * print a warning message and, possibly, terminate
250 static void
251 des_error(const char *s /* the message */)
253 (void)sprintf(errmsg, "%s", s ? s : strerror(errno));
257 * map a hex character to an integer
259 static int
260 hex_to_binary(int c /* char to be converted */,
261 int radix /* base (2 to 16) */)
263 switch(c) {
264 case '0': return(0x0);
265 case '1': return(0x1);
266 case '2': return(radix > 2 ? 0x2 : -1);
267 case '3': return(radix > 3 ? 0x3 : -1);
268 case '4': return(radix > 4 ? 0x4 : -1);
269 case '5': return(radix > 5 ? 0x5 : -1);
270 case '6': return(radix > 6 ? 0x6 : -1);
271 case '7': return(radix > 7 ? 0x7 : -1);
272 case '8': return(radix > 8 ? 0x8 : -1);
273 case '9': return(radix > 9 ? 0x9 : -1);
274 case 'A': case 'a': return(radix > 10 ? 0xa : -1);
275 case 'B': case 'b': return(radix > 11 ? 0xb : -1);
276 case 'C': case 'c': return(radix > 12 ? 0xc : -1);
277 case 'D': case 'd': return(radix > 13 ? 0xd : -1);
278 case 'E': case 'e': return(radix > 14 ? 0xe : -1);
279 case 'F': case 'f': return(radix > 15 ? 0xf : -1);
282 * invalid character
284 return(-1);
288 * convert the key to a bit pattern
290 static void
291 expand_des_key(char *obuf /* bit pattern */, char *inbuf /* the key itself */)
293 int i, j; /* counter in a for loop */
294 int nbuf[64]; /* used for hex/key translation */
297 * leading '0x' or '0X' == hex key
299 if (inbuf[0] == '0' && (inbuf[1] == 'x' || inbuf[1] == 'X')) {
300 inbuf = &inbuf[2];
302 * now translate it, bombing on any illegal hex digit
304 for (i = 0; inbuf[i] && i < 16; i++)
305 if ((nbuf[i] = hex_to_binary((int) inbuf[i], 16)) == -1)
306 des_error("bad hex digit in key");
307 while (i < 16)
308 nbuf[i++] = 0;
309 for (i = 0; i < 8; i++)
310 obuf[i] =
311 ((nbuf[2*i]&0xf)<<4) | (nbuf[2*i+1]&0xf);
312 /* preserve parity bits */
313 pflag = 1;
314 return;
317 * leading '0b' or '0B' == binary key
319 if (inbuf[0] == '0' && (inbuf[1] == 'b' || inbuf[1] == 'B')) {
320 inbuf = &inbuf[2];
322 * now translate it, bombing on any illegal binary digit
324 for (i = 0; inbuf[i] && i < 16; i++)
325 if ((nbuf[i] = hex_to_binary((int) inbuf[i], 2)) == -1)
326 des_error("bad binary digit in key");
327 while (i < 64)
328 nbuf[i++] = 0;
329 for (i = 0; i < 8; i++)
330 for (j = 0; j < 8; j++)
331 obuf[i] = (obuf[i]<<1)|nbuf[8*i+j];
332 /* preserve parity bits */
333 pflag = 1;
334 return;
337 * no special leader -- ASCII
339 (void)strncpy(obuf, inbuf, 8);
342 /*****************
343 * DES FUNCTIONS *
344 *****************/
346 * This sets the DES key and (if you're using the deszip version)
347 * the direction of the transformation. This uses the Sun
348 * to map the 64-bit key onto the 56 bits that the key schedule
349 * generation routines use: the old way, which just uses the user-
350 * supplied 64 bits as is, and the new way, which resets the parity
351 * bit to be the same as the low-order bit in each character. The
352 * new way generates a greater variety of key schedules, since many
353 * systems set the parity (high) bit of each character to 0, and the
354 * DES ignores the low order bit of each character.
356 static void
357 set_des_key(Desbuf buf /* key block */)
359 int i, j; /* counter in a for loop */
360 int par; /* parity counter */
363 * if the parity is not preserved, flip it
365 if (!pflag) {
366 for (i = 0; i < 8; i++) {
367 par = 0;
368 for (j = 1; j < 8; j++)
369 if ((bits[j]&UCHAR(buf, i)) != 0)
370 par++;
371 if ((par&01) == 01)
372 UCHAR(buf, i) = UCHAR(buf, i)&0177;
373 else
374 UCHAR(buf, i) = (UCHAR(buf, i)&0177)|0200;
378 DES_KEY(UBUFFER(buf));
383 * This encrypts using the Cipher Block Chaining mode of DES
385 static int
386 cbc_encode(char *msgbuf, int n, FILE *fp)
388 int inverse = 0; /* 0 to encrypt, 1 to decrypt */
391 * do the transformation
393 if (n == 8) {
394 for (n = 0; n < 8; n++)
395 CHAR(msgbuf, n) ^= CHAR(ivec, n);
396 DES_XFORM(UBUFFER(msgbuf));
397 MEMCPY(BUFFER(ivec), BUFFER(msgbuf), 8);
398 return WRITE(BUFFER(msgbuf), 8, fp);
401 * at EOF or last block -- in either case, the last byte contains
402 * the character representation of the number of bytes in it
405 MEMZERO(msgbuf + n, 8 - n);
408 * Pad the last block randomly
410 (void)MEMCPY(BUFFER(msgbuf + n), BUFFER(pvec), 8 - n);
411 CHAR(msgbuf, 7) = n;
412 for (n = 0; n < 8; n++)
413 CHAR(msgbuf, n) ^= CHAR(ivec, n);
414 DES_XFORM(UBUFFER(msgbuf));
415 return WRITE(BUFFER(msgbuf), 8, fp);
419 * This decrypts using the Cipher Block Chaining mode of DES
421 static int
422 cbc_decode(char *msgbuf /* I/O buffer */,
423 FILE *fp /* input file descriptor */)
425 Desbuf inbuf; /* temp buffer for initialization vector */
426 int n; /* number of bytes actually read */
427 int c; /* used to test for EOF */
428 int inverse = 1; /* 0 to encrypt, 1 to decrypt */
430 if ((n = READ(BUFFER(msgbuf), 8, fp)) == 8) {
432 * do the transformation
434 MEMCPY(BUFFER(inbuf), BUFFER(msgbuf), 8);
435 DES_XFORM(UBUFFER(msgbuf));
436 for (c = 0; c < 8; c++)
437 UCHAR(msgbuf, c) ^= UCHAR(ivec, c);
438 MEMCPY(BUFFER(ivec), BUFFER(inbuf), 8);
440 * if the last one, handle it specially
442 if ((c = fgetc(fp)) == EOF) {
443 n = CHAR(msgbuf, 7);
444 if (n < 0 || n > 7) {
445 des_error("decryption failed (block corrupted)");
446 return EOF;
448 } else
449 (void)ungetc(c, fp);
450 return n;
452 if (n > 0)
453 des_error("decryption failed (incomplete block)");
454 else if (n < 0)
455 des_error("cannot read file");
456 return EOF;
458 #endif /* DES */