Add flag to avoid continuous
authorDr. Stephen Henson <steve@openssl.org>
Sun, 1 Feb 2004 13:39:51 +0000 (13:39 +0000)
committerDr. Stephen Henson <steve@openssl.org>
Sun, 1 Feb 2004 13:39:51 +0000 (13:39 +0000)
memory allocate when calling EVP_MD_CTX_copy_ex().

Without this HMAC is several times slower than
< 0.9.7.

CHANGES
crypto/evp/digest.c
crypto/evp/evp.h

diff --git a/CHANGES b/CHANGES
index f3c304af7c9a40c494f591cee22ddca69fdb975a..5590487fd184953a525db6abb3939f393b95f0ef 100644 (file)
--- a/CHANGES
+++ b/CHANGES
 
  Changes between 0.9.7c and 0.9.7d  [xx XXX XXXX]
 
+  *) New md flag EVP_MD_CTX_FLAG_REUSE this allows md_data to be reused when
+     calling EVP_MD_CTX_copy_ex() to avoid calling OPENSSL_malloc(). Without
+     this HMAC (and other) operations are several times slower than OpenSSL
+     < 0.9.7.
+     [Steve Henson]
+
   *) Print out GeneralizedTime and UTCTime in ASN1_STRING_print_ex().
      [Peter Sylvester <Peter.Sylvester@EdelWeb.fr>]
 
index b22eed442115b544dd9b0348c3200e79b47a89f7..0623ddf1f05f87c7f6929839d0334e974475ed1f 100644 (file)
@@ -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);
index a5f3c449c9d60d2a3fa25f219946b6a8b8b24f80..dd2d186a563b3b23144ece5457bcfc6aa028a311 100644 (file)
@@ -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
        {