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