2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
40 static const char sccsid[] = "@(#)enc_des.c 8.3 (Berkeley) 5/30/95";
44 # ifdef AUTHENTICATION
45 # ifdef DES_ENCRYPTION
46 #include <arpa/telnet.h>
52 #include <openssl/des.h>
55 #include "key-proto.h"
56 #include "misc-proto.h"
58 extern int encrypt_debug_mode;
59 void des_set_random_generator_seed(des_cblock *); /* XXX */
67 #define IN_PROGRESS (NO_SEND_IV|NO_RECV_IV|NO_KEYID)
74 Schedule krbdes_sched;
76 unsigned char fb_feed[64];
92 static struct fb fb[2];
101 { "\0", 1, 0, 0, 0 }, /* default key of zero */
105 #define KEYFLAG_MASK 03
107 #define KEYFLAG_NOINIT 00
108 #define KEYFLAG_INIT 01
109 #define KEYFLAG_OK 02
110 #define KEYFLAG_BAD 03
112 #define KEYFLAG_SHIFT 2
114 #define SHIFT_VAL(a,b) (KEYFLAG_SHIFT*((a)+((b)*2)))
118 #define FB64_IV_BAD 3
121 void fb64_stream_iv P((Block, struct stinfo *));
122 void fb64_init P((struct fb *));
123 static int fb64_start P((struct fb *, int, int));
124 int fb64_is P((unsigned char *, int, struct fb *));
125 int fb64_reply P((unsigned char *, int, struct fb *));
126 static void fb64_session P((Session_Key *, int, struct fb *));
127 void fb64_stream_key P((Block, struct stinfo *));
128 int fb64_keyid P((int, unsigned char *, int *, struct fb *));
135 fb[CFB].fb_feed[4] = ENCTYPE_DES_CFB64;
136 fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, CFB);
137 fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, CFB);
145 fb[OFB].fb_feed[4] = ENCTYPE_DES_OFB64;
146 fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, OFB);
147 fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, OFB);
152 register struct fb *fbp;
154 memset((void *)fbp, 0, sizeof(*fbp));
155 fbp->state[0] = fbp->state[1] = FAILED;
156 fbp->fb_feed[0] = IAC;
157 fbp->fb_feed[1] = SB;
158 fbp->fb_feed[2] = TELOPT_ENCRYPT;
159 fbp->fb_feed[3] = ENCRYPT_IS;
164 * -1: some error. Negotiation is done, encryption not ready.
165 * 0: Successful, initial negotiation all done.
166 * 1: successful, negotiation not done yet.
167 * 2: Not yet. Other things (like getting the key from
168 * Kerberos) have to happen before we can continue.
171 cfb64_start(dir, server)
175 return(fb64_start(&fb[CFB], dir, server));
178 ofb64_start(dir, server)
182 return(fb64_start(&fb[OFB], dir, server));
186 fb64_start(fbp, dir, server)
198 * This is simply a request to have the other side
199 * start output (our input). He will negotiate an
200 * IV so we need not look for it.
202 state = fbp->state[dir-1];
208 state = fbp->state[dir-1];
211 else if ((state & NO_SEND_IV) == 0)
214 if (!VALIDKEY(fbp->krbdes_key)) {
218 state &= ~NO_SEND_IV;
220 if (encrypt_debug_mode)
221 printf("Creating new feed\r\n");
223 * Create a random feed and send it over.
225 des_new_random_key((Block *)fbp->temp_feed);
226 des_ecb_encrypt((Block *)fbp->temp_feed, (Block *)fbp->temp_feed,
227 fbp->krbdes_sched, 1);
228 p = fbp->fb_feed + 3;
232 for (x = 0; x < sizeof(Block); ++x) {
233 if ((*p++ = fbp->temp_feed[x]) == IAC)
238 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
239 net_write(fbp->fb_feed, p - fbp->fb_feed);
244 return(fbp->state[dir-1] = state);
249 * -1: some error. Negotiation is done, encryption not ready.
250 * 0: Successful, initial negotiation all done.
251 * 1: successful, negotiation not done yet.
258 return(fb64_is(data, cnt, &fb[CFB]));
265 return(fb64_is(data, cnt, &fb[OFB]));
269 fb64_is(data, cnt, fbp)
275 register int state = fbp->state[DIR_DECRYPT-1];
282 if (cnt != sizeof(Block)) {
283 if (encrypt_debug_mode)
284 printf("CFB64: initial vector failed on size\r\n");
289 if (encrypt_debug_mode)
290 printf("CFB64: initial vector received\r\n");
292 if (encrypt_debug_mode)
293 printf("Initializing Decrypt stream\r\n");
295 fb64_stream_iv((void *)data, &fbp->streams[DIR_DECRYPT-1]);
297 p = fbp->fb_feed + 3;
298 *p++ = ENCRYPT_REPLY;
303 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
304 net_write(fbp->fb_feed, p - fbp->fb_feed);
306 state = fbp->state[DIR_DECRYPT-1] = IN_PROGRESS;
310 if (encrypt_debug_mode) {
311 printf("Unknown option type: %d\r\n", *(data-1));
318 * We failed. Send an FB64_IV_BAD option
319 * to the other side so it will know that
322 p = fbp->fb_feed + 3;
323 *p++ = ENCRYPT_REPLY;
328 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
329 net_write(fbp->fb_feed, p - fbp->fb_feed);
333 return(fbp->state[DIR_DECRYPT-1] = state);
338 * -1: some error. Negotiation is done, encryption not ready.
339 * 0: Successful, initial negotiation all done.
340 * 1: successful, negotiation not done yet.
343 cfb64_reply(data, cnt)
347 return(fb64_reply(data, cnt, &fb[CFB]));
350 ofb64_reply(data, cnt)
354 return(fb64_reply(data, cnt, &fb[OFB]));
359 fb64_reply(data, cnt, fbp)
364 register int state = fbp->state[DIR_ENCRYPT-1];
371 fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
374 state &= ~NO_RECV_IV;
375 encrypt_send_keyid(DIR_ENCRYPT, (unsigned char *)"\0", 1, 1);
379 memset(fbp->temp_feed, 0, sizeof(Block));
380 fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
385 if (encrypt_debug_mode) {
386 printf("Unknown option type: %d\r\n", data[-1]);
395 return(fbp->state[DIR_ENCRYPT-1] = state);
399 cfb64_session(key, server)
403 fb64_session(key, server, &fb[CFB]);
407 ofb64_session(key, server)
411 fb64_session(key, server, &fb[OFB]);
415 fb64_session(key, server, fbp)
421 if (!key || key->type != SK_DES) {
422 if (encrypt_debug_mode)
423 printf("Can't set krbdes's session key (%d != %d)\r\n",
424 key ? key->type : -1, SK_DES);
427 memmove((void *)fbp->krbdes_key, (void *)key->data, sizeof(Block));
429 fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_ENCRYPT-1]);
430 fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_DECRYPT-1]);
432 if (fbp->once == 0) {
433 des_set_random_generator_seed((Block *)fbp->krbdes_key);
436 des_key_sched((Block *)fbp->krbdes_key, fbp->krbdes_sched);
438 * Now look to see if krbdes_start() was was waiting for
439 * the key to show up. If so, go ahead an call it now
440 * that we have the key.
442 if (fbp->need_start) {
444 fb64_start(fbp, DIR_ENCRYPT, server);
449 * We only accept a keyid of 0. If we get a keyid of
450 * 0, then mark the state as SUCCESS.
453 cfb64_keyid(dir, kp, lenp)
457 return(fb64_keyid(dir, kp, lenp, &fb[CFB]));
461 ofb64_keyid(dir, kp, lenp)
465 return(fb64_keyid(dir, kp, lenp, &fb[OFB]));
469 fb64_keyid(dir, kp, lenp, fbp)
474 register int state = fbp->state[dir-1];
476 if (*lenp != 1 || (*kp != '\0')) {
486 return(fbp->state[dir-1] = state);
490 fb64_printsub(data, cnt, buf, buflen, type)
491 unsigned char *data, *buf, *type;
498 buf[buflen-1] = '\0'; /* make sure it's NULL terminated */
503 sprintf(lbuf, "%s_IV", type);
508 sprintf(lbuf, "%s_IV_OK", type);
513 sprintf(lbuf, "%s_IV_BAD", type);
518 sprintf(lbuf, " %d (unknown)", data[2]);
521 for (; (buflen > 0) && (*buf = *cp++); buf++)
523 for (i = 3; i < cnt; i++) {
524 sprintf(lbuf, " %d", data[i]);
525 for (cp = lbuf; (buflen > 0) && (*buf = *cp++); buf++)
533 cfb64_printsub(data, cnt, buf, buflen)
534 unsigned char *data, *buf;
537 fb64_printsub(data, cnt, buf, buflen, "CFB64");
541 ofb64_printsub(data, cnt, buf, buflen)
542 unsigned char *data, *buf;
545 fb64_printsub(data, cnt, buf, buflen, "OFB64");
549 fb64_stream_iv(seed, stp)
551 register struct stinfo *stp;
554 memmove((void *)stp->str_iv, (void *)seed, sizeof(Block));
555 memmove((void *)stp->str_output, (void *)seed, sizeof(Block));
557 des_key_sched((Block *)stp->str_ikey, stp->str_sched);
559 stp->str_index = sizeof(Block);
563 fb64_stream_key(key, stp)
565 register struct stinfo *stp;
567 memmove((void *)stp->str_ikey, (void *)key, sizeof(Block));
568 des_key_sched((Block *)key, stp->str_sched);
570 memmove((void *)stp->str_output, (void *)stp->str_iv, sizeof(Block));
572 stp->str_index = sizeof(Block);
576 * DES 64 bit Cipher Feedback
582 * INPUT --(--------->(+)+---> DATA
588 * iV: Initial vector, 64 bits (8 bytes) long.
589 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
590 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
594 * V(n+1) = DES(On, key)
599 register unsigned char *s;
602 register struct stinfo *stp = &fb[CFB].streams[DIR_ENCRYPT-1];
605 index = stp->str_index;
607 if (index == sizeof(Block)) {
609 des_ecb_encrypt((Block *)stp->str_output, (Block *)b, stp->str_sched, 1);
610 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
614 /* On encryption, we store (feed ^ data) which is cypher */
615 *s = stp->str_output[index] = (stp->str_feed[index] ^ *s);
619 stp->str_index = index;
626 register struct stinfo *stp = &fb[CFB].streams[DIR_DECRYPT-1];
631 * Back up one byte. It is assumed that we will
632 * never back up more than one byte. If we do, this
633 * may or may not work.
640 index = stp->str_index++;
641 if (index == sizeof(Block)) {
643 des_ecb_encrypt((Block *)stp->str_output, (Block *)b, stp->str_sched, 1);
644 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
645 stp->str_index = 1; /* Next time will be 1 */
646 index = 0; /* But now use 0 */
649 /* On decryption we store (data) which is cypher. */
650 stp->str_output[index] = data;
651 return(data ^ stp->str_feed[index]);
655 * DES 64 bit Output Feedback
662 * INPUT -------->(+) ----> DATA
665 * iV: Initial vector, 64 bits (8 bytes) long.
666 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
667 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
670 * V(n+1) = DES(Vn, key)
675 register unsigned char *s;
678 register struct stinfo *stp = &fb[OFB].streams[DIR_ENCRYPT-1];
681 index = stp->str_index;
683 if (index == sizeof(Block)) {
685 des_ecb_encrypt((Block *)stp->str_feed, (Block *)b, stp->str_sched, 1);
686 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
689 *s++ ^= stp->str_feed[index];
692 stp->str_index = index;
699 register struct stinfo *stp = &fb[OFB].streams[DIR_DECRYPT-1];
704 * Back up one byte. It is assumed that we will
705 * never back up more than one byte. If we do, this
706 * may or may not work.
713 index = stp->str_index++;
714 if (index == sizeof(Block)) {
716 des_ecb_encrypt((Block *)stp->str_feed, (Block *)b, stp->str_sched, 1);
717 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
718 stp->str_index = 1; /* Next time will be 1 */
719 index = 0; /* But now use 0 */
722 return(data ^ stp->str_feed[index]);
724 # endif /* DES_ENCRYPTION */
725 # endif /* AUTHENTICATION */
726 #endif /* ENCRYPTION */