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