Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src Add "consttime_bcmp" and "explicit_bzero" functions for both...



details:   https://anonhg.NetBSD.org/src/rev/92aca9d3bc91
branches:  trunk
changeset: 781282:92aca9d3bc91
user:      drochner <drochner%NetBSD.org@localhost>
date:      Thu Aug 30 12:16:48 2012 +0000

description:
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:

 common/lib/libc/string/consttime_bcmp.c |  19 +++++++++++++++++++
 common/lib/libc/string/explicit_bzero.c |  22 ++++++++++++++++++++++
 include/string.h                        |   4 +++-
 lib/libc/string/Makefile.inc            |   3 ++-
 lib/libcrypt/bcrypt.c                   |   6 +++---
 lib/libcrypt/crypt-sha1.c               |   6 +++---
 lib/libcrypt/md5crypt.c                 |   6 +++---
 sys/dev/cgd_crypto.c                    |  12 ++++++------
 sys/lib/libkern/Makefile.libkern        |   5 ++++-
 sys/lib/libkern/libkern.h               |   5 ++++-
 sys/netipsec/key.c                      |   8 ++++----
 sys/netipsec/xform_ah.c                 |   6 +++---
 sys/netipsec/xform_esp.c                |   6 +++---
 sys/opencrypto/cryptosoft.c             |  16 +++++++++-------
 14 files changed, 88 insertions(+), 36 deletions(-)

diffs (truncated from 397 to 300 lines):

diff -r eae9ba6d9b28 -r 92aca9d3bc91 common/lib/libc/string/consttime_bcmp.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/common/lib/libc/string/consttime_bcmp.c   Thu Aug 30 12:16:48 2012 +0000
@@ -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 -r eae9ba6d9b28 -r 92aca9d3bc91 common/lib/libc/string/explicit_bzero.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/common/lib/libc/string/explicit_bzero.c   Thu Aug 30 12:16:48 2012 +0000
@@ -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);
+}
diff -r eae9ba6d9b28 -r 92aca9d3bc91 include/string.h
--- a/include/string.h  Thu Aug 30 02:26:38 2012 +0000
+++ b/include/string.h  Thu Aug 30 12:16:48 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: string.h,v 1.40 2012/04/20 16:20:45 joerg Exp $        */
+/*     $NetBSD: string.h,v 1.41 2012/08/30 12:16:48 drochner Exp $     */
 
 /*-
  * Copyright (c) 1990, 1993
@@ -109,6 +109,8 @@
 char   *stresep(char **, const char *, int);
 char   *strndup(const char *, size_t);
 void   *memrchr(const void *, int, size_t);
+void   __explicit_bzero(void *, size_t);
+int    __consttime_bcmp(const void *, const void *, size_t);
 __END_DECLS
 #endif
 
diff -r eae9ba6d9b28 -r 92aca9d3bc91 lib/libc/string/Makefile.inc
--- a/lib/libc/string/Makefile.inc      Thu Aug 30 02:26:38 2012 +0000
+++ b/lib/libc/string/Makefile.inc      Thu Aug 30 12:16:48 2012 +0000
@@ -1,5 +1,5 @@
 #      from: @(#)Makefile.inc  8.1 (Berkeley) 6/4/93
-#      $NetBSD: Makefile.inc,v 1.75 2009/07/30 20:57:15 dsl Exp $
+#      $NetBSD: Makefile.inc,v 1.76 2012/08/30 12:16:48 drochner Exp $
 
 # string sources
 .PATH: ${ARCHDIR}/string ${.CURDIR}/string
@@ -19,6 +19,7 @@
 SRCS+= strcat.c strcmp.c strcpy.c strcspn.c strlen.c
 SRCS+= strncat.c strncmp.c strncpy.c strpbrk.c strsep.c
 SRCS+= strspn.c strstr.c swab.c
+SRCS+= explicit_bzero.c consttime_bcmp.c
 
 SRCS+= memccpy.c memcpy.c memmem.c memmove.c
 SRCS+= strchr.c strrchr.c
diff -r eae9ba6d9b28 -r 92aca9d3bc91 lib/libcrypt/bcrypt.c
--- a/lib/libcrypt/bcrypt.c     Thu Aug 30 02:26:38 2012 +0000
+++ b/lib/libcrypt/bcrypt.c     Thu Aug 30 12:16:48 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: bcrypt.c,v 1.16 2012/03/21 05:33:26 matt Exp $ */
+/*     $NetBSD: bcrypt.c,v 1.17 2012/08/30 12:16:49 drochner Exp $     */
 /*     $OpenBSD: bcrypt.c,v 1.16 2002/02/19 19:39:36 millert Exp $     */
 
 /*
@@ -46,7 +46,7 @@
  *
  */
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: bcrypt.c,v 1.16 2012/03/21 05:33:26 matt Exp $");
+__RCSID("$NetBSD: bcrypt.c,v 1.17 2012/08/30 12:16:49 drochner Exp $");
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -314,7 +314,7 @@
        encode_base64((u_int8_t *) encrypted + i + 3, csalt, BCRYPT_MAXSALT);
        encode_base64((u_int8_t *) encrypted + strlen(encrypted), ciphertext,
            4 * BCRYPT_BLOCKS - 1);
