1 #include <stdio.h> 2 3 4 class TwoFish 5 { 6 7 // Constants 8 //........................................................................... 9 10 enum { 11 BLOCK_SIZE = 16, // bytes in a data-block 12 ROUNDS = 16, 13 TOTAL_SUBKEYS = 4 + 4 + 2*ROUNDS, 14 SK_BUMP = 0x01010101, 15 SK_ROTL = 9, 16 P_00 = 1, 17 P_01 = 0, 18 P_02 = 0, 19 P_03 = P_01 ^ 1, 20 P_04 = 1, 21 P_10 = 0, 22 P_11 = 0, 23 P_12 = 1, 24 P_13 = P_11 ^ 1, 25 P_14 = 0, 26 P_20 = 1, 27 P_21 = 1, 28 P_22 = 0, 29 P_23 = P_21 ^ 1, 30 P_24 = 0, 31 P_30 = 0, 32 P_31 = 1, 33 P_32 = 1, 34 P_33 = P_31 ^ 1, 35 P_34 = 1, 36 GF256_FDBK = 0x169, 37 GF256_FDBK_2 = 0x169 / 2, 38 GF256_FDBK_4 = 0x169 / 4, 39 RS_GF_FDBK = 0x14D, // field generator 40 MDS_GF_FDBK = 0x169 /* primitive polynomial for GF(256)*/ 41 }; 42 43 44 // Static code - to intialise the MDS matrix 45 //........................................................................... 46 private: 47 void precomputeMDSmatrix(); 48 49 50 // Instance variables 51 //........................................................................... 52 53 /** Encrypt (false) or decrypt mode (true) */ 54 bool decrypt; 55 bool outputIsFile; 56 bool outputIsBuffer; 57 bool outputIsSocket; 58 59 60 /** Key dependent S-box */ 61 int sBox[4 * 256]; 62 63 64 /** Subkeys */ 65 int subKeys[TOTAL_SUBKEYS]; 66 67 // output areas 68 FILE* fpout; 69 unsigned char* outputBuffer; 70 int sockfd; 71 72 73 public: 74 // Constructor 75 //........................................................................... 76 77 TwoFish( char* userkey, bool _decrypt, FILE* fpout, unsigned char* outbuf ); setDecrypt(bool d)78 void setDecrypt( bool d ) { decrypt = d; } setFp(FILE * fp)79 void setFp( FILE* fp ) { fpout = fp; if ( fp != NULL ) outputIsFile = true; else outputIsFile = false; } setOutputBuffer(unsigned char * obuf)80 void setOutputBuffer( unsigned char* obuf ) { outputBuffer = obuf; if ( outputBuffer != NULL ) outputIsBuffer = true; else outputIsBuffer = false; } setSocket(int sfd)81 void setSocket( int sfd ) { sockfd = sfd; if ( sfd != -1 ) outputIsSocket = true; else outputIsSocket = false; } 82 void blockCrypt( char* in, char* out, int size ); 83 void blockCrypt16( char* in, char* out ); 84 void flush(); resetCBC()85 void resetCBC() { qBlockDefined = false; } 86 87 private: 88 void flushOutput( char* output, int size ); 89 void qBlockPush( char* p, char* c ); 90 void qBlockPop( char* p, char* c ); 91 void qBlockFlush(); 92 93 char qBlockPlain[16]; 94 char qBlockCrypt[16]; 95 char prevCipher[16]; 96 bool qBlockDefined; 97 // Private methods 98 //........................................................................... 99 100 void makeSubKeys( char* k ); 101 102 103 // own methods 104 //........................................................................... 105 106 int RS_MDS_Encode( int k0, int k1 ); 107 int F32( int k64Cnt, int x, int* k32 ); 108 int Fe32( int* sBox, int x, int R ); 109 int Fe320( int* sBox, int x ); 110 int Fe323( int* sBox, int x ); 111 }; 112 char* generateKey( char* kstr ); 113 114 115 class AsciiTwofish { 116 public: 117 AsciiTwofish( TwoFish* engine ); 118 void encryptAscii( char* in, char* out, int outBufferSize ); 119 void decryptAscii( char* in, char* out ); 120 private: 121 TwoFish* engine; 122 }; 123