Use asm directives to protect OPENSSL_cleanse.

Compilers have a bad habit of removing "superfluous" memset calls that
are trying to zero memory. For example, when memset()ing a buffer and
then free()ing it, the compiler might decide that the memset is
unobservable and thus can be removed.

Previously we tried to stop this by a) implementing memset in assembly
on x86 and b) putting the function in its own file for other platforms.

This change removes those tricks in favour of using asm directives to
scare the compiler away. As best as our compiler folks can tell, this is
sufficient and will continue to be so.

Change-Id: I40e0a62c3043038bafd8c63a91814a75a3c59269
Reviewed-on: https://boringssl-review.googlesource.com/1339
Reviewed-by: David Benjamin <davidben@chromium.org>
Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/CMakeLists.txt b/crypto/CMakeLists.txt
index 35c2262..2e255b8 100644
--- a/crypto/CMakeLists.txt
+++ b/crypto/CMakeLists.txt
@@ -114,7 +114,6 @@
 
 	crypto_error.c
 	mem.c
-	mem_clear.c
 	thread.c
 	ex_data.c
 	ex_data_impl.c
diff --git a/crypto/cpu-x86-asm.pl b/crypto/cpu-x86-asm.pl
index 151493a..1ac7d84 100644
--- a/crypto/cpu-x86-asm.pl
+++ b/crypto/cpu-x86-asm.pl
@@ -321,40 +321,6 @@
 	}
 &function_end_B("OPENSSL_indirect_call");
 
-&function_begin_B("OPENSSL_cleanse");
-	&mov	("edx",&wparam(0));
-	&mov	("ecx",&wparam(1));
-	&xor	("eax","eax");
-	&cmp	("ecx",7);
-	&jae	(&label("lot"));
-	&cmp	("ecx",0);
-	&je	(&label("ret"));
-&set_label("little");
-	&mov	(&BP(0,"edx"),"al");
-	&sub	("ecx",1);
-	&lea	("edx",&DWP(1,"edx"));
-	&jnz	(&label("little"));
-&set_label("ret");
-	&ret	();
-
-&set_label("lot",16);
-	&test	("edx",3);
-	&jz	(&label("aligned"));
-	&mov	(&BP(0,"edx"),"al");
-	&lea	("ecx",&DWP(-1,"ecx"));
-	&lea	("edx",&DWP(1,"edx"));
-	&jmp	(&label("lot"));
-&set_label("aligned");
-	&mov	(&DWP(0,"edx"),"eax");
-	&lea	("ecx",&DWP(-4,"ecx"));
-	&test	("ecx",-4);
-	&lea	("edx",&DWP(4,"edx"));
-	&jnz	(&label("aligned"));
-	&cmp	("ecx",0);
-	&jne	(&label("little"));
-	&ret	();
-&function_end_B("OPENSSL_cleanse");
-
 &function_begin_B("OPENSSL_ia32_rdrand");
 	&mov	("ecx",8);
 &set_label("loop");
diff --git a/crypto/mem.c b/crypto/mem.c
index a5f639b..e9c035e 100644
--- a/crypto/mem.c
+++ b/crypto/mem.c
@@ -93,6 +93,19 @@
   return ret;
 }
 
+void OPENSSL_cleanse(void *ptr, size_t len) {
+  memset(ptr, 0, len);
+
+  /* As best as we can tell, this is sufficient to break any optimisations that
+     might try to eliminate "superfluous" memsets. If there's an easy way to
+     detect memset_s, it would be better to use that. */
+#if defined(OPENSSL_WINDOWS)
+  __asm;
+#else
+  __asm__ __volatile__("" : : "r"(ptr) : "memory");
+#endif
+}
+
 int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len) {
   size_t i;
   const uint8_t *a = in_a;
diff --git a/crypto/mem_clear.c b/crypto/mem_clear.c
deleted file mode 100644
index 2c46ffe..0000000
--- a/crypto/mem_clear.c
+++ /dev/null
@@ -1,69 +0,0 @@
-/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved.
- *
- * This package is an SSL implementation written
- * by Eric Young (eay@cryptsoft.com).
- * The implementation was written so as to conform with Netscapes SSL.
- *
- * This library is free for commercial and non-commercial use as long as
- * the following conditions are aheared to.  The following conditions
- * apply to all code found in this distribution, be it the RC4, RSA,
- * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
- * included with this distribution is covered by the same copyright terms
- * except that the holder is Tim Hudson (tjh@cryptsoft.com).
- *
- * Copyright remains Eric Young's, and as such any Copyright notices in
- * the code are not to be removed.
- * If this package is used in a product, Eric Young should be given attribution
- * as the author of the parts of the library used.
- * This can be in the form of a textual message at program startup or
- * in documentation (online or textual) provided with the package.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *    "This product includes cryptographic software written by
- *     Eric Young (eay@cryptsoft.com)"
- *    The word 'cryptographic' can be left out if the rouines from the library
- *    being used are not cryptographic related :-).
- * 4. If you include any Windows specific code (or a derivative thereof) from
- *    the apps directory (application code) you must include an acknowledgement:
- *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
- *
- * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * The licence and distribution terms for any publically available version or
- * derivative of this code cannot be changed.  i.e. this code cannot simply be
- * copied and put under another distribution licence
- * [including the GNU Public Licence.] */
-
-#include <openssl/mem.h>
-
-#include <string.h>
-
-
-#if !defined(OPENSSL_X86) || defined(OPENSSL_NO_ASM)
-
-/* OPENSSL_cleanse is given its own compilation unit in the hopes that the
- * compiler won't be able to optimise it away. */
-/* TODO(fork): this is fragile. Preferably we would be able to use memset_s
- * on platforms with it. */
-void OPENSSL_cleanse(void *ptr, size_t len) { memset(ptr, 0, len); }
-
-#endif