-       memset(&state, 0, sizeof(state));
+       __explicit_bzero(&state, sizeof(state));
        return encrypted;
 }
 
diff -r eae9ba6d9b28 -r 92aca9d3bc91 lib/libcrypt/crypt-sha1.c
--- a/lib/libcrypt/crypt-sha1.c Thu Aug 30 02:26:38 2012 +0000
+++ b/lib/libcrypt/crypt-sha1.c Thu Aug 30 12:16:48 2012 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: crypt-sha1.c,v 1.4 2011/05/09 19:15:28 drochner Exp $ */
+/* $NetBSD: crypt-sha1.c,v 1.5 2012/08/30 12:16:49 drochner Exp $ */
 
 /*
  * Copyright (c) 2004, Juniper Networks, Inc.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #if !defined(lint)
-__RCSID("$NetBSD: crypt-sha1.c,v 1.4 2011/05/09 19:15:28 drochner Exp $");
+__RCSID("$NetBSD: crypt-sha1.c,v 1.5 2012/08/30 12:16:49 drochner Exp $");
 #endif /* not lint */
 
 #include <stdlib.h>
@@ -190,7 +190,7 @@
     *ep = '\0';
 
     /* Don't leave anything around in vm they could use. */
-    memset(hmac_buf, 0, sizeof hmac_buf);
+    __explicit_bzero(hmac_buf, sizeof hmac_buf);
 
     return passwd;
 }      
diff -r eae9ba6d9b28 -r 92aca9d3bc91 lib/libcrypt/md5crypt.c
--- a/lib/libcrypt/md5crypt.c   Thu Aug 30 02:26:38 2012 +0000
+++ b/lib/libcrypt/md5crypt.c   Thu Aug 30 12:16:48 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: md5crypt.c,v 1.11 2011/11/29 17:27:10 drochner Exp $   */
+/*     $NetBSD: md5crypt.c,v 1.12 2012/08/30 12:16:49 drochner Exp $   */
 
 /*
  * ----------------------------------------------------------------------------
@@ -15,7 +15,7 @@
 
 #include <sys/cdefs.h>
 #if !defined(lint)
-__RCSID("$NetBSD: md5crypt.c,v 1.11 2011/11/29 17:27:10 drochner Exp $");
+__RCSID("$NetBSD: md5crypt.c,v 1.12 2012/08/30 12:16:49 drochner Exp $");
 #endif /* not lint */
 
 #include <unistd.h>
@@ -143,6 +143,6 @@
        *p = '\0';
 
        /* Don't leave anything around in vm they could use. */
-       memset(final, 0, sizeof(final));
+       __explicit_bzero(final, sizeof(final));
        return (passwd);
 }
diff -r eae9ba6d9b28 -r 92aca9d3bc91 sys/dev/cgd_crypto.c
--- a/sys/dev/cgd_crypto.c      Thu Aug 30 02:26:38 2012 +0000
+++ b/sys/dev/cgd_crypto.c      Thu Aug 30 12:16:48 2012 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: cgd_crypto.c,v 1.9 2008/04/28 20:23:46 martin Exp $ */
+/* $NetBSD: cgd_crypto.c,v 1.10 2012/08/30 12:16:48 drochner Exp $ */
 
 /*-
  * Copyright (c) 2002 The NetBSD Foundation, Inc.
@@ -37,7 +37,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: cgd_crypto.c,v 1.9 2008/04/28 20:23:46 martin Exp $");
+__KERNEL_RCSID(0, "$NetBSD: cgd_crypto.c,v 1.10 2012/08/30 12:16:48 drochner Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -195,7 +195,7 @@
 {
        struct aes_privdata *apd = data;
 
-       (void)memset(apd, 0, sizeof(*apd));
+       explicit_bzero(apd, sizeof(*apd));
        free(apd, M_DEVBUF);
 }
 
@@ -296,7 +296,7 @@
        error |= des_key_sched(block + 1, cp->cp_key2);
        error |= des_key_sched(block + 2, cp->cp_key3);
        if (error) {
-               (void)memset(cp, 0, sizeof(*cp));
+               explicit_bzero(cp, sizeof(*cp));
                free(cp, M_DEVBUF);
                return NULL;
        }
@@ -308,7 +308,7 @@
 {
        struct c3des_privdata *cp = data;
 
-       (void)memset(cp, 0, sizeof(*cp));
+       explicit_bzero(cp, sizeof(*cp));
        free(cp, M_DEVBUF);
 }
 
@@ -408,7 +408,7 @@
 {
        struct  bf_privdata *bp = data;
 
-       (void)memset(bp, 0, sizeof(*bp));
+       explicit_bzero(bp, sizeof(*bp));
        free(bp, M_DEVBUF);
 }
 
diff -r eae9ba6d9b28 -r 92aca9d3bc91 sys/lib/libkern/Makefile.libkern
--- a/sys/lib/libkern/Makefile.libkern  Thu Aug 30 02:26:38 2012 +0000
+++ b/sys/lib/libkern/Makefile.libkern  Thu Aug 30 12:16:48 2012 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: Makefile.libkern,v 1.17 2012/02/05 14:19:03 dholland Exp $
+#      $NetBSD: Makefile.libkern,v 1.18 2012/08/30 12:16:49 drochner Exp $
 
 # 
 # Variable definitions for libkern.  
@@ -92,6 +92,9 @@
 
 SRCS+= heapsort.c ptree.c rb.c
 
+# for crypto
+SRCS+= explicit_bzero.c consttime_bcmp.c
+
 # Files to clean up
 CLEANFILES+= lib${LIB}.o lib${LIB}.po
 
diff -r eae9ba6d9b28 -r 92aca9d3bc91 sys/lib/libkern/libkern.h
--- a/sys/lib/libkern/libkern.h Thu Aug 30 02:26:38 2012 +0000
+++ b/sys/lib/libkern/libkern.h Thu Aug 30 12:16:48 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: libkern.h,v 1.105 2012/01/22 02:57:36 rmind Exp $      */
+/*     $NetBSD: libkern.h,v 1.106 2012/08/30 12:16:49 drochner Exp $   */
 
 /*-
  * Copyright (c) 1992, 1993
@@ -345,4 +345,7 @@
 unsigned int   popcountll(unsigned long long) __constfunc;
 unsigned int   popcount32(uint32_t) __constfunc;
 unsigned int   popcount64(uint64_t) __constfunc;
+
+void   explicit_bzero(void *, size_t);
+int    consttime_bcmp(const void *, const void *, size_t);
 #endif /* !_LIB_LIBKERN_LIBKERN_H_ */
diff -r eae9ba6d9b28 -r 92aca9d3bc91 sys/netipsec/key.c
--- a/sys/netipsec/key.c        Thu Aug 30 02:26:38 2012 +0000
+++ b/sys/netipsec/key.c        Thu Aug 30 12:16:48 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: key.c,v 1.77 2012/08/29 20:37:50 drochner Exp $        */
+/*     $NetBSD: key.c,v 1.78 2012/08/30 12:16:49 drochner Exp $        */
 /*     $FreeBSD: src/sys/netipsec/key.c,v 1.3.2.3 2004/02/14 22:23:23 bms Exp $        */
 /*     $KAME: key.c,v 1.191 2001/06/27 10:46:49 sakane Exp $   */
        
@@ -32,7 +32,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: key.c,v 1.77 2012/08/29 20:37:50 drochner Exp $");
+__KERNEL_RCSID(0, "$NetBSD: key.c,v 1.78 2012/08/30 12:16:49 drochner Exp $");
 
 /*
  * This code is referd to RFC 2367
@@ -3040,9 +3040,9 @@
                sav->tdb_xform = NULL;
        } else {
                if (sav->key_auth != NULL)
-                       memset(_KEYBUF(sav->key_auth), 0, _KEYLEN(sav->key_auth));
+                       explicit_bzero(_KEYBUF(sav->key_auth), _KEYLEN(sav->key_auth));
                if (sav->key_enc != NULL)
-                       memset(_KEYBUF(sav->key_enc), 0, _KEYLEN(sav->key_enc));
+                       explicit_bzero(_KEYBUF(sav->key_enc), _KEYLEN(sav->key_enc));
        }
        if (sav->key_auth != NULL) {
                KFREE(sav->key_auth);
diff -r eae9ba6d9b28 -r 92aca9d3bc91 sys/netipsec/xform_ah.c
--- a/sys/netipsec/xform_ah.c   Thu Aug 30 02:26:38 2012 +0000
+++ b/sys/netipsec/xform_ah.c   Thu Aug 30 12:16:48 2012 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: xform_ah.c,v 1.37 2012/01/26 21:10:24 drochner Exp $   */
+/*     $NetBSD: xform_ah.c,v 1.38 2012/08/30 12:16:49 drochner Exp $   */
 /*     $FreeBSD: src/sys/netipsec/xform_ah.c,v 1.1.4.1 2003/01/24 05:11:36 sam Exp $   */
 /*     $OpenBSD: ip_ah.c,v 1.63 2001/06/26 06:18:58 angelos Exp $ */
 /*
@@ -39,7 +39,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: xform_ah.c,v 1.37 2012/01/26 21:10:24 drochner Exp $");
+__KERNEL_RCSID(0, "$NetBSD: xform_ah.c,v 1.38 2012/08/30 12:16:49 drochner Exp $");



Home | Main Index | Thread Index | Old Index