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 ***** */
40 #include "pk11func.h" /* for the PK11_ calls below. */
43 null_hash_new_context(void)
49 null_hash_clone_context(void *v)
51 PORT_Assert(v == NULL);
56 null_hash_begin(void *v)
61 null_hash_update(void *v, const unsigned char *input, unsigned int length)
66 null_hash_end(void *v, unsigned char *output, unsigned int *outLen,
73 null_hash_destroy_context(void *v, PRBool b)
75 PORT_Assert(v == NULL);
80 md2_NewContext(void) {
81 return (void *) PK11_CreateDigestContext(SEC_OID_MD2);
85 md5_NewContext(void) {
86 return (void *) PK11_CreateDigestContext(SEC_OID_MD5);
90 sha1_NewContext(void) {
91 return (void *) PK11_CreateDigestContext(SEC_OID_SHA1);
95 sha224_NewContext(void) {
96 return (void *) PK11_CreateDigestContext(SEC_OID_SHA224);
100 sha256_NewContext(void) {
101 return (void *) PK11_CreateDigestContext(SEC_OID_SHA256);
105 sha384_NewContext(void) {
106 return (void *) PK11_CreateDigestContext(SEC_OID_SHA384);
110 sha512_NewContext(void) {
111 return (void *) PK11_CreateDigestContext(SEC_OID_SHA512);
114 const SECHashObject SECHashObjects[] = {
116 (void * (*)(void)) null_hash_new_context,
117 (void * (*)(void *)) null_hash_clone_context,
118 (void (*)(void *, PRBool)) null_hash_destroy_context,
119 (void (*)(void *)) null_hash_begin,
120 (void (*)(void *, const unsigned char *, unsigned int)) null_hash_update,
121 (void (*)(void *, unsigned char *, unsigned int *,
122 unsigned int)) null_hash_end,
127 (void * (*)(void)) md2_NewContext,
128 (void * (*)(void *)) PK11_CloneContext,
129 (void (*)(void *, PRBool)) PK11_DestroyContext,
130 (void (*)(void *)) PK11_DigestBegin,
131 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp,
132 (void (*)(void *, unsigned char *, unsigned int *, unsigned int))
138 (void * (*)(void)) md5_NewContext,
139 (void * (*)(void *)) PK11_CloneContext,
140 (void (*)(void *, PRBool)) PK11_DestroyContext,
141 (void (*)(void *)) PK11_DigestBegin,
142 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp,
143 (void (*)(void *, unsigned char *, unsigned int *, unsigned int))
149 (void * (*)(void)) sha1_NewContext,
150 (void * (*)(void *)) PK11_CloneContext,
151 (void (*)(void *, PRBool)) PK11_DestroyContext,
152 (void (*)(void *)) PK11_DigestBegin,
153 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp,
154 (void (*)(void *, unsigned char *, unsigned int *, unsigned int))
160 (void * (*)(void)) sha256_NewContext,
161 (void * (*)(void *)) PK11_CloneContext,
162 (void (*)(void *, PRBool)) PK11_DestroyContext,
163 (void (*)(void *)) PK11_DigestBegin,
164 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp,
165 (void (*)(void *, unsigned char *, unsigned int *, unsigned int))
171 (void * (*)(void)) sha384_NewContext,
172 (void * (*)(void *)) PK11_CloneContext,
173 (void (*)(void *, PRBool)) PK11_DestroyContext,
174 (void (*)(void *)) PK11_DigestBegin,
175 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp,
176 (void (*)(void *, unsigned char *, unsigned int *, unsigned int))
182 (void * (*)(void)) sha512_NewContext,
183 (void * (*)(void *)) PK11_CloneContext,
184 (void (*)(void *, PRBool)) PK11_DestroyContext,
185 (void (*)(void *)) PK11_DigestBegin,
186 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp,
187 (void (*)(void *, unsigned char *, unsigned int *, unsigned int))
193 (void * (*)(void)) sha224_NewContext,
194 (void * (*)(void *)) PK11_CloneContext,
195 (void (*)(void *, PRBool)) PK11_DestroyContext,
196 (void (*)(void *)) PK11_DigestBegin,
197 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp,
198 (void (*)(void *, unsigned char *, unsigned int *, unsigned int))
205 const SECHashObject *
206 HASH_GetHashObject(HASH_HashType type)
208 return &SECHashObjects[type];
212 HASH_GetHashTypeByOidTag(SECOidTag hashOid)
214 HASH_HashType ht = HASH_AlgNULL;
217 case SEC_OID_MD2: ht = HASH_AlgMD2; break;
218 case SEC_OID_MD5: ht = HASH_AlgMD5; break;
219 case SEC_OID_SHA1: ht = HASH_AlgSHA1; break;
220 case SEC_OID_SHA224: ht = HASH_AlgSHA224; break;
221 case SEC_OID_SHA256: ht = HASH_AlgSHA256; break;
222 case SEC_OID_SHA384: ht = HASH_AlgSHA384; break;
223 case SEC_OID_SHA512: ht = HASH_AlgSHA512; break;
224 default: ht = HASH_AlgNULL;
225 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
232 HASH_GetHashOidTagByHMACOidTag(SECOidTag hmacOid)
234 SECOidTag hashOid = SEC_OID_UNKNOWN;
237 /* no oid exists for HMAC_MD2 */
238 /* NSS does not define a oid for HMAC_MD4 */
239 case SEC_OID_HMAC_SHA1: hashOid = SEC_OID_SHA1; break;
240 case SEC_OID_HMAC_SHA224: hashOid = SEC_OID_SHA224; break;
241 case SEC_OID_HMAC_SHA256: hashOid = SEC_OID_SHA256; break;
242 case SEC_OID_HMAC_SHA384: hashOid = SEC_OID_SHA384; break;
243 case SEC_OID_HMAC_SHA512: hashOid = SEC_OID_SHA512; break;
244 default: hashOid = SEC_OID_UNKNOWN;
245 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
252 HASH_GetHMACOidTagByHashOidTag(SECOidTag hashOid)
254 SECOidTag hmacOid = SEC_OID_UNKNOWN;
257 /* no oid exists for HMAC_MD2 */
258 /* NSS does not define a oid for HMAC_MD4 */
259 case SEC_OID_SHA1: hmacOid = SEC_OID_HMAC_SHA1; break;
260 case SEC_OID_SHA224: hmacOid = SEC_OID_HMAC_SHA224; break;
261 case SEC_OID_SHA256: hmacOid = SEC_OID_HMAC_SHA256; break;
262 case SEC_OID_SHA384: hmacOid = SEC_OID_HMAC_SHA384; break;
263 case SEC_OID_SHA512: hmacOid = SEC_OID_HMAC_SHA512; break;
264 default: hmacOid = SEC_OID_UNKNOWN;
265 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
271 const SECHashObject *
272 HASH_GetHashObjectByOidTag(SECOidTag hashOid)
274 HASH_HashType ht = HASH_GetHashTypeByOidTag(hashOid);
276 return (ht == HASH_AlgNULL) ? NULL : &SECHashObjects[ht];
279 /* returns zero for unknown hash OID */
281 HASH_ResultLenByOidTag(SECOidTag hashOid)
283 const SECHashObject * hashObject = HASH_GetHashObjectByOidTag(hashOid);
284 unsigned int resultLen = 0;
287 resultLen = hashObject->length;
291 /* returns zero if hash type invalid. */
293 HASH_ResultLen(HASH_HashType type)
295 if ( ( type < HASH_AlgNULL ) || ( type >= HASH_AlgTOTAL ) ) {
296 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
300 return(SECHashObjects[type].length);
304 HASH_ResultLenContext(HASHContext *context)
306 return(context->hashobj->length);
312 HASH_HashBuf(HASH_HashType type,
320 if ( ( type < HASH_AlgNULL ) || ( type >= HASH_AlgTOTAL ) ) {
324 cx = HASH_Create(type);
329 HASH_Update(cx, src, src_len);
330 HASH_End(cx, dest, &part, HASH_ResultLenContext(cx));
337 HASH_Create(HASH_HashType type)
339 void *hash_context = NULL;
340 HASHContext *ret = NULL;
342 if ( ( type < HASH_AlgNULL ) || ( type >= HASH_AlgTOTAL ) ) {
346 hash_context = (* SECHashObjects[type].create)();
347 if ( hash_context == NULL ) {
351 ret = (HASHContext *)PORT_Alloc(sizeof(HASHContext));
356 ret->hash_context = hash_context;
357 ret->hashobj = &SECHashObjects[type];
362 if ( hash_context != NULL ) {
363 (* SECHashObjects[type].destroy)(hash_context, PR_TRUE);
371 HASH_Clone(HASHContext *context)
373 void *hash_context = NULL;
374 HASHContext *ret = NULL;
376 hash_context = (* context->hashobj->clone)(context->hash_context);
377 if ( hash_context == NULL ) {
381 ret = (HASHContext *)PORT_Alloc(sizeof(HASHContext));
386 ret->hash_context = hash_context;
387 ret->hashobj = context->hashobj;
392 if ( hash_context != NULL ) {
393 (* context->hashobj->destroy)(hash_context, PR_TRUE);
401 HASH_Destroy(HASHContext *context)
403 (* context->hashobj->destroy)(context->hash_context, PR_TRUE);
410 HASH_Begin(HASHContext *context)
412 (* context->hashobj->begin)(context->hash_context);
418 HASH_Update(HASHContext *context,
419 const unsigned char *src,
422 (* context->hashobj->update)(context->hash_context, src, len);
427 HASH_End(HASHContext *context,
428 unsigned char *result,
429 unsigned int *result_len,
430 unsigned int max_result_len)
432 (* context->hashobj->end)(context->hash_context, result, result_len,
438 HASH_GetType(HASHContext *context)
440 return(context->hashobj->type);