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