1/* 2 * cc.h 3 * corecrypto 4 * 5 * Created by Michael Brouwer on 12/16/10. 6 * Copyright 2010,2011 Apple Inc. All rights reserved. 7 * 8 */ 9 10#ifndef _CORECRYPTO_CC_H_ 11#define _CORECRYPTO_CC_H_ 12 13#include <corecrypto/cc_config.h> 14#include <string.h> 15#include <stdint.h> 16 17#if KERNEL 18#include <kern/assert.h> 19#else 20#include <assert.h> 21#endif 22 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 25 a certain amount. */ 26#define cc_aligned_struct(_alignment_) \ 27 typedef struct { \ 28 uint8_t b[_alignment_]; \ 29 } __attribute__((aligned(_alignment_))) 30 31/* number of array elements used in a cc_ctx_decl */ 32#define cc_ctx_n(_type_, _size_) ((_size_ + sizeof(_type_) - 1) / sizeof(_type_)) 33 34/* sizeof of a context declared with cc_ctx_decl */ 35#define cc_ctx_sizeof(_type_, _size_) sizeof(_type_[cc_ctx_n(_type_, _size_)]) 36 37#define cc_ctx_decl(_type_, _size_, _name_) \ 38 _type_ _name_[cc_ctx_n(_type_, _size_)] 39 40#define cc_zero(_size_,_data_) bzero((_data_), (_size_)) 41 42#define cc_copy(_size_, _dst_, _src_) memcpy(_dst_, _src_, _size_) 43 44#define cc_ctx_clear(_type_, _size_, _name_) \ 45 cc_zero((_size_ + sizeof(_type_) - 1) / sizeof(_type_), _name_) 46 47CC_INLINE CC_NONNULL2 CC_NONNULL3 CC_NONNULL4 48void 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; 52 while (size--) { 53 _r[size] = _s[size] ^ _t[size]; 54 } 55} 56 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; \ 61} while(0) 62 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;}) 65 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;}) 68 69#endif /* _CORECRYPTO_CC_H_ */ 70