--- src/trspi/crypto/openssl/hash.c +++ src/trspi/crypto/openssl/hash.c @@ -56,48 +56,20 @@ #define EVP_SUCCESS 1 TSS_RESULT Trspi_Hash(UINT32 HashType, UINT32 BufSize, BYTE* Buf, BYTE* Digest) { - EVP_MD_CTX *md_ctx; - unsigned int result_size; - int rv; - - md_ctx = EVP_MD_CTX_create(); + Trspi_HashCtx ctx; + TSS_RESULT rv; - switch (HashType) { - case TSS_HASH_SHA1: - rv = EVP_DigestInit(md_ctx, EVP_sha1()); - break; - default: - rv = TSPERR(TSS_E_BAD_PARAMETER); - goto out; - break; + rv = Trspi_HashInit(&ctx, HashType); + if (rv != TSS_SUCCESS) + return rv; + + rv = Trspi_HashUpdate(&ctx, BufSize, Buf); + if (rv != TSS_SUCCESS) { + EVP_MD_CTX_destroy(ctx.ctx); + return rv; } - - if (rv != EVP_SUCCESS) { - rv = TSPERR(TSS_E_INTERNAL_ERROR); - goto err; - } - - rv = EVP_DigestUpdate(md_ctx, Buf, BufSize); - if (rv != EVP_SUCCESS) { - rv = TSPERR(TSS_E_INTERNAL_ERROR); - goto err; - } - - result_size = EVP_MD_CTX_size(md_ctx); - rv = EVP_DigestFinal(md_ctx, Digest, &result_size); - if (rv != EVP_SUCCESS) { - rv = TSPERR(TSS_E_INTERNAL_ERROR); - goto err; - } else - rv = TSS_SUCCESS; - - goto out; - -err: - DEBUG_print_openssl_errors(); -out: - EVP_MD_CTX_destroy(md_ctx); - return rv; + rv = Trspi_HashFinal(&ctx, Digest); + return rv; } TSS_RESULT @@ -115,7 +87,8 @@ return TSPERR(TSS_E_BAD_PARAMETER); break; } - if ((ctx->ctx = EVP_MD_CTX_create()) == NULL) + ctx->ctx = EVP_MD_CTX_create(); + if (ctx->ctx == NULL) return TSPERR(TSS_E_OUTOFMEMORY); rv = EVP_DigestInit((EVP_MD_CTX *)ctx->ctx, (const EVP_MD *)md); @@ -145,7 +118,7 @@ return TSS_SUCCESS; rv = EVP_DigestUpdate(ctx->ctx, data, size); if (rv != EVP_SUCCESS) { DEBUG_print_openssl_errors(); - free(ctx->ctx); + EVP_MD_CTX_destroy(ctx->ctx); ctx->ctx = NULL; return TSPERR(TSS_E_INTERNAL_ERROR); } @@ -167,7 +140,7 @@ rv = EVP_DigestFinal(ctx->ctx, digest, & if (rv != EVP_SUCCESS) return TSPERR(TSS_E_INTERNAL_ERROR); - free(ctx->ctx); + EVP_MD_CTX_destroy(ctx->ctx); ctx->ctx = NULL; return TSS_SUCCESS;