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
42 static const char copyright
[] =
43 "@(#) Copyright (c) 1991, 1993\n\
44 The Regents of the University of California. All rights reserved.\n";
49 static char sccsid
[] = "@(#)bdes.c 8.1 (Berkeley) 6/6/93";
54 * BDES -- DES encryption package for Berkeley Software Distribution 4.4
57 * -b use ECB (electronic code book) mode
58 * -d invert (decrypt) input
59 * -f b use b-bit CFB (cipher feedback) mode
60 * -F b use b-bit CFB (cipher feedback) alternative mode
61 * -k key use key as the cryptographic key
62 * -m b generate a MAC of length b
63 * -o b use b-bit OFB (output feedback) mode
64 * -p don't reset the parity bit
65 * -v v use v as the initialization vector (ignored for ECB)
66 * note: the last character of the last block is the integer indicating
67 * how many characters of that block are to be output
70 * Department of Mathematics and Computer Science
73 * Email: Matt.Bishop@dartmouth.edu
74 * ...!decvax!dartvax!Matt.Bishop
76 * See Technical Report PCS-TR91-158, Department of Mathematics and Computer
77 * Science, Dartmouth College, for a detailed description of the implemen-
78 * tation and differences between it and Sun's. The DES is described in
79 * FIPS PUB 46, and the modes in FIPS PUB 81 (see either the manual page
80 * or the technical report for a complete reference).
83 #include <sys/cdefs.h>
84 __FBSDID("$FreeBSD$");
86 #include <sys/types.h>
96 #include <openssl/des.h>
99 * BSD and System V systems offer special library calls that do
100 * block moves and fills, so if possible we take advantage of them
102 #define MEMCPY(dest,src,len) bcopy((src),(dest),(len))
103 #define MEMZERO(dest,len) bzero((dest),(len))
105 #define DES_XFORM(buf) \
106 DES_ecb_encrypt(buf, buf, &schedule, \
107 mode == MODE_ENCRYPT ? DES_ENCRYPT : DES_DECRYPT);
110 * this does an error-checking write
112 #define READ(buf, n) fread(buf, sizeof(char), n, stdin)
113 #define WRITE(buf,n) \
114 if (fwrite(buf, sizeof(char), n, stdout) != n) \
115 warnx("fwrite error at %d", n);
118 * global variables and related macros
120 #define KEY_DEFAULT 0 /* interpret radix of key from key */
121 #define KEY_ASCII 1 /* key is in ASCII characters */
122 int keybase
= KEY_DEFAULT
; /* how to interpret the key */
124 enum { /* encrypt, decrypt, authenticate */
125 MODE_ENCRYPT
, MODE_DECRYPT
, MODE_AUTHENTICATE
126 } mode
= MODE_ENCRYPT
;
128 enum { /* ecb, cbc, cfb, cfba, ofb? */
129 ALG_ECB
, ALG_CBC
, ALG_CFB
, ALG_OFB
, ALG_CFBA
132 DES_cblock ivec
; /* initialization vector */
134 char bits
[] = { /* used to extract bits from a char */
135 '\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
138 int inverse
; /* 0 to encrypt, 1 to decrypt */
139 int macbits
= -1; /* number of bits in authentication */
140 int fbbits
= -1; /* number of feedback bits */
141 int pflag
; /* 1 to preserve parity bits */
143 DES_key_schedule schedule
; /* expanded DES key */
145 static void ecbenc(void);
146 static void ecbdec(void);
147 static void cbcenc(void);
148 static void cbcdec(void);
149 static void cfbenc(void);
150 static void cfbdec(void);
151 static void cfbaenc(void);
152 static void cfbadec(void);
153 static void ofbenc(void);
154 static void ofbdec(void);
156 static void cbcauth(void);
157 static void cfbauth(void);
159 static void cvtkey(DES_cblock
, char *);
160 static int setbits(char *, int);
161 static void makekey(DES_cblock
*);
162 static int tobinhex(char, int);
164 static void usage(void);
167 main(int argc
, char *argv
[])
169 extern char *optarg
; /* argument to option if any */
170 int i
; /* counter in a for loop */
171 char *p
; /* used to obtain the key */
172 DES_cblock msgbuf
; /* I/O buffer */
173 int kflag
; /* command-line encryptiooon key */
175 setproctitle("-"); /* Hide command-line arguments */
177 /* initialize the initialization vctor */
180 /* process the argument list */
182 while ((i
= getopt(argc
, argv
, "abdF:f:k:m:o:pv:")) != -1)
184 case 'a': /* key is ASCII */
187 case 'b': /* use ECB mode */
190 case 'd': /* decrypt */
193 case 'F': /* use alternative CFB mode */
195 if ((fbbits
= setbits(optarg
, 7)) > 56 || fbbits
== 0)
196 errx(1, "-F: number must be 1-56 inclusive");
197 else if (fbbits
== -1)
198 errx(1, "-F: number must be a multiple of 7");
200 case 'f': /* use CFB mode */
202 if ((fbbits
= setbits(optarg
, 8)) > 64 || fbbits
== 0)
203 errx(1, "-f: number must be 1-64 inclusive");
204 else if (fbbits
== -1)
205 errx(1, "-f: number must be a multiple of 8");
207 case 'k': /* encryption key */
209 cvtkey(msgbuf
, optarg
);
211 case 'm': /* number of bits for MACing */
212 mode
= MODE_AUTHENTICATE
;
213 if ((macbits
= setbits(optarg
, 1)) > 64)
214 errx(1, "-m: number must be 0-64 inclusive");
216 case 'o': /* use OFB mode */
218 if ((fbbits
= setbits(optarg
, 8)) > 64 || fbbits
== 0)
219 errx(1, "-o: number must be 1-64 inclusive");
220 else if (fbbits
== -1)
221 errx(1, "-o: number must be a multiple of 8");
223 case 'p': /* preserve parity bits */
226 case 'v': /* set initialization vector */
227 cvtkey(ivec
, optarg
);
235 * if the key's not ASCII, assume it is
241 p
= getpass("Enter key: ");
243 * copy it, nul-padded, into the key area
249 inverse
= (alg
== ALG_CBC
|| alg
== ALG_ECB
) && mode
== MODE_DECRYPT
;
254 case MODE_AUTHENTICATE
: /* authenticate using CBC mode */
257 case MODE_DECRYPT
: /* decrypt using CBC mode */
260 case MODE_ENCRYPT
: /* encrypt using CBC mode */
267 case MODE_AUTHENTICATE
: /* authenticate using CFB mode */
270 case MODE_DECRYPT
: /* decrypt using CFB mode */
273 case MODE_ENCRYPT
: /* encrypt using CFB mode */
280 case MODE_AUTHENTICATE
: /* authenticate using CFBA mode */
281 errx(1, "can't authenticate with CFBA mode");
283 case MODE_DECRYPT
: /* decrypt using CFBA mode */
286 case MODE_ENCRYPT
: /* encrypt using CFBA mode */
293 case MODE_AUTHENTICATE
: /* authenticate using ECB mode */
294 errx(1, "can't authenticate with ECB mode");
296 case MODE_DECRYPT
: /* decrypt using ECB mode */
299 case MODE_ENCRYPT
: /* encrypt using ECB mode */
306 case MODE_AUTHENTICATE
: /* authenticate using OFB mode */
307 errx(1, "can't authenticate with OFB mode");
309 case MODE_DECRYPT
: /* decrypt using OFB mode */
312 case MODE_ENCRYPT
: /* encrypt using OFB mode */
322 * map a hex character to an integer
325 tobinhex(char c
, int radix
)
328 case '0': return(0x0);
329 case '1': return(0x1);
330 case '2': return(radix
> 2 ? 0x2 : -1);
331 case '3': return(radix
> 3 ? 0x3 : -1);
332 case '4': return(radix
> 4 ? 0x4 : -1);
333 case '5': return(radix
> 5 ? 0x5 : -1);
334 case '6': return(radix
> 6 ? 0x6 : -1);
335 case '7': return(radix
> 7 ? 0x7 : -1);
336 case '8': return(radix
> 8 ? 0x8 : -1);
337 case '9': return(radix
> 9 ? 0x9 : -1);
338 case 'A': case 'a': return(radix
> 10 ? 0xa : -1);
339 case 'B': case 'b': return(radix
> 11 ? 0xb : -1);
340 case 'C': case 'c': return(radix
> 12 ? 0xc : -1);
341 case 'D': case 'd': return(radix
> 13 ? 0xd : -1);
342 case 'E': case 'e': return(radix
> 14 ? 0xe : -1);
343 case 'F': case 'f': return(radix
> 15 ? 0xf : -1);
352 * convert the key to a bit pattern
355 cvtkey(DES_cblock obuf
, char *ibuf
)
357 int i
, j
; /* counter in a for loop */
358 int nbuf
[64]; /* used for hex/key translation */
361 * just switch on the key base
364 case KEY_ASCII
: /* ascii to integer */
365 (void)strncpy(obuf
, ibuf
, 8);
367 case KEY_DEFAULT
: /* tell from context */
369 * leading '0x' or '0X' == hex key
371 if (ibuf
[0] == '0' && (ibuf
[1] == 'x' || ibuf
[1] == 'X')) {
374 * now translate it, bombing on any illegal hex digit
376 for (i
= 0; ibuf
[i
] && i
< 16; i
++)
377 if ((nbuf
[i
] = tobinhex(ibuf
[i
], 16)) == -1)
378 warnx("bad hex digit in key");
381 for (i
= 0; i
< 8; i
++)
383 ((nbuf
[2*i
]&0xf)<<4) | (nbuf
[2*i
+1]&0xf);
384 /* preserve parity bits */
389 * leading '0b' or '0B' == binary key
391 if (ibuf
[0] == '0' && (ibuf
[1] == 'b' || ibuf
[1] == 'B')) {
394 * now translate it, bombing on any illegal binary digit
396 for (i
= 0; ibuf
[i
] && i
< 16; i
++)
397 if ((nbuf
[i
] = tobinhex(ibuf
[i
], 2)) == -1)
398 warnx("bad binary digit in key");
401 for (i
= 0; i
< 8; i
++)
402 for (j
= 0; j
< 8; j
++)
403 obuf
[i
] = (obuf
[i
]<<1)|nbuf
[8*i
+j
];
404 /* preserve parity bits */
409 * no special leader -- ASCII
411 (void)strncpy(obuf
, ibuf
, 8);
416 * convert an ASCII string into a decimal number:
417 * 1. must be between 0 and 64 inclusive
418 * 2. must be a valid decimal number
419 * 3. must be a multiple of mult
422 setbits(char *s
, int mult
)
424 char *p
; /* pointer in a for loop */
425 int n
= 0; /* the integer collected */
435 for (p
= s
; *p
; p
++) {
437 n
= n
* 10 + *p
- '0';
439 warnx("bad decimal digit in MAC length");
443 * be sure it's a multiple of mult
445 return((n
% mult
!= 0) ? -1 : n
);
452 * This sets the DES key and (if you're using the deszip version)
453 * the direction of the transformation. This uses the Sun
454 * to map the 64-bit key onto the 56 bits that the key schedule
455 * generation routines use: the old way, which just uses the user-
456 * supplied 64 bits as is, and the new way, which resets the parity
457 * bit to be the same as the low-order bit in each character. The
458 * new way generates a greater variety of key schedules, since many
459 * systems set the parity (high) bit of each character to 0, and the
460 * DES ignores the low order bit of each character.
463 makekey(DES_cblock
*buf
)
465 int i
, j
; /* counter in a for loop */
466 int par
; /* parity counter */
469 * if the parity is not preserved, flip it
472 for (i
= 0; i
< 8; i
++) {
474 for (j
= 1; j
< 8; j
++)
475 if ((bits
[j
] & (*buf
)[i
]) != 0)
477 if ((par
& 0x01) == 0x01)
480 (*buf
)[i
] = ((*buf
)[i
] & 0x7f) | 0x80;
484 DES_set_odd_parity(buf
);
485 DES_set_key(buf
, &schedule
);
489 * This encrypts using the Electronic Code Book mode of DES
494 int n
; /* number of bytes actually read */
495 int bn
; /* block number */
496 DES_cblock msgbuf
; /* I/O buffer */
498 for (bn
= 0; (n
= READ(msgbuf
, 8)) == 8; bn
++) {
500 * do the transformation
506 * at EOF or last block -- in either case, the last byte contains
507 * the character representation of the number of bytes in it
510 MEMZERO(&msgbuf
[n
], 8 - n
);
518 * This decrypts using the Electronic Code Book mode of DES
523 int n
; /* number of bytes actually read */
524 int c
; /* used to test for EOF */
525 int bn
; /* block number */
526 DES_cblock msgbuf
; /* I/O buffer */
528 for (bn
= 1; (n
= READ(msgbuf
, 8)) == 8; bn
++) {
530 * do the transformation
534 * if the last one, handle it specially
536 if ((c
= getchar()) == EOF
) {
539 warnx("decryption failed (block corrupt) at %d",
543 (void)ungetc(c
, stdin
);
547 warnx("decryption failed (incomplete block) at %d", bn
);
551 * This encrypts using the Cipher Block Chaining mode of DES
556 int n
; /* number of bytes actually read */
557 int bn
; /* block number */
558 DES_cblock msgbuf
; /* I/O buffer */
561 * do the transformation
563 for (bn
= 1; (n
= READ(msgbuf
, 8)) == 8; bn
++) {
564 for (n
= 0; n
< 8; n
++)
565 msgbuf
[n
] ^= ivec
[n
];
567 MEMCPY(ivec
, msgbuf
, 8);
571 * at EOF or last block -- in either case, the last byte contains
572 * the character representation of the number of bytes in it
575 MEMZERO(&msgbuf
[n
], 8 - n
);
577 for (n
= 0; n
< 8; n
++)
578 msgbuf
[n
] ^= ivec
[n
];
585 * This decrypts using the Cipher Block Chaining mode of DES
590 int n
; /* number of bytes actually read */
591 DES_cblock msgbuf
; /* I/O buffer */
592 DES_cblock ibuf
; /* temp buffer for initialization vector */
593 int c
; /* used to test for EOF */
594 int bn
; /* block number */
596 for (bn
= 0; (n
= READ(msgbuf
, 8)) == 8; bn
++) {
598 * do the transformation
600 MEMCPY(ibuf
, msgbuf
, 8);
602 for (c
= 0; c
< 8; c
++)
603 msgbuf
[c
] ^= ivec
[c
];
604 MEMCPY(ivec
, ibuf
, 8);
606 * if the last one, handle it specially
608 if ((c
= getchar()) == EOF
) {
611 warnx("decryption failed (block corrupt) at %d",
615 (void)ungetc(c
, stdin
);
619 warnx("decryption failed (incomplete block) at %d", bn
);
623 * This authenticates using the Cipher Block Chaining mode of DES
628 int n
, j
; /* number of bytes actually read */
629 DES_cblock msgbuf
; /* I/O buffer */
630 DES_cblock encbuf
; /* encryption buffer */
633 * do the transformation
634 * note we DISCARD the encrypted block;
635 * we only care about the last one
637 while ((n
= READ(msgbuf
, 8)) == 8) {
638 for (n
= 0; n
< 8; n
++)
639 encbuf
[n
] = msgbuf
[n
] ^ ivec
[n
];
641 MEMCPY(ivec
, encbuf
, 8);
644 * now compute the last one, right padding with '\0' if need be
647 MEMZERO(&msgbuf
[n
], 8 - n
);
648 for (n
= 0; n
< 8; n
++)
649 encbuf
[n
] = msgbuf
[n
] ^ ivec
[n
];
654 * we write chars until fewer than 7 bits,
655 * and then pad the last one with 0 bits
657 for (n
= 0; macbits
> 7; n
++, macbits
-= 8)
658 (void)putchar(encbuf
[n
]);
661 for (j
= 0; j
< macbits
; j
++)
662 msgbuf
[0] |= encbuf
[n
] & bits
[j
];
663 (void)putchar(msgbuf
[0]);
668 * This encrypts using the Cipher FeedBack mode of DES
673 int n
; /* number of bytes actually read */
674 int nbytes
; /* number of bytes to read */
675 int bn
; /* block number */
676 char ibuf
[8]; /* input buffer */
677 DES_cblock msgbuf
; /* encryption buffer */
680 * do things in bytes, not bits
684 * do the transformation
686 for (bn
= 1; (n
= READ(ibuf
, nbytes
)) == nbytes
; bn
++) {
687 MEMCPY(msgbuf
, ivec
, 8);
689 for (n
= 0; n
< 8 - nbytes
; n
++)
690 ivec
[n
] = ivec
[n
+nbytes
];
691 for (n
= 0; n
< nbytes
; n
++)
692 ivec
[8 - nbytes
+ n
] = ibuf
[n
] ^ msgbuf
[n
];
693 WRITE(&ivec
[8 - nbytes
], nbytes
);
696 * at EOF or last block -- in either case, the last byte contains
697 * the character representation of the number of bytes in it
700 MEMZERO(&ibuf
[n
], nbytes
- n
);
701 ibuf
[nbytes
- 1] = n
;
702 MEMCPY(msgbuf
, ivec
, 8);
704 for (n
= 0; n
< nbytes
; n
++)
705 ibuf
[n
] ^= msgbuf
[n
];
710 * This decrypts using the Cipher Block Chaining mode of DES
715 int n
; /* number of bytes actually read */
716 int c
; /* used to test for EOF */
717 int nbytes
; /* number of bytes to read */
718 int bn
; /* block number */
719 char ibuf
[8]; /* input buffer */
720 char obuf
[8]; /* output buffer */
721 DES_cblock msgbuf
; /* encryption buffer */
724 * do things in bytes, not bits
728 * do the transformation
730 for (bn
= 1; (n
= READ(ibuf
, nbytes
)) == nbytes
; bn
++) {
731 MEMCPY(msgbuf
, ivec
, 8);
733 for (c
= 0; c
< 8 - nbytes
; c
++)
734 ivec
[c
] = ivec
[c
+ nbytes
];
735 for (c
= 0; c
< nbytes
; c
++) {
736 ivec
[8 - nbytes
+ c
] = ibuf
[c
];
737 obuf
[c
] = ibuf
[c
] ^ msgbuf
[c
];
740 * if the last one, handle it specially
742 if ((c
= getchar()) == EOF
) {
744 if (n
< 0 || n
> nbytes
-1)
745 warnx("decryption failed (block corrupt) at %d",
749 (void)ungetc(c
, stdin
);
753 warnx("decryption failed (incomplete block) at %d", bn
);
757 * This encrypts using the alternative Cipher FeedBack mode of DES
762 int n
; /* number of bytes actually read */
763 int nbytes
; /* number of bytes to read */
764 int bn
; /* block number */
765 char ibuf
[8]; /* input buffer */
766 char obuf
[8]; /* output buffer */
767 DES_cblock msgbuf
; /* encryption buffer */
770 * do things in bytes, not bits
774 * do the transformation
776 for (bn
= 1; (n
= READ(ibuf
, nbytes
)) == nbytes
; bn
++) {
777 MEMCPY(msgbuf
, ivec
, 8);
779 for (n
= 0; n
< 8 - nbytes
; n
++)
780 ivec
[n
] = ivec
[n
+ nbytes
];
781 for (n
= 0; n
< nbytes
; n
++)
782 ivec
[8 - nbytes
+ n
] = (ibuf
[n
] ^ msgbuf
[n
]) | 0x80;
783 for (n
= 0; n
< nbytes
; n
++)
784 obuf
[n
] = ivec
[8 - nbytes
+ n
] & 0x7f;
788 * at EOF or last block -- in either case, the last byte contains
789 * the character representation of the number of bytes in it
792 MEMZERO(&ibuf
[n
], nbytes
- n
);
793 ibuf
[nbytes
- 1] = ('0' + n
)|0200;
794 MEMCPY(msgbuf
, ivec
, 8);
796 for (n
= 0; n
< nbytes
; n
++)
797 ibuf
[n
] ^= msgbuf
[n
];
802 * This decrypts using the alternative Cipher Block Chaining mode of DES
807 int n
; /* number of bytes actually read */
808 int c
; /* used to test for EOF */
809 int nbytes
; /* number of bytes to read */
810 int bn
; /* block number */
811 char ibuf
[8]; /* input buffer */
812 char obuf
[8]; /* output buffer */
813 DES_cblock msgbuf
; /* encryption buffer */
816 * do things in bytes, not bits
820 * do the transformation
822 for (bn
= 1; (n
= READ(ibuf
, nbytes
)) == nbytes
; bn
++) {
823 MEMCPY(msgbuf
, ivec
, 8);
825 for (c
= 0; c
< 8 - nbytes
; c
++)
826 ivec
[c
] = ivec
[c
+ nbytes
];
827 for (c
= 0; c
< nbytes
; c
++) {
828 ivec
[8 - nbytes
+ c
] = ibuf
[c
] | 0x80;
829 obuf
[c
] = (ibuf
[c
] ^ msgbuf
[c
]) & 0x7f;
832 * if the last one, handle it specially
834 if ((c
= getchar()) == EOF
) {
835 if ((n
= (obuf
[nbytes
-1] - '0')) < 0
837 warnx("decryption failed (block corrupt) at %d",
841 (void)ungetc(c
, stdin
);
845 warnx("decryption failed (incomplete block) at %d", bn
);
850 * This encrypts using the Output FeedBack mode of DES
855 int n
; /* number of bytes actually read */
856 int c
; /* used to test for EOF */
857 int nbytes
; /* number of bytes to read */
858 int bn
; /* block number */
859 char ibuf
[8]; /* input buffer */
860 char obuf
[8]; /* output buffer */
861 DES_cblock msgbuf
; /* encryption buffer */
864 * do things in bytes, not bits
868 * do the transformation
870 for (bn
= 1; (n
= READ(ibuf
, nbytes
)) == nbytes
; bn
++) {
871 MEMCPY(msgbuf
, ivec
, 8);
873 for (n
= 0; n
< 8 - nbytes
; n
++)
874 ivec
[n
] = ivec
[n
+ nbytes
];
875 for (n
= 0; n
< nbytes
; n
++) {
876 ivec
[8 - nbytes
+ n
] = msgbuf
[n
];
877 obuf
[n
] = ibuf
[n
] ^ msgbuf
[n
];
882 * at EOF or last block -- in either case, the last byte contains
883 * the character representation of the number of bytes in it
886 MEMZERO(&ibuf
[n
], nbytes
- n
);
887 ibuf
[nbytes
- 1] = n
;
888 MEMCPY(msgbuf
, ivec
, 8);
890 for (c
= 0; c
< nbytes
; c
++)
891 ibuf
[c
] ^= msgbuf
[c
];
896 * This decrypts using the Output Block Chaining mode of DES
901 int n
; /* number of bytes actually read */
902 int c
; /* used to test for EOF */
903 int nbytes
; /* number of bytes to read */
904 int bn
; /* block number */
905 char ibuf
[8]; /* input buffer */
906 char obuf
[8]; /* output buffer */
907 DES_cblock msgbuf
; /* encryption buffer */
910 * do things in bytes, not bits
914 * do the transformation
916 for (bn
= 1; (n
= READ(ibuf
, nbytes
)) == nbytes
; bn
++) {
917 MEMCPY(msgbuf
, ivec
, 8);
919 for (c
= 0; c
< 8 - nbytes
; c
++)
920 ivec
[c
] = ivec
[c
+ nbytes
];
921 for (c
= 0; c
< nbytes
; c
++) {
922 ivec
[8 - nbytes
+ c
] = msgbuf
[c
];
923 obuf
[c
] = ibuf
[c
] ^ msgbuf
[c
];
926 * if the last one, handle it specially
928 if ((c
= getchar()) == EOF
) {
930 if (n
< 0 || n
> nbytes
-1)
931 warnx("decryption failed (block corrupt) at %d",
935 (void)ungetc(c
, stdin
);
942 warnx("decryption failed (incomplete block) at %d", bn
);
946 * This authenticates using the Cipher FeedBack mode of DES
951 int n
, j
; /* number of bytes actually read */
952 int nbytes
; /* number of bytes to read */
953 char ibuf
[8]; /* input buffer */
954 DES_cblock msgbuf
; /* encryption buffer */
957 * do things in bytes, not bits
961 * do the transformation
963 while ((n
= READ(ibuf
, nbytes
)) == nbytes
) {
964 MEMCPY(msgbuf
, ivec
, 8);
966 for (n
= 0; n
< 8 - nbytes
; n
++)
967 ivec
[n
] = ivec
[n
+ nbytes
];
968 for (n
= 0; n
< nbytes
; n
++)
969 ivec
[8 - nbytes
+ n
] = ibuf
[n
] ^ msgbuf
[n
];
972 * at EOF or last block -- in either case, the last byte contains
973 * the character representation of the number of bytes in it
975 MEMZERO(&ibuf
[n
], nbytes
- n
);
976 ibuf
[nbytes
- 1] = '0' + n
;
977 MEMCPY(msgbuf
, ivec
, 8);
979 for (n
= 0; n
< nbytes
; n
++)
980 ibuf
[n
] ^= msgbuf
[n
];
983 * we write chars until fewer than 7 bits,
984 * and then pad the last one with 0 bits
986 for (n
= 0; macbits
> 7; n
++, macbits
-= 8)
987 (void)putchar(msgbuf
[n
]);
990 for (j
= 0; j
< macbits
; j
++)
991 msgbuf
[0] |= msgbuf
[n
] & bits
[j
];
992 (void)putchar(msgbuf
[0]);
997 * message about usage
1002 (void)fprintf(stderr
, "%s\n",
1003 "usage: bdes [-abdp] [-F N] [-f N] [-k key] [-m N] [-o N] [-v vector]");