162587Sitojun/* $FreeBSD$ */ 291671Sume/* $KAME: bf_locl.h,v 1.6 2001/09/10 04:03:56 itojun Exp $ */ 362587Sitojun 455009Sshin/* crypto/bf/bf_local.h */ 555009Sshin/* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au) 655009Sshin * All rights reserved. 755009Sshin * 855009Sshin * This package is an SSL implementation written 955009Sshin * by Eric Young (eay@mincom.oz.au). 1055009Sshin * The implementation was written so as to conform with Netscapes SSL. 1155009Sshin * 1255009Sshin * This library is free for commercial and non-commercial use as long as 1355009Sshin * the following conditions are aheared to. The following conditions 1455009Sshin * apply to all code found in this distribution, be it the RC4, RSA, 1555009Sshin * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1655009Sshin * included with this distribution is covered by the same copyright terms 1755009Sshin * except that the holder is Tim Hudson (tjh@mincom.oz.au). 1855009Sshin * 1955009Sshin * Copyright remains Eric Young's, and as such any Copyright notices in 2055009Sshin * the code are not to be removed. 2155009Sshin * If this package is used in a product, Eric Young should be given attribution 2255009Sshin * as the author of the parts of the library used. 2355009Sshin * This can be in the form of a textual message at program startup or 2455009Sshin * in documentation (online or textual) provided with the package. 2555009Sshin * 2655009Sshin * Redistribution and use in source and binary forms, with or without 2755009Sshin * modification, are permitted provided that the following conditions 2855009Sshin * are met: 2955009Sshin * 1. Redistributions of source code must retain the copyright 3055009Sshin * notice, this list of conditions and the following disclaimer. 3155009Sshin * 2. Redistributions in binary form must reproduce the above copyright 3255009Sshin * notice, this list of conditions and the following disclaimer in the 3355009Sshin * documentation and/or other materials provided with the distribution. 3455009Sshin * 3. All advertising materials mentioning features or use of this software 3555009Sshin * must display the following acknowledgement: 3655009Sshin * "This product includes cryptographic software written by 3755009Sshin * Eric Young (eay@mincom.oz.au)" 3855009Sshin * The word 'cryptographic' can be left out if the rouines from the library 3955009Sshin * being used are not cryptographic related :-). 4055009Sshin * 4. If you include any Windows specific code (or a derivative thereof) from 4155009Sshin * the apps directory (application code) you must include an acknowledgement: 4255009Sshin * "This product includes software written by Tim Hudson (tjh@mincom.oz.au)" 4355009Sshin * 4455009Sshin * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4555009Sshin * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4655009Sshin * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4755009Sshin * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4855009Sshin * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4955009Sshin * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 5055009Sshin * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 5155009Sshin * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 5255009Sshin * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5355009Sshin * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5455009Sshin * SUCH DAMAGE. 5555009Sshin * 5655009Sshin * The licence and distribution terms for any publically available version or 5755009Sshin * derivative of this code cannot be changed. i.e. this code cannot simply be 5855009Sshin * copied and put under another distribution licence 5955009Sshin * [including the GNU Public Licence.] 6055009Sshin */ 6155009Sshin/* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 6255009Sshin * 6355009Sshin * Always modify bf_locl.org since bf_locl.h is automatically generated from 6455009Sshin * it during SSLeay configuration. 6555009Sshin * 6655009Sshin * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING 6755009Sshin */ 6855009Sshin 6955009Sshin#undef c2l 7078064Sume#define c2l(c,l) (l =((BF_LONG)(*((c)++))) , \ 7178064Sume l|=((BF_LONG)(*((c)++)))<< 8L, \ 7278064Sume l|=((BF_LONG)(*((c)++)))<<16L, \ 7378064Sume l|=((BF_LONG)(*((c)++)))<<24L) 7455009Sshin 7555009Sshin/* NOTE - c is not incremented as per c2l */ 7655009Sshin#undef c2ln 7762587Sitojun#define c2ln(c,l1,l2,n) { \ 7855009Sshin c+=n; \ 7955009Sshin l1=l2=0; \ 8055009Sshin switch (n) { \ 8178064Sume case 8: l2 =((BF_LONG)(*(--(c))))<<24L; \ 8278064Sume case 7: l2|=((BF_LONG)(*(--(c))))<<16L; \ 8378064Sume case 6: l2|=((BF_LONG)(*(--(c))))<< 8L; \ 8478064Sume case 5: l2|=((BF_LONG)(*(--(c)))); \ 8578064Sume case 4: l1 =((BF_LONG)(*(--(c))))<<24L; \ 8678064Sume case 3: l1|=((BF_LONG)(*(--(c))))<<16L; \ 8778064Sume case 2: l1|=((BF_LONG)(*(--(c))))<< 8L; \ 8878064Sume case 1: l1|=((BF_LONG)(*(--(c)))); \ 8955009Sshin } \ 9055009Sshin } 9155009Sshin 9255009Sshin#undef l2c 9362587Sitojun#define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ 9455009Sshin *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ 9555009Sshin *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ 9655009Sshin *((c)++)=(unsigned char)(((l)>>24L)&0xff)) 9755009Sshin 9855009Sshin/* NOTE - c is not incremented as per l2c */ 9955009Sshin#undef l2cn 10062587Sitojun#define l2cn(l1,l2,c,n) { \ 10155009Sshin c+=n; \ 10255009Sshin switch (n) { \ 10355009Sshin case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \ 10455009Sshin case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \ 10555009Sshin case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \ 10655009Sshin case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \ 10755009Sshin case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \ 10855009Sshin case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \ 10955009Sshin case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \ 11055009Sshin case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \ 11155009Sshin } \ 11255009Sshin } 11355009Sshin 11455009Sshin/* NOTE - c is not incremented as per n2l */ 11562587Sitojun#define n2ln(c,l1,l2,n) { \ 11655009Sshin c+=n; \ 11755009Sshin l1=l2=0; \ 11855009Sshin switch (n) { \ 11978064Sume case 8: l2 =((BF_LONG)(*(--(c)))) ; \ 12078064Sume case 7: l2|=((BF_LONG)(*(--(c))))<< 8; \ 12178064Sume case 6: l2|=((BF_LONG)(*(--(c))))<<16; \ 12278064Sume case 5: l2|=((BF_LONG)(*(--(c))))<<24; \ 12378064Sume case 4: l1 =((BF_LONG)(*(--(c)))) ; \ 12478064Sume case 3: l1|=((BF_LONG)(*(--(c))))<< 8; \ 12578064Sume case 2: l1|=((BF_LONG)(*(--(c))))<<16; \ 12678064Sume case 1: l1|=((BF_LONG)(*(--(c))))<<24; \ 12755009Sshin } \ 12855009Sshin } 12955009Sshin 13055009Sshin/* NOTE - c is not incremented as per l2n */ 13162587Sitojun#define l2nn(l1,l2,c,n) { \ 13255009Sshin c+=n; \ 13355009Sshin switch (n) { \ 13455009Sshin case 8: *(--(c))=(unsigned char)(((l2) )&0xff); \ 13555009Sshin case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \ 13655009Sshin case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \ 13755009Sshin case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \ 13855009Sshin case 4: *(--(c))=(unsigned char)(((l1) )&0xff); \ 13955009Sshin case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \ 14055009Sshin case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \ 14155009Sshin case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \ 14255009Sshin } \ 14355009Sshin } 14455009Sshin 14555009Sshin#undef n2l 14678064Sume#define n2l(c,l) (l =((BF_LONG)(*((c)++)))<<24L, \ 14778064Sume l|=((BF_LONG)(*((c)++)))<<16L, \ 14878064Sume l|=((BF_LONG)(*((c)++)))<< 8L, \ 14978064Sume l|=((BF_LONG)(*((c)++)))) 15055009Sshin 15155009Sshin#undef l2n 15262587Sitojun#define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \ 15355009Sshin *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ 15455009Sshin *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ 15555009Sshin *((c)++)=(unsigned char)(((l) )&0xff)) 15655009Sshin 15755009Sshin/* This is actually a big endian algorithm, the most significate byte 15855009Sshin * is used to lookup array 0 */ 15955009Sshin 16055009Sshin/* use BF_PTR2 for intel boxes, 16155009Sshin * BF_PTR for sparc and MIPS/SGI 16255009Sshin * use nothing for Alpha and HP. 16355009Sshin */ 16478064Sume#undef BF_PTR 16578064Sume#undef BF_PTR2 16678064Sume#ifdef __i386__ 16778064Sume#define BF_PTR2 16878064Sume#else 16978064Sume#ifdef __mips__ 17078064Sume#define BF_PTR 17155009Sshin#endif 17278064Sume#endif 17355009Sshin 17462587Sitojun#define BF_M 0x3fc 17562587Sitojun#define BF_0 22L 17662587Sitojun#define BF_1 14L 17762587Sitojun#define BF_2 6L 17862587Sitojun#define BF_3 2L /* left shift */ 17955009Sshin 18055009Sshin#if defined(BF_PTR2) 18155009Sshin 18255009Sshin/* This is basically a special pentium verson */ 18362587Sitojun#define BF_ENC(LL,R,S,P) \ 18455009Sshin { \ 18555009Sshin BF_LONG t,u,v; \ 18655009Sshin u=R>>BF_0; \ 18755009Sshin v=R>>BF_1; \ 18855009Sshin u&=BF_M; \ 18955009Sshin v&=BF_M; \ 19055009Sshin t= *(BF_LONG *)((unsigned char *)&(S[ 0])+u); \ 19155009Sshin u=R>>BF_2; \ 19255009Sshin t+= *(BF_LONG *)((unsigned char *)&(S[256])+v); \ 19355009Sshin v=R<<BF_3; \ 19455009Sshin u&=BF_M; \ 19555009Sshin v&=BF_M; \ 19655009Sshin t^= *(BF_LONG *)((unsigned char *)&(S[512])+u); \ 19755009Sshin LL^=P; \ 19855009Sshin t+= *(BF_LONG *)((unsigned char *)&(S[768])+v); \ 19955009Sshin LL^=t; \ 20055009Sshin } 20155009Sshin 20255009Sshin#elif defined(BF_PTR) 20355009Sshin 20455009Sshin/* This is normally very good */ 20555009Sshin 20662587Sitojun#define BF_ENC(LL,R,S,P) \ 20755009Sshin LL^=P; \ 20855009Sshin LL^= (((*(BF_LONG *)((unsigned char *)&(S[ 0])+((R>>BF_0)&BF_M))+ \ 20955009Sshin *(BF_LONG *)((unsigned char *)&(S[256])+((R>>BF_1)&BF_M)))^ \ 21055009Sshin *(BF_LONG *)((unsigned char *)&(S[512])+((R>>BF_2)&BF_M)))+ \ 21155009Sshin *(BF_LONG *)((unsigned char *)&(S[768])+((R<<BF_3)&BF_M))); 21255009Sshin#else 21355009Sshin 21455009Sshin/* This will always work, even on 64 bit machines and strangly enough, 21555009Sshin * on the Alpha it is faster than the pointer versions (both 32 and 64 21655009Sshin * versions of BF_LONG) */ 21755009Sshin 21862587Sitojun#define BF_ENC(LL,R,S,P) \ 21955009Sshin LL^=P; \ 22055009Sshin LL^=((( S[ (R>>24L) ] + \ 22155009Sshin S[0x0100+((R>>16L)&0xff)])^ \ 22255009Sshin S[0x0200+((R>> 8L)&0xff)])+ \ 22355009Sshin S[0x0300+((R )&0xff)])&0xffffffff; 22455009Sshin#endif 225