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.
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#include <stdlib.h>
6055714Skris
6155714Skris#undef c2l
62280304Sjkim#define c2l(c,l)        (l =((unsigned long)(*((c)++)))    , \
63280304Sjkim                         l|=((unsigned long)(*((c)++)))<< 8L, \
64280304Sjkim                         l|=((unsigned long)(*((c)++)))<<16L, \
65280304Sjkim                         l|=((unsigned long)(*((c)++)))<<24L)
6655714Skris
6755714Skris/* NOTE - c is not incremented as per c2l */
6855714Skris#undef c2ln
69280304Sjkim#define c2ln(c,l1,l2,n) { \
70280304Sjkim                        c+=n; \
71280304Sjkim                        l1=l2=0; \
72280304Sjkim                        switch (n) { \
73280304Sjkim                        case 8: l2 =((unsigned long)(*(--(c))))<<24L; \
74280304Sjkim                        case 7: l2|=((unsigned long)(*(--(c))))<<16L; \
75280304Sjkim                        case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \
76280304Sjkim                        case 5: l2|=((unsigned long)(*(--(c))));     \
77280304Sjkim                        case 4: l1 =((unsigned long)(*(--(c))))<<24L; \
78280304Sjkim                        case 3: l1|=((unsigned long)(*(--(c))))<<16L; \
79280304Sjkim                        case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \
80280304Sjkim                        case 1: l1|=((unsigned long)(*(--(c))));     \
81280304Sjkim                                } \
82280304Sjkim                        }
8355714Skris
8455714Skris#undef l2c
85280304Sjkim#define l2c(l,c)        (*((c)++)=(unsigned char)(((l)     )&0xff), \
86280304Sjkim                         *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
87280304Sjkim                         *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
88280304Sjkim                         *((c)++)=(unsigned char)(((l)>>24L)&0xff))
8955714Skris
9055714Skris/* NOTE - c is not incremented as per l2c */
9155714Skris#undef l2cn
92280304Sjkim#define l2cn(l1,l2,c,n) { \
93280304Sjkim                        c+=n; \
94280304Sjkim                        switch (n) { \
95280304Sjkim                        case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
96280304Sjkim                        case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
97280304Sjkim                        case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
98280304Sjkim                        case 5: *(--(c))=(unsigned char)(((l2)     )&0xff); \
99280304Sjkim                        case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
100280304Sjkim                        case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
101280304Sjkim                        case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
102280304Sjkim                        case 1: *(--(c))=(unsigned char)(((l1)     )&0xff); \
103280304Sjkim                                } \
104280304Sjkim                        }
10555714Skris
10655714Skris/* NOTE - c is not incremented as per n2l */
107280304Sjkim#define n2ln(c,l1,l2,n) { \
108280304Sjkim                        c+=n; \
109280304Sjkim                        l1=l2=0; \
110280304Sjkim                        switch (n) { \
111280304Sjkim                        case 8: l2 =((unsigned long)(*(--(c))))    ; \
112280304Sjkim                        case 7: l2|=((unsigned long)(*(--(c))))<< 8; \
113280304Sjkim                        case 6: l2|=((unsigned long)(*(--(c))))<<16; \
114280304Sjkim                        case 5: l2|=((unsigned long)(*(--(c))))<<24; \
115280304Sjkim                        case 4: l1 =((unsigned long)(*(--(c))))    ; \
116280304Sjkim                        case 3: l1|=((unsigned long)(*(--(c))))<< 8; \
117280304Sjkim                        case 2: l1|=((unsigned long)(*(--(c))))<<16; \
118280304Sjkim                        case 1: l1|=((unsigned long)(*(--(c))))<<24; \
119280304Sjkim                                } \
120280304Sjkim                        }
12155714Skris
12255714Skris/* NOTE - c is not incremented as per l2n */
123280304Sjkim#define l2nn(l1,l2,c,n) { \
124280304Sjkim                        c+=n; \
125280304Sjkim                        switch (n) { \
126280304Sjkim                        case 8: *(--(c))=(unsigned char)(((l2)    )&0xff); \
127280304Sjkim                        case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
128280304Sjkim                        case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
129280304Sjkim                        case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
130280304Sjkim                        case 4: *(--(c))=(unsigned char)(((l1)    )&0xff); \
131280304Sjkim                        case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
132280304Sjkim                        case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
133280304Sjkim                        case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
134280304Sjkim                                } \
135280304Sjkim                        }
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)
150280304Sjkim# define ROTATE_l32(a,n)     _lrotl(a,n)
151280304Sjkim# 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__)
154280304Sjkim#  define ROTATE_l32(a,n)       ({ register unsigned int ret;   \
155280304Sjkim                                        asm ("roll %%cl,%0"     \
156280304Sjkim                                                : "=r"(ret)     \
157280304Sjkim                                                : "c"(n),"0"((unsigned int)(a)) \
158280304Sjkim                                                : "cc");        \
159280304Sjkim                                        ret;                    \
160280304Sjkim                                })
161280304Sjkim#  define ROTATE_r32(a,n)       ({ register unsigned int ret;   \
162280304Sjkim                                        asm ("rorl %%cl,%0"     \
163280304Sjkim                                                : "=r"(ret)     \
164280304Sjkim                                                : "c"(n),"0"((unsigned int)(a)) \
165280304Sjkim                                                : "cc");        \
166280304Sjkim                                        ret;                    \
167280304Sjkim                                })
168109998Smarkm# endif
169109998Smarkm#endif
170109998Smarkm#ifndef ROTATE_l32
171280304Sjkim# define ROTATE_l32(a,n)     (((a)<<(n&0x1f))|(((a)&0xffffffff)>>(32-(n&0x1f))))
172109998Smarkm#endif
173109998Smarkm#ifndef ROTATE_r32
174280304Sjkim# define ROTATE_r32(a,n)     (((a)<<(32-(n&0x1f)))|(((a)&0xffffffff)>>(n&0x1f)))
17555714Skris#endif
17655714Skris
177280304Sjkim#define RC5_32_MASK     0xffffffffL
17855714Skris
179280304Sjkim#define RC5_16_P        0xB7E1
180280304Sjkim#define RC5_16_Q        0x9E37
181280304Sjkim#define RC5_32_P        0xB7E15163L
182280304Sjkim#define RC5_32_Q        0x9E3779B9L
183280304Sjkim#define RC5_64_P        0xB7E151628AED2A6BLL
184280304Sjkim#define RC5_64_Q        0x9E3779B97F4A7C15LL
18555714Skris
18655714Skris#define E_RC5_32(a,b,s,n) \
187280304Sjkim        a^=b; \
188280304Sjkim        a=ROTATE_l32(a,b); \
189280304Sjkim        a+=s[n]; \
190280304Sjkim        a&=RC5_32_MASK; \
191280304Sjkim        b^=a; \
192280304Sjkim        b=ROTATE_l32(b,a); \
193280304Sjkim        b+=s[n+1]; \
194280304Sjkim        b&=RC5_32_MASK;
19555714Skris
19655714Skris#define D_RC5_32(a,b,s,n) \
197280304Sjkim        b-=s[n+1]; \
198280304Sjkim        b&=RC5_32_MASK; \
199280304Sjkim        b=ROTATE_r32(b,a); \
200280304Sjkim        b^=a; \
201280304Sjkim        a-=s[n]; \
202280304Sjkim        a&=RC5_32_MASK; \
203280304Sjkim        a=ROTATE_r32(a,b); \
204280304Sjkim        a^=b;
205