summaryrefslogtreecommitdiff
path: root/utils/tomcrypt/src/headers/tomcrypt_macros.h
diff options
context:
space:
mode:
Diffstat (limited to 'utils/tomcrypt/src/headers/tomcrypt_macros.h')
-rw-r--r--utils/tomcrypt/src/headers/tomcrypt_macros.h446
1 files changed, 446 insertions, 0 deletions
diff --git a/utils/tomcrypt/src/headers/tomcrypt_macros.h b/utils/tomcrypt/src/headers/tomcrypt_macros.h
new file mode 100644
index 0000000000..a3a335e455
--- /dev/null
+++ b/utils/tomcrypt/src/headers/tomcrypt_macros.h
@@ -0,0 +1,446 @@
1/* LibTomCrypt, modular cryptographic library -- Tom St Denis
2 *
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
5 *
6 * The library is free for all purposes without any express
7 * guarantee it works.
8 */
9
10/* ---- HELPER MACROS ---- */
11#ifdef ENDIAN_NEUTRAL
12
13#define STORE32L(x, y) \
14 do { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
15 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
16
17#define LOAD32L(x, y) \
18 do { x = ((ulong32)((y)[3] & 255)<<24) | \
19 ((ulong32)((y)[2] & 255)<<16) | \
20 ((ulong32)((y)[1] & 255)<<8) | \
21 ((ulong32)((y)[0] & 255)); } while(0)
22
23#define STORE64L(x, y) \
24 do { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
25 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
26 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
27 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
28
29#define LOAD64L(x, y) \
30 do { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
31 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
32 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
33 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } while(0)
34
35#define STORE32H(x, y) \
36 do { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
37 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } while(0)
38
39#define LOAD32H(x, y) \
40 do { x = ((ulong32)((y)[0] & 255)<<24) | \
41 ((ulong32)((y)[1] & 255)<<16) | \
42 ((ulong32)((y)[2] & 255)<<8) | \
43 ((ulong32)((y)[3] & 255)); } while(0)
44
45#define STORE64H(x, y) \
46do { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
47 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
48 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
49 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } while(0)
50
51#define LOAD64H(x, y) \
52do { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
53 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
54 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
55 (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); } while(0)
56
57
58#elif defined(ENDIAN_LITTLE)
59
60#ifdef LTC_HAVE_BSWAP_BUILTIN
61
62#define STORE32H(x, y) \
63do { ulong32 __t = __builtin_bswap32 ((x)); \
64 XMEMCPY ((y), &__t, 4); } while(0)
65
66#define LOAD32H(x, y) \
67do { XMEMCPY (&(x), (y), 4); \
68 (x) = __builtin_bswap32 ((x)); } while(0)
69
70#elif !defined(LTC_NO_BSWAP) && (defined(INTEL_CC) || (defined(__GNUC__) && (defined(__DJGPP__) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__i386__) || defined(__x86_64__))))
71
72#define STORE32H(x, y) \
73asm __volatile__ ( \
74 "bswapl %0 \n\t" \
75 "movl %0,(%1)\n\t" \
76 "bswapl %0 \n\t" \
77 ::"r"(x), "r"(y));
78
79#define LOAD32H(x, y) \
80asm __volatile__ ( \
81 "movl (%1),%0\n\t" \
82 "bswapl %0\n\t" \
83 :"=r"(x): "r"(y));
84
85#else
86
87#define STORE32H(x, y) \
88 do { (y)[0] = (unsigned char)(((x)>>24)&255); (y)[1] = (unsigned char)(((x)>>16)&255); \
89 (y)[2] = (unsigned char)(((x)>>8)&255); (y)[3] = (unsigned char)((x)&255); } while(0)
90
91#define LOAD32H(x, y) \
92 do { x = ((ulong32)((y)[0] & 255)<<24) | \
93 ((ulong32)((y)[1] & 255)<<16) | \
94 ((ulong32)((y)[2] & 255)<<8) | \
95 ((ulong32)((y)[3] & 255)); } while(0)
96
97#endif
98
99#ifdef LTC_HAVE_BSWAP_BUILTIN
100
101#define STORE64H(x, y) \
102do { ulong64 __t = __builtin_bswap64 ((x)); \
103 XMEMCPY ((y), &__t, 8); } while(0)
104
105#define LOAD64H(x, y) \
106do { XMEMCPY (&(x), (y), 8); \
107 (x) = __builtin_bswap64 ((x)); } while(0)
108
109/* x86_64 processor */
110#elif !defined(LTC_NO_BSWAP) && (defined(__GNUC__) && defined(__x86_64__))
111
112#define STORE64H(x, y) \
113asm __volatile__ ( \
114 "bswapq %0 \n\t" \
115 "movq %0,(%1)\n\t" \
116 "bswapq %0 \n\t" \
117 ::"r"(x), "r"(y): "memory");
118
119#define LOAD64H(x, y) \
120asm __volatile__ ( \
121 "movq (%1),%0\n\t" \
122 "bswapq %0\n\t" \
123 :"=r"(x): "r"(y): "memory");
124
125#else
126
127#define STORE64H(x, y) \
128do { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
129 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
130 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
131 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } while(0)
132
133#define LOAD64H(x, y) \
134do { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48) | \
135 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32) | \
136 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16) | \
137 (((ulong64)((y)[6] & 255))<<8)|(((ulong64)((y)[7] & 255))); } while(0)
138
139#endif
140
141#ifdef ENDIAN_32BITWORD
142
143#define STORE32L(x, y) \
144 do { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } while(0)
145
146#define LOAD32L(x, y) \
147 do { XMEMCPY(&(x), y, 4); } while(0)
148
149#define STORE64L(x, y) \
150 do { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
151 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
152 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
153 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
154
155#define LOAD64L(x, y) \
156 do { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48)| \
157 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32)| \
158 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16)| \
159 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } while(0)
160
161#else /* 64-bit words then */
162
163#define STORE32L(x, y) \
164 do { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } while(0)
165
166#define LOAD32L(x, y) \
167 do { XMEMCPY(&(x), y, 4); x &= 0xFFFFFFFF; } while(0)
168
169#define STORE64L(x, y) \
170 do { ulong64 __t = (x); XMEMCPY(y, &__t, 8); } while(0)
171
172#define LOAD64L(x, y) \
173 do { XMEMCPY(&(x), y, 8); } while(0)
174
175#endif /* ENDIAN_64BITWORD */
176
177#elif defined(ENDIAN_BIG)
178
179#define STORE32L(x, y) \
180 do { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
181 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
182
183#define LOAD32L(x, y) \
184 do { x = ((ulong32)((y)[3] & 255)<<24) | \
185 ((ulong32)((y)[2] & 255)<<16) | \
186 ((ulong32)((y)[1] & 255)<<8) | \
187 ((ulong32)((y)[0] & 255)); } while(0)
188
189#define STORE64L(x, y) \
190do { (y)[7] = (unsigned char)(((x)>>56)&255); (y)[6] = (unsigned char)(((x)>>48)&255); \
191 (y)[5] = (unsigned char)(((x)>>40)&255); (y)[4] = (unsigned char)(((x)>>32)&255); \
192 (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255); \
193 (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); } while(0)
194
195#define LOAD64L(x, y) \
196do { x = (((ulong64)((y)[7] & 255))<<56)|(((ulong64)((y)[6] & 255))<<48) | \
197 (((ulong64)((y)[5] & 255))<<40)|(((ulong64)((y)[4] & 255))<<32) | \
198 (((ulong64)((y)[3] & 255))<<24)|(((ulong64)((y)[2] & 255))<<16) | \
199 (((ulong64)((y)[1] & 255))<<8)|(((ulong64)((y)[0] & 255))); } while(0)
200
201#ifdef ENDIAN_32BITWORD
202
203#define STORE32H(x, y) \
204 do { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } while(0)
205
206#define LOAD32H(x, y) \
207 do { XMEMCPY(&(x), y, 4); } while(0)
208
209#define STORE64H(x, y) \
210 do { (y)[0] = (unsigned char)(((x)>>56)&255); (y)[1] = (unsigned char)(((x)>>48)&255); \
211 (y)[2] = (unsigned char)(((x)>>40)&255); (y)[3] = (unsigned char)(((x)>>32)&255); \
212 (y)[4] = (unsigned char)(((x)>>24)&255); (y)[5] = (unsigned char)(((x)>>16)&255); \
213 (y)[6] = (unsigned char)(((x)>>8)&255); (y)[7] = (unsigned char)((x)&255); } while(0)
214
215#define LOAD64H(x, y) \
216 do { x = (((ulong64)((y)[0] & 255))<<56)|(((ulong64)((y)[1] & 255))<<48)| \
217 (((ulong64)((y)[2] & 255))<<40)|(((ulong64)((y)[3] & 255))<<32)| \
218 (((ulong64)((y)[4] & 255))<<24)|(((ulong64)((y)[5] & 255))<<16)| \
219 (((ulong64)((y)[6] & 255))<<8)| (((ulong64)((y)[7] & 255))); } while(0)
220
221#else /* 64-bit words then */
222
223#define STORE32H(x, y) \
224 do { ulong32 __t = (x); XMEMCPY(y, &__t, 4); } while(0)
225
226#define LOAD32H(x, y) \
227 do { XMEMCPY(&(x), y, 4); x &= 0xFFFFFFFF; } while(0)
228
229#define STORE64H(x, y) \
230 do { ulong64 __t = (x); XMEMCPY(y, &__t, 8); } while(0)
231
232#define LOAD64H(x, y) \
233 do { XMEMCPY(&(x), y, 8); } while(0)
234
235#endif /* ENDIAN_64BITWORD */
236#endif /* ENDIAN_BIG */
237
238#define BSWAP(x) ( ((x>>24)&0x000000FFUL) | ((x<<24)&0xFF000000UL) | \
239 ((x>>8)&0x0000FF00UL) | ((x<<8)&0x00FF0000UL) )
240
241
242/* 32-bit Rotates */
243#if defined(_MSC_VER)
244#define LTC_ROx_ASM
245
246/* instrinsic rotate */
247#include <stdlib.h>
248#pragma intrinsic(_lrotr,_lrotl)
249#define ROR(x,n) _lrotr(x,n)
250#define ROL(x,n) _lrotl(x,n)
251#define RORc(x,n) _lrotr(x,n)
252#define ROLc(x,n) _lrotl(x,n)
253
254#elif !defined(__STRICT_ANSI__) && defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)) && !defined(INTEL_CC) && !defined(LTC_NO_ASM)
255#define LTC_ROx_ASM
256
257static inline ulong32 ROL(ulong32 word, int i)
258{
259 asm ("roll %%cl,%0"
260 :"=r" (word)
261 :"0" (word),"c" (i));
262 return word;
263}
264
265static inline ulong32 ROR(ulong32 word, int i)
266{
267 asm ("rorl %%cl,%0"
268 :"=r" (word)
269 :"0" (word),"c" (i));
270 return word;
271}
272
273#ifndef LTC_NO_ROLC
274
275#define ROLc(word,i) ({ \
276 ulong32 __ROLc_tmp = (word); \
277 __asm__ ("roll %2, %0" : \
278 "=r" (__ROLc_tmp) : \
279 "0" (__ROLc_tmp), \
280 "I" (i)); \
281 __ROLc_tmp; \
282 })
283#define RORc(word,i) ({ \
284 ulong32 __RORc_tmp = (word); \
285 __asm__ ("rorl %2, %0" : \
286 "=r" (__RORc_tmp) : \
287 "0" (__RORc_tmp), \
288 "I" (i)); \
289 __RORc_tmp; \
290 })
291
292#else
293
294#define ROLc ROL
295#define RORc ROR
296
297#endif
298
299#elif !defined(__STRICT_ANSI__) && defined(LTC_PPC32)
300#define LTC_ROx_ASM
301
302static inline ulong32 ROL(ulong32 word, int i)
303{
304 asm ("rotlw %0,%0,%2"
305 :"=r" (word)
306 :"0" (word),"r" (i));
307 return word;
308}
309
310static inline ulong32 ROR(ulong32 word, int i)
311{
312 asm ("rotlw %0,%0,%2"
313 :"=r" (word)
314 :"0" (word),"r" (32-i));
315 return word;
316}
317
318#ifndef LTC_NO_ROLC
319
320static inline ulong32 ROLc(ulong32 word, const int i)
321{
322 asm ("rotlwi %0,%0,%2"
323 :"=r" (word)
324 :"0" (word),"I" (i));
325 return word;
326}
327
328static inline ulong32 RORc(ulong32 word, const int i)
329{
330 asm ("rotrwi %0,%0,%2"
331 :"=r" (word)
332 :"0" (word),"I" (i));
333 return word;
334}
335
336#else
337
338#define ROLc ROL
339#define RORc ROR
340
341#endif
342
343
344#else
345
346/* rotates the hard way */
347#define ROL(x, y) ( (((ulong32)(x)<<(ulong32)((y)&31)) | (((ulong32)(x)&0xFFFFFFFFUL)>>(ulong32)((32-((y)&31))&31))) & 0xFFFFFFFFUL)
348#define ROR(x, y) ( ((((ulong32)(x)&0xFFFFFFFFUL)>>(ulong32)((y)&31)) | ((ulong32)(x)<<(ulong32)((32-((y)&31))&31))) & 0xFFFFFFFFUL)
349#define ROLc(x, y) ( (((ulong32)(x)<<(ulong32)((y)&31)) | (((ulong32)(x)&0xFFFFFFFFUL)>>(ulong32)((32-((y)&31))&31))) & 0xFFFFFFFFUL)
350#define RORc(x, y) ( ((((ulong32)(x)&0xFFFFFFFFUL)>>(ulong32)((y)&31)) | ((ulong32)(x)<<(ulong32)((32-((y)&31))&31))) & 0xFFFFFFFFUL)
351
352#endif
353
354
355/* 64-bit Rotates */
356#if !defined(__STRICT_ANSI__) && defined(__GNUC__) && defined(__x86_64__) && !defined(_WIN64) && !defined(LTC_NO_ASM)
357
358static inline ulong64 ROL64(ulong64 word, int i)
359{
360 asm("rolq %%cl,%0"
361 :"=r" (word)
362 :"0" (word),"c" (i));
363 return word;
364}
365
366static inline ulong64 ROR64(ulong64 word, int i)
367{
368 asm("rorq %%cl,%0"
369 :"=r" (word)
370 :"0" (word),"c" (i));
371 return word;
372}
373
374#ifndef LTC_NO_ROLC
375
376#define ROL64c(word,i) ({ \
377 ulong64 __ROL64c_tmp = word; \
378 __asm__ ("rolq %2, %0" : \
379 "=r" (__ROL64c_tmp) : \
380 "0" (__ROL64c_tmp), \
381 "J" (i)); \
382 __ROL64c_tmp; \
383 })
384#define ROR64c(word,i) ({ \
385 ulong64 __ROR64c_tmp = word; \
386 __asm__ ("rorq %2, %0" : \
387 "=r" (__ROR64c_tmp) : \
388 "0" (__ROR64c_tmp), \
389 "J" (i)); \
390 __ROR64c_tmp; \
391 })
392
393#else /* LTC_NO_ROLC */
394
395#define ROL64c ROL64
396#define ROR64c ROR64
397
398#endif
399
400#else /* Not x86_64 */
401
402#define ROL64(x, y) \
403 ( (((x)<<((ulong64)(y)&63)) | \
404 (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>(((ulong64)64-((y)&63))&63))) & CONST64(0xFFFFFFFFFFFFFFFF))
405
406#define ROR64(x, y) \
407 ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \
408 ((x)<<(((ulong64)64-((y)&63))&63))) & CONST64(0xFFFFFFFFFFFFFFFF))
409
410#define ROL64c(x, y) \
411 ( (((x)<<((ulong64)(y)&63)) | \
412 (((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>(((ulong64)64-((y)&63))&63))) & CONST64(0xFFFFFFFFFFFFFFFF))
413
414#define ROR64c(x, y) \
415 ( ((((x)&CONST64(0xFFFFFFFFFFFFFFFF))>>((ulong64)(y)&CONST64(63))) | \
416 ((x)<<(((ulong64)64-((y)&63))&63))) & CONST64(0xFFFFFFFFFFFFFFFF))
417
418#endif
419
420#ifndef MAX
421 #define MAX(x, y) ( ((x)>(y))?(x):(y) )
422#endif
423
424#ifndef MIN
425 #define MIN(x, y) ( ((x)<(y))?(x):(y) )
426#endif
427
428#ifndef LTC_UNUSED_PARAM
429 #define LTC_UNUSED_PARAM(x) (void)(x)
430#endif
431
432/* extract a byte portably */
433#ifdef _MSC_VER
434 #define byte(x, n) ((unsigned char)((x) >> (8 * (n))))
435#else
436 #define byte(x, n) (((x) >> (8 * (n))) & 255)
437#endif
438
439/* there is no snprintf before Visual C++ 2015 */
440#if defined(_MSC_VER) && _MSC_VER < 1900
441#define snprintf _snprintf
442#endif
443
444/* ref: HEAD -> master, tag: v1.18.2 */
445/* git commit: 7e7eb695d581782f04b24dc444cbfde86af59853 */
446/* commit time: 2018-07-01 22:49:01 +0200 */