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/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(),