Annotation of src/usr.bin/rlogin/des_rw.c, Revision 1.1
1.1 ! deraadt 1: /* $NetBSD: des_rw.c,v 1.2 1995/03/21 07:58:30 cgd Exp $ */
! 2:
! 3: /*-
! 4: * Copyright (c) 1989, 1993
! 5: * The Regents of the University of California. All rights reserved.
! 6: *
! 7: * Redistribution and use in source and binary forms, with or without
! 8: * modification, are permitted provided that the following conditions
! 9: * are met:
! 10: * 1. Redistributions of source code must retain the above copyright
! 11: * notice, this list of conditions and the following disclaimer.
! 12: * 2. Redistributions in binary form must reproduce the above copyright
! 13: * notice, this list of conditions and the following disclaimer in the
! 14: * documentation and/or other materials provided with the distribution.
! 15: * 3. All advertising materials mentioning features or use of this software
! 16: * must display the following acknowledgement:
! 17: * This product includes software developed by the University of
! 18: * California, Berkeley and its contributors.
! 19: * 4. Neither the name of the University nor the names of its contributors
! 20: * may be used to endorse or promote products derived from this software
! 21: * without specific prior written permission.
! 22: *
! 23: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
! 24: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
! 25: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 26: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
! 27: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 28: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 29: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 30: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 31: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 32: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 33: * SUCH DAMAGE.
! 34: */
! 35:
! 36: #ifndef lint
! 37: #if 0
! 38: static char sccsid[] = "@(#)des_rw.c 8.1 (Berkeley) 6/6/93";
! 39: #else
! 40: static char rcsid[] = "$NetBSD: des_rw.c,v 1.2 1995/03/21 07:58:30 cgd Exp $";
! 41: #endif
! 42: #endif /* not lint */
! 43:
! 44: #ifdef CRYPT
! 45: #ifdef KERBEROS
! 46: #include <sys/param.h>
! 47:
! 48: #include <kerberosIV/des.h>
! 49: #include <kerberosIV/krb.h>
! 50:
! 51: #include <stdlib.h>
! 52: #include <string.h>
! 53: #include <time.h>
! 54: #include <unistd.h>
! 55:
! 56: static unsigned char des_inbuf[10240], storage[10240], *store_ptr;
! 57: static bit_64 *key;
! 58: static u_char *key_schedule;
! 59:
! 60: /* XXX these should be in a kerberos include file */
! 61: int krb_net_read __P((int, char *, int));
! 62: #ifdef notdef
! 63: /* XXX too hard to make this work */
! 64: int des_pcbc_encrypt __P((des_cblock *, des_cblock *, long,
! 65: des_key_schedule, des_cblock *, int));
! 66: #endif
! 67:
! 68: /*
! 69: * NB: These routines will not function properly if NBIO
! 70: * is set
! 71: */
! 72:
! 73: /*
! 74: * des_set_key
! 75: *
! 76: * Set des encryption/decryption key for use by the des_read and
! 77: * des_write routines
! 78: *
! 79: * The inkey parameter is actually the DES initial vector,
! 80: * and the insched is the DES Key unwrapped for faster decryption
! 81: */
! 82:
! 83: void
! 84: des_set_key(inkey, insched)
! 85: bit_64 *inkey;
! 86: u_char *insched;
! 87: {
! 88: key = inkey;
! 89: key_schedule = insched;
! 90: }
! 91:
! 92: void
! 93: des_clear_key()
! 94: {
! 95: bzero((char *) key, sizeof(C_Block));
! 96: bzero((char *) key_schedule, sizeof(Key_schedule));
! 97: }
! 98:
! 99:
! 100: int
! 101: des_read(fd, buf, len)
! 102: int fd;
! 103: register char *buf;
! 104: int len;
! 105: {
! 106: int nreturned = 0;
! 107: long net_len, rd_len;
! 108: int nstored = 0;
! 109:
! 110: if (nstored >= len) {
! 111: (void) bcopy(store_ptr, buf, len);
! 112: store_ptr += len;
! 113: nstored -= len;
! 114: return(len);
! 115: } else if (nstored) {
! 116: (void) bcopy(store_ptr, buf, nstored);
! 117: nreturned += nstored;
! 118: buf += nstored;
! 119: len -= nstored;
! 120: nstored = 0;
! 121: }
! 122:
! 123: if (krb_net_read(fd, (char *)&net_len, sizeof(net_len)) !=
! 124: sizeof(net_len)) {
! 125: /* XXX can't read enough, pipe
! 126: must have closed */
! 127: return(0);
! 128: }
! 129: net_len = ntohl(net_len);
! 130: if (net_len <= 0 || net_len > sizeof(des_inbuf)) {
! 131: /* preposterous length; assume out-of-sync; only
! 132: recourse is to close connection, so return 0 */
! 133: return(0);
! 134: }
! 135: /* the writer tells us how much real data we are getting, but
! 136: we need to read the pad bytes (8-byte boundary) */
! 137: rd_len = roundup(net_len, 8);
! 138: if (krb_net_read(fd, (char *)des_inbuf, rd_len) != rd_len) {
! 139: /* pipe must have closed, return 0 */
! 140: return(0);
! 141: }
! 142: (void) des_pcbc_encrypt(des_inbuf, /* inbuf */
! 143: storage, /* outbuf */
! 144: net_len, /* length */
! 145: key_schedule, /* DES key */
! 146: key, /* IV */
! 147: DECRYPT); /* direction */
! 148:
! 149: if(net_len < 8)
! 150: store_ptr = storage + 8 - net_len;
! 151: else
! 152: store_ptr = storage;
! 153:
! 154: nstored = net_len;
! 155: if (nstored > len) {
! 156: (void) bcopy(store_ptr, buf, len);
! 157: nreturned += len;
! 158: store_ptr += len;
! 159: nstored -= len;
! 160: } else {
! 161: (void) bcopy(store_ptr, buf, nstored);
! 162: nreturned += nstored;
! 163: nstored = 0;
! 164: }
! 165:
! 166: return(nreturned);
! 167: }
! 168:
! 169: static unsigned char des_outbuf[10240]; /* > longest write */
! 170:
! 171: int
! 172: des_write(fd, buf, len)
! 173: int fd;
! 174: char *buf;
! 175: int len;
! 176: {
! 177: static int seeded = 0;
! 178: static char garbage_buf[8];
! 179: long net_len, garbage;
! 180:
! 181: if(len < 8) {
! 182: if(!seeded) {
! 183: seeded = 1;
! 184: srandom((int) time((long *)0));
! 185: }
! 186: garbage = random();
! 187: /* insert random garbage */
! 188: (void) bcopy(&garbage, garbage_buf, MIN(sizeof(long),8));
! 189: /* this "right-justifies" the data in the buffer */
! 190: (void) bcopy(buf, garbage_buf + 8 - len, len);
! 191: }
! 192: /* pcbc_encrypt outputs in 8-byte (64 bit) increments */
! 193:
! 194: (void) des_pcbc_encrypt((len < 8) ? garbage_buf : buf,
! 195: des_outbuf,
! 196: (len < 8) ? 8 : len,
! 197: key_schedule, /* DES key */
! 198: key, /* IV */
! 199: ENCRYPT);
! 200:
! 201: /* tell the other end the real amount, but send an 8-byte padded
! 202: packet */
! 203: net_len = htonl(len);
! 204: (void) write(fd, &net_len, sizeof(net_len));
! 205: (void) write(fd, des_outbuf, roundup(len,8));
! 206: return(len);
! 207: }
! 208: #endif /* KERBEROS */
! 209: #endif /* CRYPT */