155714Skris/* crypto/rc5/rc5_locl.h */
255714Skris/* Copyright (C) 1995-1998 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#include <stdlib.h>
6055714Skris
6155714Skris#undef c2l
62296341Sdelphij#define c2l(c,l)        (l =((unsigned long)(*((c)++)))    , \
63296341Sdelphij                         l|=((unsigned long)(*((c)++)))<< 8L, \
64296341Sdelphij                         l|=((unsigned long)(*((c)++)))<<16L, \
65296341Sdelphij                         l|=((unsigned long)(*((c)++)))<<24L)
6655714Skris
6755714Skris/* NOTE - c is not incremented as per c2l */
6855714Skris#undef c2ln
69296341Sdelphij#define c2ln(c,l1,l2,n) { \
70296341Sdelphij                        c+=n; \
71296341Sdelphij                        l1=l2=0; \
72296341Sdelphij                        switch (n) { \
73296341Sdelphij                        case 8: l2 =((unsigned long)(*(--(c))))<<24L; \
74296341Sdelphij                        case 7: l2|=((unsigned long)(*(--(c))))<<16L; \
75296341Sdelphij                        case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \
76296341Sdelphij                        case 5: l2|=((unsigned long)(*(--(c))));     \
77296341Sdelphij                        case 4: l1 =((unsigned long)(*(--(c))))<<24L; \
78296341Sdelphij                        case 3: l1|=((unsigned long)(*(--(c))))<<16L; \
79296341Sdelphij                        case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \
80296341Sdelphij                        case 1: l1|=((unsigned long)(*(--(c))));     \
81296341Sdelphij                                } \
82296341Sdelphij                        }
8355714Skris
8455714Skris#undef l2c
85296341Sdelphij#define l2c(l,c)        (*((c)++)=(unsigned char)(((l)     )&0xff), \
86296341Sdelphij                         *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
87296341Sdelphij                         *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
88296341Sdelphij                         *((c)++)=(unsigned char)(((l)>>24L)&0xff))
8955714Skris
9055714Skris/* NOTE - c is not incremented as per l2c */
9155714Skris#undef l2cn
92296341Sdelphij#define l2cn(l1,l2,c,n) { \
93296341Sdelphij                        c+=n; \
94296341Sdelphij                        switch (n) { \
95296341Sdelphij                        case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
96296341Sdelphij                        case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
97296341Sdelphij                        case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
98296341Sdelphij                        case 5: *(--(c))=(unsigned char)(((l2)     )&0xff); \
99296341Sdelphij                        case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
100296341Sdelphij                        case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
101296341Sdelphij                        case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
102296341Sdelphij                        case 1: *(--(c))=(unsigned char)(((l1)     )&0xff); \
103296341Sdelphij                                } \
104296341Sdelphij                        }
10555714Skris
10655714Skris/* NOTE - c is not incremented as per n2l */
107296341Sdelphij#define n2ln(c,l1,l2,n) { \
108296341Sdelphij                        c+=n; \
109296341Sdelphij                        l1=l2=0; \
110296341Sdelphij                        switch (n) { \
111296341Sdelphij                        case 8: l2 =((unsigned long)(*(--(c))))    ; \
112296341Sdelphij                        case 7: l2|=((unsigned long)(*(--(c))))<< 8; \
113296341Sdelphij                        case 6: l2|=((unsigned long)(*(--(c))))<<16; \
114296341Sdelphij                        case 5: l2|=((unsigned long)(*(--(c))))<<24; \
115296341Sdelphij                        case 4: l1 =((unsigned long)(*(--(c))))    ; \
116296341Sdelphij                        case 3: l1|=((unsigned long)(*(--(c))))<< 8; \
117296341Sdelphij                        case 2: l1|=((unsigned long)(*(--(c))))<<16; \
118296341Sdelphij                        case 1: l1|=((unsigned long)(*(--(c))))<<24; \
119296341Sdelphij                                } \
120296341Sdelphij                        }
12155714Skris
12255714Skris/* NOTE - c is not incremented as per l2n */
123296341Sdelphij#define l2nn(l1,l2,c,n) { \
124296341Sdelphij                        c+=n; \
125296341Sdelphij                        switch (n) { \
126296341Sdelphij                        case 8: *(--(c))=(unsigned char)(((l2)    )&0xff); \
127296341Sdelphij                        case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
128296341Sdelphij                        case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
129296341Sdelphij                        case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
130296341Sdelphij                        case 4: *(--(c))=(unsigned char)(((l1)    )&0xff); \
131296341Sdelphij                        case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
132296341Sdelphij                        case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
133296341Sdelphij                        case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
134296341Sdelphij                                } \
135296341Sdelphij                        }
13655714Skris
13755714Skris#undef n2l
13855714Skris#define n2l(c,l)        (l =((unsigned long)(*((c)++)))<<24L, \
13955714Skris                         l|=((unsigned long)(*((c)++)))<<16L, \
14055714Skris                         l|=((unsigned long)(*((c)++)))<< 8L, \
14155714Skris                         l|=((unsigned long)(*((c)++))))
14255714Skris
14355714Skris#undef l2n
14455714Skris#define l2n(l,c)        (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
14555714Skris                         *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
14655714Skris                         *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
14755714Skris                         *((c)++)=(unsigned char)(((l)     )&0xff))
14855714Skris
149160814Ssimon#if (defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER)) || defined(__ICC)
150296341Sdelphij# define ROTATE_l32(a,n)     _lrotl(a,n)
151296341Sdelphij# define ROTATE_r32(a,n)     _lrotr(a,n)
152111147Snectar#elif defined(__GNUC__) && __GNUC__>=2 && !defined(__STRICT_ANSI__) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)
153109998Smarkm# if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
154296341Sdelphij#  define ROTATE_l32(a,n)       ({ register unsigned int ret;   \
155296341Sdelphij                                        asm ("roll %%cl,%0"     \
156296341Sdelphij                                                : "=r"(ret)     \
157296341Sdelphij                                                : "c"(n),"0"((unsigned int)(a)) \
158296341Sdelphij                                                : "cc");        \
159296341Sdelphij                                        ret;                    \
160296341Sdelphij                                })
161296341Sdelphij#  define ROTATE_r32(a,n)       ({ register unsigned int ret;   \
162296341Sdelphij                                        asm ("rorl %%cl,%0"     \
163296341Sdelphij                                                : "=r"(ret)     \
164296341Sdelphij                                                : "c"(n),"0"((unsigned int)(a)) \
165296341Sdelphij                                                : "cc");        \
166296341Sdelphij                                        ret;                    \
167296341Sdelphij                                })
168109998Smarkm# endif
169109998Smarkm#endif
170109998Smarkm#ifndef ROTATE_l32
171296341Sdelphij# define ROTATE_l32(a,n)     (((a)<<(n&0x1f))|(((a)&0xffffffff)>>(32-(n&0x1f))))
172109998Smarkm#endif
173109998Smarkm#ifndef ROTATE_r32
174296341Sdelphij# define ROTATE_r32(a,n)     (((a)<<(32-(n&0x1f)))|(((a)&0xffffffff)>>(n&0x1f)))
17555714Skris#endif
17655714Skris
177296341Sdelphij#define RC5_32_MASK     0xffffffffL
17855714Skris
179296341Sdelphij#define RC5_16_P        0xB7E1
180296341Sdelphij#define RC5_16_Q        0x9E37
181296341Sdelphij#define RC5_32_P        0xB7E15163L
182296341Sdelphij#define RC5_32_Q        0x9E3779B9L
183296341Sdelphij#define RC5_64_P        0xB7E151628AED2A6BLL
184296341Sdelphij#define RC5_64_Q        0x9E3779B97F4A7C15LL
18555714Skris
18655714Skris#define E_RC5_32(a,b,s,n) \
187296341Sdelphij        a^=b; \
188296341Sdelphij        a=ROTATE_l32(a,b); \
189296341Sdelphij        a+=s[n]; \
190296341Sdelphij        a&=RC5_32_MASK; \
191296341Sdelphij        b^=a; \
192296341Sdelphij        b=ROTATE_l32(b,a); \
193296341Sdelphij        b+=s[n+1]; \
194296341Sdelphij        b&=RC5_32_MASK;
19555714Skris
19655714Skris#define D_RC5_32(a,b,s,n) \
197296341Sdelphij        b-=s[n+1]; \
198296341Sdelphij        b&=RC5_32_MASK; \
199296341Sdelphij        b=ROTATE_r32(b,a); \
200296341Sdelphij        b^=a; \
201296341Sdelphij        a-=s[n]; \
202296341Sdelphij        a&=RC5_32_MASK; \
203296341Sdelphij        a=ROTATE_r32(a,b); \
204296341Sdelphij        a^=b;
205