Debian Codegraph @

libtelnet/enc_des.c

raw
1 /*
2 Copyright (C) 1993-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1991, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 #include <config.h>
49
50 #ifdef ENCRYPTION
51 # ifdef AUTHENTICATION
52 # if defined DES_ENCRYPTION || defined SHISHI
53 # ifdef SHISHI
54 # include <shishi.h>
55 extern Shishi *shishi_handle;
56 # endif
57 # include <arpa/telnet.h>
58 # include <stdio.h>
59 # include <stdlib.h>
60
61 # include "encrypt.h"
62 # include "key-proto.h"
63 # include "misc-proto.h"
64
65 extern int encrypt_debug_mode;
66
67 # define CFB 0
68 # define OFB 1
69
70 # define NO_SEND_IV 1
71 # define NO_RECV_IV 2
72 # define NO_KEYID 4
73 # define IN_PROGRESS (NO_SEND_IV|NO_RECV_IV|NO_KEYID)
74 # define SUCCESS 0
75 # define FAILED -1
76
77
78 # include <string.h>
79
80 struct fb
81 {
82 Block krbdes_key;
83 Schedule krbdes_sched;
84 Block temp_feed;
85 unsigned char fb_feed[64];
86 int need_start;
87 int state[2];
88 int keyid[2];
89 int once;
90 struct stinfo
91 {
92 Block str_output;
93 Block str_feed;
94 Block str_iv;
95 Block str_ikey;
96 Schedule str_sched;
97 int str_index;
98 int str_flagshift;
99 } streams[2];
100 };
101
102 static struct fb fb[2];
103
104 struct keyidlist
105 {
106 char *keyid;
107 int keyidlen;
108 char *key;
109 int keylen;
110 int flags;
111 } keyidlist[] = {
112 {"\0", 1, 0, 0, 0}, /* default key of zero */
113 {0, 0, 0, 0, 0}
114 };
115
116 # define KEYFLAG_MASK 03
117
118 # define KEYFLAG_NOINIT 00
119 # define KEYFLAG_INIT 01
120 # define KEYFLAG_OK 02
121 # define KEYFLAG_BAD 03
122
123 # define KEYFLAG_SHIFT 2
124
125 # define SHIFT_VAL(a,b) (KEYFLAG_SHIFT*((a)+((b)*2)))
126
127 # ifndef FB64_IV
128 # define FB64_IV 1
129 # endif
130 # ifndef FB64_IV_OK
131 # define FB64_IV_OK 2
132 # endif
133 # ifndef FB64_IV_BAD
134 # define FB64_IV_BAD 3
135 # endif
136
137
138 /* Callback from consumer. */
139 extern void printsub (char, unsigned char *, int);
140
141 static void fb64_stream_iv (Block, struct stinfo *);
142 static void fb64_init (struct fb *);
143 static int fb64_start (struct fb *, int, int);
144 static int fb64_is (unsigned char *, int, struct fb *);
145 static int fb64_reply (unsigned char *, int, struct fb *);
146 static void fb64_session (Session_Key *, int, struct fb *);
147 static void fb64_stream_key (Block, struct stinfo *);
148 static int fb64_keyid (int, unsigned char *, int *, struct fb *);
149 static int des_check_parity (Block b);
150 static int des_set_parity (Block b);
151
152 # ifdef SHISHI
153 static void
154 shishi_des_ecb_encrypt (Shishi *h, const unsigned char key[sizeof (Block)],
155 const unsigned char *in, unsigned char *out)
156 {
157 char *tmp;
158
159 shishi_des (h, 0, (const char *) key, NULL, NULL,
160 (const char *) in, sizeof (Block), &tmp);
161 memcpy (out, tmp, sizeof (Block));
162 free (tmp);
163 }
164 # endif
165
166 void
167 cfb64_init (int server)
168 {
169 fb64_init (&fb[CFB]);
170 fb[CFB].fb_feed[4] = ENCTYPE_DES_CFB64;
171 fb[CFB].streams[0].str_flagshift = SHIFT_VAL (0, CFB);
172 fb[CFB].streams[1].str_flagshift = SHIFT_VAL (1, CFB);
173 }
174
175 # ifdef ENCTYPE_DES_OFB64
176 void
177 ofb64_init (int server)
178 {
179 fb64_init (&fb[OFB]);
180 fb[OFB].fb_feed[4] = ENCTYPE_DES_OFB64;
181 fb[CFB].streams[0].str_flagshift = SHIFT_VAL (0, OFB);
182 fb[CFB].streams[1].str_flagshift = SHIFT_VAL (1, OFB);
183 }
184 # endif
185 /* ENCTYPE_DES_OFB64 */
186
187 static void
188 fb64_init (struct fb *fbp)
189 {
190 memset ((void *) fbp, 0, sizeof (*fbp));
191 fbp->state[0] = fbp->state[1] = FAILED;
192 fbp->fb_feed[0] = IAC;
193 fbp->fb_feed[1] = SB;
194 fbp->fb_feed[2] = TELOPT_ENCRYPT;
195 fbp->fb_feed[3] = ENCRYPT_IS;
196 }
197
198 /*
199 * Returns:
200 * -1: some error. Negotiation is done, encryption not ready.
201 * 0: Successful, initial negotiation all done.
202 * 1: successful, negotiation not done yet.
203 * 2: Not yet. Other things (like getting the key from
204 * Kerberos) have to happen before we can continue.
205 */
206 int
207 cfb64_start (int dir, int server)
208 {
209 return (fb64_start (&fb[CFB], dir, server));
210 }
211
212 # ifdef ENCTYPE_DES_OFB64
213 int
214 ofb64_start (int dir, int server)
215 {
216 return (fb64_start (&fb[OFB], dir, server));
217 }
218 # endif
219 /* ENCTYPE_DES_OFB64 */
220
221 static int
222 fb64_start (struct fb *fbp, int dir, int server)
223 {
224 size_t x;
225 unsigned char *p;
226 int state;
227
228 switch (dir)
229 {
230 case DIR_DECRYPT:
231 /*
232 * This is simply a request to have the other side
233 * start output (our input). He will negotiate an
234 * IV so we need not look for it.
235 */
236 state = fbp->state[dir - 1];
237 if (state == FAILED)
238 state = IN_PROGRESS;
239 break;
240
241 case DIR_ENCRYPT:
242 state = fbp->state[dir - 1];
243 if (state == FAILED)
244 state = IN_PROGRESS;
245 else if ((state & NO_SEND_IV) == 0)
246 break;
247
248 if (!VALIDKEY (fbp->krbdes_key))
249 {
250 fbp->need_start = 1;
251 break;
252 }
253 state &= ~NO_SEND_IV;
254 state |= NO_RECV_IV;
255 if (encrypt_debug_mode)
256 printf ("Creating new feed\r\n");
257 /*
258 * Create a random feed and send it over.
259 */
260 # ifdef SHISHI
261 if (shishi_randomize (shishi_handle, 0,
262 fbp->temp_feed, sizeof (Block)) != SHISHI_OK)
263 return (FAILED);
264
265 # else
266 des_new_random_key (fbp->temp_feed);
267 des_ecb_encrypt (fbp->temp_feed, fbp->temp_feed, fbp->krbdes_sched, 1);
268 # endif
269 p = fbp->fb_feed + 3;
270 *p++ = ENCRYPT_IS;
271 p++;
272 *p++ = FB64_IV;
273 for (x = 0; x < sizeof (Block); ++x)
274 {
275 if ((*p++ = fbp->temp_feed[x]) == IAC)
276 *p++ = IAC;
277 }
278 *p++ = IAC;
279 *p++ = SE;
280 printsub ('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
281 net_write (fbp->fb_feed, p - fbp->fb_feed);
282 break;
283 default:
284 return (FAILED);
285 }
286
287 fbp->state[dir - 1] = state;
288
289 return (state);
290 }
291
292 /*
293 * Returns:
294 * -1: some error. Negotiation is done, encryption not ready.
295 * 0: Successful, initial negotiation all done.
296 * 1: successful, negotiation not done yet.
297 */
298 int
299 cfb64_is (unsigned char *data, int cnt)
300 {
301 return (fb64_is (data, cnt, &fb[CFB]));
302 }
303
304 # ifdef ENCTYPE_DES_OFB64
305 int
306 ofb64_is (unsigned char *data, int cnt)
307 {
308 return (fb64_is (data, cnt, &fb[OFB]));
309 }
310 # endif
311 /* ENCTYPE_DES_OFB64 */
312
313 static int
314 fb64_is (unsigned char *data, int cnt, struct fb *fbp)
315 {
316 unsigned char *p;
317 int state = fbp->state[DIR_DECRYPT - 1];
318
319 if (cnt-- < 1)
320 goto failure;
321
322 switch (*data++)
323 {
324 case FB64_IV:
325 if (cnt != sizeof (Block))
326 {
327 if (encrypt_debug_mode)
328 printf ("FB64: initial vector failed on size\r\n");
329 state = FAILED;
330 goto failure;
331 }
332
333 if (encrypt_debug_mode)
334 printf ("FB64: initial vector received\r\n");
335
336 if (encrypt_debug_mode)
337 printf ("Initializing Decrypt stream\r\n");
338
339 fb64_stream_iv ((void *) data, &fbp->streams[DIR_DECRYPT - 1]);
340
341 p = fbp->fb_feed + 3;
342 *p++ = ENCRYPT_REPLY;
343 p++;
344 *p++ = FB64_IV_OK;
345 *p++ = IAC;
346 *p++ = SE;
347 printsub ('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
348 net_write (fbp->fb_feed, p - fbp->fb_feed);
349
350 state = fbp->state[DIR_DECRYPT - 1] = IN_PROGRESS;
351 break;
352
353 default:
354 if (encrypt_debug_mode)
355 {
356 printf ("Unknown option type: %d\r\n", *(data - 1));
357 printd (data, cnt);
358 printf ("\r\n");
359 }
360 /* FALL THROUGH */
361 failure:
362 /*
363 * We failed. Send an FB64_IV_BAD option
364 * to the other side so it will know that
365 * things failed.
366 */
367 p = fbp->fb_feed + 3;
368 *p++ = ENCRYPT_REPLY;
369 p++;
370 *p++ = FB64_IV_BAD;
371 *p++ = IAC;
372 *p++ = SE;
373 printsub ('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);
374 net_write (fbp->fb_feed, p - fbp->fb_feed);
375
376 break;
377 }
378
379 fbp->state[DIR_DECRYPT - 1] = state;
380
381 return (state);
382 }
383
384 /*
385 * Returns:
386 * -1: some error. Negotiation is done, encryption not ready.
387 * 0: Successful, initial negotiation all done.
388 * 1: successful, negotiation not done yet.
389 */
390 int
391 cfb64_reply (unsigned char *data, int cnt)
392 {
393 return (fb64_reply (data, cnt, &fb[CFB]));
394 }
395
396 # ifdef ENCTYPE_DES_OFB64
397 int
398 ofb64_reply (unsigned char *data, int cnt)
399 {
400 return (fb64_reply (data, cnt, &fb[OFB]));
401 }
402 # endif
403 /* ENCTYPE_DES_OFB64 */
404
405
406 static int
407 fb64_reply (unsigned char *data, int cnt, struct fb *fbp)
408 {
409 int state = fbp->state[DIR_ENCRYPT - 1];
410
411 if (cnt-- < 1)
412 goto failure;
413
414 switch (*data++)
415 {
416 case FB64_IV_OK:
417 fb64_stream_iv (fbp->temp_feed, &fbp->streams[DIR_ENCRYPT - 1]);
418 if (state == FAILED)
419 state = IN_PROGRESS;
420 state &= ~NO_RECV_IV;
421 encrypt_send_keyid (DIR_ENCRYPT, (unsigned char *) "\0", 1, 1);
422 break;
423
424 case FB64_IV_BAD:
425 memset (fbp->temp_feed, 0, sizeof (Block));
426 fb64_stream_iv (fbp->temp_feed, &fbp->streams[DIR_ENCRYPT - 1]);
427 state = FAILED;
428 break;
429
430 default:
431 if (encrypt_debug_mode)
432 {
433 printf ("Unknown option type: %d\r\n", data[-1]);
434 printd (data, cnt);
435 printf ("\r\n");
436 }
437 /* FALL THROUGH */
438 failure:
439 state = FAILED;
440 break;
441 }
442
443 fbp->state[DIR_ENCRYPT - 1] = state;
444
445 return (state);
446 }
447
448 void
449 cfb64_session (Session_Key *key, int server)
450 {
451 fb64_session (key, server, &fb[CFB]);
452 }
453
454 # ifdef ENCTYPE_DES_OFB64
455 void
456 ofb64_session (Session_Key *key, int server)
457 {
458 fb64_session (key, server, &fb[OFB]);
459 }
460 # endif
461 /* ENCTYPE_DES_OFB64 */
462
463 static void
464 fb64_session (Session_Key *key, int server, struct fb *fbp)
465 {
466 size_t offset;
467 unsigned char *derived_key;
468
469 if (!key || key->type != SK_DES)
470 {
471 /* FIXME: Support RFC 2952 approach instead of giving up here. */
472 if (encrypt_debug_mode)
473 printf ("Received non-DES session key (%d != %d)\r\n",
474 key ? key->type : -1, SK_DES);
475 if (!key)
476 return; /* XXX: Causes a segfault, since *key is NULL. */
477
478 /* Follow RFC 2952 in using the authentication key
479 * to derived one or more DES-keys, after adjusting
480 * the parity in each eight byte block.
481 */
482 }
483
484 /* Make a copy of the authentication key,
485 * since the parity might need mending. */
486 derived_key = malloc (key->length);
487 if (!derived_key)
488 return; /* Still destructive, but no alternate method in sight. */
489
490 memmove ((void *) derived_key, (void *) key->data, key->length);
491
492 /* Check parity of each DES block, correct it whenever needed. */
493 for (offset = 0; offset < (size_t) key->length; offset += sizeof (Block))
494 (void) des_set_parity (derived_key + offset);
495
496 /* XXX: A single key block is in use for now,
497 * but all block are of correct parity.
498 * krbdes_key should be an array of block,
499 * which each encryption method may use at
500 * it own discretion. This is the content
501 * if RFC 2946 and 2952, etcetera.
502 */
503 memmove ((void *) fbp->krbdes_key, (void *) derived_key, sizeof (Block));
504
505 /* XXX: These should at least be split according
506 * to direction and role, i.e., client or server.
507 */
508 fb64_stream_key (fbp->krbdes_key, &fbp->streams[DIR_ENCRYPT - 1]);
509 fb64_stream_key (fbp->krbdes_key, &fbp->streams[DIR_DECRYPT - 1]);
510
511 /* Erase sensitive key material. */
512 memset (derived_key, 0, key->length);
513
514 if (fbp->once == 0)
515 {
516 # ifndef SHISHI
517 des_set_random_generator_seed (fbp->krbdes_key);
518 # endif
519 fbp->once = 1;
520 }
521 # ifndef SHISHI
522 des_key_sched (fbp->krbdes_key, fbp->krbdes_sched);
523 # endif
524 /*
525 * Now look to see if krbdes_start() was waiting for
526 * the key to show up. If so, go ahead and call it now
527 * that we have the key.
528 */
529 if (fbp->need_start)
530 {
531 fbp->need_start = 0;
532 fb64_start (fbp, DIR_ENCRYPT, server);
533 }
534 }
535
536 /*
537 * We only accept a keyid of 0. If we get a keyid of
538 * 0, then mark the state as SUCCESS.
539 */
540 int
541 cfb64_keyid (int dir, unsigned char *kp, int *lenp)
542 {
543 return (fb64_keyid (dir, kp, lenp, &fb[CFB]));
544 }
545
546 # ifdef ENCTYPE_DES_OFB64
547 int
548 ofb64_keyid (int dir, unsigned char *kp, int *lenp)
549 {
550 return (fb64_keyid (dir, kp, lenp, &fb[OFB]));
551 }
552 # endif
553 /* ENCTYPE_DES_OFB64 */
554
555 static int
556 fb64_keyid (int dir, unsigned char *kp, int *lenp, struct fb *fbp)
557 {
558 int state = fbp->state[dir - 1];
559
560 if (*lenp != 1 || (*kp != '\0'))
561 {
562 *lenp = 0;
563 return (state);
564 }
565
566 if (state == FAILED)
567 state = IN_PROGRESS;
568
569 state &= ~NO_KEYID;
570
571 fbp->state[dir - 1] = state;
572
573 return (state);
574 }
575
576 static void
577 fb64_printsub (unsigned char *data, int cnt,
578 char *buf, int buflen, const char *type)
579 {
580 char lbuf[32];
581 int i;
582 char *cp;
583
584 buf[buflen - 1] = '\0'; /* make sure it's NULL terminated */
585 buflen -= 1;
586
587 switch (data[2])
588 {
589 case FB64_IV:
590 sprintf (lbuf, "%s_IV", type);
591 cp = lbuf;
592 goto common;
593
594 case FB64_IV_OK:
595 sprintf (lbuf, "%s_IV_OK", type);
596 cp = lbuf;
597 goto common;
598
599 case FB64_IV_BAD:
600 sprintf (lbuf, "%s_IV_BAD", type);
601 cp = lbuf;
602 goto common;
603
604 default:
605 sprintf (lbuf, " %d (unknown)", data[2]);
606 cp = lbuf;
607 common:
608 for (; (buflen > 0) && (*buf = *cp++); buf++)
609 buflen--;
610 for (i = 3; i < cnt; i++)
611 {
612 sprintf (lbuf, " %d", data[i]);
613 for (cp = lbuf; (buflen > 0) && (*buf = *cp++); buf++)
614 buflen--;
615 }
616 break;
617 }
618 }
619
620 void
621 cfb64_printsub (unsigned char *data, int cnt, char *buf, int buflen)
622 {
623 fb64_printsub (data, cnt, buf, buflen, "CFB64");
624 }
625
626 # ifdef ENCTYPE_DES_OFB64
627 void
628 ofb64_printsub (unsigned char *data, int cnt, char *buf, int buflen)
629 {
630 fb64_printsub (data, cnt, buf, buflen, "OFB64");
631 }
632 # endif
633 /* ENCTYPE_DES_OFB64 */
634
635 static void
636 fb64_stream_iv (Block seed, struct stinfo *stp)
637 {
638
639 memmove ((void *) stp->str_iv, (void *) seed, sizeof (Block));
640 memmove ((void *) stp->str_output, (void *) seed, sizeof (Block));
641
642 # ifndef SHISHI
643 des_key_sched (stp->str_ikey, stp->str_sched);
644 # endif
645
646 stp->str_index = sizeof (Block);
647 }
648
649 static void
650 fb64_stream_key (Block key, struct stinfo *stp)
651 {
652 memmove ((void *) stp->str_ikey, (void *) key, sizeof (Block));
653 # ifndef SHISHI
654 des_key_sched (key, stp->str_sched);
655 # endif
656 memmove ((void *) stp->str_output, (void *) stp->str_iv, sizeof (Block));
657
658 stp->str_index = sizeof (Block);
659 }
660
661 /*
662 * DES 64 bit Cipher Feedback
663 *
664 * key --->+-----+
665 * +->| DES |--+
666 * | +-----+ |
667 * | v
668 * INPUT --(--------->(+)+---> DATA
669 * | |
670 * +-------------+
671 *
672 *
673 * Given:
674 * iV: Initial vector, 64 bits (8 bytes) long.
675 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
676 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
677 *
678 * V0 = DES(iV, key)
679 * On = Dn ^ Vn
680 * V(n+1) = DES(On, key)
681 */
682
683 void
684 cfb64_encrypt (unsigned char *s, int c)
685 {
686 struct stinfo *stp = &fb[CFB].streams[DIR_ENCRYPT - 1];
687 int index;
688
689 index = stp->str_index;
690 while (c-- > 0)
691 {
692 if (index == sizeof (Block))
693 {
694 Block b;
695 # ifdef SHISHI
696 shishi_des_ecb_encrypt (shishi_handle, fb[CFB].krbdes_key,
697 stp->str_output, b);
698 # else
699 des_ecb_encrypt (stp->str_output, b, stp->str_sched, 1);
700 # endif
701 memmove ((void *) stp->str_feed, (void *) b, sizeof (Block));
702 index = 0;
703 }
704
705 /* On encryption, we store (feed ^ data) which is cypher */
706 *s = stp->str_output[index] = (stp->str_feed[index] ^ *s);
707 s++;
708 index++;
709 }
710 stp->str_index = index;
711 }
712
713 int
714 cfb64_decrypt (int data)
715 {
716 struct stinfo *stp = &fb[CFB].streams[DIR_DECRYPT - 1];
717 int index;
718
719 if (data == -1)
720 {
721 /*
722 * Back up one byte. It is assumed that we will
723 * never back up more than one byte. If we do, this
724 * may or may not work.
725 */
726 if (stp->str_index)
727 --stp->str_index;
728 return (0);
729 }
730
731 index = stp->str_index++;
732 if (index == sizeof (Block))
733 {
734 Block b;
735 # ifdef SHISHI
736 shishi_des_ecb_encrypt (shishi_handle, fb[CFB].krbdes_key,
737 stp->str_output, b);
738 # else
739 des_ecb_encrypt (stp->str_output, b, stp->str_sched, 1);
740 # endif
741 memmove ((void *) stp->str_feed, (void *) b, sizeof (Block));
742 stp->str_index = 1; /* Next time will be 1 */
743 index = 0; /* But now use 0 */
744 }
745
746 /* On decryption we store (data) which is cypher. */
747 stp->str_output[index] = data;
748
749 return (data ^ stp->str_feed[index]);
750 }
751
752 /*
753 * DES 64 bit Output Feedback
754 *
755 * key --->+-----+
756 * +->| DES |--+
757 * | +-----+ |
758 * +-----------+
759 * v
760 * INPUT -------->(+) ----> DATA
761 *
762 * Given:
763 * iV: Initial vector, 64 bits (8 bytes) long.
764 * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).
765 * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.
766 *
767 * V0 = DES(iV, key)
768 * V(n+1) = DES(Vn, key)
769 * On = Dn ^ Vn
770 */
771 # ifdef ENCTYPE_DES_OFB64
772 void
773 ofb64_encrypt (unsigned char *s, int c)
774 {
775 struct stinfo *stp = &fb[OFB].streams[DIR_ENCRYPT - 1];
776 int index;
777
778 index = stp->str_index;
779 while (c-- > 0)
780 {
781 if (index == sizeof (Block))
782 {
783 Block b;
784 # ifdef SHISHI
785 shishi_des_ecb_encrypt (shishi_handle, fb[OFB].krbdes_key,
786 stp->str_feed, b);
787 # else
788 des_ecb_encrypt (stp->str_feed, b, stp->str_sched, 1);
789 # endif
790 memmove ((void *) stp->str_feed, (void *) b, sizeof (Block));
791 index = 0;
792 }
793 *s++ ^= stp->str_feed[index];
794 index++;
795 }
796 stp->str_index = index;
797 }
798
799 int
800 ofb64_decrypt (int data)
801 {
802 struct stinfo *stp = &fb[OFB].streams[DIR_DECRYPT - 1];
803 int index;
804
805 if (data == -1)
806 {
807 /*
808 * Back up one byte. It is assumed that we will
809 * never back up more than one byte. If we do, this
810 * may or may not work.
811 */
812 if (stp->str_index)
813 --stp->str_index;
814 return (0);
815 }
816
817 index = stp->str_index++;
818 if (index == sizeof (Block))
819 {
820 Block b;
821 # ifdef SHISHI
822 shishi_des_ecb_encrypt (shishi_handle, fb[OFB].krbdes_key,
823 stp->str_feed, b);
824 # else
825 des_ecb_encrypt (stp->str_feed, b, stp->str_sched, 1);
826 # endif
827 memmove ((void *) stp->str_feed, (void *) b, sizeof (Block));
828 stp->str_index = 1; /* Next time will be 1 */
829 index = 0; /* But now use 0 */
830 }
831
832 return (data ^ stp->str_feed[index]);
833 }
834 # endif
835 /* ENCTYPE_DES_OFB64 */
836
837 static int
838 des_parity (Block b, int adjust)
839 {
840 size_t index;
841 int adj = 0;
842
843 for (index = 0; index < sizeof (Block); index++)
844 {
845 unsigned char c = b[index];
846
847 c ^= (c >> 4);
848 c ^= (c >> 2);
849 c ^= (c >> 1);
850
851 if (!(c & 1))
852 {
853 /* Even parity. */
854 adj++;
855 if (adjust)
856 *(&b[index]) ^= 0x01;
857 }
858 }
859
860 return adj;
861 }
862
863 /*
864 * Returns:
865 * 0: Correct parity in full key block.
866 * n: Count of corrected bytes.
867 */
868
869 static int
870 des_check_parity (Block b)
871 {
872 return des_parity (b, 0);
873 }
874
875 static int
876 des_set_parity (Block b)
877 {
878 return des_parity (b, 1);
879 }
880
881 # endif
882 /* DES_ENCRYPTION || SHISHI */
883 # endif/* AUTHENTICATION */
884 #endif /* ENCRYPTION */