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