]>
Commit | Line | Data |
---|---|---|
f427ee49 | 1 | /* Copyright (c) (2010,2011,2012,2014,2015,2016,2017,2018,2019) Apple Inc. All rights reserved. |
316670eb | 2 | * |
f427ee49 A |
3 | * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which |
4 | * is contained in the License.txt file distributed with corecrypto) and only to | |
5 | * people who accept that license. IMPORTANT: Any license rights granted to you by | |
6 | * Apple Inc. (if any) are limited to internal use within your organization only on | |
7 | * devices and computers you own or control, for the sole purpose of verifying the | |
8 | * security characteristics and correct functioning of the Apple Software. You may | |
9 | * not, directly or indirectly, redistribute the Apple Software or any portions thereof. | |
316670eb A |
10 | */ |
11 | ||
12 | #ifndef _CORECRYPTO_CCDIGEST_H_ | |
13 | #define _CORECRYPTO_CCDIGEST_H_ | |
14 | ||
15 | #include <corecrypto/cc.h> | |
16 | #include <corecrypto/ccn.h> | |
39037602 | 17 | |
316670eb A |
18 | /* To malloc a digest context for a given di, use malloc(ccdigest_di_size(di)) |
19 | and assign the result to a pointer to a struct ccdigest_ctx. */ | |
39037602 A |
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 struct ccdigest_ctx *ccdigest_ctx_t ; | |
30 | ||
31 | struct ccdigest_state { | |
32 | union { | |
33 | uint8_t u8; | |
34 | uint32_t u32; | |
35 | uint64_t u64; | |
36 | cc_unit ccn; | |
37 | } state; | |
38 | } CC_ALIGNED(8); | |
39 | ||
40 | typedef struct ccdigest_state *ccdigest_state_t; | |
316670eb A |
41 | |
42 | struct ccdigest_info { | |
39037602 A |
43 | size_t output_size; |
44 | size_t state_size; | |
45 | size_t block_size; | |
46 | size_t oid_size; | |
47 | const unsigned char *oid; | |
316670eb | 48 | const void *initial_state; |
2a1bd2d3 | 49 | void(* CC_SPTR(ccdigest_info, compress))(ccdigest_state_t state, size_t nblocks, |
316670eb | 50 | const void *data); |
2a1bd2d3 | 51 | void(* CC_SPTR(ccdigest_info, final))(const struct ccdigest_info *di, ccdigest_ctx_t ctx, |
316670eb A |
52 | unsigned char *digest); |
53 | }; | |
54 | ||
55 | /* Return sizeof a ccdigest_ctx for a given size_t _state_size_ and | |
56 | size_t _block_size_. */ | |
57 | #define ccdigest_ctx_size(_state_size_, _block_size_) ((_state_size_) + sizeof(uint64_t) + (_block_size_) + sizeof(unsigned int)) | |
58 | /* Return sizeof a ccdigest_ctx for a given struct ccdigest_info *_di_. */ | |
59 | #define ccdigest_di_size(_di_) (ccdigest_ctx_size((_di_)->state_size, (_di_)->block_size)) | |
60 | ||
61 | /* Declare a ccdigest_ctx for a given size_t _state_size_ and | |
62 | size_t _block_size_, named _name_. Can be used in structs or on the | |
63 | stack. */ | |
64 | #define ccdigest_ctx_decl(_state_size_, _block_size_, _name_) cc_ctx_decl(struct ccdigest_ctx, ccdigest_ctx_size(_state_size_, _block_size_), _name_) | |
3e170ce0 | 65 | #define ccdigest_ctx_clear(_state_size_, _block_size_, _name_) cc_clear(ccdigest_ctx_size(_state_size_, _block_size_), _name_) |
316670eb A |
66 | /* Declare a ccdigest_ctx for a given size_t _state_size_ and |
67 | size_t _block_size_, named _name_. Can be used on the stack. */ | |
68 | #define ccdigest_di_decl(_di_, _name_) cc_ctx_decl(struct ccdigest_ctx, ccdigest_di_size(_di_), _name_) | |
3e170ce0 | 69 | #define ccdigest_di_clear(_di_, _name_) cc_clear(ccdigest_di_size(_di_), _name_) |
316670eb A |
70 | |
71 | /* Digest context field accessors. Consider the implementation private. */ | |
39037602 | 72 | #define ccdigest_state(_di_, _ctx_) ((struct ccdigest_state *)(&((ccdigest_ctx_t)(_ctx_))->state.u8 + sizeof(uint64_t))) |
39037602 | 73 | |
fe8ab488 A |
74 | #define ccdigest_state_u8(_di_, _ctx_) ccdigest_u8(ccdigest_state((_di_), (_ctx_))) |
75 | #define ccdigest_state_u32(_di_, _ctx_) ccdigest_u32(ccdigest_state((_di_), (_ctx_))) | |
76 | #define ccdigest_state_u64(_di_, _ctx_) ccdigest_u64(ccdigest_state((_di_), (_ctx_))) | |
77 | #define ccdigest_state_ccn(_di_, _ctx_) ccdigest_ccn(ccdigest_state((_di_), (_ctx_))) | |
fe8ab488 | 78 | |
39037602 A |
79 | #define ccdigest_nbits(_di_, _ctx_) (((uint64_t *)(&((ccdigest_ctx_t)(_ctx_))->state.u8))[0]) |
80 | #define ccdigest_data(_di_, _ctx_) (&((ccdigest_ctx_t)(_ctx_))->state.u8 + (_di_)->state_size + sizeof(uint64_t)) | |
81 | #define ccdigest_num(_di_, _ctx_) (((unsigned int *)(&((ccdigest_ctx_t)(_ctx_))->state.u8 + (_di_)->state_size + sizeof(uint64_t) + (_di_)->block_size))[0]) | |
316670eb | 82 | |
39037602 A |
83 | /* Digest state field accessors. Consider the implementation private. */ |
84 | #define ccdigest_u8(_state_) (&((ccdigest_state_t)(_state_))->state.u8) | |
85 | #define ccdigest_u32(_state_) (&((ccdigest_state_t)(_state_))->state.u32) | |
86 | #define ccdigest_u64(_state_) (&((ccdigest_state_t)(_state_))->state.u64) | |
87 | #define ccdigest_ccn(_state_) (&((ccdigest_state_t)(_state_))->state.ccn) | |
316670eb | 88 | |
316670eb A |
89 | void ccdigest_init(const struct ccdigest_info *di, ccdigest_ctx_t ctx); |
90 | void ccdigest_update(const struct ccdigest_info *di, ccdigest_ctx_t ctx, | |
39037602 | 91 | size_t len, const void *data); |
316670eb A |
92 | |
93 | CC_INLINE | |
94 | void ccdigest_final(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned char *digest) | |
95 | { | |
96 | di->final(di,ctx,digest); | |
97 | } | |
98 | ||
39037602 | 99 | void ccdigest(const struct ccdigest_info *di, size_t len, |
316670eb A |
100 | const void *data, void *digest); |
101 | ||
39037602 | 102 | #define OID_DEF(_VALUE_) ((const unsigned char *)_VALUE_) |
fe8ab488 | 103 | |
f427ee49 A |
104 | #define CC_DIGEST_OID_MD2 OID_DEF("\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x02") |
105 | #define CC_DIGEST_OID_MD4 OID_DEF("\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x04") | |
106 | #define CC_DIGEST_OID_MD5 OID_DEF("\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x05") | |
107 | #define CC_DIGEST_OID_SHA1 OID_DEF("\x06\x05\x2b\x0e\x03\x02\x1a") | |
108 | #define CC_DIGEST_OID_SHA224 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04") | |
109 | #define CC_DIGEST_OID_SHA256 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01") | |
110 | #define CC_DIGEST_OID_SHA384 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02") | |
111 | #define CC_DIGEST_OID_SHA512 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03") | |
112 | #define CC_DIGEST_OID_SHA512_256 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x06") | |
113 | #define CC_DIGEST_OID_RMD160 OID_DEF("\x06\x05\x2B\x24\x03\x02\x01") | |
3e170ce0 | 114 | |
316670eb | 115 | #endif /* _CORECRYPTO_CCDIGEST_H_ */ |