util-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] libuuid: use kernel crypto api
@ 2018-08-04 19:17 Sami Kerola
  2018-08-04 19:27 ` Dmitry V. Levin
  2018-08-04 19:46 ` Theodore Y. Ts'o
  0 siblings, 2 replies; 6+ messages in thread
From: Sami Kerola @ 2018-08-04 19:17 UTC (permalink / raw)
  To: util-linux; +Cc: Sami Kerola

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


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-08-06  9:04 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-08-04 19:17 [PATCH] libuuid: use kernel crypto api Sami Kerola
2018-08-04 19:27 ` 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).