1 /*
2  *
3  *
4  * Copyright (C) 1997-2000 The Cryptix Foundation Limited.
5  * Copyright (C) 2000 Farm9.
6  * Copyright (C) 2001 Frank Knobbe.
7  * All rights reserved.
8  *
9  * For Cryptix code:
10  * Use, modification, copying and distribution of this software is subject
11  * the terms and conditions of the Cryptix General Licence. You should have
12  * received a copy of the Cryptix General Licence along with this library;
13  * if not, you can download a copy from http://www.cryptix.org/ .
14  *
15  * For Farm9:
16  * ---  jojo@farm9.com, August 2000, converted from Java to C++, added CBC mode and
17  *      ciphertext stealing technique, added AsciiTwofish class for easy encryption
18  *      decryption of text strings
19  *
20  * Frank Knobbe <frank@knobbe.us>:
21  * ---  April 2001, converted from C++ to C, prefixed global variables
22  *      with TwoFish, substituted some defines, changed functions to make use of
23  *      variables supplied in a struct, modified and added routines for modular calls.
24  *      Cleaned up the code so that defines are used instead of fixed 16's and 32's.
25  *      Created two general purpose crypt routines for one block and multiple block
26  *      encryption using Joh's CBC code.
27  *      Added crypt routines that use a header (with a magic and data length).
28  *      (Basically a major rewrite).
29  *
30  *      Note: Routines labeled _TwoFish are private and should not be used
31  *      (or with extreme caution).
32  *
33  */
34 
35 #ifndef __TWOFISH_LIBRARY_HEADER__
36 #define __TWOFISH_LIBRARY_HEADER__
37 
38 #include <stdint.h>
39 
40 #ifndef FALSE
41 #define FALSE   0
42 #endif
43 #ifndef TRUE
44 #define TRUE    !FALSE
45 #endif
46 #ifndef bool
47 #define bool    int
48 #endif
49 
50 
51 /* Constants */
52 
53 #define TwoFish_DEFAULT_PW      "SnortHas2FishEncryptionRoutines!" /* default password (not more than 32 chars) */
54 #define TwoFish_MAGIC           "TwoFish"           /* to indentify a successful decryption */
55 
56 enum
57 {   TwoFish_KEY_SIZE = 256,                 /* Valid values: 64, 128, 192, 256 */
58                                             /* User 256, other key sizes have not been tested. */
59                                             /* (But should work. I substituted as much as */
60                                             /* I could with this define.) */
61     TwoFish_ROUNDS = 16,
62     TwoFish_BLOCK_SIZE = 16,                /* bytes in a data-block */
63     TwoFish_KEY_LENGTH = TwoFish_KEY_SIZE/8,    /* 32= 256-bit key */
64     TwoFish_TOTAL_SUBKEYS = 4+4+2*TwoFish_ROUNDS,
65     TwoFish_MAGIC_LEN = TwoFish_BLOCK_SIZE-8,
66     TwoFish_SK_BUMP = 0x01010101,
67     TwoFish_SK_ROTL = 9,
68     TwoFish_P_00 = 1,
69     TwoFish_P_01 = 0,
70     TwoFish_P_02 = 0,
71     TwoFish_P_03 = TwoFish_P_01 ^ 1,
72     TwoFish_P_04 = 1,
73     TwoFish_P_10 = 0,
74     TwoFish_P_11 = 0,
75     TwoFish_P_12 = 1,
76     TwoFish_P_13 = TwoFish_P_11 ^ 1,
77     TwoFish_P_14 = 0,
78     TwoFish_P_20 = 1,
79     TwoFish_P_21 = 1,
80     TwoFish_P_22 = 0,
81     TwoFish_P_23 = TwoFish_P_21 ^ 1,
82     TwoFish_P_24 = 0,
83     TwoFish_P_30 = 0,
84     TwoFish_P_31 = 1,
85     TwoFish_P_32 = 1,
86     TwoFish_P_33 = TwoFish_P_31 ^ 1,
87     TwoFish_P_34 = 1,
88     TwoFish_GF256_FDBK =   0x169,
89     TwoFish_GF256_FDBK_2 = 0x169 / 2,
90     TwoFish_GF256_FDBK_4 = 0x169 / 4,
91     TwoFish_RS_GF_FDBK = 0x14D,     /* field generator */
92     TwoFish_MDS_GF_FDBK = 0x169     /* primitive polynomial for GF(256) */
93 };
94 
95 
96 /* Global data structure for callers */
97 
98 typedef struct
99 {   uint32_t sBox[4 * 256];                 /* Key dependent S-box */
100     uint32_t subKeys[TwoFish_TOTAL_SUBKEYS];    /* Subkeys  */
101     uint8_t key[TwoFish_KEY_LENGTH];            /* Encryption Key */
102     uint8_t *output;                            /* Pointer to output buffer */
103     uint8_t qBlockPlain[TwoFish_BLOCK_SIZE];    /* Used by CBC */
104     uint8_t qBlockCrypt[TwoFish_BLOCK_SIZE];
105     uint8_t prevCipher[TwoFish_BLOCK_SIZE];
106     struct              /* Header for crypt functions. Has to be at least one block long. */
107     {   uint32_t salt;                          /* Random salt in first block (will salt the rest through CBC) */
108         uint8_t length[4];                  /* The amount of data following the header */
109         uint8_t magic[TwoFish_MAGIC_LEN];       /* Magic to identify successful decryption  */
110     }   header;
111     bool qBlockDefined;
112     bool dontflush;
113 }   TWOFISH;
114 
115 #ifndef __TWOFISH_LIBRARY_SOURCE__
116 
117 extern bool TwoFish_srand;                  /* if set to TRUE (default), first call of TwoFishInit will seed rand();  */
118                                             /* call of TwoFishInit */
119 #endif
120 
121 
122 /**** Public Functions ****/
123 
124 /*  TwoFish Initialization
125  *
126  *  This routine generates a global data structure for use with TwoFish,
127  *  initializes important values (such as subkeys, sBoxes), generates subkeys
128  *  and precomputes the MDS matrix if not already done.
129  *
130  *  Input:  User supplied password (will be appended by default password of 'SnortHas2FishEncryptionRoutines!')
131  *
132  *  Output: Pointer to TWOFISH structure. This data structure contains key dependent data.
133  *          This pointer is used with all other crypt functions.
134  */
135 TWOFISH *TwoFishInit(char *userkey);
136 
137 
138 /*  TwoFish Destroy
139  *
140  *  Nothing else but a free...
141  *
142  *  Input:  Pointer to the TwoFish structure.
143  *
144  */
145 void TwoFishDestroy(TWOFISH *tfdata);
146 
147 
148 /*  TwoFish Alloc
149  *
150  *  Allocates enough memory for the output buffer as required.
151  *
152  *  Input:  Length of the plaintext.
153  *          Boolean flag for BinHex Output.
154  *          Pointer to the TwoFish structure.
155  *
156  *  Output: Returns a pointer to the memory allocated.
157  */
158 void *TwoFishAlloc(unsigned long len,bool binhex,bool decrypt,TWOFISH *tfdata);
159 
160 
161 /*  TwoFish Free
162  *
163  *  Free's the allocated buffer.
164  *
165  *  Input:  Pointer to the TwoFish structure
166  *
167  *  Output: (none)
168  */
169 void TwoFishFree(TWOFISH *tfdata);
170 
171 
172 /*  TwoFish Set Output
173  *
174  *  If you want to allocate the output buffer yourself,
175  *  then you can set it with this function.
176  *
177  *  Input:  Pointer to your output buffer
178  *          Pointer to the TwoFish structure
179  *
180  *  Output: (none)
181  */
182 void TwoFishSetOutput(char *outp,TWOFISH *tfdata);
183 
184 
185 /*  TwoFish Raw Encryption
186  *
187  *  Does not use header, but does use CBC (if more than one block has to be encrypted).
188  *
189  *  Input:  Pointer to the buffer of the plaintext to be encrypted.
190  *          Pointer to the buffer receiving the ciphertext.
191  *          The length of the plaintext buffer.
192  *          The TwoFish structure.
193  *
194  *  Output: The amount of bytes encrypted if successful, otherwise 0.
195  */
196 unsigned long TwoFishEncryptRaw(char *in,char *out,unsigned long len,TWOFISH *tfdata);
197 
198 /*  TwoFish Raw Decryption
199  *
200  *  Does not use header, but does use CBC (if more than one block has to be decrypted).
201  *
202  *  Input:  Pointer to the buffer of the ciphertext to be decrypted.
203  *          Pointer to the buffer receiving the plaintext.
204  *          The length of the ciphertext buffer (at least one cipher block).
205  *          The TwoFish structure.
206  *
207  *  Output: The amount of bytes decrypted if successful, otherwise 0.
208  */
209 unsigned long TwoFishDecryptRaw(char *in,char *out,unsigned long len,TWOFISH *tfdata);
210 
211 
212 /*  TwoFish Encryption
213  *
214  *  Uses header and CBC. If the output area has not been intialized with TwoFishAlloc,
215  *  this routine will alloc the memory. In addition, it will include a small 'header'
216  *  containing the magic and some salt. That way the decrypt routine can check if the
217  *  packet got decrypted successfully, and return 0 instead of garbage.
218  *
219  *  Input:  Pointer to the buffer of the plaintext to be encrypted.
220  *          Pointer to the pointer to the buffer receiving the ciphertext.
221  *              The pointer either points to user allocated output buffer space, or to NULL, in which case
222  *              this routine will set the pointer to the buffer allocated through the struct.
223  *          The length of the plaintext buffer.
224  *              Can be -1 if the input is a null terminated string, in which case we'll count for you.
225  *          Boolean flag for BinHex Output (if used, output will be twice as large as input).
226  *              Note: BinHex conversion overwrites (converts) input buffer!
227  *          The TwoFish structure.
228  *
229  *  Output: The amount of bytes encrypted if successful, otherwise 0.
230  */
231 unsigned long TwoFishEncrypt(char *in,char **out,signed long len,bool binhex,TWOFISH *tfdata);
232 
233 
234 /*  TwoFish Decryption
235  *
236  *  Uses header and CBC. If the output area has not been intialized with TwoFishAlloc,
237  *  this routine will alloc the memory. In addition, it will check the small 'header'
238  *  containing the magic. If magic does not match we return 0. Otherwise we return the
239  *  amount of bytes decrypted (should be the same as the length in the header).
240  *
241  *  Input:  Pointer to the buffer of the ciphertext to be decrypted.
242  *          Pointer to the pointer to the buffer receiving the plaintext.
243  *              The pointer either points to user allocated output buffer space, or to NULL, in which case
244  *              this routine will set the pointer to the buffer allocated through the struct.
245  *          The length of the ciphertext buffer.
246  *              Can be -1 if the input is a null terminated binhex string, in which case we'll count for you.
247  *          Boolean flag for BinHex Input (if used, plaintext will be half as large as input).
248  *              Note: BinHex conversion overwrites (converts) input buffer!
249  *          The TwoFish structure.
250  *
251  *  Output: The amount of bytes decrypted if successful, otherwise 0.
252  */
253 unsigned long TwoFishDecrypt(char *in,char **out,signed long len,bool binhex,TWOFISH *tfdata);
254 
255 
256 /**** Private Functions ****/
257 
258 uint8_t TwoFish__b(uint32_t x,int n);
259 void _TwoFish_BinHex(uint8_t *buf,unsigned long len,bool bintohex);
260 unsigned long _TwoFish_CryptRawCBC(char *in,char *out,unsigned long len,bool decrypt,TWOFISH *tfdata);
261 unsigned long _TwoFish_CryptRaw16(char *in,char *out,unsigned long len,bool decrypt,TWOFISH *tfdata);
262 unsigned long _TwoFish_CryptRaw(char *in,char *out,unsigned long len,bool decrypt,TWOFISH *tfdata);
263 void _TwoFish_PrecomputeMDSmatrix(void);
264 void _TwoFish_MakeSubKeys(TWOFISH *tfdata);
265 void _TwoFish_qBlockPush(uint8_t *p,uint8_t *c,TWOFISH *tfdata);
266 void _TwoFish_qBlockPop(uint8_t *p,uint8_t *c,TWOFISH *tfdata);
267 void _TwoFish_ResetCBC(TWOFISH *tfdata);
268 void _TwoFish_FlushOutput(uint8_t *b,unsigned long len,TWOFISH *tfdata);
269 void _TwoFish_BlockCrypt(uint8_t *in,uint8_t *out,unsigned long size,int decrypt,TWOFISH *tfdata);
270 void _TwoFish_BlockCrypt16(uint8_t *in,uint8_t *out,bool decrypt,TWOFISH *tfdata);
271 uint32_t _TwoFish_RS_MDS_Encode(uint32_t k0,uint32_t k1);
272 uint32_t _TwoFish_F32(uint32_t k64Cnt,uint32_t x,uint32_t *k32);
273 uint32_t _TwoFish_Fe320(uint32_t *lsBox,uint32_t x);
274 uint32_t _TwoFish_Fe323(uint32_t *lsBox,uint32_t x);
275 uint32_t _TwoFish_Fe32(uint32_t *lsBox,uint32_t x,uint32_t R);
276 
277 
278 #endif
279