enc_des.c revision 103423
1246149Ssjg/*- 2246149Ssjg * Copyright (c) 1991, 1993 3276305Sngie * The Regents of the University of California. All rights reserved. 4276305Sngie * 5246149Ssjg * Redistribution and use in source and binary forms, with or without 6246149Ssjg * modification, are permitted provided that the following conditions 7246149Ssjg * are met: 8246149Ssjg * 1. Redistributions of source code must retain the above copyright 9246149Ssjg * notice, this list of conditions and the following disclaimer. 10246149Ssjg * 2. Redistributions in binary form must reproduce the above copyright 11246149Ssjg * notice, this list of conditions and the following disclaimer in the 12246149Ssjg * documentation and/or other materials provided with the distribution. 13246149Ssjg * 3. All advertising materials mentioning features or use of this software 14246149Ssjg * must display the following acknowledgement: 15246149Ssjg * This product includes software developed by the University of 16246149Ssjg * California, Berkeley and its contributors. 17246149Ssjg * 4. Neither the name of the University nor the names of its contributors 18246149Ssjg * may be used to endorse or promote products derived from this software 19246149Ssjg * without specific prior written permission. 20246149Ssjg * 21246149Ssjg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22246149Ssjg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23246149Ssjg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24246149Ssjg * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25246149Ssjg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26246149Ssjg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27246149Ssjg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28246149Ssjg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29246149Ssjg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30246149Ssjg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31246149Ssjg * SUCH DAMAGE. 32246149Ssjg */ 33246149Ssjg 34246149Ssjg#include <config.h> 35246149Ssjg 36246149SsjgRCSID("$Id: enc_des.c,v 1.21 2002/09/10 20:03:47 joda Exp $"); 37246149Ssjg 38246149Ssjg#if defined(AUTHENTICATION) && defined(ENCRYPTION) && defined(DES_ENCRYPTION) 39246149Ssjg#include <arpa/telnet.h> 40292068Ssjg#include <stdio.h> 41246149Ssjg#ifdef __STDC__ 42249033Ssjg#include <stdlib.h> 43249033Ssjg#include <string.h> 44246149Ssjg#endif 45246149Ssjg#include <roken.h> 46246149Ssjg#ifdef SOCKS 47246149Ssjg#include <socks.h> 48246149Ssjg#endif 49246149Ssjg 50246149Ssjg#include "encrypt.h" 51246149Ssjg#include "misc-proto.h" 52246149Ssjg 53246149Ssjg#include "crypto-headers.h" 54246149Ssjg 55246149Ssjgextern int encrypt_debug_mode; 56246149Ssjg 57246149Ssjg#define CFB 0 58246149Ssjg#define OFB 1 59246149Ssjg 60246149Ssjg#define NO_SEND_IV 1 61246149Ssjg#define NO_RECV_IV 2 62246149Ssjg#define NO_KEYID 4 63246149Ssjg#define IN_PROGRESS (NO_SEND_IV|NO_RECV_IV|NO_KEYID) 64246149Ssjg#define SUCCESS 0 65246149Ssjg#define FAILED -1 66246149Ssjg 67246149Ssjg 68246149Ssjgstruct stinfo { 69246149Ssjg des_cblock str_output; 70246149Ssjg des_cblock str_feed; 71246149Ssjg des_cblock str_iv; 72246149Ssjg des_cblock str_ikey; 73246149Ssjg des_key_schedule str_sched; 74246149Ssjg int str_index; 75246149Ssjg int str_flagshift; 76246149Ssjg}; 77246149Ssjg 78246149Ssjgstruct fb { 79246149Ssjg des_cblock krbdes_key; 80246149Ssjg des_key_schedule krbdes_sched; 81246149Ssjg des_cblock temp_feed; 82253883Ssjg unsigned char fb_feed[64]; 83246149Ssjg int need_start; 84246149Ssjg int state[2]; 85246149Ssjg int keyid[2]; 86246149Ssjg int once; 87246149Ssjg struct stinfo streams[2]; 88246149Ssjg}; 89246149Ssjg 90246149Ssjgstatic struct fb fb[2]; 91246149Ssjg 92246149Ssjgstruct keyidlist { 93246149Ssjg char *keyid; 94246149Ssjg int keyidlen; 95276305Sngie char *key; 96246149Ssjg int keylen; 97246149Ssjg int flags; 98276305Sngie} keyidlist [] = { 99246149Ssjg { "\0", 1, 0, 0, 0 }, /* default key of zero */ 100246149Ssjg { 0, 0, 0, 0, 0 } 101246149Ssjg}; 102276305Sngie 103246149Ssjg#define KEYFLAG_MASK 03 104246149Ssjg 105246149Ssjg#define KEYFLAG_NOINIT 00 106246149Ssjg#define KEYFLAG_INIT 01 107246149Ssjg#define KEYFLAG_OK 02 108246149Ssjg#define KEYFLAG_BAD 03 109246149Ssjg 110246149Ssjg#define KEYFLAG_SHIFT 2 111246149Ssjg 112253883Ssjg#define SHIFT_VAL(a,b) (KEYFLAG_SHIFT*((a)+((b)*2))) 113253883Ssjg 114253883Ssjg#define FB64_IV 1 115292068Ssjg#define FB64_IV_OK 2 116292068Ssjg#define FB64_IV_BAD 3 117253883Ssjg 118253883Ssjg 119246149Ssjgvoid fb64_stream_iv (des_cblock, struct stinfo *); 120246149Ssjgvoid fb64_init (struct fb *); 121246149Ssjgstatic int fb64_start (struct fb *, int, int); 122246149Ssjgint fb64_is (unsigned char *, int, struct fb *); 123246149Ssjgint fb64_reply (unsigned char *, int, struct fb *); 124246149Ssjgstatic void fb64_session (Session_Key *, int, struct fb *); 125246149Ssjgvoid fb64_stream_key (des_cblock, struct stinfo *); 126246149Ssjgint fb64_keyid (int, unsigned char *, int *, struct fb *); 127246149Ssjgvoid fb64_printsub(unsigned char *, int , 128246149Ssjg unsigned char *, int , char *); 129246149Ssjg 130246149Ssjgvoid cfb64_init(int server) 131246149Ssjg{ 132250837Ssjg fb64_init(&fb[CFB]); 133250837Ssjg fb[CFB].fb_feed[4] = ENCTYPE_DES_CFB64; 134250837Ssjg fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, CFB); 135250837Ssjg fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, CFB); 136250837Ssjg} 137250837Ssjg 138246149Ssjg 139246149Ssjgvoid ofb64_init(int server) 140246149Ssjg{ 141246149Ssjg fb64_init(&fb[OFB]); 142246149Ssjg fb[OFB].fb_feed[4] = ENCTYPE_DES_OFB64; 143246149Ssjg fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, OFB); 144246149Ssjg fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, OFB); 145246149Ssjg} 146292068Ssjg 147246149Ssjgvoid fb64_init(struct fb *fbp) 148246149Ssjg{ 149246149Ssjg memset(fbp,0, sizeof(*fbp)); 150246149Ssjg fbp->state[0] = fbp->state[1] = FAILED; 151246149Ssjg fbp->fb_feed[0] = IAC; 152246149Ssjg fbp->fb_feed[1] = SB; 153246149Ssjg fbp->fb_feed[2] = TELOPT_ENCRYPT; 154246149Ssjg fbp->fb_feed[3] = ENCRYPT_IS; 155246149Ssjg} 156246149Ssjg 157246149Ssjg/* 158246149Ssjg * Returns: 159246149Ssjg * -1: some error. Negotiation is done, encryption not ready. 160246149Ssjg * 0: Successful, initial negotiation all done. 161246149Ssjg * 1: successful, negotiation not done yet. 162246149Ssjg * 2: Not yet. Other things (like getting the key from 163246149Ssjg * Kerberos) have to happen before we can continue. 164249033Ssjg */ 165246149Ssjgint cfb64_start(int dir, int server) 166250837Ssjg{ 167250837Ssjg return(fb64_start(&fb[CFB], dir, server)); 168250837Ssjg} 169246149Ssjg 170276305Sngieint ofb64_start(int dir, int server) 171246149Ssjg{ 172246149Ssjg return(fb64_start(&fb[OFB], dir, server)); 173246149Ssjg} 174246149Ssjg 175246149Ssjgstatic int fb64_start(struct fb *fbp, int dir, int server) 176246149Ssjg{ 177246149Ssjg int x; 178246149Ssjg unsigned char *p; 179246149Ssjg int state; 180246149Ssjg 181246149Ssjg switch (dir) { 182246149Ssjg case DIR_DECRYPT: 183292068Ssjg /* 184292068Ssjg * This is simply a request to have the other side 185292068Ssjg * start output (our input). He will negotiate an 186292068Ssjg * IV so we need not look for it. 187246149Ssjg */ 188246149Ssjg state = fbp->state[dir-1]; 189246149Ssjg if (state == FAILED) 190246149Ssjg state = IN_PROGRESS; 191246149Ssjg break; 192246149Ssjg 193246149Ssjg case DIR_ENCRYPT: 194246149Ssjg state = fbp->state[dir-1]; 195246149Ssjg if (state == FAILED) 196246149Ssjg state = IN_PROGRESS; 197249033Ssjg else if ((state & NO_SEND_IV) == 0) { 198250837Ssjg break; 199250837Ssjg } 200249033Ssjg 201249033Ssjg if (!VALIDKEY(fbp->krbdes_key)) { 202249033Ssjg fbp->need_start = 1; 203253883Ssjg break; 204249033Ssjg } 205246149Ssjg 206246149Ssjg state &= ~NO_SEND_IV; 207246149Ssjg state |= NO_RECV_IV; 208246149Ssjg if (encrypt_debug_mode) 209246149Ssjg printf("Creating new feed\r\n"); 210246149Ssjg /* 211246149Ssjg * Create a random feed and send it over. 212246149Ssjg */ 213246149Ssjg#ifndef OLD_DES_RANDOM_KEY 214246149Ssjg des_new_random_key(&fbp->temp_feed); 215246149Ssjg#else 216246149Ssjg /* 217246149Ssjg * From des_cryp.man "If the des_check_key flag is non-zero, 218246149Ssjg * des_set_key will check that the key passed is 219246149Ssjg * of odd parity and is not a week or semi-weak key." 220250837Ssjg */ 221249033Ssjg do { 222250837Ssjg des_random_key(fbp->temp_feed); 223250837Ssjg des_set_odd_parity(fbp->temp_feed); 224250837Ssjg } while (des_is_weak_key(fbp->temp_feed)); 225249033Ssjg#endif 226246149Ssjg des_ecb_encrypt(&fbp->temp_feed, 227249033Ssjg &fbp->temp_feed, 228249033Ssjg fbp->krbdes_sched, 1); 229249033Ssjg p = fbp->fb_feed + 3; 230249033Ssjg *p++ = ENCRYPT_IS; 231249033Ssjg p++; 232249033Ssjg *p++ = FB64_IV; 233246149Ssjg for (x = 0; x < sizeof(des_cblock); ++x) { 234246149Ssjg if ((*p++ = fbp->temp_feed[x]) == IAC) 235246149Ssjg *p++ = IAC; 236246149Ssjg } 237246149Ssjg *p++ = IAC; 238246149Ssjg *p++ = SE; 239246149Ssjg printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]); 240246149Ssjg telnet_net_write(fbp->fb_feed, p - fbp->fb_feed); 241249033Ssjg break; 242249033Ssjg default: 243249033Ssjg return(FAILED); 244292068Ssjg } 245292068Ssjg return(fbp->state[dir-1] = state); 246292068Ssjg} 247246149Ssjg 248276305Sngie/* 249276305Sngie * Returns: 250276305Sngie * -1: some error. Negotiation is done, encryption not ready. 251292068Ssjg * 0: Successful, initial negotiation all done. 252246149Ssjg * 1: successful, negotiation not done yet. 253246149Ssjg */ 254246149Ssjg 255246149Ssjgint cfb64_is(unsigned char *data, int cnt) 256246149Ssjg{ 257276305Sngie return(fb64_is(data, cnt, &fb[CFB])); 258246149Ssjg} 259246149Ssjg 260246149Ssjgint ofb64_is(unsigned char *data, int cnt) 261246149Ssjg{ 262246149Ssjg return(fb64_is(data, cnt, &fb[OFB])); 263276305Sngie} 264246149Ssjg 265246149Ssjg 266246149Ssjgint fb64_is(unsigned char *data, int cnt, struct fb *fbp) 267249033Ssjg{ 268292068Ssjg unsigned char *p; 269249033Ssjg int state = fbp->state[DIR_DECRYPT-1]; 270246149Ssjg 271253883Ssjg if (cnt-- < 1) 272246149Ssjg goto failure; 273246149Ssjg 274246149Ssjg switch (*data++) { 275246149Ssjg case FB64_IV: 276246149Ssjg if (cnt != sizeof(des_cblock)) { 277246149Ssjg if (encrypt_debug_mode) 278246149Ssjg printf("CFB64: initial vector failed on size\r\n"); 279246149Ssjg state = FAILED; 280246149Ssjg goto failure; 281246149Ssjg } 282246149Ssjg 283246149Ssjg if (encrypt_debug_mode) 284246149Ssjg printf("CFB64: initial vector received\r\n"); 285246149Ssjg 286246149Ssjg if (encrypt_debug_mode) 287246149Ssjg printf("Initializing Decrypt stream\r\n"); 288246149Ssjg 289246149Ssjg fb64_stream_iv(data, &fbp->streams[DIR_DECRYPT-1]); 290246149Ssjg 291246149Ssjg p = fbp->fb_feed + 3; 292246149Ssjg *p++ = ENCRYPT_REPLY; 293246149Ssjg p++; 294276305Sngie *p++ = FB64_IV_OK; 295246149Ssjg *p++ = IAC; 296246149Ssjg *p++ = SE; 297246149Ssjg printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]); 298246149Ssjg telnet_net_write(fbp->fb_feed, p - fbp->fb_feed); 299246149Ssjg 300246149Ssjg state = fbp->state[DIR_DECRYPT-1] = IN_PROGRESS; 301246149Ssjg break; 302246149Ssjg 303246149Ssjg default: 304246149Ssjg if (encrypt_debug_mode) { 305276305Sngie printf("Unknown option type: %d\r\n", *(data-1)); 306246149Ssjg printd(data, cnt); 307246149Ssjg printf("\r\n"); 308276305Sngie } 309246149Ssjg /* FALL THROUGH */ 310246149Ssjg failure: 311246149Ssjg /* 312276305Sngie * We failed. Send an FB64_IV_BAD option 313246149Ssjg * to the other side so it will know that 314246149Ssjg * things failed. 315246149Ssjg */ 316246149Ssjg p = fbp->fb_feed + 3; 317246149Ssjg *p++ = ENCRYPT_REPLY; 318246149Ssjg p++; 319246149Ssjg *p++ = FB64_IV_BAD; 320246149Ssjg *p++ = IAC; 321276305Sngie *p++ = SE; 322246149Ssjg printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]); 323276305Sngie telnet_net_write(fbp->fb_feed, p - fbp->fb_feed); 324246149Ssjg 325246149Ssjg break; 326250837Ssjg } 327250837Ssjg return(fbp->state[DIR_DECRYPT-1] = state); 328246149Ssjg} 329246149Ssjg 330246149Ssjg/* 331246149Ssjg * Returns: 332246149Ssjg * -1: some error. Negotiation is done, encryption not ready. 333246149Ssjg * 0: Successful, initial negotiation all done. 334246149Ssjg * 1: successful, negotiation not done yet. 335246149Ssjg */ 336246149Ssjg 337246149Ssjgint cfb64_reply(unsigned char *data, int cnt) 338246149Ssjg{ 339246149Ssjg return(fb64_reply(data, cnt, &fb[CFB])); 340246149Ssjg} 341246149Ssjg 342246149Ssjgint ofb64_reply(unsigned char *data, int cnt) 343246149Ssjg{ 344246149Ssjg return(fb64_reply(data, cnt, &fb[OFB])); 345276305Sngie} 346246149Ssjg 347246149Ssjg 348246149Ssjgint fb64_reply(unsigned char *data, int cnt, struct fb *fbp) 349246149Ssjg{ 350253883Ssjg int state = fbp->state[DIR_ENCRYPT-1]; 351253883Ssjg 352253883Ssjg if (cnt-- < 1) 353253883Ssjg goto failure; 354253883Ssjg 355253883Ssjg switch (*data++) { 356276305Sngie case FB64_IV_OK: 357253883Ssjg fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]); 358253883Ssjg if (state == FAILED) 359246149Ssjg state = IN_PROGRESS; 360246149Ssjg state &= ~NO_RECV_IV; 361246149Ssjg encrypt_send_keyid(DIR_ENCRYPT, (unsigned char *)"\0", 1, 1); 362246149Ssjg break; 363246149Ssjg 364246149Ssjg case FB64_IV_BAD: 365246149Ssjg memset(fbp->temp_feed, 0, sizeof(des_cblock)); 366246149Ssjg fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]); 367246149Ssjg state = FAILED; 368246149Ssjg break; 369246149Ssjg 370246149Ssjg default: 371246149Ssjg if (encrypt_debug_mode) { 372292068Ssjg printf("Unknown option type: %d\r\n", data[-1]); 373246149Ssjg printd(data, cnt); 374246149Ssjg printf("\r\n"); 375246149Ssjg } 376292068Ssjg /* FALL THROUGH */ 377292068Ssjg failure: 378292068Ssjg state = FAILED; 379292068Ssjg break; 380292068Ssjg } 381246149Ssjg return(fbp->state[DIR_ENCRYPT-1] = state); 382292068Ssjg} 383292068Ssjg 384246149Ssjgvoid cfb64_session(Session_Key *key, int server) 385246149Ssjg{ 386246149Ssjg fb64_session(key, server, &fb[CFB]); 387246149Ssjg} 388246149Ssjg 389246149Ssjgvoid ofb64_session(Session_Key *key, int server) 390246149Ssjg{ 391292068Ssjg fb64_session(key, server, &fb[OFB]); 392246149Ssjg} 393276305Sngie 394246149Ssjgstatic void fb64_session(Session_Key *key, int server, struct fb *fbp) 395246149Ssjg{ 396246149Ssjg 397246149Ssjg if (!key || key->type != SK_DES) { 398246149Ssjg if (encrypt_debug_mode) 399253883Ssjg printf("Can't set krbdes's session key (%d != %d)\r\n", 400246149Ssjg key ? key->type : -1, SK_DES); 401246149Ssjg return; 402246149Ssjg } 403246149Ssjg memcpy(fbp->krbdes_key, key->data, sizeof(des_cblock)); 404246149Ssjg 405253883Ssjg fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_ENCRYPT-1]); 406246149Ssjg fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_DECRYPT-1]); 407246149Ssjg 408246149Ssjg if (fbp->once == 0) { 409246149Ssjg#if !defined(OLD_DES_RANDOM_KEY) && !defined(HAVE_OPENSSL) 410276305Sngie des_init_random_number_generator(&fbp->krbdes_key); 411246149Ssjg#endif 412246149Ssjg fbp->once = 1; 413246149Ssjg } 414246149Ssjg des_key_sched(&fbp->krbdes_key, fbp->krbdes_sched); 415246149Ssjg /* 416246149Ssjg * Now look to see if krbdes_start() was was waiting for 417246149Ssjg * the key to show up. If so, go ahead an call it now 418246149Ssjg * that we have the key. 419246149Ssjg */ 420246149Ssjg if (fbp->need_start) { 421246149Ssjg fbp->need_start = 0; 422246149Ssjg fb64_start(fbp, DIR_ENCRYPT, server); 423246149Ssjg } 424292068Ssjg} 425246149Ssjg 426246149Ssjg/* 427276305Sngie * We only accept a keyid of 0. If we get a keyid of 428246149Ssjg * 0, then mark the state as SUCCESS. 429246149Ssjg */ 430246149Ssjg 431246149Ssjgint cfb64_keyid(int dir, unsigned char *kp, int *lenp) 432246149Ssjg{ 433246149Ssjg return(fb64_keyid(dir, kp, lenp, &fb[CFB])); 434292068Ssjg} 435246149Ssjg 436292068Ssjgint ofb64_keyid(int dir, unsigned char *kp, int *lenp) 437246149Ssjg{ 438246149Ssjg return(fb64_keyid(dir, kp, lenp, &fb[OFB])); 439246149Ssjg} 440246149Ssjg 441246149Ssjgint fb64_keyid(int dir, unsigned char *kp, int *lenp, struct fb *fbp) 442246149Ssjg{ 443246149Ssjg int state = fbp->state[dir-1]; 444246149Ssjg 445246149Ssjg if (*lenp != 1 || (*kp != '\0')) { 446246149Ssjg *lenp = 0; 447246149Ssjg return(state); 448246149Ssjg } 449246149Ssjg 450292068Ssjg if (state == FAILED) 451246149Ssjg state = IN_PROGRESS; 452276305Sngie 453246149Ssjg state &= ~NO_KEYID; 454246149Ssjg 455246149Ssjg return(fbp->state[dir-1] = state); 456246149Ssjg} 457276305Sngie 458246149Ssjgvoid fb64_printsub(unsigned char *data, int cnt, 459246149Ssjg unsigned char *buf, int buflen, char *type) 460246149Ssjg{ 461292068Ssjg char lbuf[32]; 462292068Ssjg int i; 463292068Ssjg char *cp; 464292068Ssjg 465292068Ssjg buf[buflen-1] = '\0'; /* make sure it's NULL terminated */ 466292068Ssjg buflen -= 1; 467246149Ssjg 468292068Ssjg switch(data[2]) { 469292068Ssjg case FB64_IV: 470292068Ssjg snprintf(lbuf, sizeof(lbuf), "%s_IV", type); 471292068Ssjg cp = lbuf; 472292068Ssjg goto common; 473292068Ssjg 474292068Ssjg case FB64_IV_OK: 475292068Ssjg snprintf(lbuf, sizeof(lbuf), "%s_IV_OK", type); 476292068Ssjg cp = lbuf; 477292068Ssjg goto common; 478292068Ssjg 479292068Ssjg case FB64_IV_BAD: 480292068Ssjg snprintf(lbuf, sizeof(lbuf), "%s_IV_BAD", type); 481292068Ssjg cp = lbuf; 482292068Ssjg goto common; 483292068Ssjg 484246149Ssjg default: 485292068Ssjg snprintf(lbuf, sizeof(lbuf), " %d (unknown)", data[2]); 486292068Ssjg cp = lbuf; 487292068Ssjg common: 488292068Ssjg for (; (buflen > 0) && (*buf = *cp++); buf++) 489292068Ssjg buflen--; 490292068Ssjg for (i = 3; i < cnt; i++) { 491292068Ssjg snprintf(lbuf, sizeof(lbuf), " %d", data[i]); 492292068Ssjg for (cp = lbuf; (buflen > 0) && (*buf = *cp++); buf++) 493292068Ssjg buflen--; 494292068Ssjg } 495292068Ssjg break; 496292068Ssjg } 497292068Ssjg} 498292068Ssjg 499292068Ssjgvoid cfb64_printsub(unsigned char *data, int cnt, 500292068Ssjg unsigned char *buf, int buflen) 501292068Ssjg{ 502292068Ssjg fb64_printsub(data, cnt, buf, buflen, "CFB64"); 503292068Ssjg} 504292068Ssjg 505292068Ssjgvoid ofb64_printsub(unsigned char *data, int cnt, 506292068Ssjg unsigned char *buf, int buflen) 507292068Ssjg{ 508292068Ssjg fb64_printsub(data, cnt, buf, buflen, "OFB64"); 509292068Ssjg} 510292068Ssjg 511292068Ssjgvoid fb64_stream_iv(des_cblock seed, struct stinfo *stp) 512292068Ssjg{ 513292068Ssjg 514292068Ssjg memcpy(stp->str_iv, seed,sizeof(des_cblock)); 515292068Ssjg memcpy(stp->str_output, seed, sizeof(des_cblock)); 516292068Ssjg 517292068Ssjg des_key_sched(&stp->str_ikey, stp->str_sched); 518246149Ssjg 519292068Ssjg stp->str_index = sizeof(des_cblock); 520292068Ssjg} 521292068Ssjg 522292068Ssjgvoid fb64_stream_key(des_cblock key, struct stinfo *stp) 523292068Ssjg{ 524292068Ssjg memcpy(stp->str_ikey, key, sizeof(des_cblock)); 525292068Ssjg des_key_sched((des_cblock*)key, stp->str_sched); 526292068Ssjg 527292068Ssjg memcpy(stp->str_output, stp->str_iv, sizeof(des_cblock)); 528292068Ssjg 529292068Ssjg stp->str_index = sizeof(des_cblock); 530292068Ssjg} 531292068Ssjg 532292068Ssjg/* 533292068Ssjg * DES 64 bit Cipher Feedback 534292068Ssjg * 535292068Ssjg * key --->+-----+ 536292068Ssjg * +->| DES |--+ 537292068Ssjg * | +-----+ | 538292068Ssjg * | v 539246149Ssjg * INPUT --(--------->(+)+---> DATA 540292068Ssjg * | | 541246149Ssjg * +-------------+ 542292068Ssjg * 543292068Ssjg * 544292068Ssjg * Given: 545292068Ssjg * iV: Initial vector, 64 bits (8 bytes) long. 546292068Ssjg * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt). 547292068Ssjg * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output. 548292068Ssjg * 549292068Ssjg * V0 = DES(iV, key) 550292068Ssjg * On = Dn ^ Vn 551246149Ssjg * V(n+1) = DES(On, key) 552246149Ssjg */ 553246149Ssjg 554246149Ssjgvoid cfb64_encrypt(unsigned char *s, int c) 555246149Ssjg{ 556246149Ssjg struct stinfo *stp = &fb[CFB].streams[DIR_ENCRYPT-1]; 557292068Ssjg int index; 558246149Ssjg 559246149Ssjg index = stp->str_index; 560246149Ssjg while (c-- > 0) { 561246149Ssjg if (index == sizeof(des_cblock)) { 562246149Ssjg des_cblock b; 563292068Ssjg des_ecb_encrypt(&stp->str_output, &b,stp->str_sched, 1); 564246149Ssjg memcpy(stp->str_feed, b, sizeof(des_cblock)); 565246149Ssjg index = 0; 566246149Ssjg } 567246149Ssjg 568292068Ssjg /* On encryption, we store (feed ^ data) which is cypher */ 569246149Ssjg *s = stp->str_output[index] = (stp->str_feed[index] ^ *s); 570246149Ssjg s++; 571246149Ssjg index++; 572250837Ssjg } 573250837Ssjg stp->str_index = index; 574246149Ssjg} 575246149Ssjg 576246149Ssjgint cfb64_decrypt(int data) 577246149Ssjg{ 578292068Ssjg struct stinfo *stp = &fb[CFB].streams[DIR_DECRYPT-1]; 579246149Ssjg int index; 580246149Ssjg 581246149Ssjg if (data == -1) { 582246149Ssjg /* 583246149Ssjg * Back up one byte. It is assumed that we will 584246149Ssjg * never back up more than one byte. If we do, this 585246149Ssjg * may or may not work. 586246149Ssjg */ 587246149Ssjg if (stp->str_index) 588246149Ssjg --stp->str_index; 589246149Ssjg return(0); 590246149Ssjg } 591246149Ssjg 592246149Ssjg index = stp->str_index++; 593246149Ssjg if (index == sizeof(des_cblock)) { 594246149Ssjg des_cblock b; 595246149Ssjg des_ecb_encrypt(&stp->str_output,&b, stp->str_sched, 1); 596292068Ssjg memcpy(stp->str_feed, b, sizeof(des_cblock)); 597246149Ssjg stp->str_index = 1; /* Next time will be 1 */ 598246149Ssjg index = 0; /* But now use 0 */ 599246149Ssjg } 600246149Ssjg 601246149Ssjg /* On decryption we store (data) which is cypher. */ 602246149Ssjg stp->str_output[index] = data; 603250837Ssjg return(data ^ stp->str_feed[index]); 604250837Ssjg} 605250837Ssjg 606246149Ssjg/* 607246149Ssjg * DES 64 bit Output Feedback 608246149Ssjg * 609246149Ssjg * key --->+-----+ 610246149Ssjg * +->| DES |--+ 611246149Ssjg * | +-----+ | 612246149Ssjg * +-----------+ 613246149Ssjg * v 614246149Ssjg * INPUT -------->(+) ----> DATA 615246149Ssjg * 616246149Ssjg * Given: 617246149Ssjg * iV: Initial vector, 64 bits (8 bytes) long. 618292068Ssjg * Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt). 619246149Ssjg * On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output. 620250837Ssjg * 621250837Ssjg * V0 = DES(iV, key) 622250837Ssjg * V(n+1) = DES(Vn, key) 623246149Ssjg * On = Dn ^ Vn 624246149Ssjg */ 625246149Ssjg 626246149Ssjgvoid ofb64_encrypt(unsigned char *s, int c) 627246149Ssjg{ 628246149Ssjg struct stinfo *stp = &fb[OFB].streams[DIR_ENCRYPT-1]; 629246149Ssjg int index; 630246149Ssjg 631246149Ssjg index = stp->str_index; 632246149Ssjg while (c-- > 0) { 633246149Ssjg if (index == sizeof(des_cblock)) { 634246149Ssjg des_cblock b; 635246149Ssjg des_ecb_encrypt(&stp->str_feed,&b, stp->str_sched, 1); 636246149Ssjg memcpy(stp->str_feed, b, sizeof(des_cblock)); 637246149Ssjg index = 0; 638246149Ssjg } 639246149Ssjg *s++ ^= stp->str_feed[index]; 640246149Ssjg index++; 641246149Ssjg } 642250837Ssjg stp->str_index = index; 643250837Ssjg} 644292068Ssjg 645292068Ssjgint ofb64_decrypt(int data) 646292068Ssjg{ 647246149Ssjg struct stinfo *stp = &fb[OFB].streams[DIR_DECRYPT-1]; 648246149Ssjg int index; 649246149Ssjg 650246149Ssjg if (data == -1) { 651246149Ssjg /* 652246149Ssjg * Back up one byte. It is assumed that we will 653246149Ssjg * never back up more than one byte. If we do, this 654246149Ssjg * may or may not work. 655246149Ssjg */ 656246149Ssjg if (stp->str_index) 657246149Ssjg --stp->str_index; 658246149Ssjg return(0); 659246149Ssjg } 660246149Ssjg 661246149Ssjg index = stp->str_index++; 662246149Ssjg if (index == sizeof(des_cblock)) { 663246149Ssjg des_cblock b; 664246149Ssjg des_ecb_encrypt(&stp->str_feed,&b,stp->str_sched, 1); 665246149Ssjg memcpy(stp->str_feed, b, sizeof(des_cblock)); 666246149Ssjg stp->str_index = 1; /* Next time will be 1 */ 667246149Ssjg index = 0; /* But now use 0 */ 668246149Ssjg } 669246149Ssjg 670246149Ssjg return(data ^ stp->str_feed[index]); 671246149Ssjg} 672246149Ssjg#endif 673246149Ssjg 674246149Ssjg