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 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 static const char sccsid[] = "@(#)enc_des.c 8.3 (Berkeley) 5/30/95";
45 # ifdef AUTHENTICATION
46 #include <arpa/telnet.h>
47 #include <openssl/des.h>
53 #include "key-proto.h"
54 #include "misc-proto.h"
56 extern int encrypt_debug_mode;
57 void des_set_random_generator_seed(des_cblock *); /* XXX */
65 #define IN_PROGRESS (NO_SEND_IV|NO_RECV_IV|NO_KEYID)
72 Schedule krbdes_sched;
74 unsigned char fb_feed[64];
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(Block, 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(Block, struct stinfo *);
126 int fb64_keyid(int, unsigned char *, int *, struct fb *);
129 cfb64_init(int server __unused)
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 ofb64_init(int server __unused)
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);
147 fb64_init(struct fb *fbp)
149 memset((void *)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.
166 cfb64_start(int dir, int server)
168 return(fb64_start(&fb[CFB], dir, server));
172 ofb64_start(int dir, int server)
174 return(fb64_start(&fb[OFB], dir, server));
178 fb64_start(struct fb *fbp, int dir, int server __unused)
187 * This is simply a request to have the other side
188 * start output (our input). He will negotiate an
189 * IV so we need not look for it.
191 state = fbp->state[dir-1];
197 state = fbp->state[dir-1];
200 else if ((state & NO_SEND_IV) == 0)
203 if (!VALIDKEY(fbp->krbdes_key)) {
207 state &= ~NO_SEND_IV;
209 if (encrypt_debug_mode)
210 printf("Creating new feed\r\n");
212 * Create a random feed and send it over.
214 des_new_random_key((Block *)fbp->temp_feed);
215 des_ecb_encrypt((Block *)fbp->temp_feed, (Block *)fbp->temp_feed,
216 fbp->krbdes_sched, 1);
217 p = fbp->fb_feed + 3;
221 for (x = 0; x < sizeof(Block); ++x) {
222 if ((*p++ = fbp->temp_feed[x]) == IAC)
227 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
228 net_write(fbp->fb_feed, p - fbp->fb_feed);
233 return(fbp->state[dir-1] = state);
238 * -1: some error. Negotiation is done, encryption not ready.
239 * 0: Successful, initial negotiation all done.
240 * 1: successful, negotiation not done yet.
243 cfb64_is(unsigned char *data, int cnt)
245 return(fb64_is(data, cnt, &fb[CFB]));
249 ofb64_is(unsigned char *data, int cnt)
251 return(fb64_is(data, cnt, &fb[OFB]));
255 fb64_is(unsigned char *data, int cnt, struct fb *fbp)
258 int state = fbp->state[DIR_DECRYPT-1];
265 if (cnt != sizeof(Block)) {
266 if (encrypt_debug_mode)
267 printf("CFB64: initial vector failed on size\r\n");
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((void *)data, &fbp->streams[DIR_DECRYPT-1]);
280 p = fbp->fb_feed + 3;
281 *p++ = ENCRYPT_REPLY;
286 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
287 net_write(fbp->fb_feed, p - fbp->fb_feed);
289 state = fbp->state[DIR_DECRYPT-1] = IN_PROGRESS;
293 if (encrypt_debug_mode) {
294 printf("Unknown option type: %d\r\n", *(data-1));
301 * We failed. Send an FB64_IV_BAD option
302 * to the other side so it will know that
305 p = fbp->fb_feed + 3;
306 *p++ = ENCRYPT_REPLY;
311 printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
312 net_write(fbp->fb_feed, p - fbp->fb_feed);
316 return(fbp->state[DIR_DECRYPT-1] = state);
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 cfb64_reply(unsigned char *data, int cnt)
328 return(fb64_reply(data, cnt, &fb[CFB]));
332 ofb64_reply(unsigned char *data, int cnt)
334 return(fb64_reply(data, cnt, &fb[OFB]));
338 fb64_reply(unsigned char *data, int cnt, struct fb *fbp)
340 int state = fbp->state[DIR_ENCRYPT-1];
347 fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
350 state &= ~NO_RECV_IV;
351 encrypt_send_keyid(DIR_ENCRYPT, "\0", 1, 1);
355 memset(fbp->temp_feed, 0, sizeof(Block));
356 fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);
361 if (encrypt_debug_mode) {
362 printf("Unknown option type: %d\r\n", data[-1]);
371 return(fbp->state[DIR_ENCRYPT-1] = state);
375 cfb64_session(Session_Key *key, int server)
377 fb64_session(key, server, &fb[CFB]);
381 ofb64_session(Session_Key *key, int server)
383 fb64_session(key, server, &fb[OFB]);
387 fb64_session(Session_Key *key, int server, struct fb *fbp)
389 if (!key || key->type != SK_DES) {
390 if (encrypt_debug_mode)
391 printf("Can't set krbdes's session key (%d != %d)\r\n",
392 key ? key->type : -1, SK_DES);
395 memmove((void *)fbp->krbdes_key, (void *)key->data, sizeof(Block));
397 fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_ENCRYPT-1]);
398 fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_DECRYPT-1]);
400 if (fbp->once == 0) {
401 des_set_random_generator_seed((Block *)fbp->krbdes_key);
404 des_key_sched((Block *)fbp->krbdes_key, fbp->krbdes_sched);
406 * Now look to see if krbdes_start() was was waiting for
407 * the key to show up. If so, go ahead an call it now
408 * that we have the key.
410 if (fbp->need_start) {
412 fb64_start(fbp, DIR_ENCRYPT, server);
417 * We only accept a keyid of 0. If we get a keyid of
418 * 0, then mark the state as SUCCESS.
421 cfb64_keyid(int dir, unsigned char *kp, int *lenp)
423 return(fb64_keyid(dir, kp, lenp, &fb[CFB]));
427 ofb64_keyid(int dir, unsigned char *kp, int *lenp)
429 return(fb64_keyid(dir, kp, lenp, &fb[OFB]));
433 fb64_keyid(int dir, unsigned char *kp, int *lenp, struct fb *fbp)
435 int state = fbp->state[dir-1];
437 if (*lenp != 1 || (*kp != '\0')) {
447 return(fbp->state[dir-1] = state);
451 fb64_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen, const char *type)
457 buf[buflen-1] = '\0'; /* make sure it's NULL terminated */
462 sprintf(lbuf, "%s_IV", type);
467 sprintf(lbuf, "%s_IV_OK", type);
472 sprintf(lbuf, "%s_IV_BAD", type);
477 sprintf(lbuf, " %d (unknown)", data[2]);
480 for (; (buflen > 0) && (*buf = *cp++); buf++)
482 for (i = 3; i < cnt; i++) {
483 sprintf(lbuf, " %d", data[i]);
484 for (cp = lbuf; (buflen > 0) && (*buf = *cp++); buf++)
492 cfb64_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
494 fb64_printsub(data, cnt, buf, buflen, "CFB64");
498 ofb64_printsub(unsigned char *data, int cnt, unsigned char *buf, int buflen)
500 fb64_printsub(data, cnt, buf, buflen, "OFB64");
504 fb64_stream_iv(Block seed, struct stinfo *stp)
507 memmove((void *)stp->str_iv, (void *)seed, sizeof(Block));
508 memmove((void *)stp->str_output, (void *)seed, sizeof(Block));
510 des_key_sched((Block *)stp->str_ikey, stp->str_sched);
512 stp->str_index = sizeof(Block);
516 fb64_stream_key(Block key, struct stinfo *stp)
518 memmove((void *)stp->str_ikey, (void *)key, sizeof(Block));
519 des_key_sched((Block *)key, stp->str_sched);
521 memmove((void *)stp->str_output, (void *)stp->str_iv, sizeof(Block));
523 stp->str_index = sizeof(Block);
527 * DES 64 bit Cipher Feedback
533 * INPUT --(--------->(+)+---> DATA
539 * iV: Initial vector, 64 bits (8 bytes) long.
540 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
541 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
545 * V(n+1) = DES(On, key)
549 cfb64_encrypt(unsigned char *s, int c)
551 struct stinfo *stp = &fb[CFB].streams[DIR_ENCRYPT-1];
554 idx = stp->str_index;
556 if (idx == sizeof(Block)) {
558 des_ecb_encrypt((Block *)stp->str_output, (Block *)b, stp->str_sched, 1);
559 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
563 /* On encryption, we store (feed ^ data) which is cypher */
564 *s = stp->str_output[idx] = (stp->str_feed[idx] ^ *s);
568 stp->str_index = idx;
572 cfb64_decrypt(int data)
574 struct stinfo *stp = &fb[CFB].streams[DIR_DECRYPT-1];
579 * Back up one byte. It is assumed that we will
580 * never back up more than one byte. If we do, this
581 * may or may not work.
588 idx = stp->str_index++;
589 if (idx == sizeof(Block)) {
591 des_ecb_encrypt((Block *)stp->str_output, (Block *)b, stp->str_sched, 1);
592 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
593 stp->str_index = 1; /* Next time will be 1 */
594 idx = 0; /* But now use 0 */
597 /* On decryption we store (data) which is cypher. */
598 stp->str_output[idx] = data;
599 return(data ^ stp->str_feed[idx]);
603 * DES 64 bit Output Feedback
610 * INPUT -------->(+) ----> DATA
613 * iV: Initial vector, 64 bits (8 bytes) long.
614 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
615 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
618 * V(n+1) = DES(Vn, key)
622 ofb64_encrypt(unsigned char *s, int c)
624 struct stinfo *stp = &fb[OFB].streams[DIR_ENCRYPT-1];
627 idx = stp->str_index;
629 if (idx == sizeof(Block)) {
631 des_ecb_encrypt((Block *)stp->str_feed, (Block *)b, stp->str_sched, 1);
632 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
635 *s++ ^= stp->str_feed[idx];
638 stp->str_index = idx;
642 ofb64_decrypt(int data)
644 struct stinfo *stp = &fb[OFB].streams[DIR_DECRYPT-1];
649 * Back up one byte. It is assumed that we will
650 * never back up more than one byte. If we do, this
651 * may or may not work.
658 idx = stp->str_index++;
659 if (idx == sizeof(Block)) {
661 des_ecb_encrypt((Block *)stp->str_feed, (Block *)b, stp->str_sched, 1);
662 memmove((void *)stp->str_feed, (void *)b, sizeof(Block));
663 stp->str_index = 1; /* Next time will be 1 */
664 idx = 0; /* But now use 0 */
667 return(data ^ stp->str_feed[idx]);
669 # endif /* AUTHENTICATION */
670 #endif /* ENCRYPTION */