e8cf322d430dc82cc41e5eb2dcbb58b06451ba0e
[openssl.git] / crypto / cast / cast_lcl.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 "e_os.h"
11
12 #ifdef OPENSSL_SYS_WIN32
13 # include <stdlib.h>
14 #endif
15
16 #undef c2l
17 #define c2l(c,l)        (l =((unsigned long)(*((c)++)))    , \
18                          l|=((unsigned long)(*((c)++)))<< 8L, \
19                          l|=((unsigned long)(*((c)++)))<<16L, \
20                          l|=((unsigned long)(*((c)++)))<<24L)
21
22 /* NOTE - c is not incremented as per c2l */
23 #undef c2ln
24 #define c2ln(c,l1,l2,n) { \
25                         c+=n; \
26                         l1=l2=0; \
27                         switch (n) { \
28                         case 8: l2 =((unsigned long)(*(--(c))))<<24L; \
29                         case 7: l2|=((unsigned long)(*(--(c))))<<16L; \
30                         case 6: l2|=((unsigned long)(*(--(c))))<< 8L; \
31                         case 5: l2|=((unsigned long)(*(--(c))));     \
32                         case 4: l1 =((unsigned long)(*(--(c))))<<24L; \
33                         case 3: l1|=((unsigned long)(*(--(c))))<<16L; \
34                         case 2: l1|=((unsigned long)(*(--(c))))<< 8L; \
35                         case 1: l1|=((unsigned long)(*(--(c))));     \
36                                 } \
37                         }
38
39 #undef l2c
40 #define l2c(l,c)        (*((c)++)=(unsigned char)(((l)     )&0xff), \
41                          *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
42                          *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
43                          *((c)++)=(unsigned char)(((l)>>24L)&0xff))
44
45 /* NOTE - c is not incremented as per l2c */
46 #undef l2cn
47 #define l2cn(l1,l2,c,n) { \
48                         c+=n; \
49                         switch (n) { \
50                         case 8: *(--(c))=(unsigned char)(((l2)>>24L)&0xff); \
51                         case 7: *(--(c))=(unsigned char)(((l2)>>16L)&0xff); \
52                         case 6: *(--(c))=(unsigned char)(((l2)>> 8L)&0xff); \
53                         case 5: *(--(c))=(unsigned char)(((l2)     )&0xff); \
54                         case 4: *(--(c))=(unsigned char)(((l1)>>24L)&0xff); \
55                         case 3: *(--(c))=(unsigned char)(((l1)>>16L)&0xff); \
56                         case 2: *(--(c))=(unsigned char)(((l1)>> 8L)&0xff); \
57                         case 1: *(--(c))=(unsigned char)(((l1)     )&0xff); \
58                                 } \
59                         }
60
61 /* NOTE - c is not incremented as per n2l */
62 #define n2ln(c,l1,l2,n) { \
63                         c+=n; \
64                         l1=l2=0; \
65                         switch (n) { \
66                         case 8: l2 =((unsigned long)(*(--(c))))    ; \
67                         /* fall thru */                              \
68                         case 7: l2|=((unsigned long)(*(--(c))))<< 8; \
69                         /* fall thru */                              \
70                         case 6: l2|=((unsigned long)(*(--(c))))<<16; \
71                         /* fall thru */                              \
72                         case 5: l2|=((unsigned long)(*(--(c))))<<24; \
73                         /* fall thru */                              \
74                         case 4: l1 =((unsigned long)(*(--(c))))    ; \
75                         /* fall thru */                              \
76                         case 3: l1|=((unsigned long)(*(--(c))))<< 8; \
77                         /* fall thru */                              \
78                         case 2: l1|=((unsigned long)(*(--(c))))<<16; \
79                         /* fall thru */                              \
80                         case 1: l1|=((unsigned long)(*(--(c))))<<24; \
81                                 } \
82                         }
83
84 /* NOTE - c is not incremented as per l2n */
85 #define l2nn(l1,l2,c,n) { \
86                         c+=n; \
87                         switch (n) { \
88                         case 8: *(--(c))=(unsigned char)(((l2)    )&0xff); \
89                         /* fall thru */                                    \
90                         case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \
91                         /* fall thru */                                    \
92                         case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \
93                         /* fall thru */                                    \
94                         case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \
95                         /* fall thru */                                    \
96                         case 4: *(--(c))=(unsigned char)(((l1)    )&0xff); \
97                         /* fall thru */                                    \
98                         case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \
99                         /* fall thru */                                    \
100                         case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \
101                         /* fall thru */                                    \
102                         case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \
103                                 } \
104                         }
105
106 #undef n2l
107 #define n2l(c,l)        (l =((unsigned long)(*((c)++)))<<24L, \
108                          l|=((unsigned long)(*((c)++)))<<16L, \
109                          l|=((unsigned long)(*((c)++)))<< 8L, \
110                          l|=((unsigned long)(*((c)++))))
111
112 #undef l2n
113 #define l2n(l,c)        (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \
114                          *((c)++)=(unsigned char)(((l)>>16L)&0xff), \
115                          *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \
116                          *((c)++)=(unsigned char)(((l)     )&0xff))
117
118 #if defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER)
119 # define ROTL(a,n)     (_lrotl(a,n))
120 #else
121 # define ROTL(a,n)     ((((a)<<(n))&0xffffffffL)|((a)>>((32-(n))&31)))
122 #endif
123
124 #define C_M    0x3fc
125 #define C_0    22L
126 #define C_1    14L
127 #define C_2     6L
128 #define C_3     2L              /* left shift */
129
130 /* The rotate has an extra 16 added to it to help the x86 asm */
131 #if defined(CAST_PTR)
132 # define E_CAST(n,key,L,R,OP1,OP2,OP3) \
133         { \
134         int i; \
135         t=(key[n*2] OP1 R)&0xffffffffL; \
136         i=key[n*2+1]; \
137         t=ROTL(t,i); \
138         L^= (((((*(CAST_LONG *)((unsigned char *) \
139                         CAST_S_table0+((t>>C_2)&C_M)) OP2 \
140                 *(CAST_LONG *)((unsigned char *) \
141                         CAST_S_table1+((t<<C_3)&C_M)))&0xffffffffL) OP3 \
142                 *(CAST_LONG *)((unsigned char *) \
143                         CAST_S_table2+((t>>C_0)&C_M)))&0xffffffffL) OP1 \
144                 *(CAST_LONG *)((unsigned char *) \
145                         CAST_S_table3+((t>>C_1)&C_M)))&0xffffffffL; \
146         }
147 #elif defined(CAST_PTR2)
148 # define E_CAST(n,key,L,R,OP1,OP2,OP3) \
149         { \
150         int i; \
151         CAST_LONG u,v,w; \
152         w=(key[n*2] OP1 R)&0xffffffffL; \
153         i=key[n*2+1]; \
154         w=ROTL(w,i); \
155         u=w>>C_2; \
156         v=w<<C_3; \
157         u&=C_M; \
158         v&=C_M; \
159         t= *(CAST_LONG *)((unsigned char *)CAST_S_table0+u); \
160         u=w>>C_0; \
161         t=(t OP2 *(CAST_LONG *)((unsigned char *)CAST_S_table1+v))&0xffffffffL;\
162         v=w>>C_1; \
163         u&=C_M; \
164         v&=C_M; \
165         t=(t OP3 *(CAST_LONG *)((unsigned char *)CAST_S_table2+u)&0xffffffffL);\
166         t=(t OP1 *(CAST_LONG *)((unsigned char *)CAST_S_table3+v)&0xffffffffL);\
167         L^=(t&0xffffffff); \
168         }
169 #else
170 # define E_CAST(n,key,L,R,OP1,OP2,OP3) \
171         { \
172         CAST_LONG a,b,c,d; \
173         t=(key[n*2] OP1 R)&0xffffffff; \
174         t=ROTL(t,(key[n*2+1])); \
175         a=CAST_S_table0[(t>> 8)&0xff]; \
176         b=CAST_S_table1[(t    )&0xff]; \
177         c=CAST_S_table2[(t>>24)&0xff]; \
178         d=CAST_S_table3[(t>>16)&0xff]; \
179         L^=(((((a OP2 b)&0xffffffffL) OP3 c)&0xffffffffL) OP1 d)&0xffffffffL; \
180         }
181 #endif
182
183 extern const CAST_LONG CAST_S_table0[256];
184 extern const CAST_LONG CAST_S_table1[256];
185 extern const CAST_LONG CAST_S_table2[256];
186 extern const CAST_LONG CAST_S_table3[256];
187 extern const CAST_LONG CAST_S_table4[256];
188 extern const CAST_LONG CAST_S_table5[256];
189 extern const CAST_LONG CAST_S_table6[256];
190 extern const CAST_LONG CAST_S_table7[256];