Add |EVP_get_digestbyname|. Change-Id: If7078ea68f037caf8e26fa0b714e96e64d50dfa9 Reviewed-on: https://boringssl-review.googlesource.com/5000 Reviewed-by: Adam Langley <agl@google.com>
diff --git a/crypto/digest/digest_test.cc b/crypto/digest/digest_test.cc index dcb569c..6a6113d 100644 --- a/crypto/digest/digest_test.cc +++ b/crypto/digest/digest_test.cc
@@ -233,6 +233,16 @@ return true; } +static int TestGetters() { + if (EVP_get_digestbyname("RSA-SHA512") == NULL || + EVP_get_digestbyname("sha512WithRSAEncryption") == NULL || + EVP_get_digestbyname("nonsense") != NULL) { + return false; + } + + return true; +} + int main(void) { CRYPTO_library_init(); ERR_load_crypto_strings(); @@ -244,6 +254,10 @@ } } + if (!TestGetters()) { + return 1; + } + printf("PASS\n"); return 0; }
diff --git a/crypto/digest/digests.c b/crypto/digest/digests.c index c086c84..f5eda36 100644 --- a/crypto/digest/digests.c +++ b/crypto/digest/digests.c
@@ -57,6 +57,7 @@ #include <openssl/digest.h> #include <assert.h> +#include <string.h> #include <openssl/md4.h> #include <openssl/md5.h> @@ -256,25 +257,33 @@ struct nid_to_digest { int nid; const EVP_MD* (*md_func)(void); + const char *short_name; + const char *long_name; }; static const struct nid_to_digest nid_to_digest_mapping[] = { - { NID_md5, EVP_md5 }, - { NID_sha1, EVP_sha1 }, - { NID_sha224, EVP_sha224 }, - { NID_sha256, EVP_sha256 }, - { NID_sha384, EVP_sha384 }, - { NID_sha512, EVP_sha512 }, - { NID_md5_sha1, EVP_md5_sha1 }, - { NID_dsaWithSHA, EVP_sha1 }, - { NID_dsaWithSHA1, EVP_sha1 }, - { NID_ecdsa_with_SHA1, EVP_sha1 }, - { NID_md5WithRSAEncryption, EVP_md5 }, - { NID_sha1WithRSAEncryption, EVP_sha1 }, - { NID_sha224WithRSAEncryption, EVP_sha224 }, - { NID_sha256WithRSAEncryption, EVP_sha256 }, - { NID_sha384WithRSAEncryption, EVP_sha384 }, - { NID_sha512WithRSAEncryption, EVP_sha512 }, + { NID_md5, EVP_md5, SN_md5, LN_md5 }, + { NID_sha1, EVP_sha1, SN_sha1, LN_sha1 }, + { NID_sha224, EVP_sha224, SN_sha224, LN_sha224 }, + { NID_sha256, EVP_sha256, SN_sha256, LN_sha256 }, + { NID_sha384, EVP_sha384, SN_sha384, LN_sha384 }, + { NID_sha512, EVP_sha512, SN_sha512, LN_sha512 }, + { NID_md5_sha1, EVP_md5_sha1, SN_md5_sha1, LN_md5_sha1 }, + { NID_dsaWithSHA, EVP_sha1, SN_dsaWithSHA, LN_dsaWithSHA }, + { NID_dsaWithSHA1, EVP_sha1, SN_dsaWithSHA1, LN_dsaWithSHA1 }, + { NID_ecdsa_with_SHA1, EVP_sha1, SN_ecdsa_with_SHA1, NULL }, + { NID_md5WithRSAEncryption, EVP_md5, SN_md5WithRSAEncryption, + LN_md5WithRSAEncryption }, + { NID_sha1WithRSAEncryption, EVP_sha1, SN_sha1WithRSAEncryption, + LN_sha1WithRSAEncryption }, + { NID_sha224WithRSAEncryption, EVP_sha224, SN_sha224WithRSAEncryption, + LN_sha224WithRSAEncryption }, + { NID_sha256WithRSAEncryption, EVP_sha256, SN_sha256WithRSAEncryption, + LN_sha256WithRSAEncryption }, + { NID_sha384WithRSAEncryption, EVP_sha384, SN_sha384WithRSAEncryption, + LN_sha384WithRSAEncryption }, + { NID_sha512WithRSAEncryption, EVP_sha512, SN_sha512WithRSAEncryption, + LN_sha512WithRSAEncryption }, }; const EVP_MD* EVP_get_digestbynid(int nid) { @@ -293,3 +302,19 @@ const EVP_MD* EVP_get_digestbyobj(const ASN1_OBJECT *obj) { return EVP_get_digestbynid(OBJ_obj2nid(obj)); } + +const EVP_MD *EVP_get_digestbyname(const char *name) { + unsigned i; + + for (i = 0; i < sizeof(nid_to_digest_mapping) / sizeof(struct nid_to_digest); + i++) { + const char *short_name = nid_to_digest_mapping[i].short_name; + const char *long_name = nid_to_digest_mapping[i].long_name; + if ((short_name && strcmp(short_name, name) == 0) || + (long_name && strcmp(long_name, name) == 0)) { + return nid_to_digest_mapping[i].md_func(); + } + } + + return NULL; +}
diff --git a/include/openssl/digest.h b/include/openssl/digest.h index 1b07e84..2ea4ec4 100644 --- a/include/openssl/digest.h +++ b/include/openssl/digest.h
@@ -204,6 +204,10 @@ * compatibility with OpenSSL. */ OPENSSL_EXPORT int EVP_add_digest(const EVP_MD *digest); +/* EVP_get_cipherbyname returns an |EVP_MD| given a human readable name in + * |name|, or NULL if the name is unknown. */ +OPENSSL_EXPORT const EVP_MD *EVP_get_digestbyname(const char *); + /* Digest operation accessors. */