33a709b4537e99b453d0c98d2e5e66b8c9b872e1
[openssl.git] / crypto / rc5 / rc5_locl.h
1 /*
2  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <stdlib.h>
11
12 #undef c2l
13 #define c2l(c,l)        (l =((unsigned long)(*((c)++)))    , \
14                          l|=((unsigned long)(*((c)++)))<< 8L, \
15                          l|=((unsigned long)(*((c)++)))<<16L, \
16                          l|=((unsigned long)(*((c)++)))<<24L)
17
18 /* NOTE - c is not incremented as per c2l */
19 #undef c2ln
20 #define c2ln(c,l1,l2,n) { \
21                         c+=n; \
22                         l1=l2=0; \
23                         switch (n) { \
24                         case 8: l2 =((unsigned long)(*(--(c))))<<24L; \
25                         case 7: l2|=((unsigned long)(*(--(c))))<<16L; \
26                         case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \
27                         case 5: l2|=((unsigned long)(*(--(c))));     \
28                         case 4: l1 =((unsigned long)(*(--(c))))<<24L; \
29                         case 3: l1|=((unsigned long)(*(--(c))))<<16L; \
30                         case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \
31                         case 1: l1|=((unsigned long)(*(--(c))));     \
32                                 } \
33                         }
34
35 #undef l2c
36 #define l2c(l,c)        (*((c)++)=(unsigned char)(((l)     )&0xff), \
37                          *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
38                          *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
39                          *((c)++)=(unsigned char)(((l)>>24L)&0xff))
40
41 /* NOTE - c is not incremented as per l2c */
42 #undef l2cn
43 #define l2cn(l1,l2,c,n) { \
44                         c+=n; \
45                         switch (n) { \
46                         case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
47                         case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
48                         case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
49                         case 5: *(--(c))=(unsigned char)(((l2)     )&0xff); \
50                         case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
51                         case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
52                         case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
53                         case 1: *(--(c))=(unsigned char)(((l1)     )&0xff); \
54                                 } \
55                         }
56
57 /* NOTE - c is not incremented as per n2l */
58 #define n2ln(c,l1,l2,n) { \
59                         c+=n; \
60                         l1=l2=0; \
61                         switch (n) { \
62                         case 8: l2 =((unsigned long)(*(--(c))))    ; \
63                         case 7: l2|=((unsigned long)(*(--(c))))<< 8; \
64                         case 6: l2|=((unsigned long)(*(--(c))))<<16; \
65                         case 5: l2|=((unsigned long)(*(--(c))))<<24; \
66                         case 4: l1 =((unsigned long)(*(--(c))))    ; \
67                         case 3: l1|=((unsigned long)(*(--(c))))<< 8; \
68                         case 2: l1|=((unsigned long)(*(--(c))))<<16; \
69                         case 1: l1|=((unsigned long)(*(--(c))))<<24; \
70                                 } \
71                         }
72
73 /* NOTE - c is not incremented as per l2n */
74 #define l2nn(l1,l2,c,n) { \
75                         c+=n; \
76                         switch (n) { \
77                         case 8: *(--(c))=(unsigned char)(((l2)    )&0xff); \
78                         case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
79                         case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
80                         case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
81                         case 4: *(--(c))=(unsigned char)(((l1)    )&0xff); \
82                         case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
83                         case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
84                         case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
85                                 } \
86                         }
87
88 #undef n2l
89 #define n2l(c,l)        (l =((unsigned long)(*((c)++)))<<24L, \
90                          l|=((unsigned long)(*((c)++)))<<16L, \
91                          l|=((unsigned long)(*((c)++)))<< 8L, \
92                          l|=((unsigned long)(*((c)++))))
93
94 #undef l2n
95 #define l2n(l,c)        (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
96                          *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
97                          *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
98                          *((c)++)=(unsigned char)(((l)     )&0xff))
99
100 #if (defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER))
101 # define ROTATE_l32(a,n)     _lrotl(a,n)
102 # define ROTATE_r32(a,n)     _lrotr(a,n)
103 #elif defined(__ICC)
104 # define ROTATE_l32(a,n)     _rotl(a,n)
105 # define ROTATE_r32(a,n)     _rotr(a,n)
106 #elif defined(__GNUC__) && __GNUC__>=2 && !defined(__STRICT_ANSI__) && !defined(OPENSSL_NO_ASM) && !defined(OPENSSL_NO_INLINE_ASM) && !defined(PEDANTIC)
107 # if defined(__i386) || defined(__i386__) || defined(__x86_64) || defined(__x86_64__)
108 #  define ROTATE_l32(a,n)       ({ register unsigned int ret;   \
109                                         asm ("roll %%cl,%0"     \
110                                                 : "=r"(ret)     \
111                                                 : "c"(n),"0"((unsigned int)(a)) \
112                                                 : "cc");        \
113                                         ret;                    \
114                                 })
115 #  define ROTATE_r32(a,n)       ({ register unsigned int ret;   \
116                                         asm ("rorl %%cl,%0"     \
117                                                 : "=r"(ret)     \
118                                                 : "c"(n),"0"((unsigned int)(a)) \
119                                                 : "cc");        \
120                                         ret;                    \
121                                 })
122 # endif
123 #endif
124 #ifndef ROTATE_l32
125 # define ROTATE_l32(a,n)     (((a)<<(n&0x1f))|(((a)&0xffffffff)>>((32-n)&0x1f)))
126 #endif
127 #ifndef ROTATE_r32
128 # define ROTATE_r32(a,n)     (((a)<<((32-n)&0x1f))|(((a)&0xffffffff)>>(n&0x1f)))
129 #endif
130
131 #define RC5_32_MASK     0xffffffffL
132
133 #define RC5_16_P        0xB7E1
134 #define RC5_16_Q        0x9E37
135 #define RC5_32_P        0xB7E15163L
136 #define RC5_32_Q        0x9E3779B9L
137 #define RC5_64_P        0xB7E151628AED2A6BLL
138 #define RC5_64_Q        0x9E3779B97F4A7C15LL
139
140 #define E_RC5_32(a,b,s,n) \
141         a^=b; \
142         a=ROTATE_l32(a,b); \
143         a+=s[n]; \
144         a&=RC5_32_MASK; \
145         b^=a; \
146         b=ROTATE_l32(b,a); \
147         b+=s[n+1]; \
148         b&=RC5_32_MASK;
149
150 #define D_RC5_32(a,b,s,n) \
151         b-=s[n+1]; \
152         b&=RC5_32_MASK; \
153         b=ROTATE_r32(b,a); \
154         b^=a; \
155         a-=s[n]; \
156         a&=RC5_32_MASK; \
157         a=ROTATE_r32(a,b); \
158         a^=b;