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
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.
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 ***** */
43 #define HMAC_PAD_SIZE HASH_BLOCK_LENGTH_MAX
45 struct HMACContextStr
{
47 const SECHashObject
*hashobj
;
49 unsigned char ipad
[HMAC_PAD_SIZE
];
50 unsigned char opad
[HMAC_PAD_SIZE
];
54 HMAC_Destroy(HMACContext
*cx
, PRBool freeit
)
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
);
69 HMAC_Init( HMACContext
* cx
, const SECHashObject
*hash_obj
,
70 const unsigned char *secret
, unsigned int secret_len
, PRBool isFIPS
)
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
);
81 PORT_SetError(SEC_ERROR_INVALID_ARGS
);
84 cx
->wasAllocated
= PR_FALSE
;
85 cx
->hashobj
= hash_obj
;
86 cx
->hash
= cx
->hashobj
->create();
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
);
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
);
115 PORT_Memset(hashed_secret
, 0, sizeof hashed_secret
);
116 if (cx
->hash
!= NULL
)
117 cx
->hashobj
->destroy(cx
->hash
, PR_TRUE
);
122 HMAC_Create(const SECHashObject
*hash_obj
, const unsigned char *secret
,
123 unsigned int secret_len
, PRBool isFIPS
)
126 HMACContext
* cx
= PORT_ZNew(HMACContext
);
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 */
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
);
147 HMAC_Update(HMACContext
*cx
, const unsigned char *data
, unsigned int data_len
)
149 cx
->hashobj
->update(cx
->hash
, data
, data_len
);
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
);
161 cx
->hashobj
->end(cx
->hash
, result
, result_len
, max_result_len
);
162 if (*result_len
!= cx
->hashobj
->length
)
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
);
173 HMAC_Clone(HMACContext
*cx
)
177 newcx
= (HMACContext
*)PORT_ZAlloc(sizeof(HMACContext
));
181 newcx
->wasAllocated
= PR_TRUE
;
182 newcx
->hashobj
= cx
->hashobj
;
183 newcx
->hash
= cx
->hashobj
->clone(cx
->hash
);
184 if (newcx
->hash
== NULL
)
186 PORT_Memcpy(newcx
->ipad
, cx
->ipad
, cx
->hashobj
->blocklength
);
187 PORT_Memcpy(newcx
->opad
, cx
->opad
, cx
->hashobj
->blocklength
);
191 HMAC_Destroy(newcx
, PR_TRUE
);