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 RCSID("$Id: enc_des.c 14681 2005-03-23 16:19:31Z lha $");
38 #if defined(AUTHENTICATION) && defined(ENCRYPTION) && defined(DES_ENCRYPTION)
39 #include <arpa/telnet.h>
51 #include "misc-proto.h"
53 #include "crypto-headers.h"
55 extern int encrypt_debug_mode;
63 #define IN_PROGRESS (NO_SEND_IV|NO_RECV_IV|NO_KEYID)
69 DES_cblock str_output;
73 DES_key_schedule str_sched;
79 DES_cblock krbdes_key;
80 DES_key_schedule krbdes_sched;
82 unsigned char fb_feed[64];
87 struct stinfo streams[2];
90 static struct fb fb[2];
99 { "\0", 1, 0, 0, 0 }, /* default key of zero */
103 #define KEYFLAG_MASK 03
105 #define KEYFLAG_NOINIT 00
106 #define KEYFLAG_INIT 01
107 #define KEYFLAG_OK 02
108 #define KEYFLAG_BAD 03
110 #define KEYFLAG_SHIFT 2
112 #define SHIFT_VAL(a,b) (KEYFLAG_SHIFT*((a)+((b)*2)))
116 #define FB64_IV_BAD 3
119 void fb64_stream_iv (DES_cblock, struct stinfo *);
120 void fb64_init (struct fb *);
121 static int fb64_start (struct fb *, int, int);
122 int fb64_is (unsigned char *, int, struct fb *);
123 int fb64_reply (unsigned char *, int, struct fb *);
124 static void fb64_session (Session_Key *, int, struct fb *);
125 void fb64_stream_key (DES_cblock, struct stinfo *);
126 int fb64_keyid (int, unsigned char *, int *, struct fb *);
127 void fb64_printsub(unsigned char *, int ,
128 unsigned char *, int , char *);
130 void cfb64_init(int server)
133 fb[CFB].fb_feed[4] = ENCTYPE_DES_CFB64;
134 fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, CFB);
135 fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, CFB);
139 void ofb64_init(int server)
142 fb[OFB].fb_feed[4] = ENCTYPE_DES_OFB64;
143 fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, OFB);
144 fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, OFB);
147 void fb64_init(struct fb *fbp)
149 memset(fbp,0, sizeof(*fbp));
150 fbp->state[0] = fbp->state[1] = FAILED;
151 fbp->fb_feed[0] = IAC;
152 fbp->fb_feed[1] = SB;
153 fbp->fb_feed[2] = TELOPT_ENCRYPT;
154 fbp->fb_feed[3] = ENCRYPT_IS;
159 * -1: some error. Negotiation is done, encryption not ready.
160 * 0: Successful, initial negotiation all done.
161 * 1: successful, negotiation not done yet.
162 * 2: Not yet. Other things (like getting the key from
163 * Kerberos) have to happen before we can continue.
165 int cfb64_start(int dir, int server)
167 return(fb64_start(&fb[CFB], dir, server));
170 int ofb64_start(int dir, int server)
172 return(fb64_start(&fb[OFB], dir, server));
175 static int fb64_start(struct fb *fbp, int dir, int server)
184 * This is simply a request to have the other side
185 * start output (our input). He will negotiate an
186 * IV so we need not look for it.
188 state = fbp->state[dir-1];
194 state = fbp->state[dir-1];
197 else if ((state & NO_SEND_IV) == 0) {
201 if (!VALIDKEY(fbp->krbdes_key)) {
206 state &= ~NO_SEND_IV;
208 if (encrypt_debug_mode)
209 printf("Creating new feed\r\n");
211 * Create a random feed and send it over.
213 #ifndef OLD_DES_RANDOM_KEY
214 DES_random_key(&fbp->temp_feed);
217 * From des_cryp.man "If the des_check_key flag is non-zero,
218 * des_set_key will check that the key passed is
219 * of odd parity and is not a week or semi-weak key."
222 DES_random_key(fbp->temp_feed);
223 DES_set_odd_parity(fbp->temp_feed);
224 } while (DES_is_weak_key(fbp->temp_feed));
226 DES_ecb_encrypt(&fbp->temp_feed,
228 &fbp->krbdes_sched, 1);
229 p = fbp->fb_feed + 3;
233 for (x = 0; x < sizeof(DES_cblock); ++x) {
234 if ((*p++ = fbp->temp_feed[x]) == IAC)
239 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
240 telnet_net_write(fbp->fb_feed, p - fbp->fb_feed);
245 return(fbp->state[dir-1] = state);
250 * -1: some error. Negotiation is done, encryption not ready.
251 * 0: Successful, initial negotiation all done.
252 * 1: successful, negotiation not done yet.
255 int cfb64_is(unsigned char *data, int cnt)
257 return(fb64_is(data, cnt, &fb[CFB]));
260 int ofb64_is(unsigned char *data, int cnt)
262 return(fb64_is(data, cnt, &fb[OFB]));
266 int fb64_is(unsigned char *data, int cnt, struct fb *fbp)
269 int state = fbp->state[DIR_DECRYPT-1];
276 if (cnt != sizeof(DES_cblock)) {
277 if (encrypt_debug_mode)
278 printf("CFB64: initial vector failed on size\r\n");
283 if (encrypt_debug_mode)
284 printf("CFB64: initial vector received\r\n");
286 if (encrypt_debug_mode)
287 printf("Initializing Decrypt stream\r\n");
289 fb64_stream_iv(data, &fbp->streams[DIR_DECRYPT-1]);
291 p = fbp->fb_feed + 3;
292 *p++ = ENCRYPT_REPLY;
297 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
298 telnet_net_write(fbp->fb_feed, p - fbp->fb_feed);
300 state = fbp->state[DIR_DECRYPT-1] = IN_PROGRESS;
304 if (encrypt_debug_mode) {
305 printf("Unknown option type: %d\r\n", *(data-1));
312 * We failed. Send an FB64_IV_BAD option
313 * to the other side so it will know that
316 p = fbp->fb_feed + 3;
317 *p++ = ENCRYPT_REPLY;
322 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
323 telnet_net_write(fbp->fb_feed, p - fbp->fb_feed);
327 return(fbp->state[DIR_DECRYPT-1] = state);
332 * -1: some error. Negotiation is done, encryption not ready.
333 * 0: Successful, initial negotiation all done.
334 * 1: successful, negotiation not done yet.
337 int cfb64_reply(unsigned char *data, int cnt)
339 return(fb64_reply(data, cnt, &fb[CFB]));
342 int ofb64_reply(unsigned char *data, int cnt)
344 return(fb64_reply(data, cnt, &fb[OFB]));
348 int fb64_reply(unsigned char *data, int cnt, struct fb *fbp)
350 int state = fbp->state[DIR_ENCRYPT-1];
357 fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
360 state &= ~NO_RECV_IV;
361 encrypt_send_keyid(DIR_ENCRYPT, (unsigned char *)"\0", 1, 1);
365 memset(fbp->temp_feed, 0, sizeof(DES_cblock));
366 fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
371 if (encrypt_debug_mode) {
372 printf("Unknown option type: %d\r\n", data[-1]);
381 return(fbp->state[DIR_ENCRYPT-1] = state);
384 void cfb64_session(Session_Key *key, int server)
386 fb64_session(key, server, &fb[CFB]);
389 void ofb64_session(Session_Key *key, int server)
391 fb64_session(key, server, &fb[OFB]);
394 static void fb64_session(Session_Key *key, int server, struct fb *fbp)
397 if (!key || key->type != SK_DES) {
398 if (encrypt_debug_mode)
399 printf("Can't set krbdes's session key (%d != %d)\r\n",
400 key ? key->type : -1, SK_DES);
403 memcpy(fbp->krbdes_key, key->data, sizeof(DES_cblock));
405 fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_ENCRYPT-1]);
406 fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_DECRYPT-1]);
408 if (fbp->once == 0) {
409 #if !defined(OLD_DES_RANDOM_KEY) && !defined(HAVE_OPENSSL)
410 DES_init_random_number_generator(&fbp->krbdes_key);
414 DES_set_key_checked((DES_cblock *)&fbp->krbdes_key,
417 * Now look to see if krbdes_start() was was waiting for
418 * the key to show up. If so, go ahead an call it now
419 * that we have the key.
421 if (fbp->need_start) {
423 fb64_start(fbp, DIR_ENCRYPT, server);
428 * We only accept a keyid of 0. If we get a keyid of
429 * 0, then mark the state as SUCCESS.
432 int cfb64_keyid(int dir, unsigned char *kp, int *lenp)
434 return(fb64_keyid(dir, kp, lenp, &fb[CFB]));
437 int ofb64_keyid(int dir, unsigned char *kp, int *lenp)
439 return(fb64_keyid(dir, kp, lenp, &fb[OFB]));
442 int fb64_keyid(int dir, unsigned char *kp, int *lenp, struct fb *fbp)
444 int state = fbp->state[dir-1];
446 if (*lenp != 1 || (*kp != '\0')) {
456 return(fbp->state[dir-1] = state);
459 void fb64_printsub(unsigned char *data, int cnt,
460 unsigned char *buf, int buflen, char *type)
466 buf[buflen-1] = '\0'; /* make sure it's NULL terminated */
471 snprintf(lbuf, sizeof(lbuf), "%s_IV", type);
476 snprintf(lbuf, sizeof(lbuf), "%s_IV_OK", type);
481 snprintf(lbuf, sizeof(lbuf), "%s_IV_BAD", type);
486 snprintf(lbuf, sizeof(lbuf), " %d (unknown)", data[2]);
489 for (; (buflen > 0) && (*buf = *cp++); buf++)
491 for (i = 3; i < cnt; i++) {
492 snprintf(lbuf, sizeof(lbuf), " %d", data[i]);
493 for (cp = lbuf; (buflen > 0) && (*buf = *cp++); buf++)
500 void cfb64_printsub(unsigned char *data, int cnt,
501 unsigned char *buf, int buflen)
503 fb64_printsub(data, cnt, buf, buflen, "CFB64");
506 void ofb64_printsub(unsigned char *data, int cnt,
507 unsigned char *buf, int buflen)
509 fb64_printsub(data, cnt, buf, buflen, "OFB64");
512 void fb64_stream_iv(DES_cblock seed, struct stinfo *stp)
515 memcpy(stp->str_iv, seed,sizeof(DES_cblock));
516 memcpy(stp->str_output, seed, sizeof(DES_cblock));
518 DES_set_key_checked(&stp->str_ikey, &stp->str_sched);
520 stp->str_index = sizeof(DES_cblock);
523 void fb64_stream_key(DES_cblock key, struct stinfo *stp)
525 memcpy(stp->str_ikey, key, sizeof(DES_cblock));
526 DES_set_key_checked((DES_cblock*)key, &stp->str_sched);
528 memcpy(stp->str_output, stp->str_iv, sizeof(DES_cblock));
530 stp->str_index = sizeof(DES_cblock);
534 * DES 64 bit Cipher Feedback
540 * INPUT --(--------->(+)+---> DATA
546 * iV: Initial vector, 64 bits (8 bytes) long.
547 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
548 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
552 * V(n+1) = DES(On, key)
555 void cfb64_encrypt(unsigned char *s, int c)
557 struct stinfo *stp = &fb[CFB].streams[DIR_ENCRYPT-1];
560 index = stp->str_index;
562 if (index == sizeof(DES_cblock)) {
564 DES_ecb_encrypt(&stp->str_output, &b,&stp->str_sched, 1);
565 memcpy(stp->str_feed, b, sizeof(DES_cblock));
569 /* On encryption, we store (feed ^ data) which is cypher */
570 *s = stp->str_output[index] = (stp->str_feed[index] ^ *s);
574 stp->str_index = index;
577 int cfb64_decrypt(int data)
579 struct stinfo *stp = &fb[CFB].streams[DIR_DECRYPT-1];
584 * Back up one byte. It is assumed that we will
585 * never back up more than one byte. If we do, this
586 * may or may not work.
593 index = stp->str_index++;
594 if (index == sizeof(DES_cblock)) {
596 DES_ecb_encrypt(&stp->str_output,&b, &stp->str_sched, 1);
597 memcpy(stp->str_feed, b, sizeof(DES_cblock));
598 stp->str_index = 1; /* Next time will be 1 */
599 index = 0; /* But now use 0 */
602 /* On decryption we store (data) which is cypher. */
603 stp->str_output[index] = data;
604 return(data ^ stp->str_feed[index]);
608 * DES 64 bit Output Feedback
615 * INPUT -------->(+) ----> DATA
618 * iV: Initial vector, 64 bits (8 bytes) long.
619 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
620 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
623 * V(n+1) = DES(Vn, key)
627 void ofb64_encrypt(unsigned char *s, int c)
629 struct stinfo *stp = &fb[OFB].streams[DIR_ENCRYPT-1];
632 index = stp->str_index;
634 if (index == sizeof(DES_cblock)) {
636 DES_ecb_encrypt(&stp->str_feed,&b, &stp->str_sched, 1);
637 memcpy(stp->str_feed, b, sizeof(DES_cblock));
640 *s++ ^= stp->str_feed[index];
643 stp->str_index = index;
646 int ofb64_decrypt(int data)
648 struct stinfo *stp = &fb[OFB].streams[DIR_DECRYPT-1];
653 * Back up one byte. It is assumed that we will
654 * never back up more than one byte. If we do, this
655 * may or may not work.
662 index = stp->str_index++;
663 if (index == sizeof(DES_cblock)) {
665 DES_ecb_encrypt(&stp->str_feed,&b,&stp->str_sched, 1);
666 memcpy(stp->str_feed, b, sizeof(DES_cblock));
667 stp->str_index = 1; /* Next time will be 1 */
668 index = 0; /* But now use 0 */
671 return(data ^ stp->str_feed[index]);