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.11 2009/11/03 18:40:42 jhb Exp $
46 * BDES -- DES encryption package for Berkeley Software Distribution 4.4
49 * -b use ECB (electronic code book) mode
50 * -d invert (decrypt) input
51 * -f b use b-bit CFB (cipher feedback) mode
52 * -F b use b-bit CFB (cipher feedback) alternative mode
53 * -k key use key as the cryptographic key
54 * -m b generate a MAC of length b
55 * -o b use b-bit OFB (output feedback) mode
56 * -p don't reset the parity bit
57 * -v v use v as the initialization vector (ignored for ECB)
58 * note: the last character of the last block is the integer indicating
59 * how many characters of that block are to be output
62 * Department of Mathematics and Computer Science
65 * Email: Matt.Bishop@dartmouth.edu
66 * ...!decvax!dartvax!Matt.Bishop
68 * See Technical Report PCS-TR91-158, Department of Mathematics and Computer
69 * Science, Dartmouth College, for a detailed description of the implemen-
70 * tation and differences between it and Sun's. The DES is described in
71 * FIPS PUB 46, and the modes in FIPS PUB 81 (see either the manual page
72 * or the technical report for a complete reference).
75 #include <sys/types.h>
85 #include <openssl/des.h>
88 * BSD and System V systems offer special library calls that do
89 * block moves and fills, so if possible we take advantage of them
91 #define MEMCPY(dest,src,len) bcopy((src),(dest),(len))
92 #define MEMZERO(dest,len) bzero((dest),(len))
94 #define DES_XFORM(buf) \
95 DES_ecb_encrypt(buf, buf, &schedule, \
96 mode == MODE_ENCRYPT ? DES_ENCRYPT : DES_DECRYPT);
99 * this does an error-checking write
101 #define READ(buf, n) fread(buf, sizeof(char), n, stdin)
102 #define WRITE(buf,n) \
103 if (fwrite(buf, sizeof(char), n, stdout) != n) \
104 warnx("fwrite error at %d", n);
107 * global variables and related macros
109 #define KEY_DEFAULT 0 /* interpret radix of key from key */
110 #define KEY_ASCII 1 /* key is in ASCII characters */
111 int keybase
= KEY_DEFAULT
; /* how to interpret the key */
113 enum { /* encrypt, decrypt, authenticate */
114 MODE_ENCRYPT
, MODE_DECRYPT
, MODE_AUTHENTICATE
115 } mode
= MODE_ENCRYPT
;
117 enum { /* ecb, cbc, cfb, cfba, ofb? */
118 ALG_ECB
, ALG_CBC
, ALG_CFB
, ALG_OFB
, ALG_CFBA
121 DES_cblock ivec
; /* initialization vector */
123 char bits
[] = { /* used to extract bits from a char */
124 '\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
127 int inverse
; /* 0 to encrypt, 1 to decrypt */
128 int macbits
= -1; /* number of bits in authentication */
129 int fbbits
= -1; /* number of feedback bits */
130 int pflag
; /* 1 to preserve parity bits */
132 DES_key_schedule schedule
; /* expanded DES key */
134 static void ecbenc(void);
135 static void ecbdec(void);
136 static void cbcenc(void);
137 static void cbcdec(void);
138 static void cfbenc(void);
139 static void cfbdec(void);
140 static void cfbaenc(void);
141 static void cfbadec(void);
142 static void ofbenc(void);
143 static void ofbdec(void);
145 static void cbcauth(void);
146 static void cfbauth(void);
148 static void cvtkey(DES_cblock
, char *);
149 static int setbits(char *, int);
150 static void makekey(DES_cblock
*);
151 static int tobinhex(char, int);
153 static void usage(void);
156 main(int argc
, char *argv
[])
158 extern char *optarg
; /* argument to option if any */
159 int i
; /* counter in a for loop */
160 char *p
; /* used to obtain the key */
161 DES_cblock msgbuf
; /* I/O buffer */
162 int kflag
; /* command-line encryption key */
164 setproctitle("-"); /* Hide command-line arguments */
166 /* initialize the initialization vector */
169 /* process the argument list */
171 while ((i
= getopt(argc
, argv
, "abdF:f:k:m:o:pv:")) != -1)
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 errx(1, "-F: number must be 1-56 inclusive");
186 else if (fbbits
== -1)
187 errx(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 errx(1, "-f: number must be 1-64 inclusive");
193 else if (fbbits
== -1)
194 errx(1, "-f: number must be a multiple of 8");
196 case 'k': /* encryption key */
198 cvtkey(msgbuf
, optarg
);
200 case 'm': /* number of bits for MACing */
201 mode
= MODE_AUTHENTICATE
;
202 if ((macbits
= setbits(optarg
, 1)) > 64)
203 errx(1, "-m: number must be 0-64 inclusive");
205 case 'o': /* use OFB mode */
207 if ((fbbits
= setbits(optarg
, 8)) > 64 || fbbits
== 0)
208 errx(1, "-o: number must be 1-64 inclusive");
209 else if (fbbits
== -1)
210 errx(1, "-o: number must be a multiple of 8");
212 case 'p': /* preserve parity bits */
215 case 'v': /* set initialization vector */
216 cvtkey(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
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 errx(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 errx(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 errx(1, "can't authenticate with OFB mode");
298 case MODE_DECRYPT
: /* decrypt using OFB mode */
301 case MODE_ENCRYPT
: /* encrypt using OFB mode */
311 * map a hex character to an integer
314 tobinhex(char c
, int radix
)
317 case '0': return(0x0);
318 case '1': return(0x1);
319 case '2': return(radix
> 2 ? 0x2 : -1);
320 case '3': return(radix
> 3 ? 0x3 : -1);
321 case '4': return(radix
> 4 ? 0x4 : -1);
322 case '5': return(radix
> 5 ? 0x5 : -1);
323 case '6': return(radix
> 6 ? 0x6 : -1);
324 case '7': return(radix
> 7 ? 0x7 : -1);
325 case '8': return(radix
> 8 ? 0x8 : -1);
326 case '9': return(radix
> 9 ? 0x9 : -1);
327 case 'A': case 'a': return(radix
> 10 ? 0xa : -1);
328 case 'B': case 'b': return(radix
> 11 ? 0xb : -1);
329 case 'C': case 'c': return(radix
> 12 ? 0xc : -1);
330 case 'D': case 'd': return(radix
> 13 ? 0xd : -1);
331 case 'E': case 'e': return(radix
> 14 ? 0xe : -1);
332 case 'F': case 'f': return(radix
> 15 ? 0xf : -1);
341 * convert the key to a bit pattern
344 cvtkey(DES_cblock obuf
, char *ibuf
)
346 int i
, j
; /* counter in a for loop */
347 int nbuf
[64]; /* used for hex/key translation */
350 * just switch on the key base
353 case KEY_ASCII
: /* ascii to integer */
354 (void)strncpy(obuf
, ibuf
, 8);
356 case KEY_DEFAULT
: /* tell from context */
358 * leading '0x' or '0X' == hex key
360 if (ibuf
[0] == '0' && (ibuf
[1] == 'x' || ibuf
[1] == 'X')) {
363 * now translate it, bombing on any illegal hex digit
365 for (i
= 0; i
< 16 && ibuf
[i
]; i
++)
366 if ((nbuf
[i
] = tobinhex(ibuf
[i
], 16)) == -1)
367 warnx("bad hex digit in key");
370 for (i
= 0; i
< 8; i
++)
372 ((nbuf
[2*i
]&0xf)<<4) | (nbuf
[2*i
+1]&0xf);
373 /* preserve parity bits */
378 * leading '0b' or '0B' == binary key
380 if (ibuf
[0] == '0' && (ibuf
[1] == 'b' || ibuf
[1] == 'B')) {
383 * now translate it, bombing on any illegal binary digit
385 for (i
= 0; i
< 16 && ibuf
[i
]; i
++)
386 if ((nbuf
[i
] = tobinhex(ibuf
[i
], 2)) == -1)
387 warnx("bad binary digit in key");
390 for (i
= 0; i
< 8; i
++)
391 for (j
= 0; j
< 8; j
++)
392 obuf
[i
] = (obuf
[i
]<<1)|nbuf
[8*i
+j
];
393 /* preserve parity bits */
398 * no special leader -- ASCII
400 (void)strncpy(obuf
, ibuf
, 8);
405 * convert an ASCII string into a decimal number:
406 * 1. must be between 0 and 64 inclusive
407 * 2. must be a valid decimal number
408 * 3. must be a multiple of mult
411 setbits(char *s
, int mult
)
413 char *p
; /* pointer in a for loop */
414 int n
= 0; /* the integer collected */
424 for (p
= s
; *p
; p
++) {
426 n
= n
* 10 + *p
- '0';
428 warnx("bad decimal digit in MAC length");
432 * be sure it's a multiple of mult
434 return((n
% mult
!= 0) ? -1 : n
);
441 * This sets the DES key and (if you're using the deszip version)
442 * the direction of the transformation. This uses the Sun
443 * to map the 64-bit key onto the 56 bits that the key schedule
444 * generation routines use: the old way, which just uses the user-
445 * supplied 64 bits as is, and the new way, which resets the parity
446 * bit to be the same as the low-order bit in each character. The
447 * new way generates a greater variety of key schedules, since many
448 * systems set the parity (high) bit of each character to 0, and the
449 * DES ignores the low order bit of each character.
452 makekey(DES_cblock
*buf
)
454 int i
, j
; /* counter in a for loop */
455 int par
; /* parity counter */
458 * if the parity is not preserved, flip it
461 for (i
= 0; i
< 8; i
++) {
463 for (j
= 1; j
< 8; j
++)
464 if ((bits
[j
] & (*buf
)[i
]) != 0)
466 if ((par
& 0x01) == 0x01)
469 (*buf
)[i
] = ((*buf
)[i
] & 0x7f) | 0x80;
473 DES_set_odd_parity(buf
);
474 DES_set_key(buf
, &schedule
);
478 * This encrypts using the Electronic Code Book mode of DES
483 int n
; /* number of bytes actually read */
484 int bn
; /* block number */
485 DES_cblock msgbuf
; /* I/O buffer */
487 for (bn
= 0; (n
= READ(msgbuf
, 8)) == 8; bn
++) {
489 * do the transformation
495 * at EOF or last block -- in either case, the last byte contains
496 * the character representation of the number of bytes in it
499 MEMZERO(&msgbuf
[n
], 8 - n
);
507 * This decrypts using the Electronic Code Book mode of DES
512 int n
; /* number of bytes actually read */
513 int c
; /* used to test for EOF */
514 int bn
; /* block number */
515 DES_cblock msgbuf
; /* I/O buffer */
517 for (bn
= 1; (n
= READ(msgbuf
, 8)) == 8; bn
++) {
519 * do the transformation
523 * if the last one, handle it specially
525 if ((c
= getchar()) == EOF
) {
528 warnx("decryption failed (block corrupt) at %d",
532 (void)ungetc(c
, stdin
);
536 warnx("decryption failed (incomplete block) at %d", bn
);
540 * This encrypts using the Cipher Block Chaining mode of DES
545 int n
; /* number of bytes actually read */
546 int bn
; /* block number */
547 DES_cblock msgbuf
; /* I/O buffer */
550 * do the transformation
552 for (bn
= 1; (n
= READ(msgbuf
, 8)) == 8; bn
++) {
553 for (n
= 0; n
< 8; n
++)
554 msgbuf
[n
] ^= ivec
[n
];
556 MEMCPY(ivec
, msgbuf
, 8);
560 * at EOF or last block -- in either case, the last byte contains
561 * the character representation of the number of bytes in it
564 MEMZERO(&msgbuf
[n
], 8 - n
);
566 for (n
= 0; n
< 8; n
++)
567 msgbuf
[n
] ^= ivec
[n
];
574 * This decrypts using the Cipher Block Chaining mode of DES
579 int n
; /* number of bytes actually read */
580 DES_cblock msgbuf
; /* I/O buffer */
581 DES_cblock ibuf
; /* temp buffer for initialization vector */
582 int c
; /* used to test for EOF */
583 int bn
; /* block number */
585 for (bn
= 0; (n
= READ(msgbuf
, 8)) == 8; bn
++) {
587 * do the transformation
589 MEMCPY(ibuf
, msgbuf
, 8);
591 for (c
= 0; c
< 8; c
++)
592 msgbuf
[c
] ^= ivec
[c
];
593 MEMCPY(ivec
, ibuf
, 8);
595 * if the last one, handle it specially
597 if ((c
= getchar()) == EOF
) {
600 warnx("decryption failed (block corrupt) at %d",
604 (void)ungetc(c
, stdin
);
608 warnx("decryption failed (incomplete block) at %d", bn
);
612 * This authenticates using the Cipher Block Chaining mode of DES
617 int n
, j
; /* number of bytes actually read */
618 DES_cblock msgbuf
; /* I/O buffer */
619 DES_cblock encbuf
; /* encryption buffer */
622 * do the transformation
623 * note we DISCARD the encrypted block;
624 * we only care about the last one
626 while ((n
= READ(msgbuf
, 8)) == 8) {
627 for (n
= 0; n
< 8; n
++)
628 encbuf
[n
] = msgbuf
[n
] ^ ivec
[n
];
630 MEMCPY(ivec
, encbuf
, 8);
633 * now compute the last one, right padding with '\0' if need be
636 MEMZERO(&msgbuf
[n
], 8 - n
);
637 for (n
= 0; n
< 8; n
++)
638 encbuf
[n
] = msgbuf
[n
] ^ ivec
[n
];
643 * we write chars until fewer than 7 bits,
644 * and then pad the last one with 0 bits
646 for (n
= 0; macbits
> 7; n
++, macbits
-= 8)
647 (void)putchar(encbuf
[n
]);
650 for (j
= 0; j
< macbits
; j
++)
651 msgbuf
[0] |= encbuf
[n
] & bits
[j
];
652 (void)putchar(msgbuf
[0]);
657 * This encrypts using the Cipher FeedBack mode of DES
662 int n
; /* number of bytes actually read */
663 int nbytes
; /* number of bytes to read */
664 int bn
; /* block number */
665 char ibuf
[8]; /* input buffer */
666 DES_cblock msgbuf
; /* encryption buffer */
669 * do things in bytes, not bits
673 * do the transformation
675 for (bn
= 1; (n
= READ(ibuf
, nbytes
)) == nbytes
; bn
++) {
676 MEMCPY(msgbuf
, ivec
, 8);
678 for (n
= 0; n
< 8 - nbytes
; n
++)
679 ivec
[n
] = ivec
[n
+nbytes
];
680 for (n
= 0; n
< nbytes
; n
++)
681 ivec
[8 - nbytes
+ n
] = ibuf
[n
] ^ msgbuf
[n
];
682 WRITE(&ivec
[8 - nbytes
], nbytes
);
685 * at EOF or last block -- in either case, the last byte contains
686 * the character representation of the number of bytes in it
689 MEMZERO(&ibuf
[n
], nbytes
- n
);
690 ibuf
[nbytes
- 1] = n
;
691 MEMCPY(msgbuf
, ivec
, 8);
693 for (n
= 0; n
< nbytes
; n
++)
694 ibuf
[n
] ^= msgbuf
[n
];
699 * This decrypts using the Cipher Block Chaining mode of DES
704 int n
; /* number of bytes actually read */
705 int c
; /* used to test for EOF */
706 int nbytes
; /* number of bytes to read */
707 int bn
; /* block number */
708 char ibuf
[8]; /* input buffer */
709 char obuf
[8]; /* output buffer */
710 DES_cblock msgbuf
; /* encryption buffer */
713 * do things in bytes, not bits
717 * do the transformation
719 for (bn
= 1; (n
= READ(ibuf
, nbytes
)) == nbytes
; bn
++) {
720 MEMCPY(msgbuf
, ivec
, 8);
722 for (c
= 0; c
< 8 - nbytes
; c
++)
723 ivec
[c
] = ivec
[c
+ nbytes
];
724 for (c
= 0; c
< nbytes
; c
++) {
725 ivec
[8 - nbytes
+ c
] = ibuf
[c
];
726 obuf
[c
] = ibuf
[c
] ^ msgbuf
[c
];
729 * if the last one, handle it specially
731 if ((c
= getchar()) == EOF
) {
733 if (n
< 0 || n
> nbytes
-1)
734 warnx("decryption failed (block corrupt) at %d",
738 (void)ungetc(c
, stdin
);
742 warnx("decryption failed (incomplete block) at %d", bn
);
746 * This encrypts using the alternative Cipher FeedBack mode of DES
751 int n
; /* number of bytes actually read */
752 int nbytes
; /* number of bytes to read */
753 int bn
; /* block number */
754 char ibuf
[8]; /* input buffer */
755 char obuf
[8]; /* output buffer */
756 DES_cblock msgbuf
; /* encryption buffer */
759 * do things in bytes, not bits
763 * do the transformation
765 for (bn
= 1; (n
= READ(ibuf
, nbytes
)) == nbytes
; bn
++) {
766 MEMCPY(msgbuf
, ivec
, 8);
768 for (n
= 0; n
< 8 - nbytes
; n
++)
769 ivec
[n
] = ivec
[n
+ nbytes
];
770 for (n
= 0; n
< nbytes
; n
++)
771 ivec
[8 - nbytes
+ n
] = (ibuf
[n
] ^ msgbuf
[n
]) | 0x80;
772 for (n
= 0; n
< nbytes
; n
++)
773 obuf
[n
] = ivec
[8 - nbytes
+ n
] & 0x7f;
777 * at EOF or last block -- in either case, the last byte contains
778 * the character representation of the number of bytes in it
781 MEMZERO(&ibuf
[n
], nbytes
- n
);
782 ibuf
[nbytes
- 1] = ('0' + n
)|0200;
783 MEMCPY(msgbuf
, ivec
, 8);
785 for (n
= 0; n
< nbytes
; n
++)
786 ibuf
[n
] ^= msgbuf
[n
];
791 * This decrypts using the alternative Cipher Block Chaining mode of DES
796 int n
; /* number of bytes actually read */
797 int c
; /* used to test for EOF */
798 int nbytes
; /* number of bytes to read */
799 int bn
; /* block number */
800 char ibuf
[8]; /* input buffer */
801 char obuf
[8]; /* output buffer */
802 DES_cblock msgbuf
; /* encryption buffer */
805 * do things in bytes, not bits
809 * do the transformation
811 for (bn
= 1; (n
= READ(ibuf
, nbytes
)) == nbytes
; bn
++) {
812 MEMCPY(msgbuf
, ivec
, 8);
814 for (c
= 0; c
< 8 - nbytes
; c
++)
815 ivec
[c
] = ivec
[c
+ nbytes
];
816 for (c
= 0; c
< nbytes
; c
++) {
817 ivec
[8 - nbytes
+ c
] = ibuf
[c
] | 0x80;
818 obuf
[c
] = (ibuf
[c
] ^ msgbuf
[c
]) & 0x7f;
821 * if the last one, handle it specially
823 if ((c
= getchar()) == EOF
) {
824 if ((n
= (obuf
[nbytes
-1] - '0')) < 0
826 warnx("decryption failed (block corrupt) at %d",
830 (void)ungetc(c
, stdin
);
834 warnx("decryption failed (incomplete block) at %d", bn
);
839 * This encrypts using the Output FeedBack mode of DES
844 int n
; /* number of bytes actually read */
845 int c
; /* used to test for EOF */
846 int nbytes
; /* number of bytes to read */
847 int bn
; /* block number */
848 char ibuf
[8]; /* input buffer */
849 char obuf
[8]; /* output buffer */
850 DES_cblock msgbuf
; /* encryption buffer */
853 * do things in bytes, not bits
857 * do the transformation
859 for (bn
= 1; (n
= READ(ibuf
, nbytes
)) == nbytes
; bn
++) {
860 MEMCPY(msgbuf
, ivec
, 8);
862 for (n
= 0; n
< 8 - nbytes
; n
++)
863 ivec
[n
] = ivec
[n
+ nbytes
];
864 for (n
= 0; n
< nbytes
; n
++) {
865 ivec
[8 - nbytes
+ n
] = msgbuf
[n
];
866 obuf
[n
] = ibuf
[n
] ^ msgbuf
[n
];
871 * at EOF or last block -- in either case, the last byte contains
872 * the character representation of the number of bytes in it
875 MEMZERO(&ibuf
[n
], nbytes
- n
);
876 ibuf
[nbytes
- 1] = n
;
877 MEMCPY(msgbuf
, ivec
, 8);
879 for (c
= 0; c
< nbytes
; c
++)
880 ibuf
[c
] ^= msgbuf
[c
];
885 * This decrypts using the Output Block Chaining mode of DES
890 int n
; /* number of bytes actually read */
891 int c
; /* used to test for EOF */
892 int nbytes
; /* number of bytes to read */
893 int bn
; /* block number */
894 char ibuf
[8]; /* input buffer */
895 char obuf
[8]; /* output buffer */
896 DES_cblock msgbuf
; /* encryption buffer */
899 * do things in bytes, not bits
903 * do the transformation
905 for (bn
= 1; (n
= READ(ibuf
, nbytes
)) == nbytes
; bn
++) {
906 MEMCPY(msgbuf
, ivec
, 8);
908 for (c
= 0; c
< 8 - nbytes
; c
++)
909 ivec
[c
] = ivec
[c
+ nbytes
];
910 for (c
= 0; c
< nbytes
; c
++) {
911 ivec
[8 - nbytes
+ c
] = msgbuf
[c
];
912 obuf
[c
] = ibuf
[c
] ^ msgbuf
[c
];
915 * if the last one, handle it specially
917 if ((c
= getchar()) == EOF
) {
919 if (n
< 0 || n
> nbytes
-1)
920 warnx("decryption failed (block corrupt) at %d",
924 (void)ungetc(c
, stdin
);
931 warnx("decryption failed (incomplete block) at %d", bn
);
935 * This authenticates using the Cipher FeedBack mode of DES
940 int n
, j
; /* number of bytes actually read */
941 int nbytes
; /* number of bytes to read */
942 char ibuf
[8]; /* input buffer */
943 DES_cblock msgbuf
; /* encryption buffer */
946 * do things in bytes, not bits
950 * do the transformation
952 while ((n
= READ(ibuf
, nbytes
)) == nbytes
) {
953 MEMCPY(msgbuf
, ivec
, 8);
955 for (n
= 0; n
< 8 - nbytes
; n
++)
956 ivec
[n
] = ivec
[n
+ nbytes
];
957 for (n
= 0; n
< nbytes
; n
++)
958 ivec
[8 - nbytes
+ n
] = ibuf
[n
] ^ msgbuf
[n
];
961 * at EOF or last block -- in either case, the last byte contains
962 * the character representation of the number of bytes in it
964 MEMZERO(&ibuf
[n
], nbytes
- n
);
965 ibuf
[nbytes
- 1] = '0' + n
;
966 MEMCPY(msgbuf
, ivec
, 8);
968 for (n
= 0; n
< nbytes
; n
++)
969 ibuf
[n
] ^= msgbuf
[n
];
972 * we write chars until fewer than 7 bits,
973 * and then pad the last one with 0 bits
975 for (n
= 0; macbits
> 7; n
++, macbits
-= 8)
976 (void)putchar(msgbuf
[n
]);
979 for (j
= 0; j
< macbits
; j
++)
980 msgbuf
[0] |= msgbuf
[n
] & bits
[j
];
981 (void)putchar(msgbuf
[0]);
986 * message about usage
991 (void)fprintf(stderr
, "%s\n",
992 "usage: bdes [-abdp] [-F N] [-f N] [-k key] [-m N] [-o N] [-v vector]");