michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifdef FREEBL_NO_DEPEND michael@0: #include "stubs.h" michael@0: #endif michael@0: michael@0: #include "secport.h" michael@0: #include "hasht.h" michael@0: #include "blapit.h" michael@0: #include "alghmac.h" michael@0: #include "secerr.h" michael@0: michael@0: #define HMAC_PAD_SIZE HASH_BLOCK_LENGTH_MAX michael@0: michael@0: struct HMACContextStr { michael@0: void *hash; michael@0: const SECHashObject *hashobj; michael@0: PRBool wasAllocated; michael@0: unsigned char ipad[HMAC_PAD_SIZE]; michael@0: unsigned char opad[HMAC_PAD_SIZE]; michael@0: }; michael@0: michael@0: void michael@0: HMAC_Destroy(HMACContext *cx, PRBool freeit) michael@0: { michael@0: if (cx == NULL) michael@0: return; michael@0: michael@0: PORT_Assert(!freeit == !cx->wasAllocated); michael@0: if (cx->hash != NULL) { michael@0: cx->hashobj->destroy(cx->hash, PR_TRUE); michael@0: PORT_Memset(cx, 0, sizeof *cx); michael@0: } michael@0: if (freeit) michael@0: PORT_Free(cx); michael@0: } michael@0: michael@0: SECStatus michael@0: HMAC_Init( HMACContext * cx, const SECHashObject *hash_obj, michael@0: const unsigned char *secret, unsigned int secret_len, PRBool isFIPS) michael@0: { michael@0: unsigned int i; michael@0: unsigned char hashed_secret[HASH_LENGTH_MAX]; michael@0: michael@0: /* required by FIPS 198 Section 3 */ michael@0: if (isFIPS && secret_len < hash_obj->length/2) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); michael@0: return SECFailure; michael@0: } michael@0: if (cx == NULL) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); michael@0: return SECFailure; michael@0: } michael@0: cx->wasAllocated = PR_FALSE; michael@0: cx->hashobj = hash_obj; michael@0: cx->hash = cx->hashobj->create(); michael@0: if (cx->hash == NULL) michael@0: goto loser; michael@0: michael@0: if (secret_len > cx->hashobj->blocklength) { michael@0: cx->hashobj->begin( cx->hash); michael@0: cx->hashobj->update(cx->hash, secret, secret_len); michael@0: PORT_Assert(cx->hashobj->length <= sizeof hashed_secret); michael@0: cx->hashobj->end( cx->hash, hashed_secret, &secret_len, michael@0: sizeof hashed_secret); michael@0: if (secret_len != cx->hashobj->length) { michael@0: PORT_SetError(SEC_ERROR_LIBRARY_FAILURE); michael@0: goto loser; michael@0: } michael@0: secret = (const unsigned char *)&hashed_secret[0]; michael@0: } michael@0: michael@0: PORT_Memset(cx->ipad, 0x36, cx->hashobj->blocklength); michael@0: PORT_Memset(cx->opad, 0x5c, cx->hashobj->blocklength); michael@0: michael@0: /* fold secret into padding */ michael@0: for (i = 0; i < secret_len; i++) { michael@0: cx->ipad[i] ^= secret[i]; michael@0: cx->opad[i] ^= secret[i]; michael@0: } michael@0: PORT_Memset(hashed_secret, 0, sizeof hashed_secret); michael@0: return SECSuccess; michael@0: michael@0: loser: michael@0: PORT_Memset(hashed_secret, 0, sizeof hashed_secret); michael@0: if (cx->hash != NULL) michael@0: cx->hashobj->destroy(cx->hash, PR_TRUE); michael@0: return SECFailure; michael@0: } michael@0: michael@0: HMACContext * michael@0: HMAC_Create(const SECHashObject *hash_obj, const unsigned char *secret, michael@0: unsigned int secret_len, PRBool isFIPS) michael@0: { michael@0: SECStatus rv; michael@0: HMACContext * cx = PORT_ZNew(HMACContext); michael@0: if (cx == NULL) michael@0: return NULL; michael@0: rv = HMAC_Init(cx, hash_obj, secret, secret_len, isFIPS); michael@0: cx->wasAllocated = PR_TRUE; michael@0: if (rv != SECSuccess) { michael@0: PORT_Free(cx); /* contains no secret info */ michael@0: cx = NULL; michael@0: } michael@0: return cx; michael@0: } michael@0: michael@0: void michael@0: HMAC_Begin(HMACContext *cx) michael@0: { michael@0: /* start inner hash */ michael@0: cx->hashobj->begin(cx->hash); michael@0: cx->hashobj->update(cx->hash, cx->ipad, cx->hashobj->blocklength); michael@0: } michael@0: michael@0: void michael@0: HMAC_Update(HMACContext *cx, const unsigned char *data, unsigned int data_len) michael@0: { michael@0: cx->hashobj->update(cx->hash, data, data_len); michael@0: } michael@0: michael@0: SECStatus michael@0: HMAC_Finish(HMACContext *cx, unsigned char *result, unsigned int *result_len, michael@0: unsigned int max_result_len) michael@0: { michael@0: if (max_result_len < cx->hashobj->length) { michael@0: PORT_SetError(SEC_ERROR_INVALID_ARGS); michael@0: return SECFailure; michael@0: } michael@0: michael@0: cx->hashobj->end(cx->hash, result, result_len, max_result_len); michael@0: if (*result_len != cx->hashobj->length) michael@0: return SECFailure; michael@0: michael@0: cx->hashobj->begin(cx->hash); michael@0: cx->hashobj->update(cx->hash, cx->opad, cx->hashobj->blocklength); michael@0: cx->hashobj->update(cx->hash, result, *result_len); michael@0: cx->hashobj->end(cx->hash, result, result_len, max_result_len); michael@0: return SECSuccess; michael@0: } michael@0: michael@0: HMACContext * michael@0: HMAC_Clone(HMACContext *cx) michael@0: { michael@0: HMACContext *newcx; michael@0: michael@0: newcx = (HMACContext*)PORT_ZAlloc(sizeof(HMACContext)); michael@0: if (newcx == NULL) michael@0: goto loser; michael@0: michael@0: newcx->wasAllocated = PR_TRUE; michael@0: newcx->hashobj = cx->hashobj; michael@0: newcx->hash = cx->hashobj->clone(cx->hash); michael@0: if (newcx->hash == NULL) michael@0: goto loser; michael@0: PORT_Memcpy(newcx->ipad, cx->ipad, cx->hashobj->blocklength); michael@0: PORT_Memcpy(newcx->opad, cx->opad, cx->hashobj->blocklength); michael@0: return newcx; michael@0: michael@0: loser: michael@0: HMAC_Destroy(newcx, PR_TRUE); michael@0: return NULL; michael@0: }