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