summaryrefslogtreecommitdiff
path: root/common/lib/libc/string
diff options
context:
space:
mode:
authordrochner <drochner@NetBSD.org>2012-08-30 12:16:48 +0000
committerdrochner <drochner@NetBSD.org>2012-08-30 12:16:48 +0000
commit23bb9c3d2a6b4e05b1262696420e976fc6b15cc6 (patch)
tree25b7e2933fe8ca6f1d0a4afbeb776da833de014e /common/lib/libc/string
parent3ac5dbc4e542b7cd06bcb2ec8788fdd376152196 (diff)
Add "consttime_bcmp" and "explicit_bzero" functions for both kernel
abd userland, as proposed on tech-security, with explicit_bzero using a volatile function pointer as suggested by Alan Barrett. Both do what the name says. For userland, both are prefixed by "__" to keep them out of the user namespace. Change some memset/memcmp uses to the new functions where it makes sense -- these are just some examples, more to come.
Diffstat (limited to 'common/lib/libc/string')
-rw-r--r--common/lib/libc/string/consttime_bcmp.c19
-rw-r--r--common/lib/libc/string/explicit_bzero.c22
2 files changed, 41 insertions, 0 deletions
diff --git a/common/lib/libc/string/consttime_bcmp.c b/common/lib/libc/string/consttime_bcmp.c
new file mode 100644
index 00000000000..e6c0b31c17e
--- /dev/null
+++ b/common/lib/libc/string/consttime_bcmp.c
@@ -0,0 +1,19 @@
+/* $NetBSD: consttime_bcmp.c,v 1.1 2012/08/30 12:16:49 drochner Exp $ */
+
+#if !defined(_KERNEL) && !defined(_STANDALONE)
+#include <string.h>
+#define consttime_bcmp __consttime_bcmp
+#else
+#include <lib/libkern/libkern.h>
+#endif
+
+int
+consttime_bcmp(const void *b1, const void *b2, size_t len)
+{
+ const char *c1 = b1, *c2 = b2;
+ int res = 0;
+
+ while (len --)
+ res |= *c1++ ^ *c2++;
+ return res;
+}
diff --git a/common/lib/libc/string/explicit_bzero.c b/common/lib/libc/string/explicit_bzero.c
new file mode 100644
index 00000000000..c34410cfdde
--- /dev/null
+++ b/common/lib/libc/string/explicit_bzero.c
@@ -0,0 +1,22 @@
+/* $NetBSD: explicit_bzero.c,v 1.1 2012/08/30 12:16:49 drochner Exp $ */
+
+#if !defined(_KERNEL) && !defined(_STANDALONE)
+#include <string.h>
+#define explicit_bzero __explicit_bzero
+#define explicit_memset_impl __explicit_memset_impl
+#else
+#include <lib/libkern/libkern.h>
+#endif
+
+/*
+ * The use of a volatile pointer guarantees that the compiler
+ * will not optimise the call away.
+ */
+void *(* volatile explicit_memset_impl)(void *, int, size_t) = memset;
+
+void
+explicit_bzero(void *b, size_t len)
+{
+
+ (*explicit_memset_impl)(b, 0, len);
+}