5 * Created on 12/16/2010
7 * Copyright (c) 2010,2011,2012,2014,2015 Apple Inc. All rights reserved.
11 #ifndef _CORECRYPTO_CC_H_
12 #define _CORECRYPTO_CC_H_
14 #include <corecrypto/cc_config.h>
18 /* Manage asserts here because a few functions in header public files do use asserts */
19 #define cc_assert(x) assert(x)
21 #include <kern/assert.h>
23 #define assert(args) // No assert in S3
28 /* Declare a struct element with a guarenteed alignment of _alignment_.
29 The resulting struct can be used to create arrays that are aligned by
31 #define cc_aligned_struct(_alignment_) \
33 uint8_t b[_alignment_]; \
34 } CC_ALIGNED(_alignment_)
36 /* number of array elements used in a cc_ctx_decl */
37 #define cc_ctx_n(_type_, _size_) ((_size_ + sizeof(_type_) - 1) / sizeof(_type_))
39 /* sizeof of a context declared with cc_ctx_decl */
40 #define cc_ctx_sizeof(_type_, _size_) sizeof(_type_[cc_ctx_n(_type_, _size_)])
43 1. _alloca cannot be removed becasue this header file is compiled with both MSVC++ and with clang.
44 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
45 #define cc_ctx_decl(_type_, _size_, _name_) _type_ _name_ ## _array[cc_ctx_n(_type_, (_size_))]; _type_ *_name_ = _name_ ## _array
46 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().
49 #define cc_ctx_decl(_type_, _size_, _name_) _type_ * _name_ = (_type_ *) _alloca(sizeof(_type_) * cc_ctx_n(_type_, _size_) )
51 #define cc_ctx_decl(_type_, _size_, _name_) _type_ _name_ [cc_ctx_n(_type_, _size_)]
54 /* bzero is deprecated. memset is the way to go */
55 /* FWIW, L4, HEXAGON and ARMCC even with gnu compatibility mode don't have bzero */
56 #define cc_zero(_size_,_data_) memset((_data_),0 ,(_size_))
59 @brief cc_clear(len, dst) zeroizes array dst and it will not be optimized out.
60 @discussion It is used to clear sensitive data, particularly when the are defined in the stack
61 @param len number of bytes to be cleared in dst
62 @param dst input array
65 void cc_clear(size_t len
, void *dst
);
67 #define cc_copy(_size_, _dst_, _src_) memcpy(_dst_, _src_, _size_)
69 CC_INLINE CC_NONNULL2 CC_NONNULL3 CC_NONNULL4
70 void cc_xor(size_t size
, void *r
, const void *s
, const void *t
) {
71 uint8_t *_r
=(uint8_t *)r
;
72 const uint8_t *_s
=(const uint8_t *)s
;
73 const uint8_t *_t
=(const uint8_t *)t
;
75 _r
[size
] = _s
[size
] ^ _t
[size
];
80 @brief cc_cmp_safe(num, pt1, pt2) compares two array ptr1 and ptr2 of num bytes.
81 @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.
82 @param num number of bytes in each array
83 @param ptr1 input array
84 @param ptr2 input array
85 @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).
87 CC_NONNULL2 CC_NONNULL3
88 int cc_cmp_safe (size_t num
, const void * ptr1
, const void * ptr2
);
90 /* Exchange S and T of any type. NOTE: Both and S and T are evaluated
91 mutliple times and MUST NOT be expressions. */
92 #define CC_SWAP(S,T) do { \
93 __typeof__(S) _cc_swap_tmp = S; S = T; T = _cc_swap_tmp; \
96 /* Return the maximum value between S and T. */
97 #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;})
99 /* Return the minimum value between S and T. */
100 #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;})
102 #endif /* _CORECRYPTO_CC_H_ */