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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static const char sccsid[] = "@(#)enc_des.c 8.3 (Berkeley) 5/30/95";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 # ifdef AUTHENTICATION
40 #include <arpa/telnet.h>
41 #include <openssl/des.h>
47 #include "key-proto.h"
48 #include "misc-proto.h"
50 extern int encrypt_debug_mode;
58 #define IN_PROGRESS (NO_SEND_IV|NO_RECV_IV|NO_KEYID)
65 Schedule krbdes_sched;
67 unsigned char fb_feed[64];
82 static struct fb fb[2];
91 { "\0", 1, 0, 0, 0 }, /* default key of zero */
95 #define KEYFLAG_MASK 03
97 #define KEYFLAG_NOINIT 00
98 #define KEYFLAG_INIT 01
100 #define KEYFLAG_BAD 03
102 #define KEYFLAG_SHIFT 2
104 #define SHIFT_VAL(a,b) (KEYFLAG_SHIFT*((a)+((b)*2)))
108 #define FB64_IV_BAD 3
111 void fb64_stream_iv(Block, struct stinfo *);
112 void fb64_init(struct fb *);
113 static int fb64_start(struct fb *, int, int);
114 int fb64_is(unsigned char *, int, struct fb *);
115 int fb64_reply(unsigned char *, int, struct fb *);
116 static void fb64_session(Session_Key *, int, struct fb *);
117 void fb64_stream_key(Block, struct stinfo *);
118 int fb64_keyid(int, unsigned char *, int *, struct fb *);
121 cfb64_init(int server __unused)
124 fb[CFB].fb_feed[4] = ENCTYPE_DES_CFB64;
125 fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, CFB);
126 fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, CFB);
130 ofb64_init(int server __unused)
133 fb[OFB].fb_feed[4] = ENCTYPE_DES_OFB64;
134 fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, OFB);
135 fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, OFB);
139 fb64_init(struct fb *fbp)
141 memset((void *)fbp, 0, sizeof(*fbp));
142 fbp->state[0] = fbp->state[1] = FAILED;
143 fbp->fb_feed[0] = IAC;
144 fbp->fb_feed[1] = SB;
145 fbp->fb_feed[2] = TELOPT_ENCRYPT;
146 fbp->fb_feed[3] = ENCRYPT_IS;
151 * -1: some error. Negotiation is done, encryption not ready.
152 * 0: Successful, initial negotiation all done.
153 * 1: successful, negotiation not done yet.
154 * 2: Not yet. Other things (like getting the key from
155 * Kerberos) have to happen before we can continue.
158 cfb64_start(int dir, int server)
160 return(fb64_start(&fb[CFB], dir, server));
164 ofb64_start(int dir, int server)
166 return(fb64_start(&fb[OFB], dir, server));
170 fb64_start(struct fb *fbp, int dir, int server __unused)
179 * This is simply a request to have the other side
180 * start output (our input). He will negotiate an
181 * IV so we need not look for it.
183 state = fbp->state[dir-1];
189 state = fbp->state[dir-1];
192 else if ((state & NO_SEND_IV) == 0)
195 if (!VALIDKEY(fbp->krbdes_key)) {
199 state &= ~NO_SEND_IV;
201 if (encrypt_debug_mode)
202 printf("Creating new feed\r\n");
204 * Create a random feed and send it over.
206 DES_random_key((Block *)fbp->temp_feed);
207 DES_ecb_encrypt((Block *)fbp->temp_feed, (Block *)fbp->temp_feed,
208 &fbp->krbdes_sched, 1);
209 p = fbp->fb_feed + 3;
213 for (x = 0; x < sizeof(Block); ++x) {
214 if ((*p++ = fbp->temp_feed[x]) == IAC)
219 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
220 net_write(fbp->fb_feed, p - fbp->fb_feed);
225 return(fbp->state[dir-1] = state);
230 * -1: some error. Negotiation is done, encryption not ready.
231 * 0: Successful, initial negotiation all done.
232 * 1: successful, negotiation not done yet.
235 cfb64_is(unsigned char *data, int cnt)
237 return(fb64_is(data, cnt, &fb[CFB]));
241 ofb64_is(unsigned char *data, int cnt)
243 return(fb64_is(data, cnt, &fb[OFB]));
247 fb64_is(unsigned char *data, int cnt, struct fb *fbp)
250 int state = fbp->state[DIR_DECRYPT-1];
257 if (cnt != sizeof(Block)) {
258 if (encrypt_debug_mode)
259 printf("CFB64: initial vector failed on size\r\n");
264 if (encrypt_debug_mode)
265 printf("CFB64: initial vector received\r\n");
267 if (encrypt_debug_mode)
268 printf("Initializing Decrypt stream\r\n");
270 fb64_stream_iv((void *)data, &fbp->streams[DIR_DECRYPT-1]);
272 p = fbp->fb_feed + 3;
273 *p++ = ENCRYPT_REPLY;
278 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
279 net_write(fbp->fb_feed, p - fbp->fb_feed);
281 state = fbp->state[DIR_DECRYPT-1] = IN_PROGRESS;
285 if (encrypt_debug_mode) {
286 printf("Unknown option type: %d\r\n", *(data-1));
293 * We failed. Send an FB64_IV_BAD option
294 * to the other side so it will know that
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);
308 return(fbp->state[DIR_DECRYPT-1] = state);
313 * -1: some error. Negotiation is done, encryption not ready.
314 * 0: Successful, initial negotiation all done.
315 * 1: successful, negotiation not done yet.
318 cfb64_reply(unsigned char *data, int cnt)
320 return(fb64_reply(data, cnt, &fb[CFB]));
324 ofb64_reply(unsigned char *data, int cnt)
326 return(fb64_reply(data, cnt, &fb[OFB]));
330 fb64_reply(unsigned char *data, int cnt, struct fb *fbp)
332 int state = fbp->state[DIR_ENCRYPT-1];
339 fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
342 state &= ~NO_RECV_IV;
343 encrypt_send_keyid(DIR_ENCRYPT, "\0", 1, 1);
347 memset(fbp->temp_feed, 0, sizeof(Block));
348 fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
353 if (encrypt_debug_mode) {
354 printf("Unknown option type: %d\r\n", data[-1]);
363 return(fbp->state[DIR_ENCRYPT-1] = state);
367 cfb64_session(Session_Key *key, int server)
369 fb64_session(key, server, &fb[CFB]);
373 ofb64_session(Session_Key *key, int server)
375 fb64_session(key, server, &fb[OFB]);
379 fb64_session(Session_Key *key, int server, struct fb *fbp)
381 if (!key || key->type != SK_DES) {
382 if (encrypt_debug_mode)
383 printf("Can't set krbdes's session key (%d != %d)\r\n",
384 key ? key->type : -1, SK_DES);
387 memmove((void *)fbp->krbdes_key, (void *)key->data, sizeof(Block));
389 fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_ENCRYPT-1]);
390 fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_DECRYPT-1]);
392 DES_key_sched((Block *)fbp->krbdes_key, &fbp->krbdes_sched);
394 * Now look to see if krbdes_start() was was waiting for
395 * the key to show up. If so, go ahead an call it now
396 * that we have the key.
398 if (fbp->need_start) {
400 fb64_start(fbp, DIR_ENCRYPT, server);
405 * We only accept a keyid of 0. If we get a keyid of
406 * 0, then mark the state as SUCCESS.
409 cfb64_keyid(int dir, unsigned char *kp, int *lenp)
411 return(fb64_keyid(dir, kp, lenp, &fb[CFB]));
415 ofb64_keyid(int dir, unsigned char *kp, int *lenp)
417 return(fb64_keyid(dir, kp, lenp, &fb[OFB]));
421 fb64_keyid(int dir, unsigned char *kp, int *lenp, struct fb *fbp)
423 int state = fbp->state[dir-1];
425 if (*lenp != 1 || (*kp != '\0')) {
435 return(fbp->state[dir-1] = state);
439 fb64_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen, const char *type)
445 buf[buflen-1] = '\0'; /* make sure it's NULL terminated */
450 sprintf(lbuf, "%s_IV", type);
455 sprintf(lbuf, "%s_IV_OK", type);
460 sprintf(lbuf, "%s_IV_BAD", type);
465 sprintf(lbuf, " %d (unknown)", data[2]);
468 for (; (buflen > 0) && (*buf = *cp++); buf++)
470 for (i = 3; i < cnt; i++) {
471 sprintf(lbuf, " %d", data[i]);
472 for (cp = lbuf; (buflen > 0) && (*buf = *cp++); buf++)
480 cfb64_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
482 fb64_printsub(data, cnt, buf, buflen, "CFB64");
486 ofb64_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
488 fb64_printsub(data, cnt, buf, buflen, "OFB64");
492 fb64_stream_iv(Block seed, struct stinfo *stp)
495 memmove((void *)stp->str_iv, (void *)seed, sizeof(Block));
496 memmove((void *)stp->str_output, (void *)seed, sizeof(Block));
498 DES_key_sched((Block *)stp->str_ikey, &stp->str_sched);
500 stp->str_index = sizeof(Block);
504 fb64_stream_key(Block key, struct stinfo *stp)
506 memmove((void *)stp->str_ikey, (void *)key, sizeof(Block));
507 DES_key_sched((Block *)key, &stp->str_sched);
509 memmove((void *)stp->str_output, (void *)stp->str_iv, sizeof(Block));
511 stp->str_index = sizeof(Block);
515 * DES 64 bit Cipher Feedback
521 * INPUT --(--------->(+)+---> DATA
527 * iV: Initial vector, 64 bits (8 bytes) long.
528 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
529 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
533 * V(n+1) = DES(On, key)
537 cfb64_encrypt(unsigned char *s, int c)
539 struct stinfo *stp = &fb[CFB].streams[DIR_ENCRYPT-1];
542 idx = stp->str_index;
544 if (idx == sizeof(Block)) {
546 DES_ecb_encrypt((Block *)stp->str_output, (Block *)b, &stp->str_sched, 1);
547 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
551 /* On encryption, we store (feed ^ data) which is cypher */
552 *s = stp->str_output[idx] = (stp->str_feed[idx] ^ *s);
556 stp->str_index = idx;
560 cfb64_decrypt(int data)
562 struct stinfo *stp = &fb[CFB].streams[DIR_DECRYPT-1];
567 * Back up one byte. It is assumed that we will
568 * never back up more than one byte. If we do, this
569 * may or may not work.
576 idx = stp->str_index++;
577 if (idx == sizeof(Block)) {
579 DES_ecb_encrypt((Block *)stp->str_output, (Block *)b, &stp->str_sched, 1);
580 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
581 stp->str_index = 1; /* Next time will be 1 */
582 idx = 0; /* But now use 0 */
585 /* On decryption we store (data) which is cypher. */
586 stp->str_output[idx] = data;
587 return(data ^ stp->str_feed[idx]);
591 * DES 64 bit Output Feedback
598 * INPUT -------->(+) ----> DATA
601 * iV: Initial vector, 64 bits (8 bytes) long.
602 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
603 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
606 * V(n+1) = DES(Vn, key)
610 ofb64_encrypt(unsigned char *s, int c)
612 struct stinfo *stp = &fb[OFB].streams[DIR_ENCRYPT-1];
615 idx = stp->str_index;
617 if (idx == sizeof(Block)) {
619 DES_ecb_encrypt((Block *)stp->str_feed, (Block *)b, &stp->str_sched, 1);
620 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
623 *s++ ^= stp->str_feed[idx];
626 stp->str_index = idx;
630 ofb64_decrypt(int data)
632 struct stinfo *stp = &fb[OFB].streams[DIR_DECRYPT-1];
637 * Back up one byte. It is assumed that we will
638 * never back up more than one byte. If we do, this
639 * may or may not work.
646 idx = stp->str_index++;
647 if (idx == sizeof(Block)) {
649 DES_ecb_encrypt((Block *)stp->str_feed, (Block *)b, &stp->str_sched, 1);
650 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
651 stp->str_index = 1; /* Next time will be 1 */
652 idx = 0; /* But now use 0 */
655 return(data ^ stp->str_feed[idx]);
657 # endif /* AUTHENTICATION */
658 #endif /* ENCRYPTION */