* Copyright (c) 1989 The Regents of the University of California.
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of California, Berkeley. The name of the
* University may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
static char sccsid
[] = "@(#)des_rw.c 1.7 (Berkeley) 2/6/89";
static unsigned char des_inbuf
[10240], storage
[10240], *store_ptr
;
static u_char
*key_schedule
;
* NB: These routines will not function properly if NBIO
* Set des encryption/decryption key for use by the des_read and
* The inkey parameter is actually the DES initial vector,
* and the insched is the DES Key unwrapped for faster decryption
des_set_key(inkey
, insched
)
bzero((char *) key
, sizeof(C_Block
));
bzero((char *) key_schedule
, sizeof(Key_schedule
));
(void) bcopy(store_ptr
, buf
, len
);
(void) bcopy(store_ptr
, buf
, nstored
);
if (krb_net_read(fd
, &net_len
, sizeof(net_len
)) != sizeof(net_len
)) {
/* XXX can't read enough, pipe
net_len
= ntohl(net_len
);
if (net_len
<= 0 || net_len
> sizeof(des_inbuf
)) {
/* preposterous length; assume out-of-sync; only
recourse is to close connection, so return 0 */
/* the writer tells us how much real data we are getting, but
we need to read the pad bytes (8-byte boundary) */
rd_len
= roundup(net_len
, 8);
if (krb_net_read(fd
, des_inbuf
, rd_len
) != rd_len
) {
/* pipe must have closed, return 0 */
(void) des_pcbc_encrypt(des_inbuf
, /* inbuf */
key_schedule
, /* DES key */
DECRYPT
); /* direction */
store_ptr
= storage
+ 8 - net_len
;
(void) bcopy(store_ptr
, buf
, len
);
(void) bcopy(store_ptr
, buf
, nstored
);
static unsigned char des_outbuf
[10240]; /* > longest write */
static char garbage_buf
[8];
srandom((int) time((long *)0));
/* insert random garbage */
(void) bcopy(&garbage
, garbage_buf
, MIN(sizeof(long),8));
/* this "right-justifies" the data in the buffer */
(void) bcopy(buf
, garbage_buf
+ 8 - len
, len
);
/* pcbc_encrypt outputs in 8-byte (64 bit) increments */
(void) des_pcbc_encrypt((len
< 8) ? garbage_buf
: buf
,
key_schedule
, /* DES key */
/* tell the other end the real amount, but send an 8-byte padded
(void) write(fd
, &net_len
, sizeof(net_len
));
(void) write(fd
, des_outbuf
, roundup(len
,8));