Replace Scoped* heap types with bssl::UniquePtr.

Unlike the Scoped* types, bssl::UniquePtr is available to C++ users, and
offered for a large variety of types.  The 'extern "C++"' trick is used
to make the C++ bits digestible to C callers that wrap header files in
'extern "C"'.

Change-Id: Ifbca4c2997d6628e33028c7d7620c72aff0f862e
Reviewed-on: https://boringssl-review.googlesource.com/10521
Reviewed-by: David Benjamin <davidben@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/asn1/asn1_test.cc b/crypto/asn1/asn1_test.cc
index 8b02442..77a1ee0 100644
--- a/crypto/asn1/asn1_test.cc
+++ b/crypto/asn1/asn1_test.cc
@@ -18,8 +18,6 @@
 #include <openssl/crypto.h>
 #include <openssl/err.h>
 
-#include "../test/scoped_types.h"
-
 
 // kTag128 is an ASN.1 structure with a universal tag with number 128.
 static const uint8_t kTag128[] = {
@@ -42,7 +40,7 @@
 
 static bool TestLargeTags() {
   const uint8_t *p = kTag258;
-  ScopedASN1_TYPE obj(d2i_ASN1_TYPE(NULL, &p, sizeof(kTag258)));
+  bssl::UniquePtr<ASN1_TYPE> obj(d2i_ASN1_TYPE(NULL, &p, sizeof(kTag258)));
   if (obj) {
     fprintf(stderr, "Parsed value with illegal tag (type = %d).\n", obj->type);
     return false;
diff --git a/crypto/bio/bio_test.cc b/crypto/bio/bio_test.cc
index d7be884..cbc4fde 100644
--- a/crypto/bio/bio_test.cc
+++ b/crypto/bio/bio_test.cc
@@ -41,7 +41,6 @@
 #include <algorithm>
 
 #include "../internal.h"
-#include "../test/scoped_types.h"
 
 
 #if !defined(OPENSSL_WINDOWS)
@@ -104,7 +103,7 @@
   char hostname[80];
   BIO_snprintf(hostname, sizeof(hostname), "%s:%d", "127.0.0.1",
                ntohs(sin.sin_port));
-  ScopedBIO bio(BIO_new_connect(hostname));
+  bssl::UniquePtr<BIO> bio(BIO_new_connect(hostname));
   if (!bio) {
     fprintf(stderr, "BIO_new_connect failed.\n");
     return false;
@@ -216,8 +215,8 @@
       if (!BIO_new_bio_pair(&bio1, kBufferSize, &bio2, kBufferSize)) {
         return false;
       }
-      ScopedBIO bio1_scoper(bio1);
-      ScopedBIO bio2_scoper(bio2);
+      bssl::UniquePtr<BIO> bio1_scoper(bio1);
+      bssl::UniquePtr<BIO> bio2_scoper(bio2);
 
       total_write += BioWriteZeroCopyWrapper(
           bio1, bio1_application_send_buffer, kLengths[i]);
@@ -287,7 +286,7 @@
   // 256 (the size of the buffer) to ensure edge cases are correct.
   static const size_t kLengths[] = { 5, 250, 251, 252, 253, 254, 1023 };
 
-  ScopedBIO bio(BIO_new(BIO_s_mem()));
+  bssl::UniquePtr<BIO> bio(BIO_new(BIO_s_mem()));
   if (!bio) {
     fprintf(stderr, "BIO_new failed\n");
     return false;
@@ -331,7 +330,7 @@
 
 static bool ReadASN1(bool should_succeed, const uint8_t *data, size_t data_len,
                      size_t expected_len, size_t max_len) {
-  ScopedBIO bio(BIO_new_mem_buf(data, data_len));
+  bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(data, data_len));
 
   uint8_t *out;
   size_t out_len;
@@ -339,7 +338,7 @@
   if (!ok) {
     out = nullptr;
   }
-  ScopedOpenSSLBytes out_storage(out);
+  bssl::UniquePtr<uint8_t> out_storage(out);
 
   if (should_succeed != (ok == 1)) {
     return false;
@@ -369,7 +368,7 @@
   static const size_t kLargePayloadLen = 8000;
   static const uint8_t kLargePrefix[] = {0x30, 0x82, kLargePayloadLen >> 8,
                                          kLargePayloadLen & 0xff};
-  ScopedOpenSSLBytes large(reinterpret_cast<uint8_t *>(
+  bssl::UniquePtr<uint8_t> large(reinterpret_cast<uint8_t *>(
       OPENSSL_malloc(sizeof(kLargePrefix) + kLargePayloadLen)));
   if (!large) {
     return false;
diff --git a/crypto/bn/bn_test.cc b/crypto/bn/bn_test.cc
index bb83a40..0867dec 100644
--- a/crypto/bn/bn_test.cc
+++ b/crypto/bn/bn_test.cc
@@ -81,30 +81,30 @@
 #include <utility>
 
 #include <openssl/bn.h>
+#include <openssl/bytestring.h>
 #include <openssl/crypto.h>
 #include <openssl/err.h>
 #include <openssl/mem.h>
 
 #include "../internal.h"
 #include "../test/file_test.h"
-#include "../test/scoped_types.h"
 #include "../test/test_util.h"
 
 
-static int HexToBIGNUM(ScopedBIGNUM *out, const char *in) {
+static int HexToBIGNUM(bssl::UniquePtr<BIGNUM> *out, const char *in) {
   BIGNUM *raw = NULL;
   int ret = BN_hex2bn(&raw, in);
   out->reset(raw);
   return ret;
 }
 
-static ScopedBIGNUM GetBIGNUM(FileTest *t, const char *attribute) {
+static bssl::UniquePtr<BIGNUM> GetBIGNUM(FileTest *t, const char *attribute) {
   std::string hex;
   if (!t->GetAttribute(&hex, attribute)) {
     return nullptr;
   }
 
-  ScopedBIGNUM ret;
+  bssl::UniquePtr<BIGNUM> ret;
   if (HexToBIGNUM(&ret, hex.c_str()) != static_cast<int>(hex.size())) {
     t->PrintLine("Could not decode '%s'.", hex.c_str());
     return nullptr;
@@ -113,7 +113,7 @@
 }
 
 static bool GetInt(FileTest *t, int *out, const char *attribute) {
-  ScopedBIGNUM ret = GetBIGNUM(t, attribute);
+  bssl::UniquePtr<BIGNUM> ret = GetBIGNUM(t, attribute);
   if (!ret) {
     return false;
   }
@@ -133,8 +133,8 @@
     return true;
   }
 
-  ScopedOpenSSLString expected_str(BN_bn2hex(expected));
-  ScopedOpenSSLString actual_str(BN_bn2hex(actual));
+  bssl::UniquePtr<char> expected_str(BN_bn2hex(expected));
+  bssl::UniquePtr<char> actual_str(BN_bn2hex(actual));
   if (!expected_str || !actual_str) {
     return false;
   }
@@ -147,14 +147,14 @@
 }
 
 static bool TestSum(FileTest *t, BN_CTX *ctx) {
-  ScopedBIGNUM a = GetBIGNUM(t, "A");
-  ScopedBIGNUM b = GetBIGNUM(t, "B");
-  ScopedBIGNUM sum = GetBIGNUM(t, "Sum");
+  bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
+  bssl::UniquePtr<BIGNUM> b = GetBIGNUM(t, "B");
+  bssl::UniquePtr<BIGNUM> sum = GetBIGNUM(t, "Sum");
   if (!a || !b || !sum) {
     return false;
   }
 
-  ScopedBIGNUM ret(BN_new());
+  bssl::UniquePtr<BIGNUM> ret(BN_new());
   if (!ret ||
       !BN_add(ret.get(), a.get(), b.get()) ||
       !ExpectBIGNUMsEqual(t, "A + B", sum.get(), ret.get()) ||
@@ -246,16 +246,16 @@
 }
 
 static bool TestLShift1(FileTest *t, BN_CTX *ctx) {
-  ScopedBIGNUM a = GetBIGNUM(t, "A");
-  ScopedBIGNUM lshift1 = GetBIGNUM(t, "LShift1");
-  ScopedBIGNUM zero(BN_new());
+  bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
+  bssl::UniquePtr<BIGNUM> lshift1 = GetBIGNUM(t, "LShift1");
+  bssl::UniquePtr<BIGNUM> zero(BN_new());
   if (!a || !lshift1 || !zero) {
     return false;
   }
 
   BN_zero(zero.get());
 
-  ScopedBIGNUM ret(BN_new()), two(BN_new()), remainder(BN_new());
+  bssl::UniquePtr<BIGNUM> ret(BN_new()), two(BN_new()), remainder(BN_new());
   if (!ret || !two || !remainder ||
       !BN_set_word(two.get(), 2) ||
       !BN_add(ret.get(), a.get(), a.get()) ||
@@ -287,14 +287,14 @@
 }
 
 static bool TestLShift(FileTest *t, BN_CTX *ctx) {
-  ScopedBIGNUM a = GetBIGNUM(t, "A");
-  ScopedBIGNUM lshift = GetBIGNUM(t, "LShift");
+  bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
+  bssl::UniquePtr<BIGNUM> lshift = GetBIGNUM(t, "LShift");
   int n = 0;
   if (!a || !lshift || !GetInt(t, &n, "N")) {
     return false;
   }
 
-  ScopedBIGNUM ret(BN_new());
+  bssl::UniquePtr<BIGNUM> ret(BN_new());
   if (!ret ||
       !BN_lshift(ret.get(), a.get(), n) ||
       !ExpectBIGNUMsEqual(t, "A << N", lshift.get(), ret.get()) ||
@@ -307,14 +307,14 @@
 }
 
 static bool TestRShift(FileTest *t, BN_CTX *ctx) {
-  ScopedBIGNUM a = GetBIGNUM(t, "A");
-  ScopedBIGNUM rshift = GetBIGNUM(t, "RShift");
+  bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
+  bssl::UniquePtr<BIGNUM> rshift = GetBIGNUM(t, "RShift");
   int n = 0;
   if (!a || !rshift || !GetInt(t, &n, "N")) {
     return false;
   }
 
-  ScopedBIGNUM ret(BN_new());
+  bssl::UniquePtr<BIGNUM> ret(BN_new());
   if (!ret ||
       !BN_rshift(ret.get(), a.get(), n) ||
       !ExpectBIGNUMsEqual(t, "A >> N", rshift.get(), ret.get())) {
@@ -325,16 +325,16 @@
 }
 
 static bool TestSquare(FileTest *t, BN_CTX *ctx) {
-  ScopedBIGNUM a = GetBIGNUM(t, "A");
-  ScopedBIGNUM square = GetBIGNUM(t, "Square");
-  ScopedBIGNUM zero(BN_new());
+  bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
+  bssl::UniquePtr<BIGNUM> square = GetBIGNUM(t, "Square");
+  bssl::UniquePtr<BIGNUM> zero(BN_new());
   if (!a || !square || !zero) {
     return false;
   }
 
   BN_zero(zero.get());
 
-  ScopedBIGNUM ret(BN_new()), remainder(BN_new());
+  bssl::UniquePtr<BIGNUM> ret(BN_new()), remainder(BN_new());
   if (!ret ||
       !BN_sqr(ret.get(), a.get(), ctx) ||
       !ExpectBIGNUMsEqual(t, "A^2", square.get(), ret.get()) ||
@@ -354,7 +354,7 @@
 
   // BN_sqrt should fail on non-squares and negative numbers.
   if (!BN_is_zero(square.get())) {
-    ScopedBIGNUM tmp(BN_new());
+    bssl::UniquePtr<BIGNUM> tmp(BN_new());
     if (!tmp || !BN_copy(tmp.get(), square.get())) {
       return false;
     }
@@ -381,17 +381,17 @@
 }
 
 static bool TestProduct(FileTest *t, BN_CTX *ctx) {
-  ScopedBIGNUM a = GetBIGNUM(t, "A");
-  ScopedBIGNUM b = GetBIGNUM(t, "B");
-  ScopedBIGNUM product = GetBIGNUM(t, "Product");
-  ScopedBIGNUM zero(BN_new());
+  bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
+  bssl::UniquePtr<BIGNUM> b = GetBIGNUM(t, "B");
+  bssl::UniquePtr<BIGNUM> product = GetBIGNUM(t, "Product");
+  bssl::UniquePtr<BIGNUM> zero(BN_new());
   if (!a || !b || !product || !zero) {
     return false;
   }
 
   BN_zero(zero.get());
 
-  ScopedBIGNUM ret(BN_new()), remainder(BN_new());
+  bssl::UniquePtr<BIGNUM> ret(BN_new()), remainder(BN_new());
   if (!ret || !remainder ||
       !BN_mul(ret.get(), a.get(), b.get(), ctx) ||
       !ExpectBIGNUMsEqual(t, "A * B", product.get(), ret.get()) ||
@@ -408,15 +408,15 @@
 }
 
 static bool TestQuotient(FileTest *t, BN_CTX *ctx) {
-  ScopedBIGNUM a = GetBIGNUM(t, "A");
-  ScopedBIGNUM b = GetBIGNUM(t, "B");
-  ScopedBIGNUM quotient = GetBIGNUM(t, "Quotient");
-  ScopedBIGNUM remainder = GetBIGNUM(t, "Remainder");
+  bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
+  bssl::UniquePtr<BIGNUM> b = GetBIGNUM(t, "B");
+  bssl::UniquePtr<BIGNUM> quotient = GetBIGNUM(t, "Quotient");
+  bssl::UniquePtr<BIGNUM> remainder = GetBIGNUM(t, "Remainder");
   if (!a || !b || !quotient || !remainder) {
     return false;
   }
 
-  ScopedBIGNUM ret(BN_new()), ret2(BN_new());
+  bssl::UniquePtr<BIGNUM> ret(BN_new()), ret2(BN_new());
   if (!ret || !ret2 ||
       !BN_div(ret.get(), ret2.get(), a.get(), b.get(), ctx) ||
       !ExpectBIGNUMsEqual(t, "A / B", quotient.get(), ret.get()) ||
@@ -457,7 +457,7 @@
 
   // Test BN_nnmod.
   if (!BN_is_negative(b.get())) {
-    ScopedBIGNUM nnmod(BN_new());
+    bssl::UniquePtr<BIGNUM> nnmod(BN_new());
     if (!nnmod ||
         !BN_copy(nnmod.get(), remainder.get()) ||
         (BN_is_negative(nnmod.get()) &&
@@ -473,15 +473,15 @@
 }
 
 static bool TestModMul(FileTest *t, BN_CTX *ctx) {
-  ScopedBIGNUM a = GetBIGNUM(t, "A");
-  ScopedBIGNUM b = GetBIGNUM(t, "B");
-  ScopedBIGNUM m = GetBIGNUM(t, "M");
-  ScopedBIGNUM mod_mul = GetBIGNUM(t, "ModMul");
+  bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
+  bssl::UniquePtr<BIGNUM> b = GetBIGNUM(t, "B");
+  bssl::UniquePtr<BIGNUM> m = GetBIGNUM(t, "M");
+  bssl::UniquePtr<BIGNUM> mod_mul = GetBIGNUM(t, "ModMul");
   if (!a || !b || !m || !mod_mul) {
     return false;
   }
 
-  ScopedBIGNUM ret(BN_new());
+  bssl::UniquePtr<BIGNUM> ret(BN_new());
   if (!ret ||
       !BN_mod_mul(ret.get(), a.get(), b.get(), m.get(), ctx) ||
       !ExpectBIGNUMsEqual(t, "A * B (mod M)", mod_mul.get(), ret.get())) {
@@ -490,8 +490,8 @@
 
   if (BN_is_odd(m.get())) {
     // Reduce |a| and |b| and test the Montgomery version.
-    ScopedBN_MONT_CTX mont(BN_MONT_CTX_new());
-    ScopedBIGNUM a_tmp(BN_new()), b_tmp(BN_new());
+    bssl::UniquePtr<BN_MONT_CTX> mont(BN_MONT_CTX_new());
+    bssl::UniquePtr<BIGNUM> a_tmp(BN_new()), b_tmp(BN_new());
     if (!mont || !a_tmp || !b_tmp ||
         !BN_MONT_CTX_set(mont.get(), m.get(), ctx) ||
         !BN_nnmod(a_tmp.get(), a.get(), m.get(), ctx) ||
@@ -511,15 +511,15 @@
 }
 
 static bool TestModExp(FileTest *t, BN_CTX *ctx) {
-  ScopedBIGNUM a = GetBIGNUM(t, "A");
-  ScopedBIGNUM e = GetBIGNUM(t, "E");
-  ScopedBIGNUM m = GetBIGNUM(t, "M");
-  ScopedBIGNUM mod_exp = GetBIGNUM(t, "ModExp");
+  bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
+  bssl::UniquePtr<BIGNUM> e = GetBIGNUM(t, "E");
+  bssl::UniquePtr<BIGNUM> m = GetBIGNUM(t, "M");
+  bssl::UniquePtr<BIGNUM> mod_exp = GetBIGNUM(t, "ModExp");
   if (!a || !e || !m || !mod_exp) {
     return false;
   }
 
-  ScopedBIGNUM ret(BN_new());
+  bssl::UniquePtr<BIGNUM> ret(BN_new());
   if (!ret ||
       !BN_mod_exp(ret.get(), a.get(), e.get(), m.get(), ctx) ||
       !ExpectBIGNUMsEqual(t, "A ^ E (mod M)", mod_exp.get(), ret.get())) {
@@ -542,14 +542,14 @@
 }
 
 static bool TestExp(FileTest *t, BN_CTX *ctx) {
-  ScopedBIGNUM a = GetBIGNUM(t, "A");
-  ScopedBIGNUM e = GetBIGNUM(t, "E");
-  ScopedBIGNUM exp = GetBIGNUM(t, "Exp");
+  bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
+  bssl::UniquePtr<BIGNUM> e = GetBIGNUM(t, "E");
+  bssl::UniquePtr<BIGNUM> exp = GetBIGNUM(t, "Exp");
   if (!a || !e || !exp) {
     return false;
   }
 
-  ScopedBIGNUM ret(BN_new());
+  bssl::UniquePtr<BIGNUM> ret(BN_new());
   if (!ret ||
       !BN_exp(ret.get(), a.get(), e.get(), ctx) ||
       !ExpectBIGNUMsEqual(t, "A ^ E", exp.get(), ret.get())) {
@@ -560,15 +560,15 @@
 }
 
 static bool TestModSqrt(FileTest *t, BN_CTX *ctx) {
-  ScopedBIGNUM a = GetBIGNUM(t, "A");
-  ScopedBIGNUM p = GetBIGNUM(t, "P");
-  ScopedBIGNUM mod_sqrt = GetBIGNUM(t, "ModSqrt");
+  bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
+  bssl::UniquePtr<BIGNUM> p = GetBIGNUM(t, "P");
+  bssl::UniquePtr<BIGNUM> mod_sqrt = GetBIGNUM(t, "ModSqrt");
   if (!a || !p || !mod_sqrt) {
     return false;
   }
 
-  ScopedBIGNUM ret(BN_new());
-  ScopedBIGNUM ret2(BN_new());
+  bssl::UniquePtr<BIGNUM> ret(BN_new());
+  bssl::UniquePtr<BIGNUM> ret2(BN_new());
   if (!ret ||
       !ret2 ||
       !BN_mod_sqrt(ret.get(), a.get(), p.get(), ctx) ||
@@ -586,14 +586,14 @@
 }
 
 static bool TestModInv(FileTest *t, BN_CTX *ctx) {
-  ScopedBIGNUM a = GetBIGNUM(t, "A");
-  ScopedBIGNUM m = GetBIGNUM(t, "M");
-  ScopedBIGNUM mod_inv = GetBIGNUM(t, "ModInv");
+  bssl::UniquePtr<BIGNUM> a = GetBIGNUM(t, "A");
+  bssl::UniquePtr<BIGNUM> m = GetBIGNUM(t, "M");
+  bssl::UniquePtr<BIGNUM> mod_inv = GetBIGNUM(t, "ModInv");
   if (!a || !m || !mod_inv) {
     return false;
   }
 
-  ScopedBIGNUM ret(BN_new());
+  bssl::UniquePtr<BIGNUM> ret(BN_new());
   if (!ret ||
       !BN_mod_inverse(ret.get(), a.get(), m.get(), ctx) ||
       !ExpectBIGNUMsEqual(t, "inv(A) (mod M)", mod_inv.get(), ret.get())) {
@@ -650,7 +650,7 @@
   memset(zeros, 0, sizeof(zeros));
 
   // Test edge case at 0.
-  ScopedBIGNUM n(BN_new());
+  bssl::UniquePtr<BIGNUM> n(BN_new());
   if (!n || !BN_bn2bin_padded(NULL, 0, n.get())) {
     fprintf(stderr,
             "BN_bn2bin_padded failed to encode 0 in an empty buffer.\n");
@@ -713,7 +713,7 @@
   return true;
 }
 
-static int DecimalToBIGNUM(ScopedBIGNUM *out, const char *in) {
+static int DecimalToBIGNUM(bssl::UniquePtr<BIGNUM> *out, const char *in) {
   BIGNUM *raw = NULL;
   int ret = BN_dec2bn(&raw, in);
   out->reset(raw);
@@ -721,7 +721,7 @@
 }
 
 static bool TestDec2BN(BN_CTX *ctx) {
-  ScopedBIGNUM bn;
+  bssl::UniquePtr<BIGNUM> bn;
   int ret = DecimalToBIGNUM(&bn, "0");
   if (ret != 1 || !BN_is_zero(bn.get()) || BN_is_negative(bn.get())) {
     fprintf(stderr, "BN_dec2bn gave a bad result.\n");
@@ -756,7 +756,7 @@
 }
 
 static bool TestHex2BN(BN_CTX *ctx) {
-  ScopedBIGNUM bn;
+  bssl::UniquePtr<BIGNUM> bn;
   int ret = HexToBIGNUM(&bn, "0");
   if (ret != 1 || !BN_is_zero(bn.get()) || BN_is_negative(bn.get())) {
     fprintf(stderr, "BN_hex2bn gave a bad result.\n");
@@ -790,16 +790,16 @@
   return true;
 }
 
-static ScopedBIGNUM ASCIIToBIGNUM(const char *in) {
+static bssl::UniquePtr<BIGNUM> ASCIIToBIGNUM(const char *in) {
   BIGNUM *raw = NULL;
   if (!BN_asc2bn(&raw, in)) {
     return nullptr;
   }
-  return ScopedBIGNUM(raw);
+  return bssl::UniquePtr<BIGNUM>(raw);
 }
 
 static bool TestASC2BN(BN_CTX *ctx) {
-  ScopedBIGNUM bn = ASCIIToBIGNUM("0");
+  bssl::UniquePtr<BIGNUM> bn = ASCIIToBIGNUM("0");
   if (!bn || !BN_is_zero(bn.get()) || BN_is_negative(bn.get())) {
     fprintf(stderr, "BN_asc2bn gave a bad result.\n");
     return false;
@@ -870,7 +870,7 @@
 
   for (size_t i = 0; i < OPENSSL_ARRAY_SIZE(kMPITests); i++) {
     const MPITest &test = kMPITests[i];
-    ScopedBIGNUM bn(ASCIIToBIGNUM(test.base10));
+    bssl::UniquePtr<BIGNUM> bn(ASCIIToBIGNUM(test.base10));
     const size_t mpi_len = BN_bn2mpi(bn.get(), NULL);
     if (mpi_len > sizeof(scratch)) {
       fprintf(stderr, "MPI test #%u: MPI size is too large to test.\n",
@@ -892,7 +892,7 @@
       return false;
     }
 
-    ScopedBIGNUM bn2(BN_mpi2bn(scratch, mpi_len, NULL));
+    bssl::UniquePtr<BIGNUM> bn2(BN_mpi2bn(scratch, mpi_len, NULL));
     if (bn2.get() == nullptr) {
       fprintf(stderr, "MPI test #%u: failed to parse\n", (unsigned)i);
       return false;
@@ -908,7 +908,7 @@
 }
 
 static bool TestRand() {
-  ScopedBIGNUM bn(BN_new());
+  bssl::UniquePtr<BIGNUM> bn(BN_new());
   if (!bn) {
     return false;
   }
@@ -993,13 +993,13 @@
 
 static bool TestASN1() {
   for (const ASN1Test &test : kASN1Tests) {
-    ScopedBIGNUM bn = ASCIIToBIGNUM(test.value_ascii);
+    bssl::UniquePtr<BIGNUM> bn = ASCIIToBIGNUM(test.value_ascii);
     if (!bn) {
       return false;
     }
 
     // Test that the input is correctly parsed.
-    ScopedBIGNUM bn2(BN_new());
+    bssl::UniquePtr<BIGNUM> bn2(BN_new());
     if (!bn2) {
       return false;
     }
@@ -1025,7 +1025,7 @@
       CBB_cleanup(&cbb);
       return false;
     }
-    ScopedOpenSSLBytes delete_der(der);
+    bssl::UniquePtr<uint8_t> delete_der(der);
     if (der_len != test.der_len ||
         memcmp(der, reinterpret_cast<const uint8_t*>(test.der), der_len) != 0) {
       fprintf(stderr, "Bad serialization.\n");
@@ -1045,7 +1045,7 @@
   }
 
   for (const ASN1InvalidTest &test : kASN1InvalidTests) {
-    ScopedBIGNUM bn(BN_new());
+    bssl::UniquePtr<BIGNUM> bn(BN_new());
     if (!bn) {
       return false;
     }
@@ -1069,7 +1069,7 @@
 
   for (const ASN1Test &test : kASN1BuggyTests) {
     // These broken encodings are rejected by |BN_parse_asn1_unsigned|.
-    ScopedBIGNUM bn(BN_new());
+    bssl::UniquePtr<BIGNUM> bn(BN_new());
     if (!bn) {
       return false;
     }
@@ -1083,7 +1083,7 @@
     ERR_clear_error();
 
     // However |BN_parse_asn1_unsigned_buggy| accepts them.
-    ScopedBIGNUM bn2 = ASCIIToBIGNUM(test.value_ascii);
+    bssl::UniquePtr<BIGNUM> bn2 = ASCIIToBIGNUM(test.value_ascii);
     if (!bn2) {
       return false;
     }
@@ -1101,7 +1101,7 @@
   }
 
   // Serializing negative numbers is not supported.
-  ScopedBIGNUM bn = ASCIIToBIGNUM("-1");
+  bssl::UniquePtr<BIGNUM> bn = ASCIIToBIGNUM("-1");
   if (!bn) {
     return false;
   }
@@ -1120,9 +1120,9 @@
 }
 
 static bool TestNegativeZero(BN_CTX *ctx) {
-  ScopedBIGNUM a(BN_new());
-  ScopedBIGNUM b(BN_new());
-  ScopedBIGNUM c(BN_new());
+  bssl::UniquePtr<BIGNUM> a(BN_new());
+  bssl::UniquePtr<BIGNUM> b(BN_new());
+  bssl::UniquePtr<BIGNUM> c(BN_new());
   if (!a || !b || !c) {
     return false;
   }
@@ -1142,7 +1142,7 @@
   }
 
   for (int consttime = 0; consttime < 2; consttime++) {
-    ScopedBIGNUM numerator(BN_new()), denominator(BN_new());
+    bssl::UniquePtr<BIGNUM> numerator(BN_new()), denominator(BN_new());
     if (!numerator || !denominator) {
       return false;
     }
@@ -1190,8 +1190,8 @@
   // Test that forcibly creating a negative zero does not break |BN_bn2hex| or
   // |BN_bn2dec|.
   a->neg = 1;
-  ScopedOpenSSLString dec(BN_bn2dec(a.get()));
-  ScopedOpenSSLString hex(BN_bn2hex(a.get()));
+  bssl::UniquePtr<char> dec(BN_bn2dec(a.get()));
+  bssl::UniquePtr<char> hex(BN_bn2hex(a.get()));
   if (!dec || !hex ||
       strcmp(dec.get(), "-0") != 0 ||
       strcmp(hex.get(), "-0") != 0) {
@@ -1203,10 +1203,10 @@
 }
 
 static bool TestBadModulus(BN_CTX *ctx) {
-  ScopedBIGNUM a(BN_new());
-  ScopedBIGNUM b(BN_new());
-  ScopedBIGNUM zero(BN_new());
-  ScopedBN_MONT_CTX mont(BN_MONT_CTX_new());
+  bssl::UniquePtr<BIGNUM> a(BN_new());
+  bssl::UniquePtr<BIGNUM> b(BN_new());
+  bssl::UniquePtr<BIGNUM> zero(BN_new());
+  bssl::UniquePtr<BN_MONT_CTX> mont(BN_MONT_CTX_new());
   if (!a || !b || !zero || !mont) {
     return false;
   }
@@ -1290,7 +1290,7 @@
 
 // TestExpModZero tests that 1**0 mod 1 == 0.
 static bool TestExpModZero() {
-  ScopedBIGNUM zero(BN_new()), a(BN_new()), r(BN_new());
+  bssl::UniquePtr<BIGNUM> zero(BN_new()), a(BN_new()), r(BN_new());
   if (!zero || !a || !r ||
       !BN_rand(a.get(), 1024, BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY)) {
     return false;
@@ -1317,7 +1317,7 @@
 static bool TestSmallPrime(BN_CTX *ctx) {
   static const unsigned kBits = 10;
 
-  ScopedBIGNUM r(BN_new());
+  bssl::UniquePtr<BIGNUM> r(BN_new());
   if (!r || !BN_generate_prime_ex(r.get(), static_cast<int>(kBits), 0, NULL,
                                   NULL, NULL)) {
     return false;
@@ -1334,7 +1334,7 @@
 static bool TestCmpWord() {
   static const BN_ULONG kMaxWord = (BN_ULONG)-1;
 
-  ScopedBIGNUM r(BN_new());
+  bssl::UniquePtr<BIGNUM> r(BN_new());
   if (!r ||
       !BN_set_word(r.get(), 0)) {
     return false;
@@ -1416,13 +1416,13 @@
   };
 
   for (const char *test : kBN2DecTests) {
-    ScopedBIGNUM bn;
+    bssl::UniquePtr<BIGNUM> bn;
     int ret = DecimalToBIGNUM(&bn, test);
     if (ret == 0) {
       return false;
     }
 
-    ScopedOpenSSLString dec(BN_bn2dec(bn.get()));
+    bssl::UniquePtr<char> dec(BN_bn2dec(bn.get()));
     if (!dec) {
       fprintf(stderr, "BN_bn2dec failed on %s.\n", test);
       return false;
@@ -1445,7 +1445,7 @@
     return 1;
   }
 
-  ScopedBN_CTX ctx(BN_CTX_new());
+  bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
   if (!ctx) {
     return 1;
   }
diff --git a/crypto/bytestring/bytestring_test.cc b/crypto/bytestring/bytestring_test.cc
index f567fd0..45a2335 100644
--- a/crypto/bytestring/bytestring_test.cc
+++ b/crypto/bytestring/bytestring_test.cc
@@ -27,7 +27,6 @@
 
 #include "internal.h"
 #include "../internal.h"
-#include "../test/scoped_types.h"
 
 namespace bssl {
 
@@ -294,7 +293,7 @@
     return false;
   }
 
-  ScopedOpenSSLBytes scoper(buf);
+  bssl::UniquePtr<uint8_t> scoper(buf);
   return buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len) == 0;
 }
 
@@ -345,7 +344,7 @@
     CBB_cleanup(&cbb);
     return false;
   }
-  ScopedOpenSSLBytes scoper(out_buf);
+  bssl::UniquePtr<uint8_t> scoper(out_buf);
   return out_size == 1 && out_buf[0] == 0;
 }
 
@@ -378,7 +377,7 @@
     return false;
   }
 
-  ScopedOpenSSLBytes scoper(buf);
+  bssl::UniquePtr<uint8_t> scoper(buf);
   return buf_len == sizeof(kExpected) && memcmp(buf, kExpected, buf_len) == 0;
 }
 
@@ -418,7 +417,7 @@
   if (!CBB_finish(cbb.get(), &buf, &buf_len)) {
     return false;
   }
-  ScopedOpenSSLBytes scoper(buf);
+  bssl::UniquePtr<uint8_t> scoper(buf);
 
   static const uint8_t kExpected[] = {
         0xaa,
@@ -464,7 +463,7 @@
     CBB_cleanup(&cbb);
     return false;
   }
-  ScopedOpenSSLBytes scoper(buf);
+  bssl::UniquePtr<uint8_t> scoper(buf);
 
   if (buf_len != 3 ||
       memcmp(buf, "\x01\x01\x02", 3) != 0) {
@@ -488,7 +487,7 @@
     CBB_cleanup(&cbb);
     return false;
   }
-  ScopedOpenSSLBytes scoper(buf);
+  bssl::UniquePtr<uint8_t> scoper(buf);
 
   if (buf_len != sizeof(kExpected) || memcmp(buf, kExpected, buf_len) != 0) {
     return false;
@@ -563,7 +562,7 @@
     fprintf(stderr, "%s: CBS_asn1_ber_to_der failed.\n", name);
     return false;
   }
-  ScopedOpenSSLBytes scoper(out);
+  bssl::UniquePtr<uint8_t> scoper(out);
 
   if (out == NULL) {
     if (ber_len != der_len ||
@@ -676,7 +675,7 @@
     int ok = CBS_get_asn1_implicit_string(&in, &out, &storage,
                                           CBS_ASN1_CONTEXT_SPECIFIC | 0,
                                           CBS_ASN1_OCTETSTRING);
-    ScopedOpenSSLBytes scoper(storage);
+    bssl::UniquePtr<uint8_t> scoper(storage);
 
     if (static_cast<bool>(ok) != test.ok) {
       fprintf(stderr, "CBS_get_asn1_implicit_string unexpectedly %s\n",
@@ -754,7 +753,7 @@
       CBB_cleanup(&cbb);
       return false;
     }
-    ScopedOpenSSLBytes scoper(out);
+    bssl::UniquePtr<uint8_t> scoper(out);
     if (len != test->encoding_len || memcmp(out, test->encoding, len) != 0) {
       return false;
     }
diff --git a/crypto/cmac/cmac_test.cc b/crypto/cmac/cmac_test.cc
index 2496f2a..7cb1df5 100644
--- a/crypto/cmac/cmac_test.cc
+++ b/crypto/cmac/cmac_test.cc
@@ -16,9 +16,10 @@
 
 #include <algorithm>
 
+#include <openssl/cipher.h>
 #include <openssl/cmac.h>
+#include <openssl/mem.h>
 
-#include "../test/scoped_types.h"
 #include "../test/test_util.h"
 
 
@@ -43,7 +44,7 @@
     return 0;
   }
 
-  ScopedCMAC_CTX ctx(CMAC_CTX_new());
+  bssl::UniquePtr<CMAC_CTX> ctx(CMAC_CTX_new());
   if (!ctx || !CMAC_Init(ctx.get(), key, key_len, EVP_aes_128_cbc(), NULL)) {
     fprintf(stderr, "%s: CMAC_Init failed.\n", name);
     return 0;
diff --git a/crypto/curve25519/spake25519_test.cc b/crypto/curve25519/spake25519_test.cc
index d97a860..363b60c 100644
--- a/crypto/curve25519/spake25519_test.cc
+++ b/crypto/curve25519/spake25519_test.cc
@@ -19,18 +19,17 @@
 #include <string.h>
 
 #include <openssl/curve25519.h>
-#include "../test/scoped_types.h"
 
 
 struct SPAKE2Run {
   bool Run() {
-    ScopedSPAKE2_CTX alice(SPAKE2_CTX_new(
+    bssl::UniquePtr<SPAKE2_CTX> alice(SPAKE2_CTX_new(
         spake2_role_alice,
         reinterpret_cast<const uint8_t *>(alice_names.first.data()),
         alice_names.first.size(),
         reinterpret_cast<const uint8_t *>(alice_names.second.data()),
         alice_names.second.size()));
-    ScopedSPAKE2_CTX bob(SPAKE2_CTX_new(
+    bssl::UniquePtr<SPAKE2_CTX> bob(SPAKE2_CTX_new(
         spake2_role_bob,
         reinterpret_cast<const uint8_t *>(bob_names.first.data()),
         bob_names.first.size(),
diff --git a/crypto/dh/dh_test.cc b/crypto/dh/dh_test.cc
index b8bfe46..12984e6 100644
--- a/crypto/dh/dh_test.cc
+++ b/crypto/dh/dh_test.cc
@@ -64,11 +64,10 @@
 #include <openssl/bn.h>
 #include <openssl/c++/bytestring.h>
 #include <openssl/crypto.h>
+#include <openssl/dh.h>
 #include <openssl/err.h>
 #include <openssl/mem.h>
 
-#include "../test/scoped_types.h"
-
 namespace bssl {
 
 static bool RunBasicTests();
@@ -115,7 +114,7 @@
 static bool RunBasicTests() {
   BN_GENCB cb;
   BN_GENCB_set(&cb, &GenerateCallback, stdout);
-  ScopedDH a(DH_new());
+  bssl::UniquePtr<DH> a(DH_new());
   if (!a || !DH_generate_parameters_ex(a.get(), 64, DH_GENERATOR_5, &cb)) {
     return false;
   }
@@ -143,7 +142,7 @@
   BN_print_fp(stdout, a->g);
   printf("\n");
 
-  ScopedDH b(DH_new());
+  bssl::UniquePtr<DH> b(DH_new());
   if (!b) {
     return false;
   }
@@ -437,8 +436,8 @@
   for (unsigned i = 0; i < sizeof(kRFCTestData) / sizeof(RFC5114TestData); i++) {
     const RFC5114TestData *td = kRFCTestData + i;
     /* Set up DH structures setting key components */
-    ScopedDH dhA(td->get_param(nullptr));
-    ScopedDH dhB(td->get_param(nullptr));
+    bssl::UniquePtr<DH> dhA(td->get_param(nullptr));
+    bssl::UniquePtr<DH> dhB(td->get_param(nullptr));
     if (!dhA || !dhB) {
       fprintf(stderr, "Initialisation error RFC5114 set %u\n", i + 1);
       return false;
@@ -513,8 +512,8 @@
 };
 
 static bool TestBadY() {
-  ScopedDH dh(DH_get_2048_224(nullptr));
-  ScopedBIGNUM pub_key(
+  bssl::UniquePtr<DH> dh(DH_get_2048_224(nullptr));
+  bssl::UniquePtr<BIGNUM> pub_key(
       BN_bin2bn(kRFC5114_2048_224BadY, sizeof(kRFC5114_2048_224BadY), nullptr));
   if (!dh || !pub_key || !DH_generate_key(dh.get())) {
     return false;
@@ -544,7 +543,7 @@
   if (!BN_hex2bn(&hex_bn, hex)) {
     return false;
   }
-  ScopedBIGNUM free_hex_bn(hex_bn);
+  bssl::UniquePtr<BIGNUM> free_hex_bn(hex_bn);
   return BN_cmp(bn, hex_bn) == 0;
 }
 
@@ -560,7 +559,7 @@
 
   CBS cbs;
   CBS_init(&cbs, kParams, sizeof(kParams));
-  ScopedDH dh(DH_parse_parameters(&cbs));
+  bssl::UniquePtr<DH> dh(DH_parse_parameters(&cbs));
   if (!dh || CBS_len(&cbs) != 0 ||
       !BIGNUMEqualsHex(
           dh->p,
@@ -577,7 +576,7 @@
       !CBB_finish(cbb.get(), &der, &der_len)) {
     return false;
   }
-  ScopedOpenSSLBytes free_der(der);
+  bssl::UniquePtr<uint8_t> free_der(der);
   if (der_len != sizeof(kParams) || memcmp(der, kParams, der_len) != 0) {
     return false;
   }
@@ -619,7 +618,7 @@
       !CBB_finish(cbb.get(), &der, &der_len)) {
     return false;
   }
-  ScopedOpenSSLBytes free_der2(der);
+  bssl::UniquePtr<uint8_t> free_der2(der);
   if (der_len != sizeof(kParamsDSA) || memcmp(der, kParamsDSA, der_len) != 0) {
     return false;
   }
@@ -628,7 +627,7 @@
 }
 
 static bool TestRFC3526() {
-  ScopedBIGNUM bn(BN_get_rfc3526_prime_1536(nullptr));
+  bssl::UniquePtr<BIGNUM> bn(BN_get_rfc3526_prime_1536(nullptr));
   if (!bn) {
     return false;
   }
diff --git a/crypto/ec/ec_test.cc b/crypto/ec/ec_test.cc
index ca0e140..9648d57 100644
--- a/crypto/ec/ec_test.cc
+++ b/crypto/ec/ec_test.cc
@@ -18,12 +18,13 @@
 #include <vector>
 
 #include <openssl/c++/bytestring.h>
+#include <openssl/bn.h>
 #include <openssl/crypto.h>
 #include <openssl/ec_key.h>
 #include <openssl/err.h>
 #include <openssl/mem.h>
+#include <openssl/nid.h>
 
-#include "../test/scoped_types.h"
 
 namespace bssl {
 
@@ -97,10 +98,11 @@
 
 // DecodeECPrivateKey decodes |in| as an ECPrivateKey structure and returns the
 // result or nullptr on error.
-static ScopedEC_KEY DecodeECPrivateKey(const uint8_t *in, size_t in_len) {
+static bssl::UniquePtr<EC_KEY> DecodeECPrivateKey(const uint8_t *in,
+                                                  size_t in_len) {
   CBS cbs;
   CBS_init(&cbs, in, in_len);
-  ScopedEC_KEY ret(EC_KEY_parse_private_key(&cbs, NULL));
+  bssl::UniquePtr<EC_KEY> ret(EC_KEY_parse_private_key(&cbs, NULL));
   if (!ret || CBS_len(&cbs) != 0) {
     return nullptr;
   }
@@ -124,7 +126,7 @@
 }
 
 static bool Testd2i_ECPrivateKey() {
-  ScopedEC_KEY key = DecodeECPrivateKey(kECKeyWithoutPublic,
+  bssl::UniquePtr<EC_KEY> key = DecodeECPrivateKey(kECKeyWithoutPublic,
                                         sizeof(kECKeyWithoutPublic));
   if (!key) {
     fprintf(stderr, "Failed to parse private key.\n");
@@ -152,8 +154,8 @@
     return false;
   }
 
-  ScopedBIGNUM x(BN_new());
-  ScopedBIGNUM y(BN_new());
+  bssl::UniquePtr<BIGNUM> x(BN_new());
+  bssl::UniquePtr<BIGNUM> y(BN_new());
   if (!x || !y) {
     return false;
   }
@@ -162,8 +164,8 @@
     fprintf(stderr, "Failed to get public key in affine coordinates.\n");
     return false;
   }
-  ScopedOpenSSLString x_hex(BN_bn2hex(x.get()));
-  ScopedOpenSSLString y_hex(BN_bn2hex(y.get()));
+  bssl::UniquePtr<char> x_hex(BN_bn2hex(x.get()));
+  bssl::UniquePtr<char> y_hex(BN_bn2hex(y.get()));
   if (!x_hex || !y_hex) {
     return false;
   }
@@ -182,7 +184,7 @@
 
 static bool TestZeroPadding() {
   // Check that the correct encoding round-trips.
-  ScopedEC_KEY key = DecodeECPrivateKey(kECKeyWithZeros,
+  bssl::UniquePtr<EC_KEY> key = DecodeECPrivateKey(kECKeyWithZeros,
                                         sizeof(kECKeyWithZeros));
   std::vector<uint8_t> out;
   if (!key || !EncodeECPrivateKey(&out, key.get())) {
@@ -214,7 +216,7 @@
 
 static bool TestSpecifiedCurve() {
   // Test keys with specified curves may be decoded.
-  ScopedEC_KEY key =
+  bssl::UniquePtr<EC_KEY> key =
       DecodeECPrivateKey(kECKeySpecifiedCurve, sizeof(kECKeySpecifiedCurve));
   if (!key) {
     ERR_print_errors_fp(stderr);
@@ -245,7 +247,7 @@
 }
 
 static bool TestSetAffine(const int nid) {
-  ScopedEC_KEY key(EC_KEY_new_by_curve_name(nid));
+  bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(nid));
   if (!key) {
     return false;
   }
@@ -265,8 +267,8 @@
     return false;
   }
 
-  ScopedBIGNUM x(BN_new());
-  ScopedBIGNUM y(BN_new());
+  bssl::UniquePtr<BIGNUM> x(BN_new());
+  bssl::UniquePtr<BIGNUM> y(BN_new());
   if (!EC_POINT_get_affine_coordinates_GFp(group,
                                            EC_KEY_get0_public_key(key.get()),
                                            x.get(), y.get(), nullptr)) {
@@ -276,7 +278,7 @@
     return false;
   }
 
-  ScopedEC_POINT point(EC_POINT_new(group));
+  auto point = bssl::UniquePtr<EC_POINT>(EC_POINT_new(group));
   if (!point) {
     return false;
   }
@@ -294,7 +296,7 @@
     return false;
   }
 
-  ScopedEC_POINT invalid_point(EC_POINT_new(group));
+  bssl::UniquePtr<EC_POINT> invalid_point(EC_POINT_new(group));
   if (!invalid_point) {
     return false;
   }
@@ -314,7 +316,7 @@
 
 static bool TestArbitraryCurve() {
   // Make a P-256 key and extract the affine coordinates.
-  ScopedEC_KEY key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
+  bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
   if (!key || !EC_KEY_generate_key(key.get())) {
     return false;
   }
@@ -350,25 +352,25 @@
       0xff, 0xff, 0xff, 0xff, 0xff, 0xbc, 0xe6, 0xfa, 0xad, 0xa7, 0x17,
       0x9e, 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51,
   };
-  ScopedBN_CTX ctx(BN_CTX_new());
-  ScopedBIGNUM p(BN_bin2bn(kP, sizeof(kP), nullptr));
-  ScopedBIGNUM a(BN_bin2bn(kA, sizeof(kA), nullptr));
-  ScopedBIGNUM b(BN_bin2bn(kB, sizeof(kB), nullptr));
-  ScopedBIGNUM gx(BN_bin2bn(kX, sizeof(kX), nullptr));
-  ScopedBIGNUM gy(BN_bin2bn(kY, sizeof(kY), nullptr));
-  ScopedBIGNUM order(BN_bin2bn(kOrder, sizeof(kOrder), nullptr));
-  ScopedBIGNUM cofactor(BN_new());
+  bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
+  bssl::UniquePtr<BIGNUM> p(BN_bin2bn(kP, sizeof(kP), nullptr));
+  bssl::UniquePtr<BIGNUM> a(BN_bin2bn(kA, sizeof(kA), nullptr));
+  bssl::UniquePtr<BIGNUM> b(BN_bin2bn(kB, sizeof(kB), nullptr));
+  bssl::UniquePtr<BIGNUM> gx(BN_bin2bn(kX, sizeof(kX), nullptr));
+  bssl::UniquePtr<BIGNUM> gy(BN_bin2bn(kY, sizeof(kY), nullptr));
+  bssl::UniquePtr<BIGNUM> order(BN_bin2bn(kOrder, sizeof(kOrder), nullptr));
+  bssl::UniquePtr<BIGNUM> cofactor(BN_new());
   if (!ctx || !p || !a || !b || !gx || !gy || !order || !cofactor ||
       !BN_set_word(cofactor.get(), 1)) {
     return false;
   }
 
-  ScopedEC_GROUP group(
+  bssl::UniquePtr<EC_GROUP> group(
       EC_GROUP_new_curve_GFp(p.get(), a.get(), b.get(), ctx.get()));
   if (!group) {
     return false;
   }
-  ScopedEC_POINT generator(EC_POINT_new(group.get()));
+  bssl::UniquePtr<EC_POINT> generator(EC_POINT_new(group.get()));
   if (!generator ||
       !EC_POINT_set_affine_coordinates_GFp(group.get(), generator.get(),
                                            gx.get(), gy.get(), ctx.get()) ||
@@ -383,9 +385,9 @@
   }
 
   // Copy |key| to |key2| using |group|.
-  ScopedEC_KEY key2(EC_KEY_new());
-  ScopedEC_POINT point(EC_POINT_new(group.get()));
-  ScopedBIGNUM x(BN_new()), y(BN_new());
+  bssl::UniquePtr<EC_KEY> key2(EC_KEY_new());
+  bssl::UniquePtr<EC_POINT> point(EC_POINT_new(group.get()));
+  bssl::UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
   if (!key2 || !point || !x || !y ||
       !EC_KEY_set_group(key2.get(), group.get()) ||
       !EC_KEY_set_private_key(key2.get(), EC_KEY_get0_private_key(key.get())) ||
@@ -409,7 +411,7 @@
 }
 
 static bool TestAddingEqualPoints(int nid) {
-  ScopedEC_KEY key(EC_KEY_new_by_curve_name(nid));
+  bssl::UniquePtr<EC_KEY> key(EC_KEY_new_by_curve_name(nid));
   if (!key) {
     return false;
   }
@@ -422,10 +424,10 @@
     return false;
   }
 
-  ScopedEC_POINT p1(EC_POINT_new(group));
-  ScopedEC_POINT p2(EC_POINT_new(group));
-  ScopedEC_POINT double_p1(EC_POINT_new(group));
-  ScopedEC_POINT p1_plus_p2(EC_POINT_new(group));
+  bssl::UniquePtr<EC_POINT> p1(EC_POINT_new(group));
+  bssl::UniquePtr<EC_POINT> p2(EC_POINT_new(group));
+  bssl::UniquePtr<EC_POINT> double_p1(EC_POINT_new(group));
+  bssl::UniquePtr<EC_POINT> p1_plus_p2(EC_POINT_new(group));
   if (!p1 || !p2 || !double_p1 || !p1_plus_p2) {
     return false;
   }
@@ -437,7 +439,7 @@
     return false;
   }
 
-  ScopedBN_CTX ctx(BN_CTX_new());
+  bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
   if (!ctx) {
     return false;
   }
diff --git a/crypto/ecdh/ecdh_test.cc b/crypto/ecdh/ecdh_test.cc
index 95f1402..a02fd22 100644
--- a/crypto/ecdh/ecdh_test.cc
+++ b/crypto/ecdh/ecdh_test.cc
@@ -21,59 +21,60 @@
 #include <openssl/ec.h>
 #include <openssl/ec_key.h>
 #include <openssl/ecdh.h>
+#include <openssl/nid.h>
 
 #include "../test/file_test.h"
-#include "../test/scoped_types.h"
 
 
-static ScopedEC_GROUP GetCurve(FileTest *t, const char *key) {
+static bssl::UniquePtr<EC_GROUP> GetCurve(FileTest *t, const char *key) {
   std::string curve_name;
   if (!t->GetAttribute(&curve_name, key)) {
     return nullptr;
   }
 
   if (curve_name == "P-224") {
-    return ScopedEC_GROUP(EC_GROUP_new_by_curve_name(NID_secp224r1));
+    return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp224r1));
   }
   if (curve_name == "P-256") {
-    return ScopedEC_GROUP(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
+    return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(
+        NID_X9_62_prime256v1));
   }
   if (curve_name == "P-384") {
-    return ScopedEC_GROUP(EC_GROUP_new_by_curve_name(NID_secp384r1));
+    return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp384r1));
   }
   if (curve_name == "P-521") {
-    return ScopedEC_GROUP(EC_GROUP_new_by_curve_name(NID_secp521r1));
+    return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp521r1));
   }
 
   t->PrintLine("Unknown curve '%s'", curve_name.c_str());
   return nullptr;
 }
 
-static ScopedBIGNUM GetBIGNUM(FileTest *t, const char *key) {
+static bssl::UniquePtr<BIGNUM> GetBIGNUM(FileTest *t, const char *key) {
   std::vector<uint8_t> bytes;
   if (!t->GetBytes(&bytes, key)) {
     return nullptr;
   }
 
-  return ScopedBIGNUM(BN_bin2bn(bytes.data(), bytes.size(), nullptr));
+  return bssl::UniquePtr<BIGNUM>(BN_bin2bn(bytes.data(), bytes.size(), nullptr));
 }
 
 static bool TestECDH(FileTest *t, void *arg) {
-  ScopedEC_GROUP group = GetCurve(t, "Curve");
-  ScopedBIGNUM priv_key = GetBIGNUM(t, "Private");
-  ScopedBIGNUM x = GetBIGNUM(t, "X");
-  ScopedBIGNUM y = GetBIGNUM(t, "Y");
-  ScopedBIGNUM peer_x = GetBIGNUM(t, "PeerX");
-  ScopedBIGNUM peer_y = GetBIGNUM(t, "PeerY");
+  bssl::UniquePtr<EC_GROUP> group = GetCurve(t, "Curve");
+  bssl::UniquePtr<BIGNUM> priv_key = GetBIGNUM(t, "Private");
+  bssl::UniquePtr<BIGNUM> x = GetBIGNUM(t, "X");
+  bssl::UniquePtr<BIGNUM> y = GetBIGNUM(t, "Y");
+  bssl::UniquePtr<BIGNUM> peer_x = GetBIGNUM(t, "PeerX");
+  bssl::UniquePtr<BIGNUM> peer_y = GetBIGNUM(t, "PeerY");
   std::vector<uint8_t> z;
   if (!group || !priv_key || !x || !y || !peer_x || !peer_y ||
       !t->GetBytes(&z, "Z")) {
     return false;
   }
 
-  ScopedEC_KEY key(EC_KEY_new());
-  ScopedEC_POINT pub_key(EC_POINT_new(group.get()));
-  ScopedEC_POINT peer_pub_key(EC_POINT_new(group.get()));
+  bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
+  bssl::UniquePtr<EC_POINT> pub_key(EC_POINT_new(group.get()));
+  bssl::UniquePtr<EC_POINT> peer_pub_key(EC_POINT_new(group.get()));
   if (!key || !pub_key || !peer_pub_key ||
       !EC_KEY_set_group(key.get(), group.get()) ||
       !EC_KEY_set_private_key(key.get(), priv_key.get()) ||
diff --git a/crypto/ecdsa/ecdsa_sign_test.cc b/crypto/ecdsa/ecdsa_sign_test.cc
index 683f0f0..ee95773 100644
--- a/crypto/ecdsa/ecdsa_sign_test.cc
+++ b/crypto/ecdsa/ecdsa_sign_test.cc
@@ -21,59 +21,60 @@
 #include <openssl/ec.h>
 #include <openssl/ec_key.h>
 #include <openssl/ecdsa.h>
+#include <openssl/nid.h>
 
 #include "../test/file_test.h"
-#include "../test/scoped_types.h"
 
 
-static ScopedEC_GROUP GetCurve(FileTest *t, const char *key) {
+static bssl::UniquePtr<EC_GROUP> GetCurve(FileTest *t, const char *key) {
   std::string curve_name;
   if (!t->GetAttribute(&curve_name, key)) {
     return nullptr;
   }
 
   if (curve_name == "P-224") {
-    return ScopedEC_GROUP(EC_GROUP_new_by_curve_name(NID_secp224r1));
+    return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp224r1));
   }
   if (curve_name == "P-256") {
-    return ScopedEC_GROUP(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
+    return bssl::UniquePtr<EC_GROUP>(
+        EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
   }
   if (curve_name == "P-384") {
-    return ScopedEC_GROUP(EC_GROUP_new_by_curve_name(NID_secp384r1));
+    return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp384r1));
   }
   if (curve_name == "P-521") {
-    return ScopedEC_GROUP(EC_GROUP_new_by_curve_name(NID_secp521r1));
+    return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp521r1));
   }
 
   t->PrintLine("Unknown curve '%s'", curve_name.c_str());
   return nullptr;
 }
 
-static ScopedBIGNUM GetBIGNUM(FileTest *t, const char *key) {
+static bssl::UniquePtr<BIGNUM> GetBIGNUM(FileTest *t, const char *key) {
   std::vector<uint8_t> bytes;
   if (!t->GetBytes(&bytes, key)) {
     return nullptr;
   }
 
-  return ScopedBIGNUM(BN_bin2bn(bytes.data(), bytes.size(), nullptr));
+  return bssl::UniquePtr<BIGNUM>(BN_bin2bn(bytes.data(), bytes.size(), nullptr));
 }
 
 static bool TestECDSASign(FileTest *t, void *arg) {
-  ScopedEC_GROUP group = GetCurve(t, "Curve");
-  ScopedBIGNUM priv_key = GetBIGNUM(t, "Private");
-  ScopedBIGNUM x = GetBIGNUM(t, "X");
-  ScopedBIGNUM y = GetBIGNUM(t, "Y");
-  ScopedBIGNUM k = GetBIGNUM(t, "K");
-  ScopedBIGNUM r = GetBIGNUM(t, "R");
-  ScopedBIGNUM s = GetBIGNUM(t, "S");
+  bssl::UniquePtr<EC_GROUP> group = GetCurve(t, "Curve");
+  bssl::UniquePtr<BIGNUM> priv_key = GetBIGNUM(t, "Private");
+  bssl::UniquePtr<BIGNUM> x = GetBIGNUM(t, "X");
+  bssl::UniquePtr<BIGNUM> y = GetBIGNUM(t, "Y");
+  bssl::UniquePtr<BIGNUM> k = GetBIGNUM(t, "K");
+  bssl::UniquePtr<BIGNUM> r = GetBIGNUM(t, "R");
+  bssl::UniquePtr<BIGNUM> s = GetBIGNUM(t, "S");
   std::vector<uint8_t> digest;
   if (!group || !priv_key || !x || !y || !k || !r || !s ||
       !t->GetBytes(&digest, "Digest")) {
     return false;
   }
 
-  ScopedEC_KEY key(EC_KEY_new());
-  ScopedEC_POINT pub_key(EC_POINT_new(group.get()));
+  bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
+  bssl::UniquePtr<EC_POINT> pub_key(EC_POINT_new(group.get()));
   if (!key || !pub_key ||
       !EC_KEY_set_group(key.get(), group.get()) ||
       !EC_KEY_set_private_key(key.get(), priv_key.get()) ||
@@ -85,15 +86,15 @@
   }
 
   // |ECDSA_do_sign_ex| expects |k| to already be inverted.
-  ScopedBN_CTX ctx(BN_CTX_new());
+  bssl::UniquePtr<BN_CTX> ctx(BN_CTX_new());
   if (!ctx ||
       !BN_mod_inverse(k.get(), k.get(), EC_GROUP_get0_order(group.get()),
                       ctx.get())) {
     return false;
   }
 
-  ScopedECDSA_SIG sig(ECDSA_do_sign_ex(digest.data(), digest.size(), k.get(),
-                                       r.get(), key.get()));
+  bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_do_sign_ex(digest.data(), digest.size(), k.get(),
+                                 r.get(), key.get()));
   if (!sig) {
     return false;
   }
diff --git a/crypto/ecdsa/ecdsa_test.cc b/crypto/ecdsa/ecdsa_test.cc
index 8d7827d..7c68de4 100644
--- a/crypto/ecdsa/ecdsa_test.cc
+++ b/crypto/ecdsa/ecdsa_test.cc
@@ -62,8 +62,6 @@
 #include <openssl/nid.h>
 #include <openssl/rand.h>
 
-#include "../test/scoped_types.h"
-
 enum Api {
   kEncodedApi,
   kRawApi,
@@ -82,7 +80,7 @@
       if (!ECDSA_SIG_to_bytes(&der, &der_len, ecdsa_sig)) {
         return false;
       }
-      ScopedOpenSSLBytes delete_der(der);
+      bssl::UniquePtr<uint8_t> delete_der(der);
       actual_result = ECDSA_verify(0, digest, digest_len, der, der_len, eckey);
       break;
     }
@@ -171,7 +169,7 @@
     fprintf(out, "%s: ", kCurves[n].name);
 
     int nid = kCurves[n].nid;
-    ScopedEC_GROUP group(EC_GROUP_new_by_curve_name(nid));
+    bssl::UniquePtr<EC_GROUP> group(EC_GROUP_new_by_curve_name(nid));
     if (!group) {
       fprintf(out, " failed\n");
       return false;
@@ -184,14 +182,14 @@
     }
 
     // Create a new ECDSA key.
-    ScopedEC_KEY eckey(EC_KEY_new());
+    bssl::UniquePtr<EC_KEY> eckey(EC_KEY_new());
     if (!eckey || !EC_KEY_set_group(eckey.get(), group.get()) ||
         !EC_KEY_generate_key(eckey.get())) {
       fprintf(out, " failed\n");
       return false;
     }
     // Create a second key.
-    ScopedEC_KEY wrong_eckey(EC_KEY_new());
+    bssl::UniquePtr<EC_KEY> wrong_eckey(EC_KEY_new());
     if (!wrong_eckey || !EC_KEY_set_group(wrong_eckey.get(), group.get()) ||
         !EC_KEY_generate_key(wrong_eckey.get())) {
       fprintf(out, " failed\n");
@@ -253,7 +251,7 @@
     fprintf(out, ".");
     fflush(out);
     // Verify a tampered signature.
-    ScopedECDSA_SIG ecdsa_sig(ECDSA_SIG_from_bytes(
+    bssl::UniquePtr<ECDSA_SIG> ecdsa_sig(ECDSA_SIG_from_bytes(
         signature.data(), signature.size()));
     if (!ecdsa_sig ||
         !TestTamperedSig(out, kEncodedApi, digest, 20, ecdsa_sig.get(),
@@ -313,7 +311,7 @@
 
 static bool TestECDSA_SIG_max_len(size_t order_len) {
   /* Create the largest possible |ECDSA_SIG| of the given constraints. */
-  ScopedECDSA_SIG sig(ECDSA_SIG_new());
+  bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
   if (!sig) {
     return false;
   }
@@ -328,7 +326,7 @@
   if (!ECDSA_SIG_to_bytes(&der, &der_len, sig.get())) {
     return false;
   }
-  ScopedOpenSSLBytes delete_der(der);
+  bssl::UniquePtr<uint8_t> delete_der(der);
 
   size_t max_len = ECDSA_SIG_max_len(order_len);
   if (max_len != der_len) {
diff --git a/crypto/ecdsa/ecdsa_verify_test.cc b/crypto/ecdsa/ecdsa_verify_test.cc
index 7ef2d29..18340e2 100644
--- a/crypto/ecdsa/ecdsa_verify_test.cc
+++ b/crypto/ecdsa/ecdsa_verify_test.cc
@@ -21,58 +21,59 @@
 #include <openssl/ec.h>
 #include <openssl/ec_key.h>
 #include <openssl/ecdsa.h>
+#include <openssl/nid.h>
 
 #include "../test/file_test.h"
-#include "../test/scoped_types.h"
 
 
-static ScopedEC_GROUP GetCurve(FileTest *t, const char *key) {
+static bssl::UniquePtr<EC_GROUP> GetCurve(FileTest *t, const char *key) {
   std::string curve_name;
   if (!t->GetAttribute(&curve_name, key)) {
     return nullptr;
   }
 
   if (curve_name == "P-224") {
-    return ScopedEC_GROUP(EC_GROUP_new_by_curve_name(NID_secp224r1));
+    return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp224r1));
   }
   if (curve_name == "P-256") {
-    return ScopedEC_GROUP(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
+    return bssl::UniquePtr<EC_GROUP>(
+        EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
   }
   if (curve_name == "P-384") {
-    return ScopedEC_GROUP(EC_GROUP_new_by_curve_name(NID_secp384r1));
+    return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp384r1));
   }
   if (curve_name == "P-521") {
-    return ScopedEC_GROUP(EC_GROUP_new_by_curve_name(NID_secp521r1));
+    return bssl::UniquePtr<EC_GROUP>(EC_GROUP_new_by_curve_name(NID_secp521r1));
   }
 
   t->PrintLine("Unknown curve '%s'", curve_name.c_str());
   return nullptr;
 }
 
-static ScopedBIGNUM GetBIGNUM(FileTest *t, const char *key) {
+static bssl::UniquePtr<BIGNUM> GetBIGNUM(FileTest *t, const char *key) {
   std::vector<uint8_t> bytes;
   if (!t->GetBytes(&bytes, key)) {
     return nullptr;
   }
 
-  return ScopedBIGNUM(BN_bin2bn(bytes.data(), bytes.size(), nullptr));
+  return bssl::UniquePtr<BIGNUM>(BN_bin2bn(bytes.data(), bytes.size(), nullptr));
 }
 
 static bool TestECDSASign(FileTest *t, void *arg) {
-  ScopedEC_GROUP group = GetCurve(t, "Curve");
-  ScopedBIGNUM x = GetBIGNUM(t, "X");
-  ScopedBIGNUM y = GetBIGNUM(t, "Y");
-  ScopedBIGNUM r = GetBIGNUM(t, "R");
-  ScopedBIGNUM s = GetBIGNUM(t, "S");
+  bssl::UniquePtr<EC_GROUP> group = GetCurve(t, "Curve");
+  bssl::UniquePtr<BIGNUM> x = GetBIGNUM(t, "X");
+  bssl::UniquePtr<BIGNUM> y = GetBIGNUM(t, "Y");
+  bssl::UniquePtr<BIGNUM> r = GetBIGNUM(t, "R");
+  bssl::UniquePtr<BIGNUM> s = GetBIGNUM(t, "S");
   std::vector<uint8_t> digest;
   if (!group || !x || !y || !r || !s ||
       !t->GetBytes(&digest, "Digest")) {
     return false;
   }
 
-  ScopedEC_KEY key(EC_KEY_new());
-  ScopedEC_POINT pub_key(EC_POINT_new(group.get()));
-  ScopedECDSA_SIG sig(ECDSA_SIG_new());
+  bssl::UniquePtr<EC_KEY> key(EC_KEY_new());
+  bssl::UniquePtr<EC_POINT> pub_key(EC_POINT_new(group.get()));
+  bssl::UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
   if (!key || !pub_key || !sig ||
       !EC_KEY_set_group(key.get(), group.get()) ||
       !EC_POINT_set_affine_coordinates_GFp(group.get(), pub_key.get(), x.get(),
diff --git a/crypto/evp/evp_extra_test.cc b/crypto/evp/evp_extra_test.cc
index b2c519e..125dc03 100644
--- a/crypto/evp/evp_extra_test.cc
+++ b/crypto/evp/evp_extra_test.cc
@@ -24,10 +24,9 @@
 #include <openssl/c++/digest.h>
 #include <openssl/crypto.h>
 #include <openssl/err.h>
+#include <openssl/pkcs8.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
@@ -357,13 +356,13 @@
     0x48, 0x30, 0x01, 0xaa, 0x02, 0x86, 0xc0, 0x30, 0xdf, 0xe9, 0x80,
 };
 
-static ScopedEVP_PKEY LoadExampleRSAKey() {
-  ScopedRSA rsa(RSA_private_key_from_bytes(kExampleRSAKeyDER,
+static bssl::UniquePtr<EVP_PKEY> LoadExampleRSAKey() {
+  bssl::UniquePtr<RSA> rsa(RSA_private_key_from_bytes(kExampleRSAKeyDER,
                                            sizeof(kExampleRSAKeyDER)));
   if (!rsa) {
     return nullptr;
   }
-  ScopedEVP_PKEY pkey(EVP_PKEY_new());
+  bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKEY_new());
   if (!pkey || !EVP_PKEY_set1_RSA(pkey.get(), rsa.get())) {
     return nullptr;
   }
@@ -371,7 +370,7 @@
 }
 
 static bool TestEVP_DigestSignInit(void) {
-  ScopedEVP_PKEY pkey = LoadExampleRSAKey();
+  bssl::UniquePtr<EVP_PKEY> pkey = LoadExampleRSAKey();
   ScopedEVP_MD_CTX md_ctx;
   if (!pkey ||
       !EVP_DigestSignInit(md_ctx.get(), NULL, EVP_sha256(), NULL, pkey.get()) ||
@@ -409,7 +408,7 @@
 }
 
 static bool TestEVP_DigestVerifyInit(void) {
-  ScopedEVP_PKEY pkey = LoadExampleRSAKey();
+  bssl::UniquePtr<EVP_PKEY> pkey = LoadExampleRSAKey();
   ScopedEVP_MD_CTX md_ctx;
   if (!pkey ||
       !EVP_DigestVerifyInit(md_ctx.get(), NULL, EVP_sha256(), NULL,
@@ -422,12 +421,12 @@
 }
 
 static bool TestVerifyRecover() {
-  ScopedEVP_PKEY pkey = LoadExampleRSAKey();
+  bssl::UniquePtr<EVP_PKEY> pkey = LoadExampleRSAKey();
   if (!pkey) {
     return false;
   }
 
-  ScopedRSA rsa(EVP_PKEY_get1_RSA(pkey.get()));
+  bssl::UniquePtr<RSA> rsa(EVP_PKEY_get1_RSA(pkey.get()));
   if (!rsa) {
     return false;
   }
@@ -444,7 +443,7 @@
   }
 
   size_t out_len;
-  ScopedEVP_PKEY_CTX ctx(EVP_PKEY_CTX_new(pkey.get(), nullptr));
+  bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new(pkey.get(), nullptr));
   if (!EVP_PKEY_verify_recover_init(ctx.get()) ||
       !EVP_PKEY_CTX_set_rsa_padding(ctx.get(), RSA_PKCS1_PADDING) ||
       !EVP_PKEY_CTX_set_signature_md(ctx.get(), EVP_sha256()) ||
@@ -502,7 +501,7 @@
 static bool TestValidPrivateKey(const uint8_t *input, size_t input_len,
                                 int expected_id) {
   const uint8_t *p = input;
-  ScopedEVP_PKEY pkey(d2i_AutoPrivateKey(NULL, &p, input_len));
+  bssl::UniquePtr<EVP_PKEY> pkey(d2i_AutoPrivateKey(NULL, &p, input_len));
   if (!pkey || p != input + input_len) {
     fprintf(stderr, "d2i_AutoPrivateKey failed\n");
     return false;
@@ -556,7 +555,7 @@
   }
 
   const uint8_t *p = kInvalidPrivateKey;
-  ScopedEVP_PKEY pkey(d2i_AutoPrivateKey(NULL, &p, sizeof(kInvalidPrivateKey)));
+  bssl::UniquePtr<EVP_PKEY> pkey(d2i_AutoPrivateKey(NULL, &p, sizeof(kInvalidPrivateKey)));
   if (pkey) {
     fprintf(stderr, "Parsed invalid private key\n");
     return false;
@@ -569,14 +568,14 @@
 // TestEVP_PKCS82PKEY tests loading a bad key in PKCS8 format.
 static bool TestEVP_PKCS82PKEY(void) {
   const uint8_t *derp = kExampleBadECKeyDER;
-  ScopedPKCS8_PRIV_KEY_INFO p8inf(
+  bssl::UniquePtr<PKCS8_PRIV_KEY_INFO> p8inf(
       d2i_PKCS8_PRIV_KEY_INFO(NULL, &derp, sizeof(kExampleBadECKeyDER)));
   if (!p8inf || derp != kExampleBadECKeyDER + sizeof(kExampleBadECKeyDER)) {
     fprintf(stderr, "Failed to parse key\n");
     return false;
   }
 
-  ScopedEVP_PKEY pkey(EVP_PKCS82PKEY(p8inf.get()));
+  bssl::UniquePtr<EVP_PKEY> pkey(EVP_PKCS82PKEY(p8inf.get()));
   if (pkey) {
     fprintf(stderr, "Imported invalid EC key\n");
     return false;
@@ -588,7 +587,7 @@
 
 // TestEVPMarshalEmptyPublicKey tests |EVP_marshal_public_key| on an empty key.
 static bool TestEVPMarshalEmptyPublicKey(void) {
-  ScopedEVP_PKEY empty(EVP_PKEY_new());
+  bssl::UniquePtr<EVP_PKEY> empty(EVP_PKEY_new());
   if (!empty) {
     return false;
   }
@@ -608,7 +607,7 @@
 // Testd2i_PrivateKey tests |d2i_PrivateKey|.
 static bool Testd2i_PrivateKey(void) {
   const uint8_t *derp = kExampleRSAKeyDER;
-  ScopedEVP_PKEY pkey(d2i_PrivateKey(EVP_PKEY_RSA, nullptr, &derp,
+  bssl::UniquePtr<EVP_PKEY> pkey(d2i_PrivateKey(EVP_PKEY_RSA, nullptr, &derp,
                                      sizeof(kExampleRSAKeyDER)));
   if (!pkey || derp != kExampleRSAKeyDER + sizeof(kExampleRSAKeyDER)) {
     fprintf(stderr, "Failed to import raw RSA key.\n");
diff --git a/crypto/evp/evp_test.cc b/crypto/evp/evp_test.cc
index 9c8735b..58d3ebd 100644
--- a/crypto/evp/evp_test.cc
+++ b/crypto/evp/evp_test.cc
@@ -74,7 +74,6 @@
 #include <openssl/err.h>
 
 #include "../test/file_test.h"
-#include "../test/scoped_types.h"
 
 namespace bssl {
 
@@ -115,7 +114,7 @@
   return EVP_PKEY_NONE;
 }
 
-using KeyMap = std::map<std::string, ScopedEVP_PKEY>;
+using KeyMap = std::map<std::string, bssl::UniquePtr<EVP_PKEY>>;
 
 static bool ImportKey(FileTest *t, KeyMap *key_map,
                       EVP_PKEY *(*parse_func)(CBS *cbs),
@@ -127,7 +126,7 @@
 
   CBS cbs;
   CBS_init(&cbs, input.data(), input.size());
-  ScopedEVP_PKEY pkey(parse_func(&cbs));
+  bssl::UniquePtr<EVP_PKEY> pkey(parse_func(&cbs));
   if (!pkey) {
     return false;
   }
@@ -150,7 +149,7 @@
       !CBB_finish(cbb.get(), &der, &der_len)) {
     return false;
   }
-  ScopedOpenSSLBytes free_der(der);
+  bssl::UniquePtr<uint8_t> free_der(der);
 
   std::vector<uint8_t> output = input;
   if (t->HasAttribute("Output") &&
@@ -215,7 +214,7 @@
   }
 
   // Set up the EVP_PKEY_CTX.
-  ScopedEVP_PKEY_CTX ctx(EVP_PKEY_CTX_new(key, nullptr));
+  bssl::UniquePtr<EVP_PKEY_CTX> ctx(EVP_PKEY_CTX_new(key, nullptr));
   if (!ctx || !key_op_init(ctx.get())) {
     return false;
   }
diff --git a/crypto/newhope/newhope_statistical_test.cc b/crypto/newhope/newhope_statistical_test.cc
index 44fac48..3ca6d78 100644
--- a/crypto/newhope/newhope_statistical_test.cc
+++ b/crypto/newhope/newhope_statistical_test.cc
@@ -19,9 +19,9 @@
 #include <string.h>
 
 #include <openssl/crypto.h>
+#include <openssl/newhope.h>
 #include <openssl/rand.h>
 
-#include "../test/scoped_types.h"
 #include "internal.h"
 
 
@@ -108,7 +108,7 @@
   uint8_t key[NEWHOPE_KEY_LENGTH];
   uint8_t offermsg[NEWHOPE_OFFERMSG_LENGTH];
 
-  ScopedNEWHOPE_POLY sk(NEWHOPE_POLY_new()), pk(NEWHOPE_POLY_new()),
+  bssl::UniquePtr<NEWHOPE_POLY> sk(NEWHOPE_POLY_new()), pk(NEWHOPE_POLY_new()),
       sp(NEWHOPE_POLY_new()), ep(NEWHOPE_POLY_new()), epp(NEWHOPE_POLY_new()),
       a(NEWHOPE_POLY_new()), bp(NEWHOPE_POLY_new()), rec(NEWHOPE_POLY_new());
 
diff --git a/crypto/newhope/newhope_test.cc b/crypto/newhope/newhope_test.cc
index 6637393..a590721 100644
--- a/crypto/newhope/newhope_test.cc
+++ b/crypto/newhope/newhope_test.cc
@@ -19,7 +19,6 @@
 #include <openssl/crypto.h>
 #include <openssl/rand.h>
 
-#include "../test/scoped_types.h"
 #include "internal.h"
 
 
@@ -28,7 +27,7 @@
 
 static bool TestKeys(void) {
   // Alice generates a public key.
-  ScopedNEWHOPE_POLY sk(NEWHOPE_POLY_new());
+  bssl::UniquePtr<NEWHOPE_POLY> sk(NEWHOPE_POLY_new());
   uint8_t offer_msg[NEWHOPE_OFFERMSG_LENGTH];
   NEWHOPE_offer(offer_msg, sk.get());
 
@@ -58,7 +57,7 @@
 static bool TestInvalidSK(void) {
   // Alice generates a public key.
   uint8_t offer_msg[NEWHOPE_OFFERMSG_LENGTH];
-  ScopedNEWHOPE_POLY sk(NEWHOPE_POLY_new());
+  bssl::UniquePtr<NEWHOPE_POLY> sk(NEWHOPE_POLY_new());
   NEWHOPE_offer(offer_msg, sk.get());
 
   // Bob derives a secret key and creates a response.
@@ -93,7 +92,7 @@
 
 static bool TestInvalidAcceptMsg(void) {
   // Alice generates a public key.
-  ScopedNEWHOPE_POLY sk(NEWHOPE_POLY_new());
+  bssl::UniquePtr<NEWHOPE_POLY> sk(NEWHOPE_POLY_new());
   uint8_t offer_msg[NEWHOPE_OFFERMSG_LENGTH];
   NEWHOPE_offer(offer_msg, sk.get());
 
diff --git a/crypto/newhope/newhope_vectors_test.cc b/crypto/newhope/newhope_vectors_test.cc
index fe84cd4..64aa0bb 100644
--- a/crypto/newhope/newhope_vectors_test.cc
+++ b/crypto/newhope/newhope_vectors_test.cc
@@ -20,17 +20,16 @@
 #include <openssl/rand.h>
 
 #include "../test/file_test.h"
-#include "../test/scoped_types.h"
 #include "internal.h"
 
 
 static bool TestNewhope(FileTest *t, void *arg) {
-  ScopedNEWHOPE_POLY a(NEWHOPE_POLY_new());
-  ScopedNEWHOPE_POLY s(NEWHOPE_POLY_new()), sp(NEWHOPE_POLY_new());
-  ScopedNEWHOPE_POLY e(NEWHOPE_POLY_new()), ep(NEWHOPE_POLY_new()),
+  bssl::UniquePtr<NEWHOPE_POLY> a(NEWHOPE_POLY_new());
+  bssl::UniquePtr<NEWHOPE_POLY> s(NEWHOPE_POLY_new()), sp(NEWHOPE_POLY_new());
+  bssl::UniquePtr<NEWHOPE_POLY> e(NEWHOPE_POLY_new()), ep(NEWHOPE_POLY_new()),
       epp(NEWHOPE_POLY_new());
-  ScopedNEWHOPE_POLY in_pk(NEWHOPE_POLY_new());
-  ScopedNEWHOPE_POLY in_rec(NEWHOPE_POLY_new());
+  bssl::UniquePtr<NEWHOPE_POLY> in_pk(NEWHOPE_POLY_new());
+  bssl::UniquePtr<NEWHOPE_POLY> in_rec(NEWHOPE_POLY_new());
 
   if (t->GetType() == "InRandA") {
     std::vector<uint8_t> a_bytes, s_bytes, e_bytes, expected_pk;
diff --git a/crypto/pkcs8/pkcs12_test.cc b/crypto/pkcs8/pkcs12_test.cc
index 17bcd27..5c1a1b4 100644
--- a/crypto/pkcs8/pkcs12_test.cc
+++ b/crypto/pkcs8/pkcs12_test.cc
@@ -23,8 +23,6 @@
 #include <openssl/stack.h>
 #include <openssl/x509.h>
 
-#include "../test/scoped_types.h"
-
 
 /* kPKCS12DER contains sample PKCS#12 data generated by OpenSSL with:
  * openssl pkcs12 -export -inkey key.pem -in cacert.pem */
@@ -684,7 +682,7 @@
 static const char kPassword[] = "foo";
 
 static bool Test(const char *name, const uint8_t *der, size_t der_len) {
-  ScopedX509Stack certs(sk_X509_new_null());
+  bssl::UniquePtr<STACK_OF(X509)> certs(sk_X509_new_null());
   if (!certs) {
     return false;
   }
@@ -697,7 +695,7 @@
     ERR_print_errors_fp(stderr);
     return false;
   }
-  ScopedEVP_PKEY delete_key(key);
+  bssl::UniquePtr<EVP_PKEY> delete_key(key);
 
   if (sk_X509_num(certs.get()) != 1 || key == nullptr) {
     fprintf(stderr, "Bad result from %s data.\n", name);
@@ -708,12 +706,12 @@
 }
 
 static bool TestCompat(const uint8_t *der, size_t der_len) {
-  ScopedBIO bio(BIO_new_mem_buf(der, der_len));
+  bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(der, der_len));
   if (!bio) {
     return false;
   }
 
-  ScopedPKCS12 p12(d2i_PKCS12_bio(bio.get(), nullptr));
+  bssl::UniquePtr<PKCS12> p12(d2i_PKCS12_bio(bio.get(), nullptr));
   if (!p12) {
     fprintf(stderr, "PKCS12_parse failed.\n");
     ERR_print_errors_fp(stderr);
@@ -738,9 +736,9 @@
     ERR_print_errors_fp(stderr);
     return false;
   }
-  ScopedEVP_PKEY delete_key(key);
-  ScopedX509 delete_cert(cert);
-  ScopedX509Stack delete_ca_certs(ca_certs);
+  bssl::UniquePtr<EVP_PKEY> delete_key(key);
+  bssl::UniquePtr<X509> delete_cert(cert);
+  bssl::UniquePtr<STACK_OF(X509)> delete_ca_certs(ca_certs);
 
   if (key == nullptr || cert == nullptr) {
     fprintf(stderr, "Bad result from PKCS12_parse.\n");
diff --git a/crypto/pkcs8/pkcs8_test.cc b/crypto/pkcs8/pkcs8_test.cc
index 7a88ddf..cbb2043 100644
--- a/crypto/pkcs8/pkcs8_test.cc
+++ b/crypto/pkcs8/pkcs8_test.cc
@@ -21,8 +21,6 @@
 #include <openssl/pkcs8.h>
 #include <openssl/x509.h>
 
-#include "../test/scoped_types.h"
-
 
 /* kDER is a PKCS#8 encrypted private key. It was generated with:
  *
@@ -64,14 +62,14 @@
 
 static bool test(const uint8_t *der, size_t der_len) {
   const uint8_t *data = der;
-  ScopedX509_SIG sig(d2i_X509_SIG(NULL, &data, der_len));
+  bssl::UniquePtr<X509_SIG> sig(d2i_X509_SIG(NULL, &data, der_len));
   if (sig.get() == NULL || data != der + der_len) {
     fprintf(stderr, "d2i_X509_SIG failed or did not consume all bytes.\n");
     return false;
   }
 
   static const char kPassword[] = "testing";
-  ScopedPKCS8_PRIV_KEY_INFO keypair(PKCS8_decrypt(sig.get(), kPassword, -1));
+  bssl::UniquePtr<PKCS8_PRIV_KEY_INFO> keypair(PKCS8_decrypt(sig.get(), kPassword, -1));
   if (!keypair) {
     fprintf(stderr, "PKCS8_decrypt failed.\n");
     ERR_print_errors_fp(stderr);
diff --git a/crypto/rsa/rsa_test.cc b/crypto/rsa/rsa_test.cc
index 62177a4..8c4a787 100644
--- a/crypto/rsa/rsa_test.cc
+++ b/crypto/rsa/rsa_test.cc
@@ -65,8 +65,6 @@
 #include <openssl/err.h>
 #include <openssl/nid.h>
 
-#include "../test/scoped_types.h"
-
 
 // kPlaintext is a sample plaintext.
 static const uint8_t kPlaintext[] = "\x54\x85\x9b\x34\x2c\x49\xea\x2a";
@@ -526,7 +524,7 @@
 static bool TestRSA(const uint8_t *der, size_t der_len,
                     const uint8_t *oaep_ciphertext,
                     size_t oaep_ciphertext_len) {
-  ScopedRSA key(RSA_private_key_from_bytes(der, der_len));
+  bssl::UniquePtr<RSA> key(RSA_private_key_from_bytes(der, der_len));
   if (!key) {
     return false;
   }
@@ -612,7 +610,7 @@
 
 static bool TestMultiPrimeKey(int nprimes, const uint8_t *der, size_t der_size,
                               const uint8_t *enc, size_t enc_size) {
-  ScopedRSA rsa(d2i_RSAPrivateKey(nullptr, &der, der_size));
+  bssl::UniquePtr<RSA> rsa(d2i_RSAPrivateKey(nullptr, &der, der_size));
   if (!rsa) {
     fprintf(stderr, "%d-prime key failed to parse.\n", nprimes);
     ERR_print_errors_fp(stderr);
@@ -645,8 +643,8 @@
   uint8_t encrypted[kBits / 8], decrypted[kBits / 8];
   size_t encrypted_len, decrypted_len;
 
-  ScopedRSA rsa(RSA_new());
-  ScopedBIGNUM e(BN_new());
+  bssl::UniquePtr<RSA> rsa(RSA_new());
+  bssl::UniquePtr<BIGNUM> e(BN_new());
   if (!rsa || !e ||
       !BN_set_word(e.get(), RSA_F4) ||
       !RSA_generate_multi_prime_key(rsa.get(), kBits, 3, e.get(), nullptr) ||
@@ -666,8 +664,8 @@
 }
 
 static bool TestBadKey() {
-  ScopedRSA key(RSA_new());
-  ScopedBIGNUM e(BN_new());
+  bssl::UniquePtr<RSA> key(RSA_new());
+  bssl::UniquePtr<BIGNUM> e(BN_new());
 
   if (!key || !e || !BN_set_word(e.get(), RSA_F4)) {
     return false;
@@ -705,7 +703,7 @@
 
   uint8_t buf[64];
   unsigned buf_len = sizeof(buf);
-  ScopedRSA key(RSA_new());
+  bssl::UniquePtr<RSA> key(RSA_new());
   if (!key ||
       !BN_hex2bn(&key->n, kN) ||
       !BN_hex2bn(&key->e, kE) ||
@@ -739,7 +737,7 @@
   // Keys without the public exponent must continue to work when blinding is
   // disabled to support Java's RSAPrivateKeySpec API. See
   // https://bugs.chromium.org/p/boringssl/issues/detail?id=12.
-  ScopedRSA key2(RSA_new());
+  bssl::UniquePtr<RSA> key2(RSA_new());
   if (!key2 ||
       !BN_hex2bn(&key2->n, kN) ||
       !BN_hex2bn(&key2->d, kD)) {
@@ -772,7 +770,7 @@
 }
 
 static bool TestRecoverCRTParams() {
-  ScopedBIGNUM e(BN_new());
+  bssl::UniquePtr<BIGNUM> e(BN_new());
   if (!e || !BN_set_word(e.get(), RSA_F4)) {
     return false;
   }
@@ -780,7 +778,7 @@
   ERR_clear_error();
 
   for (unsigned i = 0; i < 1; i++) {
-    ScopedRSA key1(RSA_new());
+    bssl::UniquePtr<RSA> key1(RSA_new());
     if (!key1 ||
         !RSA_generate_key_ex(key1.get(), 512, e.get(), nullptr)) {
       fprintf(stderr, "RSA_generate_key_ex failed.\n");
@@ -794,7 +792,7 @@
       return false;
     }
 
-    ScopedRSA key2(RSA_new());
+    bssl::UniquePtr<RSA> key2(RSA_new());
     if (!key2) {
       return false;
     }
@@ -844,7 +842,7 @@
 
 static bool TestASN1() {
   // Test that private keys may be decoded.
-  ScopedRSA rsa(RSA_private_key_from_bytes(kKey1, sizeof(kKey1) - 1));
+  bssl::UniquePtr<RSA> rsa(RSA_private_key_from_bytes(kKey1, sizeof(kKey1) - 1));
   if (!rsa) {
     return false;
   }
@@ -855,7 +853,7 @@
   if (!RSA_private_key_to_bytes(&der, &der_len, rsa.get())) {
     return false;
   }
-  ScopedOpenSSLBytes delete_der(der);
+  bssl::UniquePtr<uint8_t> delete_der(der);
   if (der_len != sizeof(kKey1) - 1 || memcmp(der, kKey1, der_len) != 0) {
     return false;
   }
@@ -878,7 +876,7 @@
   if (!RSA_public_key_to_bytes(&der2, &der2_len, rsa.get())) {
     return false;
   }
-  ScopedOpenSSLBytes delete_der2(der2);
+  bssl::UniquePtr<uint8_t> delete_der2(der2);
   if (der_len != der2_len || memcmp(der, der2, der_len) != 0) {
     return false;
   }
@@ -910,7 +908,7 @@
 }
 
 static bool TestBadExponent() {
-  ScopedRSA rsa(RSA_public_key_from_bytes(kExponent1RSAKey,
+  bssl::UniquePtr<RSA> rsa(RSA_public_key_from_bytes(kExponent1RSAKey,
                                           sizeof(kExponent1RSAKey)));
 
   if (rsa) {
diff --git a/crypto/test/scoped_types.h b/crypto/test/scoped_types.h
deleted file mode 100644
index c124235..0000000
--- a/crypto/test/scoped_types.h
+++ /dev/null
@@ -1,133 +0,0 @@
-/* Copyright (c) 2015, 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_CRYPTO_TEST_SCOPED_TYPES_H
-#define OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H
-
-#include <stdint.h>
-#include <stdio.h>
-
-#include <memory>
-
-#include <openssl/aead.h>
-#include <openssl/asn1.h>
-#include <openssl/bio.h>
-#include <openssl/bn.h>
-#include <openssl/bytestring.h>
-#include <openssl/cmac.h>
-#include <openssl/curve25519.h>
-#include <openssl/dh.h>
-#include <openssl/ecdsa.h>
-#include <openssl/ec.h>
-#include <openssl/ec_key.h>
-#include <openssl/evp.h>
-#include <openssl/hmac.h>
-#include <openssl/mem.h>
-#include <openssl/newhope.h>
-#include <openssl/pkcs8.h>
-#include <openssl/rsa.h>
-#include <openssl/stack.h>
-#include <openssl/x509.h>
-
-
-template<typename T, void (*func)(T*)>
-struct OpenSSLDeleter {
-  void operator()(T *obj) {
-    func(obj);
-  }
-};
-
-template<typename StackType, typename T, void (*func)(T*)>
-struct OpenSSLStackDeleter {
-  void operator()(StackType *obj) {
-    sk_pop_free(reinterpret_cast<_STACK*>(obj),
-                reinterpret_cast<void (*)(void *)>(func));
-  }
-};
-
-template<typename T>
-struct OpenSSLFree {
-  void operator()(T *buf) {
-    OPENSSL_free(buf);
-  }
-};
-
-struct FileCloser {
-  void operator()(FILE *file) {
-    fclose(file);
-  }
-};
-
-template<typename T, void (*func)(T*)>
-using ScopedOpenSSLType = std::unique_ptr<T, OpenSSLDeleter<T, func>>;
-
-template<typename StackType, typename T, void (*func)(T*)>
-using ScopedOpenSSLStack =
-    std::unique_ptr<StackType, OpenSSLStackDeleter<StackType, T, func>>;
-
-template<typename T, typename CleanupRet, void (*init_func)(T*),
-         CleanupRet (*cleanup_func)(T*)>
-class ScopedOpenSSLContext {
- public:
-  ScopedOpenSSLContext() {
-    init_func(&ctx_);
-  }
-  ~ScopedOpenSSLContext() {
-    cleanup_func(&ctx_);
-  }
-
-  T *get() { return &ctx_; }
-  const T *get() const { return &ctx_; }
-
-  void Reset() {
-    cleanup_func(&ctx_);
-    init_func(&ctx_);
-  }
-
- private:
-  T ctx_;
-};
-
-using ScopedASN1_TYPE = ScopedOpenSSLType<ASN1_TYPE, ASN1_TYPE_free>;
-using ScopedBIO = ScopedOpenSSLType<BIO, BIO_vfree>;
-using ScopedBIGNUM = ScopedOpenSSLType<BIGNUM, BN_free>;
-using ScopedBN_CTX = ScopedOpenSSLType<BN_CTX, BN_CTX_free>;
-using ScopedBN_MONT_CTX = ScopedOpenSSLType<BN_MONT_CTX, BN_MONT_CTX_free>;
-using ScopedCMAC_CTX = ScopedOpenSSLType<CMAC_CTX, CMAC_CTX_free>;
-using ScopedDH = ScopedOpenSSLType<DH, DH_free>;
-using ScopedECDSA_SIG = ScopedOpenSSLType<ECDSA_SIG, ECDSA_SIG_free>;
-using ScopedEC_GROUP = ScopedOpenSSLType<EC_GROUP, EC_GROUP_free>;
-using ScopedEC_KEY = ScopedOpenSSLType<EC_KEY, EC_KEY_free>;
-using ScopedEC_POINT = ScopedOpenSSLType<EC_POINT, EC_POINT_free>;
-using ScopedEVP_PKEY = ScopedOpenSSLType<EVP_PKEY, EVP_PKEY_free>;
-using ScopedEVP_PKEY_CTX = ScopedOpenSSLType<EVP_PKEY_CTX, EVP_PKEY_CTX_free>;
-using ScopedNEWHOPE_POLY = ScopedOpenSSLType<NEWHOPE_POLY, NEWHOPE_POLY_free>;
-using ScopedPKCS8_PRIV_KEY_INFO = ScopedOpenSSLType<PKCS8_PRIV_KEY_INFO,
-                                                    PKCS8_PRIV_KEY_INFO_free>;
-using ScopedPKCS12 = ScopedOpenSSLType<PKCS12, PKCS12_free>;
-using ScopedSPAKE2_CTX = ScopedOpenSSLType<SPAKE2_CTX, SPAKE2_CTX_free>;
-using ScopedRSA = ScopedOpenSSLType<RSA, RSA_free>;
-using ScopedX509 = ScopedOpenSSLType<X509, X509_free>;
-using ScopedX509_ALGOR = ScopedOpenSSLType<X509_ALGOR, X509_ALGOR_free>;
-using ScopedX509_SIG = ScopedOpenSSLType<X509_SIG, X509_SIG_free>;
-using ScopedX509_STORE_CTX = ScopedOpenSSLType<X509_STORE_CTX, X509_STORE_CTX_free>;
-
-using ScopedX509Stack = ScopedOpenSSLStack<STACK_OF(X509), X509, X509_free>;
-
-using ScopedOpenSSLBytes = std::unique_ptr<uint8_t, OpenSSLFree<uint8_t>>;
-using ScopedOpenSSLString = std::unique_ptr<char, OpenSSLFree<char>>;
-
-using ScopedFILE = std::unique_ptr<FILE, FileCloser>;
-
-#endif  // OPENSSL_HEADER_CRYPTO_TEST_SCOPED_TYPES_H
diff --git a/crypto/x509/x509_test.cc b/crypto/x509/x509_test.cc
index d1eed2a..3c7cc63 100644
--- a/crypto/x509/x509_test.cc
+++ b/crypto/x509/x509_test.cc
@@ -23,8 +23,6 @@
 #include <openssl/pem.h>
 #include <openssl/x509.h>
 
-#include "../test/scoped_types.h"
-
 namespace bssl {
 
 static const char kCrossSigningRootPEM[] =
@@ -226,23 +224,25 @@
 
 // CertFromPEM parses the given, NUL-terminated pem block and returns an
 // |X509*|.
-static ScopedX509 CertFromPEM(const char *pem) {
-  ScopedBIO bio(BIO_new_mem_buf(pem, strlen(pem)));
-  return ScopedX509(PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr));
+static bssl::UniquePtr<X509> CertFromPEM(const char *pem) {
+  bssl::UniquePtr<BIO> bio(BIO_new_mem_buf(pem, strlen(pem)));
+  return bssl::UniquePtr<X509>(
+      PEM_read_bio_X509(bio.get(), nullptr, nullptr, nullptr));
 }
 
 // PrivateKeyFromPEM parses the given, NUL-terminated pem block and returns an
 // |EVP_PKEY*|.
-static ScopedEVP_PKEY PrivateKeyFromPEM(const char *pem) {
-  ScopedBIO bio(BIO_new_mem_buf(const_cast<char *>(pem), strlen(pem)));
-  return ScopedEVP_PKEY(
+static bssl::UniquePtr<EVP_PKEY> PrivateKeyFromPEM(const char *pem) {
+  bssl::UniquePtr<BIO> bio(
+      BIO_new_mem_buf(const_cast<char *>(pem), strlen(pem)));
+  return bssl::UniquePtr<EVP_PKEY>(
       PEM_read_bio_PrivateKey(bio.get(), nullptr, nullptr, nullptr));
 }
 
 // CertsToStack converts a vector of |X509*| to an OpenSSL STACK_OF(X509*),
 // bumping the reference counts for each certificate in question.
 static STACK_OF(X509)* CertsToStack(const std::vector<X509*> &certs) {
-  ScopedX509Stack stack(sk_X509_new_null());
+  bssl::UniquePtr<STACK_OF(X509)> stack(sk_X509_new_null());
   if (!stack) {
     return nullptr;
   }
@@ -259,14 +259,16 @@
 static bool Verify(X509 *leaf, const std::vector<X509 *> &roots,
                    const std::vector<X509 *> &intermediates,
                    unsigned long flags = 0) {
-  ScopedX509Stack roots_stack(CertsToStack(roots));
-  ScopedX509Stack intermediates_stack(CertsToStack(intermediates));
+  bssl::UniquePtr<STACK_OF(X509)> roots_stack(CertsToStack(roots));
+  bssl::UniquePtr<STACK_OF(X509)> intermediates_stack(
+      CertsToStack(intermediates));
+
   if (!roots_stack ||
       !intermediates_stack) {
     return false;
   }
 
-  ScopedX509_STORE_CTX ctx(X509_STORE_CTX_new());
+  bssl::UniquePtr<X509_STORE_CTX> ctx(X509_STORE_CTX_new());
   if (!ctx) {
     return false;
   }
@@ -293,14 +295,15 @@
 }
 
 static bool TestVerify() {
-  ScopedX509 cross_signing_root(CertFromPEM(kCrossSigningRootPEM));
-  ScopedX509 root(CertFromPEM(kRootCAPEM));
-  ScopedX509 root_cross_signed(CertFromPEM(kRootCrossSignedPEM));
-  ScopedX509 intermediate(CertFromPEM(kIntermediatePEM));
-  ScopedX509 intermediate_self_signed(CertFromPEM(kIntermediateSelfSignedPEM));
-  ScopedX509 leaf(CertFromPEM(kLeafPEM));
-  ScopedX509 leaf_no_key_usage(CertFromPEM(kLeafNoKeyUsagePEM));
-  ScopedX509 forgery(CertFromPEM(kForgeryPEM));
+  bssl::UniquePtr<X509> cross_signing_root(CertFromPEM(kCrossSigningRootPEM));
+  bssl::UniquePtr<X509> root(CertFromPEM(kRootCAPEM));
+  bssl::UniquePtr<X509> root_cross_signed(CertFromPEM(kRootCrossSignedPEM));
+  bssl::UniquePtr<X509> intermediate(CertFromPEM(kIntermediatePEM));
+  bssl::UniquePtr<X509> intermediate_self_signed(
+      CertFromPEM(kIntermediateSelfSignedPEM));
+  bssl::UniquePtr<X509> leaf(CertFromPEM(kLeafPEM));
+  bssl::UniquePtr<X509> leaf_no_key_usage(CertFromPEM(kLeafNoKeyUsagePEM));
+  bssl::UniquePtr<X509> forgery(CertFromPEM(kForgeryPEM));
 
   if (!cross_signing_root ||
       !root ||
@@ -380,12 +383,12 @@
 }
 
 static bool TestPSS() {
-  ScopedX509 cert(CertFromPEM(kExamplePSSCert));
+  bssl::UniquePtr<X509> cert(CertFromPEM(kExamplePSSCert));
   if (!cert) {
     return false;
   }
 
-  ScopedEVP_PKEY pkey(X509_get_pubkey(cert.get()));
+  bssl::UniquePtr<EVP_PKEY> pkey(X509_get_pubkey(cert.get()));
   if (!pkey) {
     return false;
   }
@@ -398,12 +401,12 @@
 }
 
 static bool TestBadPSSParameters() {
-  ScopedX509 cert(CertFromPEM(kBadPSSCertPEM));
+  bssl::UniquePtr<X509> cert(CertFromPEM(kBadPSSCertPEM));
   if (!cert) {
     return false;
   }
 
-  ScopedEVP_PKEY pkey(X509_get_pubkey(cert.get()));
+  bssl::UniquePtr<EVP_PKEY> pkey(X509_get_pubkey(cert.get()));
   if (!pkey) {
     return false;
   }
@@ -418,7 +421,7 @@
 
 static bool SignatureRoundTrips(EVP_MD_CTX *md_ctx, EVP_PKEY *pkey) {
   // Make a certificate like signed with |md_ctx|'s settings.'
-  ScopedX509 cert(CertFromPEM(kLeafPEM));
+  bssl::UniquePtr<X509> cert(CertFromPEM(kLeafPEM));
   if (!cert || !X509_sign_ctx(cert.get(), md_ctx)) {
     return false;
   }
@@ -429,7 +432,7 @@
 }
 
 static bool TestSignCtx() {
-  ScopedEVP_PKEY pkey(PrivateKeyFromPEM(kRSAKey));
+  bssl::UniquePtr<EVP_PKEY> pkey(PrivateKeyFromPEM(kRSAKey));
   if (!pkey) {
     return false;
   }