]>
Commit | Line | Data |
---|---|---|
316670eb A |
1 | /* |
2 | * cc.h | |
3 | * corecrypto | |
4 | * | |
3e170ce0 A |
5 | * Created on 12/16/2010 |
6 | * | |
7 | * Copyright (c) 2010,2011,2012,2014,2015 Apple Inc. All rights reserved. | |
316670eb A |
8 | * |
9 | */ | |
10 | ||
11 | #ifndef _CORECRYPTO_CC_H_ | |
12 | #define _CORECRYPTO_CC_H_ | |
13 | ||
14 | #include <corecrypto/cc_config.h> | |
d9a64523 | 15 | #include <corecrypto/cc_error.h> |
316670eb A |
16 | #include <string.h> |
17 | #include <stdint.h> | |
18 | ||
cb323159 A |
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 | ||
d9a64523 A |
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 | ||
3e170ce0 | 47 | /* Manage asserts here because a few functions in header public files do use asserts */ |
cb323159 | 48 | #if CORECRYPTO_DEBUG |
3e170ce0 | 49 | #define cc_assert(x) assert(x) |
cb323159 A |
50 | #else |
51 | #define cc_assert(x) | |
52 | #endif | |
53 | ||
fe8ab488 | 54 | #if CC_KERNEL |
316670eb | 55 | #include <kern/assert.h> |
3e170ce0 A |
56 | #elif CC_USE_S3 |
57 | #define assert(args) // No assert in S3 | |
316670eb A |
58 | #else |
59 | #include <assert.h> | |
60 | #endif | |
61 | ||
d9a64523 A |
62 | /* Provide a static assert that can be used to create compile-type failures. */ |
63 | #define cc_static_assert(e,m) \ | |
cb323159 | 64 | enum { cc_concat(static_assert_, __COUNTER__) = 1/(int)(!!(e)) } |
d9a64523 | 65 | |
316670eb A |
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_) \ | |
39037602 A |
70 | typedef struct { \ |
71 | uint8_t b[_alignment_]; \ | |
72 | } CC_ALIGNED(_alignment_) | |
316670eb | 73 | |
cb323159 A |
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 | ||
316670eb A |
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 | ||
5ba3f43e A |
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 | */ | |
39037602 | 95 | #if defined(_MSC_VER) |
cb323159 | 96 | #include <malloc.h> |
5ba3f43e | 97 | #define cc_ctx_decl(_type_, _size_, _name_) _type_ * _name_ = (_type_ *) _alloca(sizeof(_type_) * cc_ctx_n(_type_, _size_) ) |
fe8ab488 | 98 | #else |
5ba3f43e | 99 | #define cc_ctx_decl(_type_, _size_, _name_) _type_ _name_ [cc_ctx_n(_type_, _size_)] |
fe8ab488 | 100 | #endif |
316670eb | 101 | |
cb323159 A |
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_) | |
39037602 | 104 | |
5ba3f43e A |
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 | */ | |
d9a64523 | 111 | CC_NONNULL((2)) |
3e170ce0 | 112 | void cc_clear(size_t len, void *dst); |
316670eb | 113 | |
fe8ab488 | 114 | #define cc_copy(_size_, _dst_, _src_) memcpy(_dst_, _src_, _size_) |
316670eb | 115 | |
d9a64523 | 116 | CC_INLINE CC_NONNULL((2, 3, 4)) |
316670eb A |
117 | void cc_xor(size_t size, void *r, const void *s, const void *t) { |
118 | uint8_t *_r=(uint8_t *)r; | |
3e170ce0 A |
119 | const uint8_t *_s=(const uint8_t *)s; |
120 | const uint8_t *_t=(const uint8_t *)t; | |
316670eb A |
121 | while (size--) { |
122 | _r[size] = _s[size] ^ _t[size]; | |
123 | } | |
124 | } | |
125 | ||
39037602 A |
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 | */ | |
d9a64523 | 134 | CC_NONNULL((2, 3)) |
3e170ce0 A |
135 | int cc_cmp_safe (size_t num, const void * ptr1, const void * ptr2); |
136 | ||
316670eb A |
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 { \ | |
cb323159 A |
140 | volatile __typeof__(S) _cc_swap_tmp = S; S = T; T = _cc_swap_tmp; \ |
141 | _cc_swap_tmp = 0;\ | |
316670eb A |
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 | ||
cb323159 A |
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 | ||
316670eb A |
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_ */ |