[Mesa-dev] [PATCH] mesa: Add mesa SHA-1 functions
Carl Worth
cworth at cworth.org
Thu Dec 11 13:51:20 PST 2014
From: Kristian Høgsberg <krh at bitplanet.net>
The upcoming shader cache uses the SHA-1 algorithm for cryptographic
naming. These new mesa_sha1 functions are implemented with the nettle
library.
---
This patch is another in support of my upcoming shader-cache work. Thanks to
Kritian for coding this piece.
As currently written, this patch introduces a new dependency of Mesa on the
Nettle library to implement SHA-1. I'm open to recommendations if people would prefer some other option.
For example, the xserver can be configured to get a SHA-1 implementation from
libmd, libc, CommonCrypto, CryptoAPI, libnettle, libgcrypt, libsha1, or
openssl.
I don't know if it's important to offer as many options as that, which is why
I'm asking for opinions here.
Thanks,
-Carl
configure.ac | 7 ++++
src/util/Makefile.am | 2 ++
src/util/Makefile.sources | 2 ++
src/util/sha1.c | 81 +++++++++++++++++++++++++++++++++++++++++++++++
src/util/sha1.h | 22 +++++++++++++
5 files changed, 114 insertions(+)
create mode 100644 src/util/sha1.c
create mode 100644 src/util/sha1.h
diff --git a/configure.ac b/configure.ac
index 1d9d015..bbefd21 100644
--- a/configure.ac
+++ b/configure.ac
@@ -876,6 +876,13 @@ fi
AC_SUBST([MESA_LLVM])
+# Require libnettle for SHA-1 calculations
+PKG_CHECK_MODULES([NETTLE], [nettle],
+ [have_nettle=yes], [have_nettle=no])
+if test "x$have_nettle" = xno; then
+ AC_MSG_ERROR([Cannot find required nettle library (or nettle-dev package)])
+fi
+
# Check for libdrm
PKG_CHECK_MODULES([LIBDRM], [libdrm >= $LIBDRM_REQUIRED],
[have_libdrm=yes], [have_libdrm=no])
diff --git a/src/util/Makefile.am b/src/util/Makefile.am
index 8d5f90e..30899de 100644
--- a/src/util/Makefile.am
+++ b/src/util/Makefile.am
@@ -37,6 +37,8 @@ libmesautil_la_SOURCES = \
$(MESA_UTIL_FILES) \
$(MESA_UTIL_GENERATED_FILES)
+libmesautil_la_LIBADD = -lnettle
+
BUILT_SOURCES = $(MESA_UTIL_GENERATED_FILES)
CLEANFILES = $(BUILT_SOURCES)
diff --git a/src/util/Makefile.sources b/src/util/Makefile.sources
index 9e27424..22c6a22 100644
--- a/src/util/Makefile.sources
+++ b/src/util/Makefile.sources
@@ -4,6 +4,8 @@ MESA_UTIL_FILES := \
register_allocate.c \
register_allocate.h \
rgtc.c \
+ sha1.c \
+ sha1.h \
strtod.cpp
MESA_UTIL_GENERATED_FILES = \
diff --git a/src/util/sha1.c b/src/util/sha1.c
new file mode 100644
index 0000000..3025df6
--- /dev/null
+++ b/src/util/sha1.c
@@ -0,0 +1,81 @@
+/*
+ * Copyright © 2014 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+
+#include <stdlib.h>
+#include <nettle/sha.h>
+
+#include "sha1.h"
+
+struct mesa_sha1 *
+_mesa_sha1_init(void)
+{
+ struct sha1_ctx *ctx = malloc(sizeof(*ctx));
+
+ if (!ctx)
+ return NULL;
+ sha1_init(ctx);
+
+ return (struct mesa_sha1 *) ctx;
+}
+
+int
+_mesa_sha1_update(struct mesa_sha1 *ctx, const void *data, int size)
+{
+ sha1_update((struct sha1_ctx *) ctx, size, data);
+
+ return 1;
+}
+
+int
+_mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20])
+{
+ sha1_digest((struct sha1_ctx *) ctx, 20, result);
+ free(ctx);
+
+ return 1;
+}
+
+void
+_mesa_sha1_compute(const void *data, size_t size, unsigned char result[20])
+{
+ struct sha1_ctx ctx;
+
+ sha1_init(&ctx);
+ sha1_update(&ctx, size, data);
+ sha1_digest(&ctx, 20, result);
+}
+
+char *
+_mesa_sha1_format(char *buf, const unsigned char *sha1)
+{
+ static const char hex_digits[] = "0123456789abcdef";
+ int i;
+
+ for (i = 0; i < 40; i += 2) {
+ buf[i] = hex_digits[sha1[i >> 1] >> 4];
+ buf[i + 1] = hex_digits[sha1[i >> 1] & 0x0f];
+ }
+ buf[i] = '\0';
+
+ return buf;
+}
diff --git a/src/util/sha1.h b/src/util/sha1.h
new file mode 100644
index 0000000..1616134
--- /dev/null
+++ b/src/util/sha1.h
@@ -0,0 +1,22 @@
+#ifndef SHA1_H
+#define SHA1_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdlib.h>
+
+struct mesa_sha1;
+
+struct mesa_sha1 *_mesa_sha1_init(void);
+int _mesa_sha1_update(struct mesa_sha1 *ctx, const void *data, int size);
+int _mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20]);
+char *_mesa_sha1_format(char *buf, const unsigned char *sha1);
+void _mesa_sha1_compute(const void *data, size_t size, unsigned char result[20]);
+
+#ifdef __cplusplus
+} /* extern C */
+#endif
+
+#endif
--
2.1.1
More information about the mesa-dev
mailing list