155714Skris/* crypto/bf/bf_locl.h */ 255714Skris/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8296341Sdelphij * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15296341Sdelphij * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22296341Sdelphij * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37296341Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40296341Sdelphij * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52296341Sdelphij * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#ifndef HEADER_BF_LOCL_H 60296341Sdelphij# define HEADER_BF_LOCL_H 61296341Sdelphij# include <openssl/opensslconf.h>/* BF_PTR, BF_PTR2 */ 6255714Skris 63296341Sdelphij# undef c2l 64296341Sdelphij# define c2l(c,l) (l =((unsigned long)(*((c)++))) , \ 65296341Sdelphij l|=((unsigned long)(*((c)++)))<< 8L, \ 66296341Sdelphij l|=((unsigned long)(*((c)++)))<<16L, \ 67296341Sdelphij l|=((unsigned long)(*((c)++)))<<24L) 6855714Skris 6955714Skris/* NOTE - c is not incremented as per c2l */ 70296341Sdelphij# undef c2ln 71296341Sdelphij# define c2ln(c,l1,l2,n) { \ 72296341Sdelphij c+=n; \ 73296341Sdelphij l1=l2=0; \ 74296341Sdelphij switch (n) { \ 75296341Sdelphij case 8: l2 =((unsigned long)(*(--(c))))<<24L; \ 76296341Sdelphij case 7: l2|=((unsigned long)(*(--(c))))<<16L; \ 77296341Sdelphij case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \ 78296341Sdelphij case 5: l2|=((unsigned long)(*(--(c)))); \ 79296341Sdelphij case 4: l1 =((unsigned long)(*(--(c))))<<24L; \ 80296341Sdelphij case 3: l1|=((unsigned long)(*(--(c))))<<16L; \ 81296341Sdelphij case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \ 82296341Sdelphij case 1: l1|=((unsigned long)(*(--(c)))); \ 83296341Sdelphij } \ 84296341Sdelphij } 8555714Skris 86296341Sdelphij# undef l2c 87296341Sdelphij# define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ 88296341Sdelphij *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ 89296341Sdelphij *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ 90296341Sdelphij *((c)++)=(unsigned char)(((l)>>24L)&0xff)) 9155714Skris 9255714Skris/* NOTE - c is not incremented as per l2c */ 93296341Sdelphij# undef l2cn 94296341Sdelphij# define l2cn(l1,l2,c,n) { \ 95296341Sdelphij c+=n; \ 96296341Sdelphij switch (n) { \ 97296341Sdelphij case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \ 98296341Sdelphij case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \ 99296341Sdelphij case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \ 100296341Sdelphij case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \ 101296341Sdelphij case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \ 102296341Sdelphij case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \ 103296341Sdelphij case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \ 104296341Sdelphij case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \ 105296341Sdelphij } \ 106296341Sdelphij } 10755714Skris 10855714Skris/* NOTE - c is not incremented as per n2l */ 109296341Sdelphij# define n2ln(c,l1,l2,n) { \ 110296341Sdelphij c+=n; \ 111296341Sdelphij l1=l2=0; \ 112296341Sdelphij switch (n) { \ 113296341Sdelphij case 8: l2 =((unsigned long)(*(--(c)))) ; \ 114296341Sdelphij case 7: l2|=((unsigned long)(*(--(c))))<< 8; \ 115296341Sdelphij case 6: l2|=((unsigned long)(*(--(c))))<<16; \ 116296341Sdelphij case 5: l2|=((unsigned long)(*(--(c))))<<24; \ 117296341Sdelphij case 4: l1 =((unsigned long)(*(--(c)))) ; \ 118296341Sdelphij case 3: l1|=((unsigned long)(*(--(c))))<< 8; \ 119296341Sdelphij case 2: l1|=((unsigned long)(*(--(c))))<<16; \ 120296341Sdelphij case 1: l1|=((unsigned long)(*(--(c))))<<24; \ 121296341Sdelphij } \ 122296341Sdelphij } 12355714Skris 12455714Skris/* NOTE - c is not incremented as per l2n */ 125296341Sdelphij# define l2nn(l1,l2,c,n) { \ 126296341Sdelphij c+=n; \ 127296341Sdelphij switch (n) { \ 128296341Sdelphij case 8: *(--(c))=(unsigned char)(((l2) )&0xff); \ 129296341Sdelphij case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \ 130296341Sdelphij case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \ 131296341Sdelphij case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \ 132296341Sdelphij case 4: *(--(c))=(unsigned char)(((l1) )&0xff); \ 133296341Sdelphij case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \ 134296341Sdelphij case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \ 135296341Sdelphij case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \ 136296341Sdelphij } \ 137296341Sdelphij } 13855714Skris 139296341Sdelphij# undef n2l 140296341Sdelphij# define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24L, \ 14155714Skris l|=((unsigned long)(*((c)++)))<<16L, \ 14255714Skris l|=((unsigned long)(*((c)++)))<< 8L, \ 14355714Skris l|=((unsigned long)(*((c)++)))) 14455714Skris 145296341Sdelphij# undef l2n 146296341Sdelphij# define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \ 14755714Skris *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ 14855714Skris *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ 14955714Skris *((c)++)=(unsigned char)(((l) )&0xff)) 15055714Skris 151296341Sdelphij/* 152296341Sdelphij * This is actually a big endian algorithm, the most significant byte is used 153296341Sdelphij * to lookup array 0 154296341Sdelphij */ 15555714Skris 156296341Sdelphij# if defined(BF_PTR2) 15755714Skris 15855714Skris/* 15955714Skris * This is basically a special Intel version. Point is that Intel 16055714Skris * doesn't have many registers, but offers a reach choice of addressing 16155714Skris * modes. So we spare some registers by directly traversing BF_KEY 16255714Skris * structure and hiring the most decorated addressing mode. The code 16355714Skris * generated by EGCS is *perfectly* competitive with assembler 16455714Skris * implementation! 16555714Skris */ 166296341Sdelphij# define BF_ENC(LL,R,KEY,Pi) (\ 167296341Sdelphij LL^=KEY[Pi], \ 168296341Sdelphij t= KEY[BF_ROUNDS+2 + 0 + ((R>>24)&0xFF)], \ 169296341Sdelphij t+= KEY[BF_ROUNDS+2 + 256 + ((R>>16)&0xFF)], \ 170296341Sdelphij t^= KEY[BF_ROUNDS+2 + 512 + ((R>>8 )&0xFF)], \ 171296341Sdelphij t+= KEY[BF_ROUNDS+2 + 768 + ((R )&0xFF)], \ 172296341Sdelphij LL^=t \ 173296341Sdelphij ) 17455714Skris 175296341Sdelphij# elif defined(BF_PTR) 17655714Skris 177296341Sdelphij# ifndef BF_LONG_LOG2 178296341Sdelphij# define BF_LONG_LOG2 2 /* default to BF_LONG being 32 bits */ 179296341Sdelphij# endif 180296341Sdelphij# define BF_M (0xFF<<BF_LONG_LOG2) 181296341Sdelphij# define BF_0 (24-BF_LONG_LOG2) 182296341Sdelphij# define BF_1 (16-BF_LONG_LOG2) 183296341Sdelphij# define BF_2 ( 8-BF_LONG_LOG2) 184296341Sdelphij# define BF_3 BF_LONG_LOG2 /* left shift */ 18555714Skris 18655714Skris/* 18755714Skris * This is normally very good on RISC platforms where normally you 18859191Skris * have to explicitly "multiply" array index by sizeof(BF_LONG) 18959191Skris * in order to calculate the effective address. This implementation 19055714Skris * excuses CPU from this extra work. Power[PC] uses should have most 19155714Skris * fun as (R>>BF_i)&BF_M gets folded into a single instruction, namely 19255714Skris * rlwinm. So let'em double-check if their compiler does it. 19355714Skris */ 19455714Skris 195296341Sdelphij# define BF_ENC(LL,R,S,P) ( \ 196296341Sdelphij LL^=P, \ 197296341Sdelphij LL^= (((*(BF_LONG *)((unsigned char *)&(S[ 0])+((R>>BF_0)&BF_M))+ \ 198296341Sdelphij *(BF_LONG *)((unsigned char *)&(S[256])+((R>>BF_1)&BF_M)))^ \ 199296341Sdelphij *(BF_LONG *)((unsigned char *)&(S[512])+((R>>BF_2)&BF_M)))+ \ 200296341Sdelphij *(BF_LONG *)((unsigned char *)&(S[768])+((R<<BF_3)&BF_M))) \ 201296341Sdelphij ) 202296341Sdelphij# else 20355714Skris 20455714Skris/* 20555714Skris * This is a *generic* version. Seem to perform best on platforms that 20655714Skris * offer explicit support for extraction of 8-bit nibbles preferably 20755714Skris * complemented with "multiplying" of array index by sizeof(BF_LONG). 20855714Skris * For the moment of this writing the list comprises Alpha CPU featuring 20955714Skris * extbl and s[48]addq instructions. 21055714Skris */ 21155714Skris 212296341Sdelphij# define BF_ENC(LL,R,S,P) ( \ 213296341Sdelphij LL^=P, \ 214296341Sdelphij LL^=((( S[ ((int)(R>>24)&0xff)] + \ 215296341Sdelphij S[0x0100+((int)(R>>16)&0xff)])^ \ 216296341Sdelphij S[0x0200+((int)(R>> 8)&0xff)])+ \ 217296341Sdelphij S[0x0300+((int)(R )&0xff)])&0xffffffffL \ 218296341Sdelphij ) 219296341Sdelphij# endif 22055714Skris 22155714Skris#endif 222