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/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;
+}