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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $FreeBSD: src/crypto/telnet/libtelnet/enc_des.c,v 1.3.2.2 2003/02/14 22:38:13 nectar Exp $
30 * $DragonFly: src/crypto/telnet/libtelnet/enc_des.c,v 1.2 2003/06/17 04:24:37 dillon Exp $
32 * @(#)enc_des.c 8.3 (Berkeley) 5/30/95
33 * $FreeBSD: src/crypto/telnet/libtelnet/enc_des.c,v 1.3.2.2 2003/02/14 22:38:13 nectar Exp $
37 # ifdef AUTHENTICATION
38 #include <arpa/telnet.h>
39 #include <openssl/des.h>
45 #include "key-proto.h"
46 #include "misc-proto.h"
48 extern int encrypt_debug_mode
;
56 #define IN_PROGRESS (NO_SEND_IV|NO_RECV_IV|NO_KEYID)
63 Schedule krbdes_sched
;
65 unsigned char fb_feed
[64];
80 static struct fb fb
[2];
89 { "\0", 1, 0, 0, 0 }, /* default key of zero */
93 #define KEYFLAG_MASK 03
95 #define KEYFLAG_NOINIT 00
96 #define KEYFLAG_INIT 01
98 #define KEYFLAG_BAD 03
100 #define KEYFLAG_SHIFT 2
102 #define SHIFT_VAL(a,b) (KEYFLAG_SHIFT*((a)+((b)*2)))
106 #define FB64_IV_BAD 3
109 void fb64_stream_iv(Block
, struct stinfo
*);
110 void fb64_init(struct fb
*);
111 static int fb64_start(struct fb
*, int, int);
112 int fb64_is(unsigned char *, int, struct fb
*);
113 int fb64_reply(unsigned char *, int, struct fb
*);
114 static void fb64_session(Session_Key
*, int, struct fb
*);
115 void fb64_stream_key(Block
, struct stinfo
*);
116 int fb64_keyid(int, unsigned char *, int *, struct fb
*);
119 cfb64_init(int server __unused
)
122 fb
[CFB
].fb_feed
[4] = ENCTYPE_DES_CFB64
;
123 fb
[CFB
].streams
[0].str_flagshift
= SHIFT_VAL(0, CFB
);
124 fb
[CFB
].streams
[1].str_flagshift
= SHIFT_VAL(1, CFB
);
128 ofb64_init(int server __unused
)
131 fb
[OFB
].fb_feed
[4] = ENCTYPE_DES_OFB64
;
132 fb
[CFB
].streams
[0].str_flagshift
= SHIFT_VAL(0, OFB
);
133 fb
[CFB
].streams
[1].str_flagshift
= SHIFT_VAL(1, OFB
);
137 fb64_init(struct fb
*fbp
)
139 memset((void *)fbp
, 0, sizeof(*fbp
));
140 fbp
->state
[0] = fbp
->state
[1] = FAILED
;
141 fbp
->fb_feed
[0] = IAC
;
142 fbp
->fb_feed
[1] = SB
;
143 fbp
->fb_feed
[2] = TELOPT_ENCRYPT
;
144 fbp
->fb_feed
[3] = ENCRYPT_IS
;
149 * -1: some error. Negotiation is done, encryption not ready.
150 * 0: Successful, initial negotiation all done.
151 * 1: successful, negotiation not done yet.
152 * 2: Not yet. Other things (like getting the key from
153 * Kerberos) have to happen before we can continue.
156 cfb64_start(int dir
, int server
)
158 return(fb64_start(&fb
[CFB
], dir
, server
));
162 ofb64_start(int dir
, int server
)
164 return(fb64_start(&fb
[OFB
], dir
, server
));
168 fb64_start(struct fb
*fbp
, int dir
, int server __unused
)
177 * This is simply a request to have the other side
178 * start output (our input). He will negotiate an
179 * IV so we need not look for it.
181 state
= fbp
->state
[dir
-1];
187 state
= fbp
->state
[dir
-1];
190 else if ((state
& NO_SEND_IV
) == 0)
193 if (!VALIDKEY(fbp
->krbdes_key
)) {
197 state
&= ~NO_SEND_IV
;
199 if (encrypt_debug_mode
)
200 printf("Creating new feed\r\n");
202 * Create a random feed and send it over.
204 DES_random_key((Block
*)fbp
->temp_feed
);
205 DES_ecb_encrypt((Block
*)fbp
->temp_feed
, (Block
*)fbp
->temp_feed
,
206 &fbp
->krbdes_sched
, 1);
207 p
= fbp
->fb_feed
+ 3;
211 for (x
= 0; x
< sizeof(Block
); ++x
) {
212 if ((*p
++ = fbp
->temp_feed
[x
]) == IAC
)
217 printsub('>', &fbp
->fb_feed
[2], p
- &fbp
->fb_feed
[2]);
218 net_write(fbp
->fb_feed
, p
- fbp
->fb_feed
);
223 return(fbp
->state
[dir
-1] = state
);
228 * -1: some error. Negotiation is done, encryption not ready.
229 * 0: Successful, initial negotiation all done.
230 * 1: successful, negotiation not done yet.
233 cfb64_is(unsigned char *data
, int cnt
)
235 return(fb64_is(data
, cnt
, &fb
[CFB
]));
239 ofb64_is(unsigned char *data
, int cnt
)
241 return(fb64_is(data
, cnt
, &fb
[OFB
]));
245 fb64_is(unsigned char *data
, int cnt
, struct fb
*fbp
)
248 int state
= fbp
->state
[DIR_DECRYPT
-1];
255 if (cnt
!= sizeof(Block
)) {
256 if (encrypt_debug_mode
)
257 printf("CFB64: initial vector failed on size\r\n");
262 if (encrypt_debug_mode
)
263 printf("CFB64: initial vector received\r\n");
265 if (encrypt_debug_mode
)
266 printf("Initializing Decrypt stream\r\n");
268 fb64_stream_iv((void *)data
, &fbp
->streams
[DIR_DECRYPT
-1]);
270 p
= fbp
->fb_feed
+ 3;
271 *p
++ = ENCRYPT_REPLY
;
276 printsub('>', &fbp
->fb_feed
[2], p
- &fbp
->fb_feed
[2]);
277 net_write(fbp
->fb_feed
, p
- fbp
->fb_feed
);
279 state
= fbp
->state
[DIR_DECRYPT
-1] = IN_PROGRESS
;
283 if (encrypt_debug_mode
) {
284 printf("Unknown option type: %d\r\n", *(data
-1));
291 * We failed. Send an FB64_IV_BAD option
292 * to the other side so it will know that
295 p
= fbp
->fb_feed
+ 3;
296 *p
++ = ENCRYPT_REPLY
;
301 printsub('>', &fbp
->fb_feed
[2], p
- &fbp
->fb_feed
[2]);
302 net_write(fbp
->fb_feed
, p
- fbp
->fb_feed
);
306 return(fbp
->state
[DIR_DECRYPT
-1] = state
);
311 * -1: some error. Negotiation is done, encryption not ready.
312 * 0: Successful, initial negotiation all done.
313 * 1: successful, negotiation not done yet.
316 cfb64_reply(unsigned char *data
, int cnt
)
318 return(fb64_reply(data
, cnt
, &fb
[CFB
]));
322 ofb64_reply(unsigned char *data
, int cnt
)
324 return(fb64_reply(data
, cnt
, &fb
[OFB
]));
328 fb64_reply(unsigned char *data
, int cnt
, struct fb
*fbp
)
330 int state
= fbp
->state
[DIR_ENCRYPT
-1];
337 fb64_stream_iv(fbp
->temp_feed
, &fbp
->streams
[DIR_ENCRYPT
-1]);
340 state
&= ~NO_RECV_IV
;
341 encrypt_send_keyid(DIR_ENCRYPT
, "\0", 1, 1);
345 memset(fbp
->temp_feed
, 0, sizeof(Block
));
346 fb64_stream_iv(fbp
->temp_feed
, &fbp
->streams
[DIR_ENCRYPT
-1]);
351 if (encrypt_debug_mode
) {
352 printf("Unknown option type: %d\r\n", data
[-1]);
361 return(fbp
->state
[DIR_ENCRYPT
-1] = state
);
365 cfb64_session(Session_Key
*key
, int server
)
367 fb64_session(key
, server
, &fb
[CFB
]);
371 ofb64_session(Session_Key
*key
, int server
)
373 fb64_session(key
, server
, &fb
[OFB
]);
377 fb64_session(Session_Key
*key
, int server
, struct fb
*fbp
)
379 if (!key
|| key
->type
!= SK_DES
) {
380 if (encrypt_debug_mode
)
381 printf("Can't set krbdes's session key (%d != %d)\r\n",
382 key
? key
->type
: -1, SK_DES
);
385 memmove((void *)fbp
->krbdes_key
, (void *)key
->data
, sizeof(Block
));
387 fb64_stream_key(fbp
->krbdes_key
, &fbp
->streams
[DIR_ENCRYPT
-1]);
388 fb64_stream_key(fbp
->krbdes_key
, &fbp
->streams
[DIR_DECRYPT
-1]);
390 DES_key_sched((Block
*)fbp
->krbdes_key
, &fbp
->krbdes_sched
);
392 * Now look to see if krbdes_start() was was waiting for
393 * the key to show up. If so, go ahead an call it now
394 * that we have the key.
396 if (fbp
->need_start
) {
398 fb64_start(fbp
, DIR_ENCRYPT
, server
);
403 * We only accept a keyid of 0. If we get a keyid of
404 * 0, then mark the state as SUCCESS.
407 cfb64_keyid(int dir
, unsigned char *kp
, int *lenp
)
409 return(fb64_keyid(dir
, kp
, lenp
, &fb
[CFB
]));
413 ofb64_keyid(int dir
, unsigned char *kp
, int *lenp
)
415 return(fb64_keyid(dir
, kp
, lenp
, &fb
[OFB
]));
419 fb64_keyid(int dir
, unsigned char *kp
, int *lenp
, struct fb
*fbp
)
421 int state
= fbp
->state
[dir
-1];
423 if (*lenp
!= 1 || (*kp
!= '\0')) {
433 return(fbp
->state
[dir
-1] = state
);
437 fb64_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
, const char *type
)
443 buf
[buflen
-1] = '\0'; /* make sure it's NULL terminated */
448 sprintf(lbuf
, "%s_IV", type
);
453 sprintf(lbuf
, "%s_IV_OK", type
);
458 sprintf(lbuf
, "%s_IV_BAD", type
);
463 sprintf(lbuf
, " %d (unknown)", data
[2]);
466 for (; (buflen
> 0) && (*buf
= *cp
++); buf
++)
468 for (i
= 3; i
< cnt
; i
++) {
469 sprintf(lbuf
, " %d", data
[i
]);
470 for (cp
= lbuf
; (buflen
> 0) && (*buf
= *cp
++); buf
++)
478 cfb64_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
480 fb64_printsub(data
, cnt
, buf
, buflen
, "CFB64");
484 ofb64_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
486 fb64_printsub(data
, cnt
, buf
, buflen
, "OFB64");
490 fb64_stream_iv(Block seed
, struct stinfo
*stp
)
493 memmove((void *)stp
->str_iv
, (void *)seed
, sizeof(Block
));
494 memmove((void *)stp
->str_output
, (void *)seed
, sizeof(Block
));
496 DES_key_sched((Block
*)stp
->str_ikey
, &stp
->str_sched
);
498 stp
->str_index
= sizeof(Block
);
502 fb64_stream_key(Block key
, struct stinfo
*stp
)
504 memmove((void *)stp
->str_ikey
, (void *)key
, sizeof(Block
));
505 DES_key_sched((Block
*)key
, &stp
->str_sched
);
507 memmove((void *)stp
->str_output
, (void *)stp
->str_iv
, sizeof(Block
));
509 stp
->str_index
= sizeof(Block
);
513 * DES 64 bit Cipher Feedback
519 * INPUT --(--------->(+)+---> DATA
525 * iV: Initial vector, 64 bits (8 bytes) long.
526 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
527 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
531 * V(n+1) = DES(On, key)
535 cfb64_encrypt(unsigned char *s
, int c
)
537 struct stinfo
*stp
= &fb
[CFB
].streams
[DIR_ENCRYPT
-1];
540 idx
= stp
->str_index
;
542 if (idx
== sizeof(Block
)) {
544 DES_ecb_encrypt((Block
*)stp
->str_output
, (Block
*)b
, &stp
->str_sched
, 1);
545 memmove((void *)stp
->str_feed
, (void *)b
, sizeof(Block
));
549 /* On encryption, we store (feed ^ data) which is cypher */
550 *s
= stp
->str_output
[idx
] = (stp
->str_feed
[idx
] ^ *s
);
554 stp
->str_index
= idx
;
558 cfb64_decrypt(int data
)
560 struct stinfo
*stp
= &fb
[CFB
].streams
[DIR_DECRYPT
-1];
565 * Back up one byte. It is assumed that we will
566 * never back up more than one byte. If we do, this
567 * may or may not work.
574 idx
= stp
->str_index
++;
575 if (idx
== sizeof(Block
)) {
577 DES_ecb_encrypt((Block
*)stp
->str_output
, (Block
*)b
, &stp
->str_sched
, 1);
578 memmove((void *)stp
->str_feed
, (void *)b
, sizeof(Block
));
579 stp
->str_index
= 1; /* Next time will be 1 */
580 idx
= 0; /* But now use 0 */
583 /* On decryption we store (data) which is cypher. */
584 stp
->str_output
[idx
] = data
;
585 return(data
^ stp
->str_feed
[idx
]);
589 * DES 64 bit Output Feedback
596 * INPUT -------->(+) ----> DATA
599 * iV: Initial vector, 64 bits (8 bytes) long.
600 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
601 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
604 * V(n+1) = DES(Vn, key)
608 ofb64_encrypt(unsigned char *s
, int c
)
610 struct stinfo
*stp
= &fb
[OFB
].streams
[DIR_ENCRYPT
-1];
613 idx
= stp
->str_index
;
615 if (idx
== sizeof(Block
)) {
617 DES_ecb_encrypt((Block
*)stp
->str_feed
, (Block
*)b
, &stp
->str_sched
, 1);
618 memmove((void *)stp
->str_feed
, (void *)b
, sizeof(Block
));
621 *s
++ ^= stp
->str_feed
[idx
];
624 stp
->str_index
= idx
;
628 ofb64_decrypt(int data
)
630 struct stinfo
*stp
= &fb
[OFB
].streams
[DIR_DECRYPT
-1];
635 * Back up one byte. It is assumed that we will
636 * never back up more than one byte. If we do, this
637 * may or may not work.
644 idx
= stp
->str_index
++;
645 if (idx
== sizeof(Block
)) {
647 DES_ecb_encrypt((Block
*)stp
->str_feed
, (Block
*)b
, &stp
->str_sched
, 1);
648 memmove((void *)stp
->str_feed
, (void *)b
, sizeof(Block
));
649 stp
->str_index
= 1; /* Next time will be 1 */
650 idx
= 0; /* But now use 0 */
653 return(data
^ stp
->str_feed
[idx
]);
655 # endif /* AUTHENTICATION */
656 #endif /* ENCRYPTION */