[BACK]Return to des_rw.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rsh

Annotation of src/usr.bin/rsh/des_rw.c, Revision 1.1

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