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,v 1.21 2002/09/10 20:03:47 joda Exp $");
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_new_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_key_sched(&fbp
->krbdes_key
, fbp
->krbdes_sched
);
416 * Now look to see if krbdes_start() was was waiting for
417 * the key to show up. If so, go ahead an call it now
418 * that we have the key.
420 if (fbp
->need_start
) {
422 fb64_start(fbp
, DIR_ENCRYPT
, server
);
427 * We only accept a keyid of 0. If we get a keyid of
428 * 0, then mark the state as SUCCESS.
431 int cfb64_keyid(int dir
, unsigned char *kp
, int *lenp
)
433 return(fb64_keyid(dir
, kp
, lenp
, &fb
[CFB
]));
436 int ofb64_keyid(int dir
, unsigned char *kp
, int *lenp
)
438 return(fb64_keyid(dir
, kp
, lenp
, &fb
[OFB
]));
441 int fb64_keyid(int dir
, unsigned char *kp
, int *lenp
, struct fb
*fbp
)
443 int state
= fbp
->state
[dir
-1];
445 if (*lenp
!= 1 || (*kp
!= '\0')) {
455 return(fbp
->state
[dir
-1] = state
);
458 void fb64_printsub(unsigned char *data
, int cnt
,
459 unsigned char *buf
, int buflen
, char *type
)
465 buf
[buflen
-1] = '\0'; /* make sure it's NULL terminated */
470 snprintf(lbuf
, sizeof(lbuf
), "%s_IV", type
);
475 snprintf(lbuf
, sizeof(lbuf
), "%s_IV_OK", type
);
480 snprintf(lbuf
, sizeof(lbuf
), "%s_IV_BAD", type
);
485 snprintf(lbuf
, sizeof(lbuf
), " %d (unknown)", data
[2]);
488 for (; (buflen
> 0) && (*buf
= *cp
++); buf
++)
490 for (i
= 3; i
< cnt
; i
++) {
491 snprintf(lbuf
, sizeof(lbuf
), " %d", data
[i
]);
492 for (cp
= lbuf
; (buflen
> 0) && (*buf
= *cp
++); buf
++)
499 void cfb64_printsub(unsigned char *data
, int cnt
,
500 unsigned char *buf
, int buflen
)
502 fb64_printsub(data
, cnt
, buf
, buflen
, "CFB64");
505 void ofb64_printsub(unsigned char *data
, int cnt
,
506 unsigned char *buf
, int buflen
)
508 fb64_printsub(data
, cnt
, buf
, buflen
, "OFB64");
511 void fb64_stream_iv(des_cblock seed
, struct stinfo
*stp
)
514 memcpy(stp
->str_iv
, seed
,sizeof(des_cblock
));
515 memcpy(stp
->str_output
, seed
, sizeof(des_cblock
));
517 des_key_sched(&stp
->str_ikey
, stp
->str_sched
);
519 stp
->str_index
= sizeof(des_cblock
);
522 void fb64_stream_key(des_cblock key
, struct stinfo
*stp
)
524 memcpy(stp
->str_ikey
, key
, sizeof(des_cblock
));
525 des_key_sched((des_cblock
*)key
, stp
->str_sched
);
527 memcpy(stp
->str_output
, stp
->str_iv
, sizeof(des_cblock
));
529 stp
->str_index
= sizeof(des_cblock
);
533 * DES 64 bit Cipher Feedback
539 * INPUT --(--------->(+)+---> DATA
545 * iV: Initial vector, 64 bits (8 bytes) long.
546 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
547 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
551 * V(n+1) = DES(On, key)
554 void cfb64_encrypt(unsigned char *s
, int c
)
556 struct stinfo
*stp
= &fb
[CFB
].streams
[DIR_ENCRYPT
-1];
559 index
= stp
->str_index
;
561 if (index
== sizeof(des_cblock
)) {
563 des_ecb_encrypt(&stp
->str_output
, &b
,stp
->str_sched
, 1);
564 memcpy(stp
->str_feed
, b
, sizeof(des_cblock
));
568 /* On encryption, we store (feed ^ data) which is cypher */
569 *s
= stp
->str_output
[index
] = (stp
->str_feed
[index
] ^ *s
);
573 stp
->str_index
= index
;
576 int cfb64_decrypt(int data
)
578 struct stinfo
*stp
= &fb
[CFB
].streams
[DIR_DECRYPT
-1];
583 * Back up one byte. It is assumed that we will
584 * never back up more than one byte. If we do, this
585 * may or may not work.
592 index
= stp
->str_index
++;
593 if (index
== sizeof(des_cblock
)) {
595 des_ecb_encrypt(&stp
->str_output
,&b
, stp
->str_sched
, 1);
596 memcpy(stp
->str_feed
, b
, sizeof(des_cblock
));
597 stp
->str_index
= 1; /* Next time will be 1 */
598 index
= 0; /* But now use 0 */
601 /* On decryption we store (data) which is cypher. */
602 stp
->str_output
[index
] = data
;
603 return(data
^ stp
->str_feed
[index
]);
607 * DES 64 bit Output Feedback
614 * INPUT -------->(+) ----> DATA
617 * iV: Initial vector, 64 bits (8 bytes) long.
618 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
619 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
622 * V(n+1) = DES(Vn, key)
626 void ofb64_encrypt(unsigned char *s
, int c
)
628 struct stinfo
*stp
= &fb
[OFB
].streams
[DIR_ENCRYPT
-1];
631 index
= stp
->str_index
;
633 if (index
== sizeof(des_cblock
)) {
635 des_ecb_encrypt(&stp
->str_feed
,&b
, stp
->str_sched
, 1);
636 memcpy(stp
->str_feed
, b
, sizeof(des_cblock
));
639 *s
++ ^= stp
->str_feed
[index
];
642 stp
->str_index
= index
;
645 int ofb64_decrypt(int data
)
647 struct stinfo
*stp
= &fb
[OFB
].streams
[DIR_DECRYPT
-1];
652 * Back up one byte. It is assumed that we will
653 * never back up more than one byte. If we do, this
654 * may or may not work.
661 index
= stp
->str_index
++;
662 if (index
== sizeof(des_cblock
)) {
664 des_ecb_encrypt(&stp
->str_feed
,&b
,stp
->str_sched
, 1);
665 memcpy(stp
->str_feed
, b
, sizeof(des_cblock
));
666 stp
->str_index
= 1; /* Next time will be 1 */
667 index
= 0; /* But now use 0 */
670 return(data
^ stp
->str_feed
[index
]);