Fold stack-allocated types into headers.

Now that we have the extern "C++" trick, we can just embed them in the
normal headers. Move the EVP_CIPHER_CTX deleter to cipher.h and, in
doing so, take away a little bit of boilerplate in defining deleters.

Change-Id: I4a4b8d0db5274a3607914d94e76a38996bd611ec
Reviewed-on: https://boringssl-review.googlesource.com/10804
Reviewed-by: Matt Braithwaite <mab@google.com>
Commit-Queue: David Benjamin <davidben@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 45a2335..251aa37 100644
--- a/crypto/bytestring/bytestring_test.cc
+++ b/crypto/bytestring/bytestring_test.cc
@@ -22,7 +22,7 @@
 
 #include <vector>
 
-#include <openssl/c++/bytestring.h>
+#include <openssl/bytestring.h>
 #include <openssl/crypto.h>
 
 #include "internal.h"
diff --git a/crypto/cipher/aead_test.cc b/crypto/cipher/aead_test.cc
index b75737f..38a07d3 100644
--- a/crypto/cipher/aead_test.cc
+++ b/crypto/cipher/aead_test.cc
@@ -17,7 +17,7 @@
 
 #include <vector>
 
-#include <openssl/c++/aead.h>
+#include <openssl/aead.h>
 #include <openssl/crypto.h>
 #include <openssl/err.h>
 
diff --git a/crypto/cipher/cipher_test.cc b/crypto/cipher/cipher_test.cc
index 08c8bd5..cb42fc5 100644
--- a/crypto/cipher/cipher_test.cc
+++ b/crypto/cipher/cipher_test.cc
@@ -57,7 +57,7 @@
 #include <string>
 #include <vector>
 
-#include <openssl/c++/cipher.h>
+#include <openssl/cipher.h>
 #include <openssl/crypto.h>
 #include <openssl/err.h>
 
diff --git a/crypto/dh/dh_test.cc b/crypto/dh/dh_test.cc
index 12984e6..9a3d780 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/c++/bytestring.h>
+#include <openssl/bytestring.h>
 #include <openssl/crypto.h>
 #include <openssl/dh.h>
 #include <openssl/err.h>
diff --git a/crypto/digest/digest_test.cc b/crypto/digest/digest_test.cc
index 585b62a..70fa483 100644
--- a/crypto/digest/digest_test.cc
+++ b/crypto/digest/digest_test.cc
@@ -18,8 +18,8 @@
 
 #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>
diff --git a/crypto/ec/ec_test.cc b/crypto/ec/ec_test.cc
index 9648d57..839acfe 100644
--- a/crypto/ec/ec_test.cc
+++ b/crypto/ec/ec_test.cc
@@ -17,8 +17,8 @@
 
 #include <vector>
 
-#include <openssl/c++/bytestring.h>
 #include <openssl/bn.h>
+#include <openssl/bytestring.h>
 #include <openssl/crypto.h>
 #include <openssl/ec_key.h>
 #include <openssl/err.h>
diff --git a/crypto/evp/evp_extra_test.cc b/crypto/evp/evp_extra_test.cc
index 125dc03..755fa83 100644
--- a/crypto/evp/evp_extra_test.cc
+++ b/crypto/evp/evp_extra_test.cc
@@ -20,9 +20,9 @@
 #include <utility>
 #include <vector>
 
-#include <openssl/c++/bytestring.h>
-#include <openssl/c++/digest.h>
+#include <openssl/bytestring.h>
 #include <openssl/crypto.h>
+#include <openssl/digest.h>
 #include <openssl/err.h>
 #include <openssl/pkcs8.h>
 #include <openssl/rsa.h>
diff --git a/crypto/evp/evp_test.cc b/crypto/evp/evp_test.cc
index 58d3ebd..68b869a 100644
--- a/crypto/evp/evp_test.cc
+++ b/crypto/evp/evp_test.cc
@@ -68,7 +68,7 @@
 
 OPENSSL_MSVC_PRAGMA(warning(pop))
 
-#include <openssl/c++/bytestring.h>
+#include <openssl/bytestring.h>
 #include <openssl/crypto.h>
 #include <openssl/digest.h>
 #include <openssl/err.h>
diff --git a/crypto/hmac/hmac_test.cc b/crypto/hmac/hmac_test.cc
index 3d49d9e..60a9581 100644
--- a/crypto/hmac/hmac_test.cc
+++ b/crypto/hmac/hmac_test.cc
@@ -61,9 +61,9 @@
 #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"
 
diff --git a/crypto/x509/x509_test.cc b/crypto/x509/x509_test.cc
index 3c7cc63..a62088d 100644
--- a/crypto/x509/x509_test.cc
+++ b/crypto/x509/x509_test.cc
@@ -17,8 +17,8 @@
 #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/pem.h>
 #include <openssl/x509.h>
diff --git a/include/openssl/aead.h b/include/openssl/aead.h
index 7895825..71071d2 100644
--- a/include/openssl/aead.h
+++ b/include/openssl/aead.h
@@ -333,6 +333,21 @@
 
 #if defined(__cplusplus)
 }  /* extern C */
+
+#if !defined(BORINGSSL_NO_CXX)
+extern "C++" {
+
+namespace bssl {
+
+using ScopedEVP_AEAD_CTX =
+    internal::StackAllocated<EVP_AEAD_CTX, void, EVP_AEAD_CTX_zero,
+                             EVP_AEAD_CTX_cleanup>;
+
+}  // namespace bssl
+
+}  // extern C++
+#endif
+
 #endif
 
 #endif  /* OPENSSL_HEADER_AEAD_H */
diff --git a/include/openssl/asn1.h b/include/openssl/asn1.h
index e99099e..391be81 100644
--- a/include/openssl/asn1.h
+++ b/include/openssl/asn1.h
@@ -928,14 +928,10 @@
 
 namespace bssl {
 
-namespace internal {
-
 BORINGSSL_MAKE_DELETER(ASN1_OBJECT, ASN1_OBJECT_free)
 BORINGSSL_MAKE_DELETER(ASN1_STRING, ASN1_STRING_free)
 BORINGSSL_MAKE_DELETER(ASN1_TYPE, ASN1_TYPE_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/include/openssl/base.h b/include/openssl/base.h
index 8347c61..bfb4ea8 100644
--- a/include/openssl/base.h
+++ b/include/openssl/base.h
@@ -300,18 +300,22 @@
 #if defined(__cplusplus)
 }  /* extern C */
 
-extern "C++" {
-
 // MSVC doesn't set __cplusplus to 201103 to indicate C++11 support (see
 // https://connect.microsoft.com/VisualStudio/feedback/details/763051/a-value-of-predefined-macro-cplusplus-is-still-199711l)
 // so MSVC is just assumed to support C++11.
-#if defined(BORINGSSL_NO_CXX) || (__cplusplus < 201103L && !defined(_MSC_VER))
+#if !defined(BORINGSSL_NO_CXX) && __cplusplus < 201103L && !defined(_MSC_VER)
+#define BORINGSSL_NO_CXX
+#endif
+
+#if defined(BORINGSSL_NO_CXX)
 
 #define BORINGSSL_MAKE_DELETER(type, deleter)
 #define BORINGSSL_MAKE_STACK_DELETER(type, deleter)
 
 #else
 
+extern "C++" {
+
 #include <memory>
 
 namespace bssl {
@@ -320,21 +324,44 @@
 
 template <class T> struct Deleter {};
 
+template <typename T, typename CleanupRet, void (*init)(T *),
+          CleanupRet (*cleanup)(T *)>
+class StackAllocated {
+ public:
+  StackAllocated() { init(&ctx_); }
+  ~StackAllocated() { cleanup(&ctx_); }
+
+  T *get() { return &ctx_; }
+  const T *get() const { return &ctx_; }
+
+  void Reset() {
+    cleanup(&ctx_);
+    init(&ctx_);
+  }
+
+ private:
+  T ctx_;
+};
+
+}  // namespace internal
+
 #define BORINGSSL_MAKE_DELETER(type, deleter)       \
+  namespace internal {                              \
     template <> struct Deleter<type> {              \
       void operator()(type* ptr) { deleter(ptr); }  \
-    };
+    };                                              \
+  }
 
 // This makes a unique_ptr to STACK_OF(type) that owns all elements on the
 // stack, i.e. it uses sk_pop_free() to clean up.
 #define BORINGSSL_MAKE_STACK_DELETER(type, deleter)  \
+  namespace internal {                              \
     template <> struct Deleter<STACK_OF(type)> {     \
       void operator()(STACK_OF(type)* ptr) {         \
         sk_##type##_pop_free(ptr, deleter);          \
       }                                              \
-    };
-
-}  // namespace internal
+    };                                               \
+  }
 
 // Holds ownership of heap-allocated BoringSSL structures. Sample usage:
 //   bssl::UniquePtr<BIO> rsa(RSA_new());
@@ -344,10 +371,10 @@
 
 }  // namespace bssl
 
-#endif  // C++ allowed?
-
 }  /* extern C++ */
 
+#endif  // !BORINGSSL_NO_CXX
+
 #endif
 
 #endif  /* OPENSSL_HEADER_BASE_H */
diff --git a/include/openssl/bio.h b/include/openssl/bio.h
index 4aa9070..41c30ca 100644
--- a/include/openssl/bio.h
+++ b/include/openssl/bio.h
@@ -900,12 +900,8 @@
 
 namespace bssl {
 
-namespace internal {
-
 BORINGSSL_MAKE_DELETER(BIO, BIO_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/include/openssl/bn.h b/include/openssl/bn.h
index c1b9e39..554a81b 100644
--- a/include/openssl/bn.h
+++ b/include/openssl/bn.h
@@ -918,14 +918,10 @@
 
 namespace bssl {
 
-namespace internal {
-
 BORINGSSL_MAKE_DELETER(BIGNUM, BN_free)
 BORINGSSL_MAKE_DELETER(BN_CTX, BN_CTX_free)
 BORINGSSL_MAKE_DELETER(BN_MONT_CTX, BN_MONT_CTX_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/include/openssl/buf.h b/include/openssl/buf.h
index 7e39864..30f3af7 100644
--- a/include/openssl/buf.h
+++ b/include/openssl/buf.h
@@ -122,12 +122,8 @@
 
 namespace bssl {
 
-namespace internal {
-
 BORINGSSL_MAKE_DELETER(BUF_MEM, BUF_MEM_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/include/openssl/bytestring.h b/include/openssl/bytestring.h
index c055a73..2985268 100644
--- a/include/openssl/bytestring.h
+++ b/include/openssl/bytestring.h
@@ -416,6 +416,20 @@
 
 #if defined(__cplusplus)
 }  /* extern C */
+
+
+#if !defined(BORINGSSL_NO_CXX)
+extern "C++" {
+
+namespace bssl {
+
+using ScopedCBB = internal::StackAllocated<CBB, void, CBB_zero, CBB_cleanup>;
+
+}  // namespace bssl
+
+}  // extern C++
+#endif
+
 #endif
 
 #endif  /* OPENSSL_HEADER_BYTESTRING_H */
diff --git a/include/openssl/c++/aead.h b/include/openssl/c++/aead.h
deleted file mode 100644
index 1530923..0000000
--- a/include/openssl/c++/aead.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/* 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_AEAD_H
-#define OPENSSL_HEADER_CXX_AEAD_H
-
-#include <openssl/aead.h>
-#include <openssl/c++/scoped_helpers.h>
-
-namespace bssl {
-
-using ScopedEVP_AEAD_CTX =
-    ScopedContext<EVP_AEAD_CTX, void, EVP_AEAD_CTX_zero, EVP_AEAD_CTX_cleanup>;
-
-}  // namespace bssl
-
-#endif /* OPENSSL_HEADER_CXX_AEAD_H */
diff --git a/include/openssl/c++/bytestring.h b/include/openssl/c++/bytestring.h
deleted file mode 100644
index 87325a9..0000000
--- a/include/openssl/c++/bytestring.h
+++ /dev/null
@@ -1,27 +0,0 @@
-/* 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
deleted file mode 100644
index 997a606..0000000
--- a/include/openssl/c++/cipher.h
+++ /dev/null
@@ -1,29 +0,0 @@
-/* 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
deleted file mode 100644
index f557921..0000000
--- a/include/openssl/c++/digest.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/* 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
deleted file mode 100644
index 0e8d2e1..0000000
--- a/include/openssl/c++/hmac.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/* 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/include/openssl/c++/scoped_helpers.h b/include/openssl/c++/scoped_helpers.h
deleted file mode 100644
index daaf50e..0000000
--- a/include/openssl/c++/scoped_helpers.h
+++ /dev/null
@@ -1,45 +0,0 @@
-/* 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_SCOPED_HELPERS_H
-#define OPENSSL_HEADER_CXX_SCOPED_HELPERS_H
-
-namespace bssl {
-
-template<typename T, typename CleanupRet, void (*init_func)(T*),
-         CleanupRet (*cleanup_func)(T*)>
-class ScopedContext {
- public:
-  ScopedContext() {
-    init_func(&ctx_);
-  }
-  ~ScopedContext() {
-    cleanup_func(&ctx_);
-  }
-
-  T *get() { return &ctx_; }
-  const T *get() const { return &ctx_; }
-
-  void Reset() {
-    cleanup_func(&ctx_);
-    init_func(&ctx_);
-  }
-
- private:
-  T ctx_;
-};
-
-}  // namespace bssl
-
-#endif /* OPENSSL_HEADER_CXX_SCOPED_HELPERS_H */
diff --git a/include/openssl/cipher.h b/include/openssl/cipher.h
index 5312308..a8585d7 100644
--- a/include/openssl/cipher.h
+++ b/include/openssl/cipher.h
@@ -540,6 +540,23 @@
 
 #if defined(__cplusplus)
 }  /* extern C */
+
+#if !defined(BORINGSSL_NO_CXX)
+extern "C++" {
+
+namespace bssl {
+
+BORINGSSL_MAKE_DELETER(EVP_CIPHER_CTX, EVP_CIPHER_CTX_free)
+
+using ScopedEVP_CIPHER_CTX =
+    internal::StackAllocated<EVP_CIPHER_CTX, int, EVP_CIPHER_CTX_init,
+                             EVP_CIPHER_CTX_cleanup>;
+
+}  // namespace bssl
+
+}  // extern C++
+#endif
+
 #endif
 
 #define CIPHER_R_AES_KEY_SETUP_FAILED 100
diff --git a/include/openssl/cmac.h b/include/openssl/cmac.h
index fb0b9f2..0f05bc9 100644
--- a/include/openssl/cmac.h
+++ b/include/openssl/cmac.h
@@ -76,12 +76,8 @@
 
 namespace bssl {
 
-namespace internal {
-
 BORINGSSL_MAKE_DELETER(CMAC_CTX, CMAC_CTX_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/include/openssl/conf.h b/include/openssl/conf.h
index 8acb084..6e6364f 100644
--- a/include/openssl/conf.h
+++ b/include/openssl/conf.h
@@ -163,12 +163,8 @@
 
 namespace bssl {
 
-namespace internal {
-
 BORINGSSL_MAKE_DELETER(CONF, NCONF_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/include/openssl/curve25519.h b/include/openssl/curve25519.h
index 70eff7a..e9ba04d 100644
--- a/include/openssl/curve25519.h
+++ b/include/openssl/curve25519.h
@@ -172,12 +172,8 @@
 
 namespace bssl {
 
-namespace internal {
-
 BORINGSSL_MAKE_DELETER(SPAKE2_CTX, SPAKE2_CTX_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/include/openssl/dh.h b/include/openssl/dh.h
index a3f34d9..ed2396d 100644
--- a/include/openssl/dh.h
+++ b/include/openssl/dh.h
@@ -279,12 +279,8 @@
 
 namespace bssl {
 
-namespace internal {
-
 BORINGSSL_MAKE_DELETER(DH, DH_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/include/openssl/digest.h b/include/openssl/digest.h
index 6c5d6cf..ec62993 100644
--- a/include/openssl/digest.h
+++ b/include/openssl/digest.h
@@ -261,6 +261,23 @@
 
 #if defined(__cplusplus)
 }  /* extern C */
+
+#if !defined(BORINGSSL_NO_CXX)
+extern "C++" {
+
+namespace bssl {
+
+BORINGSSL_MAKE_DELETER(EVP_MD_CTX, EVP_MD_CTX_destroy)
+
+using ScopedEVP_MD_CTX =
+    internal::StackAllocated<EVP_MD_CTX, int, EVP_MD_CTX_init,
+                             EVP_MD_CTX_cleanup>;
+
+}  // namespace bssl
+
+}  // extern C++
+#endif
+
 #endif
 
 #define DIGEST_R_INPUT_NOT_INITIALIZED 100
diff --git a/include/openssl/dsa.h b/include/openssl/dsa.h
index 07f1495..d6c3204 100644
--- a/include/openssl/dsa.h
+++ b/include/openssl/dsa.h
@@ -416,13 +416,9 @@
 
 namespace bssl {
 
-namespace internal {
-
 BORINGSSL_MAKE_DELETER(DSA, DSA_free)
 BORINGSSL_MAKE_DELETER(DSA_SIG, DSA_SIG_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/include/openssl/ec.h b/include/openssl/ec.h
index 2349403..c2ef066 100644
--- a/include/openssl/ec.h
+++ b/include/openssl/ec.h
@@ -361,13 +361,9 @@
 
 namespace bssl {
 
-namespace internal {
-
 BORINGSSL_MAKE_DELETER(EC_POINT, EC_POINT_free)
 BORINGSSL_MAKE_DELETER(EC_GROUP, EC_GROUP_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/include/openssl/ec_key.h b/include/openssl/ec_key.h
index a7c8bf8..1dbae62 100644
--- a/include/openssl/ec_key.h
+++ b/include/openssl/ec_key.h
@@ -326,12 +326,8 @@
 
 namespace bssl {
 
-namespace internal {
-
 BORINGSSL_MAKE_DELETER(EC_KEY, EC_KEY_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/include/openssl/ecdsa.h b/include/openssl/ecdsa.h
index 5f07a9c..f6e9982 100644
--- a/include/openssl/ecdsa.h
+++ b/include/openssl/ecdsa.h
@@ -199,12 +199,8 @@
 
 namespace bssl {
 
-namespace internal {
-
 BORINGSSL_MAKE_DELETER(ECDSA_SIG, ECDSA_SIG_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/include/openssl/engine.h b/include/openssl/engine.h
index 9cbdf39..b029ef9 100644
--- a/include/openssl/engine.h
+++ b/include/openssl/engine.h
@@ -96,12 +96,8 @@
 
 namespace bssl {
 
-namespace internal {
-
 BORINGSSL_MAKE_DELETER(ENGINE, ENGINE_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/include/openssl/evp.h b/include/openssl/evp.h
index 41e9e14..58b388a 100644
--- a/include/openssl/evp.h
+++ b/include/openssl/evp.h
@@ -754,14 +754,9 @@
 extern "C++" {
 namespace bssl {
 
-namespace internal {
-
-BORINGSSL_MAKE_DELETER(EVP_CIPHER_CTX, EVP_CIPHER_CTX_free)
 BORINGSSL_MAKE_DELETER(EVP_PKEY, EVP_PKEY_free)
 BORINGSSL_MAKE_DELETER(EVP_PKEY_CTX, EVP_PKEY_CTX_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/include/openssl/hmac.h b/include/openssl/hmac.h
index 35c7f58..e4cc04e 100644
--- a/include/openssl/hmac.h
+++ b/include/openssl/hmac.h
@@ -155,6 +155,20 @@
 
 #if defined(__cplusplus)
 }  /* extern C */
+
+#if !defined(BORINGSSL_NO_CXX)
+extern "C++" {
+
+namespace bssl {
+
+using ScopedHMAC_CTX =
+    internal::StackAllocated<HMAC_CTX, void, HMAC_CTX_init, HMAC_CTX_cleanup>;
+
+}  // namespace bssl
+
+}  // extern C++
+#endif
+
 #endif
 
 #endif  /* OPENSSL_HEADER_HMAC_H */
diff --git a/include/openssl/mem.h b/include/openssl/mem.h
index 98ee077..5d96a2d 100644
--- a/include/openssl/mem.h
+++ b/include/openssl/mem.h
@@ -138,13 +138,9 @@
 
 namespace bssl {
 
-namespace internal {
-
 BORINGSSL_MAKE_DELETER(char, OPENSSL_free)
 BORINGSSL_MAKE_DELETER(uint8_t, OPENSSL_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/include/openssl/newhope.h b/include/openssl/newhope.h
index 67728ea..47b9913 100644
--- a/include/openssl/newhope.h
+++ b/include/openssl/newhope.h
@@ -147,12 +147,8 @@
 
 namespace bssl {
 
-namespace internal {
-
 BORINGSSL_MAKE_DELETER(NEWHOPE_POLY, NEWHOPE_POLY_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/include/openssl/pkcs8.h b/include/openssl/pkcs8.h
index 0939d8c..e04a4f3 100644
--- a/include/openssl/pkcs8.h
+++ b/include/openssl/pkcs8.h
@@ -192,13 +192,9 @@
 
 namespace bssl {
 
-namespace internal {
-
 BORINGSSL_MAKE_DELETER(PKCS12, PKCS12_free)
 BORINGSSL_MAKE_DELETER(PKCS8_PRIV_KEY_INFO, PKCS8_PRIV_KEY_INFO_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/include/openssl/rsa.h b/include/openssl/rsa.h
index f761dc6..78d5b32 100644
--- a/include/openssl/rsa.h
+++ b/include/openssl/rsa.h
@@ -641,12 +641,8 @@
 
 namespace bssl {
 
-namespace internal {
-
 BORINGSSL_MAKE_DELETER(RSA, RSA_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/include/openssl/ssl.h b/include/openssl/ssl.h
index 46640d9..ce2ba41 100644
--- a/include/openssl/ssl.h
+++ b/include/openssl/ssl.h
@@ -4651,14 +4651,10 @@
 
 namespace bssl {
 
-namespace internal {
-
 BORINGSSL_MAKE_DELETER(SSL, SSL_free)
 BORINGSSL_MAKE_DELETER(SSL_CTX, SSL_CTX_free)
 BORINGSSL_MAKE_DELETER(SSL_SESSION, SSL_SESSION_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/include/openssl/x509.h b/include/openssl/x509.h
index c32a6e7..b45dd3b 100644
--- a/include/openssl/x509.h
+++ b/include/openssl/x509.h
@@ -1229,8 +1229,6 @@
 
 namespace bssl {
 
-namespace internal {
-
 BORINGSSL_MAKE_STACK_DELETER(X509, X509_free)
 BORINGSSL_MAKE_DELETER(X509, X509_free)
 BORINGSSL_MAKE_DELETER(X509_ALGOR, X509_ALGOR_free)
@@ -1250,8 +1248,6 @@
 BORINGSSL_MAKE_DELETER(X509_STORE_CTX, X509_STORE_CTX_free)
 BORINGSSL_MAKE_DELETER(X509_VERIFY_PARAM, X509_VERIFY_PARAM_free)
 
-}  // namespace internal
-
 }  // namespace bssl
 
 }  /* extern C++ */
diff --git a/ssl/test/bssl_shim.cc b/ssl/test/bssl_shim.cc
index ff77b63..a5bea16 100644
--- a/ssl/test/bssl_shim.cc
+++ b/ssl/test/bssl_shim.cc
@@ -43,10 +43,10 @@
 #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/dh.h>
+#include <openssl/digest.h>
 #include <openssl/err.h>
 #include <openssl/evp.h>
 #include <openssl/hmac.h>