/* cbc.c: This file contains the encryption routines for the ed line editor */
* Copyright (c) 1993 The Regents of the University of California.
* Copyright (c) 1993 Andrew Moore, Talke Studio.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 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
* from: @(#)bdes.c 5.5 (Berkeley) 6/27/91
static char *rcsid
= "@(#)$Id: cbc.c,v 1.3 1993/12/14 18:01:10 alm Exp $";
* Define a divisor for rand() that yields a uniform distribution in the
#define RAND_DIV (((unsigned) RAND_MAX + 1) >> 8)
* BSD and System V systems offer special library calls that do
* block move_liness and fills, so if possible we take advantage of them
#define MEMCPY(dest,src,len) memcpy((dest),(src),(len))
#define MEMZERO(dest,len) memset((dest), 0, (len))
/* Hide the calls to the primitive encryption routines. */
if (des_cipher(buf, buf, 0L, (inverse ? -1 : 1))) \
* read/write - no error checking
#define READ(buf, n, fp) fread(buf, sizeof(char), n, fp)
#define WRITE(buf, n, fp) fwrite(buf, sizeof(char), n, fp)
* some things to make references easier
#define UCHAR(x,i) (x[i])
* global variables and related macros
enum { /* encrypt, decrypt, authenticate */
MODE_ENCRYPT
, MODE_DECRYPT
, MODE_AUTHENTICATE
Desbuf ivec
; /* initialization vector */
Desbuf pvec
; /* padding vector */
char bits
[] = { /* used to extract bits from a char */
'\200', '\100', '\040', '\020', '\010', '\004', '\002', '\001'
int pflag
; /* 1 to preserve parity bits */
unsigned char des_buf
[8]; /* shared buffer for get_des_char/put_des_char */
int des_ct
= 0; /* count for get_des_char/put_des_char */
int des_n
= 0; /* index for put_des_char/get_des_char */
/* init_des_cipher: initialize DES */
/* initialize the initialization vctor */
/* intialize the padding vector */
srand((unsigned) time((time_t *) 0));
CHAR(pvec
, i
) = (char) (rand()/RAND_DIV
);
/* get_des_char: return next char in an encrypted file */
des_ct
= cbc_decode(des_buf
, fp
);
return (des_ct
> 0) ? des_buf
[des_n
++] : EOF
;
/* put_des_char: write a char to an encrypted file; return char written */
if (des_n
== sizeof des_buf
) {
des_ct
= cbc_encode(des_buf
, des_n
, fp
);
return (des_ct
>= 0) ? (des_buf
[des_n
++] = c
) : EOF
;
/* flush_des_file: flush an encrypted file's output; return status */
if (des_n
== sizeof des_buf
) {
des_ct
= cbc_encode(des_buf
, des_n
, fp
);
return (des_ct
>= 0 && cbc_encode(des_buf
, des_n
, fp
) >= 0) ? 0 : EOF
;
* get keyword from tty or stdin
register char *p
; /* used to obtain the key */
Desbuf msgbuf
; /* I/O buffer */
if (*(p
= getpass("Enter key: "))) {
* copy it, nul-padded, into the key area
expand_des_key(BUFFER(msgbuf
), p
);
MEMZERO(p
, _PASSWORD_LEN
);
MEMZERO(msgbuf
, sizeof msgbuf
);
* print a warning message and, possibly, terminate
char *s
; /* the message */
(void)sprintf(errmsg
, "%s", s
? s
: strerror(errno
));
* map a hex character to an integer
int c
; /* char to be converted */
int radix
; /* base (2 to 16) */
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);
* convert the key to a bit pattern
expand_des_key(obuf
, ibuf
)
char *obuf
; /* bit pattern */
char *ibuf
; /* the key itself */
register int i
, j
; /* counter in a for loop */
int nbuf
[64]; /* used for hex/key translation */
* leading '0x' or '0X' == hex key
if (ibuf
[0] == '0' && (ibuf
[1] == 'x' || ibuf
[1] == 'X')) {
* now translate it, bombing on any illegal hex digit
for (i
= 0; ibuf
[i
] && i
< 16; i
++)
if ((nbuf
[i
] = hex_to_binary((int) ibuf
[i
], 16)) == -1)
des_error("bad hex digit in key");
((nbuf
[2*i
]&0xf)<<4) | (nbuf
[2*i
+1]&0xf);
/* preserve parity bits */
* leading '0b' or '0B' == binary key
if (ibuf
[0] == '0' && (ibuf
[1] == 'b' || ibuf
[1] == 'B')) {
* now translate it, bombing on any illegal binary digit
for (i
= 0; ibuf
[i
] && i
< 16; i
++)
if ((nbuf
[i
] = hex_to_binary((int) ibuf
[i
], 2)) == -1)
des_error("bad binary digit in key");
obuf
[i
] = (obuf
[i
]<<1)|nbuf
[8*i
+j
];
/* preserve parity bits */
* no special leader -- ASCII
(void)strncpy(obuf
, ibuf
, 8);
* 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.
Desbuf buf
; /* key block */
register int i
, j
; /* counter in a for loop */
register int par
; /* parity counter */
* if the parity is not preserved, flip it
for (i
= 0; i
< 8; i
++) {
if ((bits
[j
]&UCHAR(buf
, i
)) != 0)
UCHAR(buf
, i
) = UCHAR(buf
, i
)&0177;
UCHAR(buf
, i
) = (UCHAR(buf
, i
)&0177)|0200;
* This encrypts using the Cipher Block Chaining mode of DES
cbc_encode(msgbuf
, n
, fp
)
int inverse
= 0; /* 0 to encrypt, 1 to decrypt */
CHAR(msgbuf
, n
) ^= CHAR(ivec
, n
);
DES_XFORM(UBUFFER(msgbuf
));
MEMCPY(BUFFER(ivec
), BUFFER(msgbuf
), 8);
return WRITE(BUFFER(msgbuf
), 8, fp
);
* at EOF or last block -- in either case, the last byte contains
* the character representation of the number of bytes in it
MEMZERO(msgbuf + n, 8 - n);
* Pad the last block randomly
(void)MEMCPY(BUFFER(msgbuf
+ n
), BUFFER(pvec
), 8 - n
);
CHAR(msgbuf
, n
) ^= CHAR(ivec
, n
);
DES_XFORM(UBUFFER(msgbuf
));
return WRITE(BUFFER(msgbuf
), 8, fp
);
* This decrypts using the Cipher Block Chaining mode of DES
char *msgbuf
; /* I/O buffer */
FILE *fp
; /* input file descriptor */
Desbuf ibuf
; /* temp buffer for initialization vector */
register int n
; /* number of bytes actually read */
register int c
; /* used to test for EOF */
int inverse
= 1; /* 0 to encrypt, 1 to decrypt */
if ((n
= READ(BUFFER(msgbuf
), 8, fp
)) == 8) {
MEMCPY(BUFFER(ibuf
), BUFFER(msgbuf
), 8);
DES_XFORM(UBUFFER(msgbuf
));
UCHAR(msgbuf
, c
) ^= UCHAR(ivec
, c
);
MEMCPY(BUFFER(ivec
), BUFFER(ibuf
), 8);
* if the last one, handle it specially
if ((c
= fgetc(fp
)) == EOF
) {
des_error("decryption failed (block corrupted)");
des_error("decryption failed (incomplete block)");
des_error("cannot read file");