Don't include every field in every EVP_PKEY_ALG

We can just subclass things. (Really we should make EVP_PKEY_ALG a C++
interface and implement virtual methods, but I'd like to namespace our
types internally first.)

Change-Id: Ia2165581ca7a98d447dc8a53261edea4714f700f
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/81987
Reviewed-by: Lily Chen <chlily@google.com>
Auto-Submit: David Benjamin <davidben@google.com>
Commit-Queue: Lily Chen <chlily@google.com>
diff --git a/crypto/evp/internal.h b/crypto/evp/internal.h
index b4a25d9..3cf0f9d 100644
--- a/crypto/evp/internal.h
+++ b/crypto/evp/internal.h
@@ -34,10 +34,6 @@
 struct evp_pkey_alg_st {
   // method implements operations for this |EVP_PKEY_ALG|.
   const EVP_PKEY_ASN1_METHOD *method;
-
-  // ec_group returns the |EC_GROUP| for this algorithm, if |method| is for
-  // |EVP_PKEY_EC|.
-  const EC_GROUP *(*ec_group)();
 };
 
 enum evp_decode_result_t {
diff --git a/crypto/evp/p_dsa_asn1.cc b/crypto/evp/p_dsa_asn1.cc
index 04c21fe..49797b9 100644
--- a/crypto/evp/p_dsa_asn1.cc
+++ b/crypto/evp/p_dsa_asn1.cc
@@ -235,10 +235,7 @@
 };
 
 const EVP_PKEY_ALG *EVP_pkey_dsa(void) {
-  static const EVP_PKEY_ALG kAlg = {
-      /*method=*/&dsa_asn1_meth,
-      /*ec_group=*/nullptr,
-  };
+  static const EVP_PKEY_ALG kAlg = {&dsa_asn1_meth};
   return &kAlg;
 }
 
diff --git a/crypto/evp/p_ec_asn1.cc b/crypto/evp/p_ec_asn1.cc
index c5a24fa..debc6c5 100644
--- a/crypto/evp/p_ec_asn1.cc
+++ b/crypto/evp/p_ec_asn1.cc
@@ -27,6 +27,13 @@
 #include "internal.h"
 
 
+namespace {
+
+struct EVP_PKEY_ALG_EC : public EVP_PKEY_ALG {
+  // ec_group returns the |EC_GROUP| for this algorithm.
+  const EC_GROUP *(*ec_group)();
+};
+
 static int eckey_pub_encode(CBB *out, const EVP_PKEY *key) {
   const EC_KEY *ec_key = reinterpret_cast<const EC_KEY *>(key->pkey);
   const EC_GROUP *group = EC_KEY_get0_group(ec_key);
@@ -57,7 +64,7 @@
   // See RFC 5480, section 2.
 
   // Check that |params| matches |alg|. Only the namedCurve form is allowed.
-  const EC_GROUP *group = alg->ec_group();
+  const EC_GROUP *group = static_cast<const EVP_PKEY_ALG_EC*>(alg)->ec_group();
   if (ec_key_parse_curve_name(params, bssl::Span(&group, 1)) == nullptr) {
     if (ERR_equals(ERR_peek_last_error(), ERR_LIB_EC, EC_R_UNKNOWN_GROUP)) {
       ERR_clear_error();
@@ -102,7 +109,7 @@
                                              EVP_PKEY *out, CBS *params,
                                              CBS *key) {
   // See RFC 5915.
-  const EC_GROUP *group = alg->ec_group();
+  const EC_GROUP *group = static_cast<const EVP_PKEY_ALG_EC*>(alg)->ec_group();
   if (ec_key_parse_parameters(params, bssl::Span(&group, 1)) == nullptr) {
     if (ERR_equals(ERR_peek_last_error(), ERR_LIB_EC, EC_R_UNKNOWN_GROUP)) {
       ERR_clear_error();
@@ -243,6 +250,8 @@
   return EC_KEY_is_opaque(ec_key);
 }
 
+}  // namespace
+
 const EVP_PKEY_ASN1_METHOD ec_asn1_meth = {
     EVP_PKEY_EC,
     // 1.2.840.10045.2.1
@@ -278,38 +287,25 @@
 };
 
 const EVP_PKEY_ALG *EVP_pkey_ec_p224(void) {
-  static const EVP_PKEY_ALG kAlg = {
-      /*method=*/&ec_asn1_meth,
-      /*ec_group=*/&EC_group_p224,
-  };
+  static const EVP_PKEY_ALG_EC kAlg = {{&ec_asn1_meth}, &EC_group_p224};
   return &kAlg;
 }
 
 const EVP_PKEY_ALG *EVP_pkey_ec_p256(void) {
-  static const EVP_PKEY_ALG kAlg = {
-      /*method=*/&ec_asn1_meth,
-      /*ec_group=*/&EC_group_p256,
-  };
+  static const EVP_PKEY_ALG_EC kAlg = {{&ec_asn1_meth}, &EC_group_p256};
   return &kAlg;
 }
 
 const EVP_PKEY_ALG *EVP_pkey_ec_p384(void) {
-  static const EVP_PKEY_ALG kAlg = {
-      /*method=*/&ec_asn1_meth,
-      /*ec_group=*/&EC_group_p384,
-  };
+  static const EVP_PKEY_ALG_EC kAlg = {{&ec_asn1_meth}, &EC_group_p384};
   return &kAlg;
 }
 
 const EVP_PKEY_ALG *EVP_pkey_ec_p521(void) {
-  static const EVP_PKEY_ALG kAlg = {
-      /*method=*/&ec_asn1_meth,
-      /*ec_group=*/&EC_group_p521,
-  };
+  static const EVP_PKEY_ALG_EC kAlg = {{&ec_asn1_meth}, &EC_group_p521};
   return &kAlg;
 }
 
-
 int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key) {
   if (EVP_PKEY_assign_EC_KEY(pkey, key)) {
     EC_KEY_up_ref(key);
diff --git a/crypto/evp/p_ed25519_asn1.cc b/crypto/evp/p_ed25519_asn1.cc
index cdd05ba..15b4002 100644
--- a/crypto/evp/p_ed25519_asn1.cc
+++ b/crypto/evp/p_ed25519_asn1.cc
@@ -229,9 +229,6 @@
 };
 
 const EVP_PKEY_ALG *EVP_pkey_ed25519(void) {
-  static const EVP_PKEY_ALG kAlg = {
-      /*method=*/&ed25519_asn1_meth,
-      /*ec_group=*/nullptr,
-  };
+  static const EVP_PKEY_ALG kAlg = {&ed25519_asn1_meth};
   return &kAlg;
 }
diff --git a/crypto/evp/p_rsa_asn1.cc b/crypto/evp/p_rsa_asn1.cc
index fe79d8e..e0a9133 100644
--- a/crypto/evp/p_rsa_asn1.cc
+++ b/crypto/evp/p_rsa_asn1.cc
@@ -307,18 +307,12 @@
 
 
 const EVP_PKEY_ALG *EVP_pkey_rsa(void) {
-  static const EVP_PKEY_ALG kAlg = {
-      /*method=*/&rsa_asn1_meth,
-      /*ec_group=*/nullptr,
-  };
+  static const EVP_PKEY_ALG kAlg = {&rsa_asn1_meth};
   return &kAlg;
 }
 
 const EVP_PKEY_ALG *EVP_pkey_rsa_pss_sha256(void) {
-  static const EVP_PKEY_ALG kAlg = {
-      /*method=*/&rsa_pss_sha256_asn1_meth,
-      /*ec_group=*/nullptr,
-  };
+  static const EVP_PKEY_ALG kAlg = {&rsa_pss_sha256_asn1_meth};
   return &kAlg;
 }
 
diff --git a/crypto/evp/p_x25519_asn1.cc b/crypto/evp/p_x25519_asn1.cc
index 49e65ef..05f294a 100644
--- a/crypto/evp/p_x25519_asn1.cc
+++ b/crypto/evp/p_x25519_asn1.cc
@@ -243,9 +243,6 @@
 };
 
 const EVP_PKEY_ALG *EVP_pkey_x25519(void) {
-  static const EVP_PKEY_ALG kAlg = {
-      /*method=*/&x25519_asn1_meth,
-      /*ec_group=*/nullptr,
-  };
+  static const EVP_PKEY_ALG kAlg = {&x25519_asn1_meth};
   return &kAlg;
 }