1/* crypto/des/des_locl.h */
2/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
3 * All rights reserved.
4 *
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
8 *
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to.  The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15 *
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the copyright
27 *    notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 *    notice, this list of conditions and the following disclaimer in the
30 *    documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 *    must display the following acknowledgement:
33 *    "This product includes cryptographic software written by
34 *     Eric Young (eay@cryptsoft.com)"
35 *    The word 'cryptographic' can be left out if the rouines from the library
36 *    being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 *    the apps directory (application code) you must include an acknowledgement:
39 *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40 *
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51 * SUCH DAMAGE.
52 *
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed.  i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
57 */
58
59#ifndef HEADER_DES_LOCL_H
60#define HEADER_DES_LOCL_H
61
62#include <stdio.h>
63#include <stdlib.h>
64#include <unistd.h>
65#include <math.h>
66#include <des.h>
67#include <string.h>
68
69#define ITERATIONS 16
70#define HALF_ITERATIONS 8
71
72/* used in des_read and des_write */
73#define MAXWRITE	(1024*16)
74#define BSIZE		(MAXWRITE+4)
75
76#define c2l(c,l)	(l =((DES_LONG)(*((c)++)))    , \
77			 l|=((DES_LONG)(*((c)++)))<< 8L, \
78			 l|=((DES_LONG)(*((c)++)))<<16L, \
79			 l|=((DES_LONG)(*((c)++)))<<24L)
80
81/* NOTE - c is not incremented as per c2l */
82#define c2ln(c,l1,l2,n)	{ \
83			c+=n; \
84			l1=l2=0; \
85			switch (n) { \
86			case 8: l2 =((DES_LONG)(*(--(c))))<<24L; \
87			case 7: l2|=((DES_LONG)(*(--(c))))<<16L; \
88			case 6: l2|=((DES_LONG)(*(--(c))))<< 8L; \
89			case 5: l2|=((DES_LONG)(*(--(c))));     \
90			case 4: l1 =((DES_LONG)(*(--(c))))<<24L; \
91			case 3: l1|=((DES_LONG)(*(--(c))))<<16L; \
92			case 2: l1|=((DES_LONG)(*(--(c))))<< 8L; \
93			case 1: l1|=((DES_LONG)(*(--(c))));     \
94				} \
95			}
96
97#define l2c(l,c)	(*((c)++)=(unsigned char)(((l)     )&0xff), \
98			 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
99			 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
100			 *((c)++)=(unsigned char)(((l)>>24L)&0xff))
101
102/* replacements for htonl and ntohl since I have no idea what to do
103 * when faced with machines with 8 byte longs. */
104#define HDRSIZE 4
105
106#define n2l(c,l)	(l =((DES_LONG)(*((c)++)))<<24L, \
107			 l|=((DES_LONG)(*((c)++)))<<16L, \
108			 l|=((DES_LONG)(*((c)++)))<< 8L, \
109			 l|=((DES_LONG)(*((c)++))))
110
111#define l2n(l,c)	(*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
112			 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
113			 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
114			 *((c)++)=(unsigned char)(((l)     )&0xff))
115
116/* NOTE - c is not incremented as per l2c */
117#define l2cn(l1,l2,c,n)	{ \
118			c+=n; \
119			switch (n) { \
120			case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
121			case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
122			case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
123			case 5: *(--(c))=(unsigned char)(((l2)     )&0xff); \
124			case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
125			case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
126			case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
127			case 1: *(--(c))=(unsigned char)(((l1)     )&0xff); \
128				} \
129			}
130
131#define	ROTATE(a,n)	(((a)>>(n))+((a)<<(32-(n))))
132
133/* Don't worry about the LOAD_DATA() stuff, that is used by
134 * fcrypt() to add it's little bit to the front */
135
136#ifdef DES_FCRYPT
137
138#define LOAD_DATA_tmp(R,S,u,t,E0,E1) \
139	{ DES_LONG tmp; LOAD_DATA(R,S,u,t,E0,E1,tmp); }
140
141#define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
142	t=R^(R>>16L); \
143	u=t&E0; t&=E1; \
144	tmp=(u<<16); u^=R^s[S  ]; u^=tmp; \
145	tmp=(t<<16); t^=R^s[S+1]; t^=tmp
146#else
147#define LOAD_DATA_tmp(a,b,c,d,e,f) LOAD_DATA(a,b,c,d,e,f,g)
148#define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
149	u=R^s[S  ]; \
150	t=R^s[S+1]
151#endif
152
153/* The changes to this macro may help or hinder, depending on the
154 * compiler and the architecture.  gcc2 always seems to do well :-).
155 * Inspired by Dana How <how@isl.stanford.edu>
156 * DO NOT use the alternative version on machines with 8 byte longs.
157 * It does not seem to work on the Alpha, even when DES_LONG is 4
158 * bytes, probably an issue of accessing non-word aligned objects :-( */
159#ifdef DES_PTR
160
161/* It recently occurred to me that 0^0^0^0^0^0^0 == 0, so there
162 * is no reason to not xor all the sub items together.  This potentially
163 * saves a register since things can be xored directly into L */
164
165#if defined(DES_RISC1) || defined(DES_RISC2)
166#ifdef DES_RISC1
167#define D_ENCRYPT(LL,R,S) { \
168	unsigned int u1,u2,u3; \
169	LOAD_DATA(R,S,u,t,E0,E1,u1); \
170	u2=(int)u>>8L; \
171	u1=(int)u&0xfc; \
172	u2&=0xfc; \
173	t=ROTATE(t,4); \
174	u>>=16L; \
175	LL^= *(const DES_LONG *)(des_SP      +u1); \
176	LL^= *(const DES_LONG *)(des_SP+0x200+u2); \
177	u3=(int)(u>>8L); \
178	u1=(int)u&0xfc; \
179	u3&=0xfc; \
180	LL^= *(const DES_LONG *)(des_SP+0x400+u1); \
181	LL^= *(const DES_LONG *)(des_SP+0x600+u3); \
182	u2=(int)t>>8L; \
183	u1=(int)t&0xfc; \
184	u2&=0xfc; \
185	t>>=16L; \
186	LL^= *(const DES_LONG *)(des_SP+0x100+u1); \
187	LL^= *(const DES_LONG *)(des_SP+0x300+u2); \
188	u3=(int)t>>8L; \
189	u1=(int)t&0xfc; \
190	u3&=0xfc; \
191	LL^= *(const DES_LONG *)(des_SP+0x500+u1); \
192	LL^= *(const DES_LONG *)(des_SP+0x700+u3); }
193#endif
194#ifdef DES_RISC2
195#define D_ENCRYPT(LL,R,S) { \
196	unsigned int u1,u2,s1,s2; \
197	LOAD_DATA(R,S,u,t,E0,E1,u1); \
198	u2=(int)u>>8L; \
199	u1=(int)u&0xfc; \
200	u2&=0xfc; \
201	t=ROTATE(t,4); \
202	LL^= *(const DES_LONG *)(des_SP      +u1); \
203	LL^= *(const DES_LONG *)(des_SP+0x200+u2); \
204	s1=(int)(u>>16L); \
205	s2=(int)(u>>24L); \
206	s1&=0xfc; \
207	s2&=0xfc; \
208	LL^= *(const DES_LONG *)(des_SP+0x400+s1); \
209	LL^= *(const DES_LONG *)(des_SP+0x600+s2); \
210	u2=(int)t>>8L; \
211	u1=(int)t&0xfc; \
212	u2&=0xfc; \
213	LL^= *(const DES_LONG *)(des_SP+0x100+u1); \
214	LL^= *(const DES_LONG *)(des_SP+0x300+u2); \
215	s1=(int)(t>>16L); \
216	s2=(int)(t>>24L); \
217	s1&=0xfc; \
218	s2&=0xfc; \
219	LL^= *(const DES_LONG *)(des_SP+0x500+s1); \
220	LL^= *(const DES_LONG *)(des_SP+0x700+s2); }
221#endif
222#else
223#define D_ENCRYPT(LL,R,S) { \
224	LOAD_DATA_tmp(R,S,u,t,E0,E1); \
225	t=ROTATE(t,4); \
226	LL^= \
227	*(const DES_LONG *)(des_SP      +((u     )&0xfc))^ \
228	*(const DES_LONG *)(des_SP+0x200+((u>> 8L)&0xfc))^ \
229	*(const DES_LONG *)(des_SP+0x400+((u>>16L)&0xfc))^ \
230	*(const DES_LONG *)(des_SP+0x600+((u>>24L)&0xfc))^ \
231	*(const DES_LONG *)(des_SP+0x100+((t     )&0xfc))^ \
232	*(const DES_LONG *)(des_SP+0x300+((t>> 8L)&0xfc))^ \
233	*(const DES_LONG *)(des_SP+0x500+((t>>16L)&0xfc))^ \
234	*(const DES_LONG *)(des_SP+0x700+((t>>24L)&0xfc)); }
235#endif
236
237#else /* original version */
238
239#if defined(DES_RISC1) || defined(DES_RISC2)
240#ifdef DES_RISC1
241#define D_ENCRYPT(LL,R,S) {\
242	unsigned int u1,u2,u3; \
243	LOAD_DATA(R,S,u,t,E0,E1,u1); \
244	u>>=2L; \
245	t=ROTATE(t,6); \
246	u2=(int)u>>8L; \
247	u1=(int)u&0x3f; \
248	u2&=0x3f; \
249	u>>=16L; \
250	LL^=des_SPtrans[0][u1]; \
251	LL^=des_SPtrans[2][u2]; \
252	u3=(int)u>>8L; \
253	u1=(int)u&0x3f; \
254	u3&=0x3f; \
255	LL^=des_SPtrans[4][u1]; \
256	LL^=des_SPtrans[6][u3]; \
257	u2=(int)t>>8L; \
258	u1=(int)t&0x3f; \
259	u2&=0x3f; \
260	t>>=16L; \
261	LL^=des_SPtrans[1][u1]; \
262	LL^=des_SPtrans[3][u2]; \
263	u3=(int)t>>8L; \
264	u1=(int)t&0x3f; \
265	u3&=0x3f; \
266	LL^=des_SPtrans[5][u1]; \
267	LL^=des_SPtrans[7][u3]; }
268#endif
269#ifdef DES_RISC2
270#define D_ENCRYPT(LL,R,S) {\
271	unsigned int u1,u2,s1,s2; \
272	LOAD_DATA(R,S,u,t,E0,E1,u1); \
273	u>>=2L; \
274	t=ROTATE(t,6); \
275	u2=(int)u>>8L; \
276	u1=(int)u&0x3f; \
277	u2&=0x3f; \
278	LL^=des_SPtrans[0][u1]; \
279	LL^=des_SPtrans[2][u2]; \
280	s1=(int)u>>16L; \
281	s2=(int)u>>24L; \
282	s1&=0x3f; \
283	s2&=0x3f; \
284	LL^=des_SPtrans[4][s1]; \
285	LL^=des_SPtrans[6][s2]; \
286	u2=(int)t>>8L; \
287	u1=(int)t&0x3f; \
288	u2&=0x3f; \
289	LL^=des_SPtrans[1][u1]; \
290	LL^=des_SPtrans[3][u2]; \
291	s1=(int)t>>16; \
292	s2=(int)t>>24L; \
293	s1&=0x3f; \
294	s2&=0x3f; \
295	LL^=des_SPtrans[5][s1]; \
296	LL^=des_SPtrans[7][s2]; }
297#endif
298
299#else
300
301#define D_ENCRYPT(LL,R,S) {\
302	LOAD_DATA_tmp(R,S,u,t,E0,E1); \
303	t=ROTATE(t,4); \
304	LL^=\
305		des_SPtrans[0][(u>> 2L)&0x3f]^ \
306		des_SPtrans[2][(u>>10L)&0x3f]^ \
307		des_SPtrans[4][(u>>18L)&0x3f]^ \
308		des_SPtrans[6][(u>>26L)&0x3f]^ \
309		des_SPtrans[1][(t>> 2L)&0x3f]^ \
310		des_SPtrans[3][(t>>10L)&0x3f]^ \
311		des_SPtrans[5][(t>>18L)&0x3f]^ \
312		des_SPtrans[7][(t>>26L)&0x3f]; }
313#endif
314#endif
315
316	/* IP and FP
317	 * The problem is more of a geometric problem that random bit fiddling.
318	 0  1  2  3  4  5  6  7      62 54 46 38 30 22 14  6
319	 8  9 10 11 12 13 14 15      60 52 44 36 28 20 12  4
320	16 17 18 19 20 21 22 23      58 50 42 34 26 18 10  2
321	24 25 26 27 28 29 30 31  to  56 48 40 32 24 16  8  0
322
323	32 33 34 35 36 37 38 39      63 55 47 39 31 23 15  7
324	40 41 42 43 44 45 46 47      61 53 45 37 29 21 13  5
325	48 49 50 51 52 53 54 55      59 51 43 35 27 19 11  3
326	56 57 58 59 60 61 62 63      57 49 41 33 25 17  9  1
327
328	The output has been subject to swaps of the form
329	0 1 -> 3 1 but the odd and even bits have been put into
330	2 3    2 0
331	different words.  The main trick is to remember that
332	t=((l>>size)^r)&(mask);
333	r^=t;
334	l^=(t<<size);
335	can be used to swap and move bits between words.
336
337	So l =  0  1  2  3  r = 16 17 18 19
338	        4  5  6  7      20 21 22 23
339	        8  9 10 11      24 25 26 27
340	       12 13 14 15      28 29 30 31
341	becomes (for size == 2 and mask == 0x3333)
342	   t =   2^16  3^17 -- --   l =  0  1 16 17  r =  2  3 18 19
343		 6^20  7^21 -- --        4  5 20 21       6  7 22 23
344		10^24 11^25 -- --        8  9 24 25      10 11 24 25
345		14^28 15^29 -- --       12 13 28 29      14 15 28 29
346
347	Thanks for hints from Richard Outerbridge - he told me IP&FP
348	could be done in 15 xor, 10 shifts and 5 ands.
349	When I finally started to think of the problem in 2D
350	I first got ~42 operations without xors.  When I remembered
351	how to use xors :-) I got it to its final state.
352	*/
353#define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
354	(b)^=(t),\
355	(a)^=((t)<<(n)))
356
357#define IP(l,r) \
358	{ \
359	register DES_LONG tt; \
360	PERM_OP(r,l,tt, 4,0x0f0f0f0fL); \
361	PERM_OP(l,r,tt,16,0x0000ffffL); \
362	PERM_OP(r,l,tt, 2,0x33333333L); \
363	PERM_OP(l,r,tt, 8,0x00ff00ffL); \
364	PERM_OP(r,l,tt, 1,0x55555555L); \
365	}
366
367#define FP(l,r) \
368	{ \
369	register DES_LONG tt; \
370	PERM_OP(l,r,tt, 1,0x55555555L); \
371	PERM_OP(r,l,tt, 8,0x00ff00ffL); \
372	PERM_OP(l,r,tt, 2,0x33333333L); \
373	PERM_OP(r,l,tt,16,0x0000ffffL); \
374	PERM_OP(l,r,tt, 4,0x0f0f0f0fL); \
375	}
376
377extern const DES_LONG des_SPtrans[8][64];
378
379void fcrypt_body(DES_LONG *out,des_key_schedule ks,
380	DES_LONG Eswap0, DES_LONG Eswap1);
381#endif
382