Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sha256: add optional support for kernel crypto API #20

Merged
merged 1 commit into from
Feb 6, 2025

Conversation

WallaceIT
Copy link
Contributor

@WallaceIT WallaceIT commented Feb 5, 2025

Add the possibility to enable support for the Linux kernel crypto API, which allows to perform the SHA256 operations exploiting the facilities exposed by the kernel, including hardware accelerators.

@WallaceIT
Copy link
Contributor Author

On a Beagleplay (based on AM62 ARMv8 SoC), this lead to the following results:

Image size (bytes) Compression algorithm Write time with SW SHA256 (s) Write time with kcapi SHA256 (s) Write time without verification (s)
806473728 Zstandard 29.18s 21.15s 18.97s

Add the possibility to enable support for the Linux kernel crypto API,
which allows to perform the SHA256 operations exploiting the
facilities exposed by the kernel, including hardware accelerators.

Signed-off-by: Francesco Valla <[email protected]>
@WallaceIT WallaceIT force-pushed the feature/kernel-hasher branch from 2d960fb to 8ace8f8 Compare February 5, 2025 22:08
@embetrix
Copy link
Owner

embetrix commented Feb 6, 2025

@WallaceIT : this is a great enhancement, however I have some suggestions:

can we add a runtime check if the kernel support AF_ALG ?

https://stackoverflow.com/questions/36074001/detect-availability-of-linux-kernels-af-alg-sockets-for-userland-crypto

If not fallback to normal mode ( I think we can even enable it by default)

@embetrix embetrix added the enhancement New feature or request label Feb 6, 2025
@WallaceIT
Copy link
Contributor Author

Hi, the check is already taken care by the libkcapi library through the kcapi_md_init function, which will fail the initialization if the AF_ALG interface or the hasing algorithm is not supported.

The automatic fallback is already there too: if the initialization fails, the init function will nevertheless return 0 and subsequent functions will use the SW implmentation:

int sha256Init(SHA256Ctx& context) {
    if (!context.initialized) {
#ifdef USE_KERNEL_CRYPTO_API
        const char *hashname = "sha256";
        static bool warned = false;
        int ret;

        ret = kcapi_md_init(&context.handle, hashname, 0);
        if ((ret != 0) && !warned) {
            std::cerr << "Failed to init kernel crypto API: " << ret << std::endl;
            std::cerr << "Falling back to software hashing" << std::endl;
            warned = true;
        }
#endif
        context.initialized = true; // <- Here the context gets marked as initialized even in case of failure
    }

    return 0;
}

@embetrix embetrix merged commit a99fd82 into embetrix:master Feb 6, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants