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