]> git.saurik.com Git - apple/xnu.git/blob - EXTERNAL_HEADERS/corecrypto/cchmac.h
81c1ab835a5cff83249e9ba872c2d5ef2830db9f
[apple/xnu.git] / EXTERNAL_HEADERS / corecrypto / cchmac.h
1 /*
2 * cchmac.h
3 * corecrypto
4 *
5 * Created on 12/07/2010
6 *
7 * Copyright (c) 2010,2011,2012,2014,2015 Apple Inc. All rights reserved.
8 *
9 */
10
11 #ifndef _CORECRYPTO_CCHMAC_H_
12 #define _CORECRYPTO_CCHMAC_H_
13
14 #include <corecrypto/cc.h>
15 #include <corecrypto/ccdigest.h>
16
17 /* An hmac_ctx_t is normally allocated as an array of these. */
18 struct cchmac_ctx {
19 uint8_t b[8];
20 } CC_ALIGNED(8);
21
22 #if CORECRYPTO_USE_TRANSPARENT_UNION
23 typedef union {
24 struct cchmac_ctx *hdr;
25 ccdigest_ctx_t digest;
26 } cchmac_ctx_t __attribute__((transparent_union));
27 #else
28 typedef struct cchmac_ctx* cchmac_ctx_t;
29 #endif
30
31 #define cchmac_ctx_size(STATE_SIZE, BLOCK_SIZE) (ccdigest_ctx_size(STATE_SIZE, BLOCK_SIZE) + (STATE_SIZE))
32 #define cchmac_di_size(_di_) (cchmac_ctx_size((_di_)->state_size, (_di_)->block_size))
33
34 #define cchmac_ctx_n(STATE_SIZE, BLOCK_SIZE) ccn_nof_size(cchmac_ctx_size((STATE_SIZE), (BLOCK_SIZE)))
35
36 #define cchmac_ctx_decl(STATE_SIZE, BLOCK_SIZE, _name_) cc_ctx_decl(struct cchmac_ctx, cchmac_ctx_size(STATE_SIZE, BLOCK_SIZE), _name_)
37 #define cchmac_ctx_clear(STATE_SIZE, BLOCK_SIZE, _name_) cc_clear(cchmac_ctx_size(STATE_SIZE, BLOCK_SIZE), _name_)
38 #define cchmac_di_decl(_di_, _name_) cchmac_ctx_decl((_di_)->state_size, (_di_)->block_size, _name_)
39 #define cchmac_di_clear(_di_, _name_) cchmac_ctx_clear((_di_)->state_size, (_di_)->block_size, _name_)
40
41 /* Return a ccdigest_ctx_t which can be accesed with the macros in ccdigest.h */
42 #if CORECRYPTO_USE_TRANSPARENT_UNION
43 #define cchmac_digest_ctx(_di_, HC) (((cchmac_ctx_t)(HC)).digest)
44 #else
45 #define cchmac_digest_ctx(_di_, HC) ((ccdigest_ctx_t)(HC))
46 #endif
47
48 /* Accesors for ostate fields, this is all cchmac_ctx_t adds to the ccdigest_ctx_t. */
49 #if CORECRYPTO_USE_TRANSPARENT_UNION
50 #define cchmac_ostate(_di_, HC) ((struct ccdigest_state *)(((cchmac_ctx_t)(HC)).hdr->b + ccdigest_di_size(_di_)))
51 #else
52 #define cchmac_ostate(_di_, HC) ((struct ccdigest_state *)(((cchmac_ctx_t)(HC))->b + ccdigest_di_size(_di_)))
53 #endif
54 #define cchmac_ostate8(_di_, HC) (ccdigest_u8(cchmac_ostate(_di_, HC)))
55 #define cchmac_ostate32(_di_, HC) (ccdigest_u32(cchmac_ostate(_di_, HC)))
56 #define cchmac_ostate64(_di_, HC) (ccdigest_u64(cchmac_ostate(_di_, HC)))
57 #define cchmac_ostateccn(_di_, HC) (ccdigest_ccn(cchmac_ostate(_di_, HC)))
58
59 /* Convenience accessors for ccdigest_ctx_t fields. */
60 #if CORECRYPTO_USE_TRANSPARENT_UNION
61 #define cchmac_istate(_di_, HC) ccdigest_state(_di_, ((cchmac_ctx_t)(HC)).digest)
62 #else
63 #define cchmac_istate(_di_, HC) ccdigest_state(_di_, ((ccdigest_ctx_t)(HC)))
64 #endif
65 #define cchmac_istate8(_di_, HC) ccdigest_u8(cchmac_istate(_di_, HC))
66 #define cchmac_istate32(_di_, HC) ccdigest_u32(cchmac_istate(_di_, HC))
67 #define cchmac_istate64(_di_, HC) ccdigest_u64(cchmac_istate(_di_, HC))
68 #define cchmac_istateccn(_di_, HC) ccdigest_ccn(cchmac_istate(_di_, HC))
69
70 #if CORECRYPTO_USE_TRANSPARENT_UNION
71 #define cchmac_data(_di_, HC) ccdigest_data(_di_, ((cchmac_ctx_t)(HC)).digest)
72 #define cchmac_num(_di_, HC) ccdigest_num(_di_, ((cchmac_ctx_t)(HC)).digest)
73 #define cchmac_nbits(_di_, HC) ccdigest_nbits(_di_, ((cchmac_ctx_t)(HC)).digest)
74 #else
75 #define cchmac_data(_di_, HC) ccdigest_data(_di_, ((ccdigest_ctx_t)(HC)))
76 #define cchmac_num(_di_, HC) ccdigest_num(_di_, ((ccdigest_ctx_t)(HC)))
77 #define cchmac_nbits(_di_, HC) ccdigest_nbits(_di_, ((ccdigest_ctx_t)(HC)))
78 #endif
79
80 void cchmac_init(const struct ccdigest_info *di, cchmac_ctx_t ctx,
81 size_t key_len, const void *key);
82 void cchmac_update(const struct ccdigest_info *di, cchmac_ctx_t ctx,
83 size_t data_len, const void *data);
84 void cchmac_final(const struct ccdigest_info *di, cchmac_ctx_t ctx,
85 unsigned char *mac);
86
87 void cchmac(const struct ccdigest_info *di, size_t key_len,
88 const void *key, size_t data_len, const void *data,
89 unsigned char *mac);
90
91 /* Test functions */
92
93 struct cchmac_test_input {
94 const struct ccdigest_info *di;
95 size_t key_len;
96 const void *key;
97 size_t data_len;
98 const void *data;
99 size_t mac_len;
100 const void *expected_mac;
101 };
102
103 int cchmac_test(const struct cchmac_test_input *input);
104 int cchmac_test_chunks(const struct cchmac_test_input *input, size_t chunk_size);
105
106
107 #endif /* _CORECRYPTO_CCHMAC_H_ */