1 /* Modified to work without OPENSSL.
2 * os_crypto (www.ossec.net/c/os_crypto)
3 * Daniel B. Cid, danielcid@gmail.com
6 /* crypto/bf/bf_locl.h */
7 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
10 * This package is an SSL implementation written
11 * by Eric Young (eay@cryptsoft.com).
12 * The implementation was written so as to conform with Netscapes SSL.
14 * This library is free for commercial and non-commercial use as long as
15 * the following conditions are aheared to. The following conditions
16 * apply to all code found in this distribution, be it the RC4, RSA,
17 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
18 * included with this distribution is covered by the same copyright terms
19 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
21 * Copyright remains Eric Young's, and as such any Copyright notices in
22 * the code are not to be removed.
23 * If this package is used in a product, Eric Young should be given attribution
24 * as the author of the parts of the library used.
25 * This can be in the form of a textual message at program startup or
26 * in documentation (online or textual) provided with the package.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * "This product includes cryptographic software written by
39 * Eric Young (eay@cryptsoft.com)"
40 * The word 'cryptographic' can be left out if the rouines from the library
41 * being used are not cryptographic related :-).
42 * 4. If you include any Windows specific code (or a derivative thereof) from
43 * the apps directory (application code) you must include an acknowledgement:
44 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
46 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * The licence and distribution terms for any publically available version or
59 * derivative of this code cannot be changed. i.e. this code cannot simply be
60 * copied and put under another distribution licence
61 * [including the GNU Public Licence.]
64 #ifndef HEADER_BF_LOCL_H
65 #define HEADER_BF_LOCL_H
67 /* Only include if OPENSSL is present */
69 #include <openssl/opensslconf.h> /* BF_PTR, BF_PTR2 */
73 #define c2l(c,l) (l =((unsigned long)(*((c)++))) , \
74 l|=((unsigned long)(*((c)++)))<< 8L, \
75 l|=((unsigned long)(*((c)++)))<<16L, \
76 l|=((unsigned long)(*((c)++)))<<24L)
78 /* NOTE - c is not incremented as per c2l */
80 #define c2ln(c,l1,l2,n) { \
84 case 8: l2 =((unsigned long)(*(--(c))))<<24L; \
85 case 7: l2|=((unsigned long)(*(--(c))))<<16L; \
86 case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \
87 case 5: l2|=((unsigned long)(*(--(c)))); \
88 case 4: l1 =((unsigned long)(*(--(c))))<<24L; \
89 case 3: l1|=((unsigned long)(*(--(c))))<<16L; \
90 case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \
91 case 1: l1|=((unsigned long)(*(--(c)))); \
96 #define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \
97 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
98 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
99 *((c)++)=(unsigned char)(((l)>>24L)&0xff))
101 /* NOTE - c is not incremented as per l2c */
103 #define l2cn(l1,l2,c,n) { \
106 case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
107 case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
108 case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
109 case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \
110 case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
111 case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
112 case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
113 case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \
117 /* NOTE - c is not incremented as per n2l */
118 #define n2ln(c,l1,l2,n) { \
122 case 8: l2 =((unsigned long)(*(--(c)))) ; \
123 case 7: l2|=((unsigned long)(*(--(c))))<< 8; \
124 case 6: l2|=((unsigned long)(*(--(c))))<<16; \
125 case 5: l2|=((unsigned long)(*(--(c))))<<24; \
126 case 4: l1 =((unsigned long)(*(--(c)))) ; \
127 case 3: l1|=((unsigned long)(*(--(c))))<< 8; \
128 case 2: l1|=((unsigned long)(*(--(c))))<<16; \
129 case 1: l1|=((unsigned long)(*(--(c))))<<24; \
133 /* NOTE - c is not incremented as per l2n */
134 #define l2nn(l1,l2,c,n) { \
137 case 8: *(--(c))=(unsigned char)(((l2) )&0xff); \
138 case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
139 case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
140 case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
141 case 4: *(--(c))=(unsigned char)(((l1) )&0xff); \
142 case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
143 case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
144 case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
149 #define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24L, \
150 l|=((unsigned long)(*((c)++)))<<16L, \
151 l|=((unsigned long)(*((c)++)))<< 8L, \
152 l|=((unsigned long)(*((c)++))))
155 #define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
156 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
157 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
158 *((c)++)=(unsigned char)(((l) )&0xff))
160 /* This is actually a big endian algorithm, the most significant byte
161 * is used to lookup array 0 */
166 * This is basically a special Intel version. Point is that Intel
167 * doesn't have many registers, but offers a reach choice of addressing
168 * modes. So we spare some registers by directly traversing BF_KEY
169 * structure and hiring the most decorated addressing mode. The code
170 * generated by EGCS is *perfectly* competitive with assembler
173 #define BF_ENC(LL,R,KEY,Pi) (\
175 t= KEY[BF_ROUNDS+2 + 0 + ((R>>24)&0xFF)], \
176 t+= KEY[BF_ROUNDS+2 + 256 + ((R>>16)&0xFF)], \
177 t^= KEY[BF_ROUNDS+2 + 512 + ((R>>8 )&0xFF)], \
178 t+= KEY[BF_ROUNDS+2 + 768 + ((R )&0xFF)], \
182 #elif defined(BF_PTR)
185 #define BF_LONG_LOG2 2 /* default to BF_LONG being 32 bits */
187 #define BF_M (0xFF<<BF_LONG_LOG2)
188 #define BF_0 (24-BF_LONG_LOG2)
189 #define BF_1 (16-BF_LONG_LOG2)
190 #define BF_2 ( 8-BF_LONG_LOG2)
191 #define BF_3 BF_LONG_LOG2 /* left shift */
194 * This is normally very good on RISC platforms where normally you
195 * have to explicitly "multiply" array index by sizeof(BF_LONG)
196 * in order to calculate the effective address. This implementation
197 * excuses CPU from this extra work. Power[PC] uses should have most
198 * fun as (R>>BF_i)&BF_M gets folded into a single instruction, namely
199 * rlwinm. So let'em double-check if their compiler does it.
202 #define BF_ENC(LL,R,S,P) ( \
204 LL^= (((*(BF_LONG *)((unsigned char *)&(S[ 0])+((R>>BF_0)&BF_M))+ \
205 *(BF_LONG *)((unsigned char *)&(S[256])+((R>>BF_1)&BF_M)))^ \
206 *(BF_LONG *)((unsigned char *)&(S[512])+((R>>BF_2)&BF_M)))+ \
207 *(BF_LONG *)((unsigned char *)&(S[768])+((R<<BF_3)&BF_M))) \
212 * This is a *generic* version. Seem to perform best on platforms that
213 * offer explicit support for extraction of 8-bit nibbles preferably
214 * complemented with "multiplying" of array index by sizeof(BF_LONG).
215 * For the moment of this writing the list comprises Alpha CPU featuring
216 * extbl and s[48]addq instructions.
219 #define BF_ENC(LL,R,S,P) ( \
221 LL^=((( S[ ((int)(R>>24)&0xff)] + \
222 S[0x0100+((int)(R>>16)&0xff)])^ \
223 S[0x0200+((int)(R>> 8)&0xff)])+ \
224 S[0x0300+((int)(R )&0xff)])&0xffffffffL \