1994-08-07 18:17:39 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1991, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Matt Bishop of Dartmouth College.
|
|
|
|
*
|
|
|
|
* The United States Government has rights in this work pursuant
|
|
|
|
* to contract no. NAG 2-680 between the National Aeronautics and
|
|
|
|
* Space Administration and Dartmouth College.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
2003-06-02 19:10:59 +00:00
|
|
|
static const char copyright[] =
|
1994-08-07 18:17:39 +00:00
|
|
|
"@(#) Copyright (c) 1991, 1993\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n";
|
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
2000-07-16 05:48:49 +00:00
|
|
|
#if 0
|
1994-08-07 18:17:39 +00:00
|
|
|
static char sccsid[] = "@(#)bdes.c 8.1 (Berkeley) 6/6/93";
|
2000-07-16 05:48:49 +00:00
|
|
|
#endif
|
1994-08-07 18:17:39 +00:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BDES -- DES encryption package for Berkeley Software Distribution 4.4
|
|
|
|
* options:
|
|
|
|
* -a key is in ASCII
|
|
|
|
* -b use ECB (electronic code book) mode
|
|
|
|
* -d invert (decrypt) input
|
|
|
|
* -f b use b-bit CFB (cipher feedback) mode
|
|
|
|
* -F b use b-bit CFB (cipher feedback) alternative mode
|
|
|
|
* -k key use key as the cryptographic key
|
|
|
|
* -m b generate a MAC of length b
|
|
|
|
* -o b use b-bit OFB (output feedback) mode
|
|
|
|
* -p don't reset the parity bit
|
|
|
|
* -v v use v as the initialization vector (ignored for ECB)
|
|
|
|
* note: the last character of the last block is the integer indicating
|
|
|
|
* how many characters of that block are to be output
|
|
|
|
*
|
|
|
|
* Author: Matt Bishop
|
|
|
|
* Department of Mathematics and Computer Science
|
|
|
|
* Dartmouth College
|
|
|
|
* Hanover, NH 03755
|
|
|
|
* Email: Matt.Bishop@dartmouth.edu
|
|
|
|
* ...!decvax!dartvax!Matt.Bishop
|
|
|
|
*
|
|
|
|
* See Technical Report PCS-TR91-158, Department of Mathematics and Computer
|
|
|
|
* Science, Dartmouth College, for a detailed description of the implemen-
|
|
|
|
* tation and differences between it and Sun's. The DES is described in
|
|
|
|
* FIPS PUB 46, and the modes in FIPS PUB 81 (see either the manual page
|
|
|
|
* or the technical report for a complete reference).
|
|
|
|
*/
|
|
|
|
|
2003-06-02 19:10:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <err.h>
|
1994-08-07 18:17:39 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2003-06-02 19:10:59 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include <openssl/des.h>
|
1994-08-07 18:17:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* BSD and System V systems offer special library calls that do
|
|
|
|
* block moves and fills, so if possible we take advantage of them
|
|
|
|
*/
|
|
|
|
#define MEMCPY(dest,src,len) bcopy((src),(dest),(len))
|
|
|
|
#define MEMZERO(dest,len) bzero((dest),(len))
|
|
|
|
|
2003-06-02 19:10:59 +00:00
|
|
|
#define DES_XFORM(buf) \
|
|
|
|
DES_ecb_encrypt(buf, buf, &schedule, \
|
|
|
|
mode == MODE_ENCRYPT ? DES_ENCRYPT : DES_DECRYPT);
|
1994-08-07 18:17:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* this does an error-checking write
|
|
|
|
*/
|
|
|
|
#define READ(buf, n) fread(buf, sizeof(char), n, stdin)
|
|
|
|
#define WRITE(buf,n) \
|
|
|
|
if (fwrite(buf, sizeof(char), n, stdout) != n) \
|
2003-06-02 19:10:59 +00:00
|
|
|
warnx("fwrite error at %d", n);
|
1994-08-07 18:17:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* global variables and related macros
|
|
|
|
*/
|
|
|
|
#define KEY_DEFAULT 0 /* interpret radix of key from key */
|
|
|
|
#define KEY_ASCII 1 /* key is in ASCII characters */
|
|
|
|
int keybase = KEY_DEFAULT; /* how to interpret the key */
|
|
|
|
|
|
|
|
enum { /* encrypt, decrypt, authenticate */
|
|
|
|
MODE_ENCRYPT, MODE_DECRYPT, MODE_AUTHENTICATE
|
|
|
|
} mode = MODE_ENCRYPT;
|
2003-06-02 19:10:59 +00:00
|
|
|
|
1994-08-07 18:17:39 +00:00
|
|
|
enum { /* ecb, cbc, cfb, cfba, ofb? */
|
|
|
|
ALG_ECB, ALG_CBC, ALG_CFB, ALG_OFB, ALG_CFBA
|
|
|
|
} alg = ALG_CBC;
|
|
|
|
|
2003-06-02 19:10:59 +00:00
|
|
|
DES_cblock ivec; /* initialization vector */
|
|
|
|
|
1994-08-07 18:17:39 +00:00
|
|
|
char bits[] = { /* used to extract bits from a char */
|
|
|
|
'\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
|
|
|
|
};
|
2003-06-02 19:10:59 +00:00
|
|
|
|
1994-08-07 18:17:39 +00:00
|
|
|
int inverse; /* 0 to encrypt, 1 to decrypt */
|
|
|
|
int macbits = -1; /* number of bits in authentication */
|
|
|
|
int fbbits = -1; /* number of feedback bits */
|
|
|
|
int pflag; /* 1 to preserve parity bits */
|
|
|
|
|
2003-06-02 19:10:59 +00:00
|
|
|
DES_key_schedule schedule; /* expanded DES key */
|
|
|
|
|
|
|
|
static void ecbenc(void);
|
|
|
|
static void ecbdec(void);
|
|
|
|
static void cbcenc(void);
|
|
|
|
static void cbcdec(void);
|
|
|
|
static void cfbenc(void);
|
|
|
|
static void cfbdec(void);
|
|
|
|
static void cfbaenc(void);
|
|
|
|
static void cfbadec(void);
|
|
|
|
static void ofbenc(void);
|
|
|
|
static void ofbdec(void);
|
|
|
|
|
|
|
|
static void cbcauth(void);
|
|
|
|
static void cfbauth(void);
|
|
|
|
|
|
|
|
static void cvtkey(DES_cblock, char *);
|
|
|
|
static int setbits(char *, int);
|
|
|
|
static void makekey(DES_cblock *);
|
|
|
|
static int tobinhex(char, int);
|
|
|
|
|
|
|
|
static void usage(void);
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int ac, char *av[])
|
1994-08-07 18:17:39 +00:00
|
|
|
{
|
|
|
|
extern char *optarg; /* argument to option if any */
|
2003-06-02 19:10:59 +00:00
|
|
|
int i; /* counter in a for loop */
|
|
|
|
char *p; /* used to obtain the key */
|
|
|
|
DES_cblock msgbuf; /* I/O buffer */
|
1994-08-07 18:17:39 +00:00
|
|
|
int kflag; /* command-line encryptiooon key */
|
|
|
|
int argc; /* the real arg count */
|
|
|
|
char **argv; /* the real argument vector */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hide the arguments from ps(1) by making private copies of them
|
|
|
|
* and clobbering the global (visible to ps(1)) ones.
|
|
|
|
*/
|
|
|
|
argc = ac;
|
|
|
|
ac = 1;
|
|
|
|
argv = malloc((argc + 1) * sizeof(char *));
|
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
argv[i] = strdup(av[i]);
|
|
|
|
MEMZERO(av[i], strlen(av[i]));
|
|
|
|
}
|
|
|
|
argv[argc] = NULL;
|
|
|
|
|
|
|
|
/* initialize the initialization vctor */
|
|
|
|
MEMZERO(ivec, 8);
|
|
|
|
|
|
|
|
/* process the argument list */
|
|
|
|
kflag = 0;
|
|
|
|
while ((i = getopt(argc, argv, "abdF:f:k:m:o:pv:")) != EOF)
|
|
|
|
switch(i) {
|
|
|
|
case 'a': /* key is ASCII */
|
|
|
|
keybase = KEY_ASCII;
|
|
|
|
break;
|
|
|
|
case 'b': /* use ECB mode */
|
|
|
|
alg = ALG_ECB;
|
|
|
|
break;
|
|
|
|
case 'd': /* decrypt */
|
|
|
|
mode = MODE_DECRYPT;
|
|
|
|
break;
|
|
|
|
case 'F': /* use alternative CFB mode */
|
|
|
|
alg = ALG_CFBA;
|
|
|
|
if ((fbbits = setbits(optarg, 7)) > 56 || fbbits == 0)
|
2003-06-02 19:10:59 +00:00
|
|
|
errx(1, "-F: number must be 1-56 inclusive");
|
1994-08-07 18:17:39 +00:00
|
|
|
else if (fbbits == -1)
|
2003-06-02 19:10:59 +00:00
|
|
|
errx(1, "-F: number must be a multiple of 7");
|
1994-08-07 18:17:39 +00:00
|
|
|
break;
|
|
|
|
case 'f': /* use CFB mode */
|
|
|
|
alg = ALG_CFB;
|
|
|
|
if ((fbbits = setbits(optarg, 8)) > 64 || fbbits == 0)
|
2003-06-02 19:10:59 +00:00
|
|
|
errx(1, "-f: number must be 1-64 inclusive");
|
1994-08-07 18:17:39 +00:00
|
|
|
else if (fbbits == -1)
|
2003-06-02 19:10:59 +00:00
|
|
|
errx(1, "-f: number must be a multiple of 8");
|
1994-08-07 18:17:39 +00:00
|
|
|
break;
|
|
|
|
case 'k': /* encryption key */
|
|
|
|
kflag = 1;
|
2003-06-02 19:10:59 +00:00
|
|
|
cvtkey(msgbuf, optarg);
|
1994-08-07 18:17:39 +00:00
|
|
|
break;
|
|
|
|
case 'm': /* number of bits for MACing */
|
|
|
|
mode = MODE_AUTHENTICATE;
|
|
|
|
if ((macbits = setbits(optarg, 1)) > 64)
|
2003-06-02 19:10:59 +00:00
|
|
|
errx(1, "-m: number must be 0-64 inclusive");
|
1994-08-07 18:17:39 +00:00
|
|
|
break;
|
|
|
|
case 'o': /* use OFB mode */
|
|
|
|
alg = ALG_OFB;
|
|
|
|
if ((fbbits = setbits(optarg, 8)) > 64 || fbbits == 0)
|
2003-06-02 19:10:59 +00:00
|
|
|
errx(1, "-o: number must be 1-64 inclusive");
|
1994-08-07 18:17:39 +00:00
|
|
|
else if (fbbits == -1)
|
2003-06-02 19:10:59 +00:00
|
|
|
errx(1, "-o: number must be a multiple of 8");
|
1994-08-07 18:17:39 +00:00
|
|
|
break;
|
|
|
|
case 'p': /* preserve parity bits */
|
|
|
|
pflag = 1;
|
|
|
|
break;
|
|
|
|
case 'v': /* set initialization vector */
|
2003-06-02 19:10:59 +00:00
|
|
|
cvtkey(ivec, optarg);
|
1994-08-07 18:17:39 +00:00
|
|
|
break;
|
|
|
|
default: /* error */
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!kflag) {
|
|
|
|
/*
|
|
|
|
* if the key's not ASCII, assume it is
|
|
|
|
*/
|
|
|
|
keybase = KEY_ASCII;
|
|
|
|
/*
|
|
|
|
* get the key
|
|
|
|
*/
|
|
|
|
p = getpass("Enter key: ");
|
|
|
|
/*
|
|
|
|
* copy it, nul-padded, into the key area
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
cvtkey(msgbuf, p);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
|
2003-06-02 19:10:59 +00:00
|
|
|
makekey(&msgbuf);
|
1994-08-07 18:17:39 +00:00
|
|
|
inverse = (alg == ALG_CBC || alg == ALG_ECB) && mode == MODE_DECRYPT;
|
|
|
|
|
|
|
|
switch(alg) {
|
|
|
|
case ALG_CBC:
|
|
|
|
switch(mode) {
|
|
|
|
case MODE_AUTHENTICATE: /* authenticate using CBC mode */
|
|
|
|
cbcauth();
|
|
|
|
break;
|
|
|
|
case MODE_DECRYPT: /* decrypt using CBC mode */
|
|
|
|
cbcdec();
|
|
|
|
break;
|
|
|
|
case MODE_ENCRYPT: /* encrypt using CBC mode */
|
|
|
|
cbcenc();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ALG_CFB:
|
|
|
|
switch(mode) {
|
|
|
|
case MODE_AUTHENTICATE: /* authenticate using CFB mode */
|
|
|
|
cfbauth();
|
|
|
|
break;
|
|
|
|
case MODE_DECRYPT: /* decrypt using CFB mode */
|
|
|
|
cfbdec();
|
|
|
|
break;
|
|
|
|
case MODE_ENCRYPT: /* encrypt using CFB mode */
|
|
|
|
cfbenc();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ALG_CFBA:
|
|
|
|
switch(mode) {
|
|
|
|
case MODE_AUTHENTICATE: /* authenticate using CFBA mode */
|
2003-06-02 19:10:59 +00:00
|
|
|
errx(1, "can't authenticate with CFBA mode");
|
1994-08-07 18:17:39 +00:00
|
|
|
break;
|
|
|
|
case MODE_DECRYPT: /* decrypt using CFBA mode */
|
|
|
|
cfbadec();
|
|
|
|
break;
|
|
|
|
case MODE_ENCRYPT: /* encrypt using CFBA mode */
|
|
|
|
cfbaenc();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ALG_ECB:
|
|
|
|
switch(mode) {
|
|
|
|
case MODE_AUTHENTICATE: /* authenticate using ECB mode */
|
2003-06-02 19:10:59 +00:00
|
|
|
errx(1, "can't authenticate with ECB mode");
|
1994-08-07 18:17:39 +00:00
|
|
|
break;
|
|
|
|
case MODE_DECRYPT: /* decrypt using ECB mode */
|
|
|
|
ecbdec();
|
|
|
|
break;
|
|
|
|
case MODE_ENCRYPT: /* encrypt using ECB mode */
|
|
|
|
ecbenc();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ALG_OFB:
|
|
|
|
switch(mode) {
|
|
|
|
case MODE_AUTHENTICATE: /* authenticate using OFB mode */
|
2003-06-02 19:10:59 +00:00
|
|
|
errx(1, "can't authenticate with OFB mode");
|
1994-08-07 18:17:39 +00:00
|
|
|
break;
|
|
|
|
case MODE_DECRYPT: /* decrypt using OFB mode */
|
|
|
|
ofbdec();
|
|
|
|
break;
|
|
|
|
case MODE_ENCRYPT: /* encrypt using OFB mode */
|
|
|
|
ofbenc();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2003-06-02 19:10:59 +00:00
|
|
|
return (0);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* map a hex character to an integer
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
static int
|
|
|
|
tobinhex(char c, int radix)
|
1994-08-07 18:17:39 +00:00
|
|
|
{
|
|
|
|
switch(c) {
|
|
|
|
case '0': return(0x0);
|
|
|
|
case '1': return(0x1);
|
|
|
|
case '2': return(radix > 2 ? 0x2 : -1);
|
|
|
|
case '3': return(radix > 3 ? 0x3 : -1);
|
|
|
|
case '4': return(radix > 4 ? 0x4 : -1);
|
|
|
|
case '5': return(radix > 5 ? 0x5 : -1);
|
|
|
|
case '6': return(radix > 6 ? 0x6 : -1);
|
|
|
|
case '7': return(radix > 7 ? 0x7 : -1);
|
|
|
|
case '8': return(radix > 8 ? 0x8 : -1);
|
|
|
|
case '9': return(radix > 9 ? 0x9 : -1);
|
|
|
|
case 'A': case 'a': return(radix > 10 ? 0xa : -1);
|
|
|
|
case 'B': case 'b': return(radix > 11 ? 0xb : -1);
|
|
|
|
case 'C': case 'c': return(radix > 12 ? 0xc : -1);
|
|
|
|
case 'D': case 'd': return(radix > 13 ? 0xd : -1);
|
|
|
|
case 'E': case 'e': return(radix > 14 ? 0xe : -1);
|
|
|
|
case 'F': case 'f': return(radix > 15 ? 0xf : -1);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* invalid character
|
|
|
|
*/
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* convert the key to a bit pattern
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
static void
|
|
|
|
cvtkey(DES_cblock obuf, char *ibuf)
|
1994-08-07 18:17:39 +00:00
|
|
|
{
|
2003-06-02 19:10:59 +00:00
|
|
|
int i, j; /* counter in a for loop */
|
1994-08-07 18:17:39 +00:00
|
|
|
int nbuf[64]; /* used for hex/key translation */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* just switch on the key base
|
|
|
|
*/
|
|
|
|
switch(keybase) {
|
|
|
|
case KEY_ASCII: /* ascii to integer */
|
|
|
|
(void)strncpy(obuf, ibuf, 8);
|
|
|
|
return;
|
|
|
|
case KEY_DEFAULT: /* tell from context */
|
|
|
|
/*
|
|
|
|
* leading '0x' or '0X' == hex key
|
|
|
|
*/
|
|
|
|
if (ibuf[0] == '0' && (ibuf[1] == 'x' || ibuf[1] == 'X')) {
|
|
|
|
ibuf = &ibuf[2];
|
|
|
|
/*
|
|
|
|
* now translate it, bombing on any illegal hex digit
|
|
|
|
*/
|
|
|
|
for (i = 0; ibuf[i] && i < 16; i++)
|
|
|
|
if ((nbuf[i] = tobinhex(ibuf[i], 16)) == -1)
|
2003-06-02 19:10:59 +00:00
|
|
|
warnx("bad hex digit in key");
|
1994-08-07 18:17:39 +00:00
|
|
|
while (i < 16)
|
|
|
|
nbuf[i++] = 0;
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
obuf[i] =
|
|
|
|
((nbuf[2*i]&0xf)<<4) | (nbuf[2*i+1]&0xf);
|
|
|
|
/* preserve parity bits */
|
|
|
|
pflag = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* leading '0b' or '0B' == binary key
|
|
|
|
*/
|
|
|
|
if (ibuf[0] == '0' && (ibuf[1] == 'b' || ibuf[1] == 'B')) {
|
|
|
|
ibuf = &ibuf[2];
|
|
|
|
/*
|
|
|
|
* now translate it, bombing on any illegal binary digit
|
|
|
|
*/
|
|
|
|
for (i = 0; ibuf[i] && i < 16; i++)
|
|
|
|
if ((nbuf[i] = tobinhex(ibuf[i], 2)) == -1)
|
2003-06-02 19:10:59 +00:00
|
|
|
warnx("bad binary digit in key");
|
1994-08-07 18:17:39 +00:00
|
|
|
while (i < 64)
|
|
|
|
nbuf[i++] = 0;
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
for (j = 0; j < 8; j++)
|
|
|
|
obuf[i] = (obuf[i]<<1)|nbuf[8*i+j];
|
|
|
|
/* preserve parity bits */
|
|
|
|
pflag = 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* no special leader -- ASCII
|
|
|
|
*/
|
|
|
|
(void)strncpy(obuf, ibuf, 8);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* convert an ASCII string into a decimal number:
|
|
|
|
* 1. must be between 0 and 64 inclusive
|
|
|
|
* 2. must be a valid decimal number
|
|
|
|
* 3. must be a multiple of mult
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
static int
|
|
|
|
setbits(char *s, int mult)
|
1994-08-07 18:17:39 +00:00
|
|
|
{
|
2003-06-02 19:10:59 +00:00
|
|
|
char *p; /* pointer in a for loop */
|
|
|
|
int n = 0; /* the integer collected */
|
1994-08-07 18:17:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* skip white space
|
|
|
|
*/
|
|
|
|
while (isspace(*s))
|
|
|
|
s++;
|
|
|
|
/*
|
|
|
|
* get the integer
|
|
|
|
*/
|
|
|
|
for (p = s; *p; p++) {
|
|
|
|
if (isdigit(*p))
|
|
|
|
n = n * 10 + *p - '0';
|
|
|
|
else {
|
2003-06-02 19:10:59 +00:00
|
|
|
warnx("bad decimal digit in MAC length");
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* be sure it's a multiple of mult
|
|
|
|
*/
|
|
|
|
return((n % mult != 0) ? -1 : n);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*****************
|
|
|
|
* DES FUNCTIONS *
|
|
|
|
*****************/
|
|
|
|
/*
|
|
|
|
* This sets the DES key and (if you're using the deszip version)
|
|
|
|
* the direction of the transformation. This uses the Sun
|
|
|
|
* to map the 64-bit key onto the 56 bits that the key schedule
|
|
|
|
* generation routines use: the old way, which just uses the user-
|
|
|
|
* supplied 64 bits as is, and the new way, which resets the parity
|
|
|
|
* bit to be the same as the low-order bit in each character. The
|
|
|
|
* new way generates a greater variety of key schedules, since many
|
|
|
|
* systems set the parity (high) bit of each character to 0, and the
|
|
|
|
* DES ignores the low order bit of each character.
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
static void
|
|
|
|
makekey(DES_cblock *buf)
|
1994-08-07 18:17:39 +00:00
|
|
|
{
|
2003-06-02 19:10:59 +00:00
|
|
|
int i, j; /* counter in a for loop */
|
|
|
|
int par; /* parity counter */
|
1994-08-07 18:17:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* if the parity is not preserved, flip it
|
|
|
|
*/
|
|
|
|
if (!pflag) {
|
|
|
|
for (i = 0; i < 8; i++) {
|
|
|
|
par = 0;
|
|
|
|
for (j = 1; j < 8; j++)
|
2003-06-02 19:10:59 +00:00
|
|
|
if ((bits[j] & (*buf)[i]) != 0)
|
1994-08-07 18:17:39 +00:00
|
|
|
par++;
|
2003-06-02 19:10:59 +00:00
|
|
|
if ((par & 0x01) == 0x01)
|
|
|
|
(*buf)[i] &= 0x7f;
|
1994-08-07 18:17:39 +00:00
|
|
|
else
|
2003-06-02 19:10:59 +00:00
|
|
|
(*buf)[i] = ((*buf)[i] & 0x7f) | 0x80;
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-02 19:10:59 +00:00
|
|
|
DES_set_odd_parity(buf);
|
|
|
|
DES_set_key(buf, &schedule);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This encrypts using the Electronic Code Book mode of DES
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
static void
|
|
|
|
ecbenc(void)
|
1994-08-07 18:17:39 +00:00
|
|
|
{
|
2003-06-02 19:10:59 +00:00
|
|
|
int n; /* number of bytes actually read */
|
|
|
|
int bn; /* block number */
|
|
|
|
DES_cblock msgbuf; /* I/O buffer */
|
1994-08-07 18:17:39 +00:00
|
|
|
|
2003-06-02 19:10:59 +00:00
|
|
|
for (bn = 0; (n = READ(msgbuf, 8)) == 8; bn++) {
|
1994-08-07 18:17:39 +00:00
|
|
|
/*
|
|
|
|
* do the transformation
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
DES_XFORM(&msgbuf);
|
|
|
|
WRITE(&msgbuf, 8);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
/*
|
1999-04-25 13:14:36 +00:00
|
|
|
* at EOF or last block -- in either case, the last byte contains
|
1994-08-07 18:17:39 +00:00
|
|
|
* the character representation of the number of bytes in it
|
|
|
|
*/
|
|
|
|
bn++;
|
2003-06-02 19:10:59 +00:00
|
|
|
MEMZERO(&msgbuf[n], 8 - n);
|
|
|
|
msgbuf[7] = n;
|
|
|
|
DES_XFORM(&msgbuf);
|
|
|
|
WRITE(&msgbuf, 8);
|
1994-08-07 18:17:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This decrypts using the Electronic Code Book mode of DES
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
static void
|
|
|
|
ecbdec(void)
|
1994-08-07 18:17:39 +00:00
|
|
|
{
|
2003-06-02 19:10:59 +00:00
|
|
|
int n; /* number of bytes actually read */
|
|
|
|
int c; /* used to test for EOF */
|
|
|
|
int bn; /* block number */
|
|
|
|
DES_cblock msgbuf; /* I/O buffer */
|
1994-08-07 18:17:39 +00:00
|
|
|
|
2003-06-02 19:10:59 +00:00
|
|
|
for (bn = 1; (n = READ(msgbuf, 8)) == 8; bn++) {
|
1994-08-07 18:17:39 +00:00
|
|
|
/*
|
|
|
|
* do the transformation
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
DES_XFORM(&msgbuf);
|
1994-08-07 18:17:39 +00:00
|
|
|
/*
|
|
|
|
* if the last one, handle it specially
|
|
|
|
*/
|
|
|
|
if ((c = getchar()) == EOF) {
|
2003-06-02 19:10:59 +00:00
|
|
|
n = msgbuf[7];
|
1994-08-07 18:17:39 +00:00
|
|
|
if (n < 0 || n > 7)
|
2003-06-02 19:10:59 +00:00
|
|
|
warnx("decryption failed (block corrupt) at %d",
|
|
|
|
bn);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
(void)ungetc(c, stdin);
|
2003-06-02 19:10:59 +00:00
|
|
|
WRITE(msgbuf, n);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
if (n > 0)
|
2003-06-02 19:10:59 +00:00
|
|
|
warnx("decryption failed (incomplete block) at %d", bn);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This encrypts using the Cipher Block Chaining mode of DES
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
static void
|
|
|
|
cbcenc(void)
|
1994-08-07 18:17:39 +00:00
|
|
|
{
|
2003-06-02 19:10:59 +00:00
|
|
|
int n; /* number of bytes actually read */
|
|
|
|
int bn; /* block number */
|
|
|
|
DES_cblock msgbuf; /* I/O buffer */
|
1994-08-07 18:17:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* do the transformation
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
for (bn = 1; (n = READ(msgbuf, 8)) == 8; bn++) {
|
1994-08-07 18:17:39 +00:00
|
|
|
for (n = 0; n < 8; n++)
|
2003-06-02 19:10:59 +00:00
|
|
|
msgbuf[n] ^= ivec[n];
|
|
|
|
DES_XFORM(&msgbuf);
|
|
|
|
MEMCPY(ivec, msgbuf, 8);
|
|
|
|
WRITE(msgbuf, 8);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* at EOF or last block -- in either case, the last byte contains
|
|
|
|
* the character representation of the number of bytes in it
|
|
|
|
*/
|
|
|
|
bn++;
|
2003-06-02 19:10:59 +00:00
|
|
|
MEMZERO(&msgbuf[n], 8 - n);
|
|
|
|
msgbuf[7] = n;
|
1994-08-07 18:17:39 +00:00
|
|
|
for (n = 0; n < 8; n++)
|
2003-06-02 19:10:59 +00:00
|
|
|
msgbuf[n] ^= ivec[n];
|
|
|
|
DES_XFORM(&msgbuf);
|
|
|
|
WRITE(msgbuf, 8);
|
1994-08-07 18:17:39 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This decrypts using the Cipher Block Chaining mode of DES
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
static void
|
|
|
|
cbcdec(void)
|
1994-08-07 18:17:39 +00:00
|
|
|
{
|
2003-06-02 19:10:59 +00:00
|
|
|
int n; /* number of bytes actually read */
|
|
|
|
DES_cblock msgbuf; /* I/O buffer */
|
|
|
|
DES_cblock ibuf; /* temp buffer for initialization vector */
|
|
|
|
int c; /* used to test for EOF */
|
|
|
|
int bn; /* block number */
|
1994-08-07 18:17:39 +00:00
|
|
|
|
2003-06-02 19:10:59 +00:00
|
|
|
for (bn = 0; (n = READ(msgbuf, 8)) == 8; bn++) {
|
1994-08-07 18:17:39 +00:00
|
|
|
/*
|
|
|
|
* do the transformation
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
MEMCPY(ibuf, msgbuf, 8);
|
|
|
|
DES_XFORM(&msgbuf);
|
1994-08-07 18:17:39 +00:00
|
|
|
for (c = 0; c < 8; c++)
|
2003-06-02 19:10:59 +00:00
|
|
|
msgbuf[c] ^= ivec[c];
|
|
|
|
MEMCPY(ivec, ibuf, 8);
|
1994-08-07 18:17:39 +00:00
|
|
|
/*
|
|
|
|
* if the last one, handle it specially
|
|
|
|
*/
|
|
|
|
if ((c = getchar()) == EOF) {
|
2003-06-02 19:10:59 +00:00
|
|
|
n = msgbuf[7];
|
1994-08-07 18:17:39 +00:00
|
|
|
if (n < 0 || n > 7)
|
2003-06-02 19:10:59 +00:00
|
|
|
warnx("decryption failed (block corrupt) at %d",
|
|
|
|
bn);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
(void)ungetc(c, stdin);
|
2003-06-02 19:10:59 +00:00
|
|
|
WRITE(msgbuf, n);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
if (n > 0)
|
2003-06-02 19:10:59 +00:00
|
|
|
warnx("decryption failed (incomplete block) at %d", bn);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This authenticates using the Cipher Block Chaining mode of DES
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
static void
|
|
|
|
cbcauth(void)
|
1994-08-07 18:17:39 +00:00
|
|
|
{
|
2003-06-02 19:10:59 +00:00
|
|
|
int n, j; /* number of bytes actually read */
|
|
|
|
DES_cblock msgbuf; /* I/O buffer */
|
|
|
|
DES_cblock encbuf; /* encryption buffer */
|
1994-08-07 18:17:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* do the transformation
|
|
|
|
* note we DISCARD the encrypted block;
|
|
|
|
* we only care about the last one
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
while ((n = READ(msgbuf, 8)) == 8) {
|
1994-08-07 18:17:39 +00:00
|
|
|
for (n = 0; n < 8; n++)
|
2003-06-02 19:10:59 +00:00
|
|
|
encbuf[n] = msgbuf[n] ^ ivec[n];
|
|
|
|
DES_XFORM(&encbuf);
|
|
|
|
MEMCPY(ivec, encbuf, 8);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* now compute the last one, right padding with '\0' if need be
|
|
|
|
*/
|
|
|
|
if (n > 0) {
|
2003-06-02 19:10:59 +00:00
|
|
|
MEMZERO(&msgbuf[n], 8 - n);
|
1994-08-07 18:17:39 +00:00
|
|
|
for (n = 0; n < 8; n++)
|
2003-06-02 19:10:59 +00:00
|
|
|
encbuf[n] = msgbuf[n] ^ ivec[n];
|
|
|
|
DES_XFORM(&encbuf);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* drop the bits
|
|
|
|
* we write chars until fewer than 7 bits,
|
|
|
|
* and then pad the last one with 0 bits
|
|
|
|
*/
|
|
|
|
for (n = 0; macbits > 7; n++, macbits -= 8)
|
2003-06-02 19:10:59 +00:00
|
|
|
(void)putchar(encbuf[n]);
|
1994-08-07 18:17:39 +00:00
|
|
|
if (macbits > 0) {
|
2003-06-02 19:10:59 +00:00
|
|
|
msgbuf[0] = 0x00;
|
1994-08-07 18:17:39 +00:00
|
|
|
for (j = 0; j < macbits; j++)
|
2003-06-02 19:10:59 +00:00
|
|
|
msgbuf[0] |= encbuf[n] & bits[j];
|
|
|
|
(void)putchar(msgbuf[0]);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This encrypts using the Cipher FeedBack mode of DES
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
static void
|
|
|
|
cfbenc(void)
|
1994-08-07 18:17:39 +00:00
|
|
|
{
|
2003-06-02 19:10:59 +00:00
|
|
|
int n; /* number of bytes actually read */
|
|
|
|
int nbytes; /* number of bytes to read */
|
|
|
|
int bn; /* block number */
|
1994-08-07 18:17:39 +00:00
|
|
|
char ibuf[8]; /* input buffer */
|
2003-06-02 19:10:59 +00:00
|
|
|
DES_cblock msgbuf; /* encryption buffer */
|
1994-08-07 18:17:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* do things in bytes, not bits
|
|
|
|
*/
|
|
|
|
nbytes = fbbits / 8;
|
|
|
|
/*
|
|
|
|
* do the transformation
|
|
|
|
*/
|
|
|
|
for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
|
2003-06-02 19:10:59 +00:00
|
|
|
MEMCPY(msgbuf, ivec, 8);
|
|
|
|
DES_XFORM(&msgbuf);
|
1994-08-07 18:17:39 +00:00
|
|
|
for (n = 0; n < 8 - nbytes; n++)
|
2003-06-02 19:10:59 +00:00
|
|
|
ivec[n] = ivec[n+nbytes];
|
1994-08-07 18:17:39 +00:00
|
|
|
for (n = 0; n < nbytes; n++)
|
2003-06-02 19:10:59 +00:00
|
|
|
ivec[8 - nbytes + n] = ibuf[n] ^ msgbuf[n];
|
|
|
|
WRITE(&ivec[8 - nbytes], nbytes);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* at EOF or last block -- in either case, the last byte contains
|
|
|
|
* the character representation of the number of bytes in it
|
|
|
|
*/
|
|
|
|
bn++;
|
|
|
|
MEMZERO(&ibuf[n], nbytes - n);
|
|
|
|
ibuf[nbytes - 1] = n;
|
2003-06-02 19:10:59 +00:00
|
|
|
MEMCPY(msgbuf, ivec, 8);
|
|
|
|
DES_XFORM(&msgbuf);
|
1994-08-07 18:17:39 +00:00
|
|
|
for (n = 0; n < nbytes; n++)
|
2003-06-02 19:10:59 +00:00
|
|
|
ibuf[n] ^= msgbuf[n];
|
1994-08-07 18:17:39 +00:00
|
|
|
WRITE(ibuf, nbytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This decrypts using the Cipher Block Chaining mode of DES
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
static void
|
|
|
|
cfbdec(void)
|
1994-08-07 18:17:39 +00:00
|
|
|
{
|
2003-06-02 19:10:59 +00:00
|
|
|
int n; /* number of bytes actually read */
|
|
|
|
int c; /* used to test for EOF */
|
|
|
|
int nbytes; /* number of bytes to read */
|
|
|
|
int bn; /* block number */
|
1994-08-07 18:17:39 +00:00
|
|
|
char ibuf[8]; /* input buffer */
|
|
|
|
char obuf[8]; /* output buffer */
|
2003-06-02 19:10:59 +00:00
|
|
|
DES_cblock msgbuf; /* encryption buffer */
|
1994-08-07 18:17:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* do things in bytes, not bits
|
|
|
|
*/
|
|
|
|
nbytes = fbbits / 8;
|
|
|
|
/*
|
|
|
|
* do the transformation
|
|
|
|
*/
|
|
|
|
for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
|
2003-06-02 19:10:59 +00:00
|
|
|
MEMCPY(msgbuf, ivec, 8);
|
|
|
|
DES_XFORM(&msgbuf);
|
1994-08-07 18:17:39 +00:00
|
|
|
for (c = 0; c < 8 - nbytes; c++)
|
2003-06-02 19:10:59 +00:00
|
|
|
ivec[c] = ivec[c + nbytes];
|
1994-08-07 18:17:39 +00:00
|
|
|
for (c = 0; c < nbytes; c++) {
|
2003-06-02 19:10:59 +00:00
|
|
|
ivec[8 - nbytes + c] = ibuf[c];
|
|
|
|
obuf[c] = ibuf[c] ^ msgbuf[c];
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* if the last one, handle it specially
|
|
|
|
*/
|
|
|
|
if ((c = getchar()) == EOF) {
|
|
|
|
n = obuf[nbytes-1];
|
|
|
|
if (n < 0 || n > nbytes-1)
|
2003-06-02 19:10:59 +00:00
|
|
|
warnx("decryption failed (block corrupt) at %d",
|
|
|
|
bn);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
(void)ungetc(c, stdin);
|
|
|
|
WRITE(obuf, n);
|
|
|
|
}
|
|
|
|
if (n > 0)
|
2003-06-02 19:10:59 +00:00
|
|
|
warnx("decryption failed (incomplete block) at %d", bn);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This encrypts using the alternative Cipher FeedBack mode of DES
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
static void
|
|
|
|
cfbaenc(void)
|
1994-08-07 18:17:39 +00:00
|
|
|
{
|
2003-06-02 19:10:59 +00:00
|
|
|
int n; /* number of bytes actually read */
|
|
|
|
int nbytes; /* number of bytes to read */
|
|
|
|
int bn; /* block number */
|
1994-08-07 18:17:39 +00:00
|
|
|
char ibuf[8]; /* input buffer */
|
|
|
|
char obuf[8]; /* output buffer */
|
2003-06-02 19:10:59 +00:00
|
|
|
DES_cblock msgbuf; /* encryption buffer */
|
1994-08-07 18:17:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* do things in bytes, not bits
|
|
|
|
*/
|
|
|
|
nbytes = fbbits / 7;
|
|
|
|
/*
|
|
|
|
* do the transformation
|
|
|
|
*/
|
|
|
|
for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
|
2003-06-02 19:10:59 +00:00
|
|
|
MEMCPY(msgbuf, ivec, 8);
|
|
|
|
DES_XFORM(&msgbuf);
|
1994-08-07 18:17:39 +00:00
|
|
|
for (n = 0; n < 8 - nbytes; n++)
|
2003-06-02 19:10:59 +00:00
|
|
|
ivec[n] = ivec[n + nbytes];
|
1994-08-07 18:17:39 +00:00
|
|
|
for (n = 0; n < nbytes; n++)
|
2003-06-02 19:10:59 +00:00
|
|
|
ivec[8 - nbytes + n] = (ibuf[n] ^ msgbuf[n]) | 0x80;
|
1994-08-07 18:17:39 +00:00
|
|
|
for (n = 0; n < nbytes; n++)
|
2003-06-02 19:10:59 +00:00
|
|
|
obuf[n] = ivec[8 - nbytes + n] & 0x7f;
|
1994-08-07 18:17:39 +00:00
|
|
|
WRITE(obuf, nbytes);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* at EOF or last block -- in either case, the last byte contains
|
|
|
|
* the character representation of the number of bytes in it
|
|
|
|
*/
|
|
|
|
bn++;
|
|
|
|
MEMZERO(&ibuf[n], nbytes - n);
|
|
|
|
ibuf[nbytes - 1] = ('0' + n)|0200;
|
2003-06-02 19:10:59 +00:00
|
|
|
MEMCPY(msgbuf, ivec, 8);
|
|
|
|
DES_XFORM(&msgbuf);
|
1994-08-07 18:17:39 +00:00
|
|
|
for (n = 0; n < nbytes; n++)
|
2003-06-02 19:10:59 +00:00
|
|
|
ibuf[n] ^= msgbuf[n];
|
1994-08-07 18:17:39 +00:00
|
|
|
WRITE(ibuf, nbytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This decrypts using the alternative Cipher Block Chaining mode of DES
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
static void
|
|
|
|
cfbadec(void)
|
1994-08-07 18:17:39 +00:00
|
|
|
{
|
2003-06-02 19:10:59 +00:00
|
|
|
int n; /* number of bytes actually read */
|
|
|
|
int c; /* used to test for EOF */
|
|
|
|
int nbytes; /* number of bytes to read */
|
|
|
|
int bn; /* block number */
|
1994-08-07 18:17:39 +00:00
|
|
|
char ibuf[8]; /* input buffer */
|
|
|
|
char obuf[8]; /* output buffer */
|
2003-06-02 19:10:59 +00:00
|
|
|
DES_cblock msgbuf; /* encryption buffer */
|
1994-08-07 18:17:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* do things in bytes, not bits
|
|
|
|
*/
|
|
|
|
nbytes = fbbits / 7;
|
|
|
|
/*
|
|
|
|
* do the transformation
|
|
|
|
*/
|
|
|
|
for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
|
2003-06-02 19:10:59 +00:00
|
|
|
MEMCPY(msgbuf, ivec, 8);
|
|
|
|
DES_XFORM(&msgbuf);
|
1994-08-07 18:17:39 +00:00
|
|
|
for (c = 0; c < 8 - nbytes; c++)
|
2003-06-02 19:10:59 +00:00
|
|
|
ivec[c] = ivec[c + nbytes];
|
1994-08-07 18:17:39 +00:00
|
|
|
for (c = 0; c < nbytes; c++) {
|
2003-06-02 19:10:59 +00:00
|
|
|
ivec[8 - nbytes + c] = ibuf[c] | 0x80;
|
|
|
|
obuf[c] = (ibuf[c] ^ msgbuf[c]) & 0x7f;
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* if the last one, handle it specially
|
|
|
|
*/
|
|
|
|
if ((c = getchar()) == EOF) {
|
|
|
|
if ((n = (obuf[nbytes-1] - '0')) < 0
|
|
|
|
|| n > nbytes-1)
|
2003-06-02 19:10:59 +00:00
|
|
|
warnx("decryption failed (block corrupt) at %d",
|
|
|
|
bn);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
(void)ungetc(c, stdin);
|
|
|
|
WRITE(obuf, n);
|
|
|
|
}
|
|
|
|
if (n > 0)
|
2003-06-02 19:10:59 +00:00
|
|
|
warnx("decryption failed (incomplete block) at %d", bn);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This encrypts using the Output FeedBack mode of DES
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
static void
|
|
|
|
ofbenc(void)
|
1994-08-07 18:17:39 +00:00
|
|
|
{
|
2003-06-02 19:10:59 +00:00
|
|
|
int n; /* number of bytes actually read */
|
|
|
|
int c; /* used to test for EOF */
|
|
|
|
int nbytes; /* number of bytes to read */
|
|
|
|
int bn; /* block number */
|
1994-08-07 18:17:39 +00:00
|
|
|
char ibuf[8]; /* input buffer */
|
|
|
|
char obuf[8]; /* output buffer */
|
2003-06-02 19:10:59 +00:00
|
|
|
DES_cblock msgbuf; /* encryption buffer */
|
1994-08-07 18:17:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* do things in bytes, not bits
|
|
|
|
*/
|
|
|
|
nbytes = fbbits / 8;
|
|
|
|
/*
|
|
|
|
* do the transformation
|
|
|
|
*/
|
|
|
|
for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
|
2003-06-02 19:10:59 +00:00
|
|
|
MEMCPY(msgbuf, ivec, 8);
|
|
|
|
DES_XFORM(&msgbuf);
|
1994-08-07 18:17:39 +00:00
|
|
|
for (n = 0; n < 8 - nbytes; n++)
|
2003-06-02 19:10:59 +00:00
|
|
|
ivec[n] = ivec[n + nbytes];
|
1994-08-07 18:17:39 +00:00
|
|
|
for (n = 0; n < nbytes; n++) {
|
2003-06-02 19:10:59 +00:00
|
|
|
ivec[8 - nbytes + n] = msgbuf[n];
|
|
|
|
obuf[n] = ibuf[n] ^ msgbuf[n];
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
WRITE(obuf, nbytes);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* at EOF or last block -- in either case, the last byte contains
|
|
|
|
* the character representation of the number of bytes in it
|
|
|
|
*/
|
|
|
|
bn++;
|
|
|
|
MEMZERO(&ibuf[n], nbytes - n);
|
|
|
|
ibuf[nbytes - 1] = n;
|
2003-06-02 19:10:59 +00:00
|
|
|
MEMCPY(msgbuf, ivec, 8);
|
|
|
|
DES_XFORM(&msgbuf);
|
1994-08-07 18:17:39 +00:00
|
|
|
for (c = 0; c < nbytes; c++)
|
2003-06-02 19:10:59 +00:00
|
|
|
ibuf[c] ^= msgbuf[c];
|
1994-08-07 18:17:39 +00:00
|
|
|
WRITE(ibuf, nbytes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This decrypts using the Output Block Chaining mode of DES
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
static void
|
|
|
|
ofbdec(void)
|
1994-08-07 18:17:39 +00:00
|
|
|
{
|
2003-06-02 19:10:59 +00:00
|
|
|
int n; /* number of bytes actually read */
|
|
|
|
int c; /* used to test for EOF */
|
|
|
|
int nbytes; /* number of bytes to read */
|
|
|
|
int bn; /* block number */
|
1994-08-07 18:17:39 +00:00
|
|
|
char ibuf[8]; /* input buffer */
|
|
|
|
char obuf[8]; /* output buffer */
|
2003-06-02 19:10:59 +00:00
|
|
|
DES_cblock msgbuf; /* encryption buffer */
|
1994-08-07 18:17:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* do things in bytes, not bits
|
|
|
|
*/
|
|
|
|
nbytes = fbbits / 8;
|
|
|
|
/*
|
|
|
|
* do the transformation
|
|
|
|
*/
|
|
|
|
for (bn = 1; (n = READ(ibuf, nbytes)) == nbytes; bn++) {
|
2003-06-02 19:10:59 +00:00
|
|
|
MEMCPY(msgbuf, ivec, 8);
|
|
|
|
DES_XFORM(&msgbuf);
|
1994-08-07 18:17:39 +00:00
|
|
|
for (c = 0; c < 8 - nbytes; c++)
|
2003-06-02 19:10:59 +00:00
|
|
|
ivec[c] = ivec[c + nbytes];
|
1994-08-07 18:17:39 +00:00
|
|
|
for (c = 0; c < nbytes; c++) {
|
2003-06-02 19:10:59 +00:00
|
|
|
ivec[8 - nbytes + c] = msgbuf[c];
|
|
|
|
obuf[c] = ibuf[c] ^ msgbuf[c];
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* if the last one, handle it specially
|
|
|
|
*/
|
|
|
|
if ((c = getchar()) == EOF) {
|
|
|
|
n = obuf[nbytes-1];
|
|
|
|
if (n < 0 || n > nbytes-1)
|
2003-06-02 19:10:59 +00:00
|
|
|
warnx("decryption failed (block corrupt) at %d",
|
|
|
|
bn);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
(void)ungetc(c, stdin);
|
|
|
|
/*
|
|
|
|
* dump it
|
|
|
|
*/
|
|
|
|
WRITE(obuf, n);
|
|
|
|
}
|
|
|
|
if (n > 0)
|
2003-06-02 19:10:59 +00:00
|
|
|
warnx("decryption failed (incomplete block) at %d", bn);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This authenticates using the Cipher FeedBack mode of DES
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
static void
|
|
|
|
cfbauth(void)
|
1994-08-07 18:17:39 +00:00
|
|
|
{
|
2003-06-02 19:10:59 +00:00
|
|
|
int n, j; /* number of bytes actually read */
|
|
|
|
int nbytes; /* number of bytes to read */
|
1994-08-07 18:17:39 +00:00
|
|
|
char ibuf[8]; /* input buffer */
|
2003-06-02 19:10:59 +00:00
|
|
|
DES_cblock msgbuf; /* encryption buffer */
|
1994-08-07 18:17:39 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* do things in bytes, not bits
|
|
|
|
*/
|
|
|
|
nbytes = fbbits / 8;
|
|
|
|
/*
|
|
|
|
* do the transformation
|
|
|
|
*/
|
|
|
|
while ((n = READ(ibuf, nbytes)) == nbytes) {
|
2003-06-02 19:10:59 +00:00
|
|
|
MEMCPY(msgbuf, ivec, 8);
|
|
|
|
DES_XFORM(&msgbuf);
|
1994-08-07 18:17:39 +00:00
|
|
|
for (n = 0; n < 8 - nbytes; n++)
|
2003-06-02 19:10:59 +00:00
|
|
|
ivec[n] = ivec[n + nbytes];
|
1994-08-07 18:17:39 +00:00
|
|
|
for (n = 0; n < nbytes; n++)
|
2003-06-02 19:10:59 +00:00
|
|
|
ivec[8 - nbytes + n] = ibuf[n] ^ msgbuf[n];
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* at EOF or last block -- in either case, the last byte contains
|
|
|
|
* the character representation of the number of bytes in it
|
|
|
|
*/
|
|
|
|
MEMZERO(&ibuf[n], nbytes - n);
|
|
|
|
ibuf[nbytes - 1] = '0' + n;
|
2003-06-02 19:10:59 +00:00
|
|
|
MEMCPY(msgbuf, ivec, 8);
|
|
|
|
DES_XFORM(&msgbuf);
|
1994-08-07 18:17:39 +00:00
|
|
|
for (n = 0; n < nbytes; n++)
|
2003-06-02 19:10:59 +00:00
|
|
|
ibuf[n] ^= msgbuf[n];
|
1994-08-07 18:17:39 +00:00
|
|
|
/*
|
|
|
|
* drop the bits
|
|
|
|
* we write chars until fewer than 7 bits,
|
|
|
|
* and then pad the last one with 0 bits
|
|
|
|
*/
|
|
|
|
for (n = 0; macbits > 7; n++, macbits -= 8)
|
2003-06-02 19:10:59 +00:00
|
|
|
(void)putchar(msgbuf[n]);
|
1994-08-07 18:17:39 +00:00
|
|
|
if (macbits > 0) {
|
2003-06-02 19:10:59 +00:00
|
|
|
msgbuf[0] = 0x00;
|
1994-08-07 18:17:39 +00:00
|
|
|
for (j = 0; j < macbits; j++)
|
2003-06-02 19:10:59 +00:00
|
|
|
msgbuf[0] |= msgbuf[n] & bits[j];
|
|
|
|
(void)putchar(msgbuf[0]);
|
1994-08-07 18:17:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* message about usage
|
|
|
|
*/
|
2003-06-02 19:10:59 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
1994-08-07 18:17:39 +00:00
|
|
|
{
|
1995-05-30 06:12:45 +00:00
|
|
|
(void)fprintf(stderr, "%s\n",
|
1994-08-07 18:17:39 +00:00
|
|
|
"usage: bdes [-abdp] [-F bit] [-f bit] [-k key] [-m bit] [-o bit] [-v vector]");
|
|
|
|
exit(1);
|
|
|
|
}
|