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