From: Sami Kerola <kerolasa@iki.fi>
To: util-linux@vger.kernel.org
Cc: Sami Kerola <kerolasa@iki.fi>
Subject: [PATCH] libuuid: use kernel crypto api
Date: Sat, 4 Aug 2018 20:17:06 +0100 [thread overview]
Message-ID: <20180804191706.10641-1-kerolasa@iki.fi> (raw)
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@iki.fi>
---
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
next reply other threads:[~2018-08-04 21:18 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-08-04 19:17 Sami Kerola [this message]
2018-08-04 19:27 ` [PATCH] libuuid: use kernel crypto api Dmitry V. Levin
2018-08-04 19:46 ` Theodore Y. Ts'o
2018-08-05 10:42 ` Sami Kerola
2018-08-05 23:41 ` Peter Cordes
2018-08-06 6:56 ` Karel Zak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180804191706.10641-1-kerolasa@iki.fi \
--to=kerolasa@iki.fi \
--cc=util-linux@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox