Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / lib / freebl / alghmac.c
blobb1174f4602ccd43e8dbfab2d48e14aabb97ab124
1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
21 * Contributor(s):
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
37 #include "secport.h"
38 #include "hasht.h"
39 #include "blapit.h"
40 #include "alghmac.h"
41 #include "secerr.h"
43 #define HMAC_PAD_SIZE HASH_BLOCK_LENGTH_MAX
45 struct HMACContextStr {
46 void *hash;
47 const SECHashObject *hashobj;
48 PRBool wasAllocated;
49 unsigned char ipad[HMAC_PAD_SIZE];
50 unsigned char opad[HMAC_PAD_SIZE];
53 void
54 HMAC_Destroy(HMACContext *cx, PRBool freeit)
56 if (cx == NULL)
57 return;
59 PORT_Assert(!freeit == !cx->wasAllocated);
60 if (cx->hash != NULL) {
61 cx->hashobj->destroy(cx->hash, PR_TRUE);
62 PORT_Memset(cx, 0, sizeof *cx);
64 if (freeit)
65 PORT_Free(cx);
68 SECStatus
69 HMAC_Init( HMACContext * cx, const SECHashObject *hash_obj,
70 const unsigned char *secret, unsigned int secret_len, PRBool isFIPS)
72 unsigned int i;
73 unsigned char hashed_secret[HASH_LENGTH_MAX];
75 /* required by FIPS 198 Section 3 */
76 if (isFIPS && secret_len < hash_obj->length/2) {
77 PORT_SetError(SEC_ERROR_INVALID_ARGS);
78 return SECFailure;
80 if (cx == NULL) {
81 PORT_SetError(SEC_ERROR_INVALID_ARGS);
82 return SECFailure;
84 cx->wasAllocated = PR_FALSE;
85 cx->hashobj = hash_obj;
86 cx->hash = cx->hashobj->create();
87 if (cx->hash == NULL)
88 goto loser;
90 if (secret_len > cx->hashobj->blocklength) {
91 cx->hashobj->begin( cx->hash);
92 cx->hashobj->update(cx->hash, secret, secret_len);
93 PORT_Assert(cx->hashobj->length <= sizeof hashed_secret);
94 cx->hashobj->end( cx->hash, hashed_secret, &secret_len,
95 sizeof hashed_secret);
96 if (secret_len != cx->hashobj->length) {
97 PORT_SetError(SEC_ERROR_LIBRARY_FAILURE);
98 goto loser;
100 secret = (const unsigned char *)&hashed_secret[0];
103 PORT_Memset(cx->ipad, 0x36, cx->hashobj->blocklength);
104 PORT_Memset(cx->opad, 0x5c, cx->hashobj->blocklength);
106 /* fold secret into padding */
107 for (i = 0; i < secret_len; i++) {
108 cx->ipad[i] ^= secret[i];
109 cx->opad[i] ^= secret[i];
111 PORT_Memset(hashed_secret, 0, sizeof hashed_secret);
112 return SECSuccess;
114 loser:
115 PORT_Memset(hashed_secret, 0, sizeof hashed_secret);
116 if (cx->hash != NULL)
117 cx->hashobj->destroy(cx->hash, PR_TRUE);
118 return SECFailure;
121 HMACContext *
122 HMAC_Create(const SECHashObject *hash_obj, const unsigned char *secret,
123 unsigned int secret_len, PRBool isFIPS)
125 SECStatus rv;
126 HMACContext * cx = PORT_ZNew(HMACContext);
127 if (cx == NULL)
128 return NULL;
129 rv = HMAC_Init(cx, hash_obj, secret, secret_len, isFIPS);
130 cx->wasAllocated = PR_TRUE;
131 if (rv != SECSuccess) {
132 PORT_Free(cx); /* contains no secret info */
133 cx = NULL;
135 return cx;
138 void
139 HMAC_Begin(HMACContext *cx)
141 /* start inner hash */
142 cx->hashobj->begin(cx->hash);
143 cx->hashobj->update(cx->hash, cx->ipad, cx->hashobj->blocklength);
146 void
147 HMAC_Update(HMACContext *cx, const unsigned char *data, unsigned int data_len)
149 cx->hashobj->update(cx->hash, data, data_len);
152 SECStatus
153 HMAC_Finish(HMACContext *cx, unsigned char *result, unsigned int *result_len,
154 unsigned int max_result_len)
156 if (max_result_len < cx->hashobj->length) {
157 PORT_SetError(SEC_ERROR_INVALID_ARGS);
158 return SECFailure;
161 cx->hashobj->end(cx->hash, result, result_len, max_result_len);
162 if (*result_len != cx->hashobj->length)
163 return SECFailure;
165 cx->hashobj->begin(cx->hash);
166 cx->hashobj->update(cx->hash, cx->opad, cx->hashobj->blocklength);
167 cx->hashobj->update(cx->hash, result, *result_len);
168 cx->hashobj->end(cx->hash, result, result_len, max_result_len);
169 return SECSuccess;
172 HMACContext *
173 HMAC_Clone(HMACContext *cx)
175 HMACContext *newcx;
177 newcx = (HMACContext*)PORT_ZAlloc(sizeof(HMACContext));
178 if (newcx == NULL)
179 goto loser;
181 newcx->wasAllocated = PR_TRUE;
182 newcx->hashobj = cx->hashobj;
183 newcx->hash = cx->hashobj->clone(cx->hash);
184 if (newcx->hash == NULL)
185 goto loser;
186 PORT_Memcpy(newcx->ipad, cx->ipad, cx->hashobj->blocklength);
187 PORT_Memcpy(newcx->opad, cx->opad, cx->hashobj->blocklength);
188 return newcx;
190 loser:
191 HMAC_Destroy(newcx, PR_TRUE);
192 return NULL;