diff options
author | Dr. Stephen Henson <steve@openssl.org> | 2004-02-01 13:39:51 +0000 |
---|---|---|
committer | Dr. Stephen Henson <steve@openssl.org> | 2004-02-01 13:39:51 +0000 |
commit | d4575825f1ded552a32c70f8d5e62668978eda91 (patch) | |
tree | 950ce37269e253f8a3149a4ee0d3a35055407b09 /crypto/evp | |
parent | d04b1b46562880b97468b0fa519b36c3be770072 (diff) |
Add flag to avoid continuous
memory allocate when calling EVP_MD_CTX_copy_ex().
Without this HMAC is several times slower than
< 0.9.7.
Diffstat (limited to 'crypto/evp')
-rw-r--r-- | crypto/evp/digest.c | 15 | ||||
-rw-r--r-- | crypto/evp/evp.h | 2 |
2 files changed, 14 insertions, 3 deletions
diff --git a/crypto/evp/digest.c b/crypto/evp/digest.c index b22eed4421..0623ddf1f0 100644 --- a/crypto/evp/digest.c +++ b/crypto/evp/digest.c @@ -248,6 +248,7 @@ int EVP_MD_CTX_copy(EVP_MD_CTX *out, const EVP_MD_CTX *in) int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) { + unsigned char *tmp_buf; if ((in == NULL) || (in->digest == NULL)) { EVPerr(EVP_F_EVP_MD_CTX_COPY,EVP_R_INPUT_NOT_INITIALIZED); @@ -262,15 +263,22 @@ int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out, const EVP_MD_CTX *in) } #endif + if (out->digest == in->digest) + { + tmp_buf = out->md_data; + EVP_MD_CTX_set_flags(out,EVP_MD_CTX_FLAG_REUSE); + } + else tmp_buf = NULL; EVP_MD_CTX_cleanup(out); memcpy(out,in,sizeof *out); if (out->digest->ctx_size) { - out->md_data=OPENSSL_malloc(out->digest->ctx_size); + if (tmp_buf) out->md_data = tmp_buf; + else out->md_data=OPENSSL_malloc(out->digest->ctx_size); memcpy(out->md_data,in->md_data,out->digest->ctx_size); } - + if (out->digest->copy) return out->digest->copy(out,in); @@ -308,7 +316,8 @@ int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) if (ctx->digest && ctx->digest->cleanup && !EVP_MD_CTX_test_flags(ctx,EVP_MD_CTX_FLAG_CLEANED)) ctx->digest->cleanup(ctx); - if (ctx->digest && ctx->digest->ctx_size && ctx->md_data) + if (ctx->digest && ctx->digest->ctx_size && ctx->md_data + && !EVP_MD_CTX_test_flags(ctx, EVP_MD_CTX_FLAG_REUSE)) { OPENSSL_cleanse(ctx->md_data,ctx->digest->ctx_size); OPENSSL_free(ctx->md_data); diff --git a/crypto/evp/evp.h b/crypto/evp/evp.h index a5f3c449c9..dd2d186a56 100644 --- a/crypto/evp/evp.h +++ b/crypto/evp/evp.h @@ -288,6 +288,8 @@ struct env_md_ctx_st * once only */ #define EVP_MD_CTX_FLAG_CLEANED 0x0002 /* context has already been * cleaned */ +#define EVP_MD_CTX_FLAG_REUSE 0x0004 /* Don't free up ctx->md_data + * in EVP_MD_CTX_cleanup */ struct evp_cipher_st { |