1*2c2f96dcSApple OSS Distributions /* Copyright (c) (2010-2012,2014-2021) Apple Inc. All rights reserved. 2*2c2f96dcSApple OSS Distributions * 3*2c2f96dcSApple OSS Distributions * corecrypto is licensed under Apple Inc.’s Internal Use License Agreement (which 4*2c2f96dcSApple OSS Distributions * is contained in the License.txt file distributed with corecrypto) and only to 5*2c2f96dcSApple OSS Distributions * people who accept that license. IMPORTANT: Any license rights granted to you by 6*2c2f96dcSApple OSS Distributions * Apple Inc. (if any) are limited to internal use within your organization only on 7*2c2f96dcSApple OSS Distributions * devices and computers you own or control, for the sole purpose of verifying the 8*2c2f96dcSApple OSS Distributions * security characteristics and correct functioning of the Apple Software. You may 9*2c2f96dcSApple OSS Distributions * not, directly or indirectly, redistribute the Apple Software or any portions thereof. 10*2c2f96dcSApple OSS Distributions */ 11*2c2f96dcSApple OSS Distributions 12*2c2f96dcSApple OSS Distributions #ifndef _CORECRYPTO_CCHMAC_H_ 13*2c2f96dcSApple OSS Distributions #define _CORECRYPTO_CCHMAC_H_ 14*2c2f96dcSApple OSS Distributions 15*2c2f96dcSApple OSS Distributions #include <corecrypto/cc.h> 16*2c2f96dcSApple OSS Distributions #include <corecrypto/ccdigest.h> 17*2c2f96dcSApple OSS Distributions 18*2c2f96dcSApple OSS Distributions /* An hmac_ctx_t is normally allocated as an array of these. */ 19*2c2f96dcSApple OSS Distributions struct cchmac_ctx { 20*2c2f96dcSApple OSS Distributions uint8_t b[1]; 21*2c2f96dcSApple OSS Distributions } CC_ALIGNED(8); 22*2c2f96dcSApple OSS Distributions 23*2c2f96dcSApple OSS Distributions typedef struct cchmac_ctx* cchmac_ctx_t; 24*2c2f96dcSApple OSS Distributions 25*2c2f96dcSApple OSS Distributions #define cchmac_ctx_size(STATE_SIZE, BLOCK_SIZE) (cc_pad_align(ccdigest_ctx_size(STATE_SIZE, BLOCK_SIZE)) + (STATE_SIZE)) 26*2c2f96dcSApple OSS Distributions #define cchmac_di_size(_di_) (cchmac_ctx_size((_di_)->state_size, (_di_)->block_size)) 27*2c2f96dcSApple OSS Distributions 28*2c2f96dcSApple OSS Distributions #define cchmac_ctx_n(STATE_SIZE, BLOCK_SIZE) ccn_nof_size(cchmac_ctx_size((STATE_SIZE), (BLOCK_SIZE))) 29*2c2f96dcSApple OSS Distributions 30*2c2f96dcSApple OSS Distributions #define cchmac_ctx_decl(STATE_SIZE, BLOCK_SIZE, _name_) cc_ctx_decl_vla(struct cchmac_ctx, cchmac_ctx_size(STATE_SIZE, BLOCK_SIZE), _name_) 31*2c2f96dcSApple OSS Distributions #define cchmac_ctx_clear(STATE_SIZE, BLOCK_SIZE, _name_) cc_clear(cchmac_ctx_size(STATE_SIZE, BLOCK_SIZE), _name_) 32*2c2f96dcSApple OSS Distributions #define cchmac_di_decl(_di_, _name_) cchmac_ctx_decl((_di_)->state_size, (_di_)->block_size, _name_) 33*2c2f96dcSApple OSS Distributions #define cchmac_di_clear(_di_, _name_) cchmac_ctx_clear((_di_)->state_size, (_di_)->block_size, _name_) 34*2c2f96dcSApple OSS Distributions 35*2c2f96dcSApple OSS Distributions /* Return a ccdigest_ctx_t which can be accesed with the macros in ccdigest.h */ 36*2c2f96dcSApple OSS Distributions #define cchmac_digest_ctx(_di_, HC) ((ccdigest_ctx_t)(HC)) 37*2c2f96dcSApple OSS Distributions 38*2c2f96dcSApple OSS Distributions /* Accesors for ostate fields, this is all cchmac_ctx_t adds to the ccdigest_ctx_t. */ 39*2c2f96dcSApple OSS Distributions #define cchmac_ostate(_di_, HC) ((ccdigest_state_t)(((cchmac_ctx_t)(HC))->b + cc_pad_align(ccdigest_di_size(_di_)))) 40*2c2f96dcSApple OSS Distributions #define cchmac_ostate8(_di_, HC) (ccdigest_u8(cchmac_ostate(_di_, HC))) 41*2c2f96dcSApple OSS Distributions #define cchmac_ostate32(_di_, HC) (ccdigest_u32(cchmac_ostate(_di_, HC))) 42*2c2f96dcSApple OSS Distributions #define cchmac_ostate64(_di_, HC) (ccdigest_u64(cchmac_ostate(_di_, HC))) 43*2c2f96dcSApple OSS Distributions #define cchmac_ostateccn(_di_, HC) (ccdigest_ccn(cchmac_ostate(_di_, HC))) 44*2c2f96dcSApple OSS Distributions 45*2c2f96dcSApple OSS Distributions /* Convenience accessors for ccdigest_ctx_t fields. */ 46*2c2f96dcSApple OSS Distributions #define cchmac_istate(_di_, HC) ccdigest_state(_di_, ((ccdigest_ctx_t)(HC))) 47*2c2f96dcSApple OSS Distributions #define cchmac_istate8(_di_, HC) ccdigest_u8(cchmac_istate(_di_, HC)) 48*2c2f96dcSApple OSS Distributions #define cchmac_istate32(_di_, HC) ccdigest_u32(cchmac_istate(_di_, HC)) 49*2c2f96dcSApple OSS Distributions #define cchmac_istate64(_di_, HC) ccdigest_u64(cchmac_istate(_di_, HC)) 50*2c2f96dcSApple OSS Distributions #define cchmac_istateccn(_di_, HC) ccdigest_ccn(cchmac_istate(_di_, HC)) 51*2c2f96dcSApple OSS Distributions 52*2c2f96dcSApple OSS Distributions #define cchmac_data(_di_, HC) ccdigest_data(_di_, ((ccdigest_ctx_t)(HC))) 53*2c2f96dcSApple OSS Distributions #define cchmac_num(_di_, HC) ccdigest_num(_di_, ((ccdigest_ctx_t)(HC))) 54*2c2f96dcSApple OSS Distributions #define cchmac_nbits(_di_, HC) ccdigest_nbits(_di_, ((ccdigest_ctx_t)(HC))) 55*2c2f96dcSApple OSS Distributions 56*2c2f96dcSApple OSS Distributions void cchmac_init(const struct ccdigest_info *di, cchmac_ctx_t ctx, 57*2c2f96dcSApple OSS Distributions size_t key_len, const void *key); 58*2c2f96dcSApple OSS Distributions void cchmac_update(const struct ccdigest_info *di, cchmac_ctx_t ctx, 59*2c2f96dcSApple OSS Distributions size_t data_len, const void *data); 60*2c2f96dcSApple OSS Distributions void cchmac_final(const struct ccdigest_info *di, cchmac_ctx_t ctx, 61*2c2f96dcSApple OSS Distributions unsigned char *mac); 62*2c2f96dcSApple OSS Distributions 63*2c2f96dcSApple OSS Distributions void cchmac(const struct ccdigest_info *di, size_t key_len, 64*2c2f96dcSApple OSS Distributions const void *key, size_t data_len, const void *data, 65*2c2f96dcSApple OSS Distributions unsigned char *mac); 66*2c2f96dcSApple OSS Distributions 67*2c2f96dcSApple OSS Distributions #endif /* _CORECRYPTO_CCHMAC_H_ */ 68