Back to index

lightning-sunbird  0.9+nobinonly
alghmac.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 #include "secport.h"
00038 #include "hasht.h"
00039 #include "blapit.h"
00040 #include "alghmac.h"
00041 #include "secerr.h"
00042 
00043 #define HMAC_PAD_SIZE HASH_BLOCK_LENGTH_MAX
00044 
00045 struct HMACContextStr {
00046     void *hash;
00047     const SECHashObject *hashobj;
00048     PRBool        wasAllocated;
00049     unsigned char ipad[HMAC_PAD_SIZE];
00050     unsigned char opad[HMAC_PAD_SIZE];
00051 };
00052 
00053 void
00054 HMAC_Destroy(HMACContext *cx, PRBool freeit)
00055 {
00056     if (cx == NULL)
00057        return;
00058 
00059     PORT_Assert(!freeit == !cx->wasAllocated);
00060     if (cx->hash != NULL) {
00061        cx->hashobj->destroy(cx->hash, PR_TRUE);
00062        PORT_Memset(cx, 0, sizeof *cx);
00063     }
00064     if (freeit)
00065        PORT_Free(cx);
00066 }
00067 
00068 SECStatus
00069 HMAC_Init( HMACContext * cx, const SECHashObject *hash_obj, 
00070           const unsigned char *secret, unsigned int secret_len, PRBool isFIPS)
00071 {
00072     unsigned int i;
00073     unsigned char hashed_secret[HASH_LENGTH_MAX];
00074 
00075     /* required by FIPS 198 Section 3 */
00076     if (isFIPS && secret_len < hash_obj->length/2) {
00077        PORT_SetError(SEC_ERROR_INVALID_ARGS);
00078        return SECFailure;
00079     }
00080     if (cx == NULL) {
00081        PORT_SetError(SEC_ERROR_INVALID_ARGS);
00082        return SECFailure;
00083     }
00084     cx->wasAllocated = PR_FALSE;
00085     cx->hashobj = hash_obj;
00086     cx->hash = cx->hashobj->create();
00087     if (cx->hash == NULL)
00088        goto loser;
00089 
00090     if (secret_len > cx->hashobj->blocklength) {
00091        cx->hashobj->begin( cx->hash);
00092        cx->hashobj->update(cx->hash, secret, secret_len);
00093        PORT_Assert(cx->hashobj->length <= sizeof hashed_secret);
00094        cx->hashobj->end(   cx->hash, hashed_secret, &secret_len, 
00095                         sizeof hashed_secret);
00096        if (secret_len != cx->hashobj->length) {
00097            PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
00098            goto loser;
00099        }
00100        secret = (const unsigned char *)&hashed_secret[0];
00101     }
00102 
00103     PORT_Memset(cx->ipad, 0x36, cx->hashobj->blocklength);
00104     PORT_Memset(cx->opad, 0x5c, cx->hashobj->blocklength);
00105 
00106     /* fold secret into padding */
00107     for (i = 0; i < secret_len; i++) {
00108        cx->ipad[i] ^= secret[i];
00109        cx->opad[i] ^= secret[i];
00110     }
00111     PORT_Memset(hashed_secret, 0, sizeof hashed_secret);
00112     return SECSuccess;
00113 
00114 loser:
00115     PORT_Memset(hashed_secret, 0, sizeof hashed_secret);
00116     if (cx->hash != NULL)
00117        cx->hashobj->destroy(cx->hash, PR_TRUE);
00118     return SECFailure;
00119 }
00120 
00121 HMACContext *
00122 HMAC_Create(const SECHashObject *hash_obj, const unsigned char *secret, 
00123             unsigned int secret_len, PRBool isFIPS)
00124 {
00125     SECStatus rv;
00126     HMACContext * cx = PORT_ZNew(HMACContext);
00127     if (cx == NULL)
00128        return NULL;
00129     rv = HMAC_Init(cx, hash_obj, secret, secret_len, isFIPS);
00130     cx->wasAllocated = PR_TRUE;
00131     if (rv != SECSuccess) {
00132        PORT_Free(cx); /* contains no secret info */
00133        cx = NULL;
00134     }
00135     return cx;
00136 }
00137 
00138 void
00139 HMAC_Begin(HMACContext *cx)
00140 {
00141     /* start inner hash */
00142     cx->hashobj->begin(cx->hash);
00143     cx->hashobj->update(cx->hash, cx->ipad, cx->hashobj->blocklength);
00144 }
00145 
00146 void
00147 HMAC_Update(HMACContext *cx, const unsigned char *data, unsigned int data_len)
00148 {
00149     cx->hashobj->update(cx->hash, data, data_len);
00150 }
00151 
00152 SECStatus
00153 HMAC_Finish(HMACContext *cx, unsigned char *result, unsigned int *result_len,
00154            unsigned int max_result_len)
00155 {
00156     if (max_result_len < cx->hashobj->length) {
00157        PORT_SetError(SEC_ERROR_INVALID_ARGS);
00158        return SECFailure;
00159     }
00160 
00161     cx->hashobj->end(cx->hash, result, result_len, max_result_len);
00162     if (*result_len != cx->hashobj->length)
00163        return SECFailure;
00164 
00165     cx->hashobj->begin(cx->hash);
00166     cx->hashobj->update(cx->hash, cx->opad, cx->hashobj->blocklength);
00167     cx->hashobj->update(cx->hash, result, *result_len);
00168     cx->hashobj->end(cx->hash, result, result_len, max_result_len);
00169     return SECSuccess;
00170 }
00171 
00172 HMACContext *
00173 HMAC_Clone(HMACContext *cx)
00174 {
00175     HMACContext *newcx;
00176 
00177     newcx = (HMACContext*)PORT_ZAlloc(sizeof(HMACContext));
00178     if (newcx == NULL)
00179        goto loser;
00180 
00181     newcx->wasAllocated = PR_TRUE;
00182     newcx->hashobj = cx->hashobj;
00183     newcx->hash = cx->hashobj->clone(cx->hash);
00184     if (newcx->hash == NULL)
00185        goto loser;
00186     PORT_Memcpy(newcx->ipad, cx->ipad, cx->hashobj->blocklength);
00187     PORT_Memcpy(newcx->opad, cx->opad, cx->hashobj->blocklength);
00188     return newcx;
00189 
00190 loser:
00191     HMAC_Destroy(newcx, PR_TRUE);
00192     return NULL;
00193 }