Move remaining ScopedContext types out of scoped_types.h

Change-Id: I7d1fa964f0d9817db885cd43057a23ec46f21702
Reviewed-on: https://boringssl-review.googlesource.com/10240
Reviewed-by: Adam Langley <agl@google.com>
Commit-Queue: Adam Langley <agl@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
diff --git a/crypto/bytestring/bytestring_test.cc b/crypto/bytestring/bytestring_test.cc
index 9ab2c0c..0ec7d54 100644
--- a/crypto/bytestring/bytestring_test.cc
+++ b/crypto/bytestring/bytestring_test.cc
@@ -22,12 +22,13 @@
 
 #include <vector>
 
+#include <openssl/c++/bytestring.h>
 #include <openssl/crypto.h>
-#include <openssl/bytestring.h>
 
 #include "internal.h"
 #include "../test/scoped_types.h"
 
+namespace bssl {
 
 static bool TestSkip() {
   static const uint8_t kData[] = {1, 2, 3};
@@ -873,7 +874,7 @@
   return true;
 }
 
-int main(void) {
+static int Main() {
   CRYPTO_library_init();
 
   if (!TestSkip() ||
@@ -901,3 +902,9 @@
   printf("PASS\n");
   return 0;
 }
+
+}  // namespace bssl
+
+int main() {
+  return bssl::Main();
+}
diff --git a/crypto/cipher/cipher_test.cc b/crypto/cipher/cipher_test.cc
index fa384c6..08c8bd5 100644
--- a/crypto/cipher/cipher_test.cc
+++ b/crypto/cipher/cipher_test.cc
@@ -57,13 +57,13 @@
 #include <string>
 #include <vector>
 
-#include <openssl/cipher.h>
+#include <openssl/c++/cipher.h>
 #include <openssl/crypto.h>
 #include <openssl/err.h>
 
 #include "../test/file_test.h"
-#include "../test/scoped_types.h"
 
+namespace bssl {
 
 static const EVP_CIPHER *GetCipher(const std::string &name) {
   if (name == "DES-CBC") {
@@ -284,7 +284,7 @@
   return true;
 }
 
-int main(int argc, char **argv) {
+static int Main(int argc, char **argv) {
   CRYPTO_library_init();
 
   if (argc != 2) {
@@ -294,3 +294,9 @@
 
   return FileTestMain(TestCipher, nullptr, argv[1]);
 }
+
+}  // namespace bssl
+
+int main(int argc, char **argv) {
+  return bssl::Main(argc, argv);
+}
diff --git a/crypto/dh/dh_test.cc b/crypto/dh/dh_test.cc
index 60e433b..16df9a2 100644
--- a/crypto/dh/dh_test.cc
+++ b/crypto/dh/dh_test.cc
@@ -62,7 +62,7 @@
 #include <vector>
 
 #include <openssl/bn.h>
-#include <openssl/bytestring.h>
+#include <openssl/c++/bytestring.h>
 #include <openssl/crypto.h>
 #include <openssl/err.h>
 #include <openssl/mem.h>
@@ -70,6 +70,7 @@
 #include "internal.h"
 #include "../test/scoped_types.h"
 
+namespace bssl {
 
 static bool RunBasicTests();
 static bool RunRFC5114Tests();
@@ -77,7 +78,7 @@
 static bool TestASN1();
 static bool TestRFC3526();
 
-int main(int argc, char *argv[]) {
+static int Main() {
   CRYPTO_library_init();
 
   if (!RunBasicTests() ||
@@ -662,3 +663,9 @@
 
   return true;
 }
+
+}  // namespace bssl
+
+int main() {
+  return bssl::Main();
+}
diff --git a/crypto/digest/digest_test.cc b/crypto/digest/digest_test.cc
index fbe2297..c94096b 100644
--- a/crypto/digest/digest_test.cc
+++ b/crypto/digest/digest_test.cc
@@ -16,15 +16,16 @@
 #include <stdio.h>
 #include <string.h>
 
+#include <memory>
+
+#include <openssl/c++/digest.h>
 #include <openssl/crypto.h>
-#include <openssl/digest.h>
 #include <openssl/err.h>
 #include <openssl/md4.h>
 #include <openssl/md5.h>
 #include <openssl/sha.h>
 
-#include "../test/scoped_types.h"
-
+namespace bssl {
 
 struct MD {
   // name is the name of the digest.
@@ -243,7 +244,7 @@
   return true;
 }
 
-int main(void) {
+static int Main() {
   CRYPTO_library_init();
 
   for (size_t i = 0; i < sizeof(kTestVectors) / sizeof(kTestVectors[0]); i++) {
@@ -260,3 +261,9 @@
   printf("PASS\n");
   return 0;
 }
+
+}  // namespace bssl
+
+int main() {
+  return bssl::Main();
+}
diff --git a/crypto/ec/ec_test.cc b/crypto/ec/ec_test.cc
index 23befeb..ca0e140 100644
--- a/crypto/ec/ec_test.cc
+++ b/crypto/ec/ec_test.cc
@@ -17,7 +17,7 @@
 
 #include <vector>
 
-#include <openssl/bytestring.h>
+#include <openssl/c++/bytestring.h>
 #include <openssl/crypto.h>
 #include <openssl/ec_key.h>
 #include <openssl/err.h>
@@ -25,6 +25,7 @@
 
 #include "../test/scoped_types.h"
 
+namespace bssl {
 
 // kECKeyWithoutPublic is an ECPrivateKey with the optional publicKey field
 // omitted.
@@ -471,7 +472,7 @@
   return true;
 }
 
-int main(void) {
+static int Main() {
   CRYPTO_library_init();
 
   if (!Testd2i_ECPrivateKey() ||
@@ -487,3 +488,9 @@
   printf("PASS\n");
   return 0;
 }
+
+}  // namespace bssl
+
+int main() {
+  return bssl::Main();
+}
diff --git a/crypto/evp/evp_extra_test.cc b/crypto/evp/evp_extra_test.cc
index 9bc36ad..b2c519e 100644
--- a/crypto/evp/evp_extra_test.cc
+++ b/crypto/evp/evp_extra_test.cc
@@ -20,15 +20,15 @@
 #include <utility>
 #include <vector>
 
-#include <openssl/bytestring.h>
+#include <openssl/c++/bytestring.h>
+#include <openssl/c++/digest.h>
 #include <openssl/crypto.h>
-#include <openssl/digest.h>
 #include <openssl/err.h>
-#include <openssl/evp.h>
 #include <openssl/rsa.h>
 
 #include "../test/scoped_types.h"
 
+namespace bssl {
 
 // kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you
 // should never use this key anywhere but in an example.
@@ -671,7 +671,7 @@
   return true;
 }
 
-int main(void) {
+static int Main(void) {
   CRYPTO_library_init();
 
   if (!TestEVP_DigestSignInit()) {
@@ -719,3 +719,9 @@
   printf("PASS\n");
   return 0;
 }
+
+}  // namespace bssl
+
+int main() {
+  return bssl::Main();
+}
diff --git a/crypto/evp/evp_test.cc b/crypto/evp/evp_test.cc
index b01d1e4..9c8735b 100644
--- a/crypto/evp/evp_test.cc
+++ b/crypto/evp/evp_test.cc
@@ -68,7 +68,7 @@
 
 OPENSSL_MSVC_PRAGMA(warning(pop))
 
-#include <openssl/bytestring.h>
+#include <openssl/c++/bytestring.h>
 #include <openssl/crypto.h>
 #include <openssl/digest.h>
 #include <openssl/err.h>
@@ -76,6 +76,7 @@
 #include "../test/file_test.h"
 #include "../test/scoped_types.h"
 
+namespace bssl {
 
 // evp_test dispatches between multiple test types. PrivateKey tests take a key
 // name parameter and single block, decode it as a PEM private key, and save it
@@ -253,7 +254,7 @@
   return true;
 }
 
-int main(int argc, char **argv) {
+static int Main(int argc, char *argv[]) {
   CRYPTO_library_init();
   if (argc != 2) {
     fprintf(stderr, "%s <test file.txt>\n", argv[0]);
@@ -263,3 +264,9 @@
   KeyMap map;
   return FileTestMain(TestEVP, &map, argv[1]);
 }
+
+}  // namespace bssl
+
+int main(int argc, char *argv[]) {
+  return bssl::Main(argc, argv);
+}
diff --git a/crypto/hmac/hmac_test.cc b/crypto/hmac/hmac_test.cc
index 2bcb162..3d49d9e 100644
--- a/crypto/hmac/hmac_test.cc
+++ b/crypto/hmac/hmac_test.cc
@@ -57,16 +57,17 @@
 #include <stdio.h>
 #include <string.h>
 
+#include <memory>
 #include <string>
 #include <vector>
 
+#include <openssl/c++/hmac.h>
 #include <openssl/crypto.h>
 #include <openssl/digest.h>
-#include <openssl/hmac.h>
 
 #include "../test/file_test.h"
-#include "../test/scoped_types.h"
 
+namespace bssl {
 
 static const EVP_MD *GetDigest(const std::string &name) {
   if (name == "MD5") {
@@ -157,7 +158,7 @@
   return true;
 }
 
-int main(int argc, char *argv[]) {
+static int Main(int argc, char *argv[]) {
   CRYPTO_library_init();
 
   if (argc != 2) {
@@ -167,3 +168,9 @@
 
   return FileTestMain(TestHMAC, nullptr, argv[1]);
 }
+
+}  // namespace bssl
+
+int main(int argc, char **argv) {
+  return bssl::Main(argc, argv);
+}
diff --git a/crypto/test/scoped_types.h b/crypto/test/scoped_types.h
index 76e38af..c124235 100644
--- a/crypto/test/scoped_types.h
+++ b/crypto/test/scoped_types.h
@@ -125,15 +125,6 @@
 
 using ScopedX509Stack = ScopedOpenSSLStack<STACK_OF(X509), X509, X509_free>;
 
-using ScopedCBB = ScopedOpenSSLContext<CBB, void, CBB_zero, CBB_cleanup>;
-using ScopedEVP_CIPHER_CTX = ScopedOpenSSLContext<EVP_CIPHER_CTX, int,
-                                                  EVP_CIPHER_CTX_init,
-                                                  EVP_CIPHER_CTX_cleanup>;
-using ScopedEVP_MD_CTX = ScopedOpenSSLContext<EVP_MD_CTX, int, EVP_MD_CTX_init,
-                                              EVP_MD_CTX_cleanup>;
-using ScopedHMAC_CTX = ScopedOpenSSLContext<HMAC_CTX, void, HMAC_CTX_init,
-                                            HMAC_CTX_cleanup>;
-
 using ScopedOpenSSLBytes = std::unique_ptr<uint8_t, OpenSSLFree<uint8_t>>;
 using ScopedOpenSSLString = std::unique_ptr<char, OpenSSLFree<char>>;
 
diff --git a/crypto/x509/x509_test.cc b/crypto/x509/x509_test.cc
index 650163a..d1eed2a 100644
--- a/crypto/x509/x509_test.cc
+++ b/crypto/x509/x509_test.cc
@@ -17,15 +17,15 @@
 #include <assert.h>
 #include <string.h>
 
+#include <openssl/c++/digest.h>
 #include <openssl/crypto.h>
-#include <openssl/digest.h>
 #include <openssl/err.h>
-#include <openssl/evp.h>
 #include <openssl/pem.h>
 #include <openssl/x509.h>
 
 #include "../test/scoped_types.h"
 
+namespace bssl {
 
 static const char kCrossSigningRootPEM[] =
 "-----BEGIN CERTIFICATE-----\n"
@@ -457,7 +457,7 @@
   return true;
 }
 
-int main(int argc, char **argv) {
+static int Main() {
   CRYPTO_library_init();
 
   if (!TestVerify() ||
@@ -470,3 +470,9 @@
   printf("PASS\n");
   return 0;
 }
+
+}  // namespace bssl
+
+int main() {
+  return bssl::Main();
+}
diff --git a/include/openssl/c++/bytestring.h b/include/openssl/c++/bytestring.h
new file mode 100644
index 0000000..87325a9
--- /dev/null
+++ b/include/openssl/c++/bytestring.h
@@ -0,0 +1,27 @@
+/* Copyright (c) 2016, Google Inc.
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
+
+#ifndef OPENSSL_HEADER_CXX_BYTESTRING_H
+#define OPENSSL_HEADER_CXX_BYTESTRING_H
+
+#include <openssl/bytestring.h>
+#include <openssl/c++/scoped_helpers.h>
+
+namespace bssl {
+
+using ScopedCBB = ScopedContext<CBB, void, CBB_zero, CBB_cleanup>;
+
+}  // namespace bssl
+
+#endif /* OPENSSL_HEADER_CXX_BYTESTRING_H */
diff --git a/include/openssl/c++/cipher.h b/include/openssl/c++/cipher.h
new file mode 100644
index 0000000..997a606
--- /dev/null
+++ b/include/openssl/c++/cipher.h
@@ -0,0 +1,29 @@
+/* Copyright (c) 2016, Google Inc.
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
+
+#ifndef OPENSSL_HEADER_CXX_CIPHER_H
+#define OPENSSL_HEADER_CXX_CIPHER_H
+
+#include <openssl/cipher.h>
+#include <openssl/c++/scoped_helpers.h>
+
+namespace bssl {
+
+using ScopedEVP_CIPHER_CTX =
+    ScopedContext<EVP_CIPHER_CTX, int, EVP_CIPHER_CTX_init,
+                  EVP_CIPHER_CTX_cleanup>;
+
+}  // namespace bssl
+
+#endif /* OPENSSL_HEADER_CXX_CIPHER_H */
diff --git a/include/openssl/c++/digest.h b/include/openssl/c++/digest.h
new file mode 100644
index 0000000..f557921
--- /dev/null
+++ b/include/openssl/c++/digest.h
@@ -0,0 +1,28 @@
+/* Copyright (c) 2016, Google Inc.
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
+
+#ifndef OPENSSL_HEADER_CXX_DIGEST_H
+#define OPENSSL_HEADER_CXX_DIGEST_H
+
+#include <openssl/c++/scoped_helpers.h>
+#include <openssl/evp.h>
+
+namespace bssl {
+
+using ScopedEVP_MD_CTX =
+    ScopedContext<EVP_MD_CTX, int, EVP_MD_CTX_init, EVP_MD_CTX_cleanup>;
+
+}  // namespace bssl
+
+#endif /* OPENSSL_HEADER_CXX_DIGEST_H */
diff --git a/include/openssl/c++/hmac.h b/include/openssl/c++/hmac.h
new file mode 100644
index 0000000..0e8d2e1
--- /dev/null
+++ b/include/openssl/c++/hmac.h
@@ -0,0 +1,28 @@
+/* Copyright (c) 2016, Google Inc.
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+ * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
+ * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
+ * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
+
+#ifndef OPENSSL_HEADER_CXX_HMAC_H
+#define OPENSSL_HEADER_CXX_HMAC_H
+
+#include <openssl/c++/scoped_helpers.h>
+#include <openssl/hmac.h>
+
+namespace bssl {
+
+using ScopedHMAC_CTX =
+    ScopedContext<HMAC_CTX, void, HMAC_CTX_init, HMAC_CTX_cleanup>;
+
+}  // namespace bssl
+
+#endif /* OPENSSL_HEADER_CXX_HMAC_H */
diff --git a/ssl/test/bssl_shim.cc b/ssl/test/bssl_shim.cc
index 61bdd7b..aa7f398 100644
--- a/ssl/test/bssl_shim.cc
+++ b/ssl/test/bssl_shim.cc
@@ -43,6 +43,7 @@
 #include <openssl/bio.h>
 #include <openssl/buf.h>
 #include <openssl/bytestring.h>
+#include <openssl/c++/digest.h>
 #include <openssl/cipher.h>
 #include <openssl/crypto.h>
 #include <openssl/err.h>
@@ -61,6 +62,7 @@
 #include "scoped_types.h"
 #include "test_config.h"
 
+namespace bssl {
 
 #if !defined(OPENSSL_WINDOWS)
 static int closesocket(int sock) {
@@ -1634,7 +1636,7 @@
   ~StderrDelimiter() { fprintf(stderr, "--- DONE ---\n"); }
 };
 
-int main(int argc, char **argv) {
+static int Main(int argc, char **argv) {
   // To distinguish ASan's output from ours, add a trailing message to stderr.
   // Anything following this line will be considered an error.
   StderrDelimiter delimiter;
@@ -1690,3 +1692,9 @@
 
   return 0;
 }
+
+}  // namespace bssl
+
+int main(int argc, char **argv) {
+  return bssl::Main(argc, argv);
+}