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