Skip to content

Performance difference between stack and heap allocated context #7219

Closed

Description

@davidebeatrici

From https://www.openssl.org/docs/man1.1.0/crypto/EVP_DigestInit.html:

EVP_MD_CTX became opaque in OpenSSL 1.1. Consequently, stack allocated EVP_MD_CTXs are no longer supported.

I created a simple benchmark tool containing the struct definition in order to measure the performance difference between stack and heap allocation for the context object in OpenSSL.

Here are the results on a Intel Core i5-4690:

--- OpenSSL hashing performance benchmark ---

Testing SHA1()...
1.065139 seconds for 10000000 loops.

Testing EVP_sha1() [stack]...
1.268479 seconds for 10000000 loops.

Testing EVP_sha1() [heap]...
1.834510 seconds for 10000000 loops.

Testing SHA256()...
1.885495 seconds for 10000000 loops.

Testing EVP_sha256() [stack]...
2.071068 seconds for 10000000 loops.

Testing EVP_sha256() [heap]...
2.666454 seconds for 10000000 loops.

Testing SHA512()...
2.733979 seconds for 10000000 loops.

Testing EVP_sha512() [stack]...
3.006316 seconds for 10000000 loops.

Testing EVP_sha512() [heap]...
3.710656 seconds for 10000000 loops.

I guess you decided to hide the struct definition in order to prevent issues in case its size changes, but is the performance difference worth it?

A viable solution would be to:

  • Provide a function which returns the size of the struct.
  • Provide a macro which defines the size of the struct, useful in case the compiler doesn't support variable-length arrays:
void hash(const EVP_MD *md, void *dst, const void *src, const size_t size)
{
	unsigned char ctx[EVP_MD_CTX_SIZE];
	unsigned int len = 0;

	if (EVP_DigestInit_ex((EVP_MD_CTX *) ctx, md, NULL) == false) {
		printf("EVP_DigestInit_ex() failed!\n");
		return;
	}

	if (EVP_DigestUpdate((EVP_MD_CTX *) ctx, src, size) == false) {
		printf("EVP_DigestUpdate() failed!\n");
		return;
	}

	if (EVP_DigestFinal_ex((EVP_MD_CTX *) ctx, dst, &len) == false) {
		printf("EVP_DigestFinal_ex() failed!\n");
		return;
	}
}

Activity

  1. richsalz commented on Sep 13, 2018

    @richsalz
    Contributor

    The problem is that a minor release can no longer change the size, it would force recompiling the code, as opposed to just dropping in a new library and havings things still work.

  2. davidebeatrici commented on Sep 13, 2018

    @davidebeatrici
    Author

    I think that it would still be useful to know the size of the struct at compile time, because:

    • In case of dynamic linking a developer can use it as reference to allocate more space so that there are more chances of compatibility with future OpenSSL versions.
    • In case of static linking there should be no issues regarding the size of the struct, as the library version remains the same.

    Also, regarding dynamic linking:

    • Linux: the application is usually installed from the distribution's repository, meaning that the package is guaranteed to work with the installed library.
    • Windows: shared OpenSSL libraries are usually provided by the application itself, meaning that the version shouldn't change (unless the user replaces the DLLs).
  3. mspncp commented on Sep 13, 2018

    @mspncp
    Contributor

    Making the structures opaque was a deliberate design choice for OpenSSL 1.1.0, which was released back in 25 Aug 2016. So this discussion comes too late IMHO.

  4. levitte commented on Sep 14, 2018

    @levitte
    Member

    @davidebeatrici, the point is that applications linked to a specific shared library version (which isn't necessarily the same as the product version, in the case OpenSSL) needs to be able to survive a library upgrade with the library version number retained. For example, the shared library version number for all OpenSSL 1.1.x is 1.1, making it possible to upgrade the OpenSSL libraries from OpenSSL 1.1.0x to OpenSSL 1.1.1, and the applications shouldn't even notice. Meanwhile, the internals of the OpenSSL libraries have changed... perhaps dramatically in some cases.

    This completely excludes having the structs in the open (that would force us not to touch them at all, which has been hell for us before 1.1.0), and completely excludes having macros telling the size (because a change there would force application recompile, something we do not want).

    Side note: the habit of having copies of DLLs tag along the application has been nicknamed "DLL hell", the reason being that Windows didn't promote a location for third party DLLs where it would look for them... this habit kinda goes against the "shared" part of shared libraries...

  5. davidebeatrici commented on Sep 14, 2018

    @davidebeatrici
    Author

    @davidebeatrici, the point is that applications linked to a specific shared library version (which isn't necessarily the same as the product version, in the case OpenSSL) needs to be able to survive a library upgrade with the library version number retained. For example, the shared library version number for all OpenSSL 1.1.x is 1.1, making it possible to upgrade the OpenSSL libraries from OpenSSL 1.1.0x to OpenSSL 1.1.1, and the applications shouldn't even notice. Meanwhile, the internals of the OpenSSL libraries have changed... perhaps dramatically in some cases.

    This completely excludes having the structs in the open (that would force us not to touch them at all, which has been hell for us before 1.1.0), and completely excludes having macros telling the size (because a change there would force application recompile, something we do not want).

    Thank you very much for the detailed explanation. I agree that it's better to "hide" the internal implementation to avoid misuses and the need to recompile applications as much as possible.

    The main reason I would like to allocate the context object on the stack is to create a function like the one I wrote in the first message, because it eliminates the need of rewriting the code for every algorithm.

    I suppose it can be added in OpenSSL as an internal function which can then be called normally:

    unsigned int EVP_Hash(const unsigned char *d, size_t n, unsigned char *md, const EVP_MD *evp_md) 
    {
        EVP_MD_CTX c;
        unsigned int len = 0;
    
        if (md == NULL || evp_md == NULL)
            return 0;
        if (!EVP_MD_CTX_init(&c))
            return 0;
        EVP_DigestUpdate(&c, d, n);
        EVP_DigestFinal_ex(&c, md, &len);
        OPENSSL_cleanse(&c, sizeof(c));
        return len;
    }

    Side note: the habit of having copies of DLLs tag along the application has been nicknamed "DLL hell", the reason being that Windows didn't promote a location for third party DLLs where it would look for them... this habit kinda goes against the "shared" part of shared libraries...

    I agree.

  6. levitte commented on Sep 14, 2018

    @levitte
    Member

    OBTW! That function already exists in OpenSSL, it's called EVP_digest.

  7. davidebeatrici commented on Sep 14, 2018

    @davidebeatrici
    Author

    It allocates the context object on the heap though:

    openssl/crypto/evp/digest.c

    Lines 271 to 287 in f09877c

    int EVP_Digest(const void *data, size_t count,
    unsigned char *md, unsigned int *size, const EVP_MD *type,
    ENGINE *impl)
    {
    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
    int ret;
    if (ctx == NULL)
    return 0;
    EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_ONESHOT);
    ret = EVP_DigestInit_ex(ctx, type, impl)
    && EVP_DigestUpdate(ctx, data, count)
    && EVP_DigestFinal_ex(ctx, md, size);
    EVP_MD_CTX_free(ctx);
    return ret;
    }

    Testing SHA1()...
    1.076245 seconds for 10000000 loops.
    
    Testing EVP_sha1() [EVP_Digest()]...
    1.874306 seconds for 10000000 loops.
    
  8. dot-asm commented on Oct 17, 2018

    @dot-asm
    Contributor

    Solution to the conundrum is

    EVP_MD_CTX *ctx = EVP_MD_CTX_new();
    EVP_MD_CTX_set_flags(ctx, EVP_MD_CTX_FLAG_REUSE);
    

    outside loop and then passing ctx to subroutines. What one observes is how much does 'malloc' cost [in terms of single-block hashing computational task]. I mean "SHA1()" is free from mallocs, "EVP_sha1() [stack]" performs one malloc and EVP_sha1() [heap]" - two mallocs. Reusable ctx mentioned above won't give same performance as legacy "single-shot" call, but at least it would be more adequate. There are no mallocs, but there still are overheads remaining. As facultative exercise one can compare differences with and without "reptoline".

  9. omartijn commented on Feb 11, 2019

    @omartijn

    I understand that it's convenient to be able to add fields to an internal struct while maintaining ABI compatibility. Wouldn't it be possible to just add some padding to the struct to make sure it can be grown without incurring incompatibility.

    This way, it would stay compatible between releases and variables can still be created on the stack. Forcing heap allocations on everyone is just a shame.

  10. t8m commented on Jul 19, 2021

    @t8m
    Member

    We are not going to make the structures public again. That is simply not going to happen. Reusing the same context for multiple operations should be already possible.

  11. added
    resolved: wont fix
    on Jul 19, 2021
  12. added a commit that references this issue on Feb 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    resolved: wont fix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions