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

1.3     ! deraadt     1: /*     $OpenBSD: des_rw.c,v 1.2 2002/07/15 22:11:34 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.3     ! deraadt    41: static char rcsid[] = "$OpenBSD: des_rw.c,v 1.2 2002/07/15 22:11:34 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
1.3     ! deraadt    82: desrw_set_key(des_cblock *inkey, des_key_schedule *insched)
1.1       deraadt    83: {
                     84:        key = inkey;
                     85:        key_schedule = insched;
                     86:        nstored = 0;
                     87: }
                     88:
                     89: void
1.3     ! deraadt    90: desrw_clear_key(void)
1.1       deraadt    91: {
                     92:        bzero((char *) key, sizeof(C_Block));
                     93:        bzero((char *) key_schedule, sizeof(Key_schedule));
                     94: }
                     95:
                     96:
                     97: int
1.3     ! deraadt    98: des_read(int fd, void *bp, int len)
1.1       deraadt    99: {
                    100:        long net_len, rd_len;
                    101:        int nreturned = 0;
1.2       deraadt   102:        char *buf = bp;
1.1       deraadt   103:
                    104:        if (nstored >= len) {
                    105:                (void) bcopy(store_ptr, buf, len);
                    106:                store_ptr += len;
                    107:                nstored -= len;
                    108:                return(len);
                    109:        } else if (nstored) {
                    110:                (void) bcopy(store_ptr, buf, nstored);
                    111:                nreturned += nstored;
                    112:                buf += nstored;
                    113:                len -= nstored;
                    114:                nstored = 0;
                    115:        }
                    116:
                    117:        if (krb_net_read(fd, (char *)&net_len, sizeof(net_len)) !=
                    118:            sizeof(net_len)) {
                    119:                /* XXX can't read enough, pipe
                    120:                   must have closed */
                    121:                return(0);
                    122:        }
                    123:        net_len = ntohl(net_len);
                    124:        if (net_len <= 0 || net_len > sizeof(des_inbuf)) {
                    125:                /* preposterous length; assume out-of-sync; only
                    126:                   recourse is to close connection, so return 0 */
                    127:                return(0);
                    128:        }
                    129:        /* the writer tells us how much real data we are getting, but
                    130:           we need to read the pad bytes (8-byte boundary) */
                    131:        rd_len = roundup(net_len, 8);
                    132:        if (krb_net_read(fd, (char *)des_inbuf, rd_len) != rd_len) {
                    133:                /* pipe must have closed, return 0 */
                    134:                return(0);
                    135:        }
1.3     ! deraadt   136:        (void) des_pcbc_encrypt((des_cblock *)des_inbuf,
        !           137:            (des_cblock *)storage, rd_len, *key_schedule, key, DECRYPT);
1.1       deraadt   138:
                    139:        if(net_len < 8)
                    140:                store_ptr = storage + 8 - net_len;
                    141:        else
                    142:                store_ptr = storage;
                    143:
                    144:        nstored = net_len;
                    145:        if (nstored > len) {
                    146:                (void) bcopy(store_ptr, buf, len);
                    147:                nreturned += len;
                    148:                store_ptr += len;
                    149:                nstored -= len;
                    150:        } else {
                    151:                (void) bcopy(store_ptr, buf, nstored);
                    152:                nreturned += nstored;
                    153:                nstored = 0;
                    154:        }
                    155:
                    156:        return(nreturned);
                    157: }
                    158:
                    159: static unsigned char   des_outbuf[10240];      /* > longest write */
                    160:
                    161: int
1.3     ! deraadt   162: des_write(int fd, void *bp, int len)
1.1       deraadt   163: {
                    164:        static  int     seeded = 0;
                    165:        static  char    garbage_buf[8];
                    166:        long net_len, garbage;
1.2       deraadt   167:        char *buf = bp;
1.1       deraadt   168:
                    169:        if(len < 8) {
                    170:                if(!seeded) {
                    171:                        seeded = 1;
                    172:                        srandom((int) time(NULL));
                    173:                }
                    174:                garbage = random();
                    175:                /* insert random garbage */
                    176:                (void) bcopy(&garbage, garbage_buf, MIN(sizeof(long),8));
                    177:                /* this "right-justifies" the data in the buffer */
                    178:                (void) bcopy(buf, garbage_buf + 8 - len, len);
                    179:        }
                    180:        /* pcbc_encrypt outputs in 8-byte (64 bit) increments */
                    181:
                    182:        (void) des_pcbc_encrypt((des_cblock *)((len < 8) ? garbage_buf : buf),
1.3     ! deraadt   183:            (des_cblock *)des_outbuf,    (len < 8) ? 8 : len,
        !           184:            *key_schedule, key, ENCRYPT);
1.1       deraadt   185:
                    186:        /* tell the other end the real amount, but send an 8-byte padded
                    187:           packet */
                    188:        net_len = htonl(len);
                    189:        (void) write(fd, &net_len, sizeof(net_len));
                    190:        (void) write(fd, des_outbuf, roundup(len,8));
                    191:        return(len);
                    192: }
                    193: #endif /* KERBEROS */