1 /* $NetBSD: enc_des.c,v 1.16 2012/03/21 05:33:27 matt Exp $ */
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
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
32 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)enc_des.c 8.3 (Berkeley) 5/30/95"; */
37 __RCSID("$NetBSD: enc_des.c,v 1.16 2012/03/21 05:33:27 matt Exp $");
42 # ifdef AUTHENTICATION
43 # ifdef DES_ENCRYPTION
44 #include <arpa/telnet.h>
51 #include "key-proto.h"
52 #include "misc-proto.h"
60 #define IN_PROGRESS (NO_SEND_IV|NO_RECV_IV|NO_KEYID)
67 Schedule krbdes_sched
;
69 unsigned char fb_feed
[64];
85 static struct fb fb
[2];
94 { "\0", 1, 0, 0, 0 }, /* default key of zero */
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)))
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
*);
124 cfb64_init(int server
)
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
);
133 ofb64_init(int server
)
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
);
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
;
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
));
173 fb64_start(struct fb
*fbp
, int dir
, int server
)
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];
192 state
= fbp
->state
[dir
-1];
195 else if ((state
& NO_SEND_IV
) == 0)
198 if (!VALIDKEY(fbp
->krbdes_key
)) {
202 state
&= ~NO_SEND_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;
216 for (x
= 0; x
< sizeof(Block
); ++x
) {
217 if ((*p
++ = fbp
->temp_feed
[x
]) == IAC
)
222 printsub('>', &fbp
->fb_feed
[2], p
- &fbp
->fb_feed
[2]);
223 telnet_net_write(fbp
->fb_feed
, p
- fbp
->fb_feed
);
228 return(fbp
->state
[dir
-1] = state
);
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
)
252 register int state
= fbp
->state
[DIR_DECRYPT
-1];
259 if (cnt
!= sizeof(Block
)) {
260 if (encrypt_debug_mode
)
261 printf("CFB64: initial vector failed on size\r\n");
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
;
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
;
287 if (encrypt_debug_mode
) {
288 printf("Unknown option type: %d\r\n", *(data
-1));
295 * We failed. Send an FB64_IV_BAD option
296 * to the other side so it will know that
299 p
= fbp
->fb_feed
+ 3;
300 *p
++ = ENCRYPT_REPLY
;
305 printsub('>', &fbp
->fb_feed
[2], p
- &fbp
->fb_feed
[2]);
306 telnet_net_write(fbp
->fb_feed
, p
- fbp
->fb_feed
);
310 return(fbp
->state
[DIR_DECRYPT
-1] = state
);
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];
340 fb64_stream_iv(fbp
->temp_feed
, &fbp
->streams
[DIR_ENCRYPT
-1]);
343 state
&= ~NO_RECV_IV
;
344 encrypt_send_keyid(DIR_ENCRYPT
, (const unsigned char *)"\0", 1, 1);
348 memset(fbp
->temp_feed
, 0, sizeof(Block
));
349 fb64_stream_iv(fbp
->temp_feed
, &fbp
->streams
[DIR_ENCRYPT
-1]);
354 if (encrypt_debug_mode
) {
355 printf("Unknown option type: %d\r\n", data
[-1]);
364 return(fbp
->state
[DIR_ENCRYPT
-1] = state
);
368 cfb64_session(Session_Key
*key
, int server
)
370 fb64_session(key
, server
, &fb
[CFB
]);
374 ofb64_session( Session_Key
*key
, int server
)
376 fb64_session(key
, server
, &fb
[OFB
]);
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
);
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
);
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
) {
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')) {
440 return(fbp
->state
[dir
-1] = state
);
444 fb64_printsub(const unsigned char *data
, int cnt
, unsigned char *buf
,
445 int buflen
, const unsigned char *type
)
451 buf
[buflen
-1] = '\0'; /* make sure it's NULL terminated */
456 snprintf(lbuf
, sizeof(lbuf
), "%s_IV", type
);
461 snprintf(lbuf
, sizeof(lbuf
), "%s_IV_OK", type
);
466 snprintf(lbuf
, sizeof(lbuf
), "%s_IV_BAD", type
);
471 snprintf(lbuf
, sizeof(lbuf
), " %d (unknown)", data
[2]);
474 for (; (buflen
> 0) && (*buf
= *cp
++); buf
++)
476 for (i
= 3; i
< cnt
; i
++) {
477 snprintf(lbuf
, sizeof(lbuf
), " %d", data
[i
]);
478 for (cp
= lbuf
; (buflen
> 0) && (*buf
= *cp
++); buf
++)
486 cfb64_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
488 fb64_printsub(data
, cnt
, buf
, buflen
, "CFB64");
492 ofb64_printsub(unsigned char *data
, int cnt
, unsigned char *buf
, int buflen
)
494 fb64_printsub(data
, cnt
, buf
, buflen
, "OFB64");
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
);
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
527 * INPUT --(--------->(+)+---> DATA
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.
539 * V(n+1) = DES(On, key)
543 cfb64_encrypt(unsigned char *s
, int c
)
545 register struct stinfo
*stp
= &fb
[CFB
].streams
[DIR_ENCRYPT
-1];
548 idx
= stp
->str_index
;
550 if (idx
== sizeof(Block
)) {
552 des_ecb_encrypt(&stp
->str_output
, &b
, stp
->str_sched
, 1);
553 memmove((void *)stp
->str_feed
, (void *)b
, sizeof(Block
));
557 /* On encryption, we store (feed ^ data) which is cypher */
558 *s
= stp
->str_output
[idx
] = (stp
->str_feed
[idx
] ^ *s
);
562 stp
->str_index
= idx
;
566 cfb64_decrypt(int data
)
568 register struct stinfo
*stp
= &fb
[CFB
].streams
[DIR_DECRYPT
-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.
582 idx
= stp
->str_index
++;
583 if (idx
== sizeof(Block
)) {
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
604 * INPUT -------->(+) ----> DATA
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.
612 * V(n+1) = DES(Vn, key)
616 ofb64_encrypt(unsigned char *s
, int c
)
618 register struct stinfo
*stp
= &fb
[OFB
].streams
[DIR_ENCRYPT
-1];
621 idx
= stp
->str_index
;
623 if (idx
== sizeof(Block
)) {
625 des_ecb_encrypt(&stp
->str_feed
, &b
, stp
->str_sched
, 1);
626 memmove((void *)stp
->str_feed
, (void *)b
, sizeof(Block
));
629 *s
++ ^= stp
->str_feed
[idx
];
632 stp
->str_index
= idx
;
636 ofb64_decrypt(int data
)
638 register struct stinfo
*stp
= &fb
[OFB
].streams
[DIR_DECRYPT
-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.
652 idx
= stp
->str_index
++;
653 if (idx
== sizeof(Block
)) {
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 */