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
33 * $FreeBSD: src/crypto/telnet/libtelnet/enc_des.c,v 1.3.2.2 2003/02/14 22:38:13 nectar Exp $
36 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD: src/crypto/telnet/libtelnet/enc_des.c,v 1.3.2.2 2003/02/14 22:38:13 nectar Exp $");
41 static const char sccsid[] = "@(#)enc_des.c 8.3 (Berkeley) 5/30/95";
45 # ifdef AUTHENTICATION
46 #include <arpa/telnet.h>
47 #include <openssl/des.h>
53 #include "key-proto.h"
54 #include "misc-proto.h"
56 extern int encrypt_debug_mode;
64 #define IN_PROGRESS (NO_SEND_IV|NO_RECV_IV|NO_KEYID)
71 Schedule krbdes_sched;
73 unsigned char fb_feed[64];
88 static struct fb fb[2];
97 { "\0", 1, 0, 0, 0 }, /* default key of zero */
101 #define KEYFLAG_MASK 03
103 #define KEYFLAG_NOINIT 00
104 #define KEYFLAG_INIT 01
105 #define KEYFLAG_OK 02
106 #define KEYFLAG_BAD 03
108 #define KEYFLAG_SHIFT 2
110 #define SHIFT_VAL(a,b) (KEYFLAG_SHIFT*((a)+((b)*2)))
114 #define FB64_IV_BAD 3
117 void fb64_stream_iv(Block, struct stinfo *);
118 void fb64_init(struct fb *);
119 static int fb64_start(struct fb *, int, int);
120 int fb64_is(unsigned char *, int, struct fb *);
121 int fb64_reply(unsigned char *, int, struct fb *);
122 static void fb64_session(Session_Key *, int, struct fb *);
123 void fb64_stream_key(Block, struct stinfo *);
124 int fb64_keyid(int, unsigned char *, int *, struct fb *);
127 cfb64_init(int server __unused)
130 fb[CFB].fb_feed[4] = ENCTYPE_DES_CFB64;
131 fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, CFB);
132 fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, CFB);
136 ofb64_init(int server __unused)
139 fb[OFB].fb_feed[4] = ENCTYPE_DES_OFB64;
140 fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, OFB);
141 fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, OFB);
145 fb64_init(struct fb *fbp)
147 memset((void *)fbp, 0, sizeof(*fbp));
148 fbp->state[0] = fbp->state[1] = FAILED;
149 fbp->fb_feed[0] = IAC;
150 fbp->fb_feed[1] = SB;
151 fbp->fb_feed[2] = TELOPT_ENCRYPT;
152 fbp->fb_feed[3] = ENCRYPT_IS;
157 * -1: some error. Negotiation is done, encryption not ready.
158 * 0: Successful, initial negotiation all done.
159 * 1: successful, negotiation not done yet.
160 * 2: Not yet. Other things (like getting the key from
161 * Kerberos) have to happen before we can continue.
164 cfb64_start(int dir, int server)
166 return(fb64_start(&fb[CFB], dir, server));
170 ofb64_start(int dir, int server)
172 return(fb64_start(&fb[OFB], dir, server));
176 fb64_start(struct fb *fbp, int dir, int server __unused)
185 * This is simply a request to have the other side
186 * start output (our input). He will negotiate an
187 * IV so we need not look for it.
189 state = fbp->state[dir-1];
195 state = fbp->state[dir-1];
198 else if ((state & NO_SEND_IV) == 0)
201 if (!VALIDKEY(fbp->krbdes_key)) {
205 state &= ~NO_SEND_IV;
207 if (encrypt_debug_mode)
208 printf("Creating new feed\r\n");
210 * Create a random feed and send it over.
212 des_random_key((Block *)fbp->temp_feed);
213 des_ecb_encrypt((Block *)fbp->temp_feed, (Block *)fbp->temp_feed,
214 fbp->krbdes_sched, 1);
215 p = fbp->fb_feed + 3;
219 for (x = 0; x < sizeof(Block); ++x) {
220 if ((*p++ = fbp->temp_feed[x]) == IAC)
225 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
226 net_write(fbp->fb_feed, p - fbp->fb_feed);
231 return(fbp->state[dir-1] = state);
236 * -1: some error. Negotiation is done, encryption not ready.
237 * 0: Successful, initial negotiation all done.
238 * 1: successful, negotiation not done yet.
241 cfb64_is(unsigned char *data, int cnt)
243 return(fb64_is(data, cnt, &fb[CFB]));
247 ofb64_is(unsigned char *data, int cnt)
249 return(fb64_is(data, cnt, &fb[OFB]));
253 fb64_is(unsigned char *data, int cnt, struct fb *fbp)
256 int state = fbp->state[DIR_DECRYPT-1];
263 if (cnt != sizeof(Block)) {
264 if (encrypt_debug_mode)
265 printf("CFB64: initial vector failed on size\r\n");
270 if (encrypt_debug_mode)
271 printf("CFB64: initial vector received\r\n");
273 if (encrypt_debug_mode)
274 printf("Initializing Decrypt stream\r\n");
276 fb64_stream_iv((void *)data, &fbp->streams[DIR_DECRYPT-1]);
278 p = fbp->fb_feed + 3;
279 *p++ = ENCRYPT_REPLY;
284 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
285 net_write(fbp->fb_feed, p - fbp->fb_feed);
287 state = fbp->state[DIR_DECRYPT-1] = IN_PROGRESS;
291 if (encrypt_debug_mode) {
292 printf("Unknown option type: %d\r\n", *(data-1));
299 * We failed. Send an FB64_IV_BAD option
300 * to the other side so it will know that
303 p = fbp->fb_feed + 3;
304 *p++ = ENCRYPT_REPLY;
309 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
310 net_write(fbp->fb_feed, p - fbp->fb_feed);
314 return(fbp->state[DIR_DECRYPT-1] = state);
319 * -1: some error. Negotiation is done, encryption not ready.
320 * 0: Successful, initial negotiation all done.
321 * 1: successful, negotiation not done yet.
324 cfb64_reply(unsigned char *data, int cnt)
326 return(fb64_reply(data, cnt, &fb[CFB]));
330 ofb64_reply(unsigned char *data, int cnt)
332 return(fb64_reply(data, cnt, &fb[OFB]));
336 fb64_reply(unsigned char *data, int cnt, struct fb *fbp)
338 int state = fbp->state[DIR_ENCRYPT-1];
345 fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
348 state &= ~NO_RECV_IV;
349 encrypt_send_keyid(DIR_ENCRYPT, "\0", 1, 1);
353 memset(fbp->temp_feed, 0, sizeof(Block));
354 fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
359 if (encrypt_debug_mode) {
360 printf("Unknown option type: %d\r\n", data[-1]);
369 return(fbp->state[DIR_ENCRYPT-1] = state);
373 cfb64_session(Session_Key *key, int server)
375 fb64_session(key, server, &fb[CFB]);
379 ofb64_session(Session_Key *key, int server)
381 fb64_session(key, server, &fb[OFB]);
385 fb64_session(Session_Key *key, int server, struct fb *fbp)
387 if (!key || key->type != SK_DES) {
388 if (encrypt_debug_mode)
389 printf("Can't set krbdes's session key (%d != %d)\r\n",
390 key ? key->type : -1, SK_DES);
393 memmove((void *)fbp->krbdes_key, (void *)key->data, sizeof(Block));
395 fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_ENCRYPT-1]);
396 fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_DECRYPT-1]);
398 des_key_sched((Block *)fbp->krbdes_key, fbp->krbdes_sched);
400 * Now look to see if krbdes_start() was was waiting for
401 * the key to show up. If so, go ahead an call it now
402 * that we have the key.
404 if (fbp->need_start) {
406 fb64_start(fbp, DIR_ENCRYPT, server);
411 * We only accept a keyid of 0. If we get a keyid of
412 * 0, then mark the state as SUCCESS.
415 cfb64_keyid(int dir, unsigned char *kp, int *lenp)
417 return(fb64_keyid(dir, kp, lenp, &fb[CFB]));
421 ofb64_keyid(int dir, unsigned char *kp, int *lenp)
423 return(fb64_keyid(dir, kp, lenp, &fb[OFB]));
427 fb64_keyid(int dir, unsigned char *kp, int *lenp, struct fb *fbp)
429 int state = fbp->state[dir-1];
431 if (*lenp != 1 || (*kp != '\0')) {
441 return(fbp->state[dir-1] = state);
445 fb64_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen, const char *type)
451 buf[buflen-1] = '\0'; /* make sure it's NULL terminated */
456 sprintf(lbuf, "%s_IV", type);
461 sprintf(lbuf, "%s_IV_OK", type);
466 sprintf(lbuf, "%s_IV_BAD", type);
471 sprintf(lbuf, " %d (unknown)", data[2]);
474 for (; (buflen > 0) && (*buf = *cp++); buf++)
476 for (i = 3; i < cnt; i++) {
477 sprintf(lbuf, " %d", data[i]);
478 for (cp = lbuf; (buflen > 0) && (*buf = *cp++); buf++)
486 cfb64_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
488 fb64_printsub(data, cnt, buf, buflen, "CFB64");
492 ofb64_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
494 fb64_printsub(data, cnt, buf, buflen, "OFB64");
498 fb64_stream_iv(Block seed, struct stinfo *stp)
501 memmove((void *)stp->str_iv, (void *)seed, sizeof(Block));
502 memmove((void *)stp->str_output, (void *)seed, sizeof(Block));
504 des_key_sched((Block *)stp->str_ikey, stp->str_sched);
506 stp->str_index = sizeof(Block);
510 fb64_stream_key(Block key, struct stinfo *stp)
512 memmove((void *)stp->str_ikey, (void *)key, sizeof(Block));
513 des_key_sched((Block *)key, stp->str_sched);
515 memmove((void *)stp->str_output, (void *)stp->str_iv, sizeof(Block));
517 stp->str_index = sizeof(Block);
521 * DES 64 bit Cipher Feedback
527 * INPUT --(--------->(+)+---> DATA
533 * iV: Initial vector, 64 bits (8 bytes) long.
534 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
535 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
539 * V(n+1) = DES(On, key)
543 cfb64_encrypt(unsigned char *s, int c)
545 struct stinfo *stp = &fb[CFB].streams[DIR_ENCRYPT-1];
548 idx = stp->str_index;
550 if (idx == sizeof(Block)) {
552 des_ecb_encrypt((Block *)stp->str_output, (Block *)b, stp->str_sched, 1);
553 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
557 /* On encryption, we store (feed ^ data) which is cypher */
558 *s = stp->str_output[idx] = (stp->str_feed[idx] ^ *s);
562 stp->str_index = idx;
566 cfb64_decrypt(int data)
568 struct stinfo *stp = &fb[CFB].streams[DIR_DECRYPT-1];
573 * Back up one byte. It is assumed that we will
574 * never back up more than one byte. If we do, this
575 * may or may not work.
582 idx = stp->str_index++;
583 if (idx == sizeof(Block)) {
585 des_ecb_encrypt((Block *)stp->str_output, (Block *)b, stp->str_sched, 1);
586 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
587 stp->str_index = 1; /* Next time will be 1 */
588 idx = 0; /* But now use 0 */
591 /* On decryption we store (data) which is cypher. */
592 stp->str_output[idx] = data;
593 return(data ^ stp->str_feed[idx]);
597 * DES 64 bit Output Feedback
604 * INPUT -------->(+) ----> DATA
607 * iV: Initial vector, 64 bits (8 bytes) long.
608 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
609 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
612 * V(n+1) = DES(Vn, key)
616 ofb64_encrypt(unsigned char *s, int c)
618 struct stinfo *stp = &fb[OFB].streams[DIR_ENCRYPT-1];
621 idx = stp->str_index;
623 if (idx == sizeof(Block)) {
625 des_ecb_encrypt((Block *)stp->str_feed, (Block *)b, stp->str_sched, 1);
626 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
629 *s++ ^= stp->str_feed[idx];
632 stp->str_index = idx;
636 ofb64_decrypt(int data)
638 struct stinfo *stp = &fb[OFB].streams[DIR_DECRYPT-1];
643 * Back up one byte. It is assumed that we will
644 * never back up more than one byte. If we do, this
645 * may or may not work.
652 idx = stp->str_index++;
653 if (idx == sizeof(Block)) {
655 des_ecb_encrypt((Block *)stp->str_feed, (Block *)b, stp->str_sched, 1);
656 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
657 stp->str_index = 1; /* Next time will be 1 */
658 idx = 0; /* But now use 0 */
661 return(data ^ stp->str_feed[idx]);
663 # endif /* AUTHENTICATION */
664 #endif /* ENCRYPTION */