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. 8280304Sjkim * 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). 15280304Sjkim * 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. 22280304Sjkim * 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 :-). 37280304Sjkim * 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)" 40280304Sjkim * 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. 52280304Sjkim * 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 60280304Sjkim# define HEADER_BF_LOCL_H 61280304Sjkim# include <openssl/opensslconf.h>/* BF_PTR, BF_PTR2 */ 6255714Skris 63280304Sjkim# undef c2l 64280304Sjkim# define c2l(c,l) (l =((unsigned long)(*((c)++))) , \ 65280304Sjkim l|=((unsigned long)(*((c)++)))<< 8L, \ 66280304Sjkim l|=((unsigned long)(*((c)++)))<<16L, \ 67280304Sjkim l|=((unsigned long)(*((c)++)))<<24L) 6855714Skris 6955714Skris/* NOTE - c is not incremented as per c2l */ 70280304Sjkim# undef c2ln 71280304Sjkim# define c2ln(c,l1,l2,n) { \ 72280304Sjkim c+=n; \ 73280304Sjkim l1=l2=0; \ 74280304Sjkim switch (n) { \ 75280304Sjkim case 8: l2 =((unsigned long)(*(--(c))))<<24L; \ 76280304Sjkim case 7: l2|=((unsigned long)(*(--(c))))<<16L; \ 77280304Sjkim case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \ 78280304Sjkim case 5: l2|=((unsigned long)(*(--(c)))); \ 79280304Sjkim case 4: l1 =((unsigned long)(*(--(c))))<<24L; \ 80280304Sjkim case 3: l1|=((unsigned long)(*(--(c))))<<16L; \ 81280304Sjkim case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \ 82280304Sjkim case 1: l1|=((unsigned long)(*(--(c)))); \ 83280304Sjkim } \ 84280304Sjkim } 8555714Skris 86280304Sjkim# undef l2c 87280304Sjkim# define l2c(l,c) (*((c)++)=(unsigned char)(((l) )&0xff), \ 88280304Sjkim *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ 89280304Sjkim *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ 90280304Sjkim *((c)++)=(unsigned char)(((l)>>24L)&0xff)) 9155714Skris 9255714Skris/* NOTE - c is not incremented as per l2c */ 93280304Sjkim# undef l2cn 94280304Sjkim# define l2cn(l1,l2,c,n) { \ 95280304Sjkim c+=n; \ 96280304Sjkim switch (n) { \ 97280304Sjkim case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \ 98280304Sjkim case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \ 99280304Sjkim case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \ 100280304Sjkim case 5: *(--(c))=(unsigned char)(((l2) )&0xff); \ 101280304Sjkim case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \ 102280304Sjkim case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \ 103280304Sjkim case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \ 104280304Sjkim case 1: *(--(c))=(unsigned char)(((l1) )&0xff); \ 105280304Sjkim } \ 106280304Sjkim } 10755714Skris 10855714Skris/* NOTE - c is not incremented as per n2l */ 109280304Sjkim# define n2ln(c,l1,l2,n) { \ 110280304Sjkim c+=n; \ 111280304Sjkim l1=l2=0; \ 112280304Sjkim switch (n) { \ 113280304Sjkim case 8: l2 =((unsigned long)(*(--(c)))) ; \ 114280304Sjkim case 7: l2|=((unsigned long)(*(--(c))))<< 8; \ 115280304Sjkim case 6: l2|=((unsigned long)(*(--(c))))<<16; \ 116280304Sjkim case 5: l2|=((unsigned long)(*(--(c))))<<24; \ 117280304Sjkim case 4: l1 =((unsigned long)(*(--(c)))) ; \ 118280304Sjkim case 3: l1|=((unsigned long)(*(--(c))))<< 8; \ 119280304Sjkim case 2: l1|=((unsigned long)(*(--(c))))<<16; \ 120280304Sjkim case 1: l1|=((unsigned long)(*(--(c))))<<24; \ 121280304Sjkim } \ 122280304Sjkim } 12355714Skris 12455714Skris/* NOTE - c is not incremented as per l2n */ 125280304Sjkim# define l2nn(l1,l2,c,n) { \ 126280304Sjkim c+=n; \ 127280304Sjkim switch (n) { \ 128280304Sjkim case 8: *(--(c))=(unsigned char)(((l2) )&0xff); \ 129280304Sjkim case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \ 130280304Sjkim case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \ 131280304Sjkim case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \ 132280304Sjkim case 4: *(--(c))=(unsigned char)(((l1) )&0xff); \ 133280304Sjkim case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \ 134280304Sjkim case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \ 135280304Sjkim case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \ 136280304Sjkim } \ 137280304Sjkim } 13855714Skris 139280304Sjkim# undef n2l 140280304Sjkim# 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 145280304Sjkim# undef l2n 146280304Sjkim# 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 151280304Sjkim/* 152280304Sjkim * This is actually a big endian algorithm, the most significant byte is used 153280304Sjkim * to lookup array 0 154280304Sjkim */ 15555714Skris 156280304Sjkim# 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 */ 166280304Sjkim# define BF_ENC(LL,R,KEY,Pi) (\ 167280304Sjkim LL^=KEY[Pi], \ 168280304Sjkim t= KEY[BF_ROUNDS+2 + 0 + ((R>>24)&0xFF)], \ 169280304Sjkim t+= KEY[BF_ROUNDS+2 + 256 + ((R>>16)&0xFF)], \ 170280304Sjkim t^= KEY[BF_ROUNDS+2 + 512 + ((R>>8 )&0xFF)], \ 171280304Sjkim t+= KEY[BF_ROUNDS+2 + 768 + ((R )&0xFF)], \ 172280304Sjkim LL^=t \ 173280304Sjkim ) 17455714Skris 175280304Sjkim# elif defined(BF_PTR) 17655714Skris 177280304Sjkim# ifndef BF_LONG_LOG2 178280304Sjkim# define BF_LONG_LOG2 2 /* default to BF_LONG being 32 bits */ 179280304Sjkim# endif 180280304Sjkim# define BF_M (0xFF<<BF_LONG_LOG2) 181280304Sjkim# define BF_0 (24-BF_LONG_LOG2) 182280304Sjkim# define BF_1 (16-BF_LONG_LOG2) 183280304Sjkim# define BF_2 ( 8-BF_LONG_LOG2) 184280304Sjkim# 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 195280304Sjkim# define BF_ENC(LL,R,S,P) ( \ 196280304Sjkim LL^=P, \ 197280304Sjkim LL^= (((*(BF_LONG *)((unsigned char *)&(S[ 0])+((R>>BF_0)&BF_M))+ \ 198280304Sjkim *(BF_LONG *)((unsigned char *)&(S[256])+((R>>BF_1)&BF_M)))^ \ 199280304Sjkim *(BF_LONG *)((unsigned char *)&(S[512])+((R>>BF_2)&BF_M)))+ \ 200280304Sjkim *(BF_LONG *)((unsigned char *)&(S[768])+((R<<BF_3)&BF_M))) \ 201280304Sjkim ) 202280304Sjkim# 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 212280304Sjkim# define BF_ENC(LL,R,S,P) ( \ 213280304Sjkim LL^=P, \ 214280304Sjkim LL^=((( S[ ((int)(R>>24)&0xff)] + \ 215280304Sjkim S[0x0100+((int)(R>>16)&0xff)])^ \ 216280304Sjkim S[0x0200+((int)(R>> 8)&0xff)])+ \ 217280304Sjkim S[0x0300+((int)(R )&0xff)])&0xffffffffL \ 218280304Sjkim ) 219280304Sjkim# endif 22055714Skris 22155714Skris#endif 222