1 /* Copyright (c) (2010-2012,2014-2022,2024) Apple Inc. All rights reserved.
2 *
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.
10 */
11
12 #ifndef _CORECRYPTO_CCDIGEST_H_
13 #define _CORECRYPTO_CCDIGEST_H_
14
15 #include <corecrypto/cc.h>
16 #include <corecrypto/ccn.h>
17
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. */
20 struct ccdigest_ctx {
21 uint8_t state[1];
22 } CC_ALIGNED(8);
23
24 typedef struct ccdigest_ctx *ccdigest_ctx_t;
25
26 struct ccdigest_state {
27 uint8_t state[1];
28 } CC_ALIGNED(8);
29
30 typedef struct ccdigest_state *ccdigest_state_t;
31
32 struct ccdigest_info {
33 size_t output_size;
34 size_t state_size;
35 size_t block_size;
36 size_t oid_size;
37 const unsigned char *oid;
38 const void *initial_state;
39 void(* CC_SPTR(ccdigest_info, compress))(ccdigest_state_t state, size_t nblocks,
40 const void *data);
41 void(* CC_SPTR(ccdigest_info, final))(const struct ccdigest_info *di, ccdigest_ctx_t ctx,
42 unsigned char *digest);
43 cc_impl_t impl;
44 void(* CC_SPTR(ccdigest_info, compress_parallel))(ccdigest_state_t state1, size_t nblocks1,
45 const void *data1, ccdigest_state_t state2, size_t nblocks2, const void *data2);
46 };
47
48 typedef const struct ccdigest_info *(*ccdigest_info_selector_t)(void);
49
50 /* Return sizeof a ccdigest_ctx for a given size_t _state_size_ and
51 size_t _block_size_. */
52 #define ccdigest_ctx_size(_state_size_, _block_size_) ((_state_size_) + sizeof(uint64_t) + (_block_size_) + sizeof(unsigned int))
53 /* Return sizeof a ccdigest_ctx for a given struct ccdigest_info *_di_. */
54 #define ccdigest_di_size(_di_) (ccdigest_ctx_size((_di_)->state_size, (_di_)->block_size))
55
56 /* Declare a ccdigest_ctx for a given size_t _state_size_ and
57 size_t _block_size_, named _name_. Can be used in structs or on the
58 stack. */
59 #define ccdigest_ctx_decl(_state_size_, _block_size_, _name_) cc_ctx_decl(struct ccdigest_ctx, ccdigest_ctx_size(_state_size_, _block_size_), _name_)
60 #define ccdigest_ctx_clear(_state_size_, _block_size_, _name_) cc_clear(ccdigest_ctx_size(_state_size_, _block_size_), _name_)
61 /* Declare a ccdigest_ctx for a given size_t _state_size_ and
62 size_t _block_size_, named _name_. Can be used on the stack. */
63 #define ccdigest_di_decl(_di_, _name_) cc_ctx_decl_vla(struct ccdigest_ctx, ccdigest_di_size(_di_), _name_)
64 #define ccdigest_di_clear(_di_, _name_) cc_clear(ccdigest_di_size(_di_), _name_)
65
66 /* Digest context field accessors. Consider the implementation private. */
67 #define ccdigest_state_u8(_di_, _ctx_) ccdigest_u8(ccdigest_state((_di_), (_ctx_)))
68 #define ccdigest_state_u32(_di_, _ctx_) ccdigest_u32(ccdigest_state((_di_), (_ctx_)))
69 #define ccdigest_state_u64(_di_, _ctx_) ccdigest_u64(ccdigest_state((_di_), (_ctx_)))
70 #define ccdigest_state_ccn(_di_, _ctx_) ccdigest_ccn(ccdigest_state((_di_), (_ctx_)))
71
72 #define ccdigest_nbits(_di_, _ctx_) (*((uint64_t *)((ccdigest_ctx_t)(_ctx_))->state))
73 #define ccdigest_state(_di_, _ctx_) ((ccdigest_state_t)(((ccdigest_ctx_t)(_ctx_))->state + sizeof(uint64_t)))
74 #define ccdigest_data(_di_, _ctx_) (((ccdigest_ctx_t)(_ctx_))->state + (_di_)->state_size + sizeof(uint64_t))
75 #define ccdigest_num(_di_, _ctx_) (*((unsigned int *)(((ccdigest_ctx_t)(_ctx_))->state + (_di_)->state_size + sizeof(uint64_t) + (_di_)->block_size)))
76
77 /* Digest state field accessors. Consider the implementation private. */
78 #define ccdigest_u8(_state_) ((uint8_t *)((ccdigest_state_t)(_state_)))
79 #define ccdigest_u32(_state_) ((uint32_t *)((ccdigest_state_t)(_state_)))
80 #define ccdigest_u64(_state_) ((uint64_t *)((ccdigest_state_t)(_state_)))
81 #define ccdigest_ccn(_state_) ((cc_unit *)((ccdigest_state_t)(_state_)))
82
83 void ccdigest_init(const struct ccdigest_info *di, ccdigest_ctx_t ctx);
84 void ccdigest_update(const struct ccdigest_info *di, ccdigest_ctx_t ctx,
85 size_t len, const void *data);
86
87 CC_INLINE
ccdigest_final(const struct ccdigest_info * di,ccdigest_ctx_t ctx,unsigned char * digest)88 void ccdigest_final(const struct ccdigest_info *di, ccdigest_ctx_t ctx, unsigned char *digest)
89 {
90 di->final(di,ctx,digest);
91 }
92
93 void ccdigest(const struct ccdigest_info *di, size_t len,
94 const void *data, void *digest);
95
96 /*!
97 @function ccdigest_parallel
98 @abstract Hashes two inputs of the same size, in parallel where hardware support is available.
99
100 @param di digest info struct specifying the hash to use
101 @param data_nbytes the size of the inputs
102 @param data1 pointer to the first input
103 @param digest1 output pointer for the hash of data1
104 @param data2 pointer to the second input
105 @param digest2 output pointer for the hash of data2
106
107 @discussion This is intended for use in the construction of Merkle trees.
108 */
109 CC_NONNULL_ALL
110 void ccdigest_parallel(const struct ccdigest_info *di, size_t data_nbytes,
111 const void *data1, void *digest1,
112 const void *data2, void *digest2);
113
114 #define OID_DEF(_VALUE_) ((const unsigned char *)_VALUE_)
115
116 // https://csrc.nist.gov/projects/computer-security-objects-register/algorithm-registration#Hash
117 #define CC_DIGEST_OID_MD2 OID_DEF("\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x02")
118 #define CC_DIGEST_OID_MD4 OID_DEF("\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x04")
119 #define CC_DIGEST_OID_MD5 OID_DEF("\x06\x08\x2A\x86\x48\x86\xF7\x0D\x02\x05")
120 #define CC_DIGEST_OID_SHA1 OID_DEF("\x06\x05\x2b\x0e\x03\x02\x1a")
121 #define CC_DIGEST_OID_SHA224 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04")
122 #define CC_DIGEST_OID_SHA256 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01")
123 #define CC_DIGEST_OID_SHA384 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02")
124 #define CC_DIGEST_OID_SHA512 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03")
125 #define CC_DIGEST_OID_SHA512_256 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x06")
126 #define CC_DIGEST_OID_RMD160 OID_DEF("\x06\x05\x2B\x24\x03\x02\x01")
127 #define CC_DIGEST_OID_SHA3_224 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x07")
128 #define CC_DIGEST_OID_SHA3_256 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x08")
129 #define CC_DIGEST_OID_SHA3_384 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x09")
130 #define CC_DIGEST_OID_SHA3_512 OID_DEF("\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x0A")
131
132 // Provide current maximum sizes for block and state in order to prevent the
133 // need for dynamic allocation of context or many macro accessor functions.
134 #define MAX_DIGEST_BLOCK_SIZE 144 // Maximum block size is that of SHA3-224
135 #define MAX_DIGEST_STATE_SIZE 200 // SHA-3 state is 1600 bits
136 #define MAX_DIGEST_OUTPUT_SIZE 64
137
138 #endif /* _CORECRYPTO_CCDIGEST_H_ */
139