Make the EVP_PKEY_ASN1_METHOD tables static Everything that needs to access them indirects through EVP_PKEY_ALG now, so this is one less pile of symbols to juggle. Change-Id: Ic0a6d834a93200356eb033f216f9cbf38bc0930e Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/82989 Commit-Queue: David Benjamin <davidben@google.com> Reviewed-by: Lily Chen <chlily@google.com>
diff --git a/crypto/evp/internal.h b/crypto/evp/internal.h index da32821..a5a4b82 100644 --- a/crypto/evp/internal.h +++ b/crypto/evp/internal.h
@@ -253,14 +253,6 @@ int (*ctrl)(EVP_PKEY_CTX *ctx, int type, int p1, void *p2); } /* EVP_PKEY_CTX_METHOD */; -extern const EVP_PKEY_ASN1_METHOD dsa_asn1_meth; -extern const EVP_PKEY_ASN1_METHOD ec_asn1_meth; -extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meth; -extern const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth; -extern const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth; -extern const EVP_PKEY_ASN1_METHOD x25519_asn1_meth; -extern const EVP_PKEY_ASN1_METHOD dh_asn1_meth; - extern const EVP_PKEY_CTX_METHOD rsa_pkey_meth; extern const EVP_PKEY_CTX_METHOD rsa_pss_pkey_meth; extern const EVP_PKEY_CTX_METHOD ec_pkey_meth;
diff --git a/crypto/evp/p_dh.cc b/crypto/evp/p_dh.cc index 23065f2..672cc7e 100644 --- a/crypto/evp/p_dh.cc +++ b/crypto/evp/p_dh.cc
@@ -89,7 +89,7 @@ return BN_cmp(DH_get0_pub_key(a_dh), DH_get0_pub_key(b_dh)) == 0; } -const EVP_PKEY_ASN1_METHOD dh_asn1_meth = { +static const EVP_PKEY_ASN1_METHOD dh_asn1_meth = { /*pkey_id=*/EVP_PKEY_DH, /*oid=*/{0}, /*oid_len=*/0,
diff --git a/crypto/evp/p_dsa.cc b/crypto/evp/p_dsa.cc index 49797b9..d713f12 100644 --- a/crypto/evp/p_dsa.cc +++ b/crypto/evp/p_dsa.cc
@@ -24,6 +24,10 @@ #include "internal.h" +namespace { + +extern const EVP_PKEY_ASN1_METHOD dsa_asn1_meth; + static evp_decode_result_t dsa_pub_decode(const EVP_PKEY_ALG *alg, EVP_PKEY *out, CBS *params, CBS *key) { @@ -234,6 +238,8 @@ int_dsa_free, }; +} // namespace + const EVP_PKEY_ALG *EVP_pkey_dsa(void) { static const EVP_PKEY_ALG kAlg = {&dsa_asn1_meth}; return &kAlg;
diff --git a/crypto/evp/p_ec.cc b/crypto/evp/p_ec.cc index 751141e..efd7533 100644 --- a/crypto/evp/p_ec.cc +++ b/crypto/evp/p_ec.cc
@@ -41,6 +41,8 @@ const EC_GROUP *(*ec_group)(); }; +extern const EVP_PKEY_ASN1_METHOD ec_asn1_meth; + 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); @@ -258,8 +260,6 @@ 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 @@ -294,6 +294,8 @@ int_ec_free, }; +} // namespace + const EVP_PKEY_ALG *EVP_pkey_ec_p224(void) { static const EVP_PKEY_ALG_EC kAlg = {{&ec_asn1_meth}, &EC_group_p224}; return &kAlg;
diff --git a/crypto/evp/p_ed25519.cc b/crypto/evp/p_ed25519.cc index 3ccfdaa..e3807f9 100644 --- a/crypto/evp/p_ed25519.cc +++ b/crypto/evp/p_ed25519.cc
@@ -22,15 +22,18 @@ #include "../internal.h" #include "internal.h" +namespace { -typedef struct { +struct ED25519_KEY { // key is the concatenation of the private seed and public key. It is stored // as a single 64-bit array to allow passing to |ED25519_sign|. If // |has_private| is false, the first 32 bytes are uninitialized and the public // key is in the last 32 bytes. uint8_t key[64]; - char has_private; -} ED25519_KEY; + bool has_private; +}; + +extern const EVP_PKEY_ASN1_METHOD ed25519_asn1_meth; #define ED25519_PUBLIC_KEY_OFFSET 32 @@ -55,7 +58,7 @@ // full representation which we use from it. uint8_t pubkey_unused[32]; ED25519_keypair_from_seed(pubkey_unused, key->key, in); - key->has_private = 1; + key->has_private = true; evp_pkey_set0(pkey, &ed25519_asn1_meth, key); return 1; } @@ -73,7 +76,7 @@ } OPENSSL_memcpy(key->key + ED25519_PUBLIC_KEY_OFFSET, in, 32); - key->has_private = 0; + key->has_private = false; evp_pkey_set0(pkey, &ed25519_asn1_meth, key); return 1; } @@ -239,11 +242,6 @@ ed25519_free, }; -const EVP_PKEY_ALG *EVP_pkey_ed25519(void) { - static const EVP_PKEY_ALG kAlg = {&ed25519_asn1_meth}; - return &kAlg; -} - // Ed25519 has no parameters to copy. static int pkey_ed25519_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src) { return 1; } @@ -256,7 +254,7 @@ uint8_t pubkey_unused[32]; ED25519_keypair(pubkey_unused, key->key); - key->has_private = 1; + key->has_private = true; evp_pkey_set0(pkey, &ed25519_asn1_meth, key); return 1; @@ -304,6 +302,8 @@ return 1; } +} // namespace + const EVP_PKEY_CTX_METHOD ed25519_pkey_meth = { /*pkey_id=*/EVP_PKEY_ED25519, /*init=*/nullptr, @@ -321,3 +321,8 @@ /*paramgen=*/nullptr, /*ctrl=*/nullptr, }; + +const EVP_PKEY_ALG *EVP_pkey_ed25519(void) { + static const EVP_PKEY_ALG kAlg = {&ed25519_asn1_meth}; + return &kAlg; +}
diff --git a/crypto/evp/p_rsa.cc b/crypto/evp/p_rsa.cc index 263227f..a407550 100644 --- a/crypto/evp/p_rsa.cc +++ b/crypto/evp/p_rsa.cc
@@ -39,6 +39,9 @@ rsa_pss_params_t pss_params; }; +extern const EVP_PKEY_ASN1_METHOD rsa_asn1_meth; +extern const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth; + static int rsa_pub_encode(CBB *out, const EVP_PKEY *key) { // See RFC 3279, section 2.3.1. const RSA *rsa = reinterpret_cast<const RSA *>(key->pkey); @@ -250,6 +253,75 @@ pkey->pkey = nullptr; } +const EVP_PKEY_ASN1_METHOD rsa_asn1_meth = { + EVP_PKEY_RSA, + // 1.2.840.113549.1.1.1 + {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01}, + 9, + + &rsa_pkey_meth, + + rsa_pub_decode, + rsa_pub_encode, + rsa_pub_cmp, + + rsa_priv_decode, + rsa_priv_encode, + + /*set_priv_raw=*/nullptr, + /*set_pub_raw=*/nullptr, + /*get_priv_raw=*/nullptr, + /*get_pub_raw=*/nullptr, + /*set1_tls_encodedpoint=*/nullptr, + /*get1_tls_encodedpoint=*/nullptr, + + rsa_opaque, + + int_rsa_size, + rsa_bits, + + /*param_missing=*/nullptr, + /*param_copy=*/nullptr, + /*param_cmp=*/nullptr, + + int_rsa_free, +}; + +const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth = { + EVP_PKEY_RSA_PSS, + // 1.2.840.113549.1.1.10 + {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0a}, + 9, + + &rsa_pss_pkey_meth, + + rsa_pub_decode_pss, + rsa_pub_encode_pss, + rsa_pub_cmp, + + rsa_priv_decode_pss, + rsa_priv_encode_pss, + + /*set_priv_raw=*/nullptr, + /*set_pub_raw=*/nullptr, + /*get_priv_raw=*/nullptr, + /*get_pub_raw=*/nullptr, + /*set1_tls_encodedpoint=*/nullptr, + /*get1_tls_encodedpoint=*/nullptr, + + rsa_opaque, + + int_rsa_size, + rsa_bits, + + /*param_missing=*/nullptr, + /*param_copy=*/nullptr, + /*param_cmp=*/nullptr, + + int_rsa_free, +}; + + struct RSA_PKEY_CTX { // Key gen parameters int nbits = 2048; @@ -730,75 +802,6 @@ } // namespace -const EVP_PKEY_ASN1_METHOD rsa_asn1_meth = { - EVP_PKEY_RSA, - // 1.2.840.113549.1.1.1 - {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01}, - 9, - - &rsa_pkey_meth, - - rsa_pub_decode, - rsa_pub_encode, - rsa_pub_cmp, - - rsa_priv_decode, - rsa_priv_encode, - - /*set_priv_raw=*/nullptr, - /*set_pub_raw=*/nullptr, - /*get_priv_raw=*/nullptr, - /*get_pub_raw=*/nullptr, - /*set1_tls_encodedpoint=*/nullptr, - /*get1_tls_encodedpoint=*/nullptr, - - rsa_opaque, - - int_rsa_size, - rsa_bits, - - /*param_missing=*/nullptr, - /*param_copy=*/nullptr, - /*param_cmp=*/nullptr, - - int_rsa_free, -}; - -const EVP_PKEY_ASN1_METHOD rsa_pss_asn1_meth = { - EVP_PKEY_RSA_PSS, - // 1.2.840.113549.1.1.10 - {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0a}, - 9, - - &rsa_pss_pkey_meth, - - rsa_pub_decode_pss, - rsa_pub_encode_pss, - rsa_pub_cmp, - - rsa_priv_decode_pss, - rsa_priv_encode_pss, - - /*set_priv_raw=*/nullptr, - /*set_pub_raw=*/nullptr, - /*get_priv_raw=*/nullptr, - /*get_pub_raw=*/nullptr, - /*set1_tls_encodedpoint=*/nullptr, - /*get1_tls_encodedpoint=*/nullptr, - - rsa_opaque, - - int_rsa_size, - rsa_bits, - - /*param_missing=*/nullptr, - /*param_copy=*/nullptr, - /*param_cmp=*/nullptr, - - int_rsa_free, -}; - - const EVP_PKEY_ALG *EVP_pkey_rsa(void) { static const EVP_PKEY_ALG kAlg = {&rsa_asn1_meth}; return &kAlg;
diff --git a/crypto/evp/p_x25519.cc b/crypto/evp/p_x25519.cc index 563a249..3303a13 100644 --- a/crypto/evp/p_x25519.cc +++ b/crypto/evp/p_x25519.cc
@@ -23,11 +23,15 @@ #include "internal.h" -typedef struct { +namespace { + +struct X25519_KEY { uint8_t pub[32]; uint8_t priv[32]; - char has_private; -} X25519_KEY; + bool has_private; +}; + +extern const EVP_PKEY_ASN1_METHOD x25519_asn1_meth; static void x25519_free(EVP_PKEY *pkey) { OPENSSL_free(pkey->pkey); @@ -48,7 +52,7 @@ OPENSSL_memcpy(key->priv, in, 32); X25519_public_from_private(key->pub, key->priv); - key->has_private = 1; + key->has_private = true; evp_pkey_set0(pkey, &x25519_asn1_meth, key); return 1; @@ -67,7 +71,7 @@ } OPENSSL_memcpy(key->pub, in, 32); - key->has_private = 0; + key->has_private = false; evp_pkey_set0(pkey, &x25519_asn1_meth, key); return 1; @@ -248,6 +252,8 @@ x25519_free, }; +} // namespace + const EVP_PKEY_ALG *EVP_pkey_x25519(void) { static const EVP_PKEY_ALG kAlg = {&x25519_asn1_meth}; return &kAlg; @@ -264,7 +270,7 @@ } X25519_keypair(key->pub, key->priv); - key->has_private = 1; + key->has_private = true; evp_pkey_set0(pkey, &x25519_asn1_meth, key); return 1; }