Mercurial > dropbear
comparison libtomcrypt/src/headers/tomcrypt_macros.h @ 415:8b9aba1d5fa4 channel-fix
merge of '73fe066c5d9e2395354ba74756124d45c978a04d'
and 'f5014cc84558f1e8eba42dbecf9f72f94bfe6134'
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 06 Feb 2007 16:00:18 +0000 |
parents | 0cbe8f6dbf9e |
children | f849a5ca2efc |
comparison
equal
deleted
inserted
replaced
414:c53a26c430e5 | 415:8b9aba1d5fa4 |
---|---|
8 #endif | 8 #endif |
9 | 9 |
10 /* this is the "32-bit at least" data type | 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 | 11 * Re-define it to suit your platform but it must be at least 32-bits |
12 */ | 12 */ |
13 #if defined(__x86_64__) | 13 #if defined(__x86_64__) || (defined(__sparc__) && defined(__arch64__)) |
14 typedef unsigned ulong32; | 14 typedef unsigned ulong32; |
15 #else | 15 #else |
16 typedef unsigned long ulong32; | 16 typedef unsigned long ulong32; |
17 #endif | 17 #endif |
18 | 18 |
70 #if !defined(LTC_NO_BSWAP) && (defined(INTEL_CC) || (defined(__GNUC__) && (defined(__DJGPP__) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__i386__) || defined(__x86_64__)))) | 70 #if !defined(LTC_NO_BSWAP) && (defined(INTEL_CC) || (defined(__GNUC__) && (defined(__DJGPP__) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__i386__) || defined(__x86_64__)))) |
71 | 71 |
72 #define STORE32H(x, y) \ | 72 #define STORE32H(x, y) \ |
73 asm __volatile__ ( \ | 73 asm __volatile__ ( \ |
74 "bswapl %0 \n\t" \ | 74 "bswapl %0 \n\t" \ |
75 "movl %0,(%2)\n\t" \ | 75 "movl %0,(%1)\n\t" \ |
76 "bswapl %0 \n\t" \ | 76 "bswapl %0 \n\t" \ |
77 :"=r"(x):"0"(x), "r"(y)); | 77 ::"r"(x), "r"(y)); |
78 | 78 |
79 #define LOAD32H(x, y) \ | 79 #define LOAD32H(x, y) \ |
80 asm __volatile__ ( \ | 80 asm __volatile__ ( \ |
81 "movl (%2),%0\n\t" \ | 81 "movl (%1),%0\n\t" \ |
82 "bswapl %0\n\t" \ | 82 "bswapl %0\n\t" \ |
83 :"=r"(x): "0"(x), "r"(y)); | 83 :"=r"(x): "r"(y)); |
84 | 84 |
85 #else | 85 #else |
86 | 86 |
87 #define STORE32H(x, y) \ | 87 #define STORE32H(x, y) \ |
88 { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \ | 88 { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \ |
101 #if !defined(LTC_NO_BSWAP) && (defined(__GNUC__) && defined(__x86_64__)) | 101 #if !defined(LTC_NO_BSWAP) && (defined(__GNUC__) && defined(__x86_64__)) |
102 | 102 |
103 #define STORE64H(x, y) \ | 103 #define STORE64H(x, y) \ |
104 asm __volatile__ ( \ | 104 asm __volatile__ ( \ |
105 "bswapq %0 \n\t" \ | 105 "bswapq %0 \n\t" \ |
106 "movq %0,(%2)\n\t" \ | 106 "movq %0,(%1)\n\t" \ |
107 "bswapq %0 \n\t" \ | 107 "bswapq %0 \n\t" \ |
108 :"=r"(x):"0"(x), "r"(y):"0"); | 108 ::"r"(x), "r"(y)); |
109 | 109 |
110 #define LOAD64H(x, y) \ | 110 #define LOAD64H(x, y) \ |
111 asm __volatile__ ( \ | 111 asm __volatile__ ( \ |
112 "movq (%2),%0\n\t" \ | 112 "movq (%1),%0\n\t" \ |
113 "bswapq %0\n\t" \ | 113 "bswapq %0\n\t" \ |
114 :"=r"(x): "0"(x), "r"(y)); | 114 :"=r"(x): "r"(y)); |
115 | 115 |
116 #else | 116 #else |
117 | 117 |
118 #define STORE64H(x, y) \ | 118 #define STORE64H(x, y) \ |
119 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ | 119 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ |
130 #endif | 130 #endif |
131 | 131 |
132 #ifdef ENDIAN_32BITWORD | 132 #ifdef ENDIAN_32BITWORD |
133 | 133 |
134 #define STORE32L(x, y) \ | 134 #define STORE32L(x, y) \ |
135 { ulong32 __t = (x); memcpy(y, &__t, 4); } | 135 { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } |
136 | 136 |
137 #define LOAD32L(x, y) \ | 137 #define LOAD32L(x, y) \ |
138 memcpy(&(x), y, 4); | 138 XMEMCPY(&(x), y, 4); |
139 | 139 |
140 #define STORE64L(x, y) \ | 140 #define STORE64L(x, y) \ |
141 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \ | 141 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \ |
142 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \ | 142 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \ |
143 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ | 143 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ |
150 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } | 150 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } |
151 | 151 |
152 #else /* 64-bit words then */ | 152 #else /* 64-bit words then */ |
153 | 153 |
154 #define STORE32L(x, y) \ | 154 #define STORE32L(x, y) \ |
155 { ulong32 __t = (x); memcpy(y, &__t, 4); } | 155 { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } |
156 | 156 |
157 #define LOAD32L(x, y) \ | 157 #define LOAD32L(x, y) \ |
158 { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; } | 158 { XMEMCPY(&(x), y, 4); x &= 0xFFFFFFFF; } |
159 | 159 |
160 #define STORE64L(x, y) \ | 160 #define STORE64L(x, y) \ |
161 { ulong64 __t = (x); memcpy(y, &__t, 8); } | 161 { ulong64 __t = (x); XMEMCPY(y, &__t, 8); } |
162 | 162 |
163 #define LOAD64L(x, y) \ | 163 #define LOAD64L(x, y) \ |
164 { memcpy(&(x), y, 8); } | 164 { XMEMCPY(&(x), y, 8); } |
165 | 165 |
166 #endif /* ENDIAN_64BITWORD */ | 166 #endif /* ENDIAN_64BITWORD */ |
167 | 167 |
168 #endif /* ENDIAN_LITTLE */ | 168 #endif /* ENDIAN_LITTLE */ |
169 | 169 |
191 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } | 191 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } |
192 | 192 |
193 #ifdef ENDIAN_32BITWORD | 193 #ifdef ENDIAN_32BITWORD |
194 | 194 |
195 #define STORE32H(x, y) \ | 195 #define STORE32H(x, y) \ |
196 { ulong32 __t = (x); memcpy(y, &__t, 4); } | 196 { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } |
197 | 197 |
198 #define LOAD32H(x, y) \ | 198 #define LOAD32H(x, y) \ |
199 memcpy(&(x), y, 4); | 199 XMEMCPY(&(x), y, 4); |
200 | 200 |
201 #define STORE64H(x, y) \ | 201 #define STORE64H(x, y) \ |
202 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ | 202 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ |
203 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \ | 203 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \ |
204 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \ | 204 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \ |
211 (((ulong64)((y)[6] & 255))<<8)| (((ulong64)((y)[7] & 255))); } | 211 (((ulong64)((y)[6] & 255))<<8)| (((ulong64)((y)[7] & 255))); } |
212 | 212 |
213 #else /* 64-bit words then */ | 213 #else /* 64-bit words then */ |
214 | 214 |
215 #define STORE32H(x, y) \ | 215 #define STORE32H(x, y) \ |
216 { ulong32 __t = (x); memcpy(y, &__t, 4); } | 216 { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } |
217 | 217 |
218 #define LOAD32H(x, y) \ | 218 #define LOAD32H(x, y) \ |
219 { memcpy(&(x), y, 4); x &= 0xFFFFFFFF; } | 219 { XMEMCPY(&(x), y, 4); x &= 0xFFFFFFFF; } |
220 | 220 |
221 #define STORE64H(x, y) \ | 221 #define STORE64H(x, y) \ |
222 { ulong64 __t = (x); memcpy(y, &__t, 8); } | 222 { ulong64 __t = (x); XMEMCPY(y, &__t, 8); } |
223 | 223 |
224 #define LOAD64H(x, y) \ | 224 #define LOAD64H(x, y) \ |
225 { memcpy(&(x), y, 8); } | 225 { XMEMCPY(&(x), y, 8); } |
226 | 226 |
227 #endif /* ENDIAN_64BITWORD */ | 227 #endif /* ENDIAN_64BITWORD */ |
228 #endif /* ENDIAN_BIG */ | 228 #endif /* ENDIAN_BIG */ |
229 | 229 |
230 #define BSWAP(x) ( ((x>>24)&0x000000FFUL) | ((x<<24)&0xFF000000UL) | \ | 230 #define BSWAP(x) ( ((x>>24)&0x000000FFUL) | ((x<<24)&0xFF000000UL) | \ |
240 #define ROR(x,n) _lrotr(x,n) | 240 #define ROR(x,n) _lrotr(x,n) |
241 #define ROL(x,n) _lrotl(x,n) | 241 #define ROL(x,n) _lrotl(x,n) |
242 #define RORc(x,n) _lrotr(x,n) | 242 #define RORc(x,n) _lrotr(x,n) |
243 #define ROLc(x,n) _lrotl(x,n) | 243 #define ROLc(x,n) _lrotl(x,n) |
244 | 244 |
245 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(INTEL_CC) && !defined(LTC_NO_ASM) | 245 #elif !defined(__STRICT_ANSI__) && defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(INTEL_CC) && !defined(LTC_NO_ASM) |
246 | 246 |
247 static inline unsigned ROL(unsigned word, int i) | 247 static inline unsigned ROL(unsigned word, int i) |
248 { | 248 { |
249 asm ("roll %%cl,%0" | 249 asm ("roll %%cl,%0" |
250 :"=r" (word) | 250 :"=r" (word) |
282 | 282 |
283 #define ROLc ROL | 283 #define ROLc ROL |
284 #define RORc ROR | 284 #define RORc ROR |
285 | 285 |
286 #endif | 286 #endif |
287 | |
288 #elif !defined(__STRICT_ANSI__) && defined(LTC_PPC32) | |
289 | |
290 static inline unsigned ROL(unsigned word, int i) | |
291 { | |
292 asm ("rotlw %0,%0,%2" | |
293 :"=r" (word) | |
294 :"0" (word),"r" (i)); | |
295 return word; | |
296 } | |
297 | |
298 static inline unsigned ROR(unsigned word, int i) | |
299 { | |
300 asm ("rotlw %0,%0,%2" | |
301 :"=r" (word) | |
302 :"0" (word),"r" (32-i)); | |
303 return word; | |
304 } | |
305 | |
306 #ifndef LTC_NO_ROLC | |
307 | |
308 static inline unsigned ROLc(unsigned word, const int i) | |
309 { | |
310 asm ("rotlwi %0,%0,%2" | |
311 :"=r" (word) | |
312 :"0" (word),"I" (i)); | |
313 return word; | |
314 } | |
315 | |
316 static inline unsigned RORc(unsigned word, const int i) | |
317 { | |
318 asm ("rotrwi %0,%0,%2" | |
319 :"=r" (word) | |
320 :"0" (word),"I" (i)); | |
321 return word; | |
322 } | |
323 | |
324 #else | |
325 | |
326 #define ROLc ROL | |
327 #define RORc ROR | |
328 | |
329 #endif | |
330 | |
287 | 331 |
288 #else | 332 #else |
289 | 333 |
290 /* rotates the hard way */ | 334 /* rotates the hard way */ |
291 #define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) | 335 #define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) |
295 | 339 |
296 #endif | 340 #endif |
297 | 341 |
298 | 342 |
299 /* 64-bit Rotates */ | 343 /* 64-bit Rotates */ |
300 #if defined(__GNUC__) && defined(__x86_64__) && !defined(LTC_NO_ASM) | 344 #if !defined(__STRICT_ANSI__) && defined(__GNUC__) && defined(__x86_64__) && !defined(LTC_NO_ASM) |
301 | 345 |
302 static inline unsigned long ROL64(unsigned long word, int i) | 346 static inline unsigned long ROL64(unsigned long word, int i) |
303 { | 347 { |
304 asm("rolq %%cl,%0" | 348 asm("rolq %%cl,%0" |
305 :"=r" (word) | 349 :"=r" (word) |
358 ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \ | 402 ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \ |
359 ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF)) | 403 ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF)) |
360 | 404 |
361 #endif | 405 #endif |
362 | 406 |
363 #undef MAX | 407 #ifndef MAX |
364 #undef MIN | 408 #define MAX(x, y) ( ((x)>(y))?(x):(y) ) |
365 #define MAX(x, y) ( ((x)>(y))?(x):(y) ) | 409 #endif |
366 #define MIN(x, y) ( ((x)<(y))?(x):(y) ) | 410 |
411 #ifndef MIN | |
412 #define MIN(x, y) ( ((x)<(y))?(x):(y) ) | |
413 #endif | |
367 | 414 |
368 /* extract a byte portably */ | 415 /* extract a byte portably */ |
369 #ifdef _MSC_VER | 416 #ifdef _MSC_VER |
370 #define byte(x, n) ((unsigned char)((x) >> (8 * (n)))) | 417 #define byte(x, n) ((unsigned char)((x) >> (8 * (n)))) |
371 #else | 418 #else |
372 #define byte(x, n) (((x) >> (8 * (n))) & 255) | 419 #define byte(x, n) (((x) >> (8 * (n))) & 255) |
373 #endif | 420 #endif |
374 | 421 |
375 /* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_macros.h,v $ */ | 422 /* $Source: /cvs/libtom/libtomcrypt/src/headers/tomcrypt_macros.h,v $ */ |
376 /* $Revision: 1.7 $ */ | 423 /* $Revision: 1.15 $ */ |
377 /* $Date: 2005/05/05 14:35:58 $ */ | 424 /* $Date: 2006/11/29 23:43:57 $ */ |