xref: /onnv-gate/usr/src/common/openssl/crypto/rc5/rc5_locl.h (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* crypto/rc5/rc5_locl.h */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <stdlib.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #undef c2l
620Sstevel@tonic-gate #define c2l(c,l)	(l =((unsigned long)(*((c)++)))    , \
630Sstevel@tonic-gate 			 l|=((unsigned long)(*((c)++)))<< 8L, \
640Sstevel@tonic-gate 			 l|=((unsigned long)(*((c)++)))<<16L, \
650Sstevel@tonic-gate 			 l|=((unsigned long)(*((c)++)))<<24L)
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /* NOTE - c is not incremented as per c2l */
680Sstevel@tonic-gate #undef c2ln
690Sstevel@tonic-gate #define c2ln(c,l1,l2,n)	{ \
700Sstevel@tonic-gate 			c+=n; \
710Sstevel@tonic-gate 			l1=l2=0; \
720Sstevel@tonic-gate 			switch (n) { \
730Sstevel@tonic-gate 			case 8: l2 =((unsigned long)(*(--(c))))<<24L; \
740Sstevel@tonic-gate 			case 7: l2|=((unsigned long)(*(--(c))))<<16L; \
750Sstevel@tonic-gate 			case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \
760Sstevel@tonic-gate 			case 5: l2|=((unsigned long)(*(--(c))));     \
770Sstevel@tonic-gate 			case 4: l1 =((unsigned long)(*(--(c))))<<24L; \
780Sstevel@tonic-gate 			case 3: l1|=((unsigned long)(*(--(c))))<<16L; \
790Sstevel@tonic-gate 			case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \
800Sstevel@tonic-gate 			case 1: l1|=((unsigned long)(*(--(c))));     \
810Sstevel@tonic-gate 				} \
820Sstevel@tonic-gate 			}
830Sstevel@tonic-gate 
840Sstevel@tonic-gate #undef l2c
850Sstevel@tonic-gate #define l2c(l,c)	(*((c)++)=(unsigned char)(((l)     )&0xff), \
860Sstevel@tonic-gate 			 *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
870Sstevel@tonic-gate 			 *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
880Sstevel@tonic-gate 			 *((c)++)=(unsigned char)(((l)>>24L)&0xff))
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /* NOTE - c is not incremented as per l2c */
910Sstevel@tonic-gate #undef l2cn
920Sstevel@tonic-gate #define l2cn(l1,l2,c,n)	{ \
930Sstevel@tonic-gate 			c+=n; \
940Sstevel@tonic-gate 			switch (n) { \
950Sstevel@tonic-gate 			case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
960Sstevel@tonic-gate 			case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
970Sstevel@tonic-gate 			case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
980Sstevel@tonic-gate 			case 5: *(--(c))=(unsigned char)(((l2)     )&0xff); \
990Sstevel@tonic-gate 			case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
1000Sstevel@tonic-gate 			case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
1010Sstevel@tonic-gate 			case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
1020Sstevel@tonic-gate 			case 1: *(--(c))=(unsigned char)(((l1)     )&0xff); \
1030Sstevel@tonic-gate 				} \
1040Sstevel@tonic-gate 			}
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /* NOTE - c is not incremented as per n2l */
1070Sstevel@tonic-gate #define n2ln(c,l1,l2,n)	{ \
1080Sstevel@tonic-gate 			c+=n; \
1090Sstevel@tonic-gate 			l1=l2=0; \
1100Sstevel@tonic-gate 			switch (n) { \
1110Sstevel@tonic-gate 			case 8: l2 =((unsigned long)(*(--(c))))    ; \
1120Sstevel@tonic-gate 			case 7: l2|=((unsigned long)(*(--(c))))<< 8; \
1130Sstevel@tonic-gate 			case 6: l2|=((unsigned long)(*(--(c))))<<16; \
1140Sstevel@tonic-gate 			case 5: l2|=((unsigned long)(*(--(c))))<<24; \
1150Sstevel@tonic-gate 			case 4: l1 =((unsigned long)(*(--(c))))    ; \
1160Sstevel@tonic-gate 			case 3: l1|=((unsigned long)(*(--(c))))<< 8; \
1170Sstevel@tonic-gate 			case 2: l1|=((unsigned long)(*(--(c))))<<16; \
1180Sstevel@tonic-gate 			case 1: l1|=((unsigned long)(*(--(c))))<<24; \
1190Sstevel@tonic-gate 				} \
1200Sstevel@tonic-gate 			}
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /* NOTE - c is not incremented as per l2n */
1230Sstevel@tonic-gate #define l2nn(l1,l2,c,n)	{ \
1240Sstevel@tonic-gate 			c+=n; \
1250Sstevel@tonic-gate 			switch (n) { \
1260Sstevel@tonic-gate 			case 8: *(--(c))=(unsigned char)(((l2)    )&0xff); \
1270Sstevel@tonic-gate 			case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
1280Sstevel@tonic-gate 			case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
1290Sstevel@tonic-gate 			case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
1300Sstevel@tonic-gate 			case 4: *(--(c))=(unsigned char)(((l1)    )&0xff); \
1310Sstevel@tonic-gate 			case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
1320Sstevel@tonic-gate 			case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
1330Sstevel@tonic-gate 			case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
1340Sstevel@tonic-gate 				} \
1350Sstevel@tonic-gate 			}
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate #undef n2l
1380Sstevel@tonic-gate #define n2l(c,l)        (l =((unsigned long)(*((c)++)))<<24L, \
1390Sstevel@tonic-gate                          l|=((unsigned long)(*((c)++)))<<16L, \
1400Sstevel@tonic-gate                          l|=((unsigned long)(*((c)++)))<< 8L, \
1410Sstevel@tonic-gate                          l|=((unsigned long)(*((c)++))))
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate #undef l2n
1440Sstevel@tonic-gate #define l2n(l,c)        (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
1450Sstevel@tonic-gate                          *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
1460Sstevel@tonic-gate                          *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
1470Sstevel@tonic-gate                          *((c)++)=(unsigned char)(((l)     )&0xff))
1480Sstevel@tonic-gate 
149*2139Sjp161948 #if (defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER)) || defined(__ICC)
1500Sstevel@tonic-gate #define ROTATE_l32(a,n)     _lrotl(a,n)
1510Sstevel@tonic-gate #define ROTATE_r32(a,n)     _lrotr(a,n)
1520Sstevel@tonic-gate #elif defined(__GNUC__) && __GNUC__>=2 && !defined(__STRICT_ANSI__) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)
1530Sstevel@tonic-gate # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
1540Sstevel@tonic-gate #  define ROTATE_l32(a,n)	({ register unsigned int ret;	\
155*2139Sjp161948 					__asm__ ("roll %%cl,%0"	\
1560Sstevel@tonic-gate 						: "=r"(ret)	\
1570Sstevel@tonic-gate 						: "c"(n),"0"(a)	\
1580Sstevel@tonic-gate 						: "cc");	\
1590Sstevel@tonic-gate 					ret;			\
1600Sstevel@tonic-gate 				})
1610Sstevel@tonic-gate #  define ROTATE_r32(a,n)	({ register unsigned int ret;	\
162*2139Sjp161948 					__asm__ ("rorl %%cl,%0"	\
1630Sstevel@tonic-gate 						: "=r"(ret)	\
1640Sstevel@tonic-gate 						: "c"(n),"0"(a)	\
1650Sstevel@tonic-gate 						: "cc");	\
1660Sstevel@tonic-gate 					ret;			\
1670Sstevel@tonic-gate 				})
1680Sstevel@tonic-gate # endif
1690Sstevel@tonic-gate #endif
1700Sstevel@tonic-gate #ifndef ROTATE_l32
1710Sstevel@tonic-gate #define ROTATE_l32(a,n)     (((a)<<(n&0x1f))|(((a)&0xffffffff)>>(32-(n&0x1f))))
1720Sstevel@tonic-gate #endif
1730Sstevel@tonic-gate #ifndef ROTATE_r32
1740Sstevel@tonic-gate #define ROTATE_r32(a,n)     (((a)<<(32-(n&0x1f)))|(((a)&0xffffffff)>>(n&0x1f)))
1750Sstevel@tonic-gate #endif
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate #define RC5_32_MASK	0xffffffffL
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate #define RC5_16_P	0xB7E1
1800Sstevel@tonic-gate #define RC5_16_Q	0x9E37
1810Sstevel@tonic-gate #define RC5_32_P	0xB7E15163L
1820Sstevel@tonic-gate #define RC5_32_Q	0x9E3779B9L
1830Sstevel@tonic-gate #define RC5_64_P	0xB7E151628AED2A6BLL
1840Sstevel@tonic-gate #define RC5_64_Q	0x9E3779B97F4A7C15LL
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate #define E_RC5_32(a,b,s,n) \
1870Sstevel@tonic-gate 	a^=b; \
1880Sstevel@tonic-gate 	a=ROTATE_l32(a,b); \
1890Sstevel@tonic-gate 	a+=s[n]; \
1900Sstevel@tonic-gate 	a&=RC5_32_MASK; \
1910Sstevel@tonic-gate 	b^=a; \
1920Sstevel@tonic-gate 	b=ROTATE_l32(b,a); \
1930Sstevel@tonic-gate 	b+=s[n+1]; \
1940Sstevel@tonic-gate 	b&=RC5_32_MASK;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate #define D_RC5_32(a,b,s,n) \
1970Sstevel@tonic-gate 	b-=s[n+1]; \
1980Sstevel@tonic-gate 	b&=RC5_32_MASK; \
1990Sstevel@tonic-gate 	b=ROTATE_r32(b,a); \
2000Sstevel@tonic-gate 	b^=a; \
2010Sstevel@tonic-gate 	a-=s[n]; \
2020Sstevel@tonic-gate 	a&=RC5_32_MASK; \
2030Sstevel@tonic-gate 	a=ROTATE_r32(a,b); \
2040Sstevel@tonic-gate 	a^=b;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 
208