Remove building with NOCRYPTO option
[minix3.git] / lib / libtelnet / enc_des.c
blobe1cde4ddfce8669ef763e3074ecb48d1cf11c834
1 /* $NetBSD: enc_des.c,v 1.16 2012/03/21 05:33:27 matt Exp $ */
3 /*-
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)enc_des.c 8.3 (Berkeley) 5/30/95"; */
36 #else
37 __RCSID("$NetBSD: enc_des.c,v 1.16 2012/03/21 05:33:27 matt Exp $");
38 #endif
39 #endif /* not lint */
41 #ifdef ENCRYPTION
42 # ifdef AUTHENTICATION
43 # ifdef DES_ENCRYPTION
44 #include <arpa/telnet.h>
45 #include <stdio.h>
46 #include <string.h>
47 #include <stdlib.h>
49 #include <des.h>
50 #include "encrypt.h"
51 #include "key-proto.h"
52 #include "misc-proto.h"
54 #define CFB 0
55 #define OFB 1
57 #define NO_SEND_IV 1
58 #define NO_RECV_IV 2
59 #define NO_KEYID 4
60 #define IN_PROGRESS (NO_SEND_IV|NO_RECV_IV|NO_KEYID)
61 #define SUCCESS 0
62 #define FAILED -1
65 struct fb {
66 Block krbdes_key;
67 Schedule krbdes_sched;
68 Block temp_feed;
69 unsigned char fb_feed[64];
70 int need_start;
71 int state[2];
72 int keyid[2];
73 int once;
74 struct stinfo {
75 Block str_output;
76 Block str_feed;
77 Block str_iv;
78 Block str_ikey;
79 Schedule str_sched;
80 int str_index;
81 int str_flagshift;
82 } streams[2];
85 static struct fb fb[2];
87 struct keyidlist {
88 const char *keyid;
89 int keyidlen;
90 char *key;
91 int keylen;
92 int flags;
93 } keyidlist [] = {
94 { "\0", 1, 0, 0, 0 }, /* default key of zero */
95 { 0, 0, 0, 0, 0 }
98 #define KEYFLAG_MASK 03
100 #define KEYFLAG_NOINIT 00
101 #define KEYFLAG_INIT 01
102 #define KEYFLAG_OK 02
103 #define KEYFLAG_BAD 03
105 #define KEYFLAG_SHIFT 2
107 #define SHIFT_VAL(a,b) (KEYFLAG_SHIFT*((a)+((b)*2)))
109 #define FB64_IV 1
110 #define FB64_IV_OK 2
111 #define FB64_IV_BAD 3
114 void fb64_stream_iv(Block, struct stinfo *);
115 void fb64_init(struct fb *);
116 static int fb64_start(struct fb *, int, int);
117 int fb64_is(unsigned char *, int, struct fb *);
118 int fb64_reply(unsigned char *, int, struct fb *);
119 static void fb64_session(Session_Key *, int, struct fb *);
120 void fb64_stream_key(Block *, struct stinfo *);
121 int fb64_keyid(int, unsigned char *, int *, struct fb *);
123 void
124 cfb64_init(int server)
126 fb64_init(&fb[CFB]);
127 fb[CFB].fb_feed[4] = ENCTYPE_DES_CFB64;
128 fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, CFB);
129 fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, CFB);
132 void
133 ofb64_init(int server)
135 fb64_init(&fb[OFB]);
136 fb[OFB].fb_feed[4] = ENCTYPE_DES_OFB64;
137 fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, OFB);
138 fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, OFB);
141 void
142 fb64_init(register struct fb *fbp)
144 memset((void *)fbp, 0, sizeof(*fbp));
145 fbp->state[0] = fbp->state[1] = FAILED;
146 fbp->fb_feed[0] = IAC;
147 fbp->fb_feed[1] = SB;
148 fbp->fb_feed[2] = TELOPT_ENCRYPT;
149 fbp->fb_feed[3] = ENCRYPT_IS;
153 * Returns:
154 * -1: some error. Negotiation is done, encryption not ready.
155 * 0: Successful, initial negotiation all done.
156 * 1: successful, negotiation not done yet.
157 * 2: Not yet. Other things (like getting the key from
158 * Kerberos) have to happen before we can continue.
161 cfb64_start(int dir, int server)
163 return(fb64_start(&fb[CFB], dir, server));
167 ofb64_start(int dir, int server)
169 return(fb64_start(&fb[OFB], dir, server));
172 static int
173 fb64_start(struct fb *fbp, int dir, int server)
175 size_t x;
176 unsigned char *p;
177 register int state;
179 switch (dir) {
180 case DIR_DECRYPT:
182 * This is simply a request to have the other side
183 * start output (our input). He will negotiate an
184 * IV so we need not look for it.
186 state = fbp->state[dir-1];
187 if (state == FAILED)
188 state = IN_PROGRESS;
189 break;
191 case DIR_ENCRYPT:
192 state = fbp->state[dir-1];
193 if (state == FAILED)
194 state = IN_PROGRESS;
195 else if ((state & NO_SEND_IV) == 0)
196 break;
198 if (!VALIDKEY(fbp->krbdes_key)) {
199 fbp->need_start = 1;
200 break;
202 state &= ~NO_SEND_IV;
203 state |= NO_RECV_IV;
204 if (encrypt_debug_mode)
205 printf("Creating new feed\r\n");
207 * Create a random feed and send it over.
209 des_new_random_key(&fbp->temp_feed);
210 des_ecb_encrypt(&fbp->temp_feed, &fbp->temp_feed,
211 fbp->krbdes_sched, 1);
212 p = fbp->fb_feed + 3;
213 *p++ = ENCRYPT_IS;
214 p++;
215 *p++ = FB64_IV;
216 for (x = 0; x < sizeof(Block); ++x) {
217 if ((*p++ = fbp->temp_feed[x]) == IAC)
218 *p++ = IAC;
220 *p++ = IAC;
221 *p++ = SE;
222 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
223 telnet_net_write(fbp->fb_feed, p - fbp->fb_feed);
224 break;
225 default:
226 return(FAILED);
228 return(fbp->state[dir-1] = state);
232 * Returns:
233 * -1: some error. Negotiation is done, encryption not ready.
234 * 0: Successful, initial negotiation all done.
235 * 1: successful, negotiation not done yet.
238 cfb64_is(unsigned char *data, int cnt)
240 return(fb64_is(data, cnt, &fb[CFB]));
243 ofb64_is(unsigned char *data, int cnt)
245 return(fb64_is(data, cnt, &fb[OFB]));
249 fb64_is(unsigned char *data, int cnt, struct fb *fbp)
251 unsigned char *p;
252 register int state = fbp->state[DIR_DECRYPT-1];
254 if (cnt-- < 1)
255 goto failure;
257 switch (*data++) {
258 case FB64_IV:
259 if (cnt != sizeof(Block)) {
260 if (encrypt_debug_mode)
261 printf("CFB64: initial vector failed on size\r\n");
262 state = FAILED;
263 goto failure;
266 if (encrypt_debug_mode)
267 printf("CFB64: initial vector received\r\n");
269 if (encrypt_debug_mode)
270 printf("Initializing Decrypt stream\r\n");
272 fb64_stream_iv((void *)data, &fbp->streams[DIR_DECRYPT-1]);
274 p = fbp->fb_feed + 3;
275 *p++ = ENCRYPT_REPLY;
276 p++;
277 *p++ = FB64_IV_OK;
278 *p++ = IAC;
279 *p++ = SE;
280 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
281 telnet_net_write(fbp->fb_feed, p - fbp->fb_feed);
283 state = fbp->state[DIR_DECRYPT-1] = IN_PROGRESS;
284 break;
286 default:
287 if (encrypt_debug_mode) {
288 printf("Unknown option type: %d\r\n", *(data-1));
289 printd(data, cnt);
290 printf("\r\n");
292 /* FALL THROUGH */
293 failure:
295 * We failed. Send an FB64_IV_BAD option
296 * to the other side so it will know that
297 * things failed.
299 p = fbp->fb_feed + 3;
300 *p++ = ENCRYPT_REPLY;
301 p++;
302 *p++ = FB64_IV_BAD;
303 *p++ = IAC;
304 *p++ = SE;
305 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
306 telnet_net_write(fbp->fb_feed, p - fbp->fb_feed);
308 break;
310 return(fbp->state[DIR_DECRYPT-1] = state);
314 * Returns:
315 * -1: some error. Negotiation is done, encryption not ready.
316 * 0: Successful, initial negotiation all done.
317 * 1: successful, negotiation not done yet.
320 cfb64_reply(unsigned char *data, int cnt)
322 return(fb64_reply(data, cnt, &fb[CFB]));
325 ofb64_reply(unsigned char *data, int cnt)
327 return(fb64_reply(data, cnt, &fb[OFB]));
331 fb64_reply(unsigned char *data, int cnt, struct fb *fbp)
333 register int state = fbp->state[DIR_ENCRYPT-1];
335 if (cnt-- < 1)
336 goto failure;
338 switch (*data++) {
339 case FB64_IV_OK:
340 fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
341 if (state == FAILED)
342 state = IN_PROGRESS;
343 state &= ~NO_RECV_IV;
344 encrypt_send_keyid(DIR_ENCRYPT, (const unsigned char *)"\0", 1, 1);
345 break;
347 case FB64_IV_BAD:
348 memset(fbp->temp_feed, 0, sizeof(Block));
349 fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
350 state = FAILED;
351 break;
353 default:
354 if (encrypt_debug_mode) {
355 printf("Unknown option type: %d\r\n", data[-1]);
356 printd(data, cnt);
357 printf("\r\n");
359 /* FALL THROUGH */
360 failure:
361 state = FAILED;
362 break;
364 return(fbp->state[DIR_ENCRYPT-1] = state);
367 void
368 cfb64_session(Session_Key *key, int server)
370 fb64_session(key, server, &fb[CFB]);
373 void
374 ofb64_session( Session_Key *key, int server)
376 fb64_session(key, server, &fb[OFB]);
379 static void
380 fb64_session(Session_Key *key, int server, struct fb *fbp)
383 if (!key || key->type != SK_DES) {
384 if (encrypt_debug_mode)
385 printf("Can't set krbdes's session key (%d != %d)\r\n",
386 key ? key->type : -1, SK_DES);
387 return;
389 memmove((void *)fbp->krbdes_key, (void *)key->data, sizeof(Block));
391 fb64_stream_key(&fbp->krbdes_key, &fbp->streams[DIR_ENCRYPT-1]);
392 fb64_stream_key(&fbp->krbdes_key, &fbp->streams[DIR_DECRYPT-1]);
394 if (fbp->once == 0) {
395 des_init_random_number_generator(&fbp->krbdes_key);
396 fbp->once = 1;
398 des_key_sched(&fbp->krbdes_key, fbp->krbdes_sched);
400 * Now look to see if krbdes_start() was waiting for the key to
401 * show up. If so, go ahead an call it now that we have the key.
403 if (fbp->need_start) {
404 fbp->need_start = 0;
405 fb64_start(fbp, DIR_ENCRYPT, server);
410 * We only accept a keyid of 0. If we get a keyid of
411 * 0, then mark the state as SUCCESS.
414 cfb64_keyid(int dir, unsigned char *kp, int *lenp)
416 return(fb64_keyid(dir, kp, lenp, &fb[CFB]));
420 ofb64_keyid(int dir, unsigned char *kp, int *lenp)
422 return(fb64_keyid(dir, kp, lenp, &fb[OFB]));
426 fb64_keyid(int dir, unsigned char *kp, int *lenp, struct fb *fbp)
428 register int state = fbp->state[dir-1];
430 if (*lenp != 1 || (*kp != '\0')) {
431 *lenp = 0;
432 return(state);
435 if (state == FAILED)
436 state = IN_PROGRESS;
438 state &= ~NO_KEYID;
440 return(fbp->state[dir-1] = state);
443 void
444 fb64_printsub(const unsigned char *data, int cnt, unsigned char *buf,
445 int buflen, const unsigned char *type)
447 char lbuf[32];
448 register int i;
449 char *cp;
451 buf[buflen-1] = '\0'; /* make sure it's NULL terminated */
452 buflen -= 1;
454 switch(data[2]) {
455 case FB64_IV:
456 snprintf(lbuf, sizeof(lbuf), "%s_IV", type);
457 cp = lbuf;
458 goto common;
460 case FB64_IV_OK:
461 snprintf(lbuf, sizeof(lbuf), "%s_IV_OK", type);
462 cp = lbuf;
463 goto common;
465 case FB64_IV_BAD:
466 snprintf(lbuf, sizeof(lbuf), "%s_IV_BAD", type);
467 cp = lbuf;
468 goto common;
470 default:
471 snprintf(lbuf, sizeof(lbuf), " %d (unknown)", data[2]);
472 cp = lbuf;
473 common:
474 for (; (buflen > 0) && (*buf = *cp++); buf++)
475 buflen--;
476 for (i = 3; i < cnt; i++) {
477 snprintf(lbuf, sizeof(lbuf), " %d", data[i]);
478 for (cp = lbuf; (buflen > 0) && (*buf = *cp++); buf++)
479 buflen--;
481 break;
485 void
486 cfb64_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
488 fb64_printsub(data, cnt, buf, buflen, "CFB64");
491 void
492 ofb64_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
494 fb64_printsub(data, cnt, buf, buflen, "OFB64");
497 void
498 fb64_stream_iv(Block seed, struct stinfo *stp)
501 memmove((void *)stp->str_iv, (void *)seed, sizeof(Block));
502 memmove((void *)stp->str_output, (void *)seed, sizeof(Block));
504 des_key_sched(&stp->str_ikey, stp->str_sched);
506 stp->str_index = sizeof(Block);
509 void
510 fb64_stream_key(Block *key, struct stinfo *stp)
512 memmove((void *)stp->str_ikey, (void *)key, sizeof(Block));
513 des_key_sched(key, stp->str_sched);
515 memmove((void *)stp->str_output, (void *)stp->str_iv, sizeof(Block));
517 stp->str_index = sizeof(Block);
521 * DES 64 bit Cipher Feedback
523 * key --->+-----+
524 * +->| DES |--+
525 * | +-----+ |
526 * | v
527 * INPUT --(--------->(+)+---> DATA
528 * | |
529 * +-------------+
532 * Given:
533 * iV: Initial vector, 64 bits (8 bytes) long.
534 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
535 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
537 * V0 = DES(iV, key)
538 * On = Dn ^ Vn
539 * V(n+1) = DES(On, key)
542 void
543 cfb64_encrypt(unsigned char *s, int c)
545 register struct stinfo *stp = &fb[CFB].streams[DIR_ENCRYPT-1];
546 register int idx;
548 idx = stp->str_index;
549 while (c-- > 0) {
550 if (idx == sizeof(Block)) {
551 Block b;
552 des_ecb_encrypt(&stp->str_output, &b, stp->str_sched, 1);
553 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
554 idx = 0;
557 /* On encryption, we store (feed ^ data) which is cypher */
558 *s = stp->str_output[idx] = (stp->str_feed[idx] ^ *s);
559 s++;
560 idx++;
562 stp->str_index = idx;
566 cfb64_decrypt(int data)
568 register struct stinfo *stp = &fb[CFB].streams[DIR_DECRYPT-1];
569 int idx;
571 if (data == -1) {
573 * Back up one byte. It is assumed that we will
574 * never back up more than one byte. If we do, this
575 * may or may not work.
577 if (stp->str_index)
578 --stp->str_index;
579 return(0);
582 idx = stp->str_index++;
583 if (idx == sizeof(Block)) {
584 Block b;
585 des_ecb_encrypt(&stp->str_output, &b, stp->str_sched, 1);
586 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
587 stp->str_index = 1; /* Next time will be 1 */
588 idx = 0; /* But now use 0 */
591 /* On decryption we store (data) which is cypher. */
592 stp->str_output[idx] = data;
593 return(data ^ stp->str_feed[idx]);
597 * DES 64 bit Output Feedback
599 * key --->+-----+
600 * +->| DES |--+
601 * | +-----+ |
602 * +-----------+
604 * INPUT -------->(+) ----> DATA
606 * Given:
607 * iV: Initial vector, 64 bits (8 bytes) long.
608 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
609 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
611 * V0 = DES(iV, key)
612 * V(n+1) = DES(Vn, key)
613 * On = Dn ^ Vn
615 void
616 ofb64_encrypt(unsigned char *s, int c)
618 register struct stinfo *stp = &fb[OFB].streams[DIR_ENCRYPT-1];
619 register int idx;
621 idx = stp->str_index;
622 while (c-- > 0) {
623 if (idx == sizeof(Block)) {
624 Block b;
625 des_ecb_encrypt(&stp->str_feed, &b, stp->str_sched, 1);
626 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
627 idx = 0;
629 *s++ ^= stp->str_feed[idx];
630 idx++;
632 stp->str_index = idx;
636 ofb64_decrypt(int data)
638 register struct stinfo *stp = &fb[OFB].streams[DIR_DECRYPT-1];
639 int idx;
641 if (data == -1) {
643 * Back up one byte. It is assumed that we will
644 * never back up more than one byte. If we do, this
645 * may or may not work.
647 if (stp->str_index)
648 --stp->str_index;
649 return(0);
652 idx = stp->str_index++;
653 if (idx == sizeof(Block)) {
654 Block b;
655 des_ecb_encrypt(&stp->str_feed, &b, stp->str_sched, 1);
656 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
657 stp->str_index = 1; /* Next time will be 1 */
658 idx = 0; /* But now use 0 */
661 return(data ^ stp->str_feed[idx]);
663 # endif /* DES_ENCRYPTION */
664 # endif /* AUTHENTICATION */
665 #endif /* ENCRYPTION */