]> git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/cc.h
xnu-6153.41.3.tar.gz
[apple/xnu.git] / EXTERNAL_HEADERS / corecrypto / cc.h
1 /*
2 * cc.h
3 * corecrypto
4 *
5 * Created on 12/16/2010
6 *
7 * Copyright (c) 2010,2011,2012,2014,2015 Apple Inc. All rights reserved.
8 *
9 */
10
11 #ifndef _CORECRYPTO_CC_H_
12 #define _CORECRYPTO_CC_H_
13
14 #include <corecrypto/cc_config.h>
15 #include <corecrypto/cc_error.h>
16 #include <string.h>
17 #include <stdint.h>
18
19 #if __has_feature(attribute_availability_with_replacement)
20 #if __has_feature(attribute_availability_bridgeos)
21 #ifndef __CC_BRIDGE_OS_DEPRECATED
22 #define __CC_BRIDGEOS_DEPRECATED(_dep, _msg) __attribute__((availability(bridgeos,deprecated=_dep, replacement=_msg)))
23 #endif
24 #endif
25
26 #ifndef __CC_BRIDGEOS_DEPRECATED
27 #define __CC_BRIDGEOS_DEPRECATED(_dep, _msg)
28 #endif
29
30 #define cc_deprecate_with_replacement(replacement_message, ios_version, macos_version, tvos_version, watchos_version, bridgeos_version) \
31 __attribute__((availability(macos,deprecated=macos_version, replacement=replacement_message)))\
32 __attribute__((availability(ios,deprecated=ios_version, replacement=replacement_message)))\
33 __attribute__((availability(watchos,deprecated=watchos_version, replacement=replacement_message)))\
34 __attribute__((availability(tvos,deprecated=tvos_version, replacement=replacement_message)))\
35 __CC_BRIDGEOS_DEPRECATED(bridgeos_version, replacement_message)
36
37 #else /* !__has_feature(attribute_availability_with_replacement) */
38
39 #define cc_deprecate_with_replacement(replacement_message, ios_version, macos_version, tvos_version, watchos_version, bridgeos_version)
40
41 #endif /* __has_feature(attribute_availability_with_replacement) */
42
43 /* Provide a general purpose macro concat method. */
44 #define cc_concat_(a, b) a##b
45 #define cc_concat(a, b) cc_concat_(a, b)
46
47 /* Manage asserts here because a few functions in header public files do use asserts */
48 #if CORECRYPTO_DEBUG
49 #define cc_assert(x) assert(x)
50 #else
51 #define cc_assert(x)
52 #endif
53
54 #if CC_KERNEL
55 #include <kern/assert.h>
56 #elif CC_USE_S3
57 #define assert(args) // No assert in S3
58 #else
59 #include <assert.h>
60 #endif
61
62 /* Provide a static assert that can be used to create compile-type failures. */
63 #define cc_static_assert(e,m) \
64 enum { cc_concat(static_assert_, __COUNTER__) = 1/(int)(!!(e)) }
65
66 /* Declare a struct element with a guarenteed alignment of _alignment_.
67 The resulting struct can be used to create arrays that are aligned by
68 a certain amount. */
69 #define cc_aligned_struct(_alignment_) \
70 typedef struct { \
71 uint8_t b[_alignment_]; \
72 } CC_ALIGNED(_alignment_)
73
74 #if defined(__BIGGEST_ALIGNMENT__)
75 #define CC_MAX_ALIGNMENT __BIGGEST_ALIGNMENT__
76 #else
77 #define CC_MAX_ALIGNMENT 16
78 #endif
79
80 /* pads a given size to be a multiple of the biggest alignment for any type */
81 #define cc_pad_align(_size_) ((_size_ + CC_MAX_ALIGNMENT - 1) & (~(CC_MAX_ALIGNMENT - 1)))
82
83 /* number of array elements used in a cc_ctx_decl */
84 #define cc_ctx_n(_type_, _size_) ((_size_ + sizeof(_type_) - 1) / sizeof(_type_))
85
86 /* sizeof of a context declared with cc_ctx_decl */
87 #define cc_ctx_sizeof(_type_, _size_) sizeof(_type_[cc_ctx_n(_type_, _size_)])
88
89 /*
90 1. _alloca cannot be removed becasue this header file is compiled with both MSVC++ and with clang.
91 2. The _MSC_VER version of cc_ctx_decl() is not compatible with the way *_decl macros as used in CommonCrypto, AppleKeyStore and SecurityFrameworks. To observe the incompatibilities and errors, use below definition. Corecrypto itself, accepts both deinitions
92 #define cc_ctx_decl(_type_, _size_, _name_) _type_ _name_ ## _array[cc_ctx_n(_type_, (_size_))]; _type_ *_name_ = _name_ ## _array
93 3. Never use sizeof() operator for the variables declared with cc_ctx_decl(), because it is not be compatible with the _MSC_VER version of cc_ctx_decl().
94 */
95 #if defined(_MSC_VER)
96 #include <malloc.h>
97 #define cc_ctx_decl(_type_, _size_, _name_) _type_ * _name_ = (_type_ *) _alloca(sizeof(_type_) * cc_ctx_n(_type_, _size_) )
98 #else
99 #define cc_ctx_decl(_type_, _size_, _name_) _type_ _name_ [cc_ctx_n(_type_, _size_)]
100 #endif
101
102 // cc_zero is deprecated, please use cc_clear instead.
103 #define cc_zero(_size_,_data_) _Pragma ("corecrypto deprecation warning \"'cc_zero' macro is deprecated. Use 'cc_clear' instead.\"") cc_clear(_size_,_data_)
104
105 /*!
106 @brief cc_clear(len, dst) zeroizes array dst and it will not be optimized out.
107 @discussion It is used to clear sensitive data, particularly when the are defined in the stack
108 @param len number of bytes to be cleared in dst
109 @param dst input array
110 */
111 CC_NONNULL((2))
112 void cc_clear(size_t len, void *dst);
113
114 #define cc_copy(_size_, _dst_, _src_) memcpy(_dst_, _src_, _size_)
115
116 CC_INLINE CC_NONNULL((2, 3, 4))
117 void cc_xor(size_t size, void *r, const void *s, const void *t) {
118 uint8_t *_r=(uint8_t *)r;
119 const uint8_t *_s=(const uint8_t *)s;
120 const uint8_t *_t=(const uint8_t *)t;
121 while (size--) {
122 _r[size] = _s[size] ^ _t[size];
123 }
124 }
125
126 /*!
127 @brief cc_cmp_safe(num, pt1, pt2) compares two array ptr1 and ptr2 of num bytes.
128 @discussion The execution time/cycles is independent of the data and therefore guarantees no leak about the data. However, the execution time depends on num.
129 @param num number of bytes in each array
130 @param ptr1 input array
131 @param ptr2 input array
132 @return returns 0 if the num bytes starting at ptr1 are identical to the num bytes starting at ptr2 and 1 if they are different or if num is 0 (empty arrays).
133 */
134 CC_NONNULL((2, 3))
135 int cc_cmp_safe (size_t num, const void * ptr1, const void * ptr2);
136
137 /* Exchange S and T of any type. NOTE: Both and S and T are evaluated
138 mutliple times and MUST NOT be expressions. */
139 #define CC_SWAP(S,T) do { \
140 volatile __typeof__(S) _cc_swap_tmp = S; S = T; T = _cc_swap_tmp; \
141 _cc_swap_tmp = 0;\
142 } while(0)
143
144 /* Return the maximum value between S and T. */
145 #define CC_MAX(S, T) ({__typeof__(S) _cc_max_s = S; __typeof__(T) _cc_max_t = T; _cc_max_s > _cc_max_t ? _cc_max_s : _cc_max_t;})
146
147 /* Clone of CC_MAX() that evalutes S and T multiple times to allow nesting. */
148 #define CC_MAX_EVAL(S, T) ((S) > (T) ? (S) : (T))
149
150 /* Return the minimum value between S and T. */
151 #define CC_MIN(S, T) ({__typeof__(S) _cc_min_s = S; __typeof__(T) _cc_min_t = T; _cc_min_s <= _cc_min_t ? _cc_min_s : _cc_min_t;})
152
153 #endif /* _CORECRYPTO_CC_H_ */