]> git.saurik.com Git - apple/xnu.git/blame - EXTERNAL_HEADERS/corecrypto/cc_priv.h
xnu-3789.1.32.tar.gz
[apple/xnu.git] / EXTERNAL_HEADERS / corecrypto / cc_priv.h
CommitLineData
316670eb
A
1/*
2 * cc_priv.h
3 * corecrypto
4 *
3e170ce0
A
5 * Created on 12/01/2010
6 *
7 * Copyright (c) 2010,2011,2012,2014,2015 Apple Inc. All rights reserved.
316670eb
A
8 *
9 */
10
11#ifndef _CORECRYPTO_CC_PRIV_H_
12#define _CORECRYPTO_CC_PRIV_H_
13
14#include <corecrypto/cc.h>
15#include <stdint.h>
16
17/* defines the following macros :
18
19 CC_MEMCPY : optimized memcpy.
20 CC_MEMMOVE : optimized memmove.
21 CC_MEMSET : optimized memset.
316670eb
A
22
23 CC_STORE32_BE : store 32 bit value in big endian in unaligned buffer.
24 CC_STORE32_LE : store 32 bit value in little endian in unaligned buffer.
25 CC_STORE64_BE : store 64 bit value in big endian in unaligned buffer.
26 CC_STORE64_LE : store 64 bit value in little endian in unaligned buffer.
27
28 CC_LOAD32_BE : load 32 bit value in big endian from unaligned buffer.
29 CC_LOAD32_LE : load 32 bit value in little endian from unaligned buffer.
30 CC_LOAD64_BE : load 64 bit value in big endian from unaligned buffer.
31 CC_LOAD64_LE : load 64 bit value in little endian from unaligned buffer.
32
33 CC_ROR : Rotate Right 32 bits. Rotate count can be a variable.
34 CC_ROL : Rotate Left 32 bits. Rotate count can be a variable.
35 CC_RORc : Rotate Right 32 bits. Rotate count must be a constant.
36 CC_ROLc : Rotate Left 32 bits. Rotate count must be a constant.
37
38 CC_ROR64 : Rotate Right 64 bits. Rotate count can be a variable.
39 CC_ROL64 : Rotate Left 64 bits. Rotate count can be a variable.
40 CC_ROR64c : Rotate Right 64 bits. Rotate count must be a constant.
41 CC_ROL64c : Rotate Left 64 bits. Rotate count must be a constant.
42
43 CC_BSWAP : byte swap a 32 bits variable.
44
45 CC_H2BE32 : convert a 32 bits value between host and big endian order.
46 CC_H2LE32 : convert a 32 bits value between host and little endian order.
47
48The following are not defined yet... define them if needed.
49
50 CC_BSWAPc : byte swap a 32 bits constant
51
52 CC_BSWAP64 : byte swap a 64 bits variable
53 CC_BSWAP64c : byte swap a 64 bits constant
54
55 CC_READ_LE32 : read a 32 bits little endian value
56 CC_READ_LE64 : read a 64 bits little endian value
57 CC_READ_BE32 : read a 32 bits big endian value
58 CC_READ_BE64 : read a 64 bits big endian value
59
60 CC_WRITE_LE32 : write a 32 bits little endian value
61 CC_WRITE_LE64 : write a 64 bits little endian value
62 CC_WRITE_BE32 : write a 32 bits big endian value
63 CC_WRITE_BE64 : write a 64 bits big endian value
64
65 CC_H2BE64 : convert a 64 bits value between host and big endian order
66 CC_H2LE64 : convert a 64 bits value between host and little endian order
fe8ab488 67
316670eb
A
68*/
69
70/* TODO: optimized versions */
71#define CC_MEMCPY(D,S,L) memcpy((D),(S),(L))
72#define CC_MEMMOVE(D,S,L) memmove((D),(S),(L))
73#define CC_MEMSET(D,V,L) memset((D),(V),(L))
316670eb 74
fe8ab488 75// MARK: - Loads and Store
316670eb 76
fe8ab488 77// MARK: -- 32 bits - little endian
316670eb 78
fe8ab488 79// MARK: --- Default version
316670eb
A
80
81#define CC_STORE32_LE(x, y) do { \
82 ((unsigned char *)(y))[3] = (unsigned char)(((x)>>24)&255); \
83 ((unsigned char *)(y))[2] = (unsigned char)(((x)>>16)&255); \
84 ((unsigned char *)(y))[1] = (unsigned char)(((x)>>8)&255); \
85 ((unsigned char *)(y))[0] = (unsigned char)((x)&255); \
86} while(0)
87
88#define CC_LOAD32_LE(x, y) do { \
3e170ce0
A
89x = ((uint32_t)(((const unsigned char *)(y))[3] & 255)<<24) | \
90 ((uint32_t)(((const unsigned char *)(y))[2] & 255)<<16) | \
91 ((uint32_t)(((const unsigned char *)(y))[1] & 255)<<8) | \
92 ((uint32_t)(((const unsigned char *)(y))[0] & 255)); \
316670eb
A
93} while(0)
94
fe8ab488 95// MARK: -- 64 bits - little endian
316670eb
A
96
97#define CC_STORE64_LE(x, y) do { \
98 ((unsigned char *)(y))[7] = (unsigned char)(((x)>>56)&255); \
99 ((unsigned char *)(y))[6] = (unsigned char)(((x)>>48)&255); \
100 ((unsigned char *)(y))[5] = (unsigned char)(((x)>>40)&255); \
101 ((unsigned char *)(y))[4] = (unsigned char)(((x)>>32)&255); \
102 ((unsigned char *)(y))[3] = (unsigned char)(((x)>>24)&255); \
103 ((unsigned char *)(y))[2] = (unsigned char)(((x)>>16)&255); \
104 ((unsigned char *)(y))[1] = (unsigned char)(((x)>>8)&255); \
105 ((unsigned char *)(y))[0] = (unsigned char)((x)&255); \
106} while(0)
107
108#define CC_LOAD64_LE(x, y) do { \
3e170ce0
A
109x = (((uint64_t)(((const unsigned char *)(y))[7] & 255))<<56) | \
110 (((uint64_t)(((const unsigned char *)(y))[6] & 255))<<48) | \
111 (((uint64_t)(((const unsigned char *)(y))[5] & 255))<<40) | \
112 (((uint64_t)(((const unsigned char *)(y))[4] & 255))<<32) | \
113 (((uint64_t)(((const unsigned char *)(y))[3] & 255))<<24) | \
114 (((uint64_t)(((const unsigned char *)(y))[2] & 255))<<16) | \
115 (((uint64_t)(((const unsigned char *)(y))[1] & 255))<<8) | \
116 (((uint64_t)(((const unsigned char *)(y))[0] & 255))); \
316670eb
A
117} while(0)
118
fe8ab488
A
119// MARK: -- 32 bits - big endian
120// MARK: --- intel version
316670eb 121
39037602 122#if (defined(__i386__) || defined(__x86_64__)) && !defined(_MSC_VER)
316670eb
A
123
124#define CC_STORE32_BE(x, y) \
125 __asm__ __volatile__ ( \
126 "bswapl %0 \n\t" \
127 "movl %0,(%1)\n\t" \
128 "bswapl %0 \n\t" \
129 ::"r"(x), "r"(y))
130
131#define CC_LOAD32_BE(x, y) \
132 __asm__ __volatile__ ( \
133 "movl (%1),%0\n\t" \
134 "bswapl %0\n\t" \
135 :"=r"(x): "r"(y))
136
137#else
fe8ab488 138// MARK: --- default version
316670eb
A
139#define CC_STORE32_BE(x, y) do { \
140 ((unsigned char *)(y))[0] = (unsigned char)(((x)>>24)&255); \
141 ((unsigned char *)(y))[1] = (unsigned char)(((x)>>16)&255); \
142 ((unsigned char *)(y))[2] = (unsigned char)(((x)>>8)&255); \
143 ((unsigned char *)(y))[3] = (unsigned char)((x)&255); \
144} while(0)
145
146#define CC_LOAD32_BE(x, y) do { \
3e170ce0
A
147x = ((uint32_t)(((const unsigned char *)(y))[0] & 255)<<24) | \
148 ((uint32_t)(((const unsigned char *)(y))[1] & 255)<<16) | \
149 ((uint32_t)(((const unsigned char *)(y))[2] & 255)<<8) | \
150 ((uint32_t)(((const unsigned char *)(y))[3] & 255)); \
316670eb
A
151} while(0)
152
153#endif
154
fe8ab488 155// MARK: -- 64 bits - big endian
316670eb 156
fe8ab488 157// MARK: --- intel 64 bits version
316670eb 158
39037602 159#if defined(__x86_64__) && !defined (_MSC_VER)
316670eb
A
160
161#define CC_STORE64_BE(x, y) \
162__asm__ __volatile__ ( \
163"bswapq %0 \n\t" \
164"movq %0,(%1)\n\t" \
165"bswapq %0 \n\t" \
166::"r"(x), "r"(y))
167
168#define CC_LOAD64_BE(x, y) \
169__asm__ __volatile__ ( \
170"movq (%1),%0\n\t" \
171"bswapq %0\n\t" \
172:"=r"(x): "r"(y))
173
174#else
175
fe8ab488 176// MARK: --- default version
316670eb
A
177
178#define CC_STORE64_BE(x, y) do { \
179 ((unsigned char *)(y))[0] = (unsigned char)(((x)>>56)&255); \
180 ((unsigned char *)(y))[1] = (unsigned char)(((x)>>48)&255); \
181 ((unsigned char *)(y))[2] = (unsigned char)(((x)>>40)&255); \
182 ((unsigned char *)(y))[3] = (unsigned char)(((x)>>32)&255); \
183 ((unsigned char *)(y))[4] = (unsigned char)(((x)>>24)&255); \
184 ((unsigned char *)(y))[5] = (unsigned char)(((x)>>16)&255); \
185 ((unsigned char *)(y))[6] = (unsigned char)(((x)>>8)&255); \
186 ((unsigned char *)(y))[7] = (unsigned char)((x)&255); \
187} while(0)
188
189#define CC_LOAD64_BE(x, y) do { \
3e170ce0
A
190x = (((uint64_t)(((const unsigned char *)(y))[0] & 255))<<56) | \
191 (((uint64_t)(((const unsigned char *)(y))[1] & 255))<<48) | \
192 (((uint64_t)(((const unsigned char *)(y))[2] & 255))<<40) | \
193 (((uint64_t)(((const unsigned char *)(y))[3] & 255))<<32) | \
194 (((uint64_t)(((const unsigned char *)(y))[4] & 255))<<24) | \
195 (((uint64_t)(((const unsigned char *)(y))[5] & 255))<<16) | \
196 (((uint64_t)(((const unsigned char *)(y))[6] & 255))<<8) | \
197 (((uint64_t)(((const unsigned char *)(y))[7] & 255))); \
316670eb
A
198} while(0)
199
200#endif
201
fe8ab488 202// MARK: - 32-bit Rotates
316670eb
A
203
204#if defined(_MSC_VER)
fe8ab488 205// MARK: -- MSVC version
316670eb
A
206
207#include <stdlib.h>
39037602
A
208#if !defined(__clang__)
209 #pragma intrinsic(_lrotr,_lrotl)
210#endif
316670eb
A
211#define CC_ROR(x,n) _lrotr(x,n)
212#define CC_ROL(x,n) _lrotl(x,n)
213#define CC_RORc(x,n) _lrotr(x,n)
214#define CC_ROLc(x,n) _lrotl(x,n)
215
216#elif (defined(__i386__) || defined(__x86_64__))
fe8ab488 217// MARK: -- intel asm version
316670eb 218
39037602 219CC_INLINE uint32_t CC_ROL(uint32_t word, int i)
316670eb
A
220{
221 __asm__ ("roll %%cl,%0"
222 :"=r" (word)
223 :"0" (word),"c" (i));
224 return word;
225}
226
39037602 227CC_INLINE uint32_t CC_ROR(uint32_t word, int i)
316670eb
A
228{
229 __asm__ ("rorl %%cl,%0"
230 :"=r" (word)
231 :"0" (word),"c" (i));
232 return word;
233}
234
235/* Need to be a macro here, because 'i' is an immediate (constant) */
236#define CC_ROLc(word, i) \
237({ uint32_t _word=(word); \
238 __asm__ __volatile__ ("roll %2,%0" \
239 :"=r" (_word) \
240 :"0" (_word),"I" (i)); \
241 _word; \
242})
243
244
245#define CC_RORc(word, i) \
246({ uint32_t _word=(word); \
247 __asm__ __volatile__ ("rorl %2,%0" \
248 :"=r" (_word) \
249 :"0" (_word),"I" (i)); \
250 _word; \
251})
252
253#else
254
fe8ab488 255// MARK: -- default version
316670eb 256
39037602 257CC_INLINE uint32_t CC_ROL(uint32_t word, int i)
316670eb
A
258{
259 return ( (word<<(i&31)) | (word>>(32-(i&31))) );
260}
261
39037602 262CC_INLINE uint32_t CC_ROR(uint32_t word, int i)
316670eb
A
263{
264 return ( (word>>(i&31)) | (word<<(32-(i&31))) );
265}
266
267#define CC_ROLc(x, y) CC_ROL(x, y)
268#define CC_RORc(x, y) CC_ROR(x, y)
269
270#endif
271
fe8ab488 272// MARK: - 64 bits rotates
316670eb 273
39037602 274#if defined(__x86_64__) && !defined(_MSC_VER) //clang _MSVC doesn't support GNU-style inline assembly
fe8ab488 275// MARK: -- intel 64 asm version
316670eb 276
39037602 277CC_INLINE uint64_t CC_ROL64(uint64_t word, int i)
316670eb
A
278{
279 __asm__("rolq %%cl,%0"
280 :"=r" (word)
281 :"0" (word),"c" (i));
282 return word;
283}
284
39037602 285CC_INLINE uint64_t CC_ROR64(uint64_t word, int i)
316670eb
A
286{
287 __asm__("rorq %%cl,%0"
288 :"=r" (word)
289 :"0" (word),"c" (i));
290 return word;
291}
292
293/* Need to be a macro here, because 'i' is an immediate (constant) */
294#define CC_ROL64c(word, i) \
295({ \
296 uint64_t _word=(word); \
297 __asm__("rolq %2,%0" \
298 :"=r" (_word) \
299 :"0" (_word),"J" (i)); \
300 _word; \
301})
302
303#define CC_ROR64c(word, i) \
304({ \
305 uint64_t _word=(word); \
306 __asm__("rorq %2,%0" \
307 :"=r" (_word) \
308 :"0" (_word),"J" (i)); \
309 _word; \
310})
311
312
313#else /* Not x86_64 */
314
fe8ab488 315// MARK: -- default C version
316670eb 316
39037602 317CC_INLINE uint64_t CC_ROL64(uint64_t word, int i)
316670eb
A
318{
319 return ( (word<<(i&63)) | (word>>(64-(i&63))) );
320}
321
39037602 322CC_INLINE uint64_t CC_ROR64(uint64_t word, int i)
316670eb
A
323{
324 return ( (word>>(i&63)) | (word<<(64-(i&63))) );
325}
326
327#define CC_ROL64c(x, y) CC_ROL64(x, y)
328#define CC_ROR64c(x, y) CC_ROR64(x, y)
329
330#endif
331
332
fe8ab488 333// MARK: - Byte Swaps
316670eb 334
39037602 335CC_INLINE uint32_t CC_BSWAP(uint32_t x)
316670eb
A
336{
337 return (
338 ((x>>24)&0x000000FF) |
339 ((x<<24)&0xFF000000) |
340 ((x>>8) &0x0000FF00) |
341 ((x<<8) &0x00FF0000)
342 );
343}
344
fe8ab488
A
345#define CC_BSWAP64(x) \
346((uint64_t)((((uint64_t)(x) & 0xff00000000000000ULL) >> 56) | \
347(((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
348(((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
349(((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
350(((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \
351(((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
352(((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
353(((uint64_t)(x) & 0x00000000000000ffULL) << 56)))
354
316670eb
A
355#ifdef __LITTLE_ENDIAN__
356#define CC_H2BE32(x) CC_BSWAP(x)
357#define CC_H2LE32(x) (x)
358#else
316670eb
A
359#define CC_H2BE32(x) (x)
360#define CC_H2LE32(x) CC_BSWAP(x)
361#endif
362
363
364/* extract a byte portably */
365#ifdef _MSC_VER
366#define cc_byte(x, n) ((unsigned char)((x) >> (8 * (n))))
367#else
368#define cc_byte(x, n) (((x) >> (8 * (n))) & 255)
369#endif
370
fe8ab488
A
371/* HEAVISIDE_STEP (shifted by one)
372 function f(x): x->0, when x=0
373 x->1, when x>0
374 Can also be seen as a bitwise operation:
375 f(x): x -> y
376 y[0]=(OR x[i]) for all i (all bits)
377 y[i]=0 for all i>0
378 Run in constant time (log2(<bitsize of x>))
379 Useful to run constant time checks
380*/
39037602
A
381#define HEAVISIDE_STEP_UINT64(r,s) {uint64_t _t=s; \
382 _t=(((_t)>>32) | (_t)); \
3e170ce0 383 _t=(0xFFFFFFFF + (_t & 0xFFFFFFFF)); \
39037602 384 r=_t >> 32;}
3e170ce0 385
39037602
A
386#define HEAVISIDE_STEP_UINT32(r,s) {uint32_t _t=s; \
387 _t=(((_t)>>16) | (_t)); \
3e170ce0 388 _t=(0xFFFF + (_t & 0xFFFF)); \
39037602
A
389 r=_t >> 16;}
390
391#define HEAVISIDE_STEP_UINT16(r,s) {uint32_t _t=s; \
392 _t=(0xFFFF + ((_t) & 0xFFFF)); \
393 r=_t >> 16;}
394
395#define HEAVISIDE_STEP_UINT8(r,s) {uint16_t _t=s; \
396 _t=(0xFF + ((_t) & 0xFF)); \
397 r=_t >> 8;}
398
399#define CC_HEAVISIDE_STEP(r,s) { \
400 if (sizeof(s) == 1) {HEAVISIDE_STEP_UINT8(r,s);} \
401 else if (sizeof(s) == 2) {HEAVISIDE_STEP_UINT16(r,s);} \
402 else if (sizeof(s) == 4) {HEAVISIDE_STEP_UINT32(r,s);} \
403 else if (sizeof(s) == 8) {HEAVISIDE_STEP_UINT64(r,s);} \
404 else {r=(((s)==0)?0:1);} \
fe8ab488
A
405 }
406
3e170ce0
A
407/* Return 1 if x mod 4 =1,2,3, 0 otherwise */
408#define CC_CARRY_2BITS(x) (((x>>1) | x) & 0x1)
409#define CC_CARRY_3BITS(x) (((x>>2) | (x>>1) | x) & 0x1)
fe8ab488
A
410
411/* Set a variable to the biggest power of 2 which can be represented */
412#define MAX_POWER_OF_2(x) ((__typeof__(x))1<<(8*sizeof(x)-1))
3e170ce0
A
413#define cc_ceiling(a,b) (((a)+((b)-1))/(b))
414#define CC_BITLEN_TO_BYTELEN(x) cc_ceiling((x), 8)
fe8ab488 415
39037602
A
416//cc_abort() is implemented to comply with FIPS 140-2. See radar 19129408
417void cc_abort(const char * msg , ...);
418
419/*!
420 @brief cc_muxp(s, a, b) is equivalent to z = s ? a : b, but it executes in constant time
421 @param a input pointer
422 @param b input pointer
423 @param s The selection parameter s must be 0 or 1. if s is integer 1 a is returned. If s is integer 0, b is returned. Otherwise, the output is undefined.
424 @return Returns a, if s is 1 and b if s is 0
425 */
426void *cc_muxp(int s, const void *a, const void *b);
427
428/*!
429 @brief cc_mux2p
430 @param a input pointer
431 @param b input pointer
432 @param r_true output pointer: if s is integer 1 r_true=a is returned, otherwise r_true=b
433 @param r_false output pointer: if s is integer 1 r_false=b is returned, otherwise r_false=a
434 @param s The selection parameter s must be 0 or 1.
435 @discussion Executes in constant time
436 */
437void cc_mux2p(int s, void **r_true, void **r_false, const void *a, const void *b);
438
439/*!
440 @brief CC_MUXU(s, a, b) is equivalent to z = s ? a : b, but it executes in constant time
441 @param a input unsigned type
442 @param b input unsigned type
443 @param s The selection parameter s must be 0 or 1. if s is integer 1 a is returned. If s is integer 0, b is returned. Otherwise, the output is undefined.
444 @param r output
445 @return r = a, if s is 1 and b if s is 0
446 */
447#define CC_MUXU(r, s, a, b) \
448{ \
449 __typeof__(r) _cond = ((__typeof__(r))(s)-(__typeof__(r))1); \
450 r = (~_cond&(a))|(_cond&(b)); \
451}
452
453int cc_is_compiled_with_tu(void);
454
316670eb 455#endif /* _CORECRYPTO_CC_PRIV_H_ */