bdes.c revision 1.3 1 1.3 mjl /* $NetBSD: bdes.c,v 1.3 2000/10/04 20:09:05 mjl Exp $ */
2 1.1 thorpej
3 1.1 thorpej /*-
4 1.1 thorpej * Copyright (c) 1991, 1993
5 1.1 thorpej * The Regents of the University of California. All rights reserved.
6 1.1 thorpej *
7 1.1 thorpej * This code is derived from software contributed to Berkeley by
8 1.1 thorpej * Matt Bishop of Dartmouth College.
9 1.1 thorpej *
10 1.1 thorpej * The United States Government has rights in this work pursuant
11 1.1 thorpej * to contract no. NAG 2-680 between the National Aeronautics and
12 1.1 thorpej * Space Administration and Dartmouth College.
13 1.1 thorpej *
14 1.1 thorpej * Redistribution and use in source and binary forms, with or without
15 1.1 thorpej * modification, are permitted provided that the following conditions
16 1.1 thorpej * are met:
17 1.1 thorpej * 1. Redistributions of source code must retain the above copyright
18 1.1 thorpej * notice, this list of conditions and the following disclaimer.
19 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
20 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
21 1.1 thorpej * documentation and/or other materials provided with the distribution.
22 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
23 1.1 thorpej * must display the following acknowledgement:
24 1.1 thorpej * This product includes software developed by the University of
25 1.1 thorpej * California, Berkeley and its contributors.
26 1.1 thorpej * 4. Neither the name of the University nor the names of its contributors
27 1.1 thorpej * may be used to endorse or promote products derived from this software
28 1.1 thorpej * without specific prior written permission.
29 1.1 thorpej *
30 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
31 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 1.1 thorpej * SUCH DAMAGE.
41 1.1 thorpej */
42 1.1 thorpej
43 1.2 thorpej #include <sys/cdefs.h>
44 1.1 thorpej #ifndef lint
45 1.2 thorpej __COPYRIGHT("@(#) Copyright (c) 1991, 1993\n\
46 1.2 thorpej The Regents of the University of California. All rights reserved.\n");
47 1.1 thorpej #endif /* not lint */
48 1.1 thorpej
49 1.1 thorpej #ifndef lint
50 1.1 thorpej #if 0
51 1.1 thorpej static char sccsid[] = "@(#)bdes.c 8.1 (Berkeley) 6/6/93";
52 1.1 thorpej #else
53 1.3 mjl __RCSID("$NetBSD: bdes.c,v 1.3 2000/10/04 20:09:05 mjl Exp $");
54 1.1 thorpej #endif
55 1.1 thorpej #endif /* not lint */
56 1.1 thorpej
57 1.1 thorpej /*
58 1.1 thorpej * BDES -- DES encryption package for Berkeley Software Distribution 4.4
59 1.1 thorpej * options:
60 1.1 thorpej * -a key is in ASCII
61 1.1 thorpej * -b use ECB (electronic code book) mode
62 1.1 thorpej * -d invert (decrypt) input
63 1.1 thorpej * -f b use b-bit CFB (cipher feedback) mode
64 1.1 thorpej * -F b use b-bit CFB (cipher feedback) alternative mode
65 1.1 thorpej * -k key use key as the cryptographic key
66 1.1 thorpej * -m b generate a MAC of length b
67 1.1 thorpej * -o b use b-bit OFB (output feedback) mode
68 1.1 thorpej * -p don't reset the parity bit
69 1.1 thorpej * -v v use v as the initialization vector (ignored for ECB)
70 1.1 thorpej * note: the last character of the last block is the integer indicating
71 1.1 thorpej * how many characters of that block are to be output
72 1.1 thorpej *
73 1.1 thorpej * Author: Matt Bishop
74 1.1 thorpej * Department of Mathematics and Computer Science
75 1.1 thorpej * Dartmouth College
76 1.1 thorpej * Hanover, NH 03755
77 1.1 thorpej * Email: Matt.Bishop (at) dartmouth.edu
78 1.1 thorpej * ...!decvax!dartvax!Matt.Bishop
79 1.1 thorpej *
80 1.1 thorpej * See Technical Report PCS-TR91-158, Department of Mathematics and Computer
81 1.1 thorpej * Science, Dartmouth College, for a detailed description of the implemen-
82 1.1 thorpej * tation and differences between it and Sun's. The DES is described in
83 1.1 thorpej * FIPS PUB 46, and the modes in FIPS PUB 81 (see either the manual page
84 1.1 thorpej * or the technical report for a complete reference).
85 1.1 thorpej */
86 1.1 thorpej
87 1.1 thorpej #include <errno.h>
88 1.1 thorpej #include <unistd.h>
89 1.1 thorpej #include <stdio.h>
90 1.1 thorpej #include <ctype.h>
91 1.1 thorpej #include <stdlib.h>
92 1.1 thorpej #include <string.h>
93 1.1 thorpej
94 1.1 thorpej /*
95 1.1 thorpej * BSD and System V systems offer special library calls that do
96 1.1 thorpej * block moves and fills, so if possible we take advantage of them
97 1.1 thorpej */
98 1.1 thorpej #define MEMCPY(dest,src,len) bcopy((src),(dest),(len))
99 1.1 thorpej #define MEMZERO(dest,len) bzero((dest),(len))
100 1.1 thorpej
101 1.1 thorpej /* Hide the calls to the primitive encryption routines. */
102 1.1 thorpej #define FASTWAY
103 1.1 thorpej #ifdef FASTWAY
104 1.1 thorpej #define DES_KEY(buf) \
105 1.1 thorpej if (des_setkey(buf)) \
106 1.2 thorpej bdes_err(0, "des_setkey");
107 1.1 thorpej #define DES_XFORM(buf) \
108 1.1 thorpej if (des_cipher(buf, buf, 0L, (inverse ? -1 : 1))) \
109 1.2 thorpej bdes_err(0, "des_cipher");
110 1.1 thorpej #else
111 1.1 thorpej #define DES_KEY(buf) { \
112 1.1 thorpej char bits1[64]; /* bits of key */ \
113 1.1 thorpej expand(buf, bits1); \
114 1.1 thorpej if (setkey(bits1)) \
115 1.2 thorpej bdes_err(0, "setkey"); \
116 1.1 thorpej }
117 1.1 thorpej #define DES_XFORM(buf) { \
118 1.1 thorpej char bits1[64]; /* bits of message */ \
119 1.1 thorpej expand(buf, bits1); \
120 1.1 thorpej if (encrypt(bits1, inverse)) \
121 1.2 thorpej bdes_err(0, "encrypt"); \
122 1.1 thorpej compress(bits1, buf); \
123 1.1 thorpej }
124 1.1 thorpej #endif
125 1.1 thorpej
126 1.1 thorpej /*
127 1.1 thorpej * this does an error-checking write
128 1.1 thorpej */
129 1.1 thorpej #define READ(buf, n) fread(buf, sizeof(char), n, stdin)
130 1.1 thorpej #define WRITE(buf,n) \
131 1.1 thorpej if (fwrite(buf, sizeof(char), n, stdout) != n) \
132 1.2 thorpej bdes_err(bn, NULL);
133 1.1 thorpej
134 1.1 thorpej /*
135 1.1 thorpej * some things to make references easier
136 1.1 thorpej */
137 1.1 thorpej typedef char Desbuf[8];
138 1.1 thorpej #define CHAR(x,i) (x[i])
139 1.1 thorpej #define UCHAR(x,i) (x[i])
140 1.1 thorpej #define BUFFER(x) (x)
141 1.1 thorpej #define UBUFFER(x) (x)
142 1.1 thorpej
143 1.1 thorpej /*
144 1.1 thorpej * global variables and related macros
145 1.1 thorpej */
146 1.1 thorpej #define KEY_DEFAULT 0 /* interpret radix of key from key */
147 1.1 thorpej #define KEY_ASCII 1 /* key is in ASCII characters */
148 1.1 thorpej int keybase = KEY_DEFAULT; /* how to interpret the key */
149 1.1 thorpej
150 1.1 thorpej enum { /* encrypt, decrypt, authenticate */
151 1.1 thorpej MODE_ENCRYPT, MODE_DECRYPT, MODE_AUTHENTICATE
152 1.1 thorpej } mode = MODE_ENCRYPT;
153 1.1 thorpej enum { /* ecb, cbc, cfb, cfba, ofb? */
154 1.1 thorpej ALG_ECB, ALG_CBC, ALG_CFB, ALG_OFB, ALG_CFBA
155 1.1 thorpej } alg = ALG_CBC;
156 1.1 thorpej
157 1.1 thorpej Desbuf ivec; /* initialization vector */
158 1.1 thorpej char bits[] = { /* used to extract bits from a char */
159 1.1 thorpej '\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
160 1.1 thorpej };
161 1.1 thorpej int inverse; /* 0 to encrypt, 1 to decrypt */
162 1.1 thorpej int macbits = -1; /* number of bits in authentication */
163 1.1 thorpej int fbbits = -1; /* number of feedback bits */
164 1.1 thorpej int pflag; /* 1 to preserve parity bits */
165 1.1 thorpej
166 1.2 thorpej int setbits(char *, int);
167 1.2 thorpej void bdes_err(int, const char *);
168 1.2 thorpej int tobinhex(char, int);
169 1.2 thorpej void cvtkey(char *, char *);
170 1.2 thorpej void makekey(Desbuf);
171 1.2 thorpej void ecbenc(void);
172 1.2 thorpej void ecbdec(void);
173 1.2 thorpej void cbcenc(void);
174 1.2 thorpej void cbcdec(void);
175 1.2 thorpej void cbcauth(void);
176 1.2 thorpej void cfbenc(void);
177 1.2 thorpej void cfbdec(void);
178 1.2 thorpej void cfbaenc(void);
179 1.2 thorpej void cfbadec(void);
180 1.2 thorpej void ofbenc(void);
181 1.2 thorpej void ofbdec(void);
182 1.2 thorpej void cfbauth(void);
183 1.2 thorpej void expand(Desbuf, char *);
184 1.2 thorpej void compress(char *, Desbuf);
185 1.2 thorpej void usage(void);
186 1.2 thorpej
187 1.2 thorpej int
188 1.2 thorpej main(int ac, char *av[])
189 1.1 thorpej {
190 1.1 thorpej register int i; /* counter in a for loop */
191 1.1 thorpej register char *p; /* used to obtain the key */
192 1.1 thorpej Desbuf msgbuf; /* I/O buffer */
193 1.1 thorpej int kflag; /* command-line encryptiooon key */
194 1.1 thorpej int argc; /* the real arg count */
195 1.1 thorpej char **argv; /* the real argument vector */
196 1.1 thorpej
197 1.1 thorpej /*
198 1.1 thorpej * Hide the arguments from ps(1) by making private copies of them
199 1.1 thorpej * and clobbering the global (visible to ps(1)) ones.
200 1.1 thorpej */
201 1.1 thorpej argc = ac;
202 1.1 thorpej ac = 1;
203 1.1 thorpej argv = malloc((argc + 1) * sizeof(char *));
204 1.1 thorpej for (i = 0; i < argc; ++i) {
205 1.1 thorpej argv[i] = strdup(av[i]);
206 1.1 thorpej MEMZERO(av[i], strlen(av[i]));
207 1.1 thorpej }
208 1.1 thorpej argv[argc] = NULL;
209 1.1 thorpej
210 1.1 thorpej /* initialize the initialization vctor */
211 1.1 thorpej MEMZERO(ivec, 8);
212 1.1 thorpej
213 1.1 thorpej /* process the argument list */
214 1.1 thorpej kflag = 0;
215 1.3 mjl while ((i = getopt(argc, argv, "abdF:f:k:m:o:pv:")) != -1)
216 1.1 thorpej switch(i) {
217 1.1 thorpej case 'a': /* key is ASCII */
218 1.1 thorpej keybase = KEY_ASCII;
219 1.1 thorpej break;
220 1.1 thorpej case 'b': /* use ECB mode */
221 1.1 thorpej alg = ALG_ECB;
222 1.1 thorpej break;
223 1.1 thorpej case 'd': /* decrypt */
224 1.1 thorpej mode = MODE_DECRYPT;
225 1.1 thorpej break;
226 1.1 thorpej case 'F': /* use alternative CFB mode */
227 1.1 thorpej alg = ALG_CFBA;
228 1.1 thorpej if ((fbbits = setbits(optarg, 7)) > 56 || fbbits == 0)
229 1.2 thorpej bdes_err(-1,
230 1.2 thorpej "-F: number must be 1-56 inclusive");
231 1.1 thorpej else if (fbbits == -1)
232 1.2 thorpej bdes_err(-1,
233 1.2 thorpej "-F: number must be a multiple of 7");
234 1.1 thorpej break;
235 1.1 thorpej case 'f': /* use CFB mode */
236 1.1 thorpej alg = ALG_CFB;
237 1.1 thorpej if ((fbbits = setbits(optarg, 8)) > 64 || fbbits == 0)
238 1.2 thorpej bdes_err(-1,
239 1.2 thorpej "-f: number must be 1-64 inclusive");
240 1.1 thorpej else if (fbbits == -1)
241 1.2 thorpej bdes_err(-1,
242 1.2 thorpej "-f: number must be a multiple of 8");
243 1.1 thorpej break;
244 1.1 thorpej case 'k': /* encryption key */
245 1.1 thorpej kflag = 1;
246 1.1 thorpej cvtkey(BUFFER(msgbuf), optarg);
247 1.1 thorpej break;
248 1.1 thorpej case 'm': /* number of bits for MACing */
249 1.1 thorpej mode = MODE_AUTHENTICATE;
250 1.1 thorpej if ((macbits = setbits(optarg, 1)) > 64)
251 1.2 thorpej bdes_err(-1,
252 1.2 thorpej "-m: number must be 0-64 inclusive");
253 1.1 thorpej break;
254 1.1 thorpej case 'o': /* use OFB mode */
255 1.1 thorpej alg = ALG_OFB;
256 1.1 thorpej if ((fbbits = setbits(optarg, 8)) > 64 || fbbits == 0)
257 1.2 thorpej bdes_err(-1,
258 1.2 thorpej "-o: number must be 1-64 inclusive");
259 1.1 thorpej else if (fbbits == -1)
260 1.2 thorpej bdes_err(-1,
261 1.2 thorpej "-o: number must be a multiple of 8");
262 1.1 thorpej break;
263 1.1 thorpej case 'p': /* preserve parity bits */
264 1.1 thorpej pflag = 1;
265 1.1 thorpej break;
266 1.1 thorpej case 'v': /* set initialization vector */
267 1.1 thorpej cvtkey(BUFFER(ivec), optarg);
268 1.1 thorpej break;
269 1.1 thorpej default: /* error */
270 1.1 thorpej usage();
271 1.1 thorpej }
272 1.1 thorpej
273 1.1 thorpej if (!kflag) {
274 1.1 thorpej /*
275 1.1 thorpej * if the key's not ASCII, assume it is
276 1.1 thorpej */
277 1.1 thorpej keybase = KEY_ASCII;
278 1.1 thorpej /*
279 1.1 thorpej * get the key
280 1.1 thorpej */
281 1.1 thorpej p = getpass("Enter key: ");
282 1.1 thorpej /*
283 1.1 thorpej * copy it, nul-padded, into the key area
284 1.1 thorpej */
285 1.1 thorpej cvtkey(BUFFER(msgbuf), p);
286 1.1 thorpej }
287 1.1 thorpej
288 1.1 thorpej makekey(msgbuf);
289 1.1 thorpej inverse = (alg == ALG_CBC || alg == ALG_ECB) && mode == MODE_DECRYPT;
290 1.1 thorpej
291 1.1 thorpej switch(alg) {
292 1.1 thorpej case ALG_CBC:
293 1.1 thorpej switch(mode) {
294 1.1 thorpej case MODE_AUTHENTICATE: /* authenticate using CBC mode */
295 1.1 thorpej cbcauth();
296 1.1 thorpej break;
297 1.1 thorpej case MODE_DECRYPT: /* decrypt using CBC mode */
298 1.1 thorpej cbcdec();
299 1.1 thorpej break;
300 1.1 thorpej case MODE_ENCRYPT: /* encrypt using CBC mode */
301 1.1 thorpej cbcenc();
302 1.1 thorpej break;
303 1.1 thorpej }
304 1.1 thorpej break;
305 1.1 thorpej case ALG_CFB:
306 1.1 thorpej switch(mode) {
307 1.1 thorpej case MODE_AUTHENTICATE: /* authenticate using CFB mode */
308 1.1 thorpej cfbauth();
309 1.1 thorpej break;
310 1.1 thorpej case MODE_DECRYPT: /* decrypt using CFB mode */
311 1.1 thorpej cfbdec();
312 1.1 thorpej break;
313 1.1 thorpej case MODE_ENCRYPT: /* encrypt using CFB mode */
314 1.1 thorpej cfbenc();
315 1.1 thorpej break;
316 1.1 thorpej }
317 1.1 thorpej break;
318 1.1 thorpej case ALG_CFBA:
319 1.1 thorpej switch(mode) {
320 1.1 thorpej case MODE_AUTHENTICATE: /* authenticate using CFBA mode */
321 1.2 thorpej bdes_err(-1, "can't authenticate with CFBA mode");
322 1.1 thorpej break;
323 1.1 thorpej case MODE_DECRYPT: /* decrypt using CFBA mode */
324 1.1 thorpej cfbadec();
325 1.1 thorpej break;
326 1.1 thorpej case MODE_ENCRYPT: /* encrypt using CFBA mode */
327 1.1 thorpej cfbaenc();
328 1.1 thorpej break;
329 1.1 thorpej }
330 1.1 thorpej break;
331 1.1 thorpej case ALG_ECB:
332 1.1 thorpej switch(mode) {
333 1.1 thorpej case MODE_AUTHENTICATE: /* authenticate using ECB mode */
334 1.2 thorpej bdes_err(-1, "can't authenticate with ECB mode");
335 1.1 thorpej break;
336 1.1 thorpej case MODE_DECRYPT: /* decrypt using ECB mode */
337 1.1 thorpej ecbdec();
338 1.1 thorpej break;
339 1.1 thorpej case MODE_ENCRYPT: /* encrypt using ECB mode */
340 1.1 thorpej ecbenc();
341 1.1 thorpej break;
342 1.1 thorpej }
343 1.1 thorpej break;
344 1.1 thorpej case ALG_OFB:
345 1.1 thorpej switch(mode) {
346 1.1 thorpej case MODE_AUTHENTICATE: /* authenticate using OFB mode */
347 1.2 thorpej bdes_err(-1, "can't authenticate with OFB mode");
348 1.1 thorpej break;
349 1.1 thorpej case MODE_DECRYPT: /* decrypt using OFB mode */
350 1.1 thorpej ofbdec();
351 1.1 thorpej break;
352 1.1 thorpej case MODE_ENCRYPT: /* encrypt using OFB mode */
353 1.1 thorpej ofbenc();
354 1.1 thorpej break;
355 1.1 thorpej }
356 1.1 thorpej break;
357 1.1 thorpej }
358 1.1 thorpej exit(0);
359 1.1 thorpej }
360 1.1 thorpej
361 1.1 thorpej /*
362 1.1 thorpej * print a warning message and, possibly, terminate
363 1.1 thorpej */
364 1.2 thorpej void
365 1.2 thorpej bdes_err(int n, const char *s)
366 1.1 thorpej {
367 1.1 thorpej if (n > 0)
368 1.1 thorpej (void)fprintf(stderr, "bdes (block %d): ", n);
369 1.1 thorpej else
370 1.1 thorpej (void)fprintf(stderr, "bdes: ");
371 1.1 thorpej (void)fprintf(stderr, "%s\n", s ? s : strerror(errno));
372 1.1 thorpej exit(1);
373 1.1 thorpej }
374 1.1 thorpej
375 1.1 thorpej /*
376 1.1 thorpej * map a hex character to an integer
377 1.1 thorpej */
378 1.2 thorpej int
379 1.2 thorpej tobinhex(char c, int radix)
380 1.1 thorpej {
381 1.1 thorpej switch(c) {
382 1.1 thorpej case '0': return(0x0);
383 1.1 thorpej case '1': return(0x1);
384 1.1 thorpej case '2': return(radix > 2 ? 0x2 : -1);
385 1.1 thorpej case '3': return(radix > 3 ? 0x3 : -1);
386 1.1 thorpej case '4': return(radix > 4 ? 0x4 : -1);
387 1.1 thorpej case '5': return(radix > 5 ? 0x5 : -1);
388 1.1 thorpej case '6': return(radix > 6 ? 0x6 : -1);
389 1.1 thorpej case '7': return(radix > 7 ? 0x7 : -1);
390 1.1 thorpej case '8': return(radix > 8 ? 0x8 : -1);
391 1.1 thorpej case '9': return(radix > 9 ? 0x9 : -1);
392 1.1 thorpej case 'A': case 'a': return(radix > 10 ? 0xa : -1);
393 1.1 thorpej case 'B': case 'b': return(radix > 11 ? 0xb : -1);
394 1.1 thorpej case 'C': case 'c': return(radix > 12 ? 0xc : -1);
395 1.1 thorpej case 'D': case 'd': return(radix > 13 ? 0xd : -1);
396 1.1 thorpej case 'E': case 'e': return(radix > 14 ? 0xe : -1);
397 1.1 thorpej case 'F': case 'f': return(radix > 15 ? 0xf : -1);
398 1.1 thorpej }
399 1.1 thorpej /*
400 1.1 thorpej * invalid character
401 1.1 thorpej */
402 1.1 thorpej return(-1);
403 1.1 thorpej }
404 1.1 thorpej
405 1.1 thorpej /*
406 1.1 thorpej * convert the key to a bit pattern
407 1.1 thorpej */
408 1.2 thorpej void
409 1.2 thorpej cvtkey(char *obuf, char *ibuf)
410 1.1 thorpej {
411 1.1 thorpej register int i, j; /* counter in a for loop */
412 1.1 thorpej int nbuf[64]; /* used for hex/key translation */
413 1.1 thorpej
414 1.1 thorpej /*
415 1.1 thorpej * just switch on the key base
416 1.1 thorpej */
417 1.1 thorpej switch(keybase) {
418 1.1 thorpej case KEY_ASCII: /* ascii to integer */
419 1.1 thorpej (void)strncpy(obuf, ibuf, 8);
420 1.1 thorpej return;
421 1.1 thorpej case KEY_DEFAULT: /* tell from context */
422 1.1 thorpej /*
423 1.1 thorpej * leading '0x' or '0X' == hex key
424 1.1 thorpej */
425 1.1 thorpej if (ibuf[0] == '0' && (ibuf[1] == 'x' || ibuf[1] == 'X')) {
426 1.1 thorpej ibuf = &ibuf[2];
427 1.1 thorpej /*
428 1.1 thorpej * now translate it, bombing on any illegal hex digit
429 1.1 thorpej */
430 1.1 thorpej for (i = 0; ibuf[i] && i < 16; i++)
431 1.1 thorpej if ((nbuf[i] = tobinhex(ibuf[i], 16)) == -1)
432 1.2 thorpej bdes_err(-1, "bad hex digit in key");
433 1.1 thorpej while (i < 16)
434 1.1 thorpej nbuf[i++] = 0;
435 1.1 thorpej for (i = 0; i < 8; i++)
436 1.1 thorpej obuf[i] =
437 1.1 thorpej ((nbuf[2*i]&0xf)<<4) | (nbuf[2*i+1]&0xf);
438 1.1 thorpej /* preserve parity bits */
439 1.1 thorpej pflag = 1;
440 1.1 thorpej return;
441 1.1 thorpej }
442 1.1 thorpej /*
443 1.1 thorpej * leading '0b' or '0B' == binary key
444 1.1 thorpej */
445 1.1 thorpej if (ibuf[0] == '0' && (ibuf[1] == 'b' || ibuf[1] == 'B')) {
446 1.1 thorpej ibuf = &ibuf[2];
447 1.1 thorpej /*
448 1.1 thorpej * now translate it, bombing on any illegal binary digit
449 1.1 thorpej */
450 1.1 thorpej for (i = 0; ibuf[i] && i < 16; i++)
451 1.1 thorpej if ((nbuf[i] = tobinhex(ibuf[i], 2)) == -1)
452 1.2 thorpej bdes_err(-1, "bad binary digit in key");
453 1.1 thorpej while (i < 64)
454 1.1 thorpej nbuf[i++] = 0;
455 1.1 thorpej for (i = 0; i < 8; i++)
456 1.1 thorpej for (j = 0; j < 8; j++)
457 1.1 thorpej obuf[i] = (obuf[i]<<1)|nbuf[8*i+j];
458 1.1 thorpej /* preserve parity bits */
459 1.1 thorpej pflag = 1;
460 1.1 thorpej return;
461 1.1 thorpej }
462 1.1 thorpej /*
463 1.1 thorpej * no special leader -- ASCII
464 1.1 thorpej */
465 1.1 thorpej (void)strncpy(obuf, ibuf, 8);
466 1.1 thorpej }
467 1.1 thorpej }
468 1.1 thorpej
469 1.1 thorpej /*
470 1.1 thorpej * convert an ASCII string into a decimal number:
471 1.1 thorpej * 1. must be between 0 and 64 inclusive
472 1.1 thorpej * 2. must be a valid decimal number
473 1.1 thorpej * 3. must be a multiple of mult
474 1.1 thorpej */
475 1.2 thorpej int
476 1.2 thorpej setbits(char *s, int mult)
477 1.1 thorpej {
478 1.1 thorpej register char *p; /* pointer in a for loop */
479 1.1 thorpej register int n = 0; /* the integer collected */
480 1.1 thorpej
481 1.1 thorpej /*
482 1.1 thorpej * skip white space
483 1.1 thorpej */
484 1.1 thorpej while (isspace(*s))
485 1.1 thorpej s++;
486 1.1 thorpej /*
487 1.1 thorpej * get the integer
488 1.1 thorpej */
489 1.1 thorpej for (p = s; *p; p++) {
490 1.1 thorpej if (isdigit(*p))
491 1.1 thorpej n = n * 10 + *p - '0';
492 1.1 thorpej else {
493 1.2 thorpej bdes_err(-1, "bad decimal digit in MAC length");
494 1.1 thorpej }
495 1.1 thorpej }
496 1.1 thorpej /*
497 1.1 thorpej * be sure it's a multiple of mult
498 1.1 thorpej */
499 1.1 thorpej return((n % mult != 0) ? -1 : n);
500 1.1 thorpej }
501 1.1 thorpej
502 1.1 thorpej /*****************
503 1.1 thorpej * DES FUNCTIONS *
504 1.1 thorpej *****************/
505 1.1 thorpej /*
506 1.1 thorpej * This sets the DES key and (if you're using the deszip version)
507 1.1 thorpej * the direction of the transformation. This uses the Sun
508 1.1 thorpej * to map the 64-bit key onto the 56 bits that the key schedule
509 1.1 thorpej * generation routines use: the old way, which just uses the user-
510 1.1 thorpej * supplied 64 bits as is, and the new way, which resets the parity
511 1.1 thorpej * bit to be the same as the low-order bit in each character. The
512 1.1 thorpej * new way generates a greater variety of key schedules, since many
513 1.1 thorpej * systems set the parity (high) bit of each character to 0, and the
514 1.1 thorpej * DES ignores the low order bit of each character.
515 1.1 thorpej */
516 1.2 thorpej void
517 1.2 thorpej makekey(Desbuf buf)
518 1.1 thorpej {
519 1.1 thorpej register int i, j; /* counter in a for loop */
520 1.1 thorpej register int par; /* parity counter */
521 1.1 thorpej
522 1.1 thorpej /*
523 1.1 thorpej * if the parity is not preserved, flip it
524 1.1 thorpej */
525 1.1 thorpej if (!pflag) {
526 1.1 thorpej for (i = 0; i < 8; i++) {
527 1.1 thorpej par = 0;
528 1.1 thorpej for (j = 1; j < 8; j++)
529 1.1 thorpej if ((bits[j]&UCHAR(buf, i)) != 0)
530 1.1 thorpej par++;
531 1.1 thorpej if ((par&01) == 01)
532 1.1 thorpej UCHAR(buf, i) = UCHAR(buf, i)&0177;
533 1.1 thorpej else
534 1.1 thorpej UCHAR(buf, i) = (UCHAR(buf, i)&0177)|0200;
535 1.1 thorpej }
536 1.1 thorpej }
537 1.1 thorpej
538 1.1 thorpej DES_KEY(UBUFFER(buf));
539 1.1 thorpej }
540 1.1 thorpej
541 1.1 thorpej /*
542 1.1 thorpej * This encrypts using the Electronic Code Book mode of DES
543 1.1 thorpej */
544 1.2 thorpej void
545 1.2 thorpej ecbenc(void)
546 1.1 thorpej {
547 1.1 thorpej register int n; /* number of bytes actually read */
548 1.1 thorpej register int bn; /* block number */
549 1.1 thorpej Desbuf msgbuf; /* I/O buffer */
550 1.1 thorpej
551 1.1 thorpej for (bn = 0; (n = READ(BUFFER(msgbuf), 8)) == 8; bn++) {
552 1.1 thorpej /*
553 1.1 thorpej * do the transformation
554 1.1 thorpej */
555 1.1 thorpej DES_XFORM(UBUFFER(msgbuf));
556 1.1 thorpej WRITE(BUFFER(msgbuf), 8);
557 1.1 thorpej }
558 1.1 thorpej /*
559 1.1 thorpej * at EOF or last block -- in either ase, the last byte contains
560 1.1 thorpej * the character representation of the number of bytes in it
561 1.1 thorpej */
562 1.1 thorpej bn++;
563 1.1 thorpej MEMZERO(&CHAR(msgbuf, n), 8 - n);
564 1.1 thorpej CHAR(msgbuf, 7) = n;
565 1.1 thorpej DES_XFORM(UBUFFER(msgbuf));
566 1.1 thorpej WRITE(BUFFER(msgbuf), 8);
567 1.1 thorpej
568 1.1 thorpej }
569 1.1 thorpej
570 1.1 thorpej /*
571 1.1 thorpej * This decrypts using the Electronic Code Book mode of DES
572 1.1 thorpej */
573 1.2 thorpej void
574 1.2 thorpej ecbdec(void)
575 1.1 thorpej {
576 1.1 thorpej register int n; /* number of bytes actually read */
577 1.1 thorpej register int c; /* used to test for EOF */
578 1.1 thorpej register int bn; /* block number */
579 1.1 thorpej Desbuf msgbuf; /* I/O buffer */
580 1.1 thorpej
581 1.1 thorpej for (bn = 1; (n = READ(BUFFER(msgbuf), 8)) == 8; bn++) {
582 1.1 thorpej /*
583 1.1 thorpej * do the transformation
584 1.1 thorpej */
585 1.1 thorpej DES_XFORM(UBUFFER(msgbuf));
586 1.1 thorpej /*
587 1.1 thorpej * if the last one, handle it specially
588 1.1 thorpej */
589 1.1 thorpej if ((c = getchar()) == EOF) {
590 1.1 thorpej n = CHAR(msgbuf, 7);
591 1.1 thorpej if (n < 0 || n > 7)
592 1.2 thorpej bdes_err(bn,
593 1.2 thorpej "decryption failed (block corrupted)");
594 1.1 thorpej }
595 1.1 thorpej else
596 1.1 thorpej (void)ungetc(c, stdin);
597 1.1 thorpej WRITE(BUFFER(msgbuf), n);
598 1.1 thorpej }
599 1.1 thorpej if (n > 0)
600 1.2 thorpej bdes_err(bn, "decryption failed (incomplete block)");
601 1.1 thorpej }
602 1.1 thorpej
603 1.1 thorpej /*
604 1.1 thorpej * This encrypts using the Cipher Block Chaining mode of DES
605 1.1 thorpej */
606 1.2 thorpej void
607 1.2 thorpej cbcenc(void)
608 1.1 thorpej {
609 1.1 thorpej register int n; /* number of bytes actually read */
610 1.1 thorpej register int bn; /* block number */
611 1.1 thorpej Desbuf msgbuf; /* I/O buffer */
612 1.1 thorpej
613 1.1 thorpej /*
614 1.1 thorpej * do the transformation
615 1.1 thorpej */
616 1.1 thorpej for (bn = 1; (n = READ(BUFFER(msgbuf), 8)) == 8; bn++) {
617 1.1 thorpej for (n = 0; n < 8; n++)
618 1.1 thorpej CHAR(msgbuf, n) ^= CHAR(ivec, n);
619 1.1 thorpej DES_XFORM(UBUFFER(msgbuf));
620 1.1 thorpej MEMCPY(BUFFER(ivec), BUFFER(msgbuf), 8);
621 1.1 thorpej WRITE(BUFFER(msgbuf), 8);
622 1.1 thorpej }
623 1.1 thorpej /*
624 1.1 thorpej * at EOF or last block -- in either case, the last byte contains
625 1.1 thorpej * the character representation of the number of bytes in it
626 1.1 thorpej */
627 1.1 thorpej bn++;
628 1.1 thorpej MEMZERO(&CHAR(msgbuf, n), 8 - n);
629 1.1 thorpej CHAR(msgbuf, 7) = n;
630 1.1 thorpej for (n = 0; n < 8; n++)
631 1.1 thorpej CHAR(msgbuf, n) ^= CHAR(ivec, n);
632 1.1 thorpej DES_XFORM(UBUFFER(msgbuf));
633 1.1 thorpej WRITE(BUFFER(msgbuf), 8);
634 1.1 thorpej
635 1.1 thorpej }
636 1.1 thorpej
637 1.1 thorpej /*
638 1.1 thorpej * This decrypts using the Cipher Block Chaining mode of DES
639 1.1 thorpej */
640 1.2 thorpej void
641 1.2 thorpej cbcdec(void)
642 1.1 thorpej {
643 1.1 thorpej register int n; /* number of bytes actually read */
644 1.1 thorpej Desbuf msgbuf; /* I/O buffer */
645 1.1 thorpej Desbuf ibuf; /* temp buffer for initialization vector */
646 1.1 thorpej register int c; /* used to test for EOF */
647 1.1 thorpej register int bn; /* block number */
648 1.1 thorpej
649 1.1 thorpej for (bn = 0; (n = READ(BUFFER(msgbuf), 8)) == 8; bn++) {
650 1.1 thorpej /*
651 1.1 thorpej * do the transformation
652 1.1 thorpej */
653 1.1 thorpej MEMCPY(BUFFER(ibuf), BUFFER(msgbuf), 8);
654 1.1 thorpej DES_XFORM(UBUFFER(msgbuf));
655 1.1 thorpej for (c = 0; c < 8; c++)
656 1.1 thorpej UCHAR(msgbuf, c) ^= UCHAR(ivec, c);
657 1.1 thorpej MEMCPY(BUFFER(ivec), BUFFER(ibuf), 8);
658 1.1 thorpej /*
659 1.1 thorpej * if the last one, handle it specially
660 1.1 thorpej */
661 1.1 thorpej if ((c = getchar()) == EOF) {
662 1.1 thorpej n = CHAR(msgbuf, 7);
663 1.1 thorpej if (n < 0 || n > 7)
664 1.2 thorpej bdes_err(bn,
665 1.2 thorpej "decryption failed (block corrupted)");
666 1.1 thorpej }
667 1.1 thorpej else
668 1.1 thorpej (void)ungetc(c, stdin);
669 1.1 thorpej WRITE(BUFFER(msgbuf), n);
670 1.1 thorpej }
671 1.1 thorpej if (n > 0)
672 1.2 thorpej bdes_err(bn, "decryption failed (incomplete block)");
673 1.1 thorpej }
674 1.1 thorpej
675 1.1 thorpej /*
676 1.1 thorpej * This authenticates using the Cipher Block Chaining mode of DES
677 1.1 thorpej */
678 1.2 thorpej void
679 1.2 thorpej cbcauth(void)
680 1.1 thorpej {
681 1.1 thorpej register int n, j; /* number of bytes actually read */
682 1.1 thorpej Desbuf msgbuf; /* I/O buffer */
683 1.1 thorpej Desbuf encbuf; /* encryption buffer */
684 1.1 thorpej
685 1.1 thorpej /*
686 1.1 thorpej * do the transformation
687 1.1 thorpej * note we DISCARD the encrypted block;
688 1.1 thorpej * we only care about the last one
689 1.1 thorpej */
690 1.1 thorpej while ((n = READ(BUFFER(msgbuf), 8)) == 8) {
691 1.1 thorpej for (n = 0; n < 8; n++)
692 1.1 thorpej CHAR(encbuf, n) = CHAR(msgbuf, n) ^ CHAR(ivec, n);
693 1.1 thorpej DES_XFORM(UBUFFER(encbuf));
694 1.1 thorpej MEMCPY(BUFFER(ivec), BUFFER(encbuf), 8);
695 1.1 thorpej }
696 1.1 thorpej /*
697 1.1 thorpej * now compute the last one, right padding with '\0' if need be
698 1.1 thorpej */
699 1.1 thorpej if (n > 0) {
700 1.1 thorpej MEMZERO(&CHAR(msgbuf, n), 8 - n);
701 1.1 thorpej for (n = 0; n < 8; n++)
702 1.1 thorpej CHAR(encbuf, n) = CHAR(msgbuf, n) ^ CHAR(ivec, n);
703 1.1 thorpej DES_XFORM(UBUFFER(encbuf));
704 1.1 thorpej }
705 1.1 thorpej /*
706 1.1 thorpej * drop the bits
707 1.1 thorpej * we write chars until fewer than 7 bits,
708 1.1 thorpej * and then pad the last one with 0 bits
709 1.1 thorpej */
710 1.1 thorpej for (n = 0; macbits > 7; n++, macbits -= 8)
711 1.1 thorpej (void)putchar(CHAR(encbuf, n));
712 1.1 thorpej if (macbits > 0) {
713 1.1 thorpej CHAR(msgbuf, 0) = 0x00;
714 1.1 thorpej for (j = 0; j < macbits; j++)
715 1.1 thorpej CHAR(msgbuf, 0) |= (CHAR(encbuf, n)&bits[j]);
716 1.1 thorpej (void)putchar(CHAR(msgbuf, 0));
717 1.1 thorpej }
718 1.1 thorpej }
719 1.1 thorpej
720 1.1 thorpej /*
721 1.1 thorpej * This encrypts using the Cipher FeedBack mode of DES
722 1.1 thorpej */
723 1.2 thorpej void
724 1.2 thorpej cfbenc(void)
725 1.1 thorpej {
726 1.1 thorpej register int n; /* number of bytes actually read */
727 1.1 thorpej register int nbytes; /* number of bytes to read */
728 1.1 thorpej register int bn; /* block number */
729 1.1 thorpej char ibuf[8]; /* input buffer */
730 1.1 thorpej Desbuf msgbuf; /* encryption buffer */
731 1.1 thorpej
732 1.1 thorpej /*
733 1.1 thorpej * do things in bytes, not bits
734 1.1 thorpej */
735 1.1 thorpej nbytes = fbbits / 8;
736 1.1 thorpej /*
737 1.1 thorpej * do the transformation
738 1.1 thorpej */
739 1.1 thorpej for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
740 1.1 thorpej MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
741 1.1 thorpej DES_XFORM(UBUFFER(msgbuf));
742 1.1 thorpej for (n = 0; n < 8 - nbytes; n++)
743 1.1 thorpej UCHAR(ivec, n) = UCHAR(ivec, n+nbytes);
744 1.1 thorpej for (n = 0; n < nbytes; n++)
745 1.1 thorpej UCHAR(ivec, 8-nbytes+n) = ibuf[n] ^ UCHAR(msgbuf, n);
746 1.1 thorpej WRITE(&CHAR(ivec, 8-nbytes), nbytes);
747 1.1 thorpej }
748 1.1 thorpej /*
749 1.1 thorpej * at EOF or last block -- in either case, the last byte contains
750 1.1 thorpej * the character representation of the number of bytes in it
751 1.1 thorpej */
752 1.1 thorpej bn++;
753 1.1 thorpej MEMZERO(&ibuf[n], nbytes - n);
754 1.1 thorpej ibuf[nbytes - 1] = n;
755 1.1 thorpej MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
756 1.1 thorpej DES_XFORM(UBUFFER(msgbuf));
757 1.1 thorpej for (n = 0; n < nbytes; n++)
758 1.1 thorpej ibuf[n] ^= UCHAR(msgbuf, n);
759 1.1 thorpej WRITE(ibuf, nbytes);
760 1.1 thorpej }
761 1.1 thorpej
762 1.1 thorpej /*
763 1.1 thorpej * This decrypts using the Cipher Block Chaining mode of DES
764 1.1 thorpej */
765 1.2 thorpej void
766 1.2 thorpej cfbdec(void)
767 1.1 thorpej {
768 1.1 thorpej register int n; /* number of bytes actually read */
769 1.1 thorpej register int c; /* used to test for EOF */
770 1.1 thorpej register int nbytes; /* number of bytes to read */
771 1.1 thorpej register int bn; /* block number */
772 1.1 thorpej char ibuf[8]; /* input buffer */
773 1.1 thorpej char obuf[8]; /* output buffer */
774 1.1 thorpej Desbuf msgbuf; /* encryption buffer */
775 1.1 thorpej
776 1.1 thorpej /*
777 1.1 thorpej * do things in bytes, not bits
778 1.1 thorpej */
779 1.1 thorpej nbytes = fbbits / 8;
780 1.1 thorpej /*
781 1.1 thorpej * do the transformation
782 1.1 thorpej */
783 1.1 thorpej for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
784 1.1 thorpej MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
785 1.1 thorpej DES_XFORM(UBUFFER(msgbuf));
786 1.1 thorpej for (c = 0; c < 8 - nbytes; c++)
787 1.1 thorpej CHAR(ivec, c) = CHAR(ivec, c+nbytes);
788 1.1 thorpej for (c = 0; c < nbytes; c++) {
789 1.1 thorpej CHAR(ivec, 8-nbytes+c) = ibuf[c];
790 1.1 thorpej obuf[c] = ibuf[c] ^ UCHAR(msgbuf, c);
791 1.1 thorpej }
792 1.1 thorpej /*
793 1.1 thorpej * if the last one, handle it specially
794 1.1 thorpej */
795 1.1 thorpej if ((c = getchar()) == EOF) {
796 1.1 thorpej n = obuf[nbytes-1];
797 1.1 thorpej if (n < 0 || n > nbytes-1)
798 1.2 thorpej bdes_err(bn,
799 1.2 thorpej "decryption failed (block corrupted)");
800 1.1 thorpej }
801 1.1 thorpej else
802 1.1 thorpej (void)ungetc(c, stdin);
803 1.1 thorpej WRITE(obuf, n);
804 1.1 thorpej }
805 1.1 thorpej if (n > 0)
806 1.2 thorpej bdes_err(bn, "decryption failed (incomplete block)");
807 1.1 thorpej }
808 1.1 thorpej
809 1.1 thorpej /*
810 1.1 thorpej * This encrypts using the alternative Cipher FeedBack mode of DES
811 1.1 thorpej */
812 1.2 thorpej void
813 1.2 thorpej cfbaenc(void)
814 1.1 thorpej {
815 1.1 thorpej register int n; /* number of bytes actually read */
816 1.1 thorpej register int nbytes; /* number of bytes to read */
817 1.1 thorpej register int bn; /* block number */
818 1.1 thorpej char ibuf[8]; /* input buffer */
819 1.1 thorpej char obuf[8]; /* output buffer */
820 1.1 thorpej Desbuf msgbuf; /* encryption buffer */
821 1.1 thorpej
822 1.1 thorpej /*
823 1.1 thorpej * do things in bytes, not bits
824 1.1 thorpej */
825 1.1 thorpej nbytes = fbbits / 7;
826 1.1 thorpej /*
827 1.1 thorpej * do the transformation
828 1.1 thorpej */
829 1.1 thorpej for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
830 1.1 thorpej MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
831 1.1 thorpej DES_XFORM(UBUFFER(msgbuf));
832 1.1 thorpej for (n = 0; n < 8 - nbytes; n++)
833 1.1 thorpej UCHAR(ivec, n) = UCHAR(ivec, n+nbytes);
834 1.1 thorpej for (n = 0; n < nbytes; n++)
835 1.1 thorpej UCHAR(ivec, 8-nbytes+n) = (ibuf[n] ^ UCHAR(msgbuf, n))
836 1.1 thorpej |0200;
837 1.1 thorpej for (n = 0; n < nbytes; n++)
838 1.1 thorpej obuf[n] = CHAR(ivec, 8-nbytes+n)&0177;
839 1.1 thorpej WRITE(obuf, nbytes);
840 1.1 thorpej }
841 1.1 thorpej /*
842 1.1 thorpej * at EOF or last block -- in either case, the last byte contains
843 1.1 thorpej * the character representation of the number of bytes in it
844 1.1 thorpej */
845 1.1 thorpej bn++;
846 1.1 thorpej MEMZERO(&ibuf[n], nbytes - n);
847 1.1 thorpej ibuf[nbytes - 1] = ('0' + n)|0200;
848 1.1 thorpej MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
849 1.1 thorpej DES_XFORM(UBUFFER(msgbuf));
850 1.1 thorpej for (n = 0; n < nbytes; n++)
851 1.1 thorpej ibuf[n] ^= UCHAR(msgbuf, n);
852 1.1 thorpej WRITE(ibuf, nbytes);
853 1.1 thorpej }
854 1.1 thorpej
855 1.1 thorpej /*
856 1.1 thorpej * This decrypts using the alternative Cipher Block Chaining mode of DES
857 1.1 thorpej */
858 1.2 thorpej void
859 1.2 thorpej cfbadec(void)
860 1.1 thorpej {
861 1.1 thorpej register int n; /* number of bytes actually read */
862 1.1 thorpej register int c; /* used to test for EOF */
863 1.1 thorpej register int nbytes; /* number of bytes to read */
864 1.1 thorpej register int bn; /* block number */
865 1.1 thorpej char ibuf[8]; /* input buffer */
866 1.1 thorpej char obuf[8]; /* output buffer */
867 1.1 thorpej Desbuf msgbuf; /* encryption buffer */
868 1.1 thorpej
869 1.1 thorpej /*
870 1.1 thorpej * do things in bytes, not bits
871 1.1 thorpej */
872 1.1 thorpej nbytes = fbbits / 7;
873 1.1 thorpej /*
874 1.1 thorpej * do the transformation
875 1.1 thorpej */
876 1.1 thorpej for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
877 1.1 thorpej MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
878 1.1 thorpej DES_XFORM(UBUFFER(msgbuf));
879 1.1 thorpej for (c = 0; c < 8 - nbytes; c++)
880 1.1 thorpej CHAR(ivec, c) = CHAR(ivec, c+nbytes);
881 1.1 thorpej for (c = 0; c < nbytes; c++) {
882 1.1 thorpej CHAR(ivec, 8-nbytes+c) = ibuf[c]|0200;
883 1.1 thorpej obuf[c] = (ibuf[c] ^ UCHAR(msgbuf, c))&0177;
884 1.1 thorpej }
885 1.1 thorpej /*
886 1.1 thorpej * if the last one, handle it specially
887 1.1 thorpej */
888 1.1 thorpej if ((c = getchar()) == EOF) {
889 1.1 thorpej if ((n = (obuf[nbytes-1] - '0')) < 0
890 1.1 thorpej || n > nbytes-1)
891 1.2 thorpej bdes_err(bn,
892 1.2 thorpej "decryption failed (block corrupted)");
893 1.1 thorpej }
894 1.1 thorpej else
895 1.1 thorpej (void)ungetc(c, stdin);
896 1.1 thorpej WRITE(obuf, n);
897 1.1 thorpej }
898 1.1 thorpej if (n > 0)
899 1.2 thorpej bdes_err(bn, "decryption failed (incomplete block)");
900 1.1 thorpej }
901 1.1 thorpej
902 1.1 thorpej
903 1.1 thorpej /*
904 1.1 thorpej * This encrypts using the Output FeedBack mode of DES
905 1.1 thorpej */
906 1.2 thorpej void
907 1.2 thorpej ofbenc(void)
908 1.1 thorpej {
909 1.1 thorpej register int n; /* number of bytes actually read */
910 1.1 thorpej register int c; /* used to test for EOF */
911 1.1 thorpej register int nbytes; /* number of bytes to read */
912 1.1 thorpej register int bn; /* block number */
913 1.1 thorpej char ibuf[8]; /* input buffer */
914 1.1 thorpej char obuf[8]; /* output buffer */
915 1.1 thorpej Desbuf msgbuf; /* encryption buffer */
916 1.1 thorpej
917 1.1 thorpej /*
918 1.1 thorpej * do things in bytes, not bits
919 1.1 thorpej */
920 1.1 thorpej nbytes = fbbits / 8;
921 1.1 thorpej /*
922 1.1 thorpej * do the transformation
923 1.1 thorpej */
924 1.1 thorpej for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
925 1.1 thorpej MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
926 1.1 thorpej DES_XFORM(UBUFFER(msgbuf));
927 1.1 thorpej for (n = 0; n < 8 - nbytes; n++)
928 1.1 thorpej UCHAR(ivec, n) = UCHAR(ivec, n+nbytes);
929 1.1 thorpej for (n = 0; n < nbytes; n++) {
930 1.1 thorpej UCHAR(ivec, 8-nbytes+n) = UCHAR(msgbuf, n);
931 1.1 thorpej obuf[n] = ibuf[n] ^ UCHAR(msgbuf, n);
932 1.1 thorpej }
933 1.1 thorpej WRITE(obuf, nbytes);
934 1.1 thorpej }
935 1.1 thorpej /*
936 1.1 thorpej * at EOF or last block -- in either case, the last byte contains
937 1.1 thorpej * the character representation of the number of bytes in it
938 1.1 thorpej */
939 1.1 thorpej bn++;
940 1.1 thorpej MEMZERO(&ibuf[n], nbytes - n);
941 1.1 thorpej ibuf[nbytes - 1] = n;
942 1.1 thorpej MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
943 1.1 thorpej DES_XFORM(UBUFFER(msgbuf));
944 1.1 thorpej for (c = 0; c < nbytes; c++)
945 1.1 thorpej ibuf[c] ^= UCHAR(msgbuf, c);
946 1.1 thorpej WRITE(ibuf, nbytes);
947 1.1 thorpej }
948 1.1 thorpej
949 1.1 thorpej /*
950 1.1 thorpej * This decrypts using the Output Block Chaining mode of DES
951 1.1 thorpej */
952 1.2 thorpej void
953 1.2 thorpej ofbdec(void)
954 1.1 thorpej {
955 1.1 thorpej register int n; /* number of bytes actually read */
956 1.1 thorpej register int c; /* used to test for EOF */
957 1.1 thorpej register int nbytes; /* number of bytes to read */
958 1.1 thorpej register int bn; /* block number */
959 1.1 thorpej char ibuf[8]; /* input buffer */
960 1.1 thorpej char obuf[8]; /* output buffer */
961 1.1 thorpej Desbuf msgbuf; /* encryption buffer */
962 1.1 thorpej
963 1.1 thorpej /*
964 1.1 thorpej * do things in bytes, not bits
965 1.1 thorpej */
966 1.1 thorpej nbytes = fbbits / 8;
967 1.1 thorpej /*
968 1.1 thorpej * do the transformation
969 1.1 thorpej */
970 1.1 thorpej for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
971 1.1 thorpej MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
972 1.1 thorpej DES_XFORM(UBUFFER(msgbuf));
973 1.1 thorpej for (c = 0; c < 8 - nbytes; c++)
974 1.1 thorpej CHAR(ivec, c) = CHAR(ivec, c+nbytes);
975 1.1 thorpej for (c = 0; c < nbytes; c++) {
976 1.1 thorpej CHAR(ivec, 8-nbytes+c) = UCHAR(msgbuf, c);
977 1.1 thorpej obuf[c] = ibuf[c] ^ UCHAR(msgbuf, c);
978 1.1 thorpej }
979 1.1 thorpej /*
980 1.1 thorpej * if the last one, handle it specially
981 1.1 thorpej */
982 1.1 thorpej if ((c = getchar()) == EOF) {
983 1.1 thorpej n = obuf[nbytes-1];
984 1.1 thorpej if (n < 0 || n > nbytes-1)
985 1.2 thorpej bdes_err(bn,
986 1.2 thorpej "decryption failed (block corrupted)");
987 1.1 thorpej }
988 1.1 thorpej else
989 1.1 thorpej (void)ungetc(c, stdin);
990 1.1 thorpej /*
991 1.1 thorpej * dump it
992 1.1 thorpej */
993 1.1 thorpej WRITE(obuf, n);
994 1.1 thorpej }
995 1.1 thorpej if (n > 0)
996 1.2 thorpej bdes_err(bn, "decryption failed (incomplete block)");
997 1.1 thorpej }
998 1.1 thorpej
999 1.1 thorpej /*
1000 1.1 thorpej * This authenticates using the Cipher FeedBack mode of DES
1001 1.1 thorpej */
1002 1.2 thorpej void
1003 1.2 thorpej cfbauth(void)
1004 1.1 thorpej {
1005 1.1 thorpej register int n, j; /* number of bytes actually read */
1006 1.1 thorpej register int nbytes; /* number of bytes to read */
1007 1.1 thorpej char ibuf[8]; /* input buffer */
1008 1.1 thorpej Desbuf msgbuf; /* encryption buffer */
1009 1.1 thorpej
1010 1.1 thorpej /*
1011 1.1 thorpej * do things in bytes, not bits
1012 1.1 thorpej */
1013 1.1 thorpej nbytes = fbbits / 8;
1014 1.1 thorpej /*
1015 1.1 thorpej * do the transformation
1016 1.1 thorpej */
1017 1.1 thorpej while ((n = READ(ibuf, nbytes)) == nbytes) {
1018 1.1 thorpej MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
1019 1.1 thorpej DES_XFORM(UBUFFER(msgbuf));
1020 1.1 thorpej for (n = 0; n < 8 - nbytes; n++)
1021 1.1 thorpej UCHAR(ivec, n) = UCHAR(ivec, n+nbytes);
1022 1.1 thorpej for (n = 0; n < nbytes; n++)
1023 1.1 thorpej UCHAR(ivec, 8-nbytes+n) = ibuf[n] ^ UCHAR(msgbuf, n);
1024 1.1 thorpej }
1025 1.1 thorpej /*
1026 1.1 thorpej * at EOF or last block -- in either case, the last byte contains
1027 1.1 thorpej * the character representation of the number of bytes in it
1028 1.1 thorpej */
1029 1.1 thorpej MEMZERO(&ibuf[n], nbytes - n);
1030 1.1 thorpej ibuf[nbytes - 1] = '0' + n;
1031 1.1 thorpej MEMCPY(BUFFER(msgbuf), BUFFER(ivec), 8);
1032 1.1 thorpej DES_XFORM(UBUFFER(msgbuf));
1033 1.1 thorpej for (n = 0; n < nbytes; n++)
1034 1.1 thorpej ibuf[n] ^= UCHAR(msgbuf, n);
1035 1.1 thorpej /*
1036 1.1 thorpej * drop the bits
1037 1.1 thorpej * we write chars until fewer than 7 bits,
1038 1.1 thorpej * and then pad the last one with 0 bits
1039 1.1 thorpej */
1040 1.1 thorpej for (n = 0; macbits > 7; n++, macbits -= 8)
1041 1.1 thorpej (void)putchar(CHAR(msgbuf, n));
1042 1.1 thorpej if (macbits > 0) {
1043 1.1 thorpej CHAR(msgbuf, 0) = 0x00;
1044 1.1 thorpej for (j = 0; j < macbits; j++)
1045 1.1 thorpej CHAR(msgbuf, 0) |= (CHAR(msgbuf, n)&bits[j]);
1046 1.1 thorpej (void)putchar(CHAR(msgbuf, 0));
1047 1.1 thorpej }
1048 1.1 thorpej }
1049 1.1 thorpej
1050 1.1 thorpej #ifndef FASTWAY
1051 1.1 thorpej /*
1052 1.1 thorpej * change from 8 bits/Uchar to 1 bit/Uchar
1053 1.1 thorpej */
1054 1.2 thorpej void
1055 1.2 thorpej expand(Desbuf from, char *to)
1056 1.1 thorpej {
1057 1.1 thorpej register int i, j; /* counters in for loop */
1058 1.1 thorpej
1059 1.1 thorpej for (i = 0; i < 8; i++)
1060 1.1 thorpej for (j = 0; j < 8; j++)
1061 1.1 thorpej *to++ = (CHAR(from, i)>>(7-j))&01;
1062 1.1 thorpej }
1063 1.1 thorpej
1064 1.1 thorpej /*
1065 1.1 thorpej * change from 1 bit/char to 8 bits/Uchar
1066 1.1 thorpej */
1067 1.2 thorpej void
1068 1.2 thorpej compress(char *from, Desbuf to)
1069 1.1 thorpej {
1070 1.1 thorpej register int i, j; /* counters in for loop */
1071 1.1 thorpej
1072 1.1 thorpej for (i = 0; i < 8; i++) {
1073 1.1 thorpej CHAR(to, i) = 0;
1074 1.1 thorpej for (j = 0; j < 8; j++)
1075 1.1 thorpej CHAR(to, i) = ((*from++)<<(7-j))|CHAR(to, i);
1076 1.1 thorpej }
1077 1.1 thorpej }
1078 1.1 thorpej #endif
1079 1.1 thorpej
1080 1.1 thorpej /*
1081 1.1 thorpej * message about usage
1082 1.1 thorpej */
1083 1.2 thorpej void
1084 1.2 thorpej usage(void)
1085 1.1 thorpej {
1086 1.2 thorpej extern const char *__progname;
1087 1.2 thorpej
1088 1.2 thorpej (void) fprintf(stderr, "usage: %s %s\n", __progname,
1089 1.2 thorpej "[-abdp] [-F bit] [-f bit] [-k key] [-m bit] [-o bit] [-v vector]");
1090 1.1 thorpej exit(1);
1091 1.1 thorpej }
1092