Home | History | Annotate | Line # | Download | only in blowfish
      1  1.8   andvar /*	$NetBSD: bf_locl.h,v 1.8 2022/04/16 18:15:21 andvar Exp $	*/
      2  1.2   itojun /*	$KAME: bf_locl.h,v 1.5 2000/08/31 06:03:48 itojun Exp $	*/
      3  1.1  thorpej 
      4  1.1  thorpej /* crypto/bf/bf_local.h */
      5  1.1  thorpej /* Copyright (C) 1995-1997 Eric Young (eay (at) mincom.oz.au)
      6  1.1  thorpej  * All rights reserved.
      7  1.1  thorpej  *
      8  1.1  thorpej  * This package is an SSL implementation written
      9  1.1  thorpej  * by Eric Young (eay (at) mincom.oz.au).
     10  1.1  thorpej  * The implementation was written so as to conform with Netscapes SSL.
     11  1.1  thorpej  *
     12  1.1  thorpej  * This library is free for commercial and non-commercial use as long as
     13  1.1  thorpej  * the following conditions are aheared to.  The following conditions
     14  1.1  thorpej  * apply to all code found in this distribution, be it the RC4, RSA,
     15  1.1  thorpej  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
     16  1.1  thorpej  * included with this distribution is covered by the same copyright terms
     17  1.1  thorpej  * except that the holder is Tim Hudson (tjh (at) mincom.oz.au).
     18  1.1  thorpej  *
     19  1.1  thorpej  * Copyright remains Eric Young's, and as such any Copyright notices in
     20  1.1  thorpej  * the code are not to be removed.
     21  1.1  thorpej  * If this package is used in a product, Eric Young should be given attribution
     22  1.1  thorpej  * as the author of the parts of the library used.
     23  1.1  thorpej  * This can be in the form of a textual message at program startup or
     24  1.1  thorpej  * in documentation (online or textual) provided with the package.
     25  1.1  thorpej  *
     26  1.1  thorpej  * Redistribution and use in source and binary forms, with or without
     27  1.1  thorpej  * modification, are permitted provided that the following conditions
     28  1.1  thorpej  * are met:
     29  1.1  thorpej  * 1. Redistributions of source code must retain the copyright
     30  1.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     31  1.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     32  1.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     33  1.1  thorpej  *    documentation and/or other materials provided with the distribution.
     34  1.1  thorpej  * 3. All advertising materials mentioning features or use of this software
     35  1.1  thorpej  *    must display the following acknowledgement:
     36  1.1  thorpej  *    "This product includes cryptographic software written by
     37  1.1  thorpej  *     Eric Young (eay (at) mincom.oz.au)"
     38  1.1  thorpej  *    The word 'cryptographic' can be left out if the rouines from the library
     39  1.1  thorpej  *    being used are not cryptographic related :-).
     40  1.1  thorpej  * 4. If you include any Windows specific code (or a derivative thereof) from
     41  1.1  thorpej  *    the apps directory (application code) you must include an acknowledgement:
     42  1.1  thorpej  *    "This product includes software written by Tim Hudson (tjh (at) mincom.oz.au)"
     43  1.1  thorpej  *
     44  1.1  thorpej  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
     45  1.1  thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     46  1.1  thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     47  1.1  thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     48  1.1  thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     49  1.1  thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     50  1.1  thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     51  1.1  thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     52  1.1  thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     53  1.1  thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     54  1.1  thorpej  * SUCH DAMAGE.
     55  1.1  thorpej  *
     56  1.1  thorpej  * The licence and distribution terms for any publically available version or
     57  1.1  thorpej  * derivative of this code cannot be changed.  i.e. this code cannot simply be
     58  1.1  thorpej  * copied and put under another distribution licence
     59  1.1  thorpej  * [including the GNU Public Licence.]
     60  1.1  thorpej  */
     61  1.1  thorpej /* WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
     62  1.1  thorpej  *
     63  1.1  thorpej  * Always modify bf_locl.org since bf_locl.h is automatically generated from
     64  1.1  thorpej  * it during SSLeay configuration.
     65  1.1  thorpej  *
     66  1.1  thorpej  * WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING
     67  1.1  thorpej  */
     68  1.1  thorpej 
     69  1.1  thorpej #undef c2l
     70  1.2   itojun #define c2l(c,l)	(l =((BF_LONG)(*((c)++)))    , \
     71  1.2   itojun 			 l|=((BF_LONG)(*((c)++)))<< 8L, \
     72  1.2   itojun 			 l|=((BF_LONG)(*((c)++)))<<16L, \
     73  1.2   itojun 			 l|=((BF_LONG)(*((c)++)))<<24L)
     74  1.1  thorpej 
     75  1.1  thorpej /* NOTE - c is not incremented as per c2l */
     76  1.1  thorpej #undef c2ln
     77  1.1  thorpej #define c2ln(c,l1,l2,n)	{ \
     78  1.1  thorpej 			c+=n; \
     79  1.1  thorpej 			l1=l2=0; \
     80  1.1  thorpej 			switch (n) { \
     81  1.6      mrg 			case 8: l2 =((BF_LONG)(*(--(c))))<<24L; /* FALLTHROUGH */ \
     82  1.6      mrg 			case 7: l2|=((BF_LONG)(*(--(c))))<<16L; /* FALLTHROUGH */ \
     83  1.6      mrg 			case 6: l2|=((BF_LONG)(*(--(c))))<< 8L; /* FALLTHROUGH */ \
     84  1.6      mrg 			case 5: l2|=((BF_LONG)(*(--(c))));     /* FALLTHROUGH */ \
     85  1.6      mrg 			case 4: l1 =((BF_LONG)(*(--(c))))<<24L; /* FALLTHROUGH */ \
     86  1.6      mrg 			case 3: l1|=((BF_LONG)(*(--(c))))<<16L; /* FALLTHROUGH */ \
     87  1.6      mrg 			case 2: l1|=((BF_LONG)(*(--(c))))<< 8L; /* FALLTHROUGH */ \
     88  1.6      mrg 			case 1: l1|=((BF_LONG)(*(--(c))));     /* FALLTHROUGH */ \
     89  1.1  thorpej 				} \
     90  1.1  thorpej 			}
     91  1.1  thorpej 
     92  1.1  thorpej #undef l2c
     93  1.1  thorpej #define l2c(l,c)	(*((c)++)=(unsigned char)(((l)     )&0xff), \
     94  1.1  thorpej 			 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
     95  1.1  thorpej 			 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
     96  1.1  thorpej 			 *((c)++)=(unsigned char)(((l)>>24L)&0xff))
     97  1.1  thorpej 
     98  1.1  thorpej /* NOTE - c is not incremented as per l2c */
     99  1.1  thorpej #undef l2cn
    100  1.1  thorpej #define l2cn(l1,l2,c,n)	{ \
    101  1.1  thorpej 			c+=n; \
    102  1.1  thorpej 			switch (n) { \
    103  1.6      mrg 			case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); /* FALLTHROUGH */ \
    104  1.6      mrg 			case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); /* FALLTHROUGH */ \
    105  1.6      mrg 			case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); /* FALLTHROUGH */ \
    106  1.6      mrg 			case 5: *(--(c))=(unsigned char)(((l2)     )&0xff); /* FALLTHROUGH */ \
    107  1.6      mrg 			case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); /* FALLTHROUGH */ \
    108  1.6      mrg 			case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); /* FALLTHROUGH */ \
    109  1.6      mrg 			case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); /* FALLTHROUGH */ \
    110  1.6      mrg 			case 1: *(--(c))=(unsigned char)(((l1)     )&0xff); /* FALLTHROUGH */ \
    111  1.1  thorpej 				} \
    112  1.1  thorpej 			}
    113  1.1  thorpej 
    114  1.1  thorpej /* NOTE - c is not incremented as per n2l */
    115  1.1  thorpej #define n2ln(c,l1,l2,n)	{ \
    116  1.1  thorpej 			c+=n; \
    117  1.1  thorpej 			l1=l2=0; \
    118  1.1  thorpej 			switch (n) { \
    119  1.6      mrg 			case 8: l2 =((BF_LONG)(*(--(c))))    ; /* FALLTHROUGH */ \
    120  1.6      mrg 			case 7: l2|=((BF_LONG)(*(--(c))))<< 8; /* FALLTHROUGH */ \
    121  1.6      mrg 			case 6: l2|=((BF_LONG)(*(--(c))))<<16; /* FALLTHROUGH */ \
    122  1.6      mrg 			case 5: l2|=((BF_LONG)(*(--(c))))<<24; /* FALLTHROUGH */ \
    123  1.6      mrg 			case 4: l1 =((BF_LONG)(*(--(c))))    ; /* FALLTHROUGH */ \
    124  1.6      mrg 			case 3: l1|=((BF_LONG)(*(--(c))))<< 8; /* FALLTHROUGH */ \
    125  1.6      mrg 			case 2: l1|=((BF_LONG)(*(--(c))))<<16; /* FALLTHROUGH */ \
    126  1.6      mrg 			case 1: l1|=((BF_LONG)(*(--(c))))<<24; /* FALLTHROUGH */ \
    127  1.1  thorpej 				} \
    128  1.1  thorpej 			}
    129  1.1  thorpej 
    130  1.1  thorpej /* NOTE - c is not incremented as per l2n */
    131  1.1  thorpej #define l2nn(l1,l2,c,n)	{ \
    132  1.1  thorpej 			c+=n; \
    133  1.1  thorpej 			switch (n) { \
    134  1.6      mrg 			case 8: *(--(c))=(unsigned char)(((l2)    )&0xff); /* FALLTHROUGH */ \
    135  1.6      mrg 			case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); /* FALLTHROUGH */ \
    136  1.6      mrg 			case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); /* FALLTHROUGH */ \
    137  1.6      mrg 			case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); /* FALLTHROUGH */ \
    138  1.6      mrg 			case 4: *(--(c))=(unsigned char)(((l1)    )&0xff); /* FALLTHROUGH */ \
    139  1.6      mrg 			case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); /* FALLTHROUGH */ \
    140  1.6      mrg 			case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); /* FALLTHROUGH */ \
    141  1.6      mrg 			case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); /* FALLTHROUGH */ \
    142  1.1  thorpej 				} \
    143  1.1  thorpej 			}
    144  1.1  thorpej 
    145  1.1  thorpej #undef n2l
    146  1.2   itojun #define n2l(c,l)        (l =((BF_LONG)(*((c)++)))<<24L, \
    147  1.2   itojun                          l|=((BF_LONG)(*((c)++)))<<16L, \
    148  1.2   itojun                          l|=((BF_LONG)(*((c)++)))<< 8L, \
    149  1.2   itojun                          l|=((BF_LONG)(*((c)++))))
    150  1.1  thorpej 
    151  1.1  thorpej #undef l2n
    152  1.1  thorpej #define l2n(l,c)        (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
    153  1.1  thorpej                          *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
    154  1.1  thorpej                          *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
    155  1.1  thorpej                          *((c)++)=(unsigned char)(((l)     )&0xff))
    156  1.1  thorpej 
    157  1.1  thorpej /* This is actually a big endian algorithm, the most significate byte
    158  1.1  thorpej  * is used to lookup array 0 */
    159  1.1  thorpej 
    160  1.1  thorpej /* use BF_PTR2 for intel boxes,
    161  1.1  thorpej  * BF_PTR for sparc and MIPS/SGI
    162  1.1  thorpej  * use nothing for Alpha and HP.
    163  1.1  thorpej  */
    164  1.2   itojun #undef	BF_PTR
    165  1.2   itojun #undef	BF_PTR2
    166  1.2   itojun #ifdef __i386__
    167  1.2   itojun #define	BF_PTR2
    168  1.2   itojun #else
    169  1.2   itojun #ifdef __mips__
    170  1.2   itojun #define	BF_PTR
    171  1.2   itojun #endif
    172  1.1  thorpej #endif
    173  1.1  thorpej 
    174  1.1  thorpej #define BF_M	0x3fc
    175  1.1  thorpej #define BF_0	22L
    176  1.1  thorpej #define BF_1	14L
    177  1.1  thorpej #define BF_2	 6L
    178  1.1  thorpej #define BF_3	 2L /* left shift */
    179  1.1  thorpej 
    180  1.1  thorpej #if defined(BF_PTR2)
    181  1.1  thorpej 
    182  1.7   andvar /* This is basically a special pentium version */
    183  1.1  thorpej #define BF_ENC(LL,R,S,P) \
    184  1.1  thorpej 	{ \
    185  1.1  thorpej 	BF_LONG t,u,v; \
    186  1.1  thorpej 	u=R>>BF_0; \
    187  1.1  thorpej 	v=R>>BF_1; \
    188  1.1  thorpej 	u&=BF_M; \
    189  1.1  thorpej 	v&=BF_M; \
    190  1.5    pooka 	t=  *(const BF_LONG *)((const unsigned char *)&(S[  0])+u); \
    191  1.1  thorpej 	u=R>>BF_2; \
    192  1.5    pooka 	t+= *(const BF_LONG *)((const unsigned char *)&(S[256])+v); \
    193  1.1  thorpej 	v=R<<BF_3; \
    194  1.1  thorpej 	u&=BF_M; \
    195  1.1  thorpej 	v&=BF_M; \
    196  1.5    pooka 	t^= *(const BF_LONG *)((const unsigned char *)&(S[512])+u); \
    197  1.1  thorpej 	LL^=P; \
    198  1.5    pooka 	t+= *(const BF_LONG *)((const unsigned char *)&(S[768])+v); \
    199  1.1  thorpej 	LL^=t; \
    200  1.1  thorpej 	}
    201  1.1  thorpej 
    202  1.1  thorpej #elif defined(BF_PTR)
    203  1.1  thorpej 
    204  1.1  thorpej /* This is normally very good */
    205  1.1  thorpej 
    206  1.1  thorpej #define BF_ENC(LL,R,S,P) \
    207  1.1  thorpej 	LL^=P; \
    208  1.3   martin 	LL^= (((*(const BF_LONG *)((const unsigned char *)&(S[  0])+((R>>BF_0)&BF_M))+ \
    209  1.3   martin 		*(const BF_LONG *)((const unsigned char *)&(S[256])+((R>>BF_1)&BF_M)))^ \
    210  1.3   martin 		*(const BF_LONG *)((const unsigned char *)&(S[512])+((R>>BF_2)&BF_M)))+ \
    211  1.3   martin 		*(const BF_LONG *)((const unsigned char *)&(S[768])+((R<<BF_3)&BF_M)));
    212  1.1  thorpej #else
    213  1.1  thorpej 
    214  1.8   andvar /* This will always work, even on 64 bit machines and strangely enough,
    215  1.1  thorpej  * on the Alpha it is faster than the pointer versions (both 32 and 64
    216  1.1  thorpej  * versions of BF_LONG) */
    217  1.1  thorpej 
    218  1.1  thorpej #define BF_ENC(LL,R,S,P) \
    219  1.1  thorpej 	LL^=P; \
    220  1.1  thorpej 	LL^=(((	S[        (R>>24L)      ] + \
    221  1.1  thorpej 		S[0x0100+((R>>16L)&0xff)])^ \
    222  1.1  thorpej 		S[0x0200+((R>> 8L)&0xff)])+ \
    223  1.1  thorpej 		S[0x0300+((R     )&0xff)])&0xffffffff;
    224  1.1  thorpej #endif
    225