[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.2

1.2     ! deraadt     1: /*     $OpenBSD: des_rw.c,v 1.1 2002/05/06 22:23:53 deraadt Exp $      */
1.1       deraadt     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
1.2     ! deraadt    41: static char rcsid[] = "$OpenBSD: des_rw.c,v 1.1 2002/05/06 22:23:53 deraadt Exp $";
1.1       deraadt    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);
1.2     ! deraadt    58: int    des_read(int, void *, int);
        !            59: int    des_write(int, void *, int);
1.1       deraadt    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
1.2     ! deraadt   100: des_read(fd, bp, len)
1.1       deraadt   101:        int fd;
1.2     ! deraadt   102:        void *bp;
1.1       deraadt   103:        int len;
                    104: {
                    105:        long net_len, rd_len;
                    106:        int nreturned = 0;
1.2     ! deraadt   107:        char *buf = bp;
1.1       deraadt   108:
                    109:        if (nstored >= len) {
                    110:                (void) bcopy(store_ptr, buf, len);
                    111:                store_ptr += len;
                    112:                nstored -= len;
                    113:                return(len);
                    114:        } else if (nstored) {
                    115:                (void) bcopy(store_ptr, buf, nstored);
                    116:                nreturned += nstored;
                    117:                buf += nstored;
                    118:                len -= nstored;
                    119:                nstored = 0;
                    120:        }
                    121:
                    122:        if (krb_net_read(fd, (char *)&net_len, sizeof(net_len)) !=
                    123:            sizeof(net_len)) {
                    124:                /* XXX can't read enough, pipe
                    125:                   must have closed */
                    126:                return(0);
                    127:        }
                    128:        net_len = ntohl(net_len);
                    129:        if (net_len <= 0 || net_len > sizeof(des_inbuf)) {
                    130:                /* preposterous length; assume out-of-sync; only
                    131:                   recourse is to close connection, so return 0 */
                    132:                return(0);
                    133:        }
                    134:        /* the writer tells us how much real data we are getting, but
                    135:           we need to read the pad bytes (8-byte boundary) */
                    136:        rd_len = roundup(net_len, 8);
                    137:        if (krb_net_read(fd, (char *)des_inbuf, rd_len) != rd_len) {
                    138:                /* pipe must have closed, return 0 */
                    139:                return(0);
                    140:        }
                    141:        (void) des_pcbc_encrypt((des_cblock *)des_inbuf,        /* inbuf */
                    142:                            (des_cblock *)storage,              /* outbuf */
                    143:                            rd_len,                             /* length */
                    144:                            *key_schedule,                      /* DES key */
                    145:                            key,                                /* IV */
                    146:                            DECRYPT);                           /* direction */
                    147:
                    148:        if(net_len < 8)
                    149:                store_ptr = storage + 8 - net_len;
                    150:        else
                    151:                store_ptr = storage;
                    152:
                    153:        nstored = net_len;
                    154:        if (nstored > len) {
                    155:                (void) bcopy(store_ptr, buf, len);
                    156:                nreturned += len;
                    157:                store_ptr += len;
                    158:                nstored -= len;
                    159:        } else {
                    160:                (void) bcopy(store_ptr, buf, nstored);
                    161:                nreturned += nstored;
                    162:                nstored = 0;
                    163:        }
                    164:
                    165:        return(nreturned);
                    166: }
                    167:
                    168: static unsigned char   des_outbuf[10240];      /* > longest write */
                    169:
                    170: int
1.2     ! deraadt   171: des_write(fd, bp, len)
1.1       deraadt   172:        int fd;
1.2     ! deraadt   173:        void *bp;
1.1       deraadt   174:        int len;
                    175: {
                    176:        static  int     seeded = 0;
                    177:        static  char    garbage_buf[8];
                    178:        long net_len, garbage;
1.2     ! deraadt   179:        char *buf = bp;
1.1       deraadt   180:
                    181:        if(len < 8) {
                    182:                if(!seeded) {
                    183:                        seeded = 1;
                    184:                        srandom((int) time(NULL));
                    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((des_cblock *)((len < 8) ? garbage_buf : buf),
                    195:                            (des_cblock *)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 */