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 | struct ccdigest_ctx { |
20 | union { |
21 | uint8_t u8; |
22 | uint32_t u32; |
23 | uint64_t u64; |
24 | cc_unit ccn; |
25 | } state; |
26 | } CC_ALIGNED(8); |
27 | |
28 | typedef struct ccdigest_ctx *ccdigest_ctx_t ; |
29 | |
30 | struct ccdigest_state { |
31 | union { |
32 | uint8_t u8; |
33 | uint32_t u32; |
34 | uint64_t u64; |
35 | cc_unit ccn; |
36 | } state; |
37 | } CC_ALIGNED(8); |
38 | |
39 | typedef struct ccdigest_state *ccdigest_state_t; |
40 | |
41 | struct ccdigest_info { |
42 | size_t output_size; |
43 | size_t state_size; |
44 | size_t block_size; |
45 | size_t oid_size; |
46 | const unsigned char *oid; |
47 | const void *initial_state; |
48 | void(*compress)(ccdigest_state_t state, size_t nblocks, |
49 | const void *data); |
50 | void(*final)(const struct ccdigest_info *di, ccdigest_ctx_t ctx, |
51 | unsigned char *digest); |
52 | }; |
53 | |
54 | /* Return sizeof a ccdigest_ctx for a given size_t _state_size_ and |
55 | size_t _block_size_. */ |
56 | #define ccdigest_ctx_size(_state_size_, _block_size_) ((_state_size_) + sizeof(uint64_t) + (_block_size_) + sizeof(unsigned int)) |
57 | /* Return sizeof a ccdigest_ctx for a given struct ccdigest_info *_di_. */ |
58 | #define ccdigest_di_size(_di_) (ccdigest_ctx_size((_di_)->state_size, (_di_)->block_size)) |
59 | |
60 | /* Declare a ccdigest_ctx for a given size_t _state_size_ and |
61 | size_t _block_size_, named _name_. Can be used in structs or on the |
62 | stack. */ |
63 | #define ccdigest_ctx_decl(_state_size_, _block_size_, _name_) cc_ctx_decl(struct ccdigest_ctx, ccdigest_ctx_size(_state_size_, _block_size_), _name_) |
64 | #define ccdigest_ctx_clear(_state_size_, _block_size_, _name_) cc_clear(ccdigest_ctx_size(_state_size_, _block_size_), _name_) |
65 | /* Declare a ccdigest_ctx for a given size_t _state_size_ and |
66 | size_t _block_size_, named _name_. Can be used on the stack. */ |
67 | #define ccdigest_di_decl(_di_, _name_) cc_ctx_decl(struct ccdigest_ctx, ccdigest_di_size(_di_), _name_) |
68 | #define ccdigest_di_clear(_di_, _name_) cc_clear(ccdigest_di_size(_di_), _name_) |
69 | |
70 | /* Digest context field accessors. Consider the implementation private. */ |
71 | #define ccdigest_state(_di_, _ctx_) ((struct ccdigest_state *)(&((ccdigest_ctx_t)(_ctx_))->state.u8 + sizeof(uint64_t))) |
72 | |
73 | #define ccdigest_state_u8(_di_, _ctx_) ccdigest_u8(ccdigest_state((_di_), (_ctx_))) |
74 | #define ccdigest_state_u32(_di_, _ctx_) ccdigest_u32(ccdigest_state((_di_), (_ctx_))) |
75 | #define ccdigest_state_u64(_di_, _ctx_) ccdigest_u64(ccdigest_state((_di_), (_ctx_))) |
76 | #define ccdigest_state_ccn(_di_, _ctx_) ccdigest_ccn(ccdigest_state((_di_), (_ctx_))) |
77 | |
78 | #define ccdigest_nbits(_di_, _ctx_) (((uint64_t *)(&((ccdigest_ctx_t)(_ctx_))->state.u8))[0]) |
79 | #define ccdigest_data(_di_, _ctx_) (&((ccdigest_ctx_t)(_ctx_))->state.u8 + (_di_)->state_size + sizeof(uint64_t)) |
80 | #define ccdigest_num(_di_, _ctx_) (((unsigned int *)(&((ccdigest_ctx_t)(_ctx_))->state.u8 + (_di_)->state_size + sizeof(uint64_t) + (_di_)->block_size))[0]) |
81 | |
82 | /* Digest state field accessors. Consider the implementation private. */ |
83 | #define ccdigest_u8(_state_) (&((ccdigest_state_t)(_state_))->state.u8) |
84 | #define ccdigest_u32(_state_) (&((ccdigest_state_t)(_state_))->state.u32) |
85 | #define ccdigest_u64(_state_) (&((ccdigest_state_t)(_state_))->state.u64) |
86 | #define ccdigest_ccn(_state_) (&((ccdigest_state_t)(_state_))->state.ccn) |
87 | |
88 | /* We could just use memcpy instead of this special macro, but this allows us |
89 | to use the optimized ccn_set() assembly routine if we have one, which for |
90 | 32 bit arm is about 200% quicker than generic memcpy(). */ |
91 | #if CCN_SET_ASM && CCN_UNIT_SIZE <= 4 |
92 | #define ccdigest_copy_state(_di_, _dst_, _src_) ccn_set((_di_)->state_size / CCN_UNIT_SIZE, _dst_, _src_) |
93 | #else |
94 | #define ccdigest_copy_state(_di_, _dst_, _src_) CC_MEMCPY(_dst_, _src_, (_di_)->state_size) |
95 | #endif |
96 | |
97 | void ccdigest_init(const struct ccdigest_info *di, ccdigest_ctx_t ctx); |
98 | void ccdigest_update(const struct ccdigest_info *di, ccdigest_ctx_t ctx, |
99 | size_t len, const void *data); |
100 | |
101 | CC_INLINE |
102 | void ccdigest_final(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned char *digest) |
103 | { |
104 | di->final(di,ctx,digest); |
105 | } |
106 | |
107 | void ccdigest(const struct ccdigest_info *di, size_t len, |
108 | const void *data, void *digest); |
109 | |
110 | #define OID_DEF(_VALUE_) ((const unsigned char *)_VALUE_) |
111 | |
112 | #define CC_DIGEST_OID_MD2 OID_DEF("\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x02") |
113 | #define CC_DIGEST_OID_MD4 OID_DEF("\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x04") |
114 | #define CC_DIGEST_OID_MD5 OID_DEF("\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x05") |
115 | #define CC_DIGEST_OID_SHA1 OID_DEF("\x06\x05\x2b\x0e\x03\x02\x1a") |
116 | #define CC_DIGEST_OID_SHA224 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04") |
117 | #define CC_DIGEST_OID_SHA256 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01") |
118 | #define CC_DIGEST_OID_SHA384 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02") |
119 | #define CC_DIGEST_OID_SHA512 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03") |
120 | #define CC_DIGEST_OID_RMD128 OID_DEF("\x06\x06\x28\xCF\x06\x03\x00\x32") |
121 | #define CC_DIGEST_OID_RMD160 OID_DEF("\x06\x05\x2B\x24\x03\x02\x01") |
122 | #define CC_DIGEST_OID_RMD256 OID_DEF("\x06\x05\x2B\x24\x03\x02\x03") |
123 | #define CC_DIGEST_OID_RMD320 OID_DEF(NULL) |
124 | |
125 | #endif /* _CORECRYPTO_CCDIGEST_H_ */ |
126 | |