]> git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/ccdigest.h
xnu-3789.1.32.tar.gz
[apple/xnu.git] / EXTERNAL_HEADERS / corecrypto / ccdigest.h
1 /*
2 * ccdigest.h
3 * corecrypto
4 *
5 * Created on 11/30/2010
6 *
7 * Copyright (c) 2010,2011,2012,2014,2015 Apple Inc. All rights reserved.
8 *
9 */
10
11 #ifndef _CORECRYPTO_CCDIGEST_H_
12 #define _CORECRYPTO_CCDIGEST_H_
13
14 #include <corecrypto/cc.h>
15 #include <corecrypto/ccn.h>
16
17 /* To malloc a digest context for a given di, use malloc(ccdigest_di_size(di))
18 and assign the result to a pointer to a struct ccdigest_ctx. */
19 #if CORECRYPTO_USE_TRANSPARENT_UNION
20 struct ccdigest_ctx {
21 union {
22 uint8_t u8;
23 uint32_t u32;
24 uint64_t u64;
25 cc_unit ccn;
26 } state;
27 } CC_ALIGNED(8);
28
29 typedef union {
30 struct ccdigest_ctx *hdr;
31 } ccdigest_ctx_t __attribute__((transparent_union));
32
33 struct ccdigest_state {
34 union {
35 uint8_t u8;
36 uint32_t u32;
37 uint64_t u64;
38 cc_unit ccn;
39 } state;
40 } CC_ALIGNED(8);
41
42 typedef union {
43 struct ccdigest_state *hdr;
44 struct ccdigest_ctx *_ctx;
45 ccdigest_ctx_t _ctxt;
46 } ccdigest_state_t __attribute__((transparent_union));
47 #else //=======================================================
48 struct ccdigest_ctx {
49 union {
50 uint8_t u8;
51 uint32_t u32;
52 uint64_t u64;
53 cc_unit ccn;
54 } state;
55 } CC_ALIGNED(8);
56
57 typedef struct ccdigest_ctx *ccdigest_ctx_t ;
58
59 struct ccdigest_state {
60 union {
61 uint8_t u8;
62 uint32_t u32;
63 uint64_t u64;
64 cc_unit ccn;
65 } state;
66 } CC_ALIGNED(8);
67
68 typedef struct ccdigest_state *ccdigest_state_t;
69 #endif //=======================================================
70
71
72 struct ccdigest_info {
73 size_t output_size;
74 size_t state_size;
75 size_t block_size;
76 size_t oid_size;
77 const unsigned char *oid;
78 const void *initial_state;
79 void(*compress)(ccdigest_state_t state, size_t nblocks,
80 const void *data);
81 void(*final)(const struct ccdigest_info *di, ccdigest_ctx_t ctx,
82 unsigned char *digest);
83 };
84
85 /* Return sizeof a ccdigest_ctx for a given size_t _state_size_ and
86 size_t _block_size_. */
87 #define ccdigest_ctx_size(_state_size_, _block_size_) ((_state_size_) + sizeof(uint64_t) + (_block_size_) + sizeof(unsigned int))
88 /* Return sizeof a ccdigest_ctx for a given struct ccdigest_info *_di_. */
89 #define ccdigest_di_size(_di_) (ccdigest_ctx_size((_di_)->state_size, (_di_)->block_size))
90
91 /* Declare a ccdigest_ctx for a given size_t _state_size_ and
92 size_t _block_size_, named _name_. Can be used in structs or on the
93 stack. */
94 #define ccdigest_ctx_decl(_state_size_, _block_size_, _name_) cc_ctx_decl(struct ccdigest_ctx, ccdigest_ctx_size(_state_size_, _block_size_), _name_)
95 #define ccdigest_ctx_clear(_state_size_, _block_size_, _name_) cc_clear(ccdigest_ctx_size(_state_size_, _block_size_), _name_)
96 /* Declare a ccdigest_ctx for a given size_t _state_size_ and
97 size_t _block_size_, named _name_. Can be used on the stack. */
98 #define ccdigest_di_decl(_di_, _name_) cc_ctx_decl(struct ccdigest_ctx, ccdigest_di_size(_di_), _name_)
99 #define ccdigest_di_clear(_di_, _name_) cc_clear(ccdigest_di_size(_di_), _name_)
100
101 /* Digest context field accessors. Consider the implementation private. */
102 #if CORECRYPTO_USE_TRANSPARENT_UNION
103 #define ccdigest_state(_di_, _ctx_) ((struct ccdigest_state *)(&((ccdigest_ctx_t)(_ctx_)).hdr->state.u8 + sizeof(uint64_t)))
104 #else
105 #define ccdigest_state(_di_, _ctx_) ((struct ccdigest_state *)(&((ccdigest_ctx_t)(_ctx_))->state.u8 + sizeof(uint64_t)))
106 #endif
107
108 #define ccdigest_state_u8(_di_, _ctx_) ccdigest_u8(ccdigest_state((_di_), (_ctx_)))
109 #define ccdigest_state_u32(_di_, _ctx_) ccdigest_u32(ccdigest_state((_di_), (_ctx_)))
110 #define ccdigest_state_u64(_di_, _ctx_) ccdigest_u64(ccdigest_state((_di_), (_ctx_)))
111 #define ccdigest_state_ccn(_di_, _ctx_) ccdigest_ccn(ccdigest_state((_di_), (_ctx_)))
112
113 #if CORECRYPTO_USE_TRANSPARENT_UNION
114 #define ccdigest_nbits(_di_, _ctx_) (((uint64_t *)(&((ccdigest_ctx_t)(_ctx_)).hdr->state.u8))[0])
115 #define ccdigest_data(_di_, _ctx_) (&((ccdigest_ctx_t)(_ctx_)).hdr->state.u8 + (_di_)->state_size + sizeof(uint64_t))
116 #define ccdigest_num(_di_, _ctx_) (((unsigned int *)(&((ccdigest_ctx_t)(_ctx_)).hdr->state.u8 + (_di_)->state_size + sizeof(uint64_t) + (_di_)->block_size))[0])
117 #else
118 #define ccdigest_nbits(_di_, _ctx_) (((uint64_t *)(&((ccdigest_ctx_t)(_ctx_))->state.u8))[0])
119 #define ccdigest_data(_di_, _ctx_) (&((ccdigest_ctx_t)(_ctx_))->state.u8 + (_di_)->state_size + sizeof(uint64_t))
120 #define ccdigest_num(_di_, _ctx_) (((unsigned int *)(&((ccdigest_ctx_t)(_ctx_))->state.u8 + (_di_)->state_size + sizeof(uint64_t) + (_di_)->block_size))[0])
121 #endif
122
123 #if CORECRYPTO_USE_TRANSPARENT_UNION
124 /* Digest state field accessors. Consider the implementation private. */
125 #define ccdigest_u8(_state_) (&((ccdigest_state_t)(_state_)).hdr->state.u8)
126 #define ccdigest_u32(_state_) (&((ccdigest_state_t)(_state_)).hdr->state.u32)
127 #define ccdigest_u64(_state_) (&((ccdigest_state_t)(_state_)).hdr->state.u64)
128 #define ccdigest_ccn(_state_) (&((ccdigest_state_t)(_state_)).hdr->state.ccn)
129 #else
130 /* Digest state field accessors. Consider the implementation private. */
131 #define ccdigest_u8(_state_) (&((ccdigest_state_t)(_state_))->state.u8)
132 #define ccdigest_u32(_state_) (&((ccdigest_state_t)(_state_))->state.u32)
133 #define ccdigest_u64(_state_) (&((ccdigest_state_t)(_state_))->state.u64)
134 #define ccdigest_ccn(_state_) (&((ccdigest_state_t)(_state_))->state.ccn)
135 #endif
136
137 /* We could just use memcpy instead of this special macro, but this allows us
138 to use the optimized ccn_set() assembly routine if we have one, which for
139 32 bit arm is about 200% quicker than generic memcpy(). */
140 #if CCN_SET_ASM && CCN_UNIT_SIZE <= 4
141 #define ccdigest_copy_state(_di_, _dst_, _src_) ccn_set((_di_)->state_size / CCN_UNIT_SIZE, _dst_, _src_)
142 #else
143 #define ccdigest_copy_state(_di_, _dst_, _src_) CC_MEMCPY(_dst_, _src_, (_di_)->state_size)
144 #endif
145
146 void ccdigest_init(const struct ccdigest_info *di, ccdigest_ctx_t ctx);
147 void ccdigest_update(const struct ccdigest_info *di, ccdigest_ctx_t ctx,
148 size_t len, const void *data);
149
150 CC_INLINE
151 void ccdigest_final(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned char *digest)
152 {
153 di->final(di,ctx,digest);
154 }
155
156 void ccdigest(const struct ccdigest_info *di, size_t len,
157 const void *data, void *digest);
158
159 /* test functions */
160 int ccdigest_test(const struct ccdigest_info *di, size_t len,
161 const void *data, const void *digest);
162
163 int ccdigest_test_chunk(const struct ccdigest_info *di, size_t len,
164 const void *data, const void *digest, size_t chunk);
165
166 struct ccdigest_vector {
167 size_t len;
168 const void *message;
169 const void *digest;
170 };
171
172 int ccdigest_test_vector(const struct ccdigest_info *di, const struct ccdigest_vector *v);
173 int ccdigest_test_chunk_vector(const struct ccdigest_info *di, const struct ccdigest_vector *v, size_t chunk);
174
175
176 #define OID_DEF(_VALUE_) ((const unsigned char *)_VALUE_)
177
178 #define CC_DIGEST_OID_MD2 OID_DEF("\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x02")
179 #define CC_DIGEST_OID_MD4 OID_DEF("\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x04")
180 #define CC_DIGEST_OID_MD5 OID_DEF("\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x05")
181 #define CC_DIGEST_OID_SHA1 OID_DEF("\x06\x05\x2b\x0e\x03\x02\x1a")
182 #define CC_DIGEST_OID_SHA224 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04")
183 #define CC_DIGEST_OID_SHA256 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01")
184 #define CC_DIGEST_OID_SHA384 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02")
185 #define CC_DIGEST_OID_SHA512 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03")
186 #define CC_DIGEST_OID_RMD128 OID_DEF("\x06\x06\x28\xCF\x06\x03\x00\x32")
187 #define CC_DIGEST_OID_RMD160 OID_DEF("\x06\x05\x2B\x24\x03\x02\x01")
188 #define CC_DIGEST_OID_RMD256 OID_DEF("\x06\x05\x2B\x24\x03\x02\x03")
189 #define CC_DIGEST_OID_RMD320 OID_DEF(NULL)
190
191 #endif /* _CORECRYPTO_CCDIGEST_H_ */