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