Start recognizing the OPENSSL_NANOLIBC define

nanolibc is an embedded platform with no threads. To start unforking
that build, generalize some of the OPENSSL_TRUSTY defines. OpenSSL has
OPENSSL_NO_SOCK if you don't have sockets and OPENSSL_NO_POSIX_IO if you
don't have file descriptors. Those names are fine enough, so I've
borrowed them here too.

There's more to be done here, but this will clear out some of it.

Change-Id: Iaba1fafdebb46ebb8f68b7956535dd0ccaaa832f
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/60890
Auto-Submit: David Benjamin <davidben@google.com>
Commit-Queue: Bob Beck <bbe@google.com>
Reviewed-by: Bob Beck <bbe@google.com>
diff --git a/crypto/bio/connect.c b/crypto/bio/connect.c
index c19f1c4..fda8f852 100644
--- a/crypto/bio/connect.c
+++ b/crypto/bio/connect.c
@@ -56,7 +56,7 @@
 
 #include <openssl/bio.h>
 
-#if !defined(OPENSSL_TRUSTY)
+#if !defined(OPENSSL_NO_SOCK)
 
 #include <assert.h>
 #include <errno.h>
@@ -544,4 +544,4 @@
   return (int)BIO_ctrl(bio, BIO_C_DO_STATE_MACHINE, 0, NULL);
 }
 
-#endif  // OPENSSL_TRUSTY
+#endif  // OPENSSL_NO_SOCK
diff --git a/crypto/bio/fd.c b/crypto/bio/fd.c
index 7775d7a..aac8bcb 100644
--- a/crypto/bio/fd.c
+++ b/crypto/bio/fd.c
@@ -56,7 +56,7 @@
 
 #include <openssl/bio.h>
 
-#if !defined(OPENSSL_TRUSTY)
+#if !defined(OPENSSL_NO_POSIX_IO)
 
 #include <errno.h>
 #include <string.h>
@@ -276,4 +276,4 @@
   return (int)BIO_ctrl(bio, BIO_C_GET_FD, 0, (char *) out_fd);
 }
 
-#endif  // OPENSSL_TRUSTY
+#endif  // OPENSSL_NO_POSIX_IO
diff --git a/crypto/bio/socket.c b/crypto/bio/socket.c
index c5bf609..6084e96 100644
--- a/crypto/bio/socket.c
+++ b/crypto/bio/socket.c
@@ -56,7 +56,7 @@
 
 #include <openssl/bio.h>
 
-#if !defined(OPENSSL_TRUSTY)
+#if !defined(OPENSSL_NO_SOCK)
 
 #include <fcntl.h>
 #include <string.h>
@@ -186,4 +186,4 @@
   return ret;
 }
 
-#endif  // OPENSSL_TRUSTY
+#endif  // OPENSSL_NO_SOCK
diff --git a/crypto/bio/socket_helper.c b/crypto/bio/socket_helper.c
index 4cd7825..366996c 100644
--- a/crypto/bio/socket_helper.c
+++ b/crypto/bio/socket_helper.c
@@ -20,7 +20,7 @@
 #include <openssl/bio.h>
 #include <openssl/err.h>
 
-#if !defined(OPENSSL_TRUSTY)
+#if !defined(OPENSSL_NO_SOCK)
 
 #include <fcntl.h>
 #include <string.h>
@@ -121,4 +121,4 @@
   return error;
 }
 
-#endif  // OPENSSL_TRUSTY
+#endif  // OPENSSL_NO_SOCK
diff --git a/crypto/internal.h b/crypto/internal.h
index 98e2178..274a0a8 100644
--- a/crypto/internal.h
+++ b/crypto/internal.h
@@ -206,14 +206,17 @@
 #endif
 
 
+// On non-MSVC 64-bit targets, we expect __uint128_t support. This includes
+// clang-cl, which defines both __clang__ and _MSC_VER.
 #if (!defined(_MSC_VER) || defined(__clang__)) && defined(OPENSSL_64_BIT)
 #define BORINGSSL_HAS_UINT128
 typedef __int128_t int128_t;
 typedef __uint128_t uint128_t;
 
-// clang-cl supports __uint128_t but modulus and division don't work.
-// https://crbug.com/787617.
-#if !defined(_MSC_VER) || !defined(__clang__)
+// __uint128_t division depends on intrinsics in the compiler runtime. Those
+// intrinsics are missing in clang-cl (https://crbug.com/787617) and nanolibc.
+// These may be bugs in the toolchain definition, but just disable it for now.
+#if !defined(_MSC_VER) && !defined(OPENSSL_NANOLIBC)
 #define BORINGSSL_CAN_DIVIDE_UINT128
 #endif
 #endif
diff --git a/include/openssl/base.h b/include/openssl/base.h
index 2249aea..cd7b75f 100644
--- a/include/openssl/base.h
+++ b/include/openssl/base.h
@@ -153,6 +153,14 @@
 
 #if defined(__TRUSTY__)
 #define OPENSSL_TRUSTY
+#define OPENSSL_NO_POSIX_IO
+#define OPENSSL_NO_SOCK
+#define OPENSSL_NO_THREADS_CORRUPT_MEMORY_AND_LEAK_SECRETS_IF_THREADED
+#endif
+
+#if defined(OPENSSL_NANOLIBC)
+#define OPENSSL_NO_POSIX_IO
+#define OPENSSL_NO_SOCK
 #define OPENSSL_NO_THREADS_CORRUPT_MEMORY_AND_LEAK_SECRETS_IF_THREADED
 #endif