des_locl.h revision 1.3 1 1.3 itojun /* $NetBSD: des_locl.h,v 1.3 2000/11/06 14:11:41 itojun Exp $ */
2 1.3 itojun /* $KAME: des_locl.h,v 1.6 2000/11/06 13:58:09 itojun Exp $ */
3 1.1 thorpej
4 1.1 thorpej /* lib/des/des_locl.h */
5 1.1 thorpej /* Copyright (C) 1995-1996 Eric Young (eay (at) mincom.oz.au)
6 1.1 thorpej * All rights reserved.
7 1.1 thorpej *
8 1.1 thorpej * This file is part of an SSL implementation written
9 1.1 thorpej * by Eric Young (eay (at) mincom.oz.au).
10 1.1 thorpej * The implementation was written so as to conform with Netscapes SSL
11 1.1 thorpej * specification. This library and applications are
12 1.1 thorpej * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
13 1.1 thorpej * as long as the following conditions are aheared to.
14 1.1 thorpej *
15 1.1 thorpej * Copyright remains Eric Young's, and as such any Copyright notices in
16 1.1 thorpej * the code are not to be removed. If this code is used in a product,
17 1.1 thorpej * Eric Young should be given attribution as the author of the parts used.
18 1.1 thorpej * This can be in the form of a textual message at program startup or
19 1.1 thorpej * in documentation (online or textual) provided with the package.
20 1.1 thorpej *
21 1.1 thorpej * Redistribution and use in source and binary forms, with or without
22 1.1 thorpej * modification, are permitted provided that the following conditions
23 1.1 thorpej * are met:
24 1.1 thorpej * 1. Redistributions of source code must retain the copyright
25 1.1 thorpej * notice, this list of conditions and the following disclaimer.
26 1.1 thorpej * 2. Redistributions in binary form must reproduce the above copyright
27 1.1 thorpej * notice, this list of conditions and the following disclaimer in the
28 1.1 thorpej * documentation and/or other materials provided with the distribution.
29 1.1 thorpej * 3. All advertising materials mentioning features or use of this software
30 1.1 thorpej * must display the following acknowledgement:
31 1.1 thorpej * This product includes software developed by Eric Young (eay (at) mincom.oz.au)
32 1.1 thorpej *
33 1.1 thorpej * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
34 1.1 thorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35 1.1 thorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
36 1.1 thorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
37 1.1 thorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
38 1.1 thorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
39 1.1 thorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
40 1.1 thorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
41 1.1 thorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
42 1.1 thorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 1.1 thorpej * SUCH DAMAGE.
44 1.1 thorpej *
45 1.1 thorpej * The licence and distribution terms for any publically available version or
46 1.1 thorpej * derivative of this code cannot be changed. i.e. this code cannot simply be
47 1.1 thorpej * copied and put under another distribution licence
48 1.1 thorpej * [including the GNU Public Licence.]
49 1.1 thorpej */
50 1.1 thorpej /* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
51 1.1 thorpej *
52 1.1 thorpej * Always modify des_locl.org since des_locl.h is automatically generated from
53 1.1 thorpej * it during SSLeay configuration.
54 1.1 thorpej *
55 1.1 thorpej * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
56 1.1 thorpej */
57 1.1 thorpej
58 1.1 thorpej #ifndef HEADER_DES_LOCL_H
59 1.1 thorpej #define HEADER_DES_LOCL_H
60 1.1 thorpej
61 1.1 thorpej #include <crypto/des/des.h>
62 1.1 thorpej
63 1.1 thorpej #undef DES_PTR
64 1.1 thorpej
65 1.1 thorpej #ifdef __STDC__
66 1.1 thorpej #undef NOPROTO
67 1.1 thorpej #endif
68 1.1 thorpej
69 1.1 thorpej #define ITERATIONS 16
70 1.1 thorpej #define HALF_ITERATIONS 8
71 1.1 thorpej
72 1.1 thorpej /* used in des_read and des_write */
73 1.1 thorpej #define MAXWRITE (1024*16)
74 1.1 thorpej #define BSIZE (MAXWRITE+4)
75 1.1 thorpej
76 1.1 thorpej #define c2l(c,l) (l =((DES_LONG)(*((c)++))) , \
77 1.1 thorpej l|=((DES_LONG)(*((c)++)))<< 8L, \
78 1.1 thorpej l|=((DES_LONG)(*((c)++)))<<16L, \
79 1.1 thorpej l|=((DES_LONG)(*((c)++)))<<24L)
80 1.1 thorpej
81 1.1 thorpej /* NOTE - c is not incremented as per c2l */
82 1.1 thorpej #define c2ln(c,l1,l2,n) { \
83 1.1 thorpej c+=n; \
84 1.1 thorpej l1=l2=0; \
85 1.1 thorpej switch (n) { \
86 1.1 thorpej case 8: l2 =((DES_LONG)(*(--(c))))<<24L; \
87 1.1 thorpej case 7: l2|=((DES_LONG)(*(--(c))))<<16L; \
88 1.1 thorpej case 6: l2|=((DES_LONG)(*(--(c))))<< 8L; \
89 1.1 thorpej case 5: l2|=((DES_LONG)(*(--(c)))); \
90 1.1 thorpej case 4: l1 =((DES_LONG)(*(--(c))))<<24L; \
91 1.1 thorpej case 3: l1|=((DES_LONG)(*(--(c))))<<16L; \
92 1.1 thorpej case 2: l1|=((DES_LONG)(*(--(c))))<< 8L; \
93 1.1 thorpej case 1: l1|=((DES_LONG)(*(--(c)))); \
94 1.1 thorpej } \
95 1.1 thorpej }
96 1.1 thorpej
97 1.1 thorpej #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
98 1.1 thorpej *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
99 1.1 thorpej *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
100 1.1 thorpej *((c)++)=(unsigned char)(((l)>>24L)&0xff))
101 1.1 thorpej
102 1.1 thorpej /* replacements for htonl and ntohl since I have no idea what to do
103 1.1 thorpej * when faced with machines with 8 byte longs. */
104 1.1 thorpej #define HDRSIZE 4
105 1.1 thorpej
106 1.1 thorpej #define n2l(c,l) (l =((DES_LONG)(*((c)++)))<<24L, \
107 1.1 thorpej l|=((DES_LONG)(*((c)++)))<<16L, \
108 1.1 thorpej l|=((DES_LONG)(*((c)++)))<< 8L, \
109 1.1 thorpej l|=((DES_LONG)(*((c)++))))
110 1.1 thorpej
111 1.1 thorpej #define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
112 1.1 thorpej *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
113 1.1 thorpej *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
114 1.1 thorpej *((c)++)=(unsigned char)(((l) )&0xff))
115 1.1 thorpej
116 1.1 thorpej /* NOTE - c is not incremented as per l2c */
117 1.1 thorpej #define l2cn(l1,l2,c,n) { \
118 1.1 thorpej c+=n; \
119 1.1 thorpej switch (n) { \
120 1.1 thorpej case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
121 1.1 thorpej case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
122 1.1 thorpej case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
123 1.1 thorpej case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
124 1.1 thorpej case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
125 1.1 thorpej case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
126 1.1 thorpej case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
127 1.1 thorpej case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
128 1.1 thorpej } \
129 1.1 thorpej }
130 1.1 thorpej
131 1.1 thorpej #define ROTATE(a,n) (((a)>>(n))+((a)<<(32-(n))))
132 1.1 thorpej
133 1.1 thorpej /* The changes to this macro may help or hinder, depending on the
134 1.1 thorpej * compiler and the achitecture. gcc2 always seems to do well :-).
135 1.1 thorpej * Inspired by Dana How <how (at) isl.stanford.edu>
136 1.1 thorpej * DO NOT use the alternative version on machines with 8 byte longs.
137 1.1 thorpej * It does not seem to work on the Alpha, even when DES_LONG is 4
138 1.1 thorpej * bytes, probably an issue of accessing non-word aligned objects :-( */
139 1.1 thorpej #ifdef DES_PTR
140 1.1 thorpej
141 1.1 thorpej #define D_ENCRYPT(L,R,S) { \
142 1.1 thorpej u=((R^s[S ])<<2); \
143 1.1 thorpej t= R^s[S+1]; \
144 1.1 thorpej t=ROTATE(t,2); \
145 1.1 thorpej L^= (\
146 1.1 thorpej *(DES_LONG *)((unsigned char *)des_SP+0x100+((t )&0xfc))+ \
147 1.1 thorpej *(DES_LONG *)((unsigned char *)des_SP+0x300+((t>> 8)&0xfc))+ \
148 1.1 thorpej *(DES_LONG *)((unsigned char *)des_SP+0x500+((t>>16)&0xfc))+ \
149 1.1 thorpej *(DES_LONG *)((unsigned char *)des_SP+0x700+((t>>24)&0xfc))+ \
150 1.1 thorpej *(DES_LONG *)((unsigned char *)des_SP +((u )&0xfc))+ \
151 1.1 thorpej *(DES_LONG *)((unsigned char *)des_SP+0x200+((u>> 8)&0xfc))+ \
152 1.1 thorpej *(DES_LONG *)((unsigned char *)des_SP+0x400+((u>>16)&0xfc))+ \
153 1.1 thorpej *(DES_LONG *)((unsigned char *)des_SP+0x600+((u>>24)&0xfc))); }
154 1.1 thorpej #else /* original version */
155 1.1 thorpej #ifdef undef
156 1.1 thorpej #define D_ENCRYPT(L,R,S) \
157 1.1 thorpej U.l=R^s[S+1]; \
158 1.1 thorpej T.s[0]=((U.s[0]>>4)|(U.s[1]<<12))&0x3f3f; \
159 1.1 thorpej T.s[1]=((U.s[1]>>4)|(U.s[0]<<12))&0x3f3f; \
160 1.1 thorpej U.l=(R^s[S ])&0x3f3f3f3fL; \
161 1.1 thorpej L^= des_SPtrans[1][(T.c[0])]| \
162 1.1 thorpej des_SPtrans[3][(T.c[1])]| \
163 1.1 thorpej des_SPtrans[5][(T.c[2])]| \
164 1.1 thorpej des_SPtrans[7][(T.c[3])]| \
165 1.1 thorpej des_SPtrans[0][(U.c[0])]| \
166 1.1 thorpej des_SPtrans[2][(U.c[1])]| \
167 1.1 thorpej des_SPtrans[4][(U.c[2])]| \
168 1.1 thorpej des_SPtrans[6][(U.c[3])];
169 1.1 thorpej #else
170 1.1 thorpej #define D_ENCRYPT(Q,R,S) {\
171 1.1 thorpej u=(R^s[S ]); \
172 1.1 thorpej t=R^s[S+1]; \
173 1.1 thorpej t=ROTATE(t,4); \
174 1.1 thorpej Q^= des_SPtrans[1][(t )&0x3f]| \
175 1.1 thorpej des_SPtrans[3][(t>> 8L)&0x3f]| \
176 1.1 thorpej des_SPtrans[5][(t>>16L)&0x3f]| \
177 1.1 thorpej des_SPtrans[7][(t>>24L)&0x3f]| \
178 1.1 thorpej des_SPtrans[0][(u )&0x3f]| \
179 1.1 thorpej des_SPtrans[2][(u>> 8L)&0x3f]| \
180 1.1 thorpej des_SPtrans[4][(u>>16L)&0x3f]| \
181 1.1 thorpej des_SPtrans[6][(u>>24L)&0x3f]; }
182 1.1 thorpej #endif
183 1.1 thorpej #endif
184 1.1 thorpej
185 1.1 thorpej /* IP and FP
186 1.1 thorpej * The problem is more of a geometric problem that random bit fiddling.
187 1.1 thorpej 0 1 2 3 4 5 6 7 62 54 46 38 30 22 14 6
188 1.1 thorpej 8 9 10 11 12 13 14 15 60 52 44 36 28 20 12 4
189 1.1 thorpej 16 17 18 19 20 21 22 23 58 50 42 34 26 18 10 2
190 1.1 thorpej 24 25 26 27 28 29 30 31 to 56 48 40 32 24 16 8 0
191 1.1 thorpej
192 1.1 thorpej 32 33 34 35 36 37 38 39 63 55 47 39 31 23 15 7
193 1.1 thorpej 40 41 42 43 44 45 46 47 61 53 45 37 29 21 13 5
194 1.1 thorpej 48 49 50 51 52 53 54 55 59 51 43 35 27 19 11 3
195 1.1 thorpej 56 57 58 59 60 61 62 63 57 49 41 33 25 17 9 1
196 1.1 thorpej
197 1.1 thorpej The output has been subject to swaps of the form
198 1.1 thorpej 0 1 -> 3 1 but the odd and even bits have been put into
199 1.1 thorpej 2 3 2 0
200 1.1 thorpej different words. The main trick is to remember that
201 1.1 thorpej t=((l>>size)^r)&(mask);
202 1.1 thorpej r^=t;
203 1.1 thorpej l^=(t<<size);
204 1.1 thorpej can be used to swap and move bits between words.
205 1.1 thorpej
206 1.1 thorpej So l = 0 1 2 3 r = 16 17 18 19
207 1.1 thorpej 4 5 6 7 20 21 22 23
208 1.1 thorpej 8 9 10 11 24 25 26 27
209 1.1 thorpej 12 13 14 15 28 29 30 31
210 1.1 thorpej becomes (for size == 2 and mask == 0x3333)
211 1.1 thorpej t = 2^16 3^17 -- -- l = 0 1 16 17 r = 2 3 18 19
212 1.1 thorpej 6^20 7^21 -- -- 4 5 20 21 6 7 22 23
213 1.1 thorpej 10^24 11^25 -- -- 8 9 24 25 10 11 24 25
214 1.1 thorpej 14^28 15^29 -- -- 12 13 28 29 14 15 28 29
215 1.1 thorpej
216 1.1 thorpej Thanks for hints from Richard Outerbridge - he told me IP&FP
217 1.1 thorpej could be done in 15 xor, 10 shifts and 5 ands.
218 1.1 thorpej When I finally started to think of the problem in 2D
219 1.1 thorpej I first got ~42 operations without xors. When I remembered
220 1.1 thorpej how to use xors :-) I got it to its final state.
221 1.1 thorpej */
222 1.1 thorpej #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
223 1.1 thorpej (b)^=(t),\
224 1.1 thorpej (a)^=((t)<<(n)))
225 1.1 thorpej
226 1.1 thorpej #define IP(l,r) \
227 1.1 thorpej { \
228 1.1 thorpej register DES_LONG tt; \
229 1.1 thorpej PERM_OP(r,l,tt, 4,0x0f0f0f0fL); \
230 1.1 thorpej PERM_OP(l,r,tt,16,0x0000ffffL); \
231 1.1 thorpej PERM_OP(r,l,tt, 2,0x33333333L); \
232 1.1 thorpej PERM_OP(l,r,tt, 8,0x00ff00ffL); \
233 1.1 thorpej PERM_OP(r,l,tt, 1,0x55555555L); \
234 1.1 thorpej }
235 1.1 thorpej
236 1.1 thorpej #define FP(l,r) \
237 1.1 thorpej { \
238 1.1 thorpej register DES_LONG tt; \
239 1.1 thorpej PERM_OP(l,r,tt, 1,0x55555555L); \
240 1.1 thorpej PERM_OP(r,l,tt, 8,0x00ff00ffL); \
241 1.1 thorpej PERM_OP(l,r,tt, 2,0x33333333L); \
242 1.1 thorpej PERM_OP(r,l,tt,16,0x0000ffffL); \
243 1.1 thorpej PERM_OP(l,r,tt, 4,0x0f0f0f0fL); \
244 1.1 thorpej }
245 1.1 thorpej #endif
246