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