| OLD | NEW |
| 1 /* This Source Code Form is subject to the terms of the Mozilla Public | 1 /* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 * License, v. 2.0. If a copy of the MPL was not distributed with this | 2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 #include "sechash.h" | 4 #include "sechash.h" |
| 5 #include "secoidt.h" | 5 #include "secoidt.h" |
| 6 #include "secerr.h" | 6 #include "secerr.h" |
| 7 #include "blapi.h" | 7 #include "blapi.h" |
| 8 #include "pk11func.h"» /* for the PK11_ calls below. */ | 8 #include "pk11func.h" /* for the PK11_ calls below. */ |
| 9 | 9 |
| 10 static void * | 10 static void * |
| 11 null_hash_new_context(void) | 11 null_hash_new_context(void) |
| 12 { | 12 { |
| 13 return NULL; | 13 return NULL; |
| 14 } | 14 } |
| 15 | 15 |
| 16 static void * | 16 static void * |
| 17 null_hash_clone_context(void *v) | 17 null_hash_clone_context(void *v) |
| 18 { | 18 { |
| 19 PORT_Assert(v == NULL); | 19 PORT_Assert(v == NULL); |
| 20 return NULL; | 20 return NULL; |
| 21 } | 21 } |
| 22 | 22 |
| 23 static void | 23 static void |
| 24 null_hash_begin(void *v) | 24 null_hash_begin(void *v) |
| 25 { | 25 { |
| 26 } | 26 } |
| 27 | 27 |
| 28 static void | 28 static void |
| 29 null_hash_update(void *v, const unsigned char *input, unsigned int length) | 29 null_hash_update(void *v, const unsigned char *input, unsigned int length) |
| 30 { | 30 { |
| 31 } | 31 } |
| 32 | 32 |
| 33 static void | 33 static void |
| 34 null_hash_end(void *v, unsigned char *output, unsigned int *outLen, | 34 null_hash_end(void *v, unsigned char *output, unsigned int *outLen, |
| 35 » unsigned int maxOut) | 35 unsigned int maxOut) |
| 36 { | 36 { |
| 37 *outLen = 0; | 37 *outLen = 0; |
| 38 } | 38 } |
| 39 | 39 |
| 40 static void | 40 static void |
| 41 null_hash_destroy_context(void *v, PRBool b) | 41 null_hash_destroy_context(void *v, PRBool b) |
| 42 { | 42 { |
| 43 PORT_Assert(v == NULL); | 43 PORT_Assert(v == NULL); |
| 44 } | 44 } |
| 45 | 45 |
| 46 | |
| 47 static void * | 46 static void * |
| 48 md2_NewContext(void) { | 47 md2_NewContext(void) |
| 49 » return (void *) PK11_CreateDigestContext(SEC_OID_MD2); | 48 { |
| 49 return (void *)PK11_CreateDigestContext(SEC_OID_MD2); |
| 50 } | 50 } |
| 51 | 51 |
| 52 static void * | 52 static void * |
| 53 md5_NewContext(void) { | 53 md5_NewContext(void) |
| 54 » return (void *) PK11_CreateDigestContext(SEC_OID_MD5); | 54 { |
| 55 return (void *)PK11_CreateDigestContext(SEC_OID_MD5); |
| 55 } | 56 } |
| 56 | 57 |
| 57 static void * | 58 static void * |
| 58 sha1_NewContext(void) { | 59 sha1_NewContext(void) |
| 59 » return (void *) PK11_CreateDigestContext(SEC_OID_SHA1); | 60 { |
| 61 return (void *)PK11_CreateDigestContext(SEC_OID_SHA1); |
| 60 } | 62 } |
| 61 | 63 |
| 62 static void * | 64 static void * |
| 63 sha224_NewContext(void) { | 65 sha224_NewContext(void) |
| 64 » return (void *) PK11_CreateDigestContext(SEC_OID_SHA224); | 66 { |
| 67 return (void *)PK11_CreateDigestContext(SEC_OID_SHA224); |
| 65 } | 68 } |
| 66 | 69 |
| 67 static void * | 70 static void * |
| 68 sha256_NewContext(void) { | 71 sha256_NewContext(void) |
| 69 » return (void *) PK11_CreateDigestContext(SEC_OID_SHA256); | 72 { |
| 73 return (void *)PK11_CreateDigestContext(SEC_OID_SHA256); |
| 70 } | 74 } |
| 71 | 75 |
| 72 static void * | 76 static void * |
| 73 sha384_NewContext(void) { | 77 sha384_NewContext(void) |
| 74 » return (void *) PK11_CreateDigestContext(SEC_OID_SHA384); | 78 { |
| 79 return (void *)PK11_CreateDigestContext(SEC_OID_SHA384); |
| 75 } | 80 } |
| 76 | 81 |
| 77 static void * | 82 static void * |
| 78 sha512_NewContext(void) { | 83 sha512_NewContext(void) |
| 79 » return (void *) PK11_CreateDigestContext(SEC_OID_SHA512); | 84 { |
| 85 return (void *)PK11_CreateDigestContext(SEC_OID_SHA512); |
| 80 } | 86 } |
| 81 | 87 |
| 82 const SECHashObject SECHashObjects[] = { | 88 const SECHashObject SECHashObjects[] = { |
| 83 { 0, | 89 { 0, |
| 84 (void * (*)(void)) null_hash_new_context, | 90 (void *(*)(void))null_hash_new_context, |
| 85 (void * (*)(void *)) null_hash_clone_context, | 91 (void *(*)(void *))null_hash_clone_context, |
| 86 (void (*)(void *, PRBool)) null_hash_destroy_context, | 92 (void (*)(void *, PRBool))null_hash_destroy_context, |
| 87 (void (*)(void *)) null_hash_begin, | 93 (void (*)(void *))null_hash_begin, |
| 88 (void (*)(void *, const unsigned char *, unsigned int)) null_hash_update, | 94 (void (*)(void *, const unsigned char *, unsigned int))null_hash_update, |
| 89 (void (*)(void *, unsigned char *, unsigned int *, | 95 (void (*)(void *, unsigned char *, unsigned int *, |
| 90 » unsigned int)) null_hash_end, | 96 unsigned int))null_hash_end, |
| 91 0, | 97 0, |
| 92 HASH_AlgNULL | 98 HASH_AlgNULL }, |
| 93 }, | 99 { MD2_LENGTH, |
| 94 { MD2_LENGTH, | 100 (void *(*)(void))md2_NewContext, |
| 95 (void * (*)(void)) md2_NewContext, | 101 (void *(*)(void *))PK11_CloneContext, |
| 96 (void * (*)(void *)) PK11_CloneContext, | 102 (void (*)(void *, PRBool))PK11_DestroyContext, |
| 97 (void (*)(void *, PRBool)) PK11_DestroyContext, | 103 (void (*)(void *))PK11_DigestBegin, |
| 98 (void (*)(void *)) PK11_DigestBegin, | 104 (void (*)(void *, const unsigned char *, unsigned int))PK11_DigestOp, |
| 99 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp, | 105 (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) |
| 100 (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) | 106 PK11_DigestFinal, |
| 101 » » » » » » » PK11_DigestFinal, | 107 MD2_BLOCK_LENGTH, |
| 102 MD2_BLOCK_LENGTH, | 108 HASH_AlgMD2 }, |
| 103 HASH_AlgMD2 | 109 { MD5_LENGTH, |
| 104 }, | 110 (void *(*)(void))md5_NewContext, |
| 105 { MD5_LENGTH, | 111 (void *(*)(void *))PK11_CloneContext, |
| 106 (void * (*)(void)) md5_NewContext, | 112 (void (*)(void *, PRBool))PK11_DestroyContext, |
| 107 (void * (*)(void *)) PK11_CloneContext, | 113 (void (*)(void *))PK11_DigestBegin, |
| 108 (void (*)(void *, PRBool)) PK11_DestroyContext, | 114 (void (*)(void *, const unsigned char *, unsigned int))PK11_DigestOp, |
| 109 (void (*)(void *)) PK11_DigestBegin, | 115 (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) |
| 110 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp, | 116 PK11_DigestFinal, |
| 111 (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) | 117 MD5_BLOCK_LENGTH, |
| 112 » » » » » » » PK11_DigestFinal, | 118 HASH_AlgMD5 }, |
| 113 MD5_BLOCK_LENGTH, | 119 { SHA1_LENGTH, |
| 114 HASH_AlgMD5 | 120 (void *(*)(void))sha1_NewContext, |
| 115 }, | 121 (void *(*)(void *))PK11_CloneContext, |
| 116 { SHA1_LENGTH, | 122 (void (*)(void *, PRBool))PK11_DestroyContext, |
| 117 (void * (*)(void)) sha1_NewContext, | 123 (void (*)(void *))PK11_DigestBegin, |
| 118 (void * (*)(void *)) PK11_CloneContext, | 124 (void (*)(void *, const unsigned char *, unsigned int))PK11_DigestOp, |
| 119 (void (*)(void *, PRBool)) PK11_DestroyContext, | 125 (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) |
| 120 (void (*)(void *)) PK11_DigestBegin, | 126 PK11_DigestFinal, |
| 121 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp, | 127 SHA1_BLOCK_LENGTH, |
| 122 (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) | 128 HASH_AlgSHA1 }, |
| 123 » » » » » » » PK11_DigestFinal, | 129 { SHA256_LENGTH, |
| 124 SHA1_BLOCK_LENGTH, | 130 (void *(*)(void))sha256_NewContext, |
| 125 HASH_AlgSHA1 | 131 (void *(*)(void *))PK11_CloneContext, |
| 126 }, | 132 (void (*)(void *, PRBool))PK11_DestroyContext, |
| 127 { SHA256_LENGTH, | 133 (void (*)(void *))PK11_DigestBegin, |
| 128 (void * (*)(void)) sha256_NewContext, | 134 (void (*)(void *, const unsigned char *, unsigned int))PK11_DigestOp, |
| 129 (void * (*)(void *)) PK11_CloneContext, | 135 (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) |
| 130 (void (*)(void *, PRBool)) PK11_DestroyContext, | 136 PK11_DigestFinal, |
| 131 (void (*)(void *)) PK11_DigestBegin, | 137 SHA256_BLOCK_LENGTH, |
| 132 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp, | 138 HASH_AlgSHA256 }, |
| 133 (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) | 139 { SHA384_LENGTH, |
| 134 » » » » » » » PK11_DigestFinal, | 140 (void *(*)(void))sha384_NewContext, |
| 135 SHA256_BLOCK_LENGTH, | 141 (void *(*)(void *))PK11_CloneContext, |
| 136 HASH_AlgSHA256 | 142 (void (*)(void *, PRBool))PK11_DestroyContext, |
| 137 }, | 143 (void (*)(void *))PK11_DigestBegin, |
| 138 { SHA384_LENGTH, | 144 (void (*)(void *, const unsigned char *, unsigned int))PK11_DigestOp, |
| 139 (void * (*)(void)) sha384_NewContext, | 145 (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) |
| 140 (void * (*)(void *)) PK11_CloneContext, | 146 PK11_DigestFinal, |
| 141 (void (*)(void *, PRBool)) PK11_DestroyContext, | 147 SHA384_BLOCK_LENGTH, |
| 142 (void (*)(void *)) PK11_DigestBegin, | 148 HASH_AlgSHA384 }, |
| 143 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp, | 149 { SHA512_LENGTH, |
| 144 (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) | 150 (void *(*)(void))sha512_NewContext, |
| 145 » » » » » » » PK11_DigestFinal, | 151 (void *(*)(void *))PK11_CloneContext, |
| 146 SHA384_BLOCK_LENGTH, | 152 (void (*)(void *, PRBool))PK11_DestroyContext, |
| 147 HASH_AlgSHA384 | 153 (void (*)(void *))PK11_DigestBegin, |
| 148 }, | 154 (void (*)(void *, const unsigned char *, unsigned int))PK11_DigestOp, |
| 149 { SHA512_LENGTH, | 155 (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) |
| 150 (void * (*)(void)) sha512_NewContext, | 156 PK11_DigestFinal, |
| 151 (void * (*)(void *)) PK11_CloneContext, | 157 SHA512_BLOCK_LENGTH, |
| 152 (void (*)(void *, PRBool)) PK11_DestroyContext, | 158 HASH_AlgSHA512 }, |
| 153 (void (*)(void *)) PK11_DigestBegin, | 159 { SHA224_LENGTH, |
| 154 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp, | 160 (void *(*)(void))sha224_NewContext, |
| 155 (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) | 161 (void *(*)(void *))PK11_CloneContext, |
| 156 » » » » » » » PK11_DigestFinal, | 162 (void (*)(void *, PRBool))PK11_DestroyContext, |
| 157 SHA512_BLOCK_LENGTH, | 163 (void (*)(void *))PK11_DigestBegin, |
| 158 HASH_AlgSHA512 | 164 (void (*)(void *, const unsigned char *, unsigned int))PK11_DigestOp, |
| 159 }, | 165 (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) |
| 160 { SHA224_LENGTH, | 166 PK11_DigestFinal, |
| 161 (void * (*)(void)) sha224_NewContext, | 167 SHA224_BLOCK_LENGTH, |
| 162 (void * (*)(void *)) PK11_CloneContext, | 168 HASH_AlgSHA224 }, |
| 163 (void (*)(void *, PRBool)) PK11_DestroyContext, | |
| 164 (void (*)(void *)) PK11_DigestBegin, | |
| 165 (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp, | |
| 166 (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) | |
| 167 » » » » » » » PK11_DigestFinal, | |
| 168 SHA224_BLOCK_LENGTH, | |
| 169 HASH_AlgSHA224 | |
| 170 }, | |
| 171 }; | 169 }; |
| 172 | 170 |
| 173 const SECHashObject * | 171 const SECHashObject * |
| 174 HASH_GetHashObject(HASH_HashType type) | 172 HASH_GetHashObject(HASH_HashType type) |
| 175 { | 173 { |
| 176 return &SECHashObjects[type]; | 174 return &SECHashObjects[type]; |
| 177 } | 175 } |
| 178 | 176 |
| 179 HASH_HashType | 177 HASH_HashType |
| 180 HASH_GetHashTypeByOidTag(SECOidTag hashOid) | 178 HASH_GetHashTypeByOidTag(SECOidTag hashOid) |
| 181 { | 179 { |
| 182 HASH_HashType ht» = HASH_AlgNULL; | 180 HASH_HashType ht = HASH_AlgNULL; |
| 183 | 181 |
| 184 switch(hashOid) { | 182 switch (hashOid) { |
| 185 case SEC_OID_MD2:» ht = HASH_AlgMD2; break; | 183 case SEC_OID_MD2: |
| 186 case SEC_OID_MD5:» ht = HASH_AlgMD5; break; | 184 ht = HASH_AlgMD2; |
| 187 case SEC_OID_SHA1:» ht = HASH_AlgSHA1; break; | 185 break; |
| 188 case SEC_OID_SHA224: ht = HASH_AlgSHA224; break; | 186 case SEC_OID_MD5: |
| 189 case SEC_OID_SHA256: ht = HASH_AlgSHA256; break; | 187 ht = HASH_AlgMD5; |
| 190 case SEC_OID_SHA384: ht = HASH_AlgSHA384; break; | 188 break; |
| 191 case SEC_OID_SHA512: ht = HASH_AlgSHA512; break; | 189 case SEC_OID_SHA1: |
| 192 default: ht = HASH_AlgNULL; | 190 ht = HASH_AlgSHA1; |
| 193 » PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); | 191 break; |
| 194 » break; | 192 case SEC_OID_SHA224: |
| 193 ht = HASH_AlgSHA224; |
| 194 break; |
| 195 case SEC_OID_SHA256: |
| 196 ht = HASH_AlgSHA256; |
| 197 break; |
| 198 case SEC_OID_SHA384: |
| 199 ht = HASH_AlgSHA384; |
| 200 break; |
| 201 case SEC_OID_SHA512: |
| 202 ht = HASH_AlgSHA512; |
| 203 break; |
| 204 default: |
| 205 ht = HASH_AlgNULL; |
| 206 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); |
| 207 break; |
| 195 } | 208 } |
| 196 return ht; | 209 return ht; |
| 197 } | 210 } |
| 198 | 211 |
| 199 SECOidTag | 212 SECOidTag |
| 200 HASH_GetHashOidTagByHMACOidTag(SECOidTag hmacOid) | 213 HASH_GetHashOidTagByHMACOidTag(SECOidTag hmacOid) |
| 201 { | 214 { |
| 202 SECOidTag hashOid = SEC_OID_UNKNOWN; | 215 SECOidTag hashOid = SEC_OID_UNKNOWN; |
| 203 | 216 |
| 204 switch(hmacOid) { | 217 switch (hmacOid) { |
| 205 /* no oid exists for HMAC_MD2 */ | 218 /* no oid exists for HMAC_MD2 */ |
| 206 /* NSS does not define a oid for HMAC_MD4 */ | 219 /* NSS does not define a oid for HMAC_MD4 */ |
| 207 case SEC_OID_HMAC_SHA1: hashOid = SEC_OID_SHA1; break; | 220 case SEC_OID_HMAC_SHA1: |
| 208 case SEC_OID_HMAC_SHA224: hashOid = SEC_OID_SHA224; break; | 221 hashOid = SEC_OID_SHA1; |
| 209 case SEC_OID_HMAC_SHA256: hashOid = SEC_OID_SHA256; break; | 222 break; |
| 210 case SEC_OID_HMAC_SHA384: hashOid = SEC_OID_SHA384; break; | 223 case SEC_OID_HMAC_SHA224: |
| 211 case SEC_OID_HMAC_SHA512: hashOid = SEC_OID_SHA512; break; | 224 hashOid = SEC_OID_SHA224; |
| 212 default: hashOid = SEC_OID_UNKNOWN; | 225 break; |
| 213 » PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); | 226 case SEC_OID_HMAC_SHA256: |
| 214 » break; | 227 hashOid = SEC_OID_SHA256; |
| 228 break; |
| 229 case SEC_OID_HMAC_SHA384: |
| 230 hashOid = SEC_OID_SHA384; |
| 231 break; |
| 232 case SEC_OID_HMAC_SHA512: |
| 233 hashOid = SEC_OID_SHA512; |
| 234 break; |
| 235 default: |
| 236 hashOid = SEC_OID_UNKNOWN; |
| 237 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); |
| 238 break; |
| 215 } | 239 } |
| 216 return hashOid; | 240 return hashOid; |
| 217 } | 241 } |
| 218 | 242 |
| 219 SECOidTag | 243 SECOidTag |
| 220 HASH_GetHMACOidTagByHashOidTag(SECOidTag hashOid) | 244 HASH_GetHMACOidTagByHashOidTag(SECOidTag hashOid) |
| 221 { | 245 { |
| 222 SECOidTag hmacOid = SEC_OID_UNKNOWN; | 246 SECOidTag hmacOid = SEC_OID_UNKNOWN; |
| 223 | 247 |
| 224 switch(hashOid) { | 248 switch (hashOid) { |
| 225 /* no oid exists for HMAC_MD2 */ | 249 /* no oid exists for HMAC_MD2 */ |
| 226 /* NSS does not define a oid for HMAC_MD4 */ | 250 /* NSS does not define a oid for HMAC_MD4 */ |
| 227 case SEC_OID_SHA1: hmacOid = SEC_OID_HMAC_SHA1; break; | 251 case SEC_OID_SHA1: |
| 228 case SEC_OID_SHA224: hmacOid = SEC_OID_HMAC_SHA224; break; | 252 hmacOid = SEC_OID_HMAC_SHA1; |
| 229 case SEC_OID_SHA256: hmacOid = SEC_OID_HMAC_SHA256; break; | 253 break; |
| 230 case SEC_OID_SHA384: hmacOid = SEC_OID_HMAC_SHA384; break; | 254 case SEC_OID_SHA224: |
| 231 case SEC_OID_SHA512: hmacOid = SEC_OID_HMAC_SHA512; break; | 255 hmacOid = SEC_OID_HMAC_SHA224; |
| 232 default: hmacOid = SEC_OID_UNKNOWN; | 256 break; |
| 233 » PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); | 257 case SEC_OID_SHA256: |
| 234 » break; | 258 hmacOid = SEC_OID_HMAC_SHA256; |
| 259 break; |
| 260 case SEC_OID_SHA384: |
| 261 hmacOid = SEC_OID_HMAC_SHA384; |
| 262 break; |
| 263 case SEC_OID_SHA512: |
| 264 hmacOid = SEC_OID_HMAC_SHA512; |
| 265 break; |
| 266 default: |
| 267 hmacOid = SEC_OID_UNKNOWN; |
| 268 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); |
| 269 break; |
| 235 } | 270 } |
| 236 return hmacOid; | 271 return hmacOid; |
| 237 } | 272 } |
| 238 | 273 |
| 239 const SECHashObject * | 274 const SECHashObject * |
| 240 HASH_GetHashObjectByOidTag(SECOidTag hashOid) | 275 HASH_GetHashObjectByOidTag(SECOidTag hashOid) |
| 241 { | 276 { |
| 242 HASH_HashType ht» = HASH_GetHashTypeByOidTag(hashOid); | 277 HASH_HashType ht = HASH_GetHashTypeByOidTag(hashOid); |
| 243 | 278 |
| 244 return (ht == HASH_AlgNULL) ? NULL : &SECHashObjects[ht]; | 279 return (ht == HASH_AlgNULL) ? NULL : &SECHashObjects[ht]; |
| 245 } | 280 } |
| 246 | 281 |
| 247 /* returns zero for unknown hash OID */ | 282 /* returns zero for unknown hash OID */ |
| 248 unsigned int | 283 unsigned int |
| 249 HASH_ResultLenByOidTag(SECOidTag hashOid) | 284 HASH_ResultLenByOidTag(SECOidTag hashOid) |
| 250 { | 285 { |
| 251 const SECHashObject * hashObject = HASH_GetHashObjectByOidTag(hashOid); | 286 const SECHashObject *hashObject = HASH_GetHashObjectByOidTag(hashOid); |
| 252 unsigned int resultLen = 0; | 287 unsigned int resultLen = 0; |
| 253 | 288 |
| 254 if (hashObject) | 289 if (hashObject) |
| 255 » resultLen = hashObject->length; | 290 resultLen = hashObject->length; |
| 256 return resultLen; | 291 return resultLen; |
| 257 } | 292 } |
| 258 | 293 |
| 259 /* returns zero if hash type invalid. */ | 294 /* returns zero if hash type invalid. */ |
| 260 unsigned int | 295 unsigned int |
| 261 HASH_ResultLen(HASH_HashType type) | 296 HASH_ResultLen(HASH_HashType type) |
| 262 { | 297 { |
| 263 if ( ( type < HASH_AlgNULL ) || ( type >= HASH_AlgTOTAL ) ) { | 298 if ((type < HASH_AlgNULL) || (type >= HASH_AlgTOTAL)) { |
| 264 » PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); | 299 PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); |
| 265 » return(0); | 300 return (0); |
| 266 } | 301 } |
| 267 | 302 |
| 268 return(SECHashObjects[type].length); | 303 return (SECHashObjects[type].length); |
| 269 } | 304 } |
| 270 | 305 |
| 271 unsigned int | 306 unsigned int |
| 272 HASH_ResultLenContext(HASHContext *context) | 307 HASH_ResultLenContext(HASHContext *context) |
| 273 { | 308 { |
| 274 return(context->hashobj->length); | 309 return (context->hashobj->length); |
| 275 } | 310 } |
| 276 | 311 |
| 277 | |
| 278 | |
| 279 SECStatus | 312 SECStatus |
| 280 HASH_HashBuf(HASH_HashType type, | 313 HASH_HashBuf(HASH_HashType type, |
| 281 » unsigned char *dest, | 314 unsigned char *dest, |
| 282 » const unsigned char *src, | 315 const unsigned char *src, |
| 283 » PRUint32 src_len) | 316 PRUint32 src_len) |
| 284 { | 317 { |
| 285 HASHContext *cx; | 318 HASHContext *cx; |
| 286 unsigned int part; | 319 unsigned int part; |
| 287 | 320 |
| 288 if ( ( type < HASH_AlgNULL ) || ( type >= HASH_AlgTOTAL ) ) { | 321 if ((type < HASH_AlgNULL) || (type >= HASH_AlgTOTAL)) { |
| 289 » return(SECFailure); | 322 return (SECFailure); |
| 290 } | 323 } |
| 291 | 324 |
| 292 cx = HASH_Create(type); | 325 cx = HASH_Create(type); |
| 293 if ( cx == NULL ) { | 326 if (cx == NULL) { |
| 294 » return(SECFailure); | 327 return (SECFailure); |
| 295 } | 328 } |
| 296 HASH_Begin(cx); | 329 HASH_Begin(cx); |
| 297 HASH_Update(cx, src, src_len); | 330 HASH_Update(cx, src, src_len); |
| 298 HASH_End(cx, dest, &part, HASH_ResultLenContext(cx)); | 331 HASH_End(cx, dest, &part, HASH_ResultLenContext(cx)); |
| 299 HASH_Destroy(cx); | 332 HASH_Destroy(cx); |
| 300 | 333 |
| 301 return(SECSuccess); | 334 return (SECSuccess); |
| 302 } | 335 } |
| 303 | 336 |
| 304 HASHContext * | 337 HASHContext * |
| 305 HASH_Create(HASH_HashType type) | 338 HASH_Create(HASH_HashType type) |
| 306 { | 339 { |
| 307 void *hash_context = NULL; | 340 void *hash_context = NULL; |
| 308 HASHContext *ret = NULL; | 341 HASHContext *ret = NULL; |
| 309 | 342 |
| 310 if ( ( type < HASH_AlgNULL ) || ( type >= HASH_AlgTOTAL ) ) { | 343 if ((type < HASH_AlgNULL) || (type >= HASH_AlgTOTAL)) { |
| 311 » return(NULL); | 344 return (NULL); |
| 312 } | 345 } |
| 313 | 346 |
| 314 hash_context = (* SECHashObjects[type].create)(); | 347 hash_context = (*SECHashObjects[type].create)(); |
| 315 if ( hash_context == NULL ) { | 348 if (hash_context == NULL) { |
| 316 » goto loser; | 349 goto loser; |
| 317 } | 350 } |
| 318 | 351 |
| 319 ret = (HASHContext *)PORT_Alloc(sizeof(HASHContext)); | 352 ret = (HASHContext *)PORT_Alloc(sizeof(HASHContext)); |
| 320 if ( ret == NULL ) { | 353 if (ret == NULL) { |
| 321 » goto loser; | 354 goto loser; |
| 322 } | 355 } |
| 323 | 356 |
| 324 ret->hash_context = hash_context; | 357 ret->hash_context = hash_context; |
| 325 ret->hashobj = &SECHashObjects[type]; | 358 ret->hashobj = &SECHashObjects[type]; |
| 326 | 359 |
| 327 return(ret); | 360 return (ret); |
| 328 | 361 |
| 329 loser: | 362 loser: |
| 330 if ( hash_context != NULL ) { | 363 if (hash_context != NULL) { |
| 331 » (* SECHashObjects[type].destroy)(hash_context, PR_TRUE); | 364 (*SECHashObjects[type].destroy)(hash_context, PR_TRUE); |
| 332 } | 365 } |
| 333 | 366 |
| 334 return(NULL); | 367 return (NULL); |
| 335 } | 368 } |
| 336 | 369 |
| 337 | |
| 338 HASHContext * | 370 HASHContext * |
| 339 HASH_Clone(HASHContext *context) | 371 HASH_Clone(HASHContext *context) |
| 340 { | 372 { |
| 341 void *hash_context = NULL; | 373 void *hash_context = NULL; |
| 342 HASHContext *ret = NULL; | 374 HASHContext *ret = NULL; |
| 343 | 375 |
| 344 hash_context = (* context->hashobj->clone)(context->hash_context); | 376 hash_context = (*context->hashobj->clone)(context->hash_context); |
| 345 if ( hash_context == NULL ) { | 377 if (hash_context == NULL) { |
| 346 » goto loser; | 378 goto loser; |
| 347 } | 379 } |
| 348 | 380 |
| 349 ret = (HASHContext *)PORT_Alloc(sizeof(HASHContext)); | 381 ret = (HASHContext *)PORT_Alloc(sizeof(HASHContext)); |
| 350 if ( ret == NULL ) { | 382 if (ret == NULL) { |
| 351 » goto loser; | 383 goto loser; |
| 352 } | 384 } |
| 353 | 385 |
| 354 ret->hash_context = hash_context; | 386 ret->hash_context = hash_context; |
| 355 ret->hashobj = context->hashobj; | 387 ret->hashobj = context->hashobj; |
| 356 | 388 |
| 357 return(ret); | 389 return (ret); |
| 358 | 390 |
| 359 loser: | 391 loser: |
| 360 if ( hash_context != NULL ) { | 392 if (hash_context != NULL) { |
| 361 » (* context->hashobj->destroy)(hash_context, PR_TRUE); | 393 (*context->hashobj->destroy)(hash_context, PR_TRUE); |
| 362 } | 394 } |
| 363 | |
| 364 return(NULL); | |
| 365 | 395 |
| 396 return (NULL); |
| 366 } | 397 } |
| 367 | 398 |
| 368 void | 399 void |
| 369 HASH_Destroy(HASHContext *context) | 400 HASH_Destroy(HASHContext *context) |
| 370 { | 401 { |
| 371 (* context->hashobj->destroy)(context->hash_context, PR_TRUE); | 402 (*context->hashobj->destroy)(context->hash_context, PR_TRUE); |
| 372 PORT_Free(context); | 403 PORT_Free(context); |
| 373 return; | 404 return; |
| 374 } | 405 } |
| 375 | 406 |
| 376 | |
| 377 void | 407 void |
| 378 HASH_Begin(HASHContext *context) | 408 HASH_Begin(HASHContext *context) |
| 379 { | 409 { |
| 380 (* context->hashobj->begin)(context->hash_context); | 410 (*context->hashobj->begin)(context->hash_context); |
| 381 return; | 411 return; |
| 382 } | 412 } |
| 383 | 413 |
| 384 | |
| 385 void | 414 void |
| 386 HASH_Update(HASHContext *context, | 415 HASH_Update(HASHContext *context, |
| 387 » const unsigned char *src, | 416 const unsigned char *src, |
| 388 » unsigned int len) | 417 unsigned int len) |
| 389 { | 418 { |
| 390 (* context->hashobj->update)(context->hash_context, src, len); | 419 (*context->hashobj->update)(context->hash_context, src, len); |
| 391 return; | 420 return; |
| 392 } | 421 } |
| 393 | 422 |
| 394 void | 423 void |
| 395 HASH_End(HASHContext *context, | 424 HASH_End(HASHContext *context, |
| 396 » unsigned char *result, | 425 unsigned char *result, |
| 397 » unsigned int *result_len, | 426 unsigned int *result_len, |
| 398 » unsigned int max_result_len) | 427 unsigned int max_result_len) |
| 399 { | 428 { |
| 400 (* context->hashobj->end)(context->hash_context, result, result_len, | 429 (*context->hashobj->end)(context->hash_context, result, result_len, |
| 401 » » » max_result_len); | 430 max_result_len); |
| 402 return; | 431 return; |
| 403 } | 432 } |
| 404 | 433 |
| 405 HASH_HashType | 434 HASH_HashType |
| 406 HASH_GetType(HASHContext *context) | 435 HASH_GetType(HASHContext *context) |
| 407 { | 436 { |
| 408 return(context->hashobj->type); | 437 return (context->hashobj->type); |
| 409 } | 438 } |
| OLD | NEW |