2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Matt Bishop of Dartmouth College.
8 * The United States Government has rights in this work pursuant
9 * to contract no. NAG 2-680 between the National Aeronautics and
10 * Space Administration and Dartmouth College.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * @(#) Copyright (c) 1991, 1993 The Regents of the University of California. All rights reserved.
41 * @(#)bdes.c 8.1 (Berkeley) 6/6/93
42 * $FreeBSD: src/secure/usr.bin/bdes/bdes.c,v 1.3.2.1 2000/09/22 09:42:03 kris Exp $
43 * $DragonFly: src/secure/usr.bin/bdes/bdes.c,v 1.3 2005/03/09 02:53:03 drhodus Exp $
47 * BDES -- DES encryption package for Berkeley Software Distribution 4.4
50 * -b use ECB (electronic code book) mode
51 * -d invert (decrypt) input
52 * -f b use b-bit CFB (cipher feedback) mode
53 * -F b use b-bit CFB (cipher feedback) alternative mode
54 * -k key use key as the cryptographic key
55 * -m b generate a MAC of length b
56 * -o b use b-bit OFB (output feedback) mode
57 * -p don't reset the parity bit
58 * -v v use v as the initialization vector (ignored for ECB)
59 * note: the last character of the last block is the integer indicating
60 * how many characters of that block are to be output
63 * Department of Mathematics and Computer Science
66 * Email: Matt.Bishop@dartmouth.edu
67 * ...!decvax!dartvax!Matt.Bishop
69 * See Technical Report PCS-TR91-158, Department of Mathematics and Computer
70 * Science, Dartmouth College, for a detailed description of the implemen-
71 * tation and differences between it and Sun's. The DES is described in
72 * FIPS PUB 46, and the modes in FIPS PUB 81 (see either the manual page
73 * or the technical report for a complete reference).
84 * BSD and System V systems offer special library calls that do
85 * block moves and fills, so if possible we take advantage of them
87 #define MEMCPY(dest,src,len) bcopy((src),(dest),(len))
88 #define MEMZERO(dest,len) bzero((dest),(len))
90 /* Hide the calls to the primitive encryption routines. */
93 #define DES_KEY(buf) \
94 if (des_setkey(buf)) \
96 #define DES_XFORM(buf) \
97 if (des_cipher(buf, buf, 0L, (inverse ? -1 : 1))) \
100 #define DES_KEY(buf) { \
101 char bits1[64]; /* bits of key */ \
102 expand(buf, bits1); \
106 #define DES_XFORM(buf) { \
107 char bits1[64]; /* bits of message */ \
108 expand(buf, bits1); \
109 if (encrypt(bits1, inverse)) \
111 compress(bits1, buf); \
116 * this does an error-checking write
118 #define READ(buf, n) fread(buf, sizeof(char), n, stdin)
119 #define WRITE(buf,n) \
120 if (fwrite(buf, sizeof(char), n, stdout) != n) \
124 * some things to make references easier
126 typedef char Desbuf
[8];
127 #define CHAR(x,i) (x[i])
128 #define UCHAR(x,i) (x[i])
129 #define BUFFER(x) (x)
130 #define UBUFFER(x) (x)
133 * global variables and related macros
135 #define KEY_DEFAULT 0 /* interpret radix of key from key */
136 #define KEY_ASCII 1 /* key is in ASCII characters */
137 int keybase
= KEY_DEFAULT
; /* how to interpret the key */
139 enum { /* encrypt, decrypt, authenticate */
140 MODE_ENCRYPT
, MODE_DECRYPT
, MODE_AUTHENTICATE
141 } mode
= MODE_ENCRYPT
;
142 enum { /* ecb, cbc, cfb, cfba, ofb? */
143 ALG_ECB
, ALG_CBC
, ALG_CFB
, ALG_OFB
, ALG_CFBA
146 Desbuf ivec
; /* initialization vector */
147 char bits
[] = { /* used to extract bits from a char */
148 '\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
150 int inverse
; /* 0 to encrypt, 1 to decrypt */
151 int macbits
= -1; /* number of bits in authentication */
152 int fbbits
= -1; /* number of feedback bits */
153 int pflag
; /* 1 to preserve parity bits */
155 main(int argc
, char **argv
)
157 extern int optind
; /* option (argument) number */
158 extern char *optarg
; /* argument to option if any */
159 register int i
; /* counter in a for loop */
160 register char *p
; /* used to obtain the key */
161 Desbuf msgbuf
; /* I/O buffer */
162 int kflag
; /* command-line encryptiooon key */
164 setproctitle("-"); /* Hide command-line arguments */
166 /* initialize the initialization vctor */
169 /* process the argument list */
171 while ((i
= getopt(argc
, argv
, "abdF:f:k:m:o:pv:")) != EOF
)
173 case 'a': /* key is ASCII */
176 case 'b': /* use ECB mode */
179 case 'd': /* decrypt */
182 case 'F': /* use alternative CFB mode */
184 if ((fbbits
= setbits(optarg
, 7)) > 56 || fbbits
== 0)
185 err(-1, "-F: number must be 1-56 inclusive");
186 else if (fbbits
== -1)
187 err(-1, "-F: number must be a multiple of 7");
189 case 'f': /* use CFB mode */
191 if ((fbbits
= setbits(optarg
, 8)) > 64 || fbbits
== 0)
192 err(-1, "-f: number must be 1-64 inclusive");
193 else if (fbbits
== -1)
194 err(-1, "-f: number must be a multiple of 8");
196 case 'k': /* encryption key */
198 cvtkey(BUFFER(msgbuf
), optarg
);
200 case 'm': /* number of bits for MACing */
201 mode
= MODE_AUTHENTICATE
;
202 if ((macbits
= setbits(optarg
, 1)) > 64)
203 err(-1, "-m: number must be 0-64 inclusive");
205 case 'o': /* use OFB mode */
207 if ((fbbits
= setbits(optarg
, 8)) > 64 || fbbits
== 0)
208 err(-1, "-o: number must be 1-64 inclusive");
209 else if (fbbits
== -1)
210 err(-1, "-o: number must be a multiple of 8");
212 case 'p': /* preserve parity bits */
215 case 'v': /* set initialization vector */
216 cvtkey(BUFFER(ivec
), optarg
);
224 * if the key's not ASCII, assume it is
230 p
= getpass("Enter key: ");
232 * copy it, nul-padded, into the key area
234 cvtkey(BUFFER(msgbuf
), p
);
238 inverse
= (alg
== ALG_CBC
|| alg
== ALG_ECB
) && mode
== MODE_DECRYPT
;
243 case MODE_AUTHENTICATE
: /* authenticate using CBC mode */
246 case MODE_DECRYPT
: /* decrypt using CBC mode */
249 case MODE_ENCRYPT
: /* encrypt using CBC mode */
256 case MODE_AUTHENTICATE
: /* authenticate using CFB mode */
259 case MODE_DECRYPT
: /* decrypt using CFB mode */
262 case MODE_ENCRYPT
: /* encrypt using CFB mode */
269 case MODE_AUTHENTICATE
: /* authenticate using CFBA mode */
270 err(-1, "can't authenticate with CFBA mode");
272 case MODE_DECRYPT
: /* decrypt using CFBA mode */
275 case MODE_ENCRYPT
: /* encrypt using CFBA mode */
282 case MODE_AUTHENTICATE
: /* authenticate using ECB mode */
283 err(-1, "can't authenticate with ECB mode");
285 case MODE_DECRYPT
: /* decrypt using ECB mode */
288 case MODE_ENCRYPT
: /* encrypt using ECB mode */
295 case MODE_AUTHENTICATE
: /* authenticate using OFB mode */
296 err(-1, "can't authenticate with OFB mode");
298 case MODE_DECRYPT
: /* decrypt using OFB mode */
301 case MODE_ENCRYPT
: /* encrypt using OFB mode */
311 * print a warning message and, possibly, terminate
314 int n
; /* offending block number */
315 char *s
; /* the message */
318 (void)fprintf(stderr
, "bdes (block %d): ", n
);
320 (void)fprintf(stderr
, "bdes: ");
321 (void)fprintf(stderr
, "%s\n", s
? s
: strerror(errno
));
326 * map a hex character to an integer
329 char c
; /* char to be converted */
330 int radix
; /* base (2 to 16) */
333 case '0': return(0x0);
334 case '1': return(0x1);
335 case '2': return(radix
> 2 ? 0x2 : -1);
336 case '3': return(radix
> 3 ? 0x3 : -1);
337 case '4': return(radix
> 4 ? 0x4 : -1);
338 case '5': return(radix
> 5 ? 0x5 : -1);
339 case '6': return(radix
> 6 ? 0x6 : -1);
340 case '7': return(radix
> 7 ? 0x7 : -1);
341 case '8': return(radix
> 8 ? 0x8 : -1);
342 case '9': return(radix
> 9 ? 0x9 : -1);
343 case 'A': case 'a': return(radix
> 10 ? 0xa : -1);
344 case 'B': case 'b': return(radix
> 11 ? 0xb : -1);
345 case 'C': case 'c': return(radix
> 12 ? 0xc : -1);
346 case 'D': case 'd': return(radix
> 13 ? 0xd : -1);
347 case 'E': case 'e': return(radix
> 14 ? 0xe : -1);
348 case 'F': case 'f': return(radix
> 15 ? 0xf : -1);
357 * convert the key to a bit pattern
360 char *obuf
; /* bit pattern */
361 char *ibuf
; /* the key itself */
363 register int i
, j
; /* counter in a for loop */
364 int nbuf
[64]; /* used for hex/key translation */
367 * just switch on the key base
370 case KEY_ASCII
: /* ascii to integer */
371 (void)strncpy(obuf
, ibuf
, 8);
373 case KEY_DEFAULT
: /* tell from context */
375 * leading '0x' or '0X' == hex key
377 if (ibuf
[0] == '0' && (ibuf
[1] == 'x' || ibuf
[1] == 'X')) {
380 * now translate it, bombing on any illegal hex digit
382 for (i
= 0; ibuf
[i
] && i
< 16; i
++)
383 if ((nbuf
[i
] = tobinhex(ibuf
[i
], 16)) == -1)
384 err(-1, "bad hex digit in key");
387 for (i
= 0; i
< 8; i
++)
389 ((nbuf
[2*i
]&0xf)<<4) | (nbuf
[2*i
+1]&0xf);
390 /* preserve parity bits */
395 * leading '0b' or '0B' == binary key
397 if (ibuf
[0] == '0' && (ibuf
[1] == 'b' || ibuf
[1] == 'B')) {
400 * now translate it, bombing on any illegal binary digit
402 for (i
= 0; ibuf
[i
] && i
< 16; i
++)
403 if ((nbuf
[i
] = tobinhex(ibuf
[i
], 2)) == -1)
404 err(-1, "bad binary digit in key");
407 for (i
= 0; i
< 8; i
++)
408 for (j
= 0; j
< 8; j
++)
409 obuf
[i
] = (obuf
[i
]<<1)|nbuf
[8*i
+j
];
410 /* preserve parity bits */
415 * no special leader -- ASCII
417 (void)strncpy(obuf
, ibuf
, 8);
422 * convert an ASCII string into a decimal number:
423 * 1. must be between 0 and 64 inclusive
424 * 2. must be a valid decimal number
425 * 3. must be a multiple of mult
428 char *s
; /* the ASCII string */
429 int mult
; /* what it must be a multiple of */
431 register char *p
; /* pointer in a for loop */
432 register int n
= 0; /* the integer collected */
442 for (p
= s
; *p
; p
++) {
444 n
= n
* 10 + *p
- '0';
446 err(-1, "bad decimal digit in MAC length");
450 * be sure it's a multiple of mult
452 return((n
% mult
!= 0) ? -1 : n
);
459 * This sets the DES key and (if you're using the deszip version)
460 * the direction of the transformation. This uses the Sun
461 * to map the 64-bit key onto the 56 bits that the key schedule
462 * generation routines use: the old way, which just uses the user-
463 * supplied 64 bits as is, and the new way, which resets the parity
464 * bit to be the same as the low-order bit in each character. The
465 * new way generates a greater variety of key schedules, since many
466 * systems set the parity (high) bit of each character to 0, and the
467 * DES ignores the low order bit of each character.
470 Desbuf buf
; /* key block */
472 register int i
, j
; /* counter in a for loop */
473 register int par
; /* parity counter */
476 * if the parity is not preserved, flip it
479 for (i
= 0; i
< 8; i
++) {
481 for (j
= 1; j
< 8; j
++)
482 if ((bits
[j
]&UCHAR(buf
, i
)) != 0)
485 UCHAR(buf
, i
) = UCHAR(buf
, i
)&0177;
487 UCHAR(buf
, i
) = (UCHAR(buf
, i
)&0177)|0200;
491 DES_KEY(UBUFFER(buf
));
495 * This encrypts using the Electronic Code Book mode of DES
499 register int n
; /* number of bytes actually read */
500 register int bn
; /* block number */
501 Desbuf msgbuf
; /* I/O buffer */
503 for (bn
= 0; (n
= READ(BUFFER(msgbuf
), 8)) == 8; bn
++) {
505 * do the transformation
507 DES_XFORM(UBUFFER(msgbuf
));
508 WRITE(BUFFER(msgbuf
), 8);
511 * at EOF or last block -- in either case, the last byte contains
512 * the character representation of the number of bytes in it
515 MEMZERO(&CHAR(msgbuf
, n
), 8 - n
);
517 DES_XFORM(UBUFFER(msgbuf
));
518 WRITE(BUFFER(msgbuf
), 8);
523 * This decrypts using the Electronic Code Book mode of DES
527 register int n
; /* number of bytes actually read */
528 register int c
; /* used to test for EOF */
529 register int bn
; /* block number */
530 Desbuf msgbuf
; /* I/O buffer */
532 for (bn
= 1; (n
= READ(BUFFER(msgbuf
), 8)) == 8; bn
++) {
534 * do the transformation
536 DES_XFORM(UBUFFER(msgbuf
));
538 * if the last one, handle it specially
540 if ((c
= getchar()) == EOF
) {
543 err(bn
, "decryption failed (block corrupted)");
546 (void)ungetc(c
, stdin
);
547 WRITE(BUFFER(msgbuf
), n
);
550 err(bn
, "decryption failed (incomplete block)");
554 * This encrypts using the Cipher Block Chaining mode of DES
558 register int n
; /* number of bytes actually read */
559 register int bn
; /* block number */
560 Desbuf msgbuf
; /* I/O buffer */
563 * do the transformation
565 for (bn
= 1; (n
= READ(BUFFER(msgbuf
), 8)) == 8; bn
++) {
566 for (n
= 0; n
< 8; n
++)
567 CHAR(msgbuf
, n
) ^= CHAR(ivec
, n
);
568 DES_XFORM(UBUFFER(msgbuf
));
569 MEMCPY(BUFFER(ivec
), BUFFER(msgbuf
), 8);
570 WRITE(BUFFER(msgbuf
), 8);
573 * at EOF or last block -- in either case, the last byte contains
574 * the character representation of the number of bytes in it
577 MEMZERO(&CHAR(msgbuf
, n
), 8 - n
);
579 for (n
= 0; n
< 8; n
++)
580 CHAR(msgbuf
, n
) ^= CHAR(ivec
, n
);
581 DES_XFORM(UBUFFER(msgbuf
));
582 WRITE(BUFFER(msgbuf
), 8);
587 * This decrypts using the Cipher Block Chaining mode of DES
591 register int n
; /* number of bytes actually read */
592 Desbuf msgbuf
; /* I/O buffer */
593 Desbuf ibuf
; /* temp buffer for initialization vector */
594 register int c
; /* used to test for EOF */
595 register int bn
; /* block number */
597 for (bn
= 0; (n
= READ(BUFFER(msgbuf
), 8)) == 8; bn
++) {
599 * do the transformation
601 MEMCPY(BUFFER(ibuf
), BUFFER(msgbuf
), 8);
602 DES_XFORM(UBUFFER(msgbuf
));
603 for (c
= 0; c
< 8; c
++)
604 UCHAR(msgbuf
, c
) ^= UCHAR(ivec
, c
);
605 MEMCPY(BUFFER(ivec
), BUFFER(ibuf
), 8);
607 * if the last one, handle it specially
609 if ((c
= getchar()) == EOF
) {
612 err(bn
, "decryption failed (block corrupted)");
615 (void)ungetc(c
, stdin
);
616 WRITE(BUFFER(msgbuf
), n
);
619 err(bn
, "decryption failed (incomplete block)");
623 * This authenticates using the Cipher Block Chaining mode of DES
627 register int n
, j
; /* number of bytes actually read */
628 Desbuf msgbuf
; /* I/O buffer */
629 Desbuf encbuf
; /* encryption buffer */
632 * do the transformation
633 * note we DISCARD the encrypted block;
634 * we only care about the last one
636 while ((n
= READ(BUFFER(msgbuf
), 8)) == 8) {
637 for (n
= 0; n
< 8; n
++)
638 CHAR(encbuf
, n
) = CHAR(msgbuf
, n
) ^ CHAR(ivec
, n
);
639 DES_XFORM(UBUFFER(encbuf
));
640 MEMCPY(BUFFER(ivec
), BUFFER(encbuf
), 8);
643 * now compute the last one, right padding with '\0' if need be
646 MEMZERO(&CHAR(msgbuf
, n
), 8 - n
);
647 for (n
= 0; n
< 8; n
++)
648 CHAR(encbuf
, n
) = CHAR(msgbuf
, n
) ^ CHAR(ivec
, n
);
649 DES_XFORM(UBUFFER(encbuf
));
653 * we write chars until fewer than 7 bits,
654 * and then pad the last one with 0 bits
656 for (n
= 0; macbits
> 7; n
++, macbits
-= 8)
657 (void)putchar(CHAR(encbuf
, n
));
659 CHAR(msgbuf
, 0) = 0x00;
660 for (j
= 0; j
< macbits
; j
++)
661 CHAR(msgbuf
, 0) |= (CHAR(encbuf
, n
)&bits
[j
]);
662 (void)putchar(CHAR(msgbuf
, 0));
667 * This encrypts using the Cipher FeedBack mode of DES
671 register int n
; /* number of bytes actually read */
672 register int nbytes
; /* number of bytes to read */
673 register int bn
; /* block number */
674 char ibuf
[8]; /* input buffer */
675 Desbuf msgbuf
; /* encryption buffer */
678 * do things in bytes, not bits
682 * do the transformation
684 for (bn
= 1; (n
= READ(ibuf
, nbytes
)) == nbytes
; bn
++) {
685 MEMCPY(BUFFER(msgbuf
), BUFFER(ivec
), 8);
686 DES_XFORM(UBUFFER(msgbuf
));
687 for (n
= 0; n
< 8 - nbytes
; n
++)
688 UCHAR(ivec
, n
) = UCHAR(ivec
, n
+nbytes
);
689 for (n
= 0; n
< nbytes
; n
++)
690 UCHAR(ivec
, 8-nbytes
+n
) = ibuf
[n
] ^ UCHAR(msgbuf
, n
);
691 WRITE(&CHAR(ivec
, 8-nbytes
), nbytes
);
694 * at EOF or last block -- in either case, the last byte contains
695 * the character representation of the number of bytes in it
698 MEMZERO(&ibuf
[n
], nbytes
- n
);
699 ibuf
[nbytes
- 1] = n
;
700 MEMCPY(BUFFER(msgbuf
), BUFFER(ivec
), 8);
701 DES_XFORM(UBUFFER(msgbuf
));
702 for (n
= 0; n
< nbytes
; n
++)
703 ibuf
[n
] ^= UCHAR(msgbuf
, n
);
708 * This decrypts using the Cipher Block Chaining mode of DES
712 register int n
; /* number of bytes actually read */
713 register int c
; /* used to test for EOF */
714 register int nbytes
; /* number of bytes to read */
715 register int bn
; /* block number */
716 char ibuf
[8]; /* input buffer */
717 char obuf
[8]; /* output buffer */
718 Desbuf msgbuf
; /* encryption buffer */
721 * do things in bytes, not bits
725 * do the transformation
727 for (bn
= 1; (n
= READ(ibuf
, nbytes
)) == nbytes
; bn
++) {
728 MEMCPY(BUFFER(msgbuf
), BUFFER(ivec
), 8);
729 DES_XFORM(UBUFFER(msgbuf
));
730 for (c
= 0; c
< 8 - nbytes
; c
++)
731 CHAR(ivec
, c
) = CHAR(ivec
, c
+nbytes
);
732 for (c
= 0; c
< nbytes
; c
++) {
733 CHAR(ivec
, 8-nbytes
+c
) = ibuf
[c
];
734 obuf
[c
] = ibuf
[c
] ^ UCHAR(msgbuf
, c
);
737 * if the last one, handle it specially
739 if ((c
= getchar()) == EOF
) {
741 if (n
< 0 || n
> nbytes
-1)
742 err(bn
, "decryption failed (block corrupted)");
745 (void)ungetc(c
, stdin
);
749 err(bn
, "decryption failed (incomplete block)");
753 * This encrypts using the alternative Cipher FeedBack mode of DES
757 register int n
; /* number of bytes actually read */
758 register int nbytes
; /* number of bytes to read */
759 register int bn
; /* block number */
760 char ibuf
[8]; /* input buffer */
761 char obuf
[8]; /* output buffer */
762 Desbuf msgbuf
; /* encryption buffer */
765 * do things in bytes, not bits
769 * do the transformation
771 for (bn
= 1; (n
= READ(ibuf
, nbytes
)) == nbytes
; bn
++) {
772 MEMCPY(BUFFER(msgbuf
), BUFFER(ivec
), 8);
773 DES_XFORM(UBUFFER(msgbuf
));
774 for (n
= 0; n
< 8 - nbytes
; n
++)
775 UCHAR(ivec
, n
) = UCHAR(ivec
, n
+nbytes
);
776 for (n
= 0; n
< nbytes
; n
++)
777 UCHAR(ivec
, 8-nbytes
+n
) = (ibuf
[n
] ^ UCHAR(msgbuf
, n
))
779 for (n
= 0; n
< nbytes
; n
++)
780 obuf
[n
] = CHAR(ivec
, 8-nbytes
+n
)&0177;
784 * at EOF or last block -- in either case, the last byte contains
785 * the character representation of the number of bytes in it
788 MEMZERO(&ibuf
[n
], nbytes
- n
);
789 ibuf
[nbytes
- 1] = ('0' + n
)|0200;
790 MEMCPY(BUFFER(msgbuf
), BUFFER(ivec
), 8);
791 DES_XFORM(UBUFFER(msgbuf
));
792 for (n
= 0; n
< nbytes
; n
++)
793 ibuf
[n
] ^= UCHAR(msgbuf
, n
);
798 * This decrypts using the alternative Cipher Block Chaining mode of DES
802 register int n
; /* number of bytes actually read */
803 register int c
; /* used to test for EOF */
804 register int nbytes
; /* number of bytes to read */
805 register int bn
; /* block number */
806 char ibuf
[8]; /* input buffer */
807 char obuf
[8]; /* output buffer */
808 Desbuf msgbuf
; /* encryption buffer */
811 * do things in bytes, not bits
815 * do the transformation
817 for (bn
= 1; (n
= READ(ibuf
, nbytes
)) == nbytes
; bn
++) {
818 MEMCPY(BUFFER(msgbuf
), BUFFER(ivec
), 8);
819 DES_XFORM(UBUFFER(msgbuf
));
820 for (c
= 0; c
< 8 - nbytes
; c
++)
821 CHAR(ivec
, c
) = CHAR(ivec
, c
+nbytes
);
822 for (c
= 0; c
< nbytes
; c
++) {
823 CHAR(ivec
, 8-nbytes
+c
) = ibuf
[c
]|0200;
824 obuf
[c
] = (ibuf
[c
] ^ UCHAR(msgbuf
, c
))&0177;
827 * if the last one, handle it specially
829 if ((c
= getchar()) == EOF
) {
830 if ((n
= (obuf
[nbytes
-1] - '0')) < 0
832 err(bn
, "decryption failed (block corrupted)");
835 (void)ungetc(c
, stdin
);
839 err(bn
, "decryption failed (incomplete block)");
844 * This encrypts using the Output FeedBack mode of DES
848 register int n
; /* number of bytes actually read */
849 register int c
; /* used to test for EOF */
850 register int nbytes
; /* number of bytes to read */
851 register int bn
; /* block number */
852 char ibuf
[8]; /* input buffer */
853 char obuf
[8]; /* output buffer */
854 Desbuf msgbuf
; /* encryption buffer */
857 * do things in bytes, not bits
861 * do the transformation
863 for (bn
= 1; (n
= READ(ibuf
, nbytes
)) == nbytes
; bn
++) {
864 MEMCPY(BUFFER(msgbuf
), BUFFER(ivec
), 8);
865 DES_XFORM(UBUFFER(msgbuf
));
866 for (n
= 0; n
< 8 - nbytes
; n
++)
867 UCHAR(ivec
, n
) = UCHAR(ivec
, n
+nbytes
);
868 for (n
= 0; n
< nbytes
; n
++) {
869 UCHAR(ivec
, 8-nbytes
+n
) = UCHAR(msgbuf
, n
);
870 obuf
[n
] = ibuf
[n
] ^ UCHAR(msgbuf
, n
);
875 * at EOF or last block -- in either case, the last byte contains
876 * the character representation of the number of bytes in it
879 MEMZERO(&ibuf
[n
], nbytes
- n
);
880 ibuf
[nbytes
- 1] = n
;
881 MEMCPY(BUFFER(msgbuf
), BUFFER(ivec
), 8);
882 DES_XFORM(UBUFFER(msgbuf
));
883 for (c
= 0; c
< nbytes
; c
++)
884 ibuf
[c
] ^= UCHAR(msgbuf
, c
);
889 * This decrypts using the Output Block Chaining mode of DES
893 register int n
; /* number of bytes actually read */
894 register int c
; /* used to test for EOF */
895 register int nbytes
; /* number of bytes to read */
896 register int bn
; /* block number */
897 char ibuf
[8]; /* input buffer */
898 char obuf
[8]; /* output buffer */
899 Desbuf msgbuf
; /* encryption buffer */
902 * do things in bytes, not bits
906 * do the transformation
908 for (bn
= 1; (n
= READ(ibuf
, nbytes
)) == nbytes
; bn
++) {
909 MEMCPY(BUFFER(msgbuf
), BUFFER(ivec
), 8);
910 DES_XFORM(UBUFFER(msgbuf
));
911 for (c
= 0; c
< 8 - nbytes
; c
++)
912 CHAR(ivec
, c
) = CHAR(ivec
, c
+nbytes
);
913 for (c
= 0; c
< nbytes
; c
++) {
914 CHAR(ivec
, 8-nbytes
+c
) = UCHAR(msgbuf
, c
);
915 obuf
[c
] = ibuf
[c
] ^ UCHAR(msgbuf
, c
);
918 * if the last one, handle it specially
920 if ((c
= getchar()) == EOF
) {
922 if (n
< 0 || n
> nbytes
-1)
923 err(bn
, "decryption failed (block corrupted)");
926 (void)ungetc(c
, stdin
);
933 err(bn
, "decryption failed (incomplete block)");
937 * This authenticates using the Cipher FeedBack mode of DES
941 register int n
, j
; /* number of bytes actually read */
942 register int nbytes
; /* number of bytes to read */
943 char ibuf
[8]; /* input buffer */
944 Desbuf msgbuf
; /* encryption buffer */
947 * do things in bytes, not bits
951 * do the transformation
953 while ((n
= READ(ibuf
, nbytes
)) == nbytes
) {
954 MEMCPY(BUFFER(msgbuf
), BUFFER(ivec
), 8);
955 DES_XFORM(UBUFFER(msgbuf
));
956 for (n
= 0; n
< 8 - nbytes
; n
++)
957 UCHAR(ivec
, n
) = UCHAR(ivec
, n
+nbytes
);
958 for (n
= 0; n
< nbytes
; n
++)
959 UCHAR(ivec
, 8-nbytes
+n
) = ibuf
[n
] ^ UCHAR(msgbuf
, n
);
962 * at EOF or last block -- in either case, the last byte contains
963 * the character representation of the number of bytes in it
965 MEMZERO(&ibuf
[n
], nbytes
- n
);
966 ibuf
[nbytes
- 1] = '0' + n
;
967 MEMCPY(BUFFER(msgbuf
), BUFFER(ivec
), 8);
968 DES_XFORM(UBUFFER(msgbuf
));
969 for (n
= 0; n
< nbytes
; n
++)
970 ibuf
[n
] ^= UCHAR(msgbuf
, n
);
973 * we write chars until fewer than 7 bits,
974 * and then pad the last one with 0 bits
976 for (n
= 0; macbits
> 7; n
++, macbits
-= 8)
977 (void)putchar(CHAR(msgbuf
, n
));
979 CHAR(msgbuf
, 0) = 0x00;
980 for (j
= 0; j
< macbits
; j
++)
981 CHAR(msgbuf
, 0) |= (CHAR(msgbuf
, n
)&bits
[j
]);
982 (void)putchar(CHAR(msgbuf
, 0));
988 * change from 8 bits/Uchar to 1 bit/Uchar
991 Desbuf from
; /* 8bit/unsigned char string */
992 char *to
; /* 1bit/char string */
994 register int i
, j
; /* counters in for loop */
996 for (i
= 0; i
< 8; i
++)
997 for (j
= 0; j
< 8; j
++)
998 *to
++ = (CHAR(from
, i
)>>(7-j
))&01;
1002 * change from 1 bit/char to 8 bits/Uchar
1005 char *from
; /* 1bit/char string */
1006 Desbuf to
; /* 8bit/unsigned char string */
1008 register int i
, j
; /* counters in for loop */
1010 for (i
= 0; i
< 8; i
++) {
1012 for (j
= 0; j
< 8; j
++)
1013 CHAR(to
, i
) = ((*from
++)<<(7-j
))|CHAR(to
, i
);
1019 * message about usage
1023 (void)fprintf(stderr
, "%s\n",
1024 "usage: bdes [-abdp] [-F bit] [-f bit] [-k key] [-m bit] [-o bit] [-v vector]");