]>
git.cameronkatri.com Git - apple_cmds.git/blob - lib/libtelnet/enc_des.c
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
33 * $FreeBSD: src/crypto/telnet/libtelnet/enc_des.c,v 1.3.2.1 2002/04/13 10:59:07 markm Exp $
36 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD: src/crypto/telnet/libtelnet/enc_des.c,v 1.3.2.1 2002/04/13 10:59:07 markm Exp $");
43 #define __unused __attribute__((__unused__))
47 static const char sccsid
[] = "@(#)enc_des.c 8.3 (Berkeley) 5/30/95";
51 # ifdef AUTHENTICATION
52 #include <arpa/telnet.h>
59 #include "key-proto.h"
60 #include "misc-proto.h"
62 extern int encrypt_debug_mode
;
70 #define IN_PROGRESS (NO_SEND_IV|NO_RECV_IV|NO_KEYID)
77 Schedule krbdes_sched
;
79 unsigned char fb_feed
[64];
95 static struct fb fb
[2];
104 { "\0", 1, 0, 0, 0 }, /* default key of zero */
108 #define KEYFLAG_MASK 03
110 #define KEYFLAG_NOINIT 00
111 #define KEYFLAG_INIT 01
112 #define KEYFLAG_OK 02
113 #define KEYFLAG_BAD 03
115 #define KEYFLAG_SHIFT 2
117 #define SHIFT_VAL(a,b) (KEYFLAG_SHIFT*((a)+((b)*2)))
121 #define FB64_IV_BAD 3
124 void fb64_stream_iv(Block
, struct stinfo
*);
125 void fb64_init(struct fb
*);
126 static int fb64_start(struct fb
*, int, int);
127 int fb64_is(unsigned char *, int, struct fb
*);
128 int fb64_reply(unsigned char *, int, struct fb
*);
129 static void fb64_session(Session_Key
*, int, struct fb
*);
130 void fb64_stream_key(Block
, struct stinfo
*);
131 int fb64_keyid(int, unsigned char *, int *, struct fb
*);
134 cfb64_init(int server __unused
)
137 fb
[CFB
].fb_feed
[4] = ENCTYPE_DES_CFB64
;
138 fb
[CFB
].streams
[0].str_flagshift
= SHIFT_VAL(0, CFB
);
139 fb
[CFB
].streams
[1].str_flagshift
= SHIFT_VAL(1, CFB
);
143 ofb64_init(int server __unused
)
146 fb
[OFB
].fb_feed
[4] = ENCTYPE_DES_OFB64
;
147 fb
[CFB
].streams
[0].str_flagshift
= SHIFT_VAL(0, OFB
);
148 fb
[CFB
].streams
[1].str_flagshift
= SHIFT_VAL(1, OFB
);
152 fb64_init(struct fb
*fbp
)
154 memset((void *)fbp
, 0, sizeof(*fbp
));
155 fbp
->state
[0] = fbp
->state
[1] = FAILED
;
156 fbp
->fb_feed
[0] = IAC
;
157 fbp
->fb_feed
[1] = SB
;
158 fbp
->fb_feed
[2] = TELOPT_ENCRYPT
;
159 fbp
->fb_feed
[3] = ENCRYPT_IS
;
164 * -1: some error. Negotiation is done, encryption not ready.
165 * 0: Successful, initial negotiation all done.
166 * 1: successful, negotiation not done yet.
167 * 2: Not yet. Other things (like getting the key from
168 * Kerberos) have to happen before we can continue.
171 cfb64_start(int dir
, int server
)
173 return(fb64_start(&fb
[CFB
], dir
, server
));
177 ofb64_start(int dir
, int server
)
179 return(fb64_start(&fb
[OFB
], dir
, server
));
183 fb64_start(struct fb
*fbp
, int dir
, int server __unused
)
192 * This is simply a request to have the other side
193 * start output (our input). He will negotiate an
194 * IV so we need not look for it.
196 state
= fbp
->state
[dir
-1];
202 state
= fbp
->state
[dir
-1];
205 else if ((state
& NO_SEND_IV
) == 0)
208 if (!VALIDKEY(fbp
->krbdes_key
)) {
212 state
&= ~NO_SEND_IV
;
214 if (encrypt_debug_mode
)
215 printf("Creating new feed\r\n");
217 * Create a random feed and send it over.
219 des_new_random_key(fbp
->temp_feed
);
220 des_ecb_encrypt((Block
*)fbp
->temp_feed
, (Block
*)fbp
->temp_feed
,
221 fbp
->krbdes_sched
, 1);
222 p
= fbp
->fb_feed
+ 3;
226 for (x
= 0; x
< sizeof(Block
); ++x
) {
227 if ((*p
++ = fbp
->temp_feed
[x
]) == IAC
)
232 printsub('>', &fbp
->fb_feed
[2], p
- &fbp
->fb_feed
[2]);
233 net_write(fbp
->fb_feed
, p
- fbp
->fb_feed
);
238 return(fbp
->state
[dir
-1] = state
);
243 * -1: some error. Negotiation is done, encryption not ready.
244 * 0: Successful, initial negotiation all done.
245 * 1: successful, negotiation not done yet.
248 cfb64_is(unsigned char *data
, int cnt
)
250 return(fb64_is(data
, cnt
, &fb
[CFB
]));
254 ofb64_is(unsigned char *data
, int cnt
)
256 return(fb64_is(data
, cnt
, &fb
[OFB
]));
260 fb64_is(unsigned char *data
, int cnt
, struct fb
*fbp
)
263 int state
= fbp
->state
[DIR_DECRYPT
-1];
270 if (cnt
!= sizeof(Block
)) {
271 if (encrypt_debug_mode
)
272 printf("CFB64: initial vector failed on size\r\n");
277 if (encrypt_debug_mode
)
278 printf("CFB64: initial vector received\r\n");
280 if (encrypt_debug_mode
)
281 printf("Initializing Decrypt stream\r\n");
283 fb64_stream_iv((void *)data
, &fbp
->streams
[DIR_DECRYPT
-1]);
285 p
= fbp
->fb_feed
+ 3;
286 *p
++ = ENCRYPT_REPLY
;
291 printsub('>', &fbp
->fb_feed
[2], p
- &fbp
->fb_feed
[2]);
292 net_write(fbp
->fb_feed
, p
- fbp
->fb_feed
);
294 state
= fbp
->state
[DIR_DECRYPT
-1] = IN_PROGRESS
;
298 if (encrypt_debug_mode
) {
299 printf("Unknown option type: %d\r\n", *(data
-1));
306 * We failed. Send an FB64_IV_BAD option
307 * to the other side so it will know that
310 p
= fbp
->fb_feed
+ 3;
311 *p
++ = ENCRYPT_REPLY
;
316 printsub('>', &fbp
->fb_feed
[2], p
- &fbp
->fb_feed
[2]);
317 net_write(fbp
->fb_feed
, p
- fbp
->fb_feed
);
321 return(fbp
->state
[DIR_DECRYPT
-1] = state
);
326 * -1: some error. Negotiation is done, encryption not ready.
327 * 0: Successful, initial negotiation all done.
328 * 1: successful, negotiation not done yet.
331 cfb64_reply(unsigned char *data
, int cnt
)
333 return(fb64_reply(data
, cnt
, &fb
[CFB
]));
337 ofb64_reply(unsigned char *data
, int cnt
)
339 return(fb64_reply(data
, cnt
, &fb
[OFB
]));
343 fb64_reply(unsigned char *data
, int cnt
, struct fb
*fbp
)
345 int state
= fbp
->state
[DIR_ENCRYPT
-1];
352 fb64_stream_iv(fbp
->temp_feed
, &fbp
->streams
[DIR_ENCRYPT
-1]);
355 state
&= ~NO_RECV_IV
;
356 encrypt_send_keyid(DIR_ENCRYPT
, "\0", 1, 1);
360 memset(fbp
->temp_feed
, 0, sizeof(Block
));
361 fb64_stream_iv(fbp
->temp_feed
, &fbp
->streams
[DIR_ENCRYPT
-1]);
366 if (encrypt_debug_mode
) {
367 printf("Unknown option type: %d\r\n", data
[-1]);
376 return(fbp
->state
[DIR_ENCRYPT
-1] = state
);
380 cfb64_session(Session_Key
*key
, int server
)
382 fb64_session(key
, server
, &fb
[CFB
]);
386 ofb64_session(Session_Key
*key
, int server
)
388 fb64_session(key
, server
, &fb
[OFB
]);
392 fb64_session(Session_Key
*key
, int server
, struct fb
*fbp
)
394 if (!key
|| key
->type
!= SK_DES
) {
395 if (encrypt_debug_mode
)
396 printf("Can't set krbdes's session key (%d != %d)\r\n",
397 key
? key
->type
: -1, SK_DES
);
400 memmove((void *)fbp
->krbdes_key
, (void *)key
->data
, sizeof(Block
));
402 fb64_stream_key(fbp
->krbdes_key
, &fbp
->streams
[DIR_ENCRYPT
-1]);
403 fb64_stream_key(fbp
->krbdes_key
, &fbp
->streams
[DIR_DECRYPT
-1]);
405 if (fbp
->once
== 0) {
406 des_set_random_generator_seed(fbp
->krbdes_key
);
409 des_key_sched(fbp
->krbdes_key
, fbp
->krbdes_sched
);
411 * Now look to see if krbdes_start() was was waiting for
412 * the key to show up. If so, go ahead an call it now
413 * that we have the key.
415 if (fbp
->need_start
) {
417 fb64_start(fbp
, DIR_ENCRYPT
, server
);
422 * We only accept a keyid of 0. If we get a keyid of
423 * 0, then mark the state as SUCCESS.
426 cfb64_keyid(int dir
, unsigned char *kp
, int *lenp
)
428 return(fb64_keyid(dir
, kp
, lenp
, &fb
[CFB
]));
432 ofb64_keyid(int dir
, unsigned char *kp
, int *lenp
)
434 return(fb64_keyid(dir
, kp
, lenp
, &fb
[OFB
]));
438 fb64_keyid(int dir
, unsigned char *kp
, int *lenp
, struct fb
*fbp
)
440 int state
= fbp
->state
[dir
-1];
442 if (*lenp
!= 1 || (*kp
!= '\0')) {
452 return(fbp
->state
[dir
-1] = state
);
456 fb64_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
, const char *type
)
462 buf
[buflen
-1] = '\0'; /* make sure it's NULL terminated */
467 sprintf(lbuf
, "%s_IV", type
);
472 sprintf(lbuf
, "%s_IV_OK", type
);
477 sprintf(lbuf
, "%s_IV_BAD", type
);
482 sprintf(lbuf
, " %d (unknown)", data
[2]);
485 for (; (buflen
> 0) && (*buf
= *cp
++); buf
++)
487 for (i
= 3; i
< cnt
; i
++) {
488 sprintf(lbuf
, " %d", data
[i
]);
489 for (cp
= lbuf
; (buflen
> 0) && (*buf
= *cp
++); buf
++)
497 cfb64_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
499 fb64_printsub(data
, cnt
, buf
, buflen
, "CFB64");
503 ofb64_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
505 fb64_printsub(data
, cnt
, buf
, buflen
, "OFB64");
509 fb64_stream_iv(Block seed
, struct stinfo
*stp
)
512 memmove((void *)stp
->str_iv
, (void *)seed
, sizeof(Block
));
513 memmove((void *)stp
->str_output
, (void *)seed
, sizeof(Block
));
515 des_key_sched(stp
->str_ikey
, stp
->str_sched
);
517 stp
->str_index
= sizeof(Block
);
521 fb64_stream_key(Block key
, struct stinfo
*stp
)
523 memmove((void *)stp
->str_ikey
, (void *)key
, sizeof(Block
));
524 des_key_sched(key
, stp
->str_sched
);
526 memmove((void *)stp
->str_output
, (void *)stp
->str_iv
, sizeof(Block
));
528 stp
->str_index
= sizeof(Block
);
532 * DES 64 bit Cipher Feedback
538 * INPUT --(--------->(+)+---> DATA
544 * iV: Initial vector, 64 bits (8 bytes) long.
545 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
546 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
550 * V(n+1) = DES(On, key)
554 cfb64_encrypt(unsigned char *s
, int c
)
556 struct stinfo
*stp
= &fb
[CFB
].streams
[DIR_ENCRYPT
-1];
559 idx
= stp
->str_index
;
561 if (idx
== sizeof(Block
)) {
563 des_ecb_encrypt((Block
*)stp
->str_output
, (Block
*)b
, stp
->str_sched
, 1);
564 memmove((void *)stp
->str_feed
, (void *)b
, sizeof(Block
));
568 /* On encryption, we store (feed ^ data) which is cypher */
569 *s
= stp
->str_output
[idx
] = (stp
->str_feed
[idx
] ^ *s
);
573 stp
->str_index
= idx
;
577 cfb64_decrypt(int data
)
579 struct stinfo
*stp
= &fb
[CFB
].streams
[DIR_DECRYPT
-1];
584 * Back up one byte. It is assumed that we will
585 * never back up more than one byte. If we do, this
586 * may or may not work.
593 idx
= stp
->str_index
++;
594 if (idx
== sizeof(Block
)) {
596 des_ecb_encrypt((Block
*)stp
->str_output
, (Block
*)b
, stp
->str_sched
, 1);
597 memmove((void *)stp
->str_feed
, (void *)b
, sizeof(Block
));
598 stp
->str_index
= 1; /* Next time will be 1 */
599 idx
= 0; /* But now use 0 */
602 /* On decryption we store (data) which is cypher. */
603 stp
->str_output
[idx
] = data
;
604 return(data
^ stp
->str_feed
[idx
]);
608 * DES 64 bit Output Feedback
615 * INPUT -------->(+) ----> DATA
618 * iV: Initial vector, 64 bits (8 bytes) long.
619 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
620 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
623 * V(n+1) = DES(Vn, key)
627 ofb64_encrypt(unsigned char *s
, int c
)
629 struct stinfo
*stp
= &fb
[OFB
].streams
[DIR_ENCRYPT
-1];
632 idx
= stp
->str_index
;
634 if (idx
== sizeof(Block
)) {
636 des_ecb_encrypt((Block
*)stp
->str_feed
, (Block
*)b
, stp
->str_sched
, 1);
637 memmove((void *)stp
->str_feed
, (void *)b
, sizeof(Block
));
640 *s
++ ^= stp
->str_feed
[idx
];
643 stp
->str_index
= idx
;
647 ofb64_decrypt(int data
)
649 struct stinfo
*stp
= &fb
[OFB
].streams
[DIR_DECRYPT
-1];
654 * Back up one byte. It is assumed that we will
655 * never back up more than one byte. If we do, this
656 * may or may not work.
663 idx
= stp
->str_index
++;
664 if (idx
== sizeof(Block
)) {
666 des_ecb_encrypt((Block
*)stp
->str_feed
, (Block
*)b
, stp
->str_sched
, 1);
667 memmove((void *)stp
->str_feed
, (void *)b
, sizeof(Block
));
668 stp
->str_index
= 1; /* Next time will be 1 */
669 idx
= 0; /* But now use 0 */
672 return(data
^ stp
->str_feed
[idx
]);
674 # endif /* AUTHENTICATION */
675 #endif /* ENCRYPTION */