1 /* $KAME: des_locl.h,v 1.7 2001/09/10 04:03:58 itojun Exp $ */
3 /* crypto/des/des_locl.h */
4 /* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au)
7 * This file is part of an SSL implementation written
8 * by Eric Young (eay@mincom.oz.au).
9 * The implementation was written so as to conform with Netscapes SSL
10 * specification. This library and applications are
11 * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
12 * as long as the following conditions are aheared to.
14 * Copyright remains Eric Young's, and as such any Copyright notices in
15 * the code are not to be removed. If this code is used in a product,
16 * Eric Young should be given attribution as the author of the parts used.
17 * This can be in the form of a textual message at program startup or
18 * in documentation (online or textual) provided with the package.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. All advertising materials mentioning features or use of this software
29 * must display the following acknowledgement:
30 * This product includes software developed by Eric Young (eay@mincom.oz.au)
32 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44 * The licence and distribution terms for any publically available version or
45 * derivative of this code cannot be changed. i.e. this code cannot simply be
46 * copied and put under another distribution licence
47 * [including the GNU Public Licence.]
50 #ifndef HEADER_DES_LOCL_H
51 #define HEADER_DES_LOCL_H
53 #include <crypto/des/des.h>
62 #define HALF_ITERATIONS 8
64 /* used in des_read and des_write */
65 #define MAXWRITE (1024*16)
66 #define BSIZE (MAXWRITE+4)
68 #define c2l(c,l) (l =((DES_LONG)(*((c)++))) , \
69 l|=((DES_LONG)(*((c)++)))<< 8L, \
70 l|=((DES_LONG)(*((c)++)))<<16L, \
71 l|=((DES_LONG)(*((c)++)))<<24L)
73 /* NOTE - c is not incremented as per c2l */
74 #define c2ln(c,l1,l2,n) { \
78 case 8: l2 =((DES_LONG)(*(--(c))))<<24L; \
79 case 7: l2|=((DES_LONG)(*(--(c))))<<16L; \
80 case 6: l2|=((DES_LONG)(*(--(c))))<< 8L; \
81 case 5: l2|=((DES_LONG)(*(--(c)))); \
82 case 4: l1 =((DES_LONG)(*(--(c))))<<24L; \
83 case 3: l1|=((DES_LONG)(*(--(c))))<<16L; \
84 case 2: l1|=((DES_LONG)(*(--(c))))<< 8L; \
85 case 1: l1|=((DES_LONG)(*(--(c)))); \
89 #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
90 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
91 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
92 *((c)++)=(unsigned char)(((l)>>24L)&0xff))
94 /* replacements for htonl and ntohl since I have no idea what to do
95 * when faced with machines with 8 byte longs. */
98 #define n2l(c,l) (l =((DES_LONG)(*((c)++)))<<24L, \
99 l|=((DES_LONG)(*((c)++)))<<16L, \
100 l|=((DES_LONG)(*((c)++)))<< 8L, \
101 l|=((DES_LONG)(*((c)++))))
103 #define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
104 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
105 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
106 *((c)++)=(unsigned char)(((l) )&0xff))
108 /* NOTE - c is not incremented as per l2c */
109 #define l2cn(l1,l2,c,n) { \
112 case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
113 case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
114 case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
115 case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
116 case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
117 case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
118 case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
119 case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
123 #define ROTATE(a,n) (((a)>>(n))+((a)<<(32-(n))))
125 #define LOAD_DATA_tmp(a,b,c,d,e,f) LOAD_DATA(a,b,c,d,e,f,g)
126 #define LOAD_DATA(R,S,u,t,E0,E1,tmp) \
130 /* The changes to this macro may help or hinder, depending on the
131 * compiler and the achitecture. gcc2 always seems to do well :-).
132 * Inspired by Dana How <how@isl.stanford.edu>
133 * DO NOT use the alternative version on machines with 8 byte longs.
134 * It does not seem to work on the Alpha, even when DES_LONG is 4
135 * bytes, probably an issue of accessing non-word aligned objects :-( */
138 /* It recently occurred to me that 0^0^0^0^0^0^0 == 0, so there
139 * is no reason to not xor all the sub items together. This potentially
140 * saves a register since things can be xored directly into L */
142 #if defined(DES_RISC1) || defined(DES_RISC2)
144 #define D_ENCRYPT(LL,R,S) { \
145 unsigned int u1,u2,u3; \
146 LOAD_DATA(R,S,u,t,E0,E1,u1); \
152 LL^= *(const DES_LONG *)(des_SP +u1); \
153 LL^= *(const DES_LONG *)(des_SP+0x200+u2); \
157 LL^= *(const DES_LONG *)(des_SP+0x400+u1); \
158 LL^= *(const DES_LONG *)(des_SP+0x600+u3); \
163 LL^= *(const DES_LONG *)(des_SP+0x100+u1); \
164 LL^= *(const DES_LONG *)(des_SP+0x300+u2); \
168 LL^= *(const DES_LONG *)(des_SP+0x500+u1); \
169 LL^= *(const DES_LONG *)(des_SP+0x700+u3); }
170 #endif /* DES_RISC1 */
172 #define D_ENCRYPT(LL,R,S) { \
173 unsigned int u1,u2,s1,s2; \
174 LOAD_DATA(R,S,u,t,E0,E1,u1); \
179 LL^= *(const DES_LONG *)(des_SP +u1); \
180 LL^= *(const DES_LONG *)(des_SP+0x200+u2); \
185 LL^= *(const DES_LONG *)(des_SP+0x400+s1); \
186 LL^= *(const DES_LONG *)(des_SP+0x600+s2); \
190 LL^= *(const DES_LONG *)(des_SP+0x100+u1); \
191 LL^= *(const DES_LONG *)(des_SP+0x300+u2); \
196 LL^= *(const DES_LONG *)(des_SP+0x400+s1); \
197 LL^= *(const DES_LONG *)(des_SP+0x600+s2); \
201 LL^= *(const DES_LONG *)(des_SP+0x100+u1); \
202 LL^= *(const DES_LONG *)(des_SP+0x300+u2); \
207 LL^= *(const DES_LONG *)(des_SP+0x500+s1); \
208 LL^= *(const DES_LONG *)(des_SP+0x700+s2); }
209 #endif /* DES_RISC2 */
210 #else /* DES_RISC1 || DES_RISC2 */
211 #define D_ENCRYPT(LL,R,S) { \
212 LOAD_DATA_tmp(R,S,u,t,E0,E1); \
215 *(const DES_LONG *)(des_SP +((u )&0xfc))^ \
216 *(const DES_LONG *)(des_SP+0x200+((u>> 8L)&0xfc))^ \
217 *(const DES_LONG *)(des_SP+0x400+((u>>16L)&0xfc))^ \
218 *(const DES_LONG *)(des_SP+0x600+((u>>24L)&0xfc))^ \
219 *(const DES_LONG *)(des_SP+0x100+((t )&0xfc))^ \
220 *(const DES_LONG *)(des_SP+0x300+((t>> 8L)&0xfc))^ \
221 *(const DES_LONG *)(des_SP+0x500+((t>>16L)&0xfc))^ \
222 *(const DES_LONG *)(des_SP+0x700+((t>>24L)&0xfc)); }
223 #endif /* DES_RISC1 || DES_RISC2 */
224 #else /* original version */
226 #if defined(DES_RISC1) || defined(DES_RISC2)
228 #define D_ENCRYPT(LL,R,S) {\
229 unsigned int u1,u2,u3; \
230 LOAD_DATA(R,S,u,t,E0,E1,u1); \
237 LL^=des_SPtrans[0][u1]; \
238 LL^=des_SPtrans[2][u2]; \
242 LL^=des_SPtrans[4][u1]; \
243 LL^=des_SPtrans[6][u3]; \
248 LL^=des_SPtrans[1][u1]; \
249 LL^=des_SPtrans[3][u2]; \
253 LL^=des_SPtrans[5][u1]; \
254 LL^=des_SPtrans[7][u3]; }
255 #endif /* DES_RISC1 */
257 #define D_ENCRYPT(LL,R,S) {\
258 unsigned int u1,u2,s1,s2; \
259 LOAD_DATA(R,S,u,t,E0,E1,u1); \
265 LL^=des_SPtrans[0][u1]; \
266 LL^=des_SPtrans[2][u2]; \
271 LL^=des_SPtrans[4][s1]; \
272 LL^=des_SPtrans[6][s2]; \
276 LL^=des_SPtrans[1][u1]; \
277 LL^=des_SPtrans[3][u2]; \
282 LL^=des_SPtrans[5][s1]; \
283 LL^=des_SPtrans[7][s2]; }
284 #endif /* DES_RISC2 */
286 #else /* DES_RISC1 || DES_RISC2 */
288 #define D_ENCRYPT(LL,R,S) {\
289 LOAD_DATA_tmp(R,S,u,t,E0,E1); \
292 des_SPtrans[0][(u>> 2L)&0x3f]^ \
293 des_SPtrans[2][(u>>10L)&0x3f]^ \
294 des_SPtrans[4][(u>>18L)&0x3f]^ \
295 des_SPtrans[6][(u>>26L)&0x3f]^ \
296 des_SPtrans[1][(t>> 2L)&0x3f]^ \
297 des_SPtrans[3][(t>>10L)&0x3f]^ \
298 des_SPtrans[5][(t>>18L)&0x3f]^ \
299 des_SPtrans[7][(t>>26L)&0x3f]; }
300 #endif /* DES_RISC1 || DES_RISC2 */
304 * The problem is more of a geometric problem that random bit fiddling.
305 0 1 2 3 4 5 6 7 62 54 46 38 30 22 14 6
306 8 9 10 11 12 13 14 15 60 52 44 36 28 20 12 4
307 16 17 18 19 20 21 22 23 58 50 42 34 26 18 10 2
308 24 25 26 27 28 29 30 31 to 56 48 40 32 24 16 8 0
310 32 33 34 35 36 37 38 39 63 55 47 39 31 23 15 7
311 40 41 42 43 44 45 46 47 61 53 45 37 29 21 13 5
312 48 49 50 51 52 53 54 55 59 51 43 35 27 19 11 3
313 56 57 58 59 60 61 62 63 57 49 41 33 25 17 9 1
315 The output has been subject to swaps of the form
316 0 1 -> 3 1 but the odd and even bits have been put into
318 different words. The main trick is to remember that
319 t=((l>>size)^r)&(mask);
322 can be used to swap and move bits between words.
324 So l = 0 1 2 3 r = 16 17 18 19
326 8 9 10 11 24 25 26 27
327 12 13 14 15 28 29 30 31
328 becomes (for size == 2 and mask == 0x3333)
329 t = 2^16 3^17 -- -- l = 0 1 16 17 r = 2 3 18 19
330 6^20 7^21 -- -- 4 5 20 21 6 7 22 23
331 10^24 11^25 -- -- 8 9 24 25 10 11 24 25
332 14^28 15^29 -- -- 12 13 28 29 14 15 28 29
334 Thanks for hints from Richard Outerbridge - he told me IP&FP
335 could be done in 15 xor, 10 shifts and 5 ands.
336 When I finally started to think of the problem in 2D
337 I first got ~42 operations without xors. When I remembered
338 how to use xors :-) I got it to its final state.
340 #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
346 register DES_LONG tt; \
347 PERM_OP(r,l,tt, 4,0x0f0f0f0fL); \
348 PERM_OP(l,r,tt,16,0x0000ffffL); \
349 PERM_OP(r,l,tt, 2,0x33333333L); \
350 PERM_OP(l,r,tt, 8,0x00ff00ffL); \
351 PERM_OP(r,l,tt, 1,0x55555555L); \
356 register DES_LONG tt; \
357 PERM_OP(l,r,tt, 1,0x55555555L); \
358 PERM_OP(r,l,tt, 8,0x00ff00ffL); \
359 PERM_OP(l,r,tt, 2,0x33333333L); \
360 PERM_OP(r,l,tt,16,0x0000ffffL); \
361 PERM_OP(l,r,tt, 4,0x0f0f0f0fL); \