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
|
/*
* farm9crypt.cc
*
* Copyright (C) 2000-2003 farm9.com, Inc. All Rights Reserved.
*
* C interface between netcat and twofish.
*
* Intended for direct replacement of system "read" and "write" calls.
*
* Design is like a "C" version of an object.
*
* Static variables, initialized with farm9crypt_init creates a
* "readDecryptor" and "writeEncryptor" object, both of which are based
* on the assumption that text lines are being transferred between
* the two sides.
*
* jojo@farm9.com -- 29 Sept 2000, fixed buffer size (really it should have crashed!)
* jojo@farm9.com -- 2 Oct 2000, yet another bug fix...(thanks to Jimmy for reporting this!)
* jojo@farm9.com -- 2 Oct 2000, no more printf of key cuz its stupid (yet another Dragos suggestion)
* jeff@wwti.com -- 9 Feb 2001, added string.h include for yet more linux brokenness
* jsilva@farm9.com -- 2 December 2003, relicensed under GPL.
*/
/***************************************************************************
* *
* This program 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 2 of the License, or *
* (at your option) any later version. *
* *
* This program 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. *
* *
***************************************************************************/
#ifndef WIN32
#include <string.h>
#include <sys/types.h> // suggested by several people -- for OpenBSD, FreeBSD compiles
#include <sys/socket.h> /* basics, SO_ and AF_ defs, sockaddr, ... */
#include <stdlib.h>
#else
#include <fcntl.h>
#include <io.h>
#include <conio.h>
#include <winsock.h>
#include <time.h>
#endif
extern "C"
{
#include "farm9crypt.h"
}
#include "twofish2.h"
static int debug = false;
static int initialized = false;
static TwoFish* decryptor = NULL;
static TwoFish* encryptor = NULL;
extern "C" void farm9crypt_debug() {
debug = true;
}
/*
* farm9crypt_initialized
*
* Return Value:
* true if this module has been initialized, otherwise false
*/
extern "C" int farm9crypt_initialized() {
return( initialized );
}
/*
* farm9crypt_init
*
* Input Parameters:
* keystr -- used to generate Twofish key for encryption and decryption
*
* Return Value:
* none
*/
extern "C" void farm9crypt_init( char* keystr ) {
// printf( "farm9crypt_init: %s\n", keystr );
encryptor = new TwoFish( generateKey( keystr ), false, NULL, NULL );
decryptor = new TwoFish( generateKey( keystr ), true, NULL, NULL );
initialized = true;
srand( 1000 );
}
/*
* farm9crypt_read
*
* Susbstitute for socket read (one line replacement in netcat)
*
* Handles decryption
*
* Parameters same as "recv"
*/
static char outBuffer[8193];
static char inBuffer[8193];
extern "C" int farm9crypt_read( int sockfd, char* buf, int size ) {
int total = 0;
char outbuf[16];
char outbuf2[16];
if ( size > 8192 ) {
size = 8192;
}
while (total < 32) {
int result = recv( sockfd, buf + total, 32 - total, 0 );
if ( result > 0 ) {
total += result;
} else {
return(0);
}
}
decryptor->resetCBC();
decryptor->setOutputBuffer( (unsigned char*)&outBuffer[0] );
decryptor->blockCrypt( buf, outbuf, 16 );
decryptor->flush();
decryptor->setOutputBuffer( (unsigned char*)&outBuffer[0] );
decryptor->blockCrypt( buf + 16, outbuf2, 16 );
int limit = atoi( outbuf );
total = 0;
char* inbuf = &inBuffer[0];
while ( total < limit ) {
int result = recv( sockfd, inbuf + total, limit - total, 0 );
if ( result > 0 ) {
total += result;
} else {
break;
}
}
int loc = 0;
char* obuf = &outBuffer[0];
while ( total > 0 ) {
int amount = 16;
if ( total < amount ) {
amount = total;
}
decryptor->blockCrypt( inbuf + loc, outbuf, amount );
total -= amount;
loc += 16;
}
decryptor->flush();
memcpy( buf, obuf + 32, limit );
*(buf + limit) = 0; // in case
return( limit );
}
static char localBuf[2000];
extern "C" int farm9crypt_write( int sockfd, char* buf, int size ) {
char tempbuf[16];
char outbuf[16];
sprintf( tempbuf, "%d %d", size, rand() );
tempbuf[strlen(tempbuf)] = 'x';
encryptor->setSocket( sockfd );
encryptor->setOutputBuffer( (unsigned char*)&outBuffer[0] );
encryptor->resetCBC();
encryptor->blockCrypt( tempbuf, outbuf, 16 );
encryptor->blockCrypt( tempbuf, outbuf, 16 );
int loc = 0;
int totalsize = size;
while ( size > 0 ) {
int amount = 16;
if ( size < amount ) {
amount = size;
}
encryptor->blockCrypt( buf + loc, &outBuffer[loc+32], amount );
size -= amount;
loc += amount;
}
encryptor->flush();
return( totalsize );
}
|