No need to call DES_init_random_number_generator, hcrypto is sane now.
[heimdal.git] / appl / telnet / libtelnet / enc_des.c
blob970f3513c03f85bec0a322cec09fe2014f781492
1 /*-
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
7 * are met:
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
31 * SUCH DAMAGE.
34 #include <config.h>
36 RCSID("$Id$");
38 #if defined(AUTHENTICATION) && defined(ENCRYPTION) && defined(DES_ENCRYPTION)
39 #include <arpa/telnet.h>
40 #include <stdio.h>
41 #ifdef __STDC__
42 #include <stdlib.h>
43 #include <string.h>
44 #endif
45 #include <roken.h>
46 #ifdef SOCKS
47 #include <socks.h>
48 #endif
50 #include "encrypt.h"
51 #include "misc-proto.h"
53 #include "crypto-headers.h"
55 extern int encrypt_debug_mode;
57 #define CFB 0
58 #define OFB 1
60 #define NO_SEND_IV 1
61 #define NO_RECV_IV 2
62 #define NO_KEYID 4
63 #define IN_PROGRESS (NO_SEND_IV|NO_RECV_IV|NO_KEYID)
64 #define SUCCESS 0
65 #define FAILED -1
68 struct stinfo {
69 DES_cblock str_output;
70 DES_cblock str_feed;
71 DES_cblock str_iv;
72 DES_cblock str_ikey;
73 DES_key_schedule str_sched;
74 int str_index;
75 int str_flagshift;
78 struct fb {
79 DES_cblock krbdes_key;
80 DES_key_schedule krbdes_sched;
81 DES_cblock temp_feed;
82 unsigned char fb_feed[64];
83 int need_start;
84 int state[2];
85 int keyid[2];
86 struct stinfo streams[2];
89 static struct fb fb[2];
91 struct keyidlist {
92 char *keyid;
93 int keyidlen;
94 char *key;
95 int keylen;
96 int flags;
97 } keyidlist [] = {
98 { "\0", 1, 0, 0, 0 }, /* default key of zero */
99 { 0, 0, 0, 0, 0 }
102 #define KEYFLAG_MASK 03
104 #define KEYFLAG_NOINIT 00
105 #define KEYFLAG_INIT 01
106 #define KEYFLAG_OK 02
107 #define KEYFLAG_BAD 03
109 #define KEYFLAG_SHIFT 2
111 #define SHIFT_VAL(a,b) (KEYFLAG_SHIFT*((a)+((b)*2)))
113 #define FB64_IV 1
114 #define FB64_IV_OK 2
115 #define FB64_IV_BAD 3
118 void fb64_stream_iv (DES_cblock, struct stinfo *);
119 void fb64_init (struct fb *);
120 static int fb64_start (struct fb *, int, int);
121 int fb64_is (unsigned char *, int, struct fb *);
122 int fb64_reply (unsigned char *, int, struct fb *);
123 static void fb64_session (Session_Key *, int, struct fb *);
124 void fb64_stream_key (DES_cblock, struct stinfo *);
125 int fb64_keyid (int, unsigned char *, int *, struct fb *);
126 void fb64_printsub(unsigned char *, size_t ,
127 unsigned char *, size_t , char *);
129 void cfb64_init(int server)
131 fb64_init(&fb[CFB]);
132 fb[CFB].fb_feed[4] = ENCTYPE_DES_CFB64;
133 fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, CFB);
134 fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, CFB);
138 void ofb64_init(int server)
140 fb64_init(&fb[OFB]);
141 fb[OFB].fb_feed[4] = ENCTYPE_DES_OFB64;
142 fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, OFB);
143 fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, OFB);
146 void fb64_init(struct fb *fbp)
148 memset(fbp,0, sizeof(*fbp));
149 fbp->state[0] = fbp->state[1] = FAILED;
150 fbp->fb_feed[0] = IAC;
151 fbp->fb_feed[1] = SB;
152 fbp->fb_feed[2] = TELOPT_ENCRYPT;
153 fbp->fb_feed[3] = ENCRYPT_IS;
157 * Returns:
158 * -1: some error. Negotiation is done, encryption not ready.
159 * 0: Successful, initial negotiation all done.
160 * 1: successful, negotiation not done yet.
161 * 2: Not yet. Other things (like getting the key from
162 * Kerberos) have to happen before we can continue.
164 int cfb64_start(int dir, int server)
166 return(fb64_start(&fb[CFB], dir, server));
169 int ofb64_start(int dir, int server)
171 return(fb64_start(&fb[OFB], dir, server));
174 static int fb64_start(struct fb *fbp, int dir, int server)
176 int x;
177 unsigned char *p;
178 int state;
180 switch (dir) {
181 case DIR_DECRYPT:
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];
188 if (state == FAILED)
189 state = IN_PROGRESS;
190 break;
192 case DIR_ENCRYPT:
193 state = fbp->state[dir-1];
194 if (state == FAILED)
195 state = IN_PROGRESS;
196 else if ((state & NO_SEND_IV) == 0) {
197 break;
200 if (!VALIDKEY(fbp->krbdes_key)) {
201 fbp->need_start = 1;
202 break;
205 state &= ~NO_SEND_IV;
206 state |= NO_RECV_IV;
207 if (encrypt_debug_mode)
208 printf("Creating new feed\r\n");
210 * Create a random feed and send it over.
212 if (DES_new_random_key(&fbp->temp_feed))
213 abort();
215 DES_ecb_encrypt(&fbp->temp_feed,
216 &fbp->temp_feed,
217 &fbp->krbdes_sched, 1);
218 p = fbp->fb_feed + 3;
219 *p++ = ENCRYPT_IS;
220 p++;
221 *p++ = FB64_IV;
222 for (x = 0; x < sizeof(DES_cblock); ++x) {
223 if ((*p++ = fbp->temp_feed[x]) == IAC)
224 *p++ = IAC;
226 *p++ = IAC;
227 *p++ = SE;
228 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
229 telnet_net_write(fbp->fb_feed, p - fbp->fb_feed);
230 break;
231 default:
232 return(FAILED);
234 return(fbp->state[dir-1] = state);
238 * Returns:
239 * -1: some error. Negotiation is done, encryption not ready.
240 * 0: Successful, initial negotiation all done.
241 * 1: successful, negotiation not done yet.
244 int cfb64_is(unsigned char *data, int cnt)
246 return(fb64_is(data, cnt, &fb[CFB]));
249 int ofb64_is(unsigned char *data, int cnt)
251 return(fb64_is(data, cnt, &fb[OFB]));
255 int fb64_is(unsigned char *data, int cnt, struct fb *fbp)
257 unsigned char *p;
258 int state = fbp->state[DIR_DECRYPT-1];
260 if (cnt-- < 1)
261 goto failure;
263 switch (*data++) {
264 case FB64_IV:
265 if (cnt != sizeof(DES_cblock)) {
266 if (encrypt_debug_mode)
267 printf("CFB64: initial vector failed on size\r\n");
268 state = FAILED;
269 goto failure;
272 if (encrypt_debug_mode)
273 printf("CFB64: initial vector received\r\n");
275 if (encrypt_debug_mode)
276 printf("Initializing Decrypt stream\r\n");
278 fb64_stream_iv(data, &fbp->streams[DIR_DECRYPT-1]);
280 p = fbp->fb_feed + 3;
281 *p++ = ENCRYPT_REPLY;
282 p++;
283 *p++ = FB64_IV_OK;
284 *p++ = IAC;
285 *p++ = SE;
286 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
287 telnet_net_write(fbp->fb_feed, p - fbp->fb_feed);
289 state = fbp->state[DIR_DECRYPT-1] = IN_PROGRESS;
290 break;
292 default:
293 if (encrypt_debug_mode) {
294 printf("Unknown option type: %d\r\n", *(data-1));
295 printd(data, cnt);
296 printf("\r\n");
298 /* FALL THROUGH */
299 failure:
301 * We failed. Send an FB64_IV_BAD option
302 * to the other side so it will know that
303 * things failed.
305 p = fbp->fb_feed + 3;
306 *p++ = ENCRYPT_REPLY;
307 p++;
308 *p++ = FB64_IV_BAD;
309 *p++ = IAC;
310 *p++ = SE;
311 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
312 telnet_net_write(fbp->fb_feed, p - fbp->fb_feed);
314 break;
316 return(fbp->state[DIR_DECRYPT-1] = state);
320 * Returns:
321 * -1: some error. Negotiation is done, encryption not ready.
322 * 0: Successful, initial negotiation all done.
323 * 1: successful, negotiation not done yet.
326 int cfb64_reply(unsigned char *data, int cnt)
328 return(fb64_reply(data, cnt, &fb[CFB]));
331 int ofb64_reply(unsigned char *data, int cnt)
333 return(fb64_reply(data, cnt, &fb[OFB]));
337 int fb64_reply(unsigned char *data, int cnt, struct fb *fbp)
339 int state = fbp->state[DIR_ENCRYPT-1];
341 if (cnt-- < 1)
342 goto failure;
344 switch (*data++) {
345 case FB64_IV_OK:
346 fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
347 if (state == FAILED)
348 state = IN_PROGRESS;
349 state &= ~NO_RECV_IV;
350 encrypt_send_keyid(DIR_ENCRYPT, (unsigned char *)"\0", 1, 1);
351 break;
353 case FB64_IV_BAD:
354 memset(fbp->temp_feed, 0, sizeof(DES_cblock));
355 fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
356 state = FAILED;
357 break;
359 default:
360 if (encrypt_debug_mode) {
361 printf("Unknown option type: %d\r\n", data[-1]);
362 printd(data, cnt);
363 printf("\r\n");
365 /* FALL THROUGH */
366 failure:
367 state = FAILED;
368 break;
370 return(fbp->state[DIR_ENCRYPT-1] = state);
373 void cfb64_session(Session_Key *key, int server)
375 fb64_session(key, server, &fb[CFB]);
378 void ofb64_session(Session_Key *key, int server)
380 fb64_session(key, server, &fb[OFB]);
383 static void fb64_session(Session_Key *key, int server, struct fb *fbp)
386 if (!key || key->type != SK_DES) {
387 if (encrypt_debug_mode)
388 printf("Can't set krbdes's session key (%d != %d)\r\n",
389 key ? key->type : -1, SK_DES);
390 return;
392 memcpy(fbp->krbdes_key, key->data, sizeof(DES_cblock));
394 fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_ENCRYPT-1]);
395 fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_DECRYPT-1]);
397 DES_set_key_checked((DES_cblock *)&fbp->krbdes_key,
398 &fbp->krbdes_sched);
400 * Now look to see if krbdes_start() was was waiting for
401 * the key to show up. If so, go ahead an call it now
402 * that we have the key.
404 if (fbp->need_start) {
405 fbp->need_start = 0;
406 fb64_start(fbp, DIR_ENCRYPT, server);
411 * We only accept a keyid of 0. If we get a keyid of
412 * 0, then mark the state as SUCCESS.
415 int cfb64_keyid(int dir, unsigned char *kp, int *lenp)
417 return(fb64_keyid(dir, kp, lenp, &fb[CFB]));
420 int ofb64_keyid(int dir, unsigned char *kp, int *lenp)
422 return(fb64_keyid(dir, kp, lenp, &fb[OFB]));
425 int 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')) {
430 *lenp = 0;
431 return(state);
434 if (state == FAILED)
435 state = IN_PROGRESS;
437 state &= ~NO_KEYID;
439 return(fbp->state[dir-1] = state);
442 void fb64_printsub(unsigned char *data, size_t cnt,
443 unsigned char *buf, size_t buflen, char *type)
445 char lbuf[32];
446 int i;
447 char *cp;
449 buf[buflen-1] = '\0'; /* make sure it's NULL terminated */
450 buflen -= 1;
452 switch(data[2]) {
453 case FB64_IV:
454 snprintf(lbuf, sizeof(lbuf), "%s_IV", type);
455 cp = lbuf;
456 goto common;
458 case FB64_IV_OK:
459 snprintf(lbuf, sizeof(lbuf), "%s_IV_OK", type);
460 cp = lbuf;
461 goto common;
463 case FB64_IV_BAD:
464 snprintf(lbuf, sizeof(lbuf), "%s_IV_BAD", type);
465 cp = lbuf;
466 goto common;
468 default:
469 snprintf(lbuf, sizeof(lbuf), " %d (unknown)", data[2]);
470 cp = lbuf;
471 common:
472 for (; (buflen > 0) && (*buf = *cp++); buf++)
473 buflen--;
474 for (i = 3; i < cnt; i++) {
475 snprintf(lbuf, sizeof(lbuf), " %d", data[i]);
476 for (cp = lbuf; (buflen > 0) && (*buf = *cp++); buf++)
477 buflen--;
479 break;
483 void cfb64_printsub(unsigned char *data, size_t cnt,
484 unsigned char *buf, size_t buflen)
486 fb64_printsub(data, cnt, buf, buflen, "CFB64");
489 void ofb64_printsub(unsigned char *data, size_t cnt,
490 unsigned char *buf, size_t buflen)
492 fb64_printsub(data, cnt, buf, buflen, "OFB64");
495 void fb64_stream_iv(DES_cblock seed, struct stinfo *stp)
498 memcpy(stp->str_iv, seed,sizeof(DES_cblock));
499 memcpy(stp->str_output, seed, sizeof(DES_cblock));
501 DES_set_key_checked(&stp->str_ikey, &stp->str_sched);
503 stp->str_index = sizeof(DES_cblock);
506 void fb64_stream_key(DES_cblock key, struct stinfo *stp)
508 memcpy(stp->str_ikey, key, sizeof(DES_cblock));
509 DES_set_key_checked((DES_cblock*)key, &stp->str_sched);
511 memcpy(stp->str_output, stp->str_iv, sizeof(DES_cblock));
513 stp->str_index = sizeof(DES_cblock);
517 * DES 64 bit Cipher Feedback
519 * key --->+-----+
520 * +->| DES |--+
521 * | +-----+ |
522 * | v
523 * INPUT --(--------->(+)+---> DATA
524 * | |
525 * +-------------+
528 * Given:
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.
533 * V0 = DES(iV, key)
534 * On = Dn ^ Vn
535 * V(n+1) = DES(On, key)
538 void cfb64_encrypt(unsigned char *s, int c)
540 struct stinfo *stp = &fb[CFB].streams[DIR_ENCRYPT-1];
541 int index;
543 index = stp->str_index;
544 while (c-- > 0) {
545 if (index == sizeof(DES_cblock)) {
546 DES_cblock b;
547 DES_ecb_encrypt(&stp->str_output, &b,&stp->str_sched, 1);
548 memcpy(stp->str_feed, b, sizeof(DES_cblock));
549 index = 0;
552 /* On encryption, we store (feed ^ data) which is cypher */
553 *s = stp->str_output[index] = (stp->str_feed[index] ^ *s);
554 s++;
555 index++;
557 stp->str_index = index;
560 int cfb64_decrypt(int data)
562 struct stinfo *stp = &fb[CFB].streams[DIR_DECRYPT-1];
563 int index;
565 if (data == -1) {
567 * Back up one byte. It is assumed that we will
568 * never back up more than one byte. If we do, this
569 * may or may not work.
571 if (stp->str_index)
572 --stp->str_index;
573 return(0);
576 index = stp->str_index++;
577 if (index == sizeof(DES_cblock)) {
578 DES_cblock b;
579 DES_ecb_encrypt(&stp->str_output,&b, &stp->str_sched, 1);
580 memcpy(stp->str_feed, b, sizeof(DES_cblock));
581 stp->str_index = 1; /* Next time will be 1 */
582 index = 0; /* But now use 0 */
585 /* On decryption we store (data) which is cypher. */
586 stp->str_output[index] = data;
587 return(data ^ stp->str_feed[index]);
591 * DES 64 bit Output Feedback
593 * key --->+-----+
594 * +->| DES |--+
595 * | +-----+ |
596 * +-----------+
598 * INPUT -------->(+) ----> DATA
600 * Given:
601 * iV: Initial vector, 64 bits (8 bytes) long.
602 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
603 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
605 * V0 = DES(iV, key)
606 * V(n+1) = DES(Vn, key)
607 * On = Dn ^ Vn
610 void ofb64_encrypt(unsigned char *s, int c)
612 struct stinfo *stp = &fb[OFB].streams[DIR_ENCRYPT-1];
613 int index;
615 index = stp->str_index;
616 while (c-- > 0) {
617 if (index == sizeof(DES_cblock)) {
618 DES_cblock b;
619 DES_ecb_encrypt(&stp->str_feed,&b, &stp->str_sched, 1);
620 memcpy(stp->str_feed, b, sizeof(DES_cblock));
621 index = 0;
623 *s++ ^= stp->str_feed[index];
624 index++;
626 stp->str_index = index;
629 int ofb64_decrypt(int data)
631 struct stinfo *stp = &fb[OFB].streams[DIR_DECRYPT-1];
632 int index;
634 if (data == -1) {
636 * Back up one byte. It is assumed that we will
637 * never back up more than one byte. If we do, this
638 * may or may not work.
640 if (stp->str_index)
641 --stp->str_index;
642 return(0);
645 index = stp->str_index++;
646 if (index == sizeof(DES_cblock)) {
647 DES_cblock b;
648 DES_ecb_encrypt(&stp->str_feed,&b,&stp->str_sched, 1);
649 memcpy(stp->str_feed, b, sizeof(DES_cblock));
650 stp->str_index = 1; /* Next time will be 1 */
651 index = 0; /* But now use 0 */
654 return(data ^ stp->str_feed[index]);
656 #endif