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 sha256_NewContext(void) {
96 return (void *) PK11_CreateDigestContext(SEC_OID_SHA256
);
100 sha384_NewContext(void) {
101 return (void *) PK11_CreateDigestContext(SEC_OID_SHA384
);
105 sha512_NewContext(void) {
106 return (void *) PK11_CreateDigestContext(SEC_OID_SHA512
);
109 const SECHashObject SECHashObjects
[] = {
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
,
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))
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))
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))
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))
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))
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))
189 const SECHashObject
*
190 HASH_GetHashObject(HASH_HashType type
)
192 return &SECHashObjects
[type
];
196 HASH_GetHashTypeByOidTag(SECOidTag hashOid
)
198 HASH_HashType ht
= HASH_AlgNULL
;
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
);
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 */
224 HASH_ResultLenByOidTag(SECOidTag hashOid
)
226 const SECHashObject
* hashObject
= HASH_GetHashObjectByOidTag(hashOid
);
227 unsigned int resultLen
= 0;
230 resultLen
= hashObject
->length
;
234 /* returns zero if hash type invalid. */
236 HASH_ResultLen(HASH_HashType type
)
238 if ( ( type
< HASH_AlgNULL
) || ( type
>= HASH_AlgTOTAL
) ) {
239 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM
);
243 return(SECHashObjects
[type
].length
);
247 HASH_ResultLenContext(HASHContext
*context
)
249 return(context
->hashobj
->length
);
255 HASH_HashBuf(HASH_HashType type
,
263 if ( ( type
< HASH_AlgNULL
) || ( type
>= HASH_AlgTOTAL
) ) {
267 cx
= HASH_Create(type
);
272 HASH_Update(cx
, src
, src_len
);
273 HASH_End(cx
, dest
, &part
, HASH_ResultLenContext(cx
));
280 HASH_Create(HASH_HashType type
)
282 void *hash_context
= NULL
;
283 HASHContext
*ret
= NULL
;
285 if ( ( type
< HASH_AlgNULL
) || ( type
>= HASH_AlgTOTAL
) ) {
289 hash_context
= (* SECHashObjects
[type
].create
)();
290 if ( hash_context
== NULL
) {
294 ret
= (HASHContext
*)PORT_Alloc(sizeof(HASHContext
));
299 ret
->hash_context
= hash_context
;
300 ret
->hashobj
= &SECHashObjects
[type
];
305 if ( hash_context
!= NULL
) {
306 (* SECHashObjects
[type
].destroy
)(hash_context
, PR_TRUE
);
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
) {
324 ret
= (HASHContext
*)PORT_Alloc(sizeof(HASHContext
));
329 ret
->hash_context
= hash_context
;
330 ret
->hashobj
= context
->hashobj
;
335 if ( hash_context
!= NULL
) {
336 (* context
->hashobj
->destroy
)(hash_context
, PR_TRUE
);
344 HASH_Destroy(HASHContext
*context
)
346 (* context
->hashobj
->destroy
)(context
->hash_context
, PR_TRUE
);
353 HASH_Begin(HASHContext
*context
)
355 (* context
->hashobj
->begin
)(context
->hash_context
);
361 HASH_Update(HASHContext
*context
,
362 const unsigned char *src
,
365 (* context
->hashobj
->update
)(context
->hash_context
, src
, len
);
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
,