Path: blob/main/crypto/heimdal/appl/telnet/libtelnet/enc_des.c
34878 views
/*-1* Copyright (c) 1991, 19932* The Regents of the University of California. All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12* 3. All advertising materials mentioning features or use of this software13* must display the following acknowledgement:14* This product includes software developed by the University of15* California, Berkeley and its contributors.16* 4. Neither the name of the University nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include <config.h>3435RCSID("$Id$");3637#if defined(AUTHENTICATION) && defined(ENCRYPTION) && defined(DES_ENCRYPTION)38#include <arpa/telnet.h>39#include <stdio.h>40#ifdef __STDC__41#include <stdlib.h>42#include <string.h>43#endif44#include <roken.h>45#ifdef SOCKS46#include <socks.h>47#endif4849#include "encrypt.h"50#include "misc-proto.h"5152#include "crypto-headers.h"5354extern int encrypt_debug_mode;5556#define CFB 057#define OFB 15859#define NO_SEND_IV 160#define NO_RECV_IV 261#define NO_KEYID 462#define IN_PROGRESS (NO_SEND_IV|NO_RECV_IV|NO_KEYID)63#define SUCCESS 064#define FAILED -1656667struct stinfo {68DES_cblock str_output;69DES_cblock str_feed;70DES_cblock str_iv;71DES_cblock str_ikey;72DES_key_schedule str_sched;73int str_index;74int str_flagshift;75};7677struct fb {78DES_cblock krbdes_key;79DES_key_schedule krbdes_sched;80DES_cblock temp_feed;81unsigned char fb_feed[64];82int need_start;83int state[2];84int keyid[2];85struct stinfo streams[2];86};8788static struct fb fb[2];8990struct keyidlist {91char *keyid;92int keyidlen;93char *key;94int keylen;95int flags;96} keyidlist [] = {97{ "\0", 1, 0, 0, 0 }, /* default key of zero */98{ 0, 0, 0, 0, 0 }99};100101#define KEYFLAG_MASK 03102103#define KEYFLAG_NOINIT 00104#define KEYFLAG_INIT 01105#define KEYFLAG_OK 02106#define KEYFLAG_BAD 03107108#define KEYFLAG_SHIFT 2109110#define SHIFT_VAL(a,b) (KEYFLAG_SHIFT*((a)+((b)*2)))111112#define FB64_IV 1113#define FB64_IV_OK 2114#define FB64_IV_BAD 3115116117void fb64_stream_iv (DES_cblock, struct stinfo *);118void fb64_init (struct fb *);119static int fb64_start (struct fb *, int, int);120int fb64_is (unsigned char *, int, struct fb *);121int fb64_reply (unsigned char *, int, struct fb *);122static void fb64_session (Session_Key *, int, struct fb *);123void fb64_stream_key (DES_cblock, struct stinfo *);124int fb64_keyid (int, unsigned char *, int *, struct fb *);125void fb64_printsub(unsigned char *, size_t ,126unsigned char *, size_t , char *);127128void cfb64_init(int server)129{130fb64_init(&fb[CFB]);131fb[CFB].fb_feed[4] = ENCTYPE_DES_CFB64;132fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, CFB);133fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, CFB);134}135136137void ofb64_init(int server)138{139fb64_init(&fb[OFB]);140fb[OFB].fb_feed[4] = ENCTYPE_DES_OFB64;141fb[CFB].streams[0].str_flagshift = SHIFT_VAL(0, OFB);142fb[CFB].streams[1].str_flagshift = SHIFT_VAL(1, OFB);143}144145void fb64_init(struct fb *fbp)146{147memset(fbp,0, sizeof(*fbp));148fbp->state[0] = fbp->state[1] = FAILED;149fbp->fb_feed[0] = IAC;150fbp->fb_feed[1] = SB;151fbp->fb_feed[2] = TELOPT_ENCRYPT;152fbp->fb_feed[3] = ENCRYPT_IS;153}154155/*156* Returns:157* -1: some error. Negotiation is done, encryption not ready.158* 0: Successful, initial negotiation all done.159* 1: successful, negotiation not done yet.160* 2: Not yet. Other things (like getting the key from161* Kerberos) have to happen before we can continue.162*/163int cfb64_start(int dir, int server)164{165return(fb64_start(&fb[CFB], dir, server));166}167168int ofb64_start(int dir, int server)169{170return(fb64_start(&fb[OFB], dir, server));171}172173static int fb64_start(struct fb *fbp, int dir, int server)174{175int x;176unsigned char *p;177int state;178179switch (dir) {180case DIR_DECRYPT:181/*182* This is simply a request to have the other side183* start output (our input). He will negotiate an184* IV so we need not look for it.185*/186state = fbp->state[dir-1];187if (state == FAILED)188state = IN_PROGRESS;189break;190191case DIR_ENCRYPT:192state = fbp->state[dir-1];193if (state == FAILED)194state = IN_PROGRESS;195else if ((state & NO_SEND_IV) == 0) {196break;197}198199if (!VALIDKEY(fbp->krbdes_key)) {200fbp->need_start = 1;201break;202}203204state &= ~NO_SEND_IV;205state |= NO_RECV_IV;206if (encrypt_debug_mode)207printf("Creating new feed\r\n");208/*209* Create a random feed and send it over.210*/211do {212if (RAND_bytes(fbp->temp_feed,213sizeof(*fbp->temp_feed)) != 1)214abort();215DES_set_odd_parity(&fbp->temp_feed);216} while(DES_is_weak_key(&fbp->temp_feed));217218p = fbp->fb_feed + 3;219*p++ = ENCRYPT_IS;220p++;221*p++ = FB64_IV;222for (x = 0; x < sizeof(DES_cblock); ++x) {223if ((*p++ = fbp->temp_feed[x]) == IAC)224*p++ = IAC;225}226*p++ = IAC;227*p++ = SE;228printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);229telnet_net_write(fbp->fb_feed, p - fbp->fb_feed);230break;231default:232return(FAILED);233}234return(fbp->state[dir-1] = state);235}236237/*238* Returns:239* -1: some error. Negotiation is done, encryption not ready.240* 0: Successful, initial negotiation all done.241* 1: successful, negotiation not done yet.242*/243244int cfb64_is(unsigned char *data, int cnt)245{246return(fb64_is(data, cnt, &fb[CFB]));247}248249int ofb64_is(unsigned char *data, int cnt)250{251return(fb64_is(data, cnt, &fb[OFB]));252}253254255int fb64_is(unsigned char *data, int cnt, struct fb *fbp)256{257unsigned char *p;258int state = fbp->state[DIR_DECRYPT-1];259260if (cnt-- < 1)261goto failure;262263switch (*data++) {264case FB64_IV:265if (cnt != sizeof(DES_cblock)) {266if (encrypt_debug_mode)267printf("CFB64: initial vector failed on size\r\n");268state = FAILED;269goto failure;270}271272if (encrypt_debug_mode)273printf("CFB64: initial vector received\r\n");274275if (encrypt_debug_mode)276printf("Initializing Decrypt stream\r\n");277278fb64_stream_iv(data, &fbp->streams[DIR_DECRYPT-1]);279280p = fbp->fb_feed + 3;281*p++ = ENCRYPT_REPLY;282p++;283*p++ = FB64_IV_OK;284*p++ = IAC;285*p++ = SE;286printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);287telnet_net_write(fbp->fb_feed, p - fbp->fb_feed);288289state = fbp->state[DIR_DECRYPT-1] = IN_PROGRESS;290break;291292default:293if (encrypt_debug_mode) {294printf("Unknown option type: %d\r\n", *(data-1));295printd(data, cnt);296printf("\r\n");297}298/* FALL THROUGH */299failure:300/*301* We failed. Send an FB64_IV_BAD option302* to the other side so it will know that303* things failed.304*/305p = fbp->fb_feed + 3;306*p++ = ENCRYPT_REPLY;307p++;308*p++ = FB64_IV_BAD;309*p++ = IAC;310*p++ = SE;311printsub('>', &fbp->fb_feed[2], p - &fbp->fb_feed[2]);312telnet_net_write(fbp->fb_feed, p - fbp->fb_feed);313314break;315}316return(fbp->state[DIR_DECRYPT-1] = state);317}318319/*320* Returns:321* -1: some error. Negotiation is done, encryption not ready.322* 0: Successful, initial negotiation all done.323* 1: successful, negotiation not done yet.324*/325326int cfb64_reply(unsigned char *data, int cnt)327{328return(fb64_reply(data, cnt, &fb[CFB]));329}330331int ofb64_reply(unsigned char *data, int cnt)332{333return(fb64_reply(data, cnt, &fb[OFB]));334}335336337int fb64_reply(unsigned char *data, int cnt, struct fb *fbp)338{339int state = fbp->state[DIR_ENCRYPT-1];340341if (cnt-- < 1)342goto failure;343344switch (*data++) {345case FB64_IV_OK:346fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);347if (state == FAILED)348state = IN_PROGRESS;349state &= ~NO_RECV_IV;350encrypt_send_keyid(DIR_ENCRYPT, (unsigned char *)"\0", 1, 1);351break;352353case FB64_IV_BAD:354memset(fbp->temp_feed, 0, sizeof(DES_cblock));355fb64_stream_iv(fbp->temp_feed, &fbp->streams[DIR_ENCRYPT-1]);356state = FAILED;357break;358359default:360if (encrypt_debug_mode) {361printf("Unknown option type: %d\r\n", data[-1]);362printd(data, cnt);363printf("\r\n");364}365/* FALL THROUGH */366failure:367state = FAILED;368break;369}370return(fbp->state[DIR_ENCRYPT-1] = state);371}372373void cfb64_session(Session_Key *key, int server)374{375fb64_session(key, server, &fb[CFB]);376}377378void ofb64_session(Session_Key *key, int server)379{380fb64_session(key, server, &fb[OFB]);381}382383static void fb64_session(Session_Key *key, int server, struct fb *fbp)384{385386if (!key || key->type != SK_DES) {387if (encrypt_debug_mode)388printf("Can't set krbdes's session key (%d != %d)\r\n",389key ? key->type : -1, SK_DES);390return;391}392memcpy(fbp->krbdes_key, key->data, sizeof(DES_cblock));393394fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_ENCRYPT-1]);395fb64_stream_key(fbp->krbdes_key, &fbp->streams[DIR_DECRYPT-1]);396397RAND_seed(key->data, key->length);398399DES_set_key_checked((DES_cblock *)&fbp->krbdes_key,400&fbp->krbdes_sched);401/*402* Now look to see if krbdes_start() was waiting for the key to403* show up. If so, go ahead an call it now that we have the key.404*/405if (fbp->need_start) {406fbp->need_start = 0;407fb64_start(fbp, DIR_ENCRYPT, server);408}409}410411/*412* We only accept a keyid of 0. If we get a keyid of413* 0, then mark the state as SUCCESS.414*/415416int cfb64_keyid(int dir, unsigned char *kp, int *lenp)417{418return(fb64_keyid(dir, kp, lenp, &fb[CFB]));419}420421int ofb64_keyid(int dir, unsigned char *kp, int *lenp)422{423return(fb64_keyid(dir, kp, lenp, &fb[OFB]));424}425426int fb64_keyid(int dir, unsigned char *kp, int *lenp, struct fb *fbp)427{428int state = fbp->state[dir-1];429430if (*lenp != 1 || (*kp != '\0')) {431*lenp = 0;432return(state);433}434435if (state == FAILED)436state = IN_PROGRESS;437438state &= ~NO_KEYID;439440return(fbp->state[dir-1] = state);441}442443void fb64_printsub(unsigned char *data, size_t cnt,444unsigned char *buf, size_t buflen, char *type)445{446char lbuf[32];447int i;448char *cp;449450buf[buflen-1] = '\0'; /* make sure it's NULL terminated */451buflen -= 1;452453switch(data[2]) {454case FB64_IV:455snprintf(lbuf, sizeof(lbuf), "%s_IV", type);456cp = lbuf;457goto common;458459case FB64_IV_OK:460snprintf(lbuf, sizeof(lbuf), "%s_IV_OK", type);461cp = lbuf;462goto common;463464case FB64_IV_BAD:465snprintf(lbuf, sizeof(lbuf), "%s_IV_BAD", type);466cp = lbuf;467goto common;468469default:470snprintf(lbuf, sizeof(lbuf), " %d (unknown)", data[2]);471cp = lbuf;472common:473for (; (buflen > 0) && (*buf = *cp++); buf++)474buflen--;475for (i = 3; i < cnt; i++) {476snprintf(lbuf, sizeof(lbuf), " %d", data[i]);477for (cp = lbuf; (buflen > 0) && (*buf = *cp++); buf++)478buflen--;479}480break;481}482}483484void cfb64_printsub(unsigned char *data, size_t cnt,485unsigned char *buf, size_t buflen)486{487fb64_printsub(data, cnt, buf, buflen, "CFB64");488}489490void ofb64_printsub(unsigned char *data, size_t cnt,491unsigned char *buf, size_t buflen)492{493fb64_printsub(data, cnt, buf, buflen, "OFB64");494}495496void fb64_stream_iv(DES_cblock seed, struct stinfo *stp)497{498499memcpy(stp->str_iv, seed,sizeof(DES_cblock));500memcpy(stp->str_output, seed, sizeof(DES_cblock));501502DES_set_key_checked(&stp->str_ikey, &stp->str_sched);503504stp->str_index = sizeof(DES_cblock);505}506507void fb64_stream_key(DES_cblock key, struct stinfo *stp)508{509memcpy(stp->str_ikey, key, sizeof(DES_cblock));510DES_set_key_checked((DES_cblock*)key, &stp->str_sched);511512memcpy(stp->str_output, stp->str_iv, sizeof(DES_cblock));513514stp->str_index = sizeof(DES_cblock);515}516517/*518* DES 64 bit Cipher Feedback519*520* key --->+-----+521* +->| DES |--+522* | +-----+ |523* | v524* INPUT --(--------->(+)+---> DATA525* | |526* +-------------+527*528*529* Given:530* iV: Initial vector, 64 bits (8 bytes) long.531* Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).532* On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.533*534* V0 = DES(iV, key)535* On = Dn ^ Vn536* V(n+1) = DES(On, key)537*/538539void cfb64_encrypt(unsigned char *s, int c)540{541struct stinfo *stp = &fb[CFB].streams[DIR_ENCRYPT-1];542int index;543544index = stp->str_index;545while (c-- > 0) {546if (index == sizeof(DES_cblock)) {547DES_cblock b;548DES_ecb_encrypt(&stp->str_output, &b,&stp->str_sched, 1);549memcpy(stp->str_feed, b, sizeof(DES_cblock));550index = 0;551}552553/* On encryption, we store (feed ^ data) which is cypher */554*s = stp->str_output[index] = (stp->str_feed[index] ^ *s);555s++;556index++;557}558stp->str_index = index;559}560561int cfb64_decrypt(int data)562{563struct stinfo *stp = &fb[CFB].streams[DIR_DECRYPT-1];564int index;565566if (data == -1) {567/*568* Back up one byte. It is assumed that we will569* never back up more than one byte. If we do, this570* may or may not work.571*/572if (stp->str_index)573--stp->str_index;574return(0);575}576577index = stp->str_index++;578if (index == sizeof(DES_cblock)) {579DES_cblock b;580DES_ecb_encrypt(&stp->str_output,&b, &stp->str_sched, 1);581memcpy(stp->str_feed, b, sizeof(DES_cblock));582stp->str_index = 1; /* Next time will be 1 */583index = 0; /* But now use 0 */584}585586/* On decryption we store (data) which is cypher. */587stp->str_output[index] = data;588return(data ^ stp->str_feed[index]);589}590591/*592* DES 64 bit Output Feedback593*594* key --->+-----+595* +->| DES |--+596* | +-----+ |597* +-----------+598* v599* INPUT -------->(+) ----> DATA600*601* Given:602* iV: Initial vector, 64 bits (8 bytes) long.603* Dn: the nth chunk of 64 bits (8 bytes) of data to encrypt (decrypt).604* On: the nth chunk of 64 bits (8 bytes) of encrypted (decrypted) output.605*606* V0 = DES(iV, key)607* V(n+1) = DES(Vn, key)608* On = Dn ^ Vn609*/610611void ofb64_encrypt(unsigned char *s, int c)612{613struct stinfo *stp = &fb[OFB].streams[DIR_ENCRYPT-1];614int index;615616index = stp->str_index;617while (c-- > 0) {618if (index == sizeof(DES_cblock)) {619DES_cblock b;620DES_ecb_encrypt(&stp->str_feed,&b, &stp->str_sched, 1);621memcpy(stp->str_feed, b, sizeof(DES_cblock));622index = 0;623}624*s++ ^= stp->str_feed[index];625index++;626}627stp->str_index = index;628}629630int ofb64_decrypt(int data)631{632struct stinfo *stp = &fb[OFB].streams[DIR_DECRYPT-1];633int index;634635if (data == -1) {636/*637* Back up one byte. It is assumed that we will638* never back up more than one byte. If we do, this639* may or may not work.640*/641if (stp->str_index)642--stp->str_index;643return(0);644}645646index = stp->str_index++;647if (index == sizeof(DES_cblock)) {648DES_cblock b;649DES_ecb_encrypt(&stp->str_feed,&b,&stp->str_sched, 1);650memcpy(stp->str_feed, b, sizeof(DES_cblock));651stp->str_index = 1; /* Next time will be 1 */652index = 0; /* But now use 0 */653}654655return(data ^ stp->str_feed[index]);656}657#endif658659660661