00001
00027
00028
00029
00030
00031 #include <stdio.h>
00032 #include "thig_platform.h"
00034
00035
00036
00037
00038 #define DIR_ENCRYPT 0
00039 #define DIR_DECRYPT 1
00040 #define MODE_ECB 1
00041 #define MODE_CBC 2
00042 #define MODE_CFB1 3
00044 #define TRUE 1
00045 #define FALSE 0
00046
00047 #define BAD_KEY_DIR -1
00048 #define BAD_KEY_MAT -2
00049 #define BAD_KEY_INSTANCE -3
00050 #define BAD_CIPHER_MODE -4
00051 #define BAD_CIPHER_STATE -5
00053
00054
00055 #define MAX_KEY_SIZE 64
00056 #define MAX_IV_SIZE 16
00057 #define BAD_INPUT_LEN -6
00058 #define BAD_PARAMS -7
00059 #define BAD_IV_MAT -8
00060 #define BAD_ENDIAN -9
00061 #define BAD_ALIGN32 -10
00063 #define BLOCK_SIZE 128
00064 #define MAX_ROUNDS 16
00065 #define ROUNDS_128 16
00066 #define ROUNDS_192 16
00067 #define ROUNDS_256 16
00068 #define MAX_KEY_BITS 256
00069 #define MIN_KEY_BITS 128
00070 #define VALID_SIG 0x48534946
00071 #define MCT_OUTER 400
00072 #define MCT_INNER 10000
00073 #define REENTRANT 1
00075 #define INPUT_WHITEN 0
00076 #define OUTPUT_WHITEN ( INPUT_WHITEN + BLOCK_SIZE/32)
00077 #define ROUND_SUBKEYS (OUTPUT_WHITEN + BLOCK_SIZE/32)
00078 #define TOTAL_SUBKEYS (ROUND_SUBKEYS + 2*MAX_ROUNDS)
00079
00080
00081
00082
00083
00084
00085 typedef unsigned char BYTE;
00086 typedef unsigned long DWORD;
00087 typedef DWORD fullSbox[4][256];
00088
00090 typedef struct
00091 {
00092 BYTE direction;
00093 #if ALIGN32
00094 BYTE dummyAlign[3];
00095 #endif
00096 int keyLen;
00097 char keyMaterial[MAX_KEY_SIZE+4];
00100 DWORD keySig;
00101 int numRounds;
00102 DWORD key32[MAX_KEY_BITS/32];
00103 DWORD sboxKeys[MAX_KEY_BITS/64];
00104 DWORD subKeys[TOTAL_SUBKEYS];
00105 #if REENTRANT
00106 fullSbox sBox8x32;
00107 #if defined(COMPILE_KEY) && defined(USE_ASM)
00108 #undef VALID_SIG
00109 #define VALID_SIG 0x504D4F43
00110 DWORD cSig1;
00111 void *encryptFuncPtr;
00112 void *decryptFuncPtr;
00113 DWORD codeSize;
00114 DWORD cSig2;
00115 BYTE compiledCode[5000];
00116 #endif
00117 #endif
00118 } keyInstance;
00119
00120
00121
00123 typedef struct {
00124 BYTE mode;
00125 #if ALIGN32
00126 BYTE dummyAlign[3];
00127 #endif
00128 BYTE IV[MAX_IV_SIZE];
00131 DWORD cipherSig;
00132 DWORD iv32[BLOCK_SIZE/32];
00133 } cipherInstance;
00134
00136 int makeKey(keyInstance *key, BYTE direction, int keyLen, char *keyMaterial);
00137
00138 int cipherInit(cipherInstance *cipher, BYTE mode, char *IV);
00139
00140 int blockEncrypt(cipherInstance *cipher, keyInstance *key, BYTE *input,
00141 int inputLen, BYTE *outBuffer);
00142
00143 int blockDecrypt(cipherInstance *cipher, keyInstance *key, BYTE *input,
00144 int inputLen, BYTE *outBuffer);
00145
00146 int reKey(keyInstance *key);
00149 #define TAB_DISABLE 0
00150 #define TAB_ENABLE 1
00151 #define TAB_RESET 2
00152 #define TAB_QUERY 3
00153 #define TAB_MIN_QUERY 50
00154 int TableOp(int op);
00155
00156
00157 #define CONST
00159 #if BLOCK_SIZE == 128
00160 #define Copy1(d,s,N) ((DWORD *)(d))[N] = ((DWORD *)(s))[N]
00161 #define BlockCopy(d,s) { Copy1(d,s,0);Copy1(d,s,1);Copy1(d,s,2);Copy1(d,s,3); }
00162 #else
00163 #define BlockCopy(d,s) { memcpy(d,s,BLOCK_SIZE/8); }
00164 #endif
00165
00166
00167 #ifdef TEST_2FISH
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 #include <stdio.h>
00203 #include <stdlib.h>
00204 #include <time.h>
00205 #include <string.h>
00206
00207 #define MAX_BLK_CNT 1
00215 int TestTwofish(int mode,int keySize)
00216 {
00217 keyInstance ki;
00218 cipherInstance ci;
00219 BYTE plainText[MAX_BLK_CNT*(BLOCK_SIZE/8)];
00220 BYTE cipherText[MAX_BLK_CNT*(BLOCK_SIZE/8)];
00221 BYTE decryptOut[MAX_BLK_CNT*(BLOCK_SIZE/8)];
00222 BYTE iv[BLOCK_SIZE/8];
00223 int i,byteCnt;
00224
00225 if (makeKey(&ki,DIR_ENCRYPT,keySize,NULL) != TRUE)
00226 return 1;
00227 if (cipherInit(&ci,mode,NULL) != TRUE)
00228 return 1;
00229
00230 for (i=0;i<keySize/32;i++)
00231 ki.key32[i]=0x10003 * rand();
00232 reKey(&ki);
00233
00234 if (mode != MODE_ECB)
00235 {
00236 for (i=0;i<sizeof(iv);i++)
00237 iv[i]=(BYTE) rand();
00238 memcpy(ci.iv32,iv,sizeof(ci.iv32));
00239 }
00240
00241
00242
00243
00244 byteCnt = (BLOCK_SIZE/8) * (1 + (rand() % MAX_BLK_CNT));
00245
00246 for (i=0;i<byteCnt;i++)
00247 plainText[i]=(BYTE) rand();
00248
00249
00250 if (blockEncrypt(&ci,&ki, plainText,byteCnt*8,cipherText) != byteCnt*8)
00251 return 1;
00252
00253
00254 if (mode != MODE_ECB)
00255 memcpy(ci.iv32,iv,sizeof(ci.iv32));
00256
00257 if (blockDecrypt(&ci,&ki,cipherText,byteCnt*8,decryptOut) != byteCnt*8)
00258 return 1;
00259
00260
00261 if (memcmp(plainText,decryptOut,byteCnt))
00262 return 1;
00263
00264 return 0;
00265 }
00266
00270 void main(void)
00271 {
00272 int testCnt,keySize;
00273
00274 srand((unsigned) time(NULL));
00275
00276 for (keySize=128;keySize<=256;keySize+=64)
00277 for (testCnt=0;testCnt<10;testCnt++)
00278 {
00279 if (TestTwofish(MODE_ECB,keySize))
00280 { printf("ECB Failure at keySize=%d",keySize); return; }
00281 if (TestTwofish(MODE_CBC,keySize))
00282 { printf("CBC Failure at keySize=%d",keySize); return; }
00283 }
00284 printf("Tests passed");
00285 }
00286 #endif