Mercurial > dropbear
comparison mycrypt_macros.h @ 0:d7da3b1e1540 libtomcrypt
put back the 0.95 makefile which was inadvertently merged over
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 31 May 2004 18:21:40 +0000 |
parents | |
children | 901233045998 5d99163f7e32 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d7da3b1e1540 |
---|---|
1 /* fix for MSVC ...evil! */ | |
2 #ifdef _MSC_VER | |
3 #define CONST64(n) n ## ui64 | |
4 typedef unsigned __int64 ulong64; | |
5 #else | |
6 #define CONST64(n) n ## ULL | |
7 typedef unsigned long long ulong64; | |
8 #endif | |
9 | |
10 /* this is the "32-bit at least" data type | |
11 * Re-define it to suit your platform but it must be at least 32-bits | |
12 */ | |
13 typedef unsigned long ulong32; | |
14 | |
15 /* ---- HELPER MACROS ---- */ | |
16 #ifdef ENDIAN_NEUTRAL | |
17 | |
18 #define STORE32L(x, y) \ | |
19 { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ | |
20 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } | |
21 | |
22 #define LOAD32L(x, y) \ | |
23 { x = ((unsigned long)((y)[3] & 255)<<24) | \ | |
24 ((unsigned long)((y)[2] & 255)<<16) | \ | |
25 ((unsigned long)((y)[1] & 255)<<8) | \ | |
26 ((unsigned long)((y)[0] & 255)); } | |
27 | |
28 #define STORE64L(x, y) \ | |
29 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \ | |
30 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \ | |
31 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ | |
32 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } | |
33 | |
34 #define LOAD64L(x, y) \ | |
35 { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \ | |
36 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \ | |
37 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \ | |
38 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } | |
39 | |
40 #define STORE32H(x, y) \ | |
41 { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \ | |
42 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } | |
43 | |
44 #define LOAD32H(x, y) \ | |
45 { x = ((unsigned long)((y)[0] & 255)<<24) | \ | |
46 ((unsigned long)((y)[1] & 255)<<16) | \ | |
47 ((unsigned long)((y)[2] & 255)<<8) | \ | |
48 ((unsigned long)((y)[3] & 255)); } | |
49 | |
50 #define STORE64H(x, y) \ | |
51 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ | |
52 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \ | |
53 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \ | |
54 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } | |
55 | |
56 #define LOAD64H(x, y) \ | |
57 { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \ | |
58 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \ | |
59 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \ | |
60 (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); } | |
61 | |
62 #endif /* ENDIAN_NEUTRAL */ | |
63 | |
64 #ifdef ENDIAN_LITTLE | |
65 | |
66 #define STORE32H(x, y) \ | |
67 { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \ | |
68 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } | |
69 | |
70 #define LOAD32H(x, y) \ | |
71 { x = ((unsigned long)((y)[0] & 255)<<24) | \ | |
72 ((unsigned long)((y)[1] & 255)<<16) | \ | |
73 ((unsigned long)((y)[2] & 255)<<8) | \ | |
74 ((unsigned long)((y)[3] & 255)); } | |
75 | |
76 #define STORE64H(x, y) \ | |
77 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ | |
78 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \ | |
79 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \ | |
80 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } | |
81 | |
82 #define LOAD64H(x, y) \ | |
83 { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \ | |
84 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \ | |
85 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \ | |
86 (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); } | |
87 | |
88 #ifdef ENDIAN_32BITWORD | |
89 | |
90 #define STORE32L(x, y) \ | |
91 { unsigned long __t = (x); memcpy(y, &__t, 4); } | |
92 | |
93 #define LOAD32L(x, y) \ | |
94 memcpy(&(x), y, 4); | |
95 | |
96 #define STORE64L(x, y) \ | |
97 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \ | |
98 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \ | |
99 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ | |
100 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } | |
101 | |
102 #define LOAD64L(x, y) \ | |
103 { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \ | |
104 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \ | |
105 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \ | |
106 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } | |
107 | |
108 #else /* 64-bit words then */ | |
109 | |
110 #define STORE32L(x, y) \ | |
111 { unsigned long __t = (x); memcpy(y, &__t, 4); } | |
112 | |
113 #define LOAD32L(x, y) \ | |
114 { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; } | |
115 | |
116 #define STORE64L(x, y) \ | |
117 { ulong64 __t = (x); memcpy(y, &__t, 8); } | |
118 | |
119 #define LOAD64L(x, y) \ | |
120 { memcpy(&(x), y, 8); } | |
121 | |
122 #endif /* ENDIAN_64BITWORD */ | |
123 | |
124 #endif /* ENDIAN_LITTLE */ | |
125 | |
126 #ifdef ENDIAN_BIG | |
127 #define STORE32L(x, y) \ | |
128 { (y)[z0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \ | |
129 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } | |
130 | |
131 #define LOAD32L(x, y) \ | |
132 { x = ((unsigned long)((y)[0] & 255)<<24) | \ | |
133 ((unsigned long)((y)[1] & 255)<<16) | \ | |
134 ((unsigned long)((y)[2] & 255)<<8) | \ | |
135 ((unsigned long)((y)[3] & 255)); } | |
136 | |
137 #define STORE64L(x, y) \ | |
138 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ | |
139 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \ | |
140 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \ | |
141 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } | |
142 | |
143 #define LOAD64L(x, y) \ | |
144 { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \ | |
145 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \ | |
146 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \ | |
147 (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); } | |
148 | |
149 #ifdef ENDIAN_32BITWORD | |
150 | |
151 #define STORE32H(x, y) \ | |
152 { unsigned long __t = (x); memcpy(y, &__t, 4); } | |
153 | |
154 #define LOAD32H(x, y) \ | |
155 memcpy(&(x), y, 4); | |
156 | |
157 #define STORE64H(x, y) \ | |
158 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \ | |
159 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \ | |
160 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ | |
161 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } | |
162 | |
163 #define LOAD64H(x, y) \ | |
164 { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \ | |
165 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \ | |
166 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \ | |
167 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } | |
168 | |
169 #else /* 64-bit words then */ | |
170 | |
171 #define STORE32H(x, y) \ | |
172 { unsigned long __t = (x); memcpy(y, &__t, 4); } | |
173 | |
174 #define LOAD32H(x, y) \ | |
175 { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; } | |
176 | |
177 #define STORE64H(x, y) \ | |
178 { ulong64 __t = (x); memcpy(y, &__t, 8); } | |
179 | |
180 #define LOAD64H(x, y) \ | |
181 { memcpy(&(x), y, 8); } | |
182 | |
183 #endif /* ENDIAN_64BITWORD */ | |
184 #endif /* ENDIAN_BIG */ | |
185 | |
186 #define BSWAP(x) ( ((x>>24)&0x000000FFUL) | ((x<<24)&0xFF000000UL) | \ | |
187 ((x>>8)&0x0000FF00UL) | ((x<<8)&0x00FF0000UL) ) | |
188 | |
189 #ifdef _MSC_VER | |
190 | |
191 /* instrinsic rotate */ | |
192 #include <stdlib.h> | |
193 #pragma intrinsic(_lrotr,_lrotl) | |
194 #define ROR(x,n) _lrotr(x,n) | |
195 #define ROL(x,n) _lrotl(x,n) | |
196 | |
197 #elif defined(__GNUC__) && defined(__i386__) && !defined(INTEL_CC) | |
198 | |
199 static inline unsigned long ROL(unsigned long word, int i) | |
200 { | |
201 __asm__("roll %%cl,%0" | |
202 :"=r" (word) | |
203 :"0" (word),"c" (i)); | |
204 return word; | |
205 } | |
206 | |
207 static inline unsigned long ROR(unsigned long word, int i) | |
208 { | |
209 __asm__("rorl %%cl,%0" | |
210 :"=r" (word) | |
211 :"0" (word),"c" (i)); | |
212 return word; | |
213 } | |
214 | |
215 #else | |
216 | |
217 /* rotates the hard way */ | |
218 #define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) | |
219 #define ROR(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) | |
220 | |
221 #endif | |
222 | |
223 #define ROL64(x, y) \ | |
224 ( (((x)<<((ulong64)(y)&63)) | \ | |
225 (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF)) | |
226 | |
227 #define ROR64(x, y) \ | |
228 ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \ | |
229 ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF)) | |
230 | |
231 #undef MAX | |
232 #undef MIN | |
233 #define MAX(x, y) ( ((x)>(y))?(x):(y) ) | |
234 #define MIN(x, y) ( ((x)<(y))?(x):(y) ) | |
235 | |
236 /* extract a byte portably */ | |
237 #ifdef _MSC_VER | |
238 #define byte(x, n) ((unsigned char)((x) >> (8 * (n)))) | |
239 #else | |
240 #define byte(x, n) (((x) >> (8 * (n))) & 255) | |
241 #endif |