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