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 $
34 * $DragonFly: src/crypto/telnet/libtelnet/enc_des.c,v 1.2 2003/06/17 04:24:37 dillon Exp $
36 * @(#)enc_des.c 8.3 (Berkeley) 5/30/95
37 * $FreeBSD: src/crypto/telnet/libtelnet/enc_des.c,v 1.3.2.2 2003/02/14 22:38:13 nectar Exp $
41 # ifdef AUTHENTICATION
42 #include <arpa/telnet.h>
43 #include <openssl/des.h>
49 #include "key-proto.h"
50 #include "misc-proto.h"
52 extern int encrypt_debug_mode;
60 #define IN_PROGRESS (NO_SEND_IV|NO_RECV_IV|NO_KEYID)
67 Schedule krbdes_sched;
69 unsigned char fb_feed[64];
84 static struct fb fb[2];
93 { "\0", 1, 0, 0, 0 }, /* default key of zero */
97 #define KEYFLAG_MASK 03
99 #define KEYFLAG_NOINIT 00
100 #define KEYFLAG_INIT 01
101 #define KEYFLAG_OK 02
102 #define KEYFLAG_BAD 03
104 #define KEYFLAG_SHIFT 2
106 #define SHIFT_VAL(a,b) (KEYFLAG_SHIFT*((a)+((b)*2)))
110 #define FB64_IV_BAD 3
113 void fb64_stream_iv(Block, struct stinfo *);
114 void fb64_init(struct fb *);
115 static int fb64_start(struct fb *, int, int);
116 int fb64_is(unsigned char *, int, struct fb *);
117 int fb64_reply(unsigned char *, int, struct fb *);
118 static void fb64_session(Session_Key *, int, struct fb *);
119 void fb64_stream_key(Block, struct stinfo *);
120 int fb64_keyid(int, unsigned char *, int *, struct fb *);
123 cfb64_init(int server __unused)
126 fb[CFB].fb_feed[4] = ENCTYPE_DES_CFB64;
127 fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, CFB);
128 fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, CFB);
132 ofb64_init(int server __unused)
135 fb[OFB].fb_feed[4] = ENCTYPE_DES_OFB64;
136 fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, OFB);
137 fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, OFB);
141 fb64_init(struct fb *fbp)
143 memset((void *)fbp, 0, sizeof(*fbp));
144 fbp->state[0] = fbp->state[1] = FAILED;
145 fbp->fb_feed[0] = IAC;
146 fbp->fb_feed[1] = SB;
147 fbp->fb_feed[2] = TELOPT_ENCRYPT;
148 fbp->fb_feed[3] = ENCRYPT_IS;
153 * -1: some error. Negotiation is done, encryption not ready.
154 * 0: Successful, initial negotiation all done.
155 * 1: successful, negotiation not done yet.
156 * 2: Not yet. Other things (like getting the key from
157 * Kerberos) have to happen before we can continue.
160 cfb64_start(int dir, int server)
162 return(fb64_start(&fb[CFB], dir, server));
166 ofb64_start(int dir, int server)
168 return(fb64_start(&fb[OFB], dir, server));
172 fb64_start(struct fb *fbp, int dir, int server __unused)
181 * This is simply a request to have the other side
182 * start output (our input). He will negotiate an
183 * IV so we need not look for it.
185 state = fbp->state[dir-1];
191 state = fbp->state[dir-1];
194 else if ((state & NO_SEND_IV) == 0)
197 if (!VALIDKEY(fbp->krbdes_key)) {
201 state &= ~NO_SEND_IV;
203 if (encrypt_debug_mode)
204 printf("Creating new feed\r\n");
206 * Create a random feed and send it over.
208 des_random_key((Block *)fbp->temp_feed);
209 des_ecb_encrypt((Block *)fbp->temp_feed, (Block *)fbp->temp_feed,
210 fbp->krbdes_sched, 1);
211 p = fbp->fb_feed + 3;
215 for (x = 0; x < sizeof(Block); ++x) {
216 if ((*p++ = fbp->temp_feed[x]) == IAC)
221 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
222 net_write(fbp->fb_feed, p - fbp->fb_feed);
227 return(fbp->state[dir-1] = state);
232 * -1: some error. Negotiation is done, encryption not ready.
233 * 0: Successful, initial negotiation all done.
234 * 1: successful, negotiation not done yet.
237 cfb64_is(unsigned char *data, int cnt)
239 return(fb64_is(data, cnt, &fb[CFB]));
243 ofb64_is(unsigned char *data, int cnt)
245 return(fb64_is(data, cnt, &fb[OFB]));
249 fb64_is(unsigned char *data, int cnt, struct fb *fbp)
252 int state = fbp->state[DIR_DECRYPT-1];
259 if (cnt != sizeof(Block)) {
260 if (encrypt_debug_mode)
261 printf("CFB64: initial vector failed on size\r\n");
266 if (encrypt_debug_mode)
267 printf("CFB64: initial vector received\r\n");
269 if (encrypt_debug_mode)
270 printf("Initializing Decrypt stream\r\n");
272 fb64_stream_iv((void *)data, &fbp->streams[DIR_DECRYPT-1]);
274 p = fbp->fb_feed + 3;
275 *p++ = ENCRYPT_REPLY;
280 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
281 net_write(fbp->fb_feed, p - fbp->fb_feed);
283 state = fbp->state[DIR_DECRYPT-1] = IN_PROGRESS;
287 if (encrypt_debug_mode) {
288 printf("Unknown option type: %d\r\n", *(data-1));
295 * We failed. Send an FB64_IV_BAD option
296 * to the other side so it will know that
299 p = fbp->fb_feed + 3;
300 *p++ = ENCRYPT_REPLY;
305 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
306 net_write(fbp->fb_feed, p - fbp->fb_feed);
310 return(fbp->state[DIR_DECRYPT-1] = state);
315 * -1: some error. Negotiation is done, encryption not ready.
316 * 0: Successful, initial negotiation all done.
317 * 1: successful, negotiation not done yet.
320 cfb64_reply(unsigned char *data, int cnt)
322 return(fb64_reply(data, cnt, &fb[CFB]));
326 ofb64_reply(unsigned char *data, int cnt)
328 return(fb64_reply(data, cnt, &fb[OFB]));
332 fb64_reply(unsigned char *data, int cnt, struct fb *fbp)
334 int state = fbp->state[DIR_ENCRYPT-1];
341 fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
344 state &= ~NO_RECV_IV;
345 encrypt_send_keyid(DIR_ENCRYPT, "\0", 1, 1);
349 memset(fbp->temp_feed, 0, sizeof(Block));
350 fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
355 if (encrypt_debug_mode) {
356 printf("Unknown option type: %d\r\n", data[-1]);
365 return(fbp->state[DIR_ENCRYPT-1] = state);
369 cfb64_session(Session_Key *key, int server)
371 fb64_session(key, server, &fb[CFB]);
375 ofb64_session(Session_Key *key, int server)
377 fb64_session(key, server, &fb[OFB]);
381 fb64_session(Session_Key *key, int server, struct fb *fbp)
383 if (!key || key->type != SK_DES) {
384 if (encrypt_debug_mode)
385 printf("Can't set krbdes's session key (%d != %d)\r\n",
386 key ? key->type : -1, SK_DES);
389 memmove((void *)fbp->krbdes_key, (void *)key->data, sizeof(Block));
391 fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_ENCRYPT-1]);
392 fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_DECRYPT-1]);
394 des_key_sched((Block *)fbp->krbdes_key, fbp->krbdes_sched);
396 * Now look to see if krbdes_start() was was waiting for
397 * the key to show up. If so, go ahead an call it now
398 * that we have the key.
400 if (fbp->need_start) {
402 fb64_start(fbp, DIR_ENCRYPT, server);
407 * We only accept a keyid of 0. If we get a keyid of
408 * 0, then mark the state as SUCCESS.
411 cfb64_keyid(int dir, unsigned char *kp, int *lenp)
413 return(fb64_keyid(dir, kp, lenp, &fb[CFB]));
417 ofb64_keyid(int dir, unsigned char *kp, int *lenp)
419 return(fb64_keyid(dir, kp, lenp, &fb[OFB]));
423 fb64_keyid(int dir, unsigned char *kp, int *lenp, struct fb *fbp)
425 int state = fbp->state[dir-1];
427 if (*lenp != 1 || (*kp != '\0')) {
437 return(fbp->state[dir-1] = state);
441 fb64_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen, const char *type)
447 buf[buflen-1] = '\0'; /* make sure it's NULL terminated */
452 sprintf(lbuf, "%s_IV", type);
457 sprintf(lbuf, "%s_IV_OK", type);
462 sprintf(lbuf, "%s_IV_BAD", type);
467 sprintf(lbuf, " %d (unknown)", data[2]);
470 for (; (buflen > 0) && (*buf = *cp++); buf++)
472 for (i = 3; i < cnt; i++) {
473 sprintf(lbuf, " %d", data[i]);
474 for (cp = lbuf; (buflen > 0) && (*buf = *cp++); buf++)
482 cfb64_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
484 fb64_printsub(data, cnt, buf, buflen, "CFB64");
488 ofb64_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
490 fb64_printsub(data, cnt, buf, buflen, "OFB64");
494 fb64_stream_iv(Block seed, struct stinfo *stp)
497 memmove((void *)stp->str_iv, (void *)seed, sizeof(Block));
498 memmove((void *)stp->str_output, (void *)seed, sizeof(Block));
500 des_key_sched((Block *)stp->str_ikey, stp->str_sched);
502 stp->str_index = sizeof(Block);
506 fb64_stream_key(Block key, struct stinfo *stp)
508 memmove((void *)stp->str_ikey, (void *)key, sizeof(Block));
509 des_key_sched((Block *)key, stp->str_sched);
511 memmove((void *)stp->str_output, (void *)stp->str_iv, sizeof(Block));
513 stp->str_index = sizeof(Block);
517 * DES 64 bit Cipher Feedback
523 * INPUT --(--------->(+)+---> DATA
529 * iV: Initial vector, 64 bits (8 bytes) long.
530 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
531 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
535 * V(n+1) = DES(On, key)
539 cfb64_encrypt(unsigned char *s, int c)
541 struct stinfo *stp = &fb[CFB].streams[DIR_ENCRYPT-1];
544 idx = stp->str_index;
546 if (idx == sizeof(Block)) {
548 des_ecb_encrypt((Block *)stp->str_output, (Block *)b, stp->str_sched, 1);
549 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
553 /* On encryption, we store (feed ^ data) which is cypher */
554 *s = stp->str_output[idx] = (stp->str_feed[idx] ^ *s);
558 stp->str_index = idx;
562 cfb64_decrypt(int data)
564 struct stinfo *stp = &fb[CFB].streams[DIR_DECRYPT-1];
569 * Back up one byte. It is assumed that we will
570 * never back up more than one byte. If we do, this
571 * may or may not work.
578 idx = stp->str_index++;
579 if (idx == sizeof(Block)) {
581 des_ecb_encrypt((Block *)stp->str_output, (Block *)b, stp->str_sched, 1);
582 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
583 stp->str_index = 1; /* Next time will be 1 */
584 idx = 0; /* But now use 0 */
587 /* On decryption we store (data) which is cypher. */
588 stp->str_output[idx] = data;
589 return(data ^ stp->str_feed[idx]);
593 * DES 64 bit Output Feedback
600 * INPUT -------->(+) ----> DATA
603 * iV: Initial vector, 64 bits (8 bytes) long.
604 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
605 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
608 * V(n+1) = DES(Vn, key)
612 ofb64_encrypt(unsigned char *s, int c)
614 struct stinfo *stp = &fb[OFB].streams[DIR_ENCRYPT-1];
617 idx = stp->str_index;
619 if (idx == sizeof(Block)) {
621 des_ecb_encrypt((Block *)stp->str_feed, (Block *)b, stp->str_sched, 1);
622 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
625 *s++ ^= stp->str_feed[idx];
628 stp->str_index = idx;
632 ofb64_decrypt(int data)
634 struct stinfo *stp = &fb[OFB].streams[DIR_DECRYPT-1];
639 * Back up one byte. It is assumed that we will
640 * never back up more than one byte. If we do, this
641 * may or may not work.
648 idx = stp->str_index++;
649 if (idx == sizeof(Block)) {
651 des_ecb_encrypt((Block *)stp->str_feed, (Block *)b, stp->str_sched, 1);
652 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
653 stp->str_index = 1; /* Next time will be 1 */
654 idx = 0; /* But now use 0 */
657 return(data ^ stp->str_feed[idx]);
659 # endif /* AUTHENTICATION */
660 #endif /* ENCRYPTION */