des_locl.h revision 1.1 1 1.1 thorpej /* $NetBSD: des_locl.h,v 1.1 2000/06/14 19:45:35 thorpej Exp $ */
2 1.1 thorpej /* $KAME: des_locl.h,v 1.4 2000/03/27 04:43:46 sumikawa 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 #include <sys/param.h>
59 1.1 thorpej #include <sys/malloc.h>
60 1.1 thorpej #include <sys/mbuf.h>
61 1.1 thorpej #include <sys/systm.h>
62 1.1 thorpej
63 1.1 thorpej #ifndef HEADER_DES_LOCL_H
64 1.1 thorpej #define HEADER_DES_LOCL_H
65 1.1 thorpej
66 1.1 thorpej #if defined(WIN32) || defined(WIN16)
67 1.1 thorpej #ifndef MSDOS
68 1.1 thorpej #define MSDOS
69 1.1 thorpej #endif
70 1.1 thorpej #endif
71 1.1 thorpej
72 1.1 thorpej /*
73 1.1 thorpej #include <stdio.h>
74 1.1 thorpej #include <stdlib.h>
75 1.1 thorpej #ifndef MSDOS
76 1.1 thorpej #include <unistd.h>
77 1.1 thorpej #endif
78 1.1 thorpej */
79 1.1 thorpej #include <crypto/des/des.h>
80 1.1 thorpej
81 1.1 thorpej /* the following is tweaked from a config script, that is why it is a
82 1.1 thorpej * protected undef/define */
83 1.1 thorpej #ifndef DES_PTR
84 1.1 thorpej #undef DES_PTR
85 1.1 thorpej #endif
86 1.1 thorpej
87 1.1 thorpej #ifdef MSDOS /* Visual C++ 2.1 (Windows NT/95) */
88 1.1 thorpej #include <stdlib.h>
89 1.1 thorpej #include <errno.h>
90 1.1 thorpej #include <time.h>
91 1.1 thorpej #include <io.h>
92 1.1 thorpej #ifndef RAND
93 1.1 thorpej #define RAND
94 1.1 thorpej #endif
95 1.1 thorpej #undef NOPROTO
96 1.1 thorpej #endif
97 1.1 thorpej
98 1.1 thorpej #if !defined(_KERNEL) && (defined(__STDC__) || defined(VMS) || defined(M_XENIX) || defined(MSDOS))
99 1.1 thorpej #ifndef __NetBSD__
100 1.1 thorpej #include <string.h>
101 1.1 thorpej #endif
102 1.1 thorpej #endif
103 1.1 thorpej
104 1.1 thorpej #ifdef __NetBSD__
105 1.1 thorpej #include <sys/systm.h>
106 1.1 thorpej #endif
107 1.1 thorpej
108 1.1 thorpej #ifndef RAND
109 1.1 thorpej #define RAND
110 1.1 thorpej #endif
111 1.1 thorpej
112 1.1 thorpej #ifdef linux
113 1.1 thorpej #undef RAND
114 1.1 thorpej #endif
115 1.1 thorpej
116 1.1 thorpej #ifdef MSDOS
117 1.1 thorpej #define getpid() 2
118 1.1 thorpej #define RAND
119 1.1 thorpej #undef NOPROTO
120 1.1 thorpej #endif
121 1.1 thorpej
122 1.1 thorpej #if defined(NOCONST)
123 1.1 thorpej #define const
124 1.1 thorpej #endif
125 1.1 thorpej
126 1.1 thorpej #ifdef __STDC__
127 1.1 thorpej #undef NOPROTO
128 1.1 thorpej #endif
129 1.1 thorpej
130 1.1 thorpej #ifdef RAND
131 1.1 thorpej #define srandom(s) srand(s)
132 1.1 thorpej #define random rand
133 1.1 thorpej #endif
134 1.1 thorpej
135 1.1 thorpej #define ITERATIONS 16
136 1.1 thorpej #define HALF_ITERATIONS 8
137 1.1 thorpej
138 1.1 thorpej /* used in des_read and des_write */
139 1.1 thorpej #define MAXWRITE (1024*16)
140 1.1 thorpej #define BSIZE (MAXWRITE+4)
141 1.1 thorpej
142 1.1 thorpej #define c2l(c,l) (l =((DES_LONG)(*((c)++))) , \
143 1.1 thorpej l|=((DES_LONG)(*((c)++)))<< 8L, \
144 1.1 thorpej l|=((DES_LONG)(*((c)++)))<<16L, \
145 1.1 thorpej l|=((DES_LONG)(*((c)++)))<<24L)
146 1.1 thorpej
147 1.1 thorpej /* NOTE - c is not incremented as per c2l */
148 1.1 thorpej #define c2ln(c,l1,l2,n) { \
149 1.1 thorpej c+=n; \
150 1.1 thorpej l1=l2=0; \
151 1.1 thorpej switch (n) { \
152 1.1 thorpej case 8: l2 =((DES_LONG)(*(--(c))))<<24L; \
153 1.1 thorpej case 7: l2|=((DES_LONG)(*(--(c))))<<16L; \
154 1.1 thorpej case 6: l2|=((DES_LONG)(*(--(c))))<< 8L; \
155 1.1 thorpej case 5: l2|=((DES_LONG)(*(--(c)))); \
156 1.1 thorpej case 4: l1 =((DES_LONG)(*(--(c))))<<24L; \
157 1.1 thorpej case 3: l1|=((DES_LONG)(*(--(c))))<<16L; \
158 1.1 thorpej case 2: l1|=((DES_LONG)(*(--(c))))<< 8L; \
159 1.1 thorpej case 1: l1|=((DES_LONG)(*(--(c)))); \
160 1.1 thorpej } \
161 1.1 thorpej }
162 1.1 thorpej
163 1.1 thorpej #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
164 1.1 thorpej *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
165 1.1 thorpej *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
166 1.1 thorpej *((c)++)=(unsigned char)(((l)>>24L)&0xff))
167 1.1 thorpej
168 1.1 thorpej /* replacements for htonl and ntohl since I have no idea what to do
169 1.1 thorpej * when faced with machines with 8 byte longs. */
170 1.1 thorpej #define HDRSIZE 4
171 1.1 thorpej
172 1.1 thorpej #define n2l(c,l) (l =((DES_LONG)(*((c)++)))<<24L, \
173 1.1 thorpej l|=((DES_LONG)(*((c)++)))<<16L, \
174 1.1 thorpej l|=((DES_LONG)(*((c)++)))<< 8L, \
175 1.1 thorpej l|=((DES_LONG)(*((c)++))))
176 1.1 thorpej
177 1.1 thorpej #define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
178 1.1 thorpej *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
179 1.1 thorpej *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
180 1.1 thorpej *((c)++)=(unsigned char)(((l) )&0xff))
181 1.1 thorpej
182 1.1 thorpej /* NOTE - c is not incremented as per l2c */
183 1.1 thorpej #define l2cn(l1,l2,c,n) { \
184 1.1 thorpej c+=n; \
185 1.1 thorpej switch (n) { \
186 1.1 thorpej case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
187 1.1 thorpej case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
188 1.1 thorpej case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
189 1.1 thorpej case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
190 1.1 thorpej case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
191 1.1 thorpej case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
192 1.1 thorpej case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
193 1.1 thorpej case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
194 1.1 thorpej } \
195 1.1 thorpej }
196 1.1 thorpej
197 1.1 thorpej #if defined(WIN32)
198 1.1 thorpej #define ROTATE(a,n) (_lrotr(a,n))
199 1.1 thorpej #else
200 1.1 thorpej #define ROTATE(a,n) (((a)>>(n))+((a)<<(32-(n))))
201 1.1 thorpej #endif
202 1.1 thorpej
203 1.1 thorpej /* The changes to this macro may help or hinder, depending on the
204 1.1 thorpej * compiler and the achitecture. gcc2 always seems to do well :-).
205 1.1 thorpej * Inspired by Dana How <how (at) isl.stanford.edu>
206 1.1 thorpej * DO NOT use the alternative version on machines with 8 byte longs.
207 1.1 thorpej * It does not seem to work on the Alpha, even when DES_LONG is 4
208 1.1 thorpej * bytes, probably an issue of accessing non-word aligned objects :-( */
209 1.1 thorpej #ifdef DES_PTR
210 1.1 thorpej
211 1.1 thorpej #define D_ENCRYPT(L,R,S) { \
212 1.1 thorpej u=((R^s[S ])<<2); \
213 1.1 thorpej t= R^s[S+1]; \
214 1.1 thorpej t=ROTATE(t,2); \
215 1.1 thorpej L^= (\
216 1.1 thorpej *(DES_LONG *)((unsigned char *)des_SP+0x100+((t )&0xfc))+ \
217 1.1 thorpej *(DES_LONG *)((unsigned char *)des_SP+0x300+((t>> 8)&0xfc))+ \
218 1.1 thorpej *(DES_LONG *)((unsigned char *)des_SP+0x500+((t>>16)&0xfc))+ \
219 1.1 thorpej *(DES_LONG *)((unsigned char *)des_SP+0x700+((t>>24)&0xfc))+ \
220 1.1 thorpej *(DES_LONG *)((unsigned char *)des_SP +((u )&0xfc))+ \
221 1.1 thorpej *(DES_LONG *)((unsigned char *)des_SP+0x200+((u>> 8)&0xfc))+ \
222 1.1 thorpej *(DES_LONG *)((unsigned char *)des_SP+0x400+((u>>16)&0xfc))+ \
223 1.1 thorpej *(DES_LONG *)((unsigned char *)des_SP+0x600+((u>>24)&0xfc))); }
224 1.1 thorpej #else /* original version */
225 1.1 thorpej #ifdef undef
226 1.1 thorpej #define D_ENCRYPT(L,R,S) \
227 1.1 thorpej U.l=R^s[S+1]; \
228 1.1 thorpej T.s[0]=((U.s[0]>>4)|(U.s[1]<<12))&0x3f3f; \
229 1.1 thorpej T.s[1]=((U.s[1]>>4)|(U.s[0]<<12))&0x3f3f; \
230 1.1 thorpej U.l=(R^s[S ])&0x3f3f3f3fL; \
231 1.1 thorpej L^= des_SPtrans[1][(T.c[0])]| \
232 1.1 thorpej des_SPtrans[3][(T.c[1])]| \
233 1.1 thorpej des_SPtrans[5][(T.c[2])]| \
234 1.1 thorpej des_SPtrans[7][(T.c[3])]| \
235 1.1 thorpej des_SPtrans[0][(U.c[0])]| \
236 1.1 thorpej des_SPtrans[2][(U.c[1])]| \
237 1.1 thorpej des_SPtrans[4][(U.c[2])]| \
238 1.1 thorpej des_SPtrans[6][(U.c[3])];
239 1.1 thorpej #else
240 1.1 thorpej #define D_ENCRYPT(Q,R,S) {\
241 1.1 thorpej u=(R^s[S ]); \
242 1.1 thorpej t=R^s[S+1]; \
243 1.1 thorpej t=ROTATE(t,4); \
244 1.1 thorpej Q^= des_SPtrans[1][(t )&0x3f]| \
245 1.1 thorpej des_SPtrans[3][(t>> 8L)&0x3f]| \
246 1.1 thorpej des_SPtrans[5][(t>>16L)&0x3f]| \
247 1.1 thorpej des_SPtrans[7][(t>>24L)&0x3f]| \
248 1.1 thorpej des_SPtrans[0][(u )&0x3f]| \
249 1.1 thorpej des_SPtrans[2][(u>> 8L)&0x3f]| \
250 1.1 thorpej des_SPtrans[4][(u>>16L)&0x3f]| \
251 1.1 thorpej des_SPtrans[6][(u>>24L)&0x3f]; }
252 1.1 thorpej #endif
253 1.1 thorpej #endif
254 1.1 thorpej
255 1.1 thorpej /* IP and FP
256 1.1 thorpej * The problem is more of a geometric problem that random bit fiddling.
257 1.1 thorpej 0 1 2 3 4 5 6 7 62 54 46 38 30 22 14 6
258 1.1 thorpej 8 9 10 11 12 13 14 15 60 52 44 36 28 20 12 4
259 1.1 thorpej 16 17 18 19 20 21 22 23 58 50 42 34 26 18 10 2
260 1.1 thorpej 24 25 26 27 28 29 30 31 to 56 48 40 32 24 16 8 0
261 1.1 thorpej
262 1.1 thorpej 32 33 34 35 36 37 38 39 63 55 47 39 31 23 15 7
263 1.1 thorpej 40 41 42 43 44 45 46 47 61 53 45 37 29 21 13 5
264 1.1 thorpej 48 49 50 51 52 53 54 55 59 51 43 35 27 19 11 3
265 1.1 thorpej 56 57 58 59 60 61 62 63 57 49 41 33 25 17 9 1
266 1.1 thorpej
267 1.1 thorpej The output has been subject to swaps of the form
268 1.1 thorpej 0 1 -> 3 1 but the odd and even bits have been put into
269 1.1 thorpej 2 3 2 0
270 1.1 thorpej different words. The main trick is to remember that
271 1.1 thorpej t=((l>>size)^r)&(mask);
272 1.1 thorpej r^=t;
273 1.1 thorpej l^=(t<<size);
274 1.1 thorpej can be used to swap and move bits between words.
275 1.1 thorpej
276 1.1 thorpej So l = 0 1 2 3 r = 16 17 18 19
277 1.1 thorpej 4 5 6 7 20 21 22 23
278 1.1 thorpej 8 9 10 11 24 25 26 27
279 1.1 thorpej 12 13 14 15 28 29 30 31
280 1.1 thorpej becomes (for size == 2 and mask == 0x3333)
281 1.1 thorpej t = 2^16 3^17 -- -- l = 0 1 16 17 r = 2 3 18 19
282 1.1 thorpej 6^20 7^21 -- -- 4 5 20 21 6 7 22 23
283 1.1 thorpej 10^24 11^25 -- -- 8 9 24 25 10 11 24 25
284 1.1 thorpej 14^28 15^29 -- -- 12 13 28 29 14 15 28 29
285 1.1 thorpej
286 1.1 thorpej Thanks for hints from Richard Outerbridge - he told me IP&FP
287 1.1 thorpej could be done in 15 xor, 10 shifts and 5 ands.
288 1.1 thorpej When I finally started to think of the problem in 2D
289 1.1 thorpej I first got ~42 operations without xors. When I remembered
290 1.1 thorpej how to use xors :-) I got it to its final state.
291 1.1 thorpej */
292 1.1 thorpej #define PERM_OP(a,b,t,n,m) ((t)=((((a)>>(n))^(b))&(m)),\
293 1.1 thorpej (b)^=(t),\
294 1.1 thorpej (a)^=((t)<<(n)))
295 1.1 thorpej
296 1.1 thorpej #define IP(l,r) \
297 1.1 thorpej { \
298 1.1 thorpej register DES_LONG tt; \
299 1.1 thorpej PERM_OP(r,l,tt, 4,0x0f0f0f0fL); \
300 1.1 thorpej PERM_OP(l,r,tt,16,0x0000ffffL); \
301 1.1 thorpej PERM_OP(r,l,tt, 2,0x33333333L); \
302 1.1 thorpej PERM_OP(l,r,tt, 8,0x00ff00ffL); \
303 1.1 thorpej PERM_OP(r,l,tt, 1,0x55555555L); \
304 1.1 thorpej }
305 1.1 thorpej
306 1.1 thorpej #define FP(l,r) \
307 1.1 thorpej { \
308 1.1 thorpej register DES_LONG tt; \
309 1.1 thorpej PERM_OP(l,r,tt, 1,0x55555555L); \
310 1.1 thorpej PERM_OP(r,l,tt, 8,0x00ff00ffL); \
311 1.1 thorpej PERM_OP(l,r,tt, 2,0x33333333L); \
312 1.1 thorpej PERM_OP(r,l,tt,16,0x0000ffffL); \
313 1.1 thorpej PERM_OP(l,r,tt, 4,0x0f0f0f0fL); \
314 1.1 thorpej }
315 1.1 thorpej #endif
316 1.1 thorpej
317 1.1 thorpej
318 1.1 thorpej /*
319 1.1 thorpej #define mbuf2char(i_mbuf, i_index, in) \
320 1.1 thorpej { \
321 1.1 thorpej register int i; \
322 1.1 thorpej struct mbuf *m; \
323 1.1 thorpej char *buf; \
324 1.1 thorpej m = i_mbuf; \
325 1.1 thorpej for (i = 0; i < 8; i ++){ \
326 1.1 thorpej if (i_index + i == m->m_len){ \
327 1.1 thorpej m = m->m_next; \
328 1.1 thorpej } \
329 1.1 thorpej buf = mtod(m, char *); \
330 1.1 thorpej in[i] = *(buf + i); \
331 1.1 thorpej }
332 1.1 thorpej
333 1.1 thorpej
334 1.1 thorpej #define char2mbuf(o_mbuf, o_index, out) \
335 1.1 thorpej { \
336 1.1 thorpej register int i; \
337 1.1 thorpej struct mbuf *m; \
338 1.1 thorpej char *buf; \
339 1.1 thorpej m = o_mbuf; \
340 1.1 thorpej for (i = 0; i < 8; i ++){ \
341 1.1 thorpej if (i_index + i == m->m_len){ \
342 1.1 thorpej m = m->m_next; \
343 1.1 thorpej } \
344 1.1 thorpej buf = mtod(m, char *); \
345 1.1 thorpej *(buf + i) = out[i]; \
346 1.1 thorpej }
347 1.1 thorpej */
348 1.1 thorpej
349