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 static const char sccsid
[] = "@(#)enc_des.c 8.3 (Berkeley) 5/30/95";
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/contrib/telnet/libtelnet/enc_des.c,v 1.10 2003/05/04 02:54:48 obrien Exp $");
43 # ifdef AUTHENTICATION
44 #include <arpa/telnet.h>
45 #include <openssl/des.h>
51 #include "key-proto.h"
52 #include "misc-proto.h"
54 extern int encrypt_debug_mode
;
62 #define IN_PROGRESS (NO_SEND_IV|NO_RECV_IV|NO_KEYID)
69 Schedule krbdes_sched
;
71 unsigned char fb_feed
[64];
86 static struct fb fb
[2];
95 { "\0", 1, 0, 0, 0 }, /* default key of zero */
99 #define KEYFLAG_MASK 03
101 #define KEYFLAG_NOINIT 00
102 #define KEYFLAG_INIT 01
103 #define KEYFLAG_OK 02
104 #define KEYFLAG_BAD 03
106 #define KEYFLAG_SHIFT 2
108 #define SHIFT_VAL(a,b) (KEYFLAG_SHIFT*((a)+((b)*2)))
112 #define FB64_IV_BAD 3
115 void fb64_stream_iv(Block
, struct stinfo
*);
116 void fb64_init(struct fb
*);
117 static int fb64_start(struct fb
*, int, int);
118 int fb64_is(unsigned char *, int, struct fb
*);
119 int fb64_reply(unsigned char *, int, struct fb
*);
120 static void fb64_session(Session_Key
*, int, struct fb
*);
121 void fb64_stream_key(Block
, struct stinfo
*);
122 int fb64_keyid(int, unsigned char *, int *, struct fb
*);
125 cfb64_init(int server __unused
)
128 fb
[CFB
].fb_feed
[4] = ENCTYPE_DES_CFB64
;
129 fb
[CFB
].streams
[0].str_flagshift
= SHIFT_VAL(0, CFB
);
130 fb
[CFB
].streams
[1].str_flagshift
= SHIFT_VAL(1, CFB
);
134 ofb64_init(int server __unused
)
137 fb
[OFB
].fb_feed
[4] = ENCTYPE_DES_OFB64
;
138 fb
[CFB
].streams
[0].str_flagshift
= SHIFT_VAL(0, OFB
);
139 fb
[CFB
].streams
[1].str_flagshift
= SHIFT_VAL(1, OFB
);
143 fb64_init(struct fb
*fbp
)
145 memset((void *)fbp
, 0, sizeof(*fbp
));
146 fbp
->state
[0] = fbp
->state
[1] = FAILED
;
147 fbp
->fb_feed
[0] = IAC
;
148 fbp
->fb_feed
[1] = SB
;
149 fbp
->fb_feed
[2] = TELOPT_ENCRYPT
;
150 fbp
->fb_feed
[3] = ENCRYPT_IS
;
155 * -1: some error. Negotiation is done, encryption not ready.
156 * 0: Successful, initial negotiation all done.
157 * 1: successful, negotiation not done yet.
158 * 2: Not yet. Other things (like getting the key from
159 * Kerberos) have to happen before we can continue.
162 cfb64_start(int dir
, int server
)
164 return(fb64_start(&fb
[CFB
], dir
, server
));
168 ofb64_start(int dir
, int server
)
170 return(fb64_start(&fb
[OFB
], dir
, server
));
174 fb64_start(struct fb
*fbp
, int dir
, int server __unused
)
183 * This is simply a request to have the other side
184 * start output (our input). He will negotiate an
185 * IV so we need not look for it.
187 state
= fbp
->state
[dir
-1];
193 state
= fbp
->state
[dir
-1];
196 else if ((state
& NO_SEND_IV
) == 0)
199 if (!VALIDKEY(fbp
->krbdes_key
)) {
203 state
&= ~NO_SEND_IV
;
205 if (encrypt_debug_mode
)
206 printf("Creating new feed\r\n");
208 * Create a random feed and send it over.
210 des_random_key((Block
*)fbp
->temp_feed
);
211 des_ecb_encrypt((Block
*)fbp
->temp_feed
, (Block
*)fbp
->temp_feed
,
212 fbp
->krbdes_sched
, 1);
213 p
= fbp
->fb_feed
+ 3;
217 for (x
= 0; x
< sizeof(Block
); ++x
) {
218 if ((*p
++ = fbp
->temp_feed
[x
]) == IAC
)
223 printsub('>', &fbp
->fb_feed
[2], p
- &fbp
->fb_feed
[2]);
224 net_write(fbp
->fb_feed
, p
- fbp
->fb_feed
);
229 return(fbp
->state
[dir
-1] = state
);
234 * -1: some error. Negotiation is done, encryption not ready.
235 * 0: Successful, initial negotiation all done.
236 * 1: successful, negotiation not done yet.
239 cfb64_is(unsigned char *data
, int cnt
)
241 return(fb64_is(data
, cnt
, &fb
[CFB
]));
245 ofb64_is(unsigned char *data
, int cnt
)
247 return(fb64_is(data
, cnt
, &fb
[OFB
]));
251 fb64_is(unsigned char *data
, int cnt
, struct fb
*fbp
)
254 int state
= fbp
->state
[DIR_DECRYPT
-1];
261 if (cnt
!= sizeof(Block
)) {
262 if (encrypt_debug_mode
)
263 printf("CFB64: initial vector failed on size\r\n");
268 if (encrypt_debug_mode
)
269 printf("CFB64: initial vector received\r\n");
271 if (encrypt_debug_mode
)
272 printf("Initializing Decrypt stream\r\n");
274 fb64_stream_iv((void *)data
, &fbp
->streams
[DIR_DECRYPT
-1]);
276 p
= fbp
->fb_feed
+ 3;
277 *p
++ = ENCRYPT_REPLY
;
282 printsub('>', &fbp
->fb_feed
[2], p
- &fbp
->fb_feed
[2]);
283 net_write(fbp
->fb_feed
, p
- fbp
->fb_feed
);
285 state
= fbp
->state
[DIR_DECRYPT
-1] = IN_PROGRESS
;
289 if (encrypt_debug_mode
) {
290 printf("Unknown option type: %d\r\n", *(data
-1));
297 * We failed. Send an FB64_IV_BAD option
298 * to the other side so it will know that
301 p
= fbp
->fb_feed
+ 3;
302 *p
++ = ENCRYPT_REPLY
;
307 printsub('>', &fbp
->fb_feed
[2], p
- &fbp
->fb_feed
[2]);
308 net_write(fbp
->fb_feed
, p
- fbp
->fb_feed
);
312 return(fbp
->state
[DIR_DECRYPT
-1] = state
);
317 * -1: some error. Negotiation is done, encryption not ready.
318 * 0: Successful, initial negotiation all done.
319 * 1: successful, negotiation not done yet.
322 cfb64_reply(unsigned char *data
, int cnt
)
324 return(fb64_reply(data
, cnt
, &fb
[CFB
]));
328 ofb64_reply(unsigned char *data
, int cnt
)
330 return(fb64_reply(data
, cnt
, &fb
[OFB
]));
334 fb64_reply(unsigned char *data
, int cnt
, struct fb
*fbp
)
336 int state
= fbp
->state
[DIR_ENCRYPT
-1];
343 fb64_stream_iv(fbp
->temp_feed
, &fbp
->streams
[DIR_ENCRYPT
-1]);
346 state
&= ~NO_RECV_IV
;
347 encrypt_send_keyid(DIR_ENCRYPT
, "\0", 1, 1);
351 memset(fbp
->temp_feed
, 0, sizeof(Block
));
352 fb64_stream_iv(fbp
->temp_feed
, &fbp
->streams
[DIR_ENCRYPT
-1]);
357 if (encrypt_debug_mode
) {
358 printf("Unknown option type: %d\r\n", data
[-1]);
367 return(fbp
->state
[DIR_ENCRYPT
-1] = state
);
371 cfb64_session(Session_Key
*key
, int server
)
373 fb64_session(key
, server
, &fb
[CFB
]);
377 ofb64_session(Session_Key
*key
, int server
)
379 fb64_session(key
, server
, &fb
[OFB
]);
383 fb64_session(Session_Key
*key
, int server
, struct fb
*fbp
)
385 if (!key
|| key
->type
!= SK_DES
) {
386 if (encrypt_debug_mode
)
387 printf("Can't set krbdes's session key (%d != %d)\r\n",
388 key
? key
->type
: -1, SK_DES
);
391 memmove((void *)fbp
->krbdes_key
, (void *)key
->data
, sizeof(Block
));
393 fb64_stream_key(fbp
->krbdes_key
, &fbp
->streams
[DIR_ENCRYPT
-1]);
394 fb64_stream_key(fbp
->krbdes_key
, &fbp
->streams
[DIR_DECRYPT
-1]);
396 des_key_sched((Block
*)fbp
->krbdes_key
, fbp
->krbdes_sched
);
398 * Now look to see if krbdes_start() was was waiting for
399 * the key to show up. If so, go ahead an call it now
400 * that we have the key.
402 if (fbp
->need_start
) {
404 fb64_start(fbp
, DIR_ENCRYPT
, server
);
409 * We only accept a keyid of 0. If we get a keyid of
410 * 0, then mark the state as SUCCESS.
413 cfb64_keyid(int dir
, unsigned char *kp
, int *lenp
)
415 return(fb64_keyid(dir
, kp
, lenp
, &fb
[CFB
]));
419 ofb64_keyid(int dir
, unsigned char *kp
, int *lenp
)
421 return(fb64_keyid(dir
, kp
, lenp
, &fb
[OFB
]));
425 fb64_keyid(int dir
, unsigned char *kp
, int *lenp
, struct fb
*fbp
)
427 int state
= fbp
->state
[dir
-1];
429 if (*lenp
!= 1 || (*kp
!= '\0')) {
439 return(fbp
->state
[dir
-1] = state
);
443 fb64_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
, const char *type
)
449 buf
[buflen
-1] = '\0'; /* make sure it's NULL terminated */
454 sprintf(lbuf
, "%s_IV", type
);
459 sprintf(lbuf
, "%s_IV_OK", type
);
464 sprintf(lbuf
, "%s_IV_BAD", type
);
469 sprintf(lbuf
, " %d (unknown)", data
[2]);
472 for (; (buflen
> 0) && (*buf
= *cp
++); buf
++)
474 for (i
= 3; i
< cnt
; i
++) {
475 sprintf(lbuf
, " %d", data
[i
]);
476 for (cp
= lbuf
; (buflen
> 0) && (*buf
= *cp
++); buf
++)
484 cfb64_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
486 fb64_printsub(data
, cnt
, buf
, buflen
, "CFB64");
490 ofb64_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
492 fb64_printsub(data
, cnt
, buf
, buflen
, "OFB64");
496 fb64_stream_iv(Block seed
, struct stinfo
*stp
)
499 memmove((void *)stp
->str_iv
, (void *)seed
, sizeof(Block
));
500 memmove((void *)stp
->str_output
, (void *)seed
, sizeof(Block
));
502 des_key_sched((Block
*)stp
->str_ikey
, stp
->str_sched
);
504 stp
->str_index
= sizeof(Block
);
508 fb64_stream_key(Block key
, struct stinfo
*stp
)
510 memmove((void *)stp
->str_ikey
, (void *)key
, sizeof(Block
));
511 des_key_sched((Block
*)key
, stp
->str_sched
);
513 memmove((void *)stp
->str_output
, (void *)stp
->str_iv
, sizeof(Block
));
515 stp
->str_index
= sizeof(Block
);
519 * DES 64 bit Cipher Feedback
525 * INPUT --(--------->(+)+---> DATA
531 * iV: Initial vector, 64 bits (8 bytes) long.
532 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
533 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
537 * V(n+1) = DES(On, key)
541 cfb64_encrypt(unsigned char *s
, int c
)
543 struct stinfo
*stp
= &fb
[CFB
].streams
[DIR_ENCRYPT
-1];
546 idx
= stp
->str_index
;
548 if (idx
== sizeof(Block
)) {
550 des_ecb_encrypt((Block
*)stp
->str_output
, (Block
*)b
, stp
->str_sched
, 1);
551 memmove((void *)stp
->str_feed
, (void *)b
, sizeof(Block
));
555 /* On encryption, we store (feed ^ data) which is cypher */
556 *s
= stp
->str_output
[idx
] = (stp
->str_feed
[idx
] ^ *s
);
560 stp
->str_index
= idx
;
564 cfb64_decrypt(int data
)
566 struct stinfo
*stp
= &fb
[CFB
].streams
[DIR_DECRYPT
-1];
571 * Back up one byte. It is assumed that we will
572 * never back up more than one byte. If we do, this
573 * may or may not work.
580 idx
= stp
->str_index
++;
581 if (idx
== sizeof(Block
)) {
583 des_ecb_encrypt((Block
*)stp
->str_output
, (Block
*)b
, stp
->str_sched
, 1);
584 memmove((void *)stp
->str_feed
, (void *)b
, sizeof(Block
));
585 stp
->str_index
= 1; /* Next time will be 1 */
586 idx
= 0; /* But now use 0 */
589 /* On decryption we store (data) which is cypher. */
590 stp
->str_output
[idx
] = data
;
591 return(data
^ stp
->str_feed
[idx
]);
595 * DES 64 bit Output Feedback
602 * INPUT -------->(+) ----> DATA
605 * iV: Initial vector, 64 bits (8 bytes) long.
606 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
607 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
610 * V(n+1) = DES(Vn, key)
614 ofb64_encrypt(unsigned char *s
, int c
)
616 struct stinfo
*stp
= &fb
[OFB
].streams
[DIR_ENCRYPT
-1];
619 idx
= stp
->str_index
;
621 if (idx
== sizeof(Block
)) {
623 des_ecb_encrypt((Block
*)stp
->str_feed
, (Block
*)b
, stp
->str_sched
, 1);
624 memmove((void *)stp
->str_feed
, (void *)b
, sizeof(Block
));
627 *s
++ ^= stp
->str_feed
[idx
];
630 stp
->str_index
= idx
;
634 ofb64_decrypt(int data
)
636 struct stinfo
*stp
= &fb
[OFB
].streams
[DIR_DECRYPT
-1];
641 * Back up one byte. It is assumed that we will
642 * never back up more than one byte. If we do, this
643 * may or may not work.
650 idx
= stp
->str_index
++;
651 if (idx
== sizeof(Block
)) {
653 des_ecb_encrypt((Block
*)stp
->str_feed
, (Block
*)b
, stp
->str_sched
, 1);
654 memmove((void *)stp
->str_feed
, (void *)b
, sizeof(Block
));
655 stp
->str_index
= 1; /* Next time will be 1 */
656 idx
= 0; /* But now use 0 */
659 return(data
^ stp
->str_feed
[idx
]);
661 # endif /* AUTHENTICATION */
662 #endif /* ENCRYPTION */