1fdad9c93SAxel Dörfler /*-
2fdad9c93SAxel Dörfler * Copyright (c) 1991, 1993
3fdad9c93SAxel Dörfler * The Regents of the University of California. All rights reserved.
4fdad9c93SAxel Dörfler *
5fdad9c93SAxel Dörfler * Redistribution and use in source and binary forms, with or without
6fdad9c93SAxel Dörfler * modification, are permitted provided that the following conditions
7fdad9c93SAxel Dörfler * are met:
8fdad9c93SAxel Dörfler * 1. Redistributions of source code must retain the above copyright
9fdad9c93SAxel Dörfler * notice, this list of conditions and the following disclaimer.
10fdad9c93SAxel Dörfler * 2. Redistributions in binary form must reproduce the above copyright
11fdad9c93SAxel Dörfler * notice, this list of conditions and the following disclaimer in the
12fdad9c93SAxel Dörfler * documentation and/or other materials provided with the distribution.
13*6b99b206SAugustin Cavalier * 3. Neither the name of the University nor the names of its contributors
14fdad9c93SAxel Dörfler * may be used to endorse or promote products derived from this software
15fdad9c93SAxel Dörfler * without specific prior written permission.
16fdad9c93SAxel Dörfler *
17fdad9c93SAxel Dörfler * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18fdad9c93SAxel Dörfler * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19fdad9c93SAxel Dörfler * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20fdad9c93SAxel Dörfler * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21fdad9c93SAxel Dörfler * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22fdad9c93SAxel Dörfler * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23fdad9c93SAxel Dörfler * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24fdad9c93SAxel Dörfler * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25fdad9c93SAxel Dörfler * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26fdad9c93SAxel Dörfler * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27fdad9c93SAxel Dörfler * SUCH DAMAGE.
28fdad9c93SAxel Dörfler */
29fdad9c93SAxel Dörfler
30fdad9c93SAxel Dörfler #if 0
31fdad9c93SAxel Dörfler #ifndef lint
32fdad9c93SAxel Dörfler static const char sccsid[] = "@(#)enc_des.c 8.3 (Berkeley) 5/30/95";
33fdad9c93SAxel Dörfler #endif /* not lint */
34fdad9c93SAxel Dörfler #endif
35fdad9c93SAxel Dörfler #include <sys/cdefs.h>
36*6b99b206SAugustin Cavalier __FBSDID("$FreeBSD$");
37fdad9c93SAxel Dörfler
38fdad9c93SAxel Dörfler #ifdef ENCRYPTION
39fdad9c93SAxel Dörfler # ifdef AUTHENTICATION
40fdad9c93SAxel Dörfler #include <arpa/telnet.h>
41fdad9c93SAxel Dörfler #include <openssl/des.h>
42fdad9c93SAxel Dörfler #include <stdio.h>
43fdad9c93SAxel Dörfler #include <stdlib.h>
44fdad9c93SAxel Dörfler #include <string.h>
45fdad9c93SAxel Dörfler
46fdad9c93SAxel Dörfler #include "encrypt.h"
47fdad9c93SAxel Dörfler #include "key-proto.h"
48fdad9c93SAxel Dörfler #include "misc-proto.h"
49fdad9c93SAxel Dörfler
50fdad9c93SAxel Dörfler extern int encrypt_debug_mode;
51fdad9c93SAxel Dörfler
52fdad9c93SAxel Dörfler #define CFB 0
53fdad9c93SAxel Dörfler #define OFB 1
54fdad9c93SAxel Dörfler
55fdad9c93SAxel Dörfler #define NO_SEND_IV 1
56fdad9c93SAxel Dörfler #define NO_RECV_IV 2
57fdad9c93SAxel Dörfler #define NO_KEYID 4
58fdad9c93SAxel Dörfler #define IN_PROGRESS (NO_SEND_IV|NO_RECV_IV|NO_KEYID)
59fdad9c93SAxel Dörfler #define SUCCESS 0
60fdad9c93SAxel Dörfler #define FAILED -1
61fdad9c93SAxel Dörfler
62fdad9c93SAxel Dörfler
63fdad9c93SAxel Dörfler struct fb {
64fdad9c93SAxel Dörfler Block krbdes_key;
65fdad9c93SAxel Dörfler Schedule krbdes_sched;
66fdad9c93SAxel Dörfler Block temp_feed;
67fdad9c93SAxel Dörfler unsigned char fb_feed[64];
68fdad9c93SAxel Dörfler int need_start;
69fdad9c93SAxel Dörfler int state[2];
70fdad9c93SAxel Dörfler int keyid[2];
71fdad9c93SAxel Dörfler struct stinfo {
72fdad9c93SAxel Dörfler Block str_output;
73fdad9c93SAxel Dörfler Block str_feed;
74fdad9c93SAxel Dörfler Block str_iv;
75fdad9c93SAxel Dörfler Block str_ikey;
76fdad9c93SAxel Dörfler Schedule str_sched;
77fdad9c93SAxel Dörfler int str_index;
78fdad9c93SAxel Dörfler int str_flagshift;
79fdad9c93SAxel Dörfler } streams[2];
80fdad9c93SAxel Dörfler };
81fdad9c93SAxel Dörfler
82fdad9c93SAxel Dörfler static struct fb fb[2];
83fdad9c93SAxel Dörfler
84fdad9c93SAxel Dörfler struct keyidlist {
85fdad9c93SAxel Dörfler const char *keyid;
86fdad9c93SAxel Dörfler int keyidlen;
87fdad9c93SAxel Dörfler char *key;
88fdad9c93SAxel Dörfler int keylen;
89fdad9c93SAxel Dörfler int flags;
90fdad9c93SAxel Dörfler } keyidlist [] = {
91fdad9c93SAxel Dörfler { "\0", 1, 0, 0, 0 }, /* default key of zero */
92fdad9c93SAxel Dörfler { 0, 0, 0, 0, 0 }
93fdad9c93SAxel Dörfler };
94fdad9c93SAxel Dörfler
95fdad9c93SAxel Dörfler #define KEYFLAG_MASK 03
96fdad9c93SAxel Dörfler
97fdad9c93SAxel Dörfler #define KEYFLAG_NOINIT 00
98fdad9c93SAxel Dörfler #define KEYFLAG_INIT 01
99fdad9c93SAxel Dörfler #define KEYFLAG_OK 02
100fdad9c93SAxel Dörfler #define KEYFLAG_BAD 03
101fdad9c93SAxel Dörfler
102fdad9c93SAxel Dörfler #define KEYFLAG_SHIFT 2
103fdad9c93SAxel Dörfler
104fdad9c93SAxel Dörfler #define SHIFT_VAL(a,b) (KEYFLAG_SHIFT*((a)+((b)*2)))
105fdad9c93SAxel Dörfler
106fdad9c93SAxel Dörfler #define FB64_IV 1
107fdad9c93SAxel Dörfler #define FB64_IV_OK 2
108fdad9c93SAxel Dörfler #define FB64_IV_BAD 3
109fdad9c93SAxel Dörfler
110fdad9c93SAxel Dörfler
111fdad9c93SAxel Dörfler void fb64_stream_iv(Block, struct stinfo *);
112fdad9c93SAxel Dörfler void fb64_init(struct fb *);
113fdad9c93SAxel Dörfler static int fb64_start(struct fb *, int, int);
114fdad9c93SAxel Dörfler int fb64_is(unsigned char *, int, struct fb *);
115fdad9c93SAxel Dörfler int fb64_reply(unsigned char *, int, struct fb *);
116fdad9c93SAxel Dörfler static void fb64_session(Session_Key *, int, struct fb *);
117fdad9c93SAxel Dörfler void fb64_stream_key(Block, struct stinfo *);
118fdad9c93SAxel Dörfler int fb64_keyid(int, unsigned char *, int *, struct fb *);
119fdad9c93SAxel Dörfler
120fdad9c93SAxel Dörfler void
cfb64_init(int server __unused)121fdad9c93SAxel Dörfler cfb64_init(int server __unused)
122fdad9c93SAxel Dörfler {
123fdad9c93SAxel Dörfler fb64_init(&fb[CFB]);
124fdad9c93SAxel Dörfler fb[CFB].fb_feed[4] = ENCTYPE_DES_CFB64;
125fdad9c93SAxel Dörfler fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, CFB);
126fdad9c93SAxel Dörfler fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, CFB);
127fdad9c93SAxel Dörfler }
128fdad9c93SAxel Dörfler
129fdad9c93SAxel Dörfler void
ofb64_init(int server __unused)130fdad9c93SAxel Dörfler ofb64_init(int server __unused)
131fdad9c93SAxel Dörfler {
132fdad9c93SAxel Dörfler fb64_init(&fb[OFB]);
133fdad9c93SAxel Dörfler fb[OFB].fb_feed[4] = ENCTYPE_DES_OFB64;
134fdad9c93SAxel Dörfler fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, OFB);
135fdad9c93SAxel Dörfler fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, OFB);
136fdad9c93SAxel Dörfler }
137fdad9c93SAxel Dörfler
138fdad9c93SAxel Dörfler void
fb64_init(struct fb * fbp)139fdad9c93SAxel Dörfler fb64_init(struct fb *fbp)
140fdad9c93SAxel Dörfler {
141fdad9c93SAxel Dörfler memset((void *)fbp, 0, sizeof(*fbp));
142fdad9c93SAxel Dörfler fbp->state[0] = fbp->state[1] = FAILED;
143fdad9c93SAxel Dörfler fbp->fb_feed[0] = IAC;
144fdad9c93SAxel Dörfler fbp->fb_feed[1] = SB;
145fdad9c93SAxel Dörfler fbp->fb_feed[2] = TELOPT_ENCRYPT;
146fdad9c93SAxel Dörfler fbp->fb_feed[3] = ENCRYPT_IS;
147fdad9c93SAxel Dörfler }
148fdad9c93SAxel Dörfler
149fdad9c93SAxel Dörfler /*
150fdad9c93SAxel Dörfler * Returns:
151fdad9c93SAxel Dörfler * -1: some error. Negotiation is done, encryption not ready.
152fdad9c93SAxel Dörfler * 0: Successful, initial negotiation all done.
153fdad9c93SAxel Dörfler * 1: successful, negotiation not done yet.
154fdad9c93SAxel Dörfler * 2: Not yet. Other things (like getting the key from
155fdad9c93SAxel Dörfler * Kerberos) have to happen before we can continue.
156fdad9c93SAxel Dörfler */
157fdad9c93SAxel Dörfler int
cfb64_start(int dir,int server)158fdad9c93SAxel Dörfler cfb64_start(int dir, int server)
159fdad9c93SAxel Dörfler {
160fdad9c93SAxel Dörfler return(fb64_start(&fb[CFB], dir, server));
161fdad9c93SAxel Dörfler }
162fdad9c93SAxel Dörfler
163fdad9c93SAxel Dörfler int
ofb64_start(int dir,int server)164fdad9c93SAxel Dörfler ofb64_start(int dir, int server)
165fdad9c93SAxel Dörfler {
166fdad9c93SAxel Dörfler return(fb64_start(&fb[OFB], dir, server));
167fdad9c93SAxel Dörfler }
168fdad9c93SAxel Dörfler
169fdad9c93SAxel Dörfler static int
fb64_start(struct fb * fbp,int dir,int server __unused)170fdad9c93SAxel Dörfler fb64_start(struct fb *fbp, int dir, int server __unused)
171fdad9c93SAxel Dörfler {
172fdad9c93SAxel Dörfler size_t x;
173fdad9c93SAxel Dörfler unsigned char *p;
174fdad9c93SAxel Dörfler int state;
175fdad9c93SAxel Dörfler
176fdad9c93SAxel Dörfler switch (dir) {
177fdad9c93SAxel Dörfler case DIR_DECRYPT:
178fdad9c93SAxel Dörfler /*
179fdad9c93SAxel Dörfler * This is simply a request to have the other side
180fdad9c93SAxel Dörfler * start output (our input). He will negotiate an
181fdad9c93SAxel Dörfler * IV so we need not look for it.
182fdad9c93SAxel Dörfler */
183fdad9c93SAxel Dörfler state = fbp->state[dir-1];
184fdad9c93SAxel Dörfler if (state == FAILED)
185fdad9c93SAxel Dörfler state = IN_PROGRESS;
186fdad9c93SAxel Dörfler break;
187fdad9c93SAxel Dörfler
188fdad9c93SAxel Dörfler case DIR_ENCRYPT:
189fdad9c93SAxel Dörfler state = fbp->state[dir-1];
190fdad9c93SAxel Dörfler if (state == FAILED)
191fdad9c93SAxel Dörfler state = IN_PROGRESS;
192fdad9c93SAxel Dörfler else if ((state & NO_SEND_IV) == 0)
193fdad9c93SAxel Dörfler break;
194fdad9c93SAxel Dörfler
195fdad9c93SAxel Dörfler if (!VALIDKEY(fbp->krbdes_key)) {
196fdad9c93SAxel Dörfler fbp->need_start = 1;
197fdad9c93SAxel Dörfler break;
198fdad9c93SAxel Dörfler }
199fdad9c93SAxel Dörfler state &= ~NO_SEND_IV;
200fdad9c93SAxel Dörfler state |= NO_RECV_IV;
201fdad9c93SAxel Dörfler if (encrypt_debug_mode)
202fdad9c93SAxel Dörfler printf("Creating new feed\r\n");
203fdad9c93SAxel Dörfler /*
204fdad9c93SAxel Dörfler * Create a random feed and send it over.
205fdad9c93SAxel Dörfler */
206*6b99b206SAugustin Cavalier DES_random_key((Block *)fbp->temp_feed);
207*6b99b206SAugustin Cavalier DES_ecb_encrypt((Block *)fbp->temp_feed, (Block *)fbp->temp_feed,
208*6b99b206SAugustin Cavalier &fbp->krbdes_sched, 1);
209fdad9c93SAxel Dörfler p = fbp->fb_feed + 3;
210fdad9c93SAxel Dörfler *p++ = ENCRYPT_IS;
211fdad9c93SAxel Dörfler p++;
212fdad9c93SAxel Dörfler *p++ = FB64_IV;
213fdad9c93SAxel Dörfler for (x = 0; x < sizeof(Block); ++x) {
214fdad9c93SAxel Dörfler if ((*p++ = fbp->temp_feed[x]) == IAC)
215fdad9c93SAxel Dörfler *p++ = IAC;
216fdad9c93SAxel Dörfler }
217fdad9c93SAxel Dörfler *p++ = IAC;
218fdad9c93SAxel Dörfler *p++ = SE;
219fdad9c93SAxel Dörfler printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
220fdad9c93SAxel Dörfler net_write(fbp->fb_feed, p - fbp->fb_feed);
221fdad9c93SAxel Dörfler break;
222fdad9c93SAxel Dörfler default:
223fdad9c93SAxel Dörfler return(FAILED);
224fdad9c93SAxel Dörfler }
225fdad9c93SAxel Dörfler return(fbp->state[dir-1] = state);
226fdad9c93SAxel Dörfler }
227fdad9c93SAxel Dörfler
228fdad9c93SAxel Dörfler /*
229fdad9c93SAxel Dörfler * Returns:
230fdad9c93SAxel Dörfler * -1: some error. Negotiation is done, encryption not ready.
231fdad9c93SAxel Dörfler * 0: Successful, initial negotiation all done.
232fdad9c93SAxel Dörfler * 1: successful, negotiation not done yet.
233fdad9c93SAxel Dörfler */
234fdad9c93SAxel Dörfler int
cfb64_is(unsigned char * data,int cnt)235fdad9c93SAxel Dörfler cfb64_is(unsigned char *data, int cnt)
236fdad9c93SAxel Dörfler {
237fdad9c93SAxel Dörfler return(fb64_is(data, cnt, &fb[CFB]));
238fdad9c93SAxel Dörfler }
239fdad9c93SAxel Dörfler
240fdad9c93SAxel Dörfler int
ofb64_is(unsigned char * data,int cnt)241fdad9c93SAxel Dörfler ofb64_is(unsigned char *data, int cnt)
242fdad9c93SAxel Dörfler {
243fdad9c93SAxel Dörfler return(fb64_is(data, cnt, &fb[OFB]));
244fdad9c93SAxel Dörfler }
245fdad9c93SAxel Dörfler
246fdad9c93SAxel Dörfler int
fb64_is(unsigned char * data,int cnt,struct fb * fbp)247fdad9c93SAxel Dörfler fb64_is(unsigned char *data, int cnt, struct fb *fbp)
248fdad9c93SAxel Dörfler {
249fdad9c93SAxel Dörfler unsigned char *p;
250fdad9c93SAxel Dörfler int state = fbp->state[DIR_DECRYPT-1];
251fdad9c93SAxel Dörfler
252fdad9c93SAxel Dörfler if (cnt-- < 1)
253fdad9c93SAxel Dörfler goto failure;
254fdad9c93SAxel Dörfler
255fdad9c93SAxel Dörfler switch (*data++) {
256fdad9c93SAxel Dörfler case FB64_IV:
257fdad9c93SAxel Dörfler if (cnt != sizeof(Block)) {
258fdad9c93SAxel Dörfler if (encrypt_debug_mode)
259fdad9c93SAxel Dörfler printf("CFB64: initial vector failed on size\r\n");
260fdad9c93SAxel Dörfler state = FAILED;
261fdad9c93SAxel Dörfler goto failure;
262fdad9c93SAxel Dörfler }
263fdad9c93SAxel Dörfler
264fdad9c93SAxel Dörfler if (encrypt_debug_mode)
265fdad9c93SAxel Dörfler printf("CFB64: initial vector received\r\n");
266fdad9c93SAxel Dörfler
267fdad9c93SAxel Dörfler if (encrypt_debug_mode)
268fdad9c93SAxel Dörfler printf("Initializing Decrypt stream\r\n");
269fdad9c93SAxel Dörfler
270fdad9c93SAxel Dörfler fb64_stream_iv((void *)data, &fbp->streams[DIR_DECRYPT-1]);
271fdad9c93SAxel Dörfler
272fdad9c93SAxel Dörfler p = fbp->fb_feed + 3;
273fdad9c93SAxel Dörfler *p++ = ENCRYPT_REPLY;
274fdad9c93SAxel Dörfler p++;
275fdad9c93SAxel Dörfler *p++ = FB64_IV_OK;
276fdad9c93SAxel Dörfler *p++ = IAC;
277fdad9c93SAxel Dörfler *p++ = SE;
278fdad9c93SAxel Dörfler printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
279fdad9c93SAxel Dörfler net_write(fbp->fb_feed, p - fbp->fb_feed);
280fdad9c93SAxel Dörfler
281fdad9c93SAxel Dörfler state = fbp->state[DIR_DECRYPT-1] = IN_PROGRESS;
282fdad9c93SAxel Dörfler break;
283fdad9c93SAxel Dörfler
284fdad9c93SAxel Dörfler default:
285fdad9c93SAxel Dörfler if (encrypt_debug_mode) {
286fdad9c93SAxel Dörfler printf("Unknown option type: %d\r\n", *(data-1));
287fdad9c93SAxel Dörfler printd(data, cnt);
288fdad9c93SAxel Dörfler printf("\r\n");
289fdad9c93SAxel Dörfler }
290fdad9c93SAxel Dörfler /* FALL THROUGH */
291fdad9c93SAxel Dörfler failure:
292fdad9c93SAxel Dörfler /*
293fdad9c93SAxel Dörfler * We failed. Send an FB64_IV_BAD option
294fdad9c93SAxel Dörfler * to the other side so it will know that
295fdad9c93SAxel Dörfler * things failed.
296fdad9c93SAxel Dörfler */
297fdad9c93SAxel Dörfler p = fbp->fb_feed + 3;
298fdad9c93SAxel Dörfler *p++ = ENCRYPT_REPLY;
299fdad9c93SAxel Dörfler p++;
300fdad9c93SAxel Dörfler *p++ = FB64_IV_BAD;
301fdad9c93SAxel Dörfler *p++ = IAC;
302fdad9c93SAxel Dörfler *p++ = SE;
303fdad9c93SAxel Dörfler printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
304fdad9c93SAxel Dörfler net_write(fbp->fb_feed, p - fbp->fb_feed);
305fdad9c93SAxel Dörfler
306fdad9c93SAxel Dörfler break;
307fdad9c93SAxel Dörfler }
308fdad9c93SAxel Dörfler return(fbp->state[DIR_DECRYPT-1] = state);
309fdad9c93SAxel Dörfler }
310fdad9c93SAxel Dörfler
311fdad9c93SAxel Dörfler /*
312fdad9c93SAxel Dörfler * Returns:
313fdad9c93SAxel Dörfler * -1: some error. Negotiation is done, encryption not ready.
314fdad9c93SAxel Dörfler * 0: Successful, initial negotiation all done.
315fdad9c93SAxel Dörfler * 1: successful, negotiation not done yet.
316fdad9c93SAxel Dörfler */
317fdad9c93SAxel Dörfler int
cfb64_reply(unsigned char * data,int cnt)318fdad9c93SAxel Dörfler cfb64_reply(unsigned char *data, int cnt)
319fdad9c93SAxel Dörfler {
320fdad9c93SAxel Dörfler return(fb64_reply(data, cnt, &fb[CFB]));
321fdad9c93SAxel Dörfler }
322fdad9c93SAxel Dörfler
323fdad9c93SAxel Dörfler int
ofb64_reply(unsigned char * data,int cnt)324fdad9c93SAxel Dörfler ofb64_reply(unsigned char *data, int cnt)
325fdad9c93SAxel Dörfler {
326fdad9c93SAxel Dörfler return(fb64_reply(data, cnt, &fb[OFB]));
327fdad9c93SAxel Dörfler }
328fdad9c93SAxel Dörfler
329fdad9c93SAxel Dörfler int
fb64_reply(unsigned char * data,int cnt,struct fb * fbp)330fdad9c93SAxel Dörfler fb64_reply(unsigned char *data, int cnt, struct fb *fbp)
331fdad9c93SAxel Dörfler {
332fdad9c93SAxel Dörfler int state = fbp->state[DIR_ENCRYPT-1];
333fdad9c93SAxel Dörfler
334fdad9c93SAxel Dörfler if (cnt-- < 1)
335fdad9c93SAxel Dörfler goto failure;
336fdad9c93SAxel Dörfler
337fdad9c93SAxel Dörfler switch (*data++) {
338fdad9c93SAxel Dörfler case FB64_IV_OK:
339fdad9c93SAxel Dörfler fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
340fdad9c93SAxel Dörfler if (state == FAILED)
341fdad9c93SAxel Dörfler state = IN_PROGRESS;
342fdad9c93SAxel Dörfler state &= ~NO_RECV_IV;
343fdad9c93SAxel Dörfler encrypt_send_keyid(DIR_ENCRYPT, "\0", 1, 1);
344fdad9c93SAxel Dörfler break;
345fdad9c93SAxel Dörfler
346fdad9c93SAxel Dörfler case FB64_IV_BAD:
347fdad9c93SAxel Dörfler memset(fbp->temp_feed, 0, sizeof(Block));
348fdad9c93SAxel Dörfler fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
349fdad9c93SAxel Dörfler state = FAILED;
350fdad9c93SAxel Dörfler break;
351fdad9c93SAxel Dörfler
352fdad9c93SAxel Dörfler default:
353fdad9c93SAxel Dörfler if (encrypt_debug_mode) {
354fdad9c93SAxel Dörfler printf("Unknown option type: %d\r\n", data[-1]);
355fdad9c93SAxel Dörfler printd(data, cnt);
356fdad9c93SAxel Dörfler printf("\r\n");
357fdad9c93SAxel Dörfler }
358fdad9c93SAxel Dörfler /* FALL THROUGH */
359fdad9c93SAxel Dörfler failure:
360fdad9c93SAxel Dörfler state = FAILED;
361fdad9c93SAxel Dörfler break;
362fdad9c93SAxel Dörfler }
363fdad9c93SAxel Dörfler return(fbp->state[DIR_ENCRYPT-1] = state);
364fdad9c93SAxel Dörfler }
365fdad9c93SAxel Dörfler
366fdad9c93SAxel Dörfler void
cfb64_session(Session_Key * key,int server)367fdad9c93SAxel Dörfler cfb64_session(Session_Key *key, int server)
368fdad9c93SAxel Dörfler {
369fdad9c93SAxel Dörfler fb64_session(key, server, &fb[CFB]);
370fdad9c93SAxel Dörfler }
371fdad9c93SAxel Dörfler
372fdad9c93SAxel Dörfler void
ofb64_session(Session_Key * key,int server)373fdad9c93SAxel Dörfler ofb64_session(Session_Key *key, int server)
374fdad9c93SAxel Dörfler {
375fdad9c93SAxel Dörfler fb64_session(key, server, &fb[OFB]);
376fdad9c93SAxel Dörfler }
377fdad9c93SAxel Dörfler
378fdad9c93SAxel Dörfler static void
fb64_session(Session_Key * key,int server,struct fb * fbp)379fdad9c93SAxel Dörfler fb64_session(Session_Key *key, int server, struct fb *fbp)
380fdad9c93SAxel Dörfler {
381fdad9c93SAxel Dörfler if (!key || key->type != SK_DES) {
382fdad9c93SAxel Dörfler if (encrypt_debug_mode)
383fdad9c93SAxel Dörfler printf("Can't set krbdes's session key (%d != %d)\r\n",
384fdad9c93SAxel Dörfler key ? key->type : -1, SK_DES);
385fdad9c93SAxel Dörfler return;
386fdad9c93SAxel Dörfler }
387fdad9c93SAxel Dörfler memmove((void *)fbp->krbdes_key, (void *)key->data, sizeof(Block));
388fdad9c93SAxel Dörfler
389fdad9c93SAxel Dörfler fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_ENCRYPT-1]);
390fdad9c93SAxel Dörfler fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_DECRYPT-1]);
391fdad9c93SAxel Dörfler
392*6b99b206SAugustin Cavalier DES_key_sched((Block *)fbp->krbdes_key, &fbp->krbdes_sched);
393fdad9c93SAxel Dörfler /*
394fdad9c93SAxel Dörfler * Now look to see if krbdes_start() was was waiting for
395fdad9c93SAxel Dörfler * the key to show up. If so, go ahead an call it now
396fdad9c93SAxel Dörfler * that we have the key.
397fdad9c93SAxel Dörfler */
398fdad9c93SAxel Dörfler if (fbp->need_start) {
399fdad9c93SAxel Dörfler fbp->need_start = 0;
400fdad9c93SAxel Dörfler fb64_start(fbp, DIR_ENCRYPT, server);
401fdad9c93SAxel Dörfler }
402fdad9c93SAxel Dörfler }
403fdad9c93SAxel Dörfler
404fdad9c93SAxel Dörfler /*
405fdad9c93SAxel Dörfler * We only accept a keyid of 0. If we get a keyid of
406fdad9c93SAxel Dörfler * 0, then mark the state as SUCCESS.
407fdad9c93SAxel Dörfler */
408fdad9c93SAxel Dörfler int
cfb64_keyid(int dir,unsigned char * kp,int * lenp)409fdad9c93SAxel Dörfler cfb64_keyid(int dir, unsigned char *kp, int *lenp)
410fdad9c93SAxel Dörfler {
411fdad9c93SAxel Dörfler return(fb64_keyid(dir, kp, lenp, &fb[CFB]));
412fdad9c93SAxel Dörfler }
413fdad9c93SAxel Dörfler
414fdad9c93SAxel Dörfler int
ofb64_keyid(int dir,unsigned char * kp,int * lenp)415fdad9c93SAxel Dörfler ofb64_keyid(int dir, unsigned char *kp, int *lenp)
416fdad9c93SAxel Dörfler {
417fdad9c93SAxel Dörfler return(fb64_keyid(dir, kp, lenp, &fb[OFB]));
418fdad9c93SAxel Dörfler }
419fdad9c93SAxel Dörfler
420fdad9c93SAxel Dörfler int
fb64_keyid(int dir,unsigned char * kp,int * lenp,struct fb * fbp)421fdad9c93SAxel Dörfler fb64_keyid(int dir, unsigned char *kp, int *lenp, struct fb *fbp)
422fdad9c93SAxel Dörfler {
423fdad9c93SAxel Dörfler int state = fbp->state[dir-1];
424fdad9c93SAxel Dörfler
425fdad9c93SAxel Dörfler if (*lenp != 1 || (*kp != '\0')) {
426fdad9c93SAxel Dörfler *lenp = 0;
427fdad9c93SAxel Dörfler return(state);
428fdad9c93SAxel Dörfler }
429fdad9c93SAxel Dörfler
430fdad9c93SAxel Dörfler if (state == FAILED)
431fdad9c93SAxel Dörfler state = IN_PROGRESS;
432fdad9c93SAxel Dörfler
433fdad9c93SAxel Dörfler state &= ~NO_KEYID;
434fdad9c93SAxel Dörfler
435fdad9c93SAxel Dörfler return(fbp->state[dir-1] = state);
436fdad9c93SAxel Dörfler }
437fdad9c93SAxel Dörfler
438fdad9c93SAxel Dörfler void
fb64_printsub(unsigned char * data,int cnt,unsigned char * buf,int buflen,const char * type)439fdad9c93SAxel Dörfler fb64_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen, const char *type)
440fdad9c93SAxel Dörfler {
441fdad9c93SAxel Dörfler char lbuf[32];
442fdad9c93SAxel Dörfler int i;
443fdad9c93SAxel Dörfler char *cp;
444fdad9c93SAxel Dörfler
445fdad9c93SAxel Dörfler buf[buflen-1] = '\0'; /* make sure it's NULL terminated */
446fdad9c93SAxel Dörfler buflen -= 1;
447fdad9c93SAxel Dörfler
448fdad9c93SAxel Dörfler switch(data[2]) {
449fdad9c93SAxel Dörfler case FB64_IV:
450fdad9c93SAxel Dörfler sprintf(lbuf, "%s_IV", type);
451fdad9c93SAxel Dörfler cp = lbuf;
452fdad9c93SAxel Dörfler goto common;
453fdad9c93SAxel Dörfler
454fdad9c93SAxel Dörfler case FB64_IV_OK:
455fdad9c93SAxel Dörfler sprintf(lbuf, "%s_IV_OK", type);
456fdad9c93SAxel Dörfler cp = lbuf;
457fdad9c93SAxel Dörfler goto common;
458fdad9c93SAxel Dörfler
459fdad9c93SAxel Dörfler case FB64_IV_BAD:
460fdad9c93SAxel Dörfler sprintf(lbuf, "%s_IV_BAD", type);
461fdad9c93SAxel Dörfler cp = lbuf;
462fdad9c93SAxel Dörfler goto common;
463fdad9c93SAxel Dörfler
464fdad9c93SAxel Dörfler default:
465fdad9c93SAxel Dörfler sprintf(lbuf, " %d (unknown)", data[2]);
466fdad9c93SAxel Dörfler cp = lbuf;
467fdad9c93SAxel Dörfler common:
468fdad9c93SAxel Dörfler for (; (buflen > 0) && (*buf = *cp++); buf++)
469fdad9c93SAxel Dörfler buflen--;
470fdad9c93SAxel Dörfler for (i = 3; i < cnt; i++) {
471fdad9c93SAxel Dörfler sprintf(lbuf, " %d", data[i]);
472fdad9c93SAxel Dörfler for (cp = lbuf; (buflen > 0) && (*buf = *cp++); buf++)
473fdad9c93SAxel Dörfler buflen--;
474fdad9c93SAxel Dörfler }
475fdad9c93SAxel Dörfler break;
476fdad9c93SAxel Dörfler }
477fdad9c93SAxel Dörfler }
478fdad9c93SAxel Dörfler
479fdad9c93SAxel Dörfler void
cfb64_printsub(unsigned char * data,int cnt,unsigned char * buf,int buflen)480fdad9c93SAxel Dörfler cfb64_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
481fdad9c93SAxel Dörfler {
482fdad9c93SAxel Dörfler fb64_printsub(data, cnt, buf, buflen, "CFB64");
483fdad9c93SAxel Dörfler }
484fdad9c93SAxel Dörfler
485fdad9c93SAxel Dörfler void
ofb64_printsub(unsigned char * data,int cnt,unsigned char * buf,int buflen)486fdad9c93SAxel Dörfler ofb64_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
487fdad9c93SAxel Dörfler {
488fdad9c93SAxel Dörfler fb64_printsub(data, cnt, buf, buflen, "OFB64");
489fdad9c93SAxel Dörfler }
490fdad9c93SAxel Dörfler
491fdad9c93SAxel Dörfler void
fb64_stream_iv(Block seed,struct stinfo * stp)492fdad9c93SAxel Dörfler fb64_stream_iv(Block seed, struct stinfo *stp)
493fdad9c93SAxel Dörfler {
494fdad9c93SAxel Dörfler
495fdad9c93SAxel Dörfler memmove((void *)stp->str_iv, (void *)seed, sizeof(Block));
496fdad9c93SAxel Dörfler memmove((void *)stp->str_output, (void *)seed, sizeof(Block));
497fdad9c93SAxel Dörfler
498*6b99b206SAugustin Cavalier DES_key_sched((Block *)stp->str_ikey, &stp->str_sched);
499fdad9c93SAxel Dörfler
500fdad9c93SAxel Dörfler stp->str_index = sizeof(Block);
501fdad9c93SAxel Dörfler }
502fdad9c93SAxel Dörfler
503fdad9c93SAxel Dörfler void
fb64_stream_key(Block key,struct stinfo * stp)504fdad9c93SAxel Dörfler fb64_stream_key(Block key, struct stinfo *stp)
505fdad9c93SAxel Dörfler {
506fdad9c93SAxel Dörfler memmove((void *)stp->str_ikey, (void *)key, sizeof(Block));
507*6b99b206SAugustin Cavalier DES_key_sched((Block *)key, &stp->str_sched);
508fdad9c93SAxel Dörfler
509fdad9c93SAxel Dörfler memmove((void *)stp->str_output, (void *)stp->str_iv, sizeof(Block));
510fdad9c93SAxel Dörfler
511fdad9c93SAxel Dörfler stp->str_index = sizeof(Block);
512fdad9c93SAxel Dörfler }
513fdad9c93SAxel Dörfler
514fdad9c93SAxel Dörfler /*
515fdad9c93SAxel Dörfler * DES 64 bit Cipher Feedback
516fdad9c93SAxel Dörfler *
517fdad9c93SAxel Dörfler * key --->+-----+
518fdad9c93SAxel Dörfler * +->| DES |--+
519fdad9c93SAxel Dörfler * | +-----+ |
520fdad9c93SAxel Dörfler * | v
521fdad9c93SAxel Dörfler * INPUT --(--------->(+)+---> DATA
522fdad9c93SAxel Dörfler * | |
523fdad9c93SAxel Dörfler * +-------------+
524fdad9c93SAxel Dörfler *
525fdad9c93SAxel Dörfler *
526fdad9c93SAxel Dörfler * Given:
527fdad9c93SAxel Dörfler * iV: Initial vector, 64 bits (8 bytes) long.
528fdad9c93SAxel Dörfler * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
529fdad9c93SAxel Dörfler * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
530fdad9c93SAxel Dörfler *
531fdad9c93SAxel Dörfler * V0 = DES(iV, key)
532fdad9c93SAxel Dörfler * On = Dn ^ Vn
533fdad9c93SAxel Dörfler * V(n+1) = DES(On, key)
534fdad9c93SAxel Dörfler */
535fdad9c93SAxel Dörfler
536fdad9c93SAxel Dörfler void
cfb64_encrypt(unsigned char * s,int c)537fdad9c93SAxel Dörfler cfb64_encrypt(unsigned char *s, int c)
538fdad9c93SAxel Dörfler {
539fdad9c93SAxel Dörfler struct stinfo *stp = &fb[CFB].streams[DIR_ENCRYPT-1];
540fdad9c93SAxel Dörfler int idx;
541fdad9c93SAxel Dörfler
542fdad9c93SAxel Dörfler idx = stp->str_index;
543fdad9c93SAxel Dörfler while (c-- > 0) {
544fdad9c93SAxel Dörfler if (idx == sizeof(Block)) {
545fdad9c93SAxel Dörfler Block b;
546*6b99b206SAugustin Cavalier DES_ecb_encrypt((Block *)stp->str_output, (Block *)b, &stp->str_sched, 1);
547fdad9c93SAxel Dörfler memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
548fdad9c93SAxel Dörfler idx = 0;
549fdad9c93SAxel Dörfler }
550fdad9c93SAxel Dörfler
551fdad9c93SAxel Dörfler /* On encryption, we store (feed ^ data) which is cypher */
552fdad9c93SAxel Dörfler *s = stp->str_output[idx] = (stp->str_feed[idx] ^ *s);
553fdad9c93SAxel Dörfler s++;
554fdad9c93SAxel Dörfler idx++;
555fdad9c93SAxel Dörfler }
556fdad9c93SAxel Dörfler stp->str_index = idx;
557fdad9c93SAxel Dörfler }
558fdad9c93SAxel Dörfler
559fdad9c93SAxel Dörfler int
cfb64_decrypt(int data)560fdad9c93SAxel Dörfler cfb64_decrypt(int data)
561fdad9c93SAxel Dörfler {
562fdad9c93SAxel Dörfler struct stinfo *stp = &fb[CFB].streams[DIR_DECRYPT-1];
563fdad9c93SAxel Dörfler int idx;
564fdad9c93SAxel Dörfler
565fdad9c93SAxel Dörfler if (data == -1) {
566fdad9c93SAxel Dörfler /*
567fdad9c93SAxel Dörfler * Back up one byte. It is assumed that we will
568fdad9c93SAxel Dörfler * never back up more than one byte. If we do, this
569fdad9c93SAxel Dörfler * may or may not work.
570fdad9c93SAxel Dörfler */
571fdad9c93SAxel Dörfler if (stp->str_index)
572fdad9c93SAxel Dörfler --stp->str_index;
573fdad9c93SAxel Dörfler return(0);
574fdad9c93SAxel Dörfler }
575fdad9c93SAxel Dörfler
576fdad9c93SAxel Dörfler idx = stp->str_index++;
577fdad9c93SAxel Dörfler if (idx == sizeof(Block)) {
578fdad9c93SAxel Dörfler Block b;
579*6b99b206SAugustin Cavalier DES_ecb_encrypt((Block *)stp->str_output, (Block *)b, &stp->str_sched, 1);
580fdad9c93SAxel Dörfler memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
581fdad9c93SAxel Dörfler stp->str_index = 1; /* Next time will be 1 */
582fdad9c93SAxel Dörfler idx = 0; /* But now use 0 */
583fdad9c93SAxel Dörfler }
584fdad9c93SAxel Dörfler
585fdad9c93SAxel Dörfler /* On decryption we store (data) which is cypher. */
586fdad9c93SAxel Dörfler stp->str_output[idx] = data;
587fdad9c93SAxel Dörfler return(data ^ stp->str_feed[idx]);
588fdad9c93SAxel Dörfler }
589fdad9c93SAxel Dörfler
590fdad9c93SAxel Dörfler /*
591fdad9c93SAxel Dörfler * DES 64 bit Output Feedback
592fdad9c93SAxel Dörfler *
593fdad9c93SAxel Dörfler * key --->+-----+
594fdad9c93SAxel Dörfler * +->| DES |--+
595fdad9c93SAxel Dörfler * | +-----+ |
596fdad9c93SAxel Dörfler * +-----------+
597fdad9c93SAxel Dörfler * v
598fdad9c93SAxel Dörfler * INPUT -------->(+) ----> DATA
599fdad9c93SAxel Dörfler *
600fdad9c93SAxel Dörfler * Given:
601fdad9c93SAxel Dörfler * iV: Initial vector, 64 bits (8 bytes) long.
602fdad9c93SAxel Dörfler * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
603fdad9c93SAxel Dörfler * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
604fdad9c93SAxel Dörfler *
605fdad9c93SAxel Dörfler * V0 = DES(iV, key)
606fdad9c93SAxel Dörfler * V(n+1) = DES(Vn, key)
607fdad9c93SAxel Dörfler * On = Dn ^ Vn
608fdad9c93SAxel Dörfler */
609fdad9c93SAxel Dörfler void
ofb64_encrypt(unsigned char * s,int c)610fdad9c93SAxel Dörfler ofb64_encrypt(unsigned char *s, int c)
611fdad9c93SAxel Dörfler {
612fdad9c93SAxel Dörfler struct stinfo *stp = &fb[OFB].streams[DIR_ENCRYPT-1];
613fdad9c93SAxel Dörfler int idx;
614fdad9c93SAxel Dörfler
615fdad9c93SAxel Dörfler idx = stp->str_index;
616fdad9c93SAxel Dörfler while (c-- > 0) {
617fdad9c93SAxel Dörfler if (idx == sizeof(Block)) {
618fdad9c93SAxel Dörfler Block b;
619*6b99b206SAugustin Cavalier DES_ecb_encrypt((Block *)stp->str_feed, (Block *)b, &stp->str_sched, 1);
620fdad9c93SAxel Dörfler memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
621fdad9c93SAxel Dörfler idx = 0;
622fdad9c93SAxel Dörfler }
623fdad9c93SAxel Dörfler *s++ ^= stp->str_feed[idx];
624fdad9c93SAxel Dörfler idx++;
625fdad9c93SAxel Dörfler }
626fdad9c93SAxel Dörfler stp->str_index = idx;
627fdad9c93SAxel Dörfler }
628fdad9c93SAxel Dörfler
629fdad9c93SAxel Dörfler int
ofb64_decrypt(int data)630fdad9c93SAxel Dörfler ofb64_decrypt(int data)
631fdad9c93SAxel Dörfler {
632fdad9c93SAxel Dörfler struct stinfo *stp = &fb[OFB].streams[DIR_DECRYPT-1];
633fdad9c93SAxel Dörfler int idx;
634fdad9c93SAxel Dörfler
635fdad9c93SAxel Dörfler if (data == -1) {
636fdad9c93SAxel Dörfler /*
637fdad9c93SAxel Dörfler * Back up one byte. It is assumed that we will
638fdad9c93SAxel Dörfler * never back up more than one byte. If we do, this
639fdad9c93SAxel Dörfler * may or may not work.
640fdad9c93SAxel Dörfler */
641fdad9c93SAxel Dörfler if (stp->str_index)
642fdad9c93SAxel Dörfler --stp->str_index;
643fdad9c93SAxel Dörfler return(0);
644fdad9c93SAxel Dörfler }
645fdad9c93SAxel Dörfler
646fdad9c93SAxel Dörfler idx = stp->str_index++;
647fdad9c93SAxel Dörfler if (idx == sizeof(Block)) {
648fdad9c93SAxel Dörfler Block b;
649*6b99b206SAugustin Cavalier DES_ecb_encrypt((Block *)stp->str_feed, (Block *)b, &stp->str_sched, 1);
650fdad9c93SAxel Dörfler memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
651fdad9c93SAxel Dörfler stp->str_index = 1; /* Next time will be 1 */
652fdad9c93SAxel Dörfler idx = 0; /* But now use 0 */
653fdad9c93SAxel Dörfler }
654fdad9c93SAxel Dörfler
655fdad9c93SAxel Dörfler return(data ^ stp->str_feed[idx]);
656fdad9c93SAxel Dörfler }
657fdad9c93SAxel Dörfler # endif /* AUTHENTICATION */
658fdad9c93SAxel Dörfler #endif /* ENCRYPTION */
659