]>
Commit | Line | Data |
---|---|---|
b1322259 | 1 | /* |
da1c088f | 2 | * Copyright 1995-2023 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 | ||
58964a49 | 14 | /* NOTE - c is not incremented as per n2l */ |
0f113f3e MC |
15 | #define n2ln(c,l1,l2,n) { \ |
16 | c+=n; \ | |
17 | l1=l2=0; \ | |
18 | switch (n) { \ | |
19 | case 8: l2 =((unsigned long)(*(--(c)))) ; \ | |
1567a821 | 20 | /* fall through */ \ |
0f113f3e | 21 | case 7: l2|=((unsigned long)(*(--(c))))<< 8; \ |
1567a821 | 22 | /* fall through */ \ |
0f113f3e | 23 | case 6: l2|=((unsigned long)(*(--(c))))<<16; \ |
1567a821 | 24 | /* fall through */ \ |
0f113f3e | 25 | case 5: l2|=((unsigned long)(*(--(c))))<<24; \ |
1567a821 | 26 | /* fall through */ \ |
0f113f3e | 27 | case 4: l1 =((unsigned long)(*(--(c)))) ; \ |
1567a821 | 28 | /* fall through */ \ |
0f113f3e | 29 | case 3: l1|=((unsigned long)(*(--(c))))<< 8; \ |
1567a821 | 30 | /* fall through */ \ |
0f113f3e | 31 | case 2: l1|=((unsigned long)(*(--(c))))<<16; \ |
1567a821 | 32 | /* fall through */ \ |
0f113f3e MC |
33 | case 1: l1|=((unsigned long)(*(--(c))))<<24; \ |
34 | } \ | |
35 | } | |
58964a49 RE |
36 | |
37 | /* NOTE - c is not incremented as per l2n */ | |
0f113f3e MC |
38 | #define l2nn(l1,l2,c,n) { \ |
39 | c+=n; \ | |
40 | switch (n) { \ | |
41 | case 8: *(--(c))=(unsigned char)(((l2) )&0xff); \ | |
1567a821 | 42 | /* fall through */ \ |
0f113f3e | 43 | case 7: *(--(c))=(unsigned char)(((l2)>> 8)&0xff); \ |
1567a821 | 44 | /* fall through */ \ |
0f113f3e | 45 | case 6: *(--(c))=(unsigned char)(((l2)>>16)&0xff); \ |
1567a821 | 46 | /* fall through */ \ |
0f113f3e | 47 | case 5: *(--(c))=(unsigned char)(((l2)>>24)&0xff); \ |
1567a821 | 48 | /* fall through */ \ |
0f113f3e | 49 | case 4: *(--(c))=(unsigned char)(((l1) )&0xff); \ |
1567a821 | 50 | /* fall through */ \ |
0f113f3e | 51 | case 3: *(--(c))=(unsigned char)(((l1)>> 8)&0xff); \ |
1567a821 | 52 | /* fall through */ \ |
0f113f3e | 53 | case 2: *(--(c))=(unsigned char)(((l1)>>16)&0xff); \ |
1567a821 | 54 | /* fall through */ \ |
0f113f3e MC |
55 | case 1: *(--(c))=(unsigned char)(((l1)>>24)&0xff); \ |
56 | } \ | |
57 | } | |
58964a49 RE |
58 | |
59 | #undef n2l | |
60 | #define n2l(c,l) (l =((unsigned long)(*((c)++)))<<24L, \ | |
61 | l|=((unsigned long)(*((c)++)))<<16L, \ | |
62 | l|=((unsigned long)(*((c)++)))<< 8L, \ | |
63 | l|=((unsigned long)(*((c)++)))) | |
64 | ||
65 | #undef l2n | |
66 | #define l2n(l,c) (*((c)++)=(unsigned char)(((l)>>24L)&0xff), \ | |
67 | *((c)++)=(unsigned char)(((l)>>16L)&0xff), \ | |
68 | *((c)++)=(unsigned char)(((l)>> 8L)&0xff), \ | |
69 | *((c)++)=(unsigned char)(((l) )&0xff)) | |
70 | ||
cf1b7d96 | 71 | #if defined(OPENSSL_SYS_WIN32) && defined(_MSC_VER) |
0f113f3e | 72 | # define ROTL(a,n) (_lrotl(a,n)) |
58964a49 | 73 | #else |
cdbf5779 | 74 | # define ROTL(a,n) ((((a)<<(n))&0xffffffffL)|((a)>>((32-(n))&31))) |
58964a49 RE |
75 | #endif |
76 | ||
77 | #define C_M 0x3fc | |
78 | #define C_0 22L | |
79 | #define C_1 14L | |
80 | #define C_2 6L | |
0f113f3e | 81 | #define C_3 2L /* left shift */ |
58964a49 RE |
82 | |
83 | /* The rotate has an extra 16 added to it to help the x86 asm */ | |
84 | #if defined(CAST_PTR) | |
0f113f3e MC |
85 | # define E_CAST(n,key,L,R,OP1,OP2,OP3) \ |
86 | { \ | |
87 | int i; \ | |
88 | t=(key[n*2] OP1 R)&0xffffffffL; \ | |
89 | i=key[n*2+1]; \ | |
90 | t=ROTL(t,i); \ | |
91 | L^= (((((*(CAST_LONG *)((unsigned char *) \ | |
92 | CAST_S_table0+((t>>C_2)&C_M)) OP2 \ | |
93 | *(CAST_LONG *)((unsigned char *) \ | |
94 | CAST_S_table1+((t<<C_3)&C_M)))&0xffffffffL) OP3 \ | |
95 | *(CAST_LONG *)((unsigned char *) \ | |
96 | CAST_S_table2+((t>>C_0)&C_M)))&0xffffffffL) OP1 \ | |
97 | *(CAST_LONG *)((unsigned char *) \ | |
98 | CAST_S_table3+((t>>C_1)&C_M)))&0xffffffffL; \ | |
99 | } | |
58964a49 | 100 | #elif defined(CAST_PTR2) |
0f113f3e MC |
101 | # define E_CAST(n,key,L,R,OP1,OP2,OP3) \ |
102 | { \ | |
103 | int i; \ | |
104 | CAST_LONG u,v,w; \ | |
105 | w=(key[n*2] OP1 R)&0xffffffffL; \ | |
106 | i=key[n*2+1]; \ | |
107 | w=ROTL(w,i); \ | |
108 | u=w>>C_2; \ | |
109 | v=w<<C_3; \ | |
110 | u&=C_M; \ | |
111 | v&=C_M; \ | |
112 | t= *(CAST_LONG *)((unsigned char *)CAST_S_table0+u); \ | |
113 | u=w>>C_0; \ | |
114 | t=(t OP2 *(CAST_LONG *)((unsigned char *)CAST_S_table1+v))&0xffffffffL;\ | |
115 | v=w>>C_1; \ | |
116 | u&=C_M; \ | |
117 | v&=C_M; \ | |
118 | t=(t OP3 *(CAST_LONG *)((unsigned char *)CAST_S_table2+u)&0xffffffffL);\ | |
119 | t=(t OP1 *(CAST_LONG *)((unsigned char *)CAST_S_table3+v)&0xffffffffL);\ | |
120 | L^=(t&0xffffffff); \ | |
121 | } | |
58964a49 | 122 | #else |
0f113f3e MC |
123 | # define E_CAST(n,key,L,R,OP1,OP2,OP3) \ |
124 | { \ | |
125 | CAST_LONG a,b,c,d; \ | |
126 | t=(key[n*2] OP1 R)&0xffffffff; \ | |
127 | t=ROTL(t,(key[n*2+1])); \ | |
128 | a=CAST_S_table0[(t>> 8)&0xff]; \ | |
129 | b=CAST_S_table1[(t )&0xff]; \ | |
130 | c=CAST_S_table2[(t>>24)&0xff]; \ | |
131 | d=CAST_S_table3[(t>>16)&0xff]; \ | |
132 | L^=(((((a OP2 b)&0xffffffffL) OP3 c)&0xffffffffL) OP1 d)&0xffffffffL; \ | |
133 | } | |
58964a49 RE |
134 | #endif |
135 | ||
e62991a0 AP |
136 | extern const CAST_LONG CAST_S_table0[256]; |
137 | extern const CAST_LONG CAST_S_table1[256]; | |
138 | extern const CAST_LONG CAST_S_table2[256]; | |
139 | extern const CAST_LONG CAST_S_table3[256]; | |
140 | extern const CAST_LONG CAST_S_table4[256]; | |
141 | extern const CAST_LONG CAST_S_table5[256]; | |
142 | extern const CAST_LONG CAST_S_table6[256]; | |
143 | extern const CAST_LONG CAST_S_table7[256]; |