andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: #include "sechash.h" andre@0: #include "secoidt.h" andre@0: #include "secerr.h" andre@0: #include "blapi.h" andre@0: #include "pk11func.h" /* for the PK11_ calls below. */ andre@0: andre@0: static void * andre@0: null_hash_new_context(void) andre@0: { andre@0: return NULL; andre@0: } andre@0: andre@0: static void * andre@0: null_hash_clone_context(void *v) andre@0: { andre@0: PORT_Assert(v == NULL); andre@0: return NULL; andre@0: } andre@0: andre@0: static void andre@0: null_hash_begin(void *v) andre@0: { andre@0: } andre@0: andre@0: static void andre@0: null_hash_update(void *v, const unsigned char *input, unsigned int length) andre@0: { andre@0: } andre@0: andre@0: static void andre@0: null_hash_end(void *v, unsigned char *output, unsigned int *outLen, andre@0: unsigned int maxOut) andre@0: { andre@0: *outLen = 0; andre@0: } andre@0: andre@0: static void andre@0: null_hash_destroy_context(void *v, PRBool b) andre@0: { andre@0: PORT_Assert(v == NULL); andre@0: } andre@0: andre@0: andre@0: static void * andre@0: md2_NewContext(void) { andre@0: return (void *) PK11_CreateDigestContext(SEC_OID_MD2); andre@0: } andre@0: andre@0: static void * andre@0: md5_NewContext(void) { andre@0: return (void *) PK11_CreateDigestContext(SEC_OID_MD5); andre@0: } andre@0: andre@0: static void * andre@0: sha1_NewContext(void) { andre@0: return (void *) PK11_CreateDigestContext(SEC_OID_SHA1); andre@0: } andre@0: andre@0: static void * andre@0: sha224_NewContext(void) { andre@0: return (void *) PK11_CreateDigestContext(SEC_OID_SHA224); andre@0: } andre@0: andre@0: static void * andre@0: sha256_NewContext(void) { andre@0: return (void *) PK11_CreateDigestContext(SEC_OID_SHA256); andre@0: } andre@0: andre@0: static void * andre@0: sha384_NewContext(void) { andre@0: return (void *) PK11_CreateDigestContext(SEC_OID_SHA384); andre@0: } andre@0: andre@0: static void * andre@0: sha512_NewContext(void) { andre@0: return (void *) PK11_CreateDigestContext(SEC_OID_SHA512); andre@0: } andre@0: andre@0: const SECHashObject SECHashObjects[] = { andre@0: { 0, andre@0: (void * (*)(void)) null_hash_new_context, andre@0: (void * (*)(void *)) null_hash_clone_context, andre@0: (void (*)(void *, PRBool)) null_hash_destroy_context, andre@0: (void (*)(void *)) null_hash_begin, andre@0: (void (*)(void *, const unsigned char *, unsigned int)) null_hash_update, andre@0: (void (*)(void *, unsigned char *, unsigned int *, andre@0: unsigned int)) null_hash_end, andre@0: 0, andre@0: HASH_AlgNULL andre@0: }, andre@0: { MD2_LENGTH, andre@0: (void * (*)(void)) md2_NewContext, andre@0: (void * (*)(void *)) PK11_CloneContext, andre@0: (void (*)(void *, PRBool)) PK11_DestroyContext, andre@0: (void (*)(void *)) PK11_DigestBegin, andre@0: (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp, andre@0: (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) andre@0: PK11_DigestFinal, andre@0: MD2_BLOCK_LENGTH, andre@0: HASH_AlgMD2 andre@0: }, andre@0: { MD5_LENGTH, andre@0: (void * (*)(void)) md5_NewContext, andre@0: (void * (*)(void *)) PK11_CloneContext, andre@0: (void (*)(void *, PRBool)) PK11_DestroyContext, andre@0: (void (*)(void *)) PK11_DigestBegin, andre@0: (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp, andre@0: (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) andre@0: PK11_DigestFinal, andre@0: MD5_BLOCK_LENGTH, andre@0: HASH_AlgMD5 andre@0: }, andre@0: { SHA1_LENGTH, andre@0: (void * (*)(void)) sha1_NewContext, andre@0: (void * (*)(void *)) PK11_CloneContext, andre@0: (void (*)(void *, PRBool)) PK11_DestroyContext, andre@0: (void (*)(void *)) PK11_DigestBegin, andre@0: (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp, andre@0: (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) andre@0: PK11_DigestFinal, andre@0: SHA1_BLOCK_LENGTH, andre@0: HASH_AlgSHA1 andre@0: }, andre@0: { SHA256_LENGTH, andre@0: (void * (*)(void)) sha256_NewContext, andre@0: (void * (*)(void *)) PK11_CloneContext, andre@0: (void (*)(void *, PRBool)) PK11_DestroyContext, andre@0: (void (*)(void *)) PK11_DigestBegin, andre@0: (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp, andre@0: (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) andre@0: PK11_DigestFinal, andre@0: SHA256_BLOCK_LENGTH, andre@0: HASH_AlgSHA256 andre@0: }, andre@0: { SHA384_LENGTH, andre@0: (void * (*)(void)) sha384_NewContext, andre@0: (void * (*)(void *)) PK11_CloneContext, andre@0: (void (*)(void *, PRBool)) PK11_DestroyContext, andre@0: (void (*)(void *)) PK11_DigestBegin, andre@0: (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp, andre@0: (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) andre@0: PK11_DigestFinal, andre@0: SHA384_BLOCK_LENGTH, andre@0: HASH_AlgSHA384 andre@0: }, andre@0: { SHA512_LENGTH, andre@0: (void * (*)(void)) sha512_NewContext, andre@0: (void * (*)(void *)) PK11_CloneContext, andre@0: (void (*)(void *, PRBool)) PK11_DestroyContext, andre@0: (void (*)(void *)) PK11_DigestBegin, andre@0: (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp, andre@0: (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) andre@0: PK11_DigestFinal, andre@0: SHA512_BLOCK_LENGTH, andre@0: HASH_AlgSHA512 andre@0: }, andre@0: { SHA224_LENGTH, andre@0: (void * (*)(void)) sha224_NewContext, andre@0: (void * (*)(void *)) PK11_CloneContext, andre@0: (void (*)(void *, PRBool)) PK11_DestroyContext, andre@0: (void (*)(void *)) PK11_DigestBegin, andre@0: (void (*)(void *, const unsigned char *, unsigned int)) PK11_DigestOp, andre@0: (void (*)(void *, unsigned char *, unsigned int *, unsigned int)) andre@0: PK11_DigestFinal, andre@0: SHA224_BLOCK_LENGTH, andre@0: HASH_AlgSHA224 andre@0: }, andre@0: }; andre@0: andre@0: const SECHashObject * andre@0: HASH_GetHashObject(HASH_HashType type) andre@0: { andre@0: return &SECHashObjects[type]; andre@0: } andre@0: andre@0: HASH_HashType andre@0: HASH_GetHashTypeByOidTag(SECOidTag hashOid) andre@0: { andre@0: HASH_HashType ht = HASH_AlgNULL; andre@0: andre@0: switch(hashOid) { andre@0: case SEC_OID_MD2: ht = HASH_AlgMD2; break; andre@0: case SEC_OID_MD5: ht = HASH_AlgMD5; break; andre@0: case SEC_OID_SHA1: ht = HASH_AlgSHA1; break; andre@0: case SEC_OID_SHA224: ht = HASH_AlgSHA224; break; andre@0: case SEC_OID_SHA256: ht = HASH_AlgSHA256; break; andre@0: case SEC_OID_SHA384: ht = HASH_AlgSHA384; break; andre@0: case SEC_OID_SHA512: ht = HASH_AlgSHA512; break; andre@0: default: ht = HASH_AlgNULL; andre@0: PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); andre@0: break; andre@0: } andre@0: return ht; andre@0: } andre@0: andre@0: SECOidTag andre@0: HASH_GetHashOidTagByHMACOidTag(SECOidTag hmacOid) andre@0: { andre@0: SECOidTag hashOid = SEC_OID_UNKNOWN; andre@0: andre@0: switch(hmacOid) { andre@0: /* no oid exists for HMAC_MD2 */ andre@0: /* NSS does not define a oid for HMAC_MD4 */ andre@0: case SEC_OID_HMAC_SHA1: hashOid = SEC_OID_SHA1; break; andre@0: case SEC_OID_HMAC_SHA224: hashOid = SEC_OID_SHA224; break; andre@0: case SEC_OID_HMAC_SHA256: hashOid = SEC_OID_SHA256; break; andre@0: case SEC_OID_HMAC_SHA384: hashOid = SEC_OID_SHA384; break; andre@0: case SEC_OID_HMAC_SHA512: hashOid = SEC_OID_SHA512; break; andre@0: default: hashOid = SEC_OID_UNKNOWN; andre@0: PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); andre@0: break; andre@0: } andre@0: return hashOid; andre@0: } andre@0: andre@0: SECOidTag andre@0: HASH_GetHMACOidTagByHashOidTag(SECOidTag hashOid) andre@0: { andre@0: SECOidTag hmacOid = SEC_OID_UNKNOWN; andre@0: andre@0: switch(hashOid) { andre@0: /* no oid exists for HMAC_MD2 */ andre@0: /* NSS does not define a oid for HMAC_MD4 */ andre@0: case SEC_OID_SHA1: hmacOid = SEC_OID_HMAC_SHA1; break; andre@0: case SEC_OID_SHA224: hmacOid = SEC_OID_HMAC_SHA224; break; andre@0: case SEC_OID_SHA256: hmacOid = SEC_OID_HMAC_SHA256; break; andre@0: case SEC_OID_SHA384: hmacOid = SEC_OID_HMAC_SHA384; break; andre@0: case SEC_OID_SHA512: hmacOid = SEC_OID_HMAC_SHA512; break; andre@0: default: hmacOid = SEC_OID_UNKNOWN; andre@0: PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); andre@0: break; andre@0: } andre@0: return hmacOid; andre@0: } andre@0: andre@0: const SECHashObject * andre@0: HASH_GetHashObjectByOidTag(SECOidTag hashOid) andre@0: { andre@0: HASH_HashType ht = HASH_GetHashTypeByOidTag(hashOid); andre@0: andre@0: return (ht == HASH_AlgNULL) ? NULL : &SECHashObjects[ht]; andre@0: } andre@0: andre@0: /* returns zero for unknown hash OID */ andre@0: unsigned int andre@0: HASH_ResultLenByOidTag(SECOidTag hashOid) andre@0: { andre@0: const SECHashObject * hashObject = HASH_GetHashObjectByOidTag(hashOid); andre@0: unsigned int resultLen = 0; andre@0: andre@0: if (hashObject) andre@0: resultLen = hashObject->length; andre@0: return resultLen; andre@0: } andre@0: andre@0: /* returns zero if hash type invalid. */ andre@0: unsigned int andre@0: HASH_ResultLen(HASH_HashType type) andre@0: { andre@0: if ( ( type < HASH_AlgNULL ) || ( type >= HASH_AlgTOTAL ) ) { andre@0: PORT_SetError(SEC_ERROR_INVALID_ALGORITHM); andre@0: return(0); andre@0: } andre@0: andre@0: return(SECHashObjects[type].length); andre@0: } andre@0: andre@0: unsigned int andre@0: HASH_ResultLenContext(HASHContext *context) andre@0: { andre@0: return(context->hashobj->length); andre@0: } andre@0: andre@0: andre@0: andre@0: SECStatus andre@0: HASH_HashBuf(HASH_HashType type, andre@0: unsigned char *dest, andre@0: const unsigned char *src, andre@0: PRUint32 src_len) andre@0: { andre@0: HASHContext *cx; andre@0: unsigned int part; andre@0: andre@0: if ( ( type < HASH_AlgNULL ) || ( type >= HASH_AlgTOTAL ) ) { andre@0: return(SECFailure); andre@0: } andre@0: andre@0: cx = HASH_Create(type); andre@0: if ( cx == NULL ) { andre@0: return(SECFailure); andre@0: } andre@0: HASH_Begin(cx); andre@0: HASH_Update(cx, src, src_len); andre@0: HASH_End(cx, dest, &part, HASH_ResultLenContext(cx)); andre@0: HASH_Destroy(cx); andre@0: andre@0: return(SECSuccess); andre@0: } andre@0: andre@0: HASHContext * andre@0: HASH_Create(HASH_HashType type) andre@0: { andre@0: void *hash_context = NULL; andre@0: HASHContext *ret = NULL; andre@0: andre@0: if ( ( type < HASH_AlgNULL ) || ( type >= HASH_AlgTOTAL ) ) { andre@0: return(NULL); andre@0: } andre@0: andre@0: hash_context = (* SECHashObjects[type].create)(); andre@0: if ( hash_context == NULL ) { andre@0: goto loser; andre@0: } andre@0: andre@0: ret = (HASHContext *)PORT_Alloc(sizeof(HASHContext)); andre@0: if ( ret == NULL ) { andre@0: goto loser; andre@0: } andre@0: andre@0: ret->hash_context = hash_context; andre@0: ret->hashobj = &SECHashObjects[type]; andre@0: andre@0: return(ret); andre@0: andre@0: loser: andre@0: if ( hash_context != NULL ) { andre@0: (* SECHashObjects[type].destroy)(hash_context, PR_TRUE); andre@0: } andre@0: andre@0: return(NULL); andre@0: } andre@0: andre@0: andre@0: HASHContext * andre@0: HASH_Clone(HASHContext *context) andre@0: { andre@0: void *hash_context = NULL; andre@0: HASHContext *ret = NULL; andre@0: andre@0: hash_context = (* context->hashobj->clone)(context->hash_context); andre@0: if ( hash_context == NULL ) { andre@0: goto loser; andre@0: } andre@0: andre@0: ret = (HASHContext *)PORT_Alloc(sizeof(HASHContext)); andre@0: if ( ret == NULL ) { andre@0: goto loser; andre@0: } andre@0: andre@0: ret->hash_context = hash_context; andre@0: ret->hashobj = context->hashobj; andre@0: andre@0: return(ret); andre@0: andre@0: loser: andre@0: if ( hash_context != NULL ) { andre@0: (* context->hashobj->destroy)(hash_context, PR_TRUE); andre@0: } andre@0: andre@0: return(NULL); andre@0: andre@0: } andre@0: andre@0: void andre@0: HASH_Destroy(HASHContext *context) andre@0: { andre@0: (* context->hashobj->destroy)(context->hash_context, PR_TRUE); andre@0: PORT_Free(context); andre@0: return; andre@0: } andre@0: andre@0: andre@0: void andre@0: HASH_Begin(HASHContext *context) andre@0: { andre@0: (* context->hashobj->begin)(context->hash_context); andre@0: return; andre@0: } andre@0: andre@0: andre@0: void andre@0: HASH_Update(HASHContext *context, andre@0: const unsigned char *src, andre@0: unsigned int len) andre@0: { andre@0: (* context->hashobj->update)(context->hash_context, src, len); andre@0: return; andre@0: } andre@0: andre@0: void andre@0: HASH_End(HASHContext *context, andre@0: unsigned char *result, andre@0: unsigned int *result_len, andre@0: unsigned int max_result_len) andre@0: { andre@0: (* context->hashobj->end)(context->hash_context, result, result_len, andre@0: max_result_len); andre@0: return; andre@0: } andre@0: andre@0: HASH_HashType andre@0: HASH_GetType(HASHContext *context) andre@0: { andre@0: return(context->hashobj->type); andre@0: }