version 1.4, 1998/05/07 19:12:17 |
version 1.5, 2001/02/05 08:38:23 |
|
|
* or the technical report for a complete reference). |
* or the technical report for a complete reference). |
*/ |
*/ |
|
|
|
#include <err.h> |
#include <errno.h> |
#include <errno.h> |
#include <unistd.h> |
#include <unistd.h> |
#include <stdio.h> |
#include <stdio.h> |
|
|
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
|
|
|
typedef char Desbuf[8]; |
|
int tobinhexi __P((char, int)); |
|
void cvtkey __P((char *, char *)); |
|
int setbits __P((char *, int)); |
|
void makekey __P((Desbuf)); |
|
void ecbenc __P((void)); |
|
void ecbdec __P((void)); |
|
void cbcenc __P((void)); |
|
void cbcdec __P((void)); |
|
void cbcauth __P((void)); |
|
void cfbenc __P((void)); |
|
void cfbdec __P((void)); |
|
void cfbaenc __P((void)); |
|
void cfbadec __P((void)); |
|
void cfbauth __P((void)); |
|
void ofbdec __P((void)); |
|
void ofbenc __P((void)); |
|
void usage __P((void)); |
|
|
/* |
/* |
* BSD and System V systems offer special library calls that do |
* BSD and System V systems offer special library calls that do |
* block moves and fills, so if possible we take advantage of them |
* block moves and fills, so if possible we take advantage of them |
|
|
#ifdef FASTWAY |
#ifdef FASTWAY |
#define DES_KEY(buf) \ |
#define DES_KEY(buf) \ |
if (des_setkey(buf)) \ |
if (des_setkey(buf)) \ |
err("des_setkey", 0); |
err(1, "des_setkey"); |
#define DES_XFORM(buf) \ |
#define DES_XFORM(buf) \ |
if (des_cipher(buf, buf, 0L, (inverse ? -1 : 1))) \ |
if (des_cipher(buf, buf, 0L, (inverse ? -1 : 1))) \ |
err("des_cipher", 0); |
err(1, "des_cipher"); |
#else |
#else |
#define DES_KEY(buf) { \ |
#define DES_KEY(buf) { \ |
char bits1[64]; /* bits of key */ \ |
char bits1[64]; /* bits of key */ \ |
expand(buf, bits1); \ |
expand(buf, bits1); \ |
if (setkey(bits1)) \ |
if (setkey(bits1)) \ |
err("setkey", 0); \ |
err(1, "setkey"); \ |
} |
} |
#define DES_XFORM(buf) { \ |
#define DES_XFORM(buf) { \ |
char bits1[64]; /* bits of message */ \ |
char bits1[64]; /* bits of message */ \ |
expand(buf, bits1); \ |
expand(buf, bits1); \ |
if (encrypt(bits1, inverse)) \ |
if (encrypt(bits1, inverse)) \ |
err("encrypt", 0); \ |
err(1, "encrypt"); \ |
compress(bits1, buf); \ |
compress(bits1, buf); \ |
} |
} |
#endif |
#endif |
|
|
/* |
/* |
* some things to make references easier |
* some things to make references easier |
*/ |
*/ |
typedef char Desbuf[8]; |
|
#define CHAR(x,i) (x[i]) |
#define CHAR(x,i) (x[i]) |
#define UCHAR(x,i) (x[i]) |
#define UCHAR(x,i) (x[i]) |
#define BUFFER(x) (x) |
#define BUFFER(x) (x) |
|
|
int fbbits = -1; /* number of feedback bits */ |
int fbbits = -1; /* number of feedback bits */ |
int pflag; /* 1 to preserve parity bits */ |
int pflag; /* 1 to preserve parity bits */ |
|
|
|
|
|
int |
main(ac, av) |
main(ac, av) |
int ac; /* arg count */ |
int ac; /* arg count */ |
char **av; /* arg vector */ |
char **av; /* arg vector */ |
|
|
} |
} |
|
|
/* |
/* |
* print a warning message and, possibly, terminate |
|
*/ |
|
err(n, s) |
|
int n; /* offending block number */ |
|
char *s; /* the message */ |
|
{ |
|
if (n > 0) |
|
(void)fprintf(stderr, "bdes (block %d): ", n); |
|
else |
|
(void)fprintf(stderr, "bdes: "); |
|
(void)fprintf(stderr, "%s\n", s ? s : strerror(errno)); |
|
exit(1); |
|
} |
|
|
|
/* |
|
* map a hex character to an integer |
* map a hex character to an integer |
*/ |
*/ |
|
int |
tobinhex(c, radix) |
tobinhex(c, radix) |
char c; /* char to be converted */ |
char c; /* char to be converted */ |
int radix; /* base (2 to 16) */ |
int radix; /* base (2 to 16) */ |
|
|
/* |
/* |
* convert the key to a bit pattern |
* convert the key to a bit pattern |
*/ |
*/ |
|
void |
cvtkey(obuf, ibuf) |
cvtkey(obuf, ibuf) |
char *obuf; /* bit pattern */ |
char *obuf; /* bit pattern */ |
char *ibuf; /* the key itself */ |
char *ibuf; /* the key itself */ |
|
|
* 2. must be a valid decimal number |
* 2. must be a valid decimal number |
* 3. must be a multiple of mult |
* 3. must be a multiple of mult |
*/ |
*/ |
|
int |
setbits(s, mult) |
setbits(s, mult) |
char *s; /* the ASCII string */ |
char *s; /* the ASCII string */ |
int mult; /* what it must be a multiple of */ |
int mult; /* what it must be a multiple of */ |
|
|
* systems set the parity (high) bit of each character to 0, and the |
* systems set the parity (high) bit of each character to 0, and the |
* DES ignores the low order bit of each character. |
* DES ignores the low order bit of each character. |
*/ |
*/ |
|
void |
makekey(buf) |
makekey(buf) |
Desbuf buf; /* key block */ |
Desbuf buf; /* key block */ |
{ |
{ |
|
|
/* |
/* |
* This encrypts using the Electronic Code Book mode of DES |
* This encrypts using the Electronic Code Book mode of DES |
*/ |
*/ |
|
void |
ecbenc() |
ecbenc() |
{ |
{ |
register int n; /* number of bytes actually read */ |
register int n; /* number of bytes actually read */ |
|
|
/* |
/* |
* This decrypts using the Electronic Code Book mode of DES |
* This decrypts using the Electronic Code Book mode of DES |
*/ |
*/ |
|
void |
ecbdec() |
ecbdec() |
{ |
{ |
register int n; /* number of bytes actually read */ |
register int n; /* number of bytes actually read */ |
|
|
/* |
/* |
* This encrypts using the Cipher Block Chaining mode of DES |
* This encrypts using the Cipher Block Chaining mode of DES |
*/ |
*/ |
|
void |
cbcenc() |
cbcenc() |
{ |
{ |
register int n; /* number of bytes actually read */ |
register int n; /* number of bytes actually read */ |
|
|
/* |
/* |
* This decrypts using the Cipher Block Chaining mode of DES |
* This decrypts using the Cipher Block Chaining mode of DES |
*/ |
*/ |
|
void |
cbcdec() |
cbcdec() |
{ |
{ |
register int n; /* number of bytes actually read */ |
register int n; /* number of bytes actually read */ |
|
|
/* |
/* |
* This authenticates using the Cipher Block Chaining mode of DES |
* This authenticates using the Cipher Block Chaining mode of DES |
*/ |
*/ |
|
void |
cbcauth() |
cbcauth() |
{ |
{ |
register int n, j; /* number of bytes actually read */ |
register int n, j; /* number of bytes actually read */ |
|
|
/* |
/* |
* This encrypts using the Cipher FeedBack mode of DES |
* This encrypts using the Cipher FeedBack mode of DES |
*/ |
*/ |
|
void |
cfbenc() |
cfbenc() |
{ |
{ |
register int n; /* number of bytes actually read */ |
register int n; /* number of bytes actually read */ |
|
|
/* |
/* |
* This decrypts using the Cipher Block Chaining mode of DES |
* This decrypts using the Cipher Block Chaining mode of DES |
*/ |
*/ |
|
void |
cfbdec() |
cfbdec() |
{ |
{ |
register int n; /* number of bytes actually read */ |
register int n; /* number of bytes actually read */ |
|
|
/* |
/* |
* This encrypts using the alternative Cipher FeedBack mode of DES |
* This encrypts using the alternative Cipher FeedBack mode of DES |
*/ |
*/ |
|
void |
cfbaenc() |
cfbaenc() |
{ |
{ |
register int n; /* number of bytes actually read */ |
register int n; /* number of bytes actually read */ |
|
|
/* |
/* |
* This decrypts using the alternative Cipher Block Chaining mode of DES |
* This decrypts using the alternative Cipher Block Chaining mode of DES |
*/ |
*/ |
|
void |
cfbadec() |
cfbadec() |
{ |
{ |
register int n; /* number of bytes actually read */ |
register int n; /* number of bytes actually read */ |
|
|
/* |
/* |
* This encrypts using the Output FeedBack mode of DES |
* This encrypts using the Output FeedBack mode of DES |
*/ |
*/ |
|
void |
ofbenc() |
ofbenc() |
{ |
{ |
register int n; /* number of bytes actually read */ |
register int n; /* number of bytes actually read */ |
|
|
/* |
/* |
* This decrypts using the Output Block Chaining mode of DES |
* This decrypts using the Output Block Chaining mode of DES |
*/ |
*/ |
|
void |
ofbdec() |
ofbdec() |
{ |
{ |
register int n; /* number of bytes actually read */ |
register int n; /* number of bytes actually read */ |
|
|
/* |
/* |
* This authenticates using the Cipher FeedBack mode of DES |
* This authenticates using the Cipher FeedBack mode of DES |
*/ |
*/ |
|
void |
cfbauth() |
cfbauth() |
{ |
{ |
register int n, j; /* number of bytes actually read */ |
register int n, j; /* number of bytes actually read */ |
|
|
/* |
/* |
* message about usage |
* message about usage |
*/ |
*/ |
|
void |
usage() |
usage() |
{ |
{ |
(void)fprintf(stderr, "%s\n", |
(void)fprintf(stderr, "%s\n", |