]> git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/cc.h
xnu-3248.60.10.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 <string.h>
16 #include <stdint.h>
17
18 /* Manage asserts here because a few functions in header public files do use asserts */
19 #define cc_assert(x) assert(x)
20 #if CC_KERNEL
21 #include <kern/assert.h>
22 #elif CC_USE_S3
23 #define assert(args) // No assert in S3
24 #else
25 #include <assert.h>
26 #endif
27
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
30 a certain amount. */
31 #define cc_aligned_struct(_alignment_) \
32 typedef struct { \
33 uint8_t b[_alignment_]; \
34 } __attribute__((aligned(_alignment_)))
35
36 /* number of array elements used in a cc_ctx_decl */
37 #define cc_ctx_n(_type_, _size_) ((_size_ + sizeof(_type_) - 1) / sizeof(_type_))
38
39 /* sizeof of a context declared with cc_ctx_decl */
40 #define cc_ctx_sizeof(_type_, _size_) sizeof(_type_[cc_ctx_n(_type_, _size_)])
41
42 #define cc_ctx_decl(_type_, _size_, _name_) \
43 _type_ _name_[cc_ctx_n(_type_, _size_)]
44
45 #if CC_HAS_BZERO
46 #define cc_zero(_size_,_data_) bzero((_data_), (_size_))
47 #else
48 /* Alternate version if you don't have bzero. */
49 #define cc_zero(_size_,_data_) memset((_data_),0 ,(_size_))
50 #endif
51
52 /* cc_clear:
53 Set "len" bytes of memory to zero at address "dst".
54 cc_clear has been developed so that it won't be optimized out.
55 To be used to clear key buffers or sensitive data.
56 */
57 CC_NONNULL2
58 void cc_clear(size_t len, void *dst);
59
60 #define cc_copy(_size_, _dst_, _src_) memcpy(_dst_, _src_, _size_)
61
62 CC_INLINE CC_NONNULL2 CC_NONNULL3 CC_NONNULL4
63 void cc_xor(size_t size, void *r, const void *s, const void *t) {
64 uint8_t *_r=(uint8_t *)r;
65 const uint8_t *_s=(const uint8_t *)s;
66 const uint8_t *_t=(const uint8_t *)t;
67 while (size--) {
68 _r[size] = _s[size] ^ _t[size];
69 }
70 }
71
72 /* cc_cmp_safe:
73 Compare "num" pointed by ptr1 and ptr2, array of identical size.
74 Functional behavior: Returns 0 if the "num" bytes starting at ptr1 are identical to the "num"
75 bytes starting at ptr2.
76 Return !=0 if they are different or if "num" is 0 (empty arrays)
77 Security: The execution time/cycles is *independent* of the data and therefore guarantees
78 no leak about the data.
79 However, the execution time depends on "num".
80 */
81 CC_NONNULL2 CC_NONNULL3
82 int cc_cmp_safe (size_t num, const void * ptr1, const void * ptr2);
83
84
85 /* Exchange S and T of any type. NOTE: Both and S and T are evaluated
86 mutliple times and MUST NOT be expressions. */
87 #define CC_SWAP(S,T) do { \
88 __typeof__(S) _cc_swap_tmp = S; S = T; T = _cc_swap_tmp; \
89 } while(0)
90
91 /* Return the maximum value between S and T. */
92 #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;})
93
94 /* Return the minimum value between S and T. */
95 #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;})
96
97 #endif /* _CORECRYPTO_CC_H_ */