5 * Created by Michael Brouwer on 12/16/10.
6 * Copyright 2010,2011 Apple Inc. All rights reserved.
10 #ifndef _CORECRYPTO_CC_H_
11 #define _CORECRYPTO_CC_H_
13 #include <corecrypto/cc_config.h>
18 #include <kern/assert.h>
24 /* Declare a struct element with a guarenteed alignment of _alignment_.
25 The resulting struct can be used to create arrays that are aligned by
27 #define cc_aligned_struct(_alignment_) \
29 uint8_t b[_alignment_]; \
30 } __attribute__((aligned(_alignment_)))
32 /* number of array elements used in a cc_ctx_decl */
33 #define cc_ctx_n(_type_, _size_) ((_size_ + sizeof(_type_) - 1) / sizeof(_type_))
35 /* sizeof of a context declared with cc_ctx_decl */
36 #define cc_ctx_sizeof(_type_, _size_) sizeof(_type_[cc_ctx_n(_type_, _size_)])
38 #define cc_ctx_decl(_type_, _size_, _name_) \
39 _type_ _name_[cc_ctx_n(_type_, _size_)]
42 #define cc_zero(_size_,_data_) bzero((_data_), (_size_))
44 /* Alternate version if you don't have bzero. */
45 #define cc_zero(_size_,_data_) memset((_data_),0 ,(_size_))
49 #define cc_printf(x...) printf(x)
51 #define cc_printf(x...) fprintf(stderr, x)
54 #define cc_assert(x) assert(x)
56 #define cc_copy(_size_, _dst_, _src_) memcpy(_dst_, _src_, _size_)
58 CC_INLINE CC_NONNULL2 CC_NONNULL3 CC_NONNULL4
59 void cc_xor(size_t size
, void *r
, const void *s
, const void *t
) {
60 uint8_t *_r
=(uint8_t *)r
;
61 const uint8_t *_s
=(uint8_t *)s
;
62 const uint8_t *_t
=(uint8_t *)t
;
64 _r
[size
] = _s
[size
] ^ _t
[size
];
68 /* Exchange S and T of any type. NOTE: Both and S and T are evaluated
69 mutliple times and MUST NOT be expressions. */
70 #define CC_SWAP(S,T) do { \
71 __typeof__(S) _cc_swap_tmp = S; S = T; T = _cc_swap_tmp; \
74 /* Return the maximum value between S and T. */
75 #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;})
77 /* Return the minimum value between S and T. */
78 #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;})
80 #endif /* _CORECRYPTO_CC_H_ */