bf_locl.h revision 1.2.2.2 1 1.2.2.2 bouyer /* $NetBSD: bf_locl.h,v 1.2.2.2 2000/11/20 22:21:42 bouyer Exp $ */
2 1.2.2.2 bouyer /* $KAME: bf_locl.h,v 1.5 2000/08/31 06:03:48 itojun Exp $ */
3 1.2.2.2 bouyer
4 1.2.2.2 bouyer /* crypto/bf/bf_local.h */
5 1.2.2.2 bouyer /* Copyright (C) 1995-1997 Eric Young (eay (at) mincom.oz.au)
6 1.2.2.2 bouyer * All rights reserved.
7 1.2.2.2 bouyer *
8 1.2.2.2 bouyer * This package is an SSL implementation written
9 1.2.2.2 bouyer * by Eric Young (eay (at) mincom.oz.au).
10 1.2.2.2 bouyer * The implementation was written so as to conform with Netscapes SSL.
11 1.2.2.2 bouyer *
12 1.2.2.2 bouyer * This library is free for commercial and non-commercial use as long as
13 1.2.2.2 bouyer * the following conditions are aheared to. The following conditions
14 1.2.2.2 bouyer * apply to all code found in this distribution, be it the RC4, RSA,
15 1.2.2.2 bouyer * lhash, DES, etc., code; not just the SSL code. The SSL documentation
16 1.2.2.2 bouyer * included with this distribution is covered by the same copyright terms
17 1.2.2.2 bouyer * except that the holder is Tim Hudson (tjh (at) mincom.oz.au).
18 1.2.2.2 bouyer *
19 1.2.2.2 bouyer * Copyright remains Eric Young's, and as such any Copyright notices in
20 1.2.2.2 bouyer * the code are not to be removed.
21 1.2.2.2 bouyer * If this package is used in a product, Eric Young should be given attribution
22 1.2.2.2 bouyer * as the author of the parts of the library used.
23 1.2.2.2 bouyer * This can be in the form of a textual message at program startup or
24 1.2.2.2 bouyer * in documentation (online or textual) provided with the package.
25 1.2.2.2 bouyer *
26 1.2.2.2 bouyer * Redistribution and use in source and binary forms, with or without
27 1.2.2.2 bouyer * modification, are permitted provided that the following conditions
28 1.2.2.2 bouyer * are met:
29 1.2.2.2 bouyer * 1. Redistributions of source code must retain the copyright
30 1.2.2.2 bouyer * notice, this list of conditions and the following disclaimer.
31 1.2.2.2 bouyer * 2. Redistributions in binary form must reproduce the above copyright
32 1.2.2.2 bouyer * notice, this list of conditions and the following disclaimer in the
33 1.2.2.2 bouyer * documentation and/or other materials provided with the distribution.
34 1.2.2.2 bouyer * 3. All advertising materials mentioning features or use of this software
35 1.2.2.2 bouyer * must display the following acknowledgement:
36 1.2.2.2 bouyer * "This product includes cryptographic software written by
37 1.2.2.2 bouyer * Eric Young (eay (at) mincom.oz.au)"
38 1.2.2.2 bouyer * The word 'cryptographic' can be left out if the rouines from the library
39 1.2.2.2 bouyer * being used are not cryptographic related :-).
40 1.2.2.2 bouyer * 4. If you include any Windows specific code (or a derivative thereof) from
41 1.2.2.2 bouyer * the apps directory (application code) you must include an acknowledgement:
42 1.2.2.2 bouyer * "This product includes software written by Tim Hudson (tjh (at) mincom.oz.au)"
43 1.2.2.2 bouyer *
44 1.2.2.2 bouyer * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
45 1.2.2.2 bouyer * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 1.2.2.2 bouyer * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 1.2.2.2 bouyer * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48 1.2.2.2 bouyer * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 1.2.2.2 bouyer * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 1.2.2.2 bouyer * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 1.2.2.2 bouyer * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 1.2.2.2 bouyer * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 1.2.2.2 bouyer * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 1.2.2.2 bouyer * SUCH DAMAGE.
55 1.2.2.2 bouyer *
56 1.2.2.2 bouyer * The licence and distribution terms for any publically available version or
57 1.2.2.2 bouyer * derivative of this code cannot be changed. i.e. this code cannot simply be
58 1.2.2.2 bouyer * copied and put under another distribution licence
59 1.2.2.2 bouyer * [including the GNU Public Licence.]
60 1.2.2.2 bouyer */
61 1.2.2.2 bouyer /* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
62 1.2.2.2 bouyer *
63 1.2.2.2 bouyer * Always modify bf_locl.org since bf_locl.h is automatically generated from
64 1.2.2.2 bouyer * it during SSLeay configuration.
65 1.2.2.2 bouyer *
66 1.2.2.2 bouyer * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
67 1.2.2.2 bouyer */
68 1.2.2.2 bouyer
69 1.2.2.2 bouyer #undef c2l
70 1.2.2.2 bouyer #define c2l(c,l) (l =((BF_LONG)(*((c)++))) , \
71 1.2.2.2 bouyer l|=((BF_LONG)(*((c)++)))<< 8L, \
72 1.2.2.2 bouyer l|=((BF_LONG)(*((c)++)))<<16L, \
73 1.2.2.2 bouyer l|=((BF_LONG)(*((c)++)))<<24L)
74 1.2.2.2 bouyer
75 1.2.2.2 bouyer /* NOTE - c is not incremented as per c2l */
76 1.2.2.2 bouyer #undef c2ln
77 1.2.2.2 bouyer #define c2ln(c,l1,l2,n) { \
78 1.2.2.2 bouyer c+=n; \
79 1.2.2.2 bouyer l1=l2=0; \
80 1.2.2.2 bouyer switch (n) { \
81 1.2.2.2 bouyer case 8: l2 =((BF_LONG)(*(--(c))))<<24L; \
82 1.2.2.2 bouyer case 7: l2|=((BF_LONG)(*(--(c))))<<16L; \
83 1.2.2.2 bouyer case 6: l2|=((BF_LONG)(*(--(c))))<< 8L; \
84 1.2.2.2 bouyer case 5: l2|=((BF_LONG)(*(--(c)))); \
85 1.2.2.2 bouyer case 4: l1 =((BF_LONG)(*(--(c))))<<24L; \
86 1.2.2.2 bouyer case 3: l1|=((BF_LONG)(*(--(c))))<<16L; \
87 1.2.2.2 bouyer case 2: l1|=((BF_LONG)(*(--(c))))<< 8L; \
88 1.2.2.2 bouyer case 1: l1|=((BF_LONG)(*(--(c)))); \
89 1.2.2.2 bouyer } \
90 1.2.2.2 bouyer }
91 1.2.2.2 bouyer
92 1.2.2.2 bouyer #undef l2c
93 1.2.2.2 bouyer #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
94 1.2.2.2 bouyer *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
95 1.2.2.2 bouyer *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
96 1.2.2.2 bouyer *((c)++)=(unsigned char)(((l)>>24L)&0xff))
97 1.2.2.2 bouyer
98 1.2.2.2 bouyer /* NOTE - c is not incremented as per l2c */
99 1.2.2.2 bouyer #undef l2cn
100 1.2.2.2 bouyer #define l2cn(l1,l2,c,n) { \
101 1.2.2.2 bouyer c+=n; \
102 1.2.2.2 bouyer switch (n) { \
103 1.2.2.2 bouyer case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
104 1.2.2.2 bouyer case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
105 1.2.2.2 bouyer case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
106 1.2.2.2 bouyer case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
107 1.2.2.2 bouyer case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
108 1.2.2.2 bouyer case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
109 1.2.2.2 bouyer case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
110 1.2.2.2 bouyer case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
111 1.2.2.2 bouyer } \
112 1.2.2.2 bouyer }
113 1.2.2.2 bouyer
114 1.2.2.2 bouyer /* NOTE - c is not incremented as per n2l */
115 1.2.2.2 bouyer #define n2ln(c,l1,l2,n) { \
116 1.2.2.2 bouyer c+=n; \
117 1.2.2.2 bouyer l1=l2=0; \
118 1.2.2.2 bouyer switch (n) { \
119 1.2.2.2 bouyer case 8: l2 =((BF_LONG)(*(--(c)))) ; \
120 1.2.2.2 bouyer case 7: l2|=((BF_LONG)(*(--(c))))<< 8; \
121 1.2.2.2 bouyer case 6: l2|=((BF_LONG)(*(--(c))))<<16; \
122 1.2.2.2 bouyer case 5: l2|=((BF_LONG)(*(--(c))))<<24; \
123 1.2.2.2 bouyer case 4: l1 =((BF_LONG)(*(--(c)))) ; \
124 1.2.2.2 bouyer case 3: l1|=((BF_LONG)(*(--(c))))<< 8; \
125 1.2.2.2 bouyer case 2: l1|=((BF_LONG)(*(--(c))))<<16; \
126 1.2.2.2 bouyer case 1: l1|=((BF_LONG)(*(--(c))))<<24; \
127 1.2.2.2 bouyer } \
128 1.2.2.2 bouyer }
129 1.2.2.2 bouyer
130 1.2.2.2 bouyer /* NOTE - c is not incremented as per l2n */
131 1.2.2.2 bouyer #define l2nn(l1,l2,c,n) { \
132 1.2.2.2 bouyer c+=n; \
133 1.2.2.2 bouyer switch (n) { \
134 1.2.2.2 bouyer case 8: *(--(c))=(unsigned char)(((l2) )&0xff); \
135 1.2.2.2 bouyer case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
136 1.2.2.2 bouyer case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
137 1.2.2.2 bouyer case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
138 1.2.2.2 bouyer case 4: *(--(c))=(unsigned char)(((l1) )&0xff); \
139 1.2.2.2 bouyer case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
140 1.2.2.2 bouyer case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
141 1.2.2.2 bouyer case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
142 1.2.2.2 bouyer } \
143 1.2.2.2 bouyer }
144 1.2.2.2 bouyer
145 1.2.2.2 bouyer #undef n2l
146 1.2.2.2 bouyer #define n2l(c,l) (l =((BF_LONG)(*((c)++)))<<24L, \
147 1.2.2.2 bouyer l|=((BF_LONG)(*((c)++)))<<16L, \
148 1.2.2.2 bouyer l|=((BF_LONG)(*((c)++)))<< 8L, \
149 1.2.2.2 bouyer l|=((BF_LONG)(*((c)++))))
150 1.2.2.2 bouyer
151 1.2.2.2 bouyer #undef l2n
152 1.2.2.2 bouyer #define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
153 1.2.2.2 bouyer *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
154 1.2.2.2 bouyer *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
155 1.2.2.2 bouyer *((c)++)=(unsigned char)(((l) )&0xff))
156 1.2.2.2 bouyer
157 1.2.2.2 bouyer /* This is actually a big endian algorithm, the most significate byte
158 1.2.2.2 bouyer * is used to lookup array 0 */
159 1.2.2.2 bouyer
160 1.2.2.2 bouyer /* use BF_PTR2 for intel boxes,
161 1.2.2.2 bouyer * BF_PTR for sparc and MIPS/SGI
162 1.2.2.2 bouyer * use nothing for Alpha and HP.
163 1.2.2.2 bouyer */
164 1.2.2.2 bouyer #undef BF_PTR
165 1.2.2.2 bouyer #undef BF_PTR2
166 1.2.2.2 bouyer #ifdef __i386__
167 1.2.2.2 bouyer #define BF_PTR2
168 1.2.2.2 bouyer #else
169 1.2.2.2 bouyer #ifdef __mips__
170 1.2.2.2 bouyer #define BF_PTR
171 1.2.2.2 bouyer #endif
172 1.2.2.2 bouyer #endif
173 1.2.2.2 bouyer
174 1.2.2.2 bouyer #define BF_M 0x3fc
175 1.2.2.2 bouyer #define BF_0 22L
176 1.2.2.2 bouyer #define BF_1 14L
177 1.2.2.2 bouyer #define BF_2 6L
178 1.2.2.2 bouyer #define BF_3 2L /* left shift */
179 1.2.2.2 bouyer
180 1.2.2.2 bouyer #if defined(BF_PTR2)
181 1.2.2.2 bouyer
182 1.2.2.2 bouyer /* This is basically a special pentium verson */
183 1.2.2.2 bouyer #define BF_ENC(LL,R,S,P) \
184 1.2.2.2 bouyer { \
185 1.2.2.2 bouyer BF_LONG t,u,v; \
186 1.2.2.2 bouyer u=R>>BF_0; \
187 1.2.2.2 bouyer v=R>>BF_1; \
188 1.2.2.2 bouyer u&=BF_M; \
189 1.2.2.2 bouyer v&=BF_M; \
190 1.2.2.2 bouyer t= *(BF_LONG *)((unsigned char *)&(S[ 0])+u); \
191 1.2.2.2 bouyer u=R>>BF_2; \
192 1.2.2.2 bouyer t+= *(BF_LONG *)((unsigned char *)&(S[256])+v); \
193 1.2.2.2 bouyer v=R<<BF_3; \
194 1.2.2.2 bouyer u&=BF_M; \
195 1.2.2.2 bouyer v&=BF_M; \
196 1.2.2.2 bouyer t^= *(BF_LONG *)((unsigned char *)&(S[512])+u); \
197 1.2.2.2 bouyer LL^=P; \
198 1.2.2.2 bouyer t+= *(BF_LONG *)((unsigned char *)&(S[768])+v); \
199 1.2.2.2 bouyer LL^=t; \
200 1.2.2.2 bouyer }
201 1.2.2.2 bouyer
202 1.2.2.2 bouyer #elif defined(BF_PTR)
203 1.2.2.2 bouyer
204 1.2.2.2 bouyer /* This is normally very good */
205 1.2.2.2 bouyer
206 1.2.2.2 bouyer #define BF_ENC(LL,R,S,P) \
207 1.2.2.2 bouyer LL^=P; \
208 1.2.2.2 bouyer LL^= (((*(BF_LONG *)((unsigned char *)&(S[ 0])+((R>>BF_0)&BF_M))+ \
209 1.2.2.2 bouyer *(BF_LONG *)((unsigned char *)&(S[256])+((R>>BF_1)&BF_M)))^ \
210 1.2.2.2 bouyer *(BF_LONG *)((unsigned char *)&(S[512])+((R>>BF_2)&BF_M)))+ \
211 1.2.2.2 bouyer *(BF_LONG *)((unsigned char *)&(S[768])+((R<<BF_3)&BF_M)));
212 1.2.2.2 bouyer #else
213 1.2.2.2 bouyer
214 1.2.2.2 bouyer /* This will always work, even on 64 bit machines and strangly enough,
215 1.2.2.2 bouyer * on the Alpha it is faster than the pointer versions (both 32 and 64
216 1.2.2.2 bouyer * versions of BF_LONG) */
217 1.2.2.2 bouyer
218 1.2.2.2 bouyer #define BF_ENC(LL,R,S,P) \
219 1.2.2.2 bouyer LL^=P; \
220 1.2.2.2 bouyer LL^=((( S[ (R>>24L) ] + \
221 1.2.2.2 bouyer S[0x0100+((R>>16L)&0xff)])^ \
222 1.2.2.2 bouyer S[0x0200+((R>> 8L)&0xff)])+ \
223 1.2.2.2 bouyer S[0x0300+((R )&0xff)])&0xffffffff;
224 1.2.2.2 bouyer #endif
225