Added winpr_md_type_from_string and winpr_md_type_to_string
authorArmin Novak <armin.novak@thincast.com>
Wed, 12 Feb 2020 12:27:30 +0000 (13:27 +0100)
committerakallabeth <akallabeth@users.noreply.github.com>
Fri, 6 Mar 2020 10:37:35 +0000 (11:37 +0100)
winpr/include/winpr/crypto.h
winpr/libwinpr/crypto/cipher.c
winpr/libwinpr/crypto/hash.c

index 213b97b..67c2d99 100644 (file)
@@ -671,6 +671,9 @@ extern "C"
 {
 #endif
 
+       WINPR_API WINPR_MD_TYPE winpr_md_type_from_string(const char* name);
+       WINPR_API const char* winpr_md_type_to_string(WINPR_MD_TYPE md);
+
        WINPR_API WINPR_HMAC_CTX* winpr_HMAC_New(void);
        WINPR_API BOOL winpr_HMAC_Init(WINPR_HMAC_CTX* ctx, WINPR_MD_TYPE md, const BYTE* key,
                                       size_t keylen);
index 09e98bf..5fbe59b 100644 (file)
@@ -128,7 +128,7 @@ void winpr_RC4_Free(WINPR_RC4_CTX* ctx)
  */
 
 #ifdef WITH_OPENSSL
-extern const EVP_MD* winpr_openssl_get_evp_md(int md);
+extern const EVP_MD* winpr_openssl_get_evp_md(WINPR_MD_TYPE md);
 #endif
 
 #ifdef WITH_MBEDTLS
@@ -681,7 +681,7 @@ int winpr_Cipher_BytesToKey(int cipher, int md, const BYTE* salt, const BYTE* da
 #if defined(WITH_OPENSSL)
        const EVP_MD* evp_md;
        const EVP_CIPHER* evp_cipher;
-       evp_md = winpr_openssl_get_evp_md(md);
+       evp_md = winpr_openssl_get_evp_md((WINPR_MD_TYPE)md);
        evp_cipher = winpr_openssl_get_evp_cipher(cipher);
        return EVP_BytesToKey(evp_cipher, evp_md, salt, data, datal, count, key, iv);
 #elif defined(WITH_MBEDTLS)
index cae4991..8265b2e 100644 (file)
  */
 
 #ifdef WITH_OPENSSL
-const EVP_MD* winpr_openssl_get_evp_md(int md)
+const EVP_MD* winpr_openssl_get_evp_md(WINPR_MD_TYPE md)
 {
-       const EVP_MD* evp = NULL;
-
-       switch (md)
-       {
-               case WINPR_MD_MD2:
-                       evp = EVP_get_digestbyname("md2");
-                       break;
-
-               case WINPR_MD_MD4:
-                       evp = EVP_get_digestbyname("md4");
-                       break;
-
-               case WINPR_MD_MD5:
-                       evp = EVP_get_digestbyname("md5");
-                       break;
-
-               case WINPR_MD_SHA1:
-                       evp = EVP_get_digestbyname("sha1");
-                       break;
-
-               case WINPR_MD_SHA224:
-                       evp = EVP_get_digestbyname("sha224");
-                       break;
-
-               case WINPR_MD_SHA256:
-                       evp = EVP_get_digestbyname("sha256");
-                       break;
-
-               case WINPR_MD_SHA384:
-                       evp = EVP_get_digestbyname("sha384");
-                       break;
-
-               case WINPR_MD_SHA512:
-                       evp = EVP_get_digestbyname("sha512");
-                       break;
-
-               case WINPR_MD_RIPEMD160:
-                       evp = EVP_get_digestbyname("ripemd160");
-                       break;
-               case WINPR_MD_SHA3_224:
-                       evp = EVP_get_digestbyname("sha3_224");
-                       break;
-               case WINPR_MD_SHA3_256:
-                       evp = EVP_get_digestbyname("sha3_256");
-                       break;
-               case WINPR_MD_SHA3_384:
-                       evp = EVP_get_digestbyname("sha3_384");
-                       break;
-               case WINPR_MD_SHA3_512:
-                       evp = EVP_get_digestbyname("sha3_512");
-                       break;
-               case WINPR_MD_SHAKE128:
-                       evp = EVP_get_digestbyname("shake128");
-                       break;
-               case WINPR_MD_SHAKE256:
-                       evp = EVP_get_digestbyname("shake256");
-                       break;
-               default:
-                       break;
-       }
-
-       return evp;
+       const char* name = winpr_md_type_to_string(md);
+       if (!name)
+               return NULL;
+       return EVP_get_digestbyname(name);
 }
 #endif
 
@@ -159,6 +101,51 @@ mbedtls_md_type_t winpr_mbedtls_get_md_type(int md)
 }
 #endif
 
+struct hash_map
+{
+       const char* name;
+       WINPR_MD_TYPE md;
+};
+static const struct hash_map hashes[] = { { "md2", WINPR_MD_MD2 },
+                                             { "md4", WINPR_MD_MD4 },
+                                             { "md5", WINPR_MD_MD5 },
+                                             { "sha1", WINPR_MD_SHA1 },
+                                             { "sha224", WINPR_MD_SHA224 },
+                                             { "sha256", WINPR_MD_SHA256 },
+                                             { "sha384", WINPR_MD_SHA384 },
+                                             { "sha512", WINPR_MD_SHA512 },
+                                             { "sha3_224", WINPR_MD_SHA3_224 },
+                                             { "sha3_256", WINPR_MD_SHA3_256 },
+                                             { "sha3_384", WINPR_MD_SHA3_384 },
+                                             { "sha3_512", WINPR_MD_SHA3_512 },
+                                             { "shake128", WINPR_MD_SHAKE128 },
+                                             { "shake256", WINPR_MD_SHAKE256 },
+                                             { NULL, WINPR_MD_NONE } };
+
+WINPR_MD_TYPE winpr_md_type_from_string(const char* name)
+{
+       const struct hash_map* cur = hashes;
+       while (cur->name)
+       {
+               if (_stricmp(cur->name, name) == 0)
+                       return cur->md;
+               cur++;
+       }
+       return WINPR_MD_NONE;
+}
+
+const char* winpr_md_type_to_string(WINPR_MD_TYPE md)
+{
+       const struct hash_map* cur = hashes;
+       while (cur->name)
+       {
+               if (cur->md == md)
+                       return cur->name;
+               cur++;
+       }
+       return NULL;
+}
+
 WINPR_HMAC_CTX* winpr_HMAC_New(void)
 {
        WINPR_HMAC_CTX* ctx = NULL;