comparison mycrypt_macros.h @ 143:5d99163f7e32 libtomcrypt-orig

import of libtomcrypt 0.99
author Matt Johnston <matt@ucc.asn.au>
date Sun, 19 Dec 2004 11:34:45 +0000
parents 7faae8f46238
children 7ed585a2c53b
comparison
equal deleted inserted replaced
15:6362d3854bb4 143:5d99163f7e32
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 typedef unsigned long ulong32; 13 #if defined(__x86_64__)
14 typedef unsigned ulong32;
15 #else
16 typedef unsigned long ulong32;
17 #endif
14 18
15 /* ---- HELPER MACROS ---- */ 19 /* ---- HELPER MACROS ---- */
16 #ifdef ENDIAN_NEUTRAL 20 #ifdef ENDIAN_NEUTRAL
17 21
18 #define STORE32L(x, y) \ 22 #define STORE32L(x, y) \
123 127
124 #endif /* ENDIAN_LITTLE */ 128 #endif /* ENDIAN_LITTLE */
125 129
126 #ifdef ENDIAN_BIG 130 #ifdef ENDIAN_BIG
127 #define STORE32L(x, y) \ 131 #define STORE32L(x, y) \
128 { (y)[z0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \ 132 { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
129 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } 133 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
130 134
131 #define LOAD32L(x, y) \ 135 #define LOAD32L(x, y) \
132 { x = ((unsigned long)((y)[0] & 255)<<24) | \ 136 { x = ((unsigned long)((y)[3] & 255)<<24) | \
133 ((unsigned long)((y)[1] & 255)<<16) | \ 137 ((unsigned long)((y)[2] & 255)<<16) | \
134 ((unsigned long)((y)[2] & 255)<<8) | \ 138 ((unsigned long)((y)[1] & 255)<<8) | \
135 ((unsigned long)((y)[3] & 255)); } 139 ((unsigned long)((y)[0] & 255)); }
136 140
137 #define STORE64L(x, y) \ 141 #define STORE64L(x, y) \
138 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \ 142 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
139 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \ 143 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
140 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \ 144 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
141 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } 145 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
142 146
143 #define LOAD64L(x, y) \ 147 #define LOAD64L(x, y) \
144 { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \ 148 { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48) | \
145 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \ 149 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32) | \
146 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \ 150 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16) | \
147 (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); } 151 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); }
148 152
149 #ifdef ENDIAN_32BITWORD 153 #ifdef ENDIAN_32BITWORD
150 154
151 #define STORE32H(x, y) \ 155 #define STORE32H(x, y) \
152 { unsigned long __t = (x); memcpy(y, &__t, 4); } 156 { unsigned long __t = (x); memcpy(y, &__t, 4); }
153 157
154 #define LOAD32H(x, y) \ 158 #define LOAD32H(x, y) \
155 memcpy(&(x), y, 4); 159 memcpy(&(x), y, 4);
156 160
157 #define STORE64H(x, y) \ 161 #define STORE64H(x, y) \
158 { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \ 162 { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
159 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \ 163 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
160 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \ 164 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
161 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } 165 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); }
162 166
163 #define LOAD64H(x, y) \ 167 #define LOAD64H(x, y) \
164 { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \ 168 { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48)| \
165 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \ 169 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32)| \
166 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \ 170 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16)| \
167 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } 171 (((ulong64)((y)[6] & 255))<<8)| (((ulong64)((y)[7] & 255))); }
168 172
169 #else /* 64-bit words then */ 173 #else /* 64-bit words then */
170 174
171 #define STORE32H(x, y) \ 175 #define STORE32H(x, y) \
172 { unsigned long __t = (x); memcpy(y, &__t, 4); } 176 { unsigned long __t = (x); memcpy(y, &__t, 4); }
192 #include <stdlib.h> 196 #include <stdlib.h>
193 #pragma intrinsic(_lrotr,_lrotl) 197 #pragma intrinsic(_lrotr,_lrotl)
194 #define ROR(x,n) _lrotr(x,n) 198 #define ROR(x,n) _lrotr(x,n)
195 #define ROL(x,n) _lrotl(x,n) 199 #define ROL(x,n) _lrotl(x,n)
196 200
197 #elif defined(__GNUC__) && defined(__i386__) && !defined(INTEL_CC) 201 #elif defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(INTEL_CC)
198 202
199 static inline unsigned long ROL(unsigned long word, int i) 203 static inline unsigned ROL(unsigned word, int i)
200 { 204 {
201 __asm__("roll %%cl,%0" 205 __asm__("roll %%cl,%0"
202 :"=r" (word) 206 :"=r" (word)
203 :"0" (word),"c" (i)); 207 :"0" (word),"c" (i));
204 return word; 208 return word;
205 } 209 }
206 210
207 static inline unsigned long ROR(unsigned long word, int i) 211 static inline unsigned ROR(unsigned word, int i)
208 { 212 {
209 __asm__("rorl %%cl,%0" 213 __asm__("rorl %%cl,%0"
210 :"=r" (word) 214 :"=r" (word)
211 :"0" (word),"c" (i)); 215 :"0" (word),"c" (i));
212 return word; 216 return word;
218 #define ROL(x, y) ( (((unsigned long)(x)<<(unsigned long)((y)&31)) | (((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL) 222 #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) 223 #define ROR(x, y) ( ((((unsigned long)(x)&0xFFFFFFFFUL)>>(unsigned long)((y)&31)) | ((unsigned long)(x)<<(unsigned long)(32-((y)&31)))) & 0xFFFFFFFFUL)
220 224
221 #endif 225 #endif
222 226
227 #if defined(__GNUCC__) && defined(__x86_64__)
228
229 static inline unsigned long ROL64(unsigned long word, int i)
230 {
231 __asm__("rolq %%cl,%0"
232 :"=r" (word)
233 :"0" (word),"c" (i));
234 return word;
235 }
236
237 static inline unsigned long ROR64(unsigned long word, int i)
238 {
239 __asm__("rorq %%cl,%0"
240 :"=r" (word)
241 :"0" (word),"c" (i));
242 return word;
243 }
244
245 #else
246
223 #define ROL64(x, y) \ 247 #define ROL64(x, y) \
224 ( (((x)<<((ulong64)(y)&63)) | \ 248 ( (((x)<<((ulong64)(y)&63)) | \
225 (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF)) 249 (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)64-((y)&63)))) & CONST64(0xFFFFFFFFFFFFFFFF))
226 250
227 #define ROR64(x, y) \ 251 #define ROR64(x, y) \
228 ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \ 252 ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \
229 ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF)) 253 ((x)<<((ulong64)(64-((y)&CONST64(63)))))) & CONST64(0xFFFFFFFFFFFFFFFF))
254
255 #endif
230 256
231 #undef MAX 257 #undef MAX
232 #undef MIN 258 #undef MIN
233 #define MAX(x, y) ( ((x)>(y))?(x):(y) ) 259 #define MAX(x, y) ( ((x)>(y))?(x):(y) )
234 #define MIN(x, y) ( ((x)<(y))?(x):(y) ) 260 #define MIN(x, y) ( ((x)<(y))?(x):(y) )