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