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>
23 /* Declare a struct element with a guarenteed alignment of _alignment_.
24 The resulting struct can be used to create arrays that are aligned by
26 #define cc_aligned_struct(_alignment_) \
28 uint8_t b[_alignment_]; \
29 } __attribute__((aligned(_alignment_)))
31 /* number of array elements used in a cc_ctx_decl */
32 #define cc_ctx_n(_type_, _size_) ((_size_ + sizeof(_type_) - 1) / sizeof(_type_))
34 /* sizeof of a context declared with cc_ctx_decl */
35 #define cc_ctx_sizeof(_type_, _size_) sizeof(_type_[cc_ctx_n(_type_, _size_)])
37 #define cc_ctx_decl(_type_, _size_, _name_) \
38 _type_ _name_[cc_ctx_n(_type_, _size_)]
40 #define cc_zero(_size_,_data_) bzero((_data_), (_size_))
42 #define cc_copy(_size_, _dst_, _src_) memcpy(_dst_, _src_, _size_)
44 #define cc_ctx_clear(_type_, _size_, _name_) \
45 cc_zero((_size_ + sizeof(_type_) - 1) / sizeof(_type_), _name_)
47 CC_INLINE CC_NONNULL2 CC_NONNULL3 CC_NONNULL4
48 void cc_xor(size_t size
, void *r
, const void *s
, const void *t
) {
49 uint8_t *_r
=(uint8_t *)r
;
50 const uint8_t *_s
=(uint8_t *)s
;
51 const uint8_t *_t
=(uint8_t *)t
;
53 _r
[size
] = _s
[size
] ^ _t
[size
];
57 /* Exchange S and T of any type. NOTE: Both and S and T are evaluated
58 mutliple times and MUST NOT be expressions. */
59 #define CC_SWAP(S,T) do { \
60 __typeof__(S) _cc_swap_tmp = S; S = T; T = _cc_swap_tmp; \
63 /* Return the maximum value between S and T. */
64 #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;})
66 /* Return the minimum value between S and T. */
67 #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;})
69 #endif /* _CORECRYPTO_CC_H_ */