diff options
| author | drochner <drochner@NetBSD.org> | 2012-08-30 12:16:48 +0000 |
|---|---|---|
| committer | drochner <drochner@NetBSD.org> | 2012-08-30 12:16:48 +0000 |
| commit | 23bb9c3d2a6b4e05b1262696420e976fc6b15cc6 (patch) | |
| tree | 25b7e2933fe8ca6f1d0a4afbeb776da833de014e /common/lib/libc/string | |
| parent | 3ac5dbc4e542b7cd06bcb2ec8788fdd376152196 (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.c | 19 | ||||
| -rw-r--r-- | common/lib/libc/string/explicit_bzero.c | 22 |
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); +} |
