Import from 1.9a8 tarball
[mozilla-nss.git] / security / nss / lib / cryptohi / sechash.c
blobeebe23d956dbb1eac8b0f3e5c9ec3b23216973e9
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 ***** */
36 #include "sechash.h"
37 #include "secoidt.h"
38 #include "secerr.h"
39 #include "blapi.h"
40 #include "pk11func.h" /* for the PK11_ calls below. */
42 static void *
43 null_hash_new_context(void)
45 return NULL;
48 static void *
49 null_hash_clone_context(void *v)
51 PORT_Assert(v == NULL);
52 return NULL;
55 static void
56 null_hash_begin(void *v)
60 static void
61 null_hash_update(void *v, const unsigned char *input, unsigned int length)
65 static void
66 null_hash_end(void *v, unsigned char *output, unsigned int *outLen,
67 unsigned int maxOut)
69 *outLen = 0;
72 static void
73 null_hash_destroy_context(void *v, PRBool b)
75 PORT_Assert(v == NULL);
79 static void *
80 md2_NewContext(void) {
81 return (void *) PK11_CreateDigestContext(SEC_OID_MD2);
84 static void *
85 md5_NewContext(void) {
86 return (void *) PK11_CreateDigestContext(SEC_OID_MD5);
89 static void *
90 sha1_NewContext(void) {
91 return (void *) PK11_CreateDigestContext(SEC_OID_SHA1);
94 static void *
95 sha256_NewContext(void) {
96 return (void *) PK11_CreateDigestContext(SEC_OID_SHA256);
99 static void *
100 sha384_NewContext(void) {
101 return (void *) PK11_CreateDigestContext(SEC_OID_SHA384);
104 static void *
105 sha512_NewContext(void) {
106 return (void *) PK11_CreateDigestContext(SEC_OID_SHA512);
109 const SECHashObject SECHashObjects[] = {
110 { 0,
111 (void * (*)(void)) null_hash_new_context,
112 (void * (*)(void *)) null_hash_clone_context,
113 (void (*)(void *, PRBool)) null_hash_destroy_context,
114 (void (*)(void *)) null_hash_begin,
115 (void (*)(void *, const unsigned char *, unsigned int)) null_hash_update,
116 (void (*)(void *, unsigned char *, unsigned int *,
117 unsigned int)) null_hash_end,
119 HASH_AlgNULL
121 { MD2_LENGTH,
122 (void * (*)(void)) md2_NewContext,
123 (void * (*)(void *)) PK11_CloneContext,
124 (void (*)(void *, PRBool)) PK11_DestroyContext,
125 (void (*)(void *)) PK11_DigestBegin,
126 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp,
127 (void (*)(void *, unsigned char *, unsigned int *, unsigned int))
128 PK11_DigestFinal,
129 MD2_BLOCK_LENGTH,
130 HASH_AlgMD2
132 { MD5_LENGTH,
133 (void * (*)(void)) md5_NewContext,
134 (void * (*)(void *)) PK11_CloneContext,
135 (void (*)(void *, PRBool)) PK11_DestroyContext,
136 (void (*)(void *)) PK11_DigestBegin,
137 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp,
138 (void (*)(void *, unsigned char *, unsigned int *, unsigned int))
139 PK11_DigestFinal,
140 MD5_BLOCK_LENGTH,
141 HASH_AlgMD5
143 { SHA1_LENGTH,
144 (void * (*)(void)) sha1_NewContext,
145 (void * (*)(void *)) PK11_CloneContext,
146 (void (*)(void *, PRBool)) PK11_DestroyContext,
147 (void (*)(void *)) PK11_DigestBegin,
148 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp,
149 (void (*)(void *, unsigned char *, unsigned int *, unsigned int))
150 PK11_DigestFinal,
151 SHA1_BLOCK_LENGTH,
152 HASH_AlgSHA1
154 { SHA256_LENGTH,
155 (void * (*)(void)) sha256_NewContext,
156 (void * (*)(void *)) PK11_CloneContext,
157 (void (*)(void *, PRBool)) PK11_DestroyContext,
158 (void (*)(void *)) PK11_DigestBegin,
159 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp,
160 (void (*)(void *, unsigned char *, unsigned int *, unsigned int))
161 PK11_DigestFinal,
162 SHA256_BLOCK_LENGTH,
163 HASH_AlgSHA256
165 { SHA384_LENGTH,
166 (void * (*)(void)) sha384_NewContext,
167 (void * (*)(void *)) PK11_CloneContext,
168 (void (*)(void *, PRBool)) PK11_DestroyContext,
169 (void (*)(void *)) PK11_DigestBegin,
170 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp,
171 (void (*)(void *, unsigned char *, unsigned int *, unsigned int))
172 PK11_DigestFinal,
173 SHA384_BLOCK_LENGTH,
174 HASH_AlgSHA384
176 { SHA512_LENGTH,
177 (void * (*)(void)) sha512_NewContext,
178 (void * (*)(void *)) PK11_CloneContext,
179 (void (*)(void *, PRBool)) PK11_DestroyContext,
180 (void (*)(void *)) PK11_DigestBegin,
181 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp,
182 (void (*)(void *, unsigned char *, unsigned int *, unsigned int))
183 PK11_DigestFinal,
184 SHA512_BLOCK_LENGTH,
185 HASH_AlgSHA512
189 const SECHashObject *
190 HASH_GetHashObject(HASH_HashType type)
192 return &SECHashObjects[type];
195 HASH_HashType
196 HASH_GetHashTypeByOidTag(SECOidTag hashOid)
198 HASH_HashType ht = HASH_AlgNULL;
200 switch(hashOid) {
201 case SEC_OID_MD2: ht = HASH_AlgMD2; break;
202 case SEC_OID_MD5: ht = HASH_AlgMD5; break;
203 case SEC_OID_SHA1: ht = HASH_AlgSHA1; break;
204 case SEC_OID_SHA256: ht = HASH_AlgSHA256; break;
205 case SEC_OID_SHA384: ht = HASH_AlgSHA384; break;
206 case SEC_OID_SHA512: ht = HASH_AlgSHA512; break;
207 default: ht = HASH_AlgNULL;
208 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
209 break;
211 return ht;
214 const SECHashObject *
215 HASH_GetHashObjectByOidTag(SECOidTag hashOid)
217 HASH_HashType ht = HASH_GetHashTypeByOidTag(hashOid);
219 return (ht == HASH_AlgNULL) ? NULL : &SECHashObjects[ht];
222 /* returns zero for unknown hash OID */
223 unsigned int
224 HASH_ResultLenByOidTag(SECOidTag hashOid)
226 const SECHashObject * hashObject = HASH_GetHashObjectByOidTag(hashOid);
227 unsigned int resultLen = 0;
229 if (hashObject)
230 resultLen = hashObject->length;
231 return resultLen;
234 /* returns zero if hash type invalid. */
235 unsigned int
236 HASH_ResultLen(HASH_HashType type)
238 if ( ( type < HASH_AlgNULL ) || ( type >= HASH_AlgTOTAL ) ) {
239 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM);
240 return(0);
243 return(SECHashObjects[type].length);
246 unsigned int
247 HASH_ResultLenContext(HASHContext *context)
249 return(context->hashobj->length);
254 SECStatus
255 HASH_HashBuf(HASH_HashType type,
256 unsigned char *dest,
257 unsigned char *src,
258 uint32 src_len)
260 HASHContext *cx;
261 unsigned int part;
263 if ( ( type < HASH_AlgNULL ) || ( type >= HASH_AlgTOTAL ) ) {
264 return(SECFailure);
267 cx = HASH_Create(type);
268 if ( cx == NULL ) {
269 return(SECFailure);
271 HASH_Begin(cx);
272 HASH_Update(cx, src, src_len);
273 HASH_End(cx, dest, &part, HASH_ResultLenContext(cx));
274 HASH_Destroy(cx);
276 return(SECSuccess);
279 HASHContext *
280 HASH_Create(HASH_HashType type)
282 void *hash_context = NULL;
283 HASHContext *ret = NULL;
285 if ( ( type < HASH_AlgNULL ) || ( type >= HASH_AlgTOTAL ) ) {
286 return(NULL);
289 hash_context = (* SECHashObjects[type].create)();
290 if ( hash_context == NULL ) {
291 goto loser;
294 ret = (HASHContext *)PORT_Alloc(sizeof(HASHContext));
295 if ( ret == NULL ) {
296 goto loser;
299 ret->hash_context = hash_context;
300 ret->hashobj = &SECHashObjects[type];
302 return(ret);
304 loser:
305 if ( hash_context != NULL ) {
306 (* SECHashObjects[type].destroy)(hash_context, PR_TRUE);
309 return(NULL);
313 HASHContext *
314 HASH_Clone(HASHContext *context)
316 void *hash_context = NULL;
317 HASHContext *ret = NULL;
319 hash_context = (* context->hashobj->clone)(context->hash_context);
320 if ( hash_context == NULL ) {
321 goto loser;
324 ret = (HASHContext *)PORT_Alloc(sizeof(HASHContext));
325 if ( ret == NULL ) {
326 goto loser;
329 ret->hash_context = hash_context;
330 ret->hashobj = context->hashobj;
332 return(ret);
334 loser:
335 if ( hash_context != NULL ) {
336 (* context->hashobj->destroy)(hash_context, PR_TRUE);
339 return(NULL);
343 void
344 HASH_Destroy(HASHContext *context)
346 (* context->hashobj->destroy)(context->hash_context, PR_TRUE);
347 PORT_Free(context);
348 return;
352 void
353 HASH_Begin(HASHContext *context)
355 (* context->hashobj->begin)(context->hash_context);
356 return;
360 void
361 HASH_Update(HASHContext *context,
362 const unsigned char *src,
363 unsigned int len)
365 (* context->hashobj->update)(context->hash_context, src, len);
366 return;
369 void
370 HASH_End(HASHContext *context,
371 unsigned char *result,
372 unsigned int *result_len,
373 unsigned int max_result_len)
375 (* context->hashobj->end)(context->hash_context, result, result_len,
376 max_result_len);
377 return;