- SSLContext *ctx;
-
- /* this one is set once with the key, and it then cloned
- * for each init() */
- CCHmacContext ccHmacTemplate;
-
- /* the one we actually feed data to */
- CCHmacContext ccHmac;
- size_t macSize;
-
- /* FIXME not sure if we need this */
- const struct HMACReference *hmac;
-};
-
-#pragma mark -
-#pragma mark CommonCryptor HMAC routines
-
-/* Create an HMAC session */
-static OSStatus HMAC_Alloc(
- const struct HMACReference *hmac,
- SSLContext *ctx,
- const void *keyPtr,
- unsigned keyLen,
- HMACContextRef *hmacCtxOut) // RETURNED
-{
- CCHmacAlgorithm ccAlg;
-
- HMACContextRef hmacCtx = (HMACContextRef)sslMalloc(sizeof(struct HMACContext));
-
- if(hmacCtx == NULL) {
- return memFullErr;
- }
- hmacCtx->ctx = ctx;
- hmacCtx->hmac = hmac;
-
- switch(hmac->alg) {
- case HA_SHA384:
- ccAlg = kCCHmacAlgSHA384;
- hmacCtx->macSize = CC_SHA384_DIGEST_LENGTH;
- break;
- case HA_SHA256:
- ccAlg = kCCHmacAlgSHA256;
- hmacCtx->macSize = CC_SHA256_DIGEST_LENGTH;
- break;
- case HA_SHA1:
- ccAlg = kCCHmacAlgSHA1;
- hmacCtx->macSize = CC_SHA1_DIGEST_LENGTH;
- break;
- case HA_MD5:
- ccAlg = kCCHmacAlgMD5;
- hmacCtx->macSize = CC_MD5_DIGEST_LENGTH;
- break;
- default:
- ASSERT(0);
- return errSSLInternal;
- }
-
- /* create the template from which individual record MAC-ers are cloned */
- CCHmacInit(&hmacCtx->ccHmacTemplate, ccAlg, keyPtr, keyLen);
- *hmacCtxOut = hmacCtx;
- return noErr;
-}
-
-/* free a session */
-static OSStatus HMAC_Free(
- HMACContextRef hmacCtx)
-{
- if(hmacCtx != NULL) {
- memset(hmacCtx, 0, sizeof(*hmacCtx));
- sslFree(hmacCtx);
- }
- return noErr;
-}
-
-/* Reusable init - clone from template */
-static OSStatus HMAC_Init(
- HMACContextRef hmacCtx)
-{
- if(hmacCtx == NULL) {
- return errSSLInternal;
- }
- hmacCtx->ccHmac = hmacCtx->ccHmacTemplate;
- return noErr;
-}
-
-/* normal crypt ops */
-static OSStatus HMAC_Update(
- HMACContextRef hmacCtx,
- const void *data,
- unsigned dataLen)
-{
- CCHmacUpdate(&hmacCtx->ccHmac, data, dataLen);
- return noErr;
-}
-
-static OSStatus HMAC_Final(
- HMACContextRef hmacCtx,
- void *hmac, // mallocd by caller
- unsigned *hmacLen) // IN/OUT
-{
- if(*hmacLen < hmacCtx->macSize) {
- return errSSLInternal;
- }
- CCHmacFinal(&hmacCtx->ccHmac, hmac);
- *hmacLen = hmacCtx->macSize;
- return noErr;
-}
-
-/* one-shot */
-static OSStatus HMAC_Hmac (
- HMACContextRef hmacCtx,
- const void *data,
- unsigned dataLen,
- void *hmac, // mallocd by caller
- unsigned *hmacLen) // IN/OUT
-{
- OSStatus serr;
- const HMACReference *hmacRef;
-
- if(hmacCtx == NULL) {
- return errSSLInternal;
- }
- hmacRef = hmacCtx->hmac;
- assert(hmacRef != NULL);
- serr = hmacRef->init(hmacCtx);
- if(serr) {
- return serr;
- }
- serr = hmacRef->update(hmacCtx, data, dataLen);
- if(serr) {
- return serr;
- }
- return hmacRef->final(hmacCtx, hmac, hmacLen);
-}
-
-#else
-
-/* Per-session state, opaque to callers; all fields set at alloc time */
-struct HMACContext {
- SSLContext *ctx;