1 /* fix for MSVC ...evil! */
4 #define CONST64(n) n ## ui64
5 typedef unsigned __int64 ulong64;
7 #define CONST64(n) n ## ULL
8 typedef unsigned long long ulong64;
11 /* this is the "32-bit at least" data type
12 * Re-define it to suit your platform but it must be at least 32-bits
14 #if defined(__x86_64__) || (defined(__sparc__) && defined(__arch64__))
15 typedef unsigned ulong32;
17 typedef unsigned long ulong32;
21 /* ---- HELPER MACROS ---- */
24 #define STORE32L(x, y) \
25 { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
26 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
28 #define LOAD32L(x, y) \
29 { x = ((unsigned long)((y)[3] & 255)<<24) | \
30 ((unsigned long)((y)[2] & 255)<<16) | \
31 ((unsigned long)((y)[1] & 255)<<8) | \
32 ((unsigned long)((y)[0] & 255)); }
34 #define STORE64L(x, y) \
35 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
36 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
37 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
38 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
40 #define LOAD64L(x, y) \
41 { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
42 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
43 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
44 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
46 #define STORE32H(x, y) \
47 { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
48 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
50 #define LOAD32H(x, y) \
51 { x = ((unsigned long)((y)[0] & 255)<<24) | \
52 ((unsigned long)((y)[1] & 255)<<16) | \
53 ((unsigned long)((y)[2] & 255)<<8) | \
54 ((unsigned long)((y)[3] & 255)); }
56 #define STORE64H(x, y) \
57 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
58 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
59 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
60 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
62 #define LOAD64H(x, y) \
63 { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
64 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
65 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
66 (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); }
68 #endif /* ENDIAN_NEUTRAL */
72 #if !defined(LTC_NO_BSWAP) && (defined(INTEL_CC) || (defined(__GNUC__) && (defined(__DJGPP__) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__i386__) || defined(__x86_64__))))
74 #define STORE32H(x, y) \
81 #define LOAD32H(x, y) \
89 #define STORE32H(x, y) \
90 { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
91 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); }
93 #define LOAD32H(x, y) \
94 { x = ((unsigned long)((y)[0] & 255)<<24) | \
95 ((unsigned long)((y)[1] & 255)<<16) | \
96 ((unsigned long)((y)[2] & 255)<<8) | \
97 ((unsigned long)((y)[3] & 255)); }
102 /* x86_64 processor */
103 #if !defined(LTC_NO_BSWAP) && (defined(__GNUC__) && defined(__x86_64__))
105 #define STORE64H(x, y) \
112 #define LOAD64H(x, y) \
120 #define STORE64H(x, y) \
121 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
122 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
123 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
124 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
126 #define LOAD64H(x, y) \
127 { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
128 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
129 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
130 (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); }
134 #ifdef ENDIAN_32BITWORD
136 #define STORE32L(x, y) \
137 { ulong32 __t = (x); XMEMCPY(y, &__t, 4); }
139 #define LOAD32L(x, y) \
142 #define STORE64L(x, y) \
143 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
144 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
145 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
146 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
148 #define LOAD64L(x, y) \
149 { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
150 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
151 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
152 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
154 #else /* 64-bit words then */
156 #define STORE32L(x, y) \
157 { ulong32 __t = (x); XMEMCPY(y, &__t, 4); }
159 #define LOAD32L(x, y) \
160 { XMEMCPY(&(x), y, 4); x &= 0xFFFFFFFF; }
162 #define STORE64L(x, y) \
163 { ulong64 __t = (x); XMEMCPY(y, &__t, 8); }
165 #define LOAD64L(x, y) \
166 { XMEMCPY(&(x), y, 8); }
168 #endif /* ENDIAN_64BITWORD */
170 #endif /* ENDIAN_LITTLE */
173 #define STORE32L(x, y) \
174 { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
175 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
177 #define LOAD32L(x, y) \
178 { x = ((unsigned long)((y)[3] & 255)<<24) | \
179 ((unsigned long)((y)[2] & 255)<<16) | \
180 ((unsigned long)((y)[1] & 255)<<8) | \
181 ((unsigned long)((y)[0] & 255)); }
183 #define STORE64L(x, y) \
184 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
185 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
186 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
187 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
189 #define LOAD64L(x, y) \
190 { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48) | \
191 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32) | \
192 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16) | \
193 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
195 #ifdef ENDIAN_32BITWORD
197 #define STORE32H(x, y) \
198 { ulong32 __t = (x); XMEMCPY(y, &__t, 4); }
200 #define LOAD32H(x, y) \
203 #define STORE64H(x, y) \
204 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
205 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
206 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
207 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
209 #define LOAD64H(x, y) \
210 { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48)| \
211 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32)| \
212 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16)| \
213 (((ulong64)((y)[6] & 255))<<8)| (((ulong64)((y)[7] & 255))); }
215 #else /* 64-bit words then */
217 #define STORE32H(x, y) \
218 { ulong32 __t = (x); XMEMCPY(y, &__t, 4); }
220 #define LOAD32H(x, y) \
221 { XMEMCPY(&(x), y, 4); x &= 0xFFFFFFFF; }
223 #define STORE64H(x, y) \
224 { ulong64 __t = (x); XMEMCPY(y, &__t, 8); }
226 #define LOAD64H(x, y) \
227 { XMEMCPY(&(x), y, 8); }
229 #endif /* ENDIAN_64BITWORD */
230 #endif /* ENDIAN_BIG */
232 #define BSWAP(x) ( ((x>>24)&0x000000FFUL) | ((x<<24)&0xFF000000UL) | \
233 ((x>>8)&0x0000FF00UL) | ((x<<8)&0x00FF0000UL) )
237 #if defined(_MSC_VER)
239 /* instrinsic rotate */
241 #pragma intrinsic(_lrotr,_lrotl)
242 #define ROR(x,n) _lrotr(x,n)
243 #define ROL(x,n) _lrotl(x,n)
244 #define RORc(x,n) _lrotr(x,n)
245 #define ROLc(x,n) _lrotl(x,n)
247 #elif !defined(__STRICT_ANSI__) && defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(INTEL_CC) && !defined(LTC_NO_ASM)
249 static inline unsigned ROL(unsigned word, int i)
253 :"0" (word),"c" (i));
257 static inline unsigned ROR(unsigned word, int i)
261 :"0" (word),"c" (i));
267 static inline unsigned ROLc(unsigned word, const int i)
271 :"0" (word),"I" (i));
275 static inline unsigned RORc(unsigned word, const int i)
279 :"0" (word),"I" (i));
290 #elif !defined(__STRICT_ANSI__) && defined(LTC_PPC32)
292 static inline unsigned ROL(unsigned word, int i)
294 asm ("rotlw %0,%0,%2"
296 :"0" (word),"r" (i));
300 static inline unsigned ROR(unsigned word, int i)
302 asm ("rotlw %0,%0,%2"
304 :"0" (word),"r" (32-i));
310 static inline unsigned ROLc(unsigned word, const int i)
312 asm ("rotlwi %0,%0,%2"
314 :"0" (word),"I" (i));
318 static inline unsigned RORc(unsigned word, const int i)
320 asm ("rotrwi %0,%0,%2"
322 :"0" (word),"I" (i));
336 /* rotates the hard way */
337 #define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
338 #define ROR(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
339 #define ROLc(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
340 #define RORc(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
346 #if !defined(__STRICT_ANSI__) && defined(__GNUC__) && defined(__x86_64__) && !defined(LTC_NO_ASM)
348 static inline unsigned long ROL64(unsigned long word, int i)
352 :"0" (word),"c" (i));
356 static inline unsigned long ROR64(unsigned long word, int i)
360 :"0" (word),"c" (i));
366 static inline unsigned long ROL64c(unsigned long word, const int i)
370 :"0" (word),"J" (i));
374 static inline unsigned long ROR64c(unsigned long word, const int i)
378 :"0" (word),"J" (i));
382 #else /* LTC_NO_ROLC */
389 #else /* Not x86_64 */
391 #define ROL64(x, y) \
392 ( (((x)<<((ulong64)(y)&63)) | \
393 (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF))
395 #define ROR64(x, y) \
396 ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \
397 ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF))
399 #define ROL64c(x, y) \
400 ( (((x)<<((ulong64)(y)&63)) | \
401 (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF))
403 #define ROR64c(x, y) \
404 ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \
405 ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF))
410 #define MAX(x, y) ( ((x)>(y))?(x):(y) )
414 #define MIN(x, y) ( ((x)<(y))?(x):(y) )
417 /* extract a byte portably */
419 #define byte(x, n) ((unsigned char)((x) >> (8 * (n))))
421 #define byte(x, n) (((x) >> (8 * (n))) & 255)
424 /* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_macros.h,v $ */
425 /* $Revision: 1.15 $ */
426 /* $Date: 2006/11/29 23:43:57 $ */