Try to speed up md5 checksums by using hardware accelerators when they are present with use of kernel crypto api. Reference: http://www.chronox.de/libkcapi/html/ch01s02.html Signed-off-by: Sami Kerola <kerolasa@xxxxxx> --- libuuid/src/gen_uuid.c | 51 +++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/libuuid/src/gen_uuid.c b/libuuid/src/gen_uuid.c index a374e75c9..a1867460e 100644 --- a/libuuid/src/gen_uuid.c +++ b/libuuid/src/gen_uuid.c @@ -80,6 +80,8 @@ #if defined(__linux__) && defined(HAVE_SYS_SYSCALL_H) #include <sys/syscall.h> #endif +#include <linux/if_alg.h> +#include <sys/uio.h> #include "all-io.h" #include "uuidP.h" @@ -564,17 +566,50 @@ void uuid_generate(uuid_t out) */ void uuid_generate_md5(uuid_t out, const uuid_t ns, const char *name, size_t len) { - UL_MD5_CTX ctx; - char hash[UL_MD5LENGTH]; + int tfmfd; + struct sockaddr_alg sa = { + .salg_family = AF_ALG, + .salg_type = "hash", + .salg_name = "md5" + }; + + tfmfd = socket(AF_ALG, SOCK_SEQPACKET, 0); + old_md5: + if (tfmfd != -1) { + int opfd = -1, fail = 0; + struct iovec const iov[2] = { + {.iov_base = (void *)ns, .iov_len = sizeof(uuid_t)}, + {.iov_base = (void *)name, .iov_len = len} + }; + + if (bind(tfmfd, (struct sockaddr *)&sa, sizeof(sa)) < 0) + fail = 1; + if (!fail && (opfd = accept(tfmfd, NULL, 0)) < 0) + fail = 1; + if (!fail && writev(opfd, iov, 2) < 0) + fail = 1; + if (!fail && read(opfd, out, sizeof(uuid_t)) == -1) + fail = 1; + if (opfd != -1) + close(opfd); + close(tfmfd); + if (fail) { + tfmfd = -1; + goto old_md5; + } + } else { + UL_MD5_CTX ctx; + char hash[UL_MD5LENGTH]; - ul_MD5Init(&ctx); - /* hash concatenation of well-known UUID with name */ - ul_MD5Update(&ctx, ns, sizeof(uuid_t)); - ul_MD5Update(&ctx, (const unsigned char *)name, len); + ul_MD5Init(&ctx); + /* hash concatenation of well-known UUID with name */ + ul_MD5Update(&ctx, ns, sizeof(uuid_t)); + ul_MD5Update(&ctx, (const unsigned char *)name, len); - ul_MD5Final((unsigned char *)hash, &ctx); + ul_MD5Final((unsigned char *)hash, &ctx); - memcpy(out, hash, sizeof(uuid_t)); + memcpy(out, hash, sizeof(uuid_t)); + } out[6] &= ~(UUID_TYPE_MASK << UUID_TYPE_SHIFT); out[6] |= (UUID_TYPE_DCE_MD5 << UUID_TYPE_SHIFT); -- 2.18.0 -- To unsubscribe from this list: send the line "unsubscribe util-linux" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html