Annotation of 43BSDReno/usr.bin/rlogin/des_rw.c, revision 1.1.1.1

1.1       root        1: /*-
                      2:  * Copyright (c) 1989 The Regents of the University of California.
                      3:  * All rights reserved.
                      4:  *
                      5:  * Redistribution and use in source and binary forms are permitted
                      6:  * provided that: (1) source distributions retain this entire copyright
                      7:  * notice and comment, and (2) distributions including binaries display
                      8:  * the following acknowledgement:  ``This product includes software
                      9:  * developed by the University of California, Berkeley and its contributors''
                     10:  * in the documentation or other materials provided with the distribution
                     11:  * and in all advertising materials mentioning features or use of this
                     12:  * software. Neither the name of the University nor the names of its
                     13:  * contributors may be used to endorse or promote products derived
                     14:  * from this software without specific prior written permission.
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
                     16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
                     17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     18:  */
                     19: 
                     20: #ifndef lint
                     21: static char sccsid[] = "@(#)des_rw.c   5.5 (Berkeley) 6/1/90";
                     22: #endif /* not lint */
                     23: 
                     24: #include <sys/param.h>
                     25: #include <kerberosIV/des.h>
                     26: #include <kerberosIV/krb.h>
                     27: 
                     28: extern long            random();
                     29: static unsigned char   des_inbuf[10240], storage[10240], *store_ptr;
                     30: static bit_64          *key;
                     31: static u_char          *key_schedule;
                     32: 
                     33: /*
                     34:  * NB: These routines will not function properly if NBIO
                     35:  *     is set
                     36:  */
                     37: 
                     38: /*
                     39:  * des_set_key
                     40:  *
                     41:  * Set des encryption/decryption key for use by the des_read and
                     42:  * des_write routines
                     43:  *
                     44:  * The inkey parameter is actually the DES initial vector,
                     45:  * and the insched is the DES Key unwrapped for faster decryption
                     46:  */
                     47: 
                     48: void
                     49: des_set_key(inkey, insched)
                     50:        bit_64          *inkey;
                     51:        u_char          *insched;
                     52: {
                     53:        key = inkey;
                     54:        key_schedule = insched;
                     55: }
                     56: 
                     57: void
                     58: des_clear_key()
                     59: {
                     60:        bzero((char *) key, sizeof(C_Block));
                     61:        bzero((char *) key_schedule, sizeof(Key_schedule));
                     62: }
                     63:        
                     64: 
                     65: int
                     66: des_read(fd, buf, len)
                     67:        int fd;
                     68:        register char *buf;
                     69:        int len;
                     70: {
                     71:        int nreturned = 0;
                     72:        long net_len, rd_len;
                     73:        int nstored = 0;
                     74: 
                     75:        if (nstored >= len) {
                     76:                (void) bcopy(store_ptr, buf, len);
                     77:                store_ptr += len;
                     78:                nstored -= len;
                     79:                return(len);
                     80:        } else if (nstored) {
                     81:                (void) bcopy(store_ptr, buf, nstored);
                     82:                nreturned += nstored;
                     83:                buf += nstored;
                     84:                len -= nstored;
                     85:                nstored = 0;
                     86:        }
                     87:        
                     88:        if (krb_net_read(fd, &net_len, sizeof(net_len)) != sizeof(net_len)) {
                     89:                /* XXX can't read enough, pipe
                     90:                   must have closed */
                     91:                return(0);
                     92:        }
                     93:        net_len = ntohl(net_len);
                     94:        if (net_len <= 0 || net_len > sizeof(des_inbuf)) {
                     95:                /* preposterous length; assume out-of-sync; only
                     96:                   recourse is to close connection, so return 0 */
                     97:                return(0);
                     98:        }
                     99:        /* the writer tells us how much real data we are getting, but
                    100:           we need to read the pad bytes (8-byte boundary) */
                    101:        rd_len = roundup(net_len, 8);
                    102:        if (krb_net_read(fd, des_inbuf, rd_len) != rd_len) {
                    103:                /* pipe must have closed, return 0 */
                    104:                return(0);
                    105:        }
                    106:        (void) des_pcbc_encrypt(des_inbuf,      /* inbuf */
                    107:                            storage,            /* outbuf */
                    108:                            net_len,            /* length */
                    109:                            key_schedule,       /* DES key */
                    110:                            key,                /* IV */
                    111:                            DECRYPT);           /* direction */
                    112: 
                    113:        if(net_len < 8)
                    114:                store_ptr = storage + 8 - net_len;
                    115:        else
                    116:                store_ptr = storage;
                    117: 
                    118:        nstored = net_len;
                    119:        if (nstored > len) {
                    120:                (void) bcopy(store_ptr, buf, len);
                    121:                nreturned += len;
                    122:                store_ptr += len;
                    123:                nstored -= len;
                    124:        } else {
                    125:                (void) bcopy(store_ptr, buf, nstored);
                    126:                nreturned += nstored;
                    127:                nstored = 0;
                    128:        }
                    129:        
                    130:        return(nreturned);
                    131: }
                    132: 
                    133: static unsigned char des_outbuf[10240];        /* > longest write */
                    134: 
                    135: int
                    136: des_write(fd, buf, len)
                    137:        int fd;
                    138:        char *buf;
                    139:        int len;
                    140: {
                    141:        static  int     seeded = 0;
                    142:        static  char    garbage_buf[8];
                    143:        long net_len, garbage;
                    144: 
                    145:        if(len < 8) {
                    146:                if(!seeded) {
                    147:                        seeded = 1;
                    148:                        srandom((int) time((long *)0));
                    149:                }
                    150:                garbage = random();
                    151:                /* insert random garbage */
                    152:                (void) bcopy(&garbage, garbage_buf, MIN(sizeof(long),8));
                    153:                /* this "right-justifies" the data in the buffer */
                    154:                (void) bcopy(buf, garbage_buf + 8 - len, len);
                    155:        }
                    156:        /* pcbc_encrypt outputs in 8-byte (64 bit) increments */
                    157: 
                    158:        (void) des_pcbc_encrypt((len < 8) ? garbage_buf : buf,
                    159:                            des_outbuf,
                    160:                            (len < 8) ? 8 : len,
                    161:                            key_schedule,       /* DES key */
                    162:                            key,                /* IV */
                    163:                            ENCRYPT);
                    164: 
                    165:        /* tell the other end the real amount, but send an 8-byte padded
                    166:           packet */
                    167:        net_len = htonl(len);
                    168:        (void) write(fd, &net_len, sizeof(net_len));
                    169:        (void) write(fd, des_outbuf, roundup(len,8));
                    170:        return(len);
                    171: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.