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