]> git.saurik.com Git - apple/xnu.git/blame - EXTERNAL_HEADERS/corecrypto/cc.h
xnu-3789.21.4.tar.gz
[apple/xnu.git] / EXTERNAL_HEADERS / corecrypto / cc.h
CommitLineData
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>
15#include <string.h>
16#include <stdint.h>
17
3e170ce0
A
18/* Manage asserts here because a few functions in header public files do use asserts */
19#define cc_assert(x) assert(x)
fe8ab488 20#if CC_KERNEL
316670eb 21#include <kern/assert.h>
3e170ce0
A
22#elif CC_USE_S3
23#define assert(args) // No assert in S3
316670eb
A
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_) \
39037602
A
32typedef struct { \
33uint8_t b[_alignment_]; \
34} CC_ALIGNED(_alignment_)
316670eb
A
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
39037602
A
42//- WARNING: The _MSC_VER version of cc_ctx_decl() is not compatible with the way *_decl macros are used in CommonCrypto, AppleKeyStore and SecurityFrameworks
43// to observe the incompatibilities and errors, use below definition. Corecrypto itself, accepts both deinitions
44// #define cc_ctx_decl(_type_, _size_, _name_) _type_ _name_ ## _array[cc_ctx_n(_type_, (_size_))]; _type_ *_name_ = _name_ ## _array
45//- 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().
46#if defined(_MSC_VER)
47 #define UNIQUE_ARRAY(data_type, _var_, total_count) data_type* _var_ = (data_type*)_alloca(sizeof(data_type)*(total_count));
48 #define cc_ctx_decl(_type_, _size_, _name_) UNIQUE_ARRAY(_type_, _name_,cc_ctx_n(_type_, (_size_)))
fe8ab488 49#else
39037602 50 #define cc_ctx_decl(_type_, _size_, _name_) _type_ _name_ [cc_ctx_n(_type_, _size_)]
fe8ab488 51#endif
316670eb 52
39037602
A
53/* bzero is deprecated. memset is the way to go */
54/* FWIW, L4, HEXAGON and ARMCC even with gnu compatibility mode don't have bzero */
55#define cc_zero(_size_,_data_) memset((_data_),0 ,(_size_))
56
3e170ce0
A
57/* cc_clear:
58 Set "len" bytes of memory to zero at address "dst".
59 cc_clear has been developed so that it won't be optimized out.
60 To be used to clear key buffers or sensitive data.
61*/
62CC_NONNULL2
63void cc_clear(size_t len, void *dst);
316670eb 64
fe8ab488 65#define cc_copy(_size_, _dst_, _src_) memcpy(_dst_, _src_, _size_)
316670eb
A
66
67CC_INLINE CC_NONNULL2 CC_NONNULL3 CC_NONNULL4
68void cc_xor(size_t size, void *r, const void *s, const void *t) {
69 uint8_t *_r=(uint8_t *)r;
3e170ce0
A
70 const uint8_t *_s=(const uint8_t *)s;
71 const uint8_t *_t=(const uint8_t *)t;
316670eb
A
72 while (size--) {
73 _r[size] = _s[size] ^ _t[size];
74 }
75}
76
39037602
A
77/*!
78 @brief cc_cmp_safe(num, pt1, pt2) compares two array ptr1 and ptr2 of num bytes.
79 @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.
80 @param num number of bytes in each array
81 @param ptr1 input array
82 @param ptr2 input array
83 @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).
84 */
3e170ce0
A
85CC_NONNULL2 CC_NONNULL3
86int cc_cmp_safe (size_t num, const void * ptr1, const void * ptr2);
87
316670eb
A
88/* Exchange S and T of any type. NOTE: Both and S and T are evaluated
89 mutliple times and MUST NOT be expressions. */
90#define CC_SWAP(S,T) do { \
91 __typeof__(S) _cc_swap_tmp = S; S = T; T = _cc_swap_tmp; \
92} while(0)
93
94/* Return the maximum value between S and T. */
95#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;})
96
97/* Return the minimum value between S and T. */
98#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;})
99
100#endif /* _CORECRYPTO_CC_H_ */