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

Annotation of src/usr.bin/rlogin/des_rw.c, Revision 1.7

1.7     ! provos      1: /*     $OpenBSD: des_rw.c,v 1.6 1997/06/24 01:51:13 dgregor 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.7     ! provos     41: static char rcsid[] = "$OpenBSD: des_rw.c,v 1.6 1997/06/24 01:51:13 dgregor Exp $";
1.1       deraadt    42: #endif
                     43: #endif /* not lint */
                     44:
                     45: #ifdef KERBEROS
                     46: #include <sys/param.h>
                     47:
1.7     ! provos     48: #include <des.h>
1.1       deraadt    49: #include <kerberosIV/krb.h>
                     50:
                     51: #include <stdlib.h>
                     52: #include <string.h>
                     53: #include <time.h>
                     54: #include <unistd.h>
                     55:
1.3       tholo      56: static unsigned char   des_inbuf[10240], storage[10240], *store_ptr;
1.5       mickey     57: static des_cblock      *key;
                     58: static des_key_schedule        *key_schedule;
1.1       deraadt    59:
                     60: /*
                     61:  * NB: These routines will not function properly if NBIO
                     62:  *     is set
                     63:  */
                     64:
                     65: /*
                     66:  * des_set_key
                     67:  *
                     68:  * Set des encryption/decryption key for use by the des_read and
                     69:  * des_write routines
                     70:  *
                     71:  * The inkey parameter is actually the DES initial vector,
                     72:  * and the insched is the DES Key unwrapped for faster decryption
                     73:  */
1.3       tholo      74: static int nstored = 0;
1.1       deraadt    75:
1.3       tholo      76: void
                     77: desrw_set_key(inkey, insched)
1.5       mickey     78:        des_cblock      *inkey;
                     79:        des_key_schedule*insched;
1.1       deraadt    80: {
                     81:        key = inkey;
1.3       tholo      82:        key_schedule = insched;
                     83:        nstored = 0;
1.1       deraadt    84: }
                     85:
                     86: void
1.3       tholo      87: desrw_clear_key()
1.1       deraadt    88: {
                     89:        bzero((char *) key, sizeof(C_Block));
                     90:        bzero((char *) key_schedule, sizeof(Key_schedule));
                     91: }
                     92:
                     93:
                     94: int
                     95: des_read(fd, buf, len)
                     96:        int fd;
                     97:        register char *buf;
                     98:        int len;
                     99: {
1.3       tholo     100:        long net_len, rd_len;
1.1       deraadt   101:        int nreturned = 0;
                    102:
                    103:        if (nstored >= len) {
                    104:                (void) bcopy(store_ptr, buf, len);
                    105:                store_ptr += len;
                    106:                nstored -= len;
                    107:                return(len);
                    108:        } else if (nstored) {
                    109:                (void) bcopy(store_ptr, buf, nstored);
                    110:                nreturned += nstored;
                    111:                buf += nstored;
                    112:                len -= nstored;
                    113:                nstored = 0;
                    114:        }
                    115:
                    116:        if (krb_net_read(fd, (char *)&net_len, sizeof(net_len)) !=
                    117:            sizeof(net_len)) {
                    118:                /* XXX can't read enough, pipe
                    119:                   must have closed */
                    120:                return(0);
                    121:        }
                    122:        net_len = ntohl(net_len);
                    123:        if (net_len <= 0 || net_len > sizeof(des_inbuf)) {
                    124:                /* preposterous length; assume out-of-sync; only
                    125:                   recourse is to close connection, so return 0 */
                    126:                return(0);
                    127:        }
                    128:        /* the writer tells us how much real data we are getting, but
                    129:           we need to read the pad bytes (8-byte boundary) */
                    130:        rd_len = roundup(net_len, 8);
                    131:        if (krb_net_read(fd, (char *)des_inbuf, rd_len) != rd_len) {
                    132:                /* pipe must have closed, return 0 */
                    133:                return(0);
                    134:        }
1.3       tholo     135:        (void) des_pcbc_encrypt((des_cblock *)des_inbuf,        /* inbuf */
                    136:                            (des_cblock *)storage,              /* outbuf */
                    137:                            rd_len,                             /* length */
1.5       mickey    138:                            *key_schedule,                      /* DES key */
                    139:                            key,                                /* IV */
1.3       tholo     140:                            DECRYPT);                           /* direction */
1.1       deraadt   141:
                    142:        if(net_len < 8)
                    143:                store_ptr = storage + 8 - net_len;
                    144:        else
                    145:                store_ptr = storage;
                    146:
                    147:        nstored = net_len;
                    148:        if (nstored > len) {
                    149:                (void) bcopy(store_ptr, buf, len);
                    150:                nreturned += len;
                    151:                store_ptr += len;
                    152:                nstored -= len;
                    153:        } else {
                    154:                (void) bcopy(store_ptr, buf, nstored);
                    155:                nreturned += nstored;
                    156:                nstored = 0;
                    157:        }
                    158:
                    159:        return(nreturned);
                    160: }
                    161:
1.3       tholo     162: static unsigned char   des_outbuf[10240];      /* > longest write */
1.1       deraadt   163:
                    164: int
                    165: des_write(fd, buf, len)
                    166:        int fd;
                    167:        char *buf;
                    168:        int len;
                    169: {
                    170:        static  int     seeded = 0;
1.3       tholo     171:        static  char    garbage_buf[8];
1.1       deraadt   172:        long net_len, garbage;
                    173:
                    174:        if(len < 8) {
                    175:                if(!seeded) {
                    176:                        seeded = 1;
1.6       dgregor   177:                        srandom((int) time(NULL));
1.1       deraadt   178:                }
                    179:                garbage = random();
                    180:                /* insert random garbage */
                    181:                (void) bcopy(&garbage, garbage_buf, MIN(sizeof(long),8));
                    182:                /* this "right-justifies" the data in the buffer */
                    183:                (void) bcopy(buf, garbage_buf + 8 - len, len);
                    184:        }
                    185:        /* pcbc_encrypt outputs in 8-byte (64 bit) increments */
                    186:
1.3       tholo     187:        (void) des_pcbc_encrypt((des_cblock *)((len < 8) ? garbage_buf : buf),
                    188:                            (des_cblock *)des_outbuf,
1.1       deraadt   189:                            (len < 8) ? 8 : len,
1.5       mickey    190:                            *key_schedule,              /* DES key */
                    191:                            key,                        /* IV */
1.1       deraadt   192:                            ENCRYPT);
                    193:
                    194:        /* tell the other end the real amount, but send an 8-byte padded
                    195:           packet */
                    196:        net_len = htonl(len);
                    197:        (void) write(fd, &net_len, sizeof(net_len));
                    198:        (void) write(fd, des_outbuf, roundup(len,8));
                    199:        return(len);
                    200: }
                    201: #endif /* KERBEROS